<?php
$pgMod="marksheet";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
if($_POST){
	//echo "<pre>";//print_r($_POST);echo "<hr/>";
	$db->delete("exammarks","where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$_POST["idno"]." and session='".$_SESSION["admin"]["session"]."'");
	foreach($_POST["type"] as $type){
		$newExam=array();
		$newExam["type"]=$type;$i=0;
		foreach($_POST["subject"] as $subject){
			$newExam["subject"]=$subject;
			$newExam["idno"]=$_POST["idno"];
			$newExam["class"]=$_POST["class"];
			$newExam["total"]=$_POST[$subject."_".$type][0];
			$newExam["obtain"]=$_POST[$type."_student"][$i];
			$newExam["school_id"]=$_SESSION["admin"]["school_id"];
$newExam["added_by"]=$_SESSION["admin"]["uid"];
			$newExam["session"]= $_SESSION["admin"]["session"];
			$db->insertAry("exammarks",$newExam);
			//print_r($newExam);
			$i++;
		}
		$i++;
	}
	//exit;
	redirect("?page_id=marksheet&action=".$_GET["action"]);
}
if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}

if(isset($_GET['id']) && trim($_GET['id']) !='')
{
	error_reporting(55);
	$_REQUEST["class"]=$db->getVal("select class from student where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$_GET["id"]);
	$exam=$db->getVal("select idno from student where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."' limit 0,1");
	$examtype=$db->getRows("select name,id,total from examterm where school_id='".$_SESSION["admin"]["school_id"]."' order by lorder");
	//echo $db->getLastQuery();print_r($examtype);
	if(($exam)!=""){
	?>
	<form method="post" action="" id="printdiv">
			<input type="hidden" name="class" value="<?php echo $_REQUEST["class"] ?>" />
            <input type="hidden" name="idno" value="<?php echo $_GET["id"] ?>" />
			<div class="row-fluid">
				<div class="span12">
					<div class="top-bar">
						<h3><i class="icon-cog"></i> Exam type detail</h3>
						<div>
                        <?php if($_GET["type"]=="view"){?>
	                        <button type="button" name="print" onclick="printDiv('printdiv')"  class="btn btn-primary"> Print</button>
                        <?php } ?>
								<button type="submit" name="fee" onclick="createExam()" class="btn btn-primary"> Save</button>
						</div>
					</div>
					<div class="well">
                    <?php $aryForm=$db->getRow("SELECT * FROM student WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'"); ?>
                        <div class="span3">
                            <ul class="unstyled">
                              <li><strong class="st">id no</strong> : <?php echo $aryForm["idno"] ?></li>
                              <li><strong class="st">DOA</strong> : <?php echo $aryForm["doa"] ?></li>
                            </ul>
                        </div>
                        <div class="span2">
                            <ul class="unstyled">
                              <li><strong class="st">Class</strong> : <?php echo $aryForm["class"] ?></li>
                              <li><strong class="st">Father</strong> : <?php echo $aryForm["fname"] ?></li>
                            </ul>
                        </div>
                        <div class="span2">
                            <ul class="unstyled">
                              <li><strong class="st">Roll no</strong> : <?php echo $aryForm["rollno"] ?></li>
                              <li><strong class="st">Subling</strong> : <?php echo $aryForm["sibling"] ?></li>
                            </ul>
                        </div>
                        <div class="span3">
                            <ul class="unstyled">
                              <li><strong class="st">Name</strong> : <?php echo $aryForm["name"] ?></li>
                              <li><strong class="st">DOB</strong> : <?php echo $aryForm["dob"] ?></li>
                            </ul>
                        </div>
                        <div class="span2">
                            <?php if($aryForm["photo"]!=""){
                                ?><img src="media/photo/<?php echo $aryForm["photo"]; ?>" style="height:70px" /><?php
                            }?>
                        </div>
					<table class="table">
					<tr>
						<td style="border-right:2px solid #FA0606"></td>
						<?php 
						
						if(is_array($examtype) && count($examtype)>0){
							foreach($examtype as $eytpe){ //echo "<pre>";print_r($eytpe);echo "</pre>";?>
							<td style="border-right:2px solid #FA0606">
                            <input type="hidden" name="type[]" style="width:80px" class="total" value="<?php echo $eytpe[0] ?>" placeholder="Exam type" /><?php echo $eytpe[0] ?><br />
                            Total /Obtain (Grade)
</td>
						<?php }
						}?>
					</tr>
					<?php $x=0;
					$detail=$db->getRows("select * from subject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' and session='".$_SESSION["admin"]["session"]."'");
					foreach($detail as $d){
					?><tr>
							<td style="border-right:2px solid #FA0606"><?php echo ucfirst($d["subject"]) ?><input type="hidden" name="subject[]" value="<?php echo $d["subject"] ?>" /></td>
							<?php 
							for($i=1; $i<=count($examtype);$i++){ 
							$examResultData=$db->getRow("select * from exammarks where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."' and type='".$examtype[$i-1][0]."' and subject='".$d["subject"]."'");
							?>
							<td style="border-right:2px solid #FA0606">
                            <input readonly="readonly" type="hidden" style="width:40px" name="<?php echo $d["subject"]."_".$examtype[$i-1][0];?>[]" class="<?php echo $examtype[$i-1][0];?>_total_marks total" value="<?php
							echo $total=$db->getVal("select total from examterm where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$examtype[$i-1][1]."'"); ?>" placeholder="Total Maks" /><?php echo $total; ?>/
							 <?php if($_GET["type"]=="view")echo (int)$examResultData["obtain"];?>
                             <input required type="<?php if($_GET["type"]=="view") echo "hidden";else echo "text";?>" onkeyup="getTotal('<?php echo $d["subject"]?>','<?php echo $examtype[$i-1][0];?>')" style="width:70px" name="<?php echo $examtype[$i-1][0];?>_student[]" class="onlyZero obtain <?php echo $examtype[$i-1][0];?>_obtain_marks" value="<?php echo (int)$examResultData["obtain"]; ?>" placeholder="Obtain Maks" />
                             (<?php echo FindGrade((int)$examResultData["obtain"],$total); ?>)
                             </td>
							<?php }$x++; ?>
						</tr>
					<?php
					}
					?>
                    <tr>
                    	<td style="border-right:2px solid #FA0606">Total</td>
                        <?php 
						for($i=1; $i<=count($examtype);$i++){ ?>
                        	<td style="border-right:2px solid #FA0606">
                            <script type="text/javascript">
							$(document).ready(function(){
								getTotal("0","<?php echo $examtype[$i-1][0];?>");
								onlyNumber("<?php echo $examtype[$i-1][0]; ?>_obtain_marks")
							})
                            </script>
                            <input type="hidden" style="width:70px" id="<?php echo $examtype[$i-1][0];?>_alltotal" name="<?php echo $examtype[$i-1][0];?>_total[]" class="total" value="" placeholder="total Maks" /><span id="<?php echo $examtype[$i-1][0];?>_alldivtotal"></span>/
                            	<input type="hidden" style="width:70px" id="<?php echo $examtype[$i-1][0];?>_allobtain" name="<?php echo$examtype[$i-1][0];?>_total[]" class="total" value="" placeholder="total obtain" />
                                <span id="<?php echo $examtype[$i-1][0];?>_alldivobtain"></span>
                            </td>
                            
						<?php } ?>
                    </tr>
                    <tr>
                    	<td style="border-right:2px solid #FA0606">Percentage</td>
                        <?php 
						for($i=1; $i<=count($examtype);$i++){ ?>
                        <td style="border-right:2px solid #FA0606">
                        <input type="hidden" style="width:70px" id="<?php echo $examtype[$i-1][0];?>_percentage" name="<?php echo$examtype[$i-1][0];?>_per[]" class="per" value="" placeholder="total obtain" />
                        <span id="<?php echo $examtype[$i-1][0];?>_divpercentage"></span>
                        </td>
                        <?php }?>
                    </tr>
					</table>
                    Part 2 : <strong>Co-scholastic areas</strong>(<em>to be assessted on a 5 point scale once in a session</em>)<br />
                    2 (A). Life Skills<br />
                    <table  class="table">
                                <thead>
                                	<tr>
                                    	<th>S.No.</th>
                                        <th>Descriptive Indicators</th>
                                        <th>Grade</th>
                                    </tr>
                                </thead>
                                <tbody id="tbodyskill">
								<?php
					 $lifeskill=$db->getRows("select * from criteria where school_id='".$_SESSION["admin"]["school_id"]."' and type='skill'");
					 echo $db->getErMsg(); $i=1;
					 //echo"<pre>";print_r($all_product);echo"</pre>";
					  if(is_array($lifeskill) && count($lifeskill))
                      {
                        foreach($lifeskill as $iSkill)
                        {
					 ?>
                      <tr>
                      	<td>(<?php echo $i; ?>)</td>
                        <td ><input class="input-block-level" type="hidden" value="skill" name="type[]">
                        <?php echo $iSkill["name"]; ?></td>
                        <td><input type="text" placeholder="Enter Grade" class="input-block-level" name="grade[]"></td>
                      </tr>
                     <?php $i++;
					 	} 
					  }
					 ?>
                     </tbody></table>
                      * Descriptive Indicators are statements used to describe each learner.<br />
                      
                      2(B). Work Education<br />
                      <table class="table">
                                <thead>
                                	<tr>
                                        <th>Descriptive Indicators</th>
                                        <th>Grade</th>
                                    </tr>
                                </thead>
                                <tbody>
                                	<tr>
                                    	<td><textarea name="work"></textarea></td>
                                        <td><input type="text" /></td>
                                    </tr>
                      </tbody>
                      </table>
				</div>
			</div>
			</div>
		</form>   
	<?php
	}else{
		echo '<div class="alert alert-error">
			  <a class="close" data-dismiss="alert">×</a>
			  <i class="icon icon-warning-sign"></i> <b>Notice</b> Please create Subject for this class. click <a href="?page_id=marksheet&action='.$_GET["action"].'">here</a> to search again</i>
			</div>';
	}
}
else{
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("page_id"=>$pgMod,"action"=>"add");
	if($_GET["action"]=="add")
	$aryList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and issue_TC=0 order by name");
	else
	$aryList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and idno in(select distinct(idno) from exammarks) and session='".$_SESSION["admin"]["session"]."' and status=1 and issue_TC=0 order by name");
	/*if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>SerialNo</th>
                    <th>Name</th>
                    <th>Class</th>
                    <th>Medium</th>
                    <th>Father</th>
                    <th>Mobile</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['idno'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["name"] ?></td>
                    <td><?php echo $clist["class"] ?></td>
                     <td><?php echo $clist["medium"] ?></td>
                    <td><?php echo $clist["fname"] ?></td>
                    <td><?php echo $clist["mobile"] ?></td>
                    <td>
                     <?php
					 $aryPgAct['term']=$_GET["term"];
					 if($_GET["action"]=="add"){
					 	$aryPgAct['type']="addMarksheet";$aryPgAct['action']="add"; ?>
                         <a href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>" class="confirmation"><button type="button" class="btn btn-danger">add Marksheer</button></a>  
                     <?php }else{ 
					 	 $aryPgAct['type']="view";$aryPgAct['action']="view";$aryPgAct['page_id']="viewmarksheet"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">View <?php echo $_GET["term"] ?> Marksheet</button></a>
                    <?php } ?>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
             <tfoot>
                <tr>
                	<th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th colspan="2"></th>
                </tr>
            </tfoot>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found <?php echo $db->getErMsg(); ?></i>
        </div>
        <?php
	}*/
	
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <div style="position: absolute;width:300px;z-index: 99999;margin: 30px;">
        <select onchange="fetchByClass()" class="span6" name="class" id="class"  <?php if($checkPay!="0" && $pgAct=="edit")echo 'disabled="disabled"'; ?> >
									<option value="">Class</option>
                                    <?php 
										$class=$db->getRows("select * from class where school_id='".$_SESSION["admin"]["school_id"]."'");
										foreach($class as $cla){
											?>
											<option <?php if($cla["class"]==$aryForm["class"])echo 'selected';?> value="<?php echo $cla["class"] ?>">
												<?php echo $cla["class"] ?>
                                            </option>
											<?php
								
										}
									?>
								</select>
          <select onchange="fetchByClass()" class="span6" name="section" id="section" <?php if($checkPay!="0" && $pgAct=="edit")echo 'disabled="disabled"'; ?> >
                <option value="">Section</option>
                <?php 
                    $class=$db->getRows("select * from section where school_id='".$_SESSION["admin"]["school_id"]."'");
                    foreach($class as $cla){
                        ?>
                        <option value="<?php echo $cla["name"] ?>">
                            <?php echo $cla["name"] ?>
                        </option>
                        <?php
            
                    }
                ?>
            </select></div>
        <input type="hidden" id="tabletype" value="exam_data" />
        <table class="table" id="ajaxtable">
            <thead>
                <tr>
                    <th>Serial No</th>
                    <th>Name</th>
                    <th>Class</th>
                    <th>Medium</th>
                    <th>Father Name</th>
                    <th>Mobile</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tfoot>
            </tfoot>
            <tbody>
           
            </tbody>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}?>
	</div></div></div>
	<?php
}
?>
<!-- / Forms -->
<script>
$(document).ready(function(e) {
    var oTable = $('#ajaxtable').dataTable( {
		"bProcessing": true,
		'sPaginationType': 'full_numbers',
		"sAjaxSource": 'ExamCBSE/ajax.php?term=<?php echo $_GET["term"] ?>&action=<?php echo $_GET["action"] ?>&type='+$('#tabletype').val(),
		'iDisplayLength': 5,
		"oLanguage": {
			"sSearch": "Search by any columns:"
		}
	});
});
function fetchByClass(id){
	var name=$("#class").val();
	var section=$("#section").val();
	//alert(name+section);
	if(name!=""){
		var url='ExamCBSE/ajax.php?term=<?php echo $_GET["term"] ?>&action=<?php echo $_GET["action"] ?>&class='+name+'&type='+$('#tabletype').val();
		if(section!=""){
			url+='&section='+section;
		}
		 oTable = $('#ajaxtable').dataTable( {
			"bProcessing": true,
			'sPaginationType': 'full_numbers',
			"bServerSide": false,
			"bDestroy":true,
			"sAjaxSource": url,
			'iDisplayLength': 5,
			"oLanguage": {
				"sSearch": "Search by any columns:"
			}
		});
	}else{
		var oTable = $('#ajaxtable').dataTable( {
			"bProcessing": true,
			'sPaginationType': 'full_numbers',
			"bServerSide": false,
			"bDestroy":true,
			"sAjaxSource": 'ExamCBSE/ajax.php?term=<?php echo $_GET["term"] ?>&action=<?php echo $_GET["action"] ?>&type='+$('#tabletype').val(),
			'iDisplayLength': 5,
			"oLanguage": {
				"sSearch": "Search by any columns:"
			}
		});
	}
}
function onlyNumber(name){
	$('.'+name).keydown(function(event) {
		if(event.shiftKey)
		{
			event.preventDefault();
		}
   		if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 190 || event.keyCode == 110 )   {
   		}
   		else {
        if (event.keyCode < 95) {
			if (event.keyCode < 48 || event.keyCode > 57) {
                event.preventDefault();
			}
        }
        else {
              if (event.keyCode < 96 || event.keyCode > 105) {
                  event.preventDefault();
              }
        }
      }
   });
}

function getTotal(subject,type){var alltotal=0;var allobtain=0;
	$("."+type+"_total_marks").each(function(){
		alltotal = parseInt(alltotal) + parseInt($(this).val());
	});
	$("."+type+"_obtain_marks").each(function(){
		allobtain = parseInt(allobtain) + parseInt($(this).val());
	});
	$("#"+type+"_alltotal").val(parseInt(alltotal));
	$("#"+type+"_alldivtotal").html(parseInt(alltotal));
	$("#"+type+"_allobtain").val(parseInt(allobtain));
	$("#"+type+"_alldivobtain").html(parseInt(allobtain));
	getAverage($("#"+type+"_alltotal").val(),$("#"+type+"_allobtain").val(),type);
} 

function getAverage(total,obtain,type){
	var average=0;
	average=(100*obtain)/total;
	$("#"+type+"_percentage").val(average+"%");
	$("#"+type+"_divpercentage").html(average+"%");
}
</script>