<?
include('auth.php');
include('common.php');

//needed for proper pagination
if (!isset($_GET['p']))
	$p = 1;
else
	$p = $_GET['p'];
$SQL_limit = buildLimitClause($p);


$SQL_x="";

//active state
if (!isset($_GET['active']))
	$act=1;
else
	$act=$_GET['active'];
	
//search term
$s = secure_user_input($_GET['s']);
if ($s)
{
	$SQL_x .= " AND (firstname LIKE '%".$s."%' OR lastname LIKE '%".$s."%')";
}


//council
$c = $_GET['council'];

//coords/tutor can ONLY see their council's learners
if ($_SESSION['ulevel'] == $_COORD || $_SESSION['ulevel'] == $_TUTOR )
{	
	$c = $_SESSION['council_id'];
}


if ($c && $c != -1)
{
	$SQL_x .= " AND council_id=".$c;
}

//tutor can only see learners associated to him/her
if ($_SESSION['ulevel'] == $_TUTOR )
{	
	$SQL_x .= " AND id IN (SELECT learner_id
						FROM `tutor_learner`
						WHERE tutor_id =".$_SESSION['user_id'].")";
}
		
$SQL="SELECT count(id) as rcount FROM learner WHERE active=".$act.$SQL_x;
$results = llnb_query($SQL);
$rowcount = llnb_fetch_assoc($results);

$SQL="SELECT * FROM learner WHERE active=".$act.$SQL_x.$SQL_limit;
$results = llnb_query($SQL);
while($row = llnb_fetch_assoc($results))
{
	$TRs .= "  
		<tr>
		  <td>".$row['llnb_id']."</td>
		  <td><a href=\"learner_overview.php?lid=".$row['id']."\" class=\"btnsml btn\">".$row['firstname']." ".$row['lastname']."</a></td>
		  <td>".dispPhone($row['homephone'])."</td>
		  <td><a class=\"btnsml_m\" rel=\"{handler:'iframe',size:{x:550,y:400}}\" href=\"learner_time_m.php?lid=".$row['id']."&c=".$c."\">Add Session</a></td>
		 </tr>";

}


?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
  <meta charset="utf-8">

  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

  <title>LLNB System</title>
  <meta name="description" content="">

  <!-- Mobile viewport optimized: h5bp.com/viewport -->
  <meta name="viewport" content="width=device-width, maximum-scale=1.0">

  <!-- Place favicon.ico and apple-touch-icon.png in the root directory: mathiasbynens.be/notes/touch-icons -->
  <LINK REL="SHORTCUT ICON" HREF="favicon.ico">

  	<link rel="stylesheet" href="css/style.css">
  	 <link rel="stylesheet" href="css/llnb-style.css">
	 <script src="js/libs/modernizr-2.5.3.min.js"></script>	
	 
	<link href="scripts/SqueezeBox.css" rel="stylesheet" type="text/css" />
	<script src="scripts/mootools.js" type="text/javascript" ></script>
	<script src="scripts/SqueezeBox.js" type="text/javascript"></script>

	
	<script type="text/javascript">
	function OnChange(dropdown)
	{
		var myindex  = dropdown.selectedIndex
		var SelValue = dropdown.options[myindex].value
		self.location.href = 'learner_list.php?s=<?=$s;?>&active=<?=$act;?>&council='+SelValue;
	}
	</script>
	
	
	<script type="text/javascript">
		
		window.addEvent('domready', function()
		{
			SqueezeBox.assign($$('a.btnsml_m'), {
				parse: 'rel'
			});
		});
		
</script>
</head>


<body>
<div id="wrap">
  <header><a href="index.php" class="btn lfr" style="position:relative; top: 15px; right: 0;">Logout</a>
	<h1>Laubach Literacy - <?=uType();?></h1> 
	<h3><?=fetchCouncilName($_SESSION['council_id']);?></h3>
	<nav id="menu" class="clearfix navmenu"><a href="home.php" class="btn lfl">Home</a></nav>
  </header>
  <div role="main" id="main" class="clearfix">
  	<div id="location">
    <?
		if ($_SESSION['ulevel'] == $_EXECDIR || $_SESSION['ulevel'] == $_COORD )
	  		echo "<a href=\"learner_personalinfo.php?mode=new\" class=\"btn lfr\" style=\"position:relative; top: 0; right: 10px;\">Add Learner</a>";
  	
  	?>

    </div> <!-- end of location -->
    
<div>
      <H2>Learner List</H2>

    <?
	if ($_SESSION['ulevel'] == $_EXECDIR )
	{
   	 echo 
   	 "<select name=\"council\" class=\"selectcouncils\" title=\"Select Council\"  onchange='OnChange(this);'>
  	   		".buildCouncilDropdownALLoption($c)." 
   	 </select>";
   	}
    ?>
    
<form name="searchform" id="searchform" action="learner_list.php" method="GET">
<input name="s" type="text" class="search" id="search" title="Search" value="<?= $_GET['s'];?>" >
<input type="submit" class="searchbtn searchmove" id="searchbtn" value="Search">
<input type="button" class="searchbtn" id="clearbtn" onClick="self.document.searchform.s.value=''; self.document.searchform.submit();" value="Clear Search">

<input type="hidden" name="active" value="<?=$act;?>">
<input type="hidden" name="council" value="<?=$_GET['council'];?>">
 </form>


    <div style="clear:both;"></div>
    <table  border="0" cellspacing="0" cellpadding="0" class="infotable">
      <tr>
        <th scope="col">ID</th>
        <th scope="col">Name</th>
        <th scope="col">Phone</th>
        <th scope="col">Time</th>
      </tr>
      <?
       echo $TRs;
      ?>
     
   </table>
    
    </div>
    
   <?
   
	//pagination functionality
   $urlstring="s=".$s."&active=".$act."&council=".$c;
   echo constructPagination($rowcount['rcount'], $p, "learner_list.php", $urlstring);

   ?> 
  
  
  </div><!-- end of main -->
  
  <div class="sidecolumn">
  	<?
	if ($_SESSION['ulevel'] != $_TUTOR )
	{
	 	if ($act == 1)
	 	 	echo "<a href=\"learner_list.php?active=0&s=".$s."&council=".$_GET['council']."\" class=\"btn btncnt\">Inactive Learners</a>";
		 else 
	 		echo "<a href=\"learner_list.php?active=1&s=".$s."&council=".$_GET['council']."\" class=\"btn btncnt\">Active Learners</a>";
	}	  
	 ?> 
	 </div>
	 

    <div style="clear:both;"></div>


    
  <footer>
	<?php include('footer.php'); ?>
  </footer>
  

  </div> <!-- end of wrap -->



</body>
</html>