<?php
$pgMod="marksheet";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
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");
	
	$termWhere="";if($_GET["term"]!="final") $termWhere=" and term='".$_GET["term"]."' ";
	$examtype=$db->getRows("select name,id,total from examterm  where school_id='".$_SESSION["admin"]["school_id"]."' $termWhere order by lorder");
	//echo $db->getLastQuery();print_r($examtype);
	if(count($exam)>0){
	?>
    
	<form method="post" action="">
			<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>
	                        <button type="button" name="print" onclick="printDiv('printdiv')"  class="btn btn-primary"> Print</button>
                        </div>
					</div>
					<div class="well" id="printdiv">
                    <?php $aryForm=$db->getRow("SELECT * FROM student WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");$overallTotalGrade=0; ?>
                    <table style="width:100%" border="1" cellpadding="0" cellspacing="0"><tr><td>
                    <strong class="st">Name</strong> : <?php echo $aryForm["name"] ?>
                    </td><td>
                    <strong class="st">Class</strong> : <?php echo $aryForm["class"] ?>
                    </td><td>
                    <strong class="st">Father</strong> : <?php echo $aryForm["fname"] ?>
                    </td>
                    </tr></table>
                    
                     <div style="font-size:18px; text-align:center; clear:both; margin-bottom:20px; margin-top:20px;">Part–I :  Academic Performance: Scholastic Areas </div> 
					<table style="width:100%" border="1" cellpadding="0" cellspacing="0">
					<tr>
                    	
                    	<td rowspan="2" style="border-right:2px solid #FA0606">Subject</td>
						<?php if($_GET["term"]=="final"){?> 
                        <td colspan="3" style="border-right:2px solid #FA0606; text-align:center">Term-I</td>
                        <td colspan="3" style="border-right:2px solid #FA0606; text-align:center">Term-II</td>
                        <td colspan="3" style="border-right:2px solid #FA0606; text-align:center">(Term I+II)</td>
                        <?php }elseif($_GET["term"]=="term1"){ ?>
                        <td colspan="2" style="border-right:2px solid #FA0606; text-align:center">Term-I</td>
                        <td colspan="2" style="border-right:2px solid #FA0606; text-align:center">(Overall Grade)</td>
                        <?php }elseif($_GET["term"]=="term2"){ ?>
                        <td colspan="2" style="border-right:2px solid #FA0606; text-align:center">Term-II</td>
                        <td colspan="2" style="border-right:2px solid #FA0606; text-align:center">(Overall Grade)</td>
                        <?php } ?>
                        <td></td>
                    </tr>
                    <tr>
						<?php 
						
						if(is_array($examtype) && count($examtype)>0){
							 $finalObtain=0;$finalTotal=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] ?>
</td>
						<?php }
						}?>
                        <?php if($_GET["term"]=="final"){?> 
                        <td style="border-right:2px solid #FA0606">FA1+FA2+FA3+FA4</td>
                        <td style="border-right:2px solid #FA0606">SA1+SA2</td>
                        <td style="border-right:2px solid #FA0606">Grade</td>
                        <?php }elseif($_GET["term"]=="term1"){ ?>
                        <td style="border-right:2px solid #FA0606">FA+SA</td>
						<?php }elseif($_GET["term"]=="term2"){ ?>
                        <td style="border-right:2px solid #FA0606">FA+SA</td>
						<?php } ?>
                        <td>Grade Point<br />(GP)</td>
					</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){ $fsAry=array();$final="";
					?><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 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" />
                             <input required type="hidden" 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
							 $fs=$examtype[$i-1][0];
							 $fsAry[$fs."_Obtain"]=(int)$examResultData["obtain"];
							 $fsAry[$fs."_Total"]=$total;
							  echo FindGrade((int)$examResultData["obtain"],$total); ?>
                             </td>
							<?php }$x++; ?>
                            <?php if($_GET["term"]=="final"){?> 
                            <td style="border-right:2px solid #FA0606"><?php $faObtain=$fsAry["FA1_Obtain"]+$fsAry["FA2_Obtain"]+$fsAry["FA3_Obtain"]+$fsAry["FA4_Obtain"];$faTotal=$fsAry["FA1_Total"]+$fsAry["FA2_Total"]+$fsAry["FA3_Total"]+$fsAry["FA4_Total"];echo FindGrade($faObtain,$faTotal); ?></td>
                            <td style="border-right:2px solid #FA0606"><?php $saObtain=$fsAry["SA1_Obtain"]+$fsAry["SA2_Obtain"];$saTotal=$fsAry["SA1_Total"]+$fsAry["SA2_Total"];echo FindGrade($saObtain,$saTotal); ?></td>
                            <td style="border-right:2px solid #FA0606">
                            <?php 
							$AllObtain = $saObtain + $faObtain;
							$AllTotal = $saTotal + $faTotal;
							echo $final=FindGrade($AllObtain,$AllTotal);//."/".$AllObtain."-".$AllTotal;
							$finalObtain+=$AllObtain;
							$finalTotal+=$AllTotal;
                            ?>
                            </td>
                            <?php }elseif($_GET["term"]=="term1"){ ?>
                            <td style="border-right:2px solid #FA0606"><?php 
								$faObtain=$fsAry["FA1_Obtain"]+$fsAry["FA2_Obtain"];
								$faTotal=$fsAry["FA1_Total"]+$fsAry["FA2_Total"];
								$saObtain=$fsAry["SA1_Obtain"];
								$saTotal=$fsAry["SA1_Total"];
								$AllObtain = $saObtain + $faObtain;
								$AllTotal = $saTotal + $faTotal;
								echo $final=FindGrade($AllObtain,$AllTotal);//."/".$AllObtain."-".$AllTotal;
								$finalObtain+=$AllObtain;
								$finalTotal+=$AllTotal;
                            ?></td>
                            <?php }elseif($_GET["term"]=="term2"){ ?>
                            <td style="border-right:2px solid #FA0606"><?php 
								$faObtain=$fsAry["FA3_Obtain"]+$fsAry["FA4_Obtain"];
								$faTotal=$fsAry["FA3_Total"]+$fsAry["FA4_Total"];
								$saObtain=$fsAry["SA2_Obtain"];$saTotal=$fsAry["SA2_Total"];
								$AllObtain = $saObtain + $faObtain;
								$AllTotal = $saTotal + $faTotal;
								echo $final=FindGrade($AllObtain,$AllTotal);//."/".$AllObtain."-".$AllTotal;
								$finalObtain+=$AllObtain;
								$finalTotal+=$AllTotal;
                            ?></td>
                            <?php } ?>
                            <td style="border-right:2px solid #FA0606"><?php $tgrade=$db->getVal("select point from examgrade where grade='".$final."'"); echo number_format((float)$tgrade, 2, '.', ''); $overallTotalGrade+=$tgrade;?></td>
						</tr>
					<?php
					}
					?>
					</table>
					<span class="pull-right">
                    Cumulative Grade Point Average(CGPA) : <?php echo $overallTotalGrade/count($detail); ?>
                    </span>
					<?php
					$type=$db->getRows("select * from criteria where school_id='".$_SESSION["admin"]["school_id"]."' group by type order by id");
					if(is_array($type) && count($type)>0){
						?><div style="font-size:18px; text-align:center; clear:both; margin-top:40px;">Part–2 :  Co-Scholastic Areas </div>
						<table  width="100%" cellpadding="0" cellspacing="0">
						<?php $ijk=0;
						foreach($type as $t){$ijk++;
							?>
                            <tr>
                                <td><br />
<br />
<?php echo $t["type"] ?><br /><br /></td>
                            </tr>
                            <tr>
                                <td>
                                <table style="border-color:#ccc" border="1" width="100%" cellpadding="0" cellspacing="0">
                                        
                                    <tbody id="tbodyskill">
                                    <?php
                                     $lifeskill=$db->getRows("select * from criteria where school_id='".$_SESSION["admin"]["school_id"]."' and type='".$t["type"]."'");
                                     echo $db->getErMsg(); $i=1;
                                     //echo"<pre>";print_r($all_product);echo"</pre>";
                                      if(is_array($lifeskill) && count($lifeskill))
                                      {
										   ?>
										  <tr>
                                          <th>Trait</th><th>Descriptive Indicators</th><th>Grade</th>
                                          </tr>
										  <?php
										  
                                        foreach($lifeskill as $iSkill)
                                        {
											$grade=$db->getVal("select grade from examcriteriarecord where idno='".$_GET['id']."' and term='".$_GET["term"]."' and criteria='".$t["type"]."' and subcriteria='".$iSkill["name"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'"); 
                                     ?>
                                      <tr>
                                        <td width="200px"><?php echo $iSkill["name"]; ?></td>
                                        
                                        <td ><?php echo $db->getVal("select detail from grade where grade='".$grade."' and  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'  and criteria='".$t["type"]."'"); ?></td>
                                        <td width="55px"><?php echo $grade;?></td>
                                      </tr>
                                     <?php $i++;
                                        } 
                                    }
                                     ?>
                             	</tbody>
                                        </table>
                                </td>
                            </tr><?php
						}
						?></table><?php
						echo $db->getErMsg();
					}
					?>
				
                <table width="100%" class="table">
                <tr><td colspan="2">Promoted class.........................</td><td>Result : Pass/Fail</td></tr>
				<tr>
                <td>Teacher signature</td>
                <td>principal signature</td>
                <td>guardian signature</td>
                </tr></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{
	redirect("?page_id=marksheet");
}
?>
<!-- / Forms -->
