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

//propagate the data we need
if ($_GET['lid']) 	$lid= $_GET['lid'];  else 	$lid = $_POST['lid'];
if ($_GET['lgid']) 	$lgid= $_GET['lgid']; else	$lgid = $_POST['lgid'];	
if ($_GET['mode'])	$mode= $_GET['mode']; else	$mode = $_POST['mode'];		


$do_close=0;

if (count($_POST) > 0)
{
	
	//essential skills
	$aosarray = $_POST['eskills'];
	if (count($aosarray)>0)
	{
		foreach ($aosarray as $value)
		{
		    $AREAS_selected[$value]=true;
		}
	}

	$goal = secure_user_input($_POST['goal']);
	$strategy = secure_user_input($_POST['strategy']);
	$knowwhen = secure_user_input($_POST['knowwhen']);
	$d = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
	
	if ($mode == "new")
	{
	
		$SQL="INSERT INTO learnergoals 
			(
			learner_id, 
			goal, 
			strategy, 
			knowwhen,
			start 
			)
			VALUES (
			'".$lid."',
			'".$goal."',
			'".$strategy."' ,
			'".$knowwhen."' ,
			'".$d."'
			)";
	}
	else
	{
		$SQL="UPDATE learnergoals  SET
				learner_id= '".$lid."',
				goal=	  '".$goal."',
				strategy=  '".$strategy."' ,
				knowwhen=  '".$knowwhen."' ,
				start =	  '".$d."'	
				WHERE id=".$lgid;
	}
	$results = llnb_query($SQL);

	if ($mode == "new")
		$currentid = mysql_insert_id();
	else
		$currentid = $lgid; 
	
	//delete records for this goal and update with new records
	$SQL = "DELETE FROM learnergoals_essentialskills WHERE lg_id=".$lgid;
	$results = llnb_query($SQL);

	if (count($AREAS_selected)>0)
	{
		//insert the areas of interest
		foreach ($AREAS_selected as $thekey=>$value)
		{
			$SQL = "INSERT INTO learnergoals_essentialskills
				(lg_id, skill_id)
				VALUES (".$currentid.", ".$thekey.")";
			//echo $SQL."<BR><BR>";
			$results = llnb_query($SQL);
		}
	}
	
	$do_close=1;
}
else if ($_GET['lgid'])
{

	//fetch the learning goal and populate
	$SQL="SELECT *, DAY(start) as theday, MONTH(start) as themonth, YEAR(start) as theyear from learnergoals
		WHERE id=".$_GET['lgid'];
	$results = llnb_query($SQL);
	$row = llnb_fetch_assoc($results);
	
	//fetch e skills
	$AREAS_selected = fetchEssentialSkills($_GET['lgid']);
}
?><head>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/llnb-style.css">


</head>








<style type="text/css">
body { background-color: #fff; background-image:none; padding: 20px;}

div { font-family: sans-serif; color: #222; }

.btn	{ -moz-border-radius: 7px; -webkit-border-radius: 7px; -khtml-border-radius: 7px; border-radius: 7px; border: 1px #222 solid; background-image: url(img/btn-bg-nor.jpg) ; line-height: 25px; padding: 3px 10px 0; text-decoration: none; color: #222; }
.btn:hover	{ border-color: #33C; color: #33c;}

.btnhidden {border-color: #CCC; background-image: none; font-size: .8em;}

.btncnt	{ width: 160px; text-align:center; display: block; margin-bottom: 10px;}

.btnlrg	{width: 300px; padding-left: 20px;}
.btnsml	{ padding: 1px 10px; font-weight: bold;}
.btnpg	{ padding: 2px 4px 1px; font-size: .75em;margin-right: 4px;}

.lfr	{ float: right; margin-right: 10px;}
.lfl	{ float: left; margin-right: 10px;}
form label { display:block; margin-top: 10px; font-size: .8em; color: #555; }
.session { margin-bottom: 15px; padding-bottom: 10px;}
.talrg	{ width: 460px; height: 60px;}

input.ssminp[type="text"] {width: 40px;}
input.sminp[type="text"] {width: 80px;}
input.btn { height: 30px;}


</style>

<body>
<? 
if ($do_close) 
	echo "<script>parent.location.href='learner_learningplan.php?lid=".$lid."';</script>"; 
	$number = rand();
?>

 <div class="sidecolumn">
		<div class="graybox">
			  <h3>Forms</h3>
			  <a href="forms/WOW_portfolio.pdf?=<?php echo $number ?>" target="_blank"  class="btn btncnt">WOW Portfolio</a>
			  <a href="forms/essential_skills.pdf?=<?php echo $number ?>" target="_blank"  class="btn btncnt">Essential Skills</a>
		</div>
 </div>
<div style="width: 500px;margin-bottom: 20px;">



<h2>Add Learning Goal for:<br />
  Learner <?= fetchLearnerName($lid); ?></h2>


<form name="learnergoal" action="learner_learninggoal_m.php" method="post">

<div class="session">
<div style="float:left; display:block; margin-bottom: 15px;">

<label style="display:block;">Start Date:</label>
<select name="month" title="month">
       	<?
       	$mymon = ($_POST['month']) ? stripslashes($_POST['month']) : $row['themonth'];
       	echo buildMonthDropdown($mymon, $_MONTHS_abbr); 
       	?>
</select>
<select name="day" title="date">
       	<?
       	$myday = ($_POST['day']) ? stripslashes($_POST['day']) : $row['theday'];
       	echo buildDayDropdown($myday); 
       	?>
</select>

<select name="year" title="Year">
       	<?
       	$myyear = ($_POST['year']) ? stripslashes($_POST['year']) : $row['theyear'];
       	echo buildFiveYearDropdown($myyear,  date('Y')); 
       	?>
</select>
</div>

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

<div class="input">
        <label>Learning Goal:</label>
        <input name="goal" type="text"   value="<?=$row['goal'];?>" class="lrginp">
</div>

    <div class="input" style="width: 550px;">
        <label>Essential skills, click all that apply.</label>
			<?
					echo buildEssentialSkills($AREAS_selected);
				?>
           
          <div style="clear:both;"></div>    




<div class="input">
        <label>I&rsquo;ll know I can do this when:</label>
        <input name="knowwhen" type="text" value="<?=$row['knowwhen'];?>" class="lrginp">
</div>


<div style="clear:both; "></div>
<label>Strategies:</label>
<textarea name="strategy" cols="" rows="3"  class="talrg"><?=$row['strategy'];?></textarea>

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

<p><input type="submit"  class="btnlfr_m" value="Save &amp; Close">
<a href="#" onclick="parent.SqueezeBox.close();"class="btn lfr">Cancel</a>

<input type="hidden" name="lgid" value="<?=$_GET['lgid'];?>">
<input type="hidden" name="lid" value="<?=$_GET['lid'];?>">
<input type="hidden" name="mode" value="<?=$_GET['mode'];?>">


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