<?   
include('auth.php');
include('common.php');
$update_success=false;
$insert_success=false;

$lid = $_GET['lid'];

$mode="";
$SQL="SELECT * FROM learneredu WHERE learner_id=".$_GET['lid'];
$results = llnb_query($SQL);
$row = llnb_fetch_assoc($results);
if ($row)
	$mode="edit";
else
	$mode="new";


if (count($_POST) > 0) //form has been submitted by user
{
	//areas of interest
	$aosarray = $_POST['areaofdisability'];
	if (count($aosarray)>0)
	{
		foreach ($aosarray as $value)
		{
		    $AREAS_selected[$value]=true;
		}
	}
	
	$hsgrad= secure_user_input($_POST['hsgrad']);
	$lastgrade= secure_user_input($_POST['lastgrade']);
	$edulevel= secure_user_input($_POST['edulevel']);
	$lastattend= secure_user_input($_POST['lastattend']);
	$readprob= secure_user_input($_POST['readprob']);
	$readunderstand= secure_user_input($_POST['readunderstand']);
	$writingprob= secure_user_input($_POST['writingprob']);
	$diagnosed= secure_user_input($_POST['diagnosed']);
	$likereading= secure_user_input($_POST['likereading']);
	$whatread= secure_user_input($_POST['whatread']);

	
	$lid = $_POST['lid'];
	
	if ($mode == 'new')
	{
		$SQL = "INSERT INTO learneredu
		(`learner_id`, 
		`hsgrad`,		
		`lastgrade` ,
		`edulevel` ,
		`lastattend` ,		
		`readprob` ,		
		`readunderstand`,	
		`writingprob` ,		
		`diagnosed` 	,	
		`likereading`,
		`whatread` 	
		)			
		 VALUES 
		(
		'".$lid."',
		'".$hsgrad."',
		 '".$lastgrade."',
		 '".$edulevel."',
		'".$lastattend."',
		 '".$readprob."',
		 '".$readunderstand."',
		 '".$writingprob."',
		 '".$diagnosed."',
		 '".$likereading."',
		 '".$whatread."'
	)";						

		if (!llnb_query($SQL))
			echo "ERROR on INSERT SQL";
		//echo $SQL;
		
		$insert_success=true;
		header("Location: ./learner_overview.php?lid=".$lid);

	}
	else if ($mode="edit" )
	{
		$SQL = "UPDATE learneredu SET
		`hsgrad`=			  '".$hsgrad."',
		`lastgrade` =		   '".$lastgrade."',
		`edulevel` =		   '".$edulevel."',
		`lastattend` =	  '".$lastattend."',
		`readprob` =		   '".$readprob."',
		`readunderstand`=	   '".$readunderstand."',
		`writingprob` =	 '".$writingprob."',	
		`diagnosed` 	=	   '".$diagnosed."',
		`likereading`=		   '".$likereading."',
		`whatread` =		   '".$whatread."'
	
			WHERE learner_id=".$_POST['lid'];

		//echo $SQL;
		$results = llnb_query($SQL);
		$update_success = true;
		header("Location: ./learner_overview.php?lid=".$lid);

	}
	
	$currentid = $_POST['lid'];
	//delete records for this user and update with new records
	$SQL = "DELETE FROM learner_disability WHERE learner_id=".$currentid;
	$results = llnb_query($SQL);

	if (count($AREAS_selected)>0)
	{
		//insert the areas of interest
		foreach ($AREAS_selected as $thekey=>$value)
		{
			$SQL = "INSERT INTO learner_disability
				(learner_id, disability_id)
				VALUES (".$currentid.", ".$thekey.")";
			//echo $SQL."<BR><BR>";
			$results = llnb_query($SQL);
		}
	}
	
}
else if (isset($_GET['lid']) && $mode !="new")
{
	$SQL="SELECT * FROM learneredu WHERE learner_id=".$_GET['lid'];
	$results = llnb_query($SQL);
	$row = llnb_fetch_assoc($results);
	
	//fetch areas of disability
	$AREAS_selected = fetchAreasOfDisability($_GET['lid']);
	//print_r($AREAS_selected);
	
	$hsgrad= secure_user_input($row['hsgrad']);	if ($row['hsgrad'] == 1)	$hsgradyes="checked"; else $hsgradno="checked";

	$lastgrade= secure_user_input($row['lastgrade']);
	$edulevel= secure_user_input($row['edulevel']);
	$lastattend= secure_user_input($row['lastattend']);
	$readprob= secure_user_input($row['readprob']);  if ($row['readprob'] == 1)	$readprobyes="checked"; else $readprobno="checked";
	$readunderstand= secure_user_input($row['readunderstand']);
	
	if ($row['readunderstand'] == 1)
		$readunderstandyes="checked";
	else if ($row['readunderstand'] == 0)
		$readunderstandno="checked";
	else 
		$readunderstandsome="checked";
	
	$writingprob= secure_user_input($row['writingprob']); if ($row['writingprob'] == 1)	$writingprobyes="checked"; else $writingprobno="checked";
	$diagnosed= secure_user_input($row['diagnosed']); if ($row['diagnosed'] == 1)	$diagnosedyes="checked"; else $diagnosedno="checked";
	$likereading= secure_user_input($row['likereading']); if ($row['likereading'] == 1)	$likereadingyes="checked"; else $likereadingno="checked";
	$whatread= secure_user_input($row['whatread']);	
	
	
}
?>



<!doctype html>
<html class="no-js" lang="en"> <!--<![endif]-->

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

<!-- Use the .htaccess and remove these lines to avoid edge case issues.
       More info: h5bp.com/i/378 -->
<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">

<!-- More ideas for your <head> here: h5bp.com/d/head-Tips -->

<script src="js/libs/modernizr-2.5.3.min.js"></script>
</head>
<body >
<div id="wrap"> 
  <!-- Prompt IE 6 users to install Chrome Frame. Remove this if you support IE 6.
       chromium.org/developers/how-tos/chrome-frame-getting-started --> 
  <!--[if lt IE 7]><p class=chromeframe>Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->
  <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>
    <a href="learner_list.php" class="btn lfl">Learner List</a>
    <a href="learner_overview.php?lid=<?=$lid?>" class="btn lfl">Learner Overview</a></nav>
  </header>
  <div role="main" id="main" class="clearfix">
    <div id="location">
      <H2><span>Learner:</span> <?= fetchLearnerName($lid); ?></H2>
    </div>
    <!-- end of location -->
    <div>
    <H2>Education Information</H2>
   	 <form class="midform" name="learnereducationinfo" action="learner_educationinfo.php?lid=<?= $_GET['lid'];?>" method="POST">

    <div class="input">
	    <label>Did you graduate from High School?</label>
	<label class="radio" style="margin-right:15px;">
	<input name="hsgrad" type="radio" value="1" <?=$hsgradyes;?>>Yes</label>
	<label class="radio">
	<input name="hsgrad" type="radio" value="0" <?=$hsgradno;?>>No
	</label>
        </div>
          <div style="clear:both;"></div>

<div class="input  plusleft" style="width: 535px;">
      <label style="display:block;">a. If No, then what was the last grade of school you completed?</label>
      <select name="lastgrade" class="floatingleft">
	<?=  buildGradeDropdown($lastgrade);?>
    </select>
    
        </div><div style="clear:both;"></div>


<div class="input plusleft" style="width: 535px;">
    <label style="display: inline-block;">b. If Yes, then what is the highest level of eduction you have completed?</label>
      <select name="edulevel" class="floatingleft">
	<?= buildEdulevelDropdown($edulevel);?>
    </select>
        </div><div style="clear:both;"></div>

    
<div class="input plusleft" style="width: 535px;">
      <label style="display:block;">c. What year did you last attend school?</label>
      <select name="lastattend" class="floatingleft" title="Client of">
	<?= buildYearDropdown($lastattend, "2002"); ?>
    </select>
    
        </div><div style="clear:both;"></div>

    <div class="input">
	    <label>Do you find reading a problem?</label>
		<label class="radio" style="margin-right:15px;">
		<input name="readprob" type="radio" value="1" <?=$readprobyes;?>>Yes</label> 
		<label class="radio">
		<input name="readprob" type="radio" value="0" <?=$readprobno;?>>No</label>
        </div>
          <div style="clear:both;"></div>
        
    <div class="input">
	    <label>Do you understand what you read?</label>
<label class="radio" style="margin-right:15px;">
<input name="readunderstand" type="radio" value="1" <?=$readunderstandyes;?>>Yes</label>
<label class="radio">
<input name="readunderstand" type="radio" value="0" <?=$readunderstandno;?>>No</label>
<label class="radio">
<input name="readunderstand" type="radio" value="2" <?=$readunderstandsome;?>>Sometimes</label>
        </div>
          <div style="clear:both;"></div>
    
    <div class="input">
	    <label>Do you find writing a problem?</label>
<label class="radio" style="margin-right:15px;">
<input name="writingprob" type="radio" value="1" <?=$writingprobyes;?> >Yes</label> 
<label class="radio">
<input name="writingprob" type="radio" value="0" <?=$writingprobno;?> >No
</label>
        </div>
          <div style="clear:both;"></div>
    
    <div class="input">
	    <label>Have you ever been diagnosed with a disability?</label>
<label class="radio" style="margin-right:15px;">
<input name="diagnosed" type="radio" value="1" <?=$diagnosedyes;?>>Yes</label>
<label class="radio">
<input name="diagnosed" type="radio" value="0" <?=$diagnosedno;?>>No</label>
        </div>
          <div style="clear:both;"></div>    
    
    <div class="input plusleft" style="width: 400px;">
        <label>If Yes, then please identify all that apply.</label>
		<?
			echo buildAreasOfDisability($AREAS_selected);
		?>
           
           </div>
          <div style="clear:both;"></div>    
    
    
 		<div class="input">
	    <label>Do you like to read?</label>
<label class="radio" style="margin-right:15px;">
<input name="likereading" type="radio" value="1" <?=$likereadingyes;?>>Yes</label>
<label class="radio">
<input name="likereading" type="radio" value="0" <?=$likereadingno;?>>No</label>
        </div>
          <div style="clear:both;"></div>        
    
 <div class="input" style="width: 400px; margin-bottom: 0px;">
        <label>What would you like to read?</label>
        <input name="whatread" type="text"   value="<?= $row['whatread']; ?>" class="lrginp">
</div>
          <div style="clear:both;"></div>    
                
      <p class="clearfix">
      <input type="submit" class="btn lfr" value="Save &amp; Close"> 
      <a href="learner_overview.php?lid=<?=$lid?>" class="btn lfr">Cancel</a></p>
   <input type="hidden" name="lid" value="<?=$lid;?>">

    </form>
    </div>
  </div> <!-- end of main -->
<aside>
</aside>
    <div style="clear:both;"></div>
<footer>
  <?php include('footer.php'); ?>
</footer>
</div>
<!-- end of wrap --> 

<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline --> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js"><\/script>')</script> 

<!-- scripts concatenated and minified via build script --> 
<script src="js/plugins.js"></script> 
<script src="js/script.js"></script> 
<!-- end scripts -->

</body>
</html>