<style>
.top-bar-minimize { display: none; }
thead, tfoot { display: table-header-group; }
.top-bar+.well, .accordion .accordion-heading+.accordion-body { display: inline-block !important; width: 100% !important; overflow: inherit !important; }
</style>
<script type="text/javascript">
function getType(id){ 
	$.post( "ajax.php", { type: "exmtype" ,class: id } , function( data ) { 
	  	$("#extype").html(data);
	}); 
}
 
function smsDivNoHeader(){ 
	var id=$("#smsTXT").val();
	var examtype=$("#examtype").val();
	$.post( "ajax.php", { type: "sendsmsexamcbse" ,AllData: id, examtype: examtype } , function( data ) {
	  	alert(data);
	});
}
function findStudent(t){
	var cls=$("#class").val();
	var section=$("#section").val();
	$.post( "ExamCBSE/ajax.php", { type: "findStudent" ,cls: cls, section: section } , function( data ) {
	  	$("#findStudent").html(data);
	});
}
</script>
<style>
.headPrint { display: none }
table{font-size:10px;}
@media print {
body {-webkit-print-color-adjust: exact;}
}
</style>
<form method="post" action="">
  <div class="row-fluid">
    <div class="span12">
      <div class="top-bar">
        <h3><i class="icon-cog"></i>Search Student Record</h3>
        <div>
          <?php 
						if($_POST){
							?>
          <!--<button type="button" name="fee" onClick="printDivNoHeader('printableDiv')" class="btn btn-primary"> Print</button>-->
		<a href="<?php echo URL_ROOT; ?>print/?page_id=term_wise_result&class=<?php echo $_REQUEST['class']; ?>&section=<?php echo $_REQUEST['section']; ?>&term=<?php echo $_REQUEST['term']; ?>&idno=<?php echo $_REQUEST['idno']; ?>" target="_blank" type="button" name="fee" class="btn btn-primary" style="color:white;"> Print Result</a>
          <button type="button" name="fee" onClick="smsDivNoHeader()" class="btn btn-primary"> SMS</button>
          <?php
          if(isset($_POST["submihostel"])){
              $db->delete("promote","where 1");
            $i = 0;
            foreach($_POST["idno"] as $class) {
                $saveAry=array(
                    "class" => $class,
                    "promoteclass"=>$_POST["pra"][$i],
                    "session"=>$_SESSION["admin"]["session"]
            	);
            	$flgUp=$db->insertAry("promote",$saveAry);
            	$i++;
            	}

                if(!is_null($flgUp))
        		{
        			$_SESSION['msg']='update Successfully';
        		}
    
    			}
						}
						?>

        <button type="submit" name="promote" class="btn btn-primary"> Promote</button>

		<button type="submit" name="final_percentage" class="btn btn-primary">Percentage Ranking</button>

          <button type="submit" name="fee" class="btn btn-primary"> View Record</button>
          <button type="submit" name="grade" class="btn btn-primary"> View Grade</button>
        </div>
      </div> 
      <div class="well no-padding">
        <div class="controls controls-row">
        
           			    <div class="control-group span3">

                            <div class="controls span3">
                                <select onchange="findStudent()" name="class" id="class">
                                    <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>
                            </div>
                       </div>
                       	<div class="control-group span3">
                            <div class="controls span3">
                               <select name="section" onchange="findStudent()" id="section">
                                    <option value="">Section</option>
                                    <?php 
                                        $class=$db->getRows("select * from section where school_id='".$_SESSION["admin"]["school_id"]."'");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["name"]==$aryForm["section"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
                                                <?php echo $cla["name"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="control-group span3">   
                               <select name="term">
                                <option value="">Exam type</option>
                                <option value="Term1">Term1</option>
                                <option value="Term2">Term2</option>
                                <option value="Final">Final</option>
                              </select>
                           </div>
                        <div class="control-group span3">
                            <div class="controls span3" id="findStudent">
                              <select  name="idno" id="idno">
                               <option value="">All Student</option>
                              </select>
                            </div>
         			   </div>   
        </div>
        <br>
      </div>
      <!-- / Forms: Content --> 
      
    </div>
  </div>
</form>
<div id="printableDiv">
  <?php
    if($_POST){
$mss=0; $smsAry="";$where="";$stsearch="";


$chkclassoptional = $db->getVal("select id from optional_class where class='$_POST[class]' and section='$_POST[section]' ");

if(isset($_POST["section"]) && $_POST["section"]!="")
			$where=" AND section='".$_POST["section"]."' ";
if(isset($_POST["idno"]) && $_POST["idno"]!="")
	$stsearch=" and idno='".$_POST["idno"]."' ";

 if(isset($_POST['fee'])){
	$allStudent=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_POST["class"]."' $where $stsearch and status=1 and issue_TC=0 order by name");

	$examtype=$db->getRows("select name,id,total from examterm where school_id='".$_SESSION["admin"]["school_id"]."' order by lorder");
	if(is_array($allStudent) && count($allStudent)>0){ $he=0;
		foreach($allStudent as $aryForm){$mss++;$he++;
 ?>
	  <div style="height:1195px;margin-bottom:20px;"  class="mydiv">
	  <div style="height:1290px;border:8px solid #D5DA13;">
		<?php
		$_GET['id']=$aryForm["idno"];
		error_reporting(55);
		$_REQUEST["class"]=$db->getVal("select class from student where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$aryForm["idno"]);
		$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($_POST["term"]!="Final") $termWhere=" and term='".$_POST["term"]."' ";

        $checksection = $db->getVal("select count(section) from subject where class='$_REQUEST[class]' and section!='' and session='".$_SESSION["admin"]["session"]."' ");
        $wheresection = '';
        if($checksection > 0){
            $wheresection = " and section='$_REQUEST[section]' ";
        }

        $examtype=$db->getRows("select * from term_permission  where school_id='".$_SESSION["admin"]["school_id"]."' and class='$_POST[class]' $termWhere order by ordering ");

		if(count($exam)>0){
		?>
		<?php $aryForm=$db->getRow("SELECT * FROM student WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$aryForm['idno']."'");$overallTotalGrade=0; ?>
		<form method="post" action="">
		  <input type="hidden" name="class" value="<?php echo $_REQUEST["class"] ?>" />
		  <input type="hidden" name="idno" value="<?php echo $aryForm["idno"] ?>" />
		  <div class="row-fluid">
			<div class="span12">
			  <div class="well" style="min-height:1050px;">
			  <div class="headPrint hide" style="border: none;">

    			 <table style="width:100%;">
    			     <tr>
    			         <td><img src="<?php echo URL_ROOT ?>uploads/cbselogo.png" height="90px" width="110px" style="margin:0px 0px 3px 10px;"></td>
    			         <td style="float:left;text-align:center;margin-bottom:18px;"><h1 style="font-size:30px;margin:5px 0px 0px 0px;color: #EC1E1E;"><?php echo ucfirst($LinksDetails["site_name"]) ?></h1>
        			         <span style="font-size:16px;font-weight:800;  color: #186916;"> <?php echo ucfirst(nl2br($LinksDetails["contact_content"])) ?></span>
    			         </td>
    			         <td><img src="<?php echo $LinksDetails["logo"]; ?>" height="90px" width="110px" style="margin:0px 0px 3px 10px;"></td>
    			     </tr>
    			 </table>
    		  </div>

				<div style="font-size: 16px;clear: both;font-weight: bold;padding: 5px;color: #000;text-align: center;border-bottom: 2px solid #000;;"> Report Card for Academic Session: <?php echo $_SESSION["admin"]["session"];?>  (<?php echo ucwords($_POST['term']); ?>)</div>

                <h4 style="text-align:center;color:#fa0606;margin-top:5px;margin:auto;padding-left:100px;"><?php echo strtoupper($aryForm['name']); ?></h4>
                <h6 style="text-align:center;">
                 <?php
                    if($aryForm['photo']!=""){
                    ?>
                        <img src="<?php echo URL_ROOT; ?>media/photo/<?php echo $aryForm['photo']; ?>" style="margin-top:-1px;position:absolute;height:110px;width:110px;border-radius:50%;border:solid 2px #d5da13" />
                    <?php
                } ?>
                </h6>

                <table style="width:100%;font-size:13px;font-weight: bold;border: 2px solid #D5DA13;height:90px;" cellspacing="0">
                    <tr>
                        <td style="color:#000;">Class : <span style="color:#FA0606;"> <?php echo $aryForm["class"] ?>-<?php echo $aryForm["section"] ?></span></td>
                        <td style="color:#000;text-align:right;">Father's Name : <span style="color:#FA0606;"> <?php echo $aryForm["fname"] ?></span></td>
                    </tr>
                    <tr>
                        <td style="color:#000;">Roll No. : <span style="color:#FA0606;"> <?php echo $aryForm["fincome"] ?></span></td>
                        <td style="color:#000;text-align:right;">Mother's Name : <span style="color:#FA0606;"> <?php echo $aryForm["mname"] ?></span></td>
                    </tr>
                    <tr>
                        <td style="color:#000;">Admission No : <span style="color:#FA0606;"> <?php echo $aryForm["admissionno"] ?></span></td>
                        <td style="color:#000;text-align:right;">Date of Birth : <span style="color:#FA0606;"> <?php echo $aryForm["dob"] ?></span></td>
                    </tr>
                </table>

				<h4 style="text-align:center;color:#fa0606;padding-left:50px;">SCHOLASTIC AREAS</h4>


            <?php if($_POST['term']=="Term1" || $_POST['term']=="Term2") 
            { ?>
                <table style="width:100%; font-size:14px;border:2px solid #D5DA13;text-align: center;color: #000;font-weight: 800;margin-top:-10px;" cellpadding="3" cellspacing="0">
						<tr style="font-weight: 800;padding: 5px;color: #000;">
							<td style="border-right:2px solid #D5DA13;border-bottom: 1px solid #D5DA13;padding:5px;color:#000;">SUBJECTS</td>
							<?php
    							if(is_array($examtype) && count($examtype)>0){
    								 $finalObtain=0;$finalTotal=0; $rowtotal = 0;
    								foreach($examtype as $etype){

    								$examterm = $db->getRow("select * from examterm where name='$etype[exam]' and class='".$_POST["class"]."' $wheresection  and session='".$_SESSION["admin"]["session"]."' ");

    								if($examterm['term_percent']=="" && count($examterm)>1){
            						    ?>
            						<td style="border-right:2px solid #D5DA13;border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">TOTAL </td>    
            						    <?php
            						}
    								?>
    								<td style="border-right:2px solid #D5DA13;border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">
                                    <?php
                                        echo $etype["exam"];
            							if($examterm['term_percent']!=""){
            							    echo "<br>(".$examterm['term_percent'].")";
            							    $rowtotal+=$examterm['term_percent'];
            							}
            						 ?>
                                   </td>
    							<?php }
    							    }   ?>
                             <td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;">Total<br>(<?php echo $rowtotal; ?>)</td>
							<td style="border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">GRADE</td>
						</tr>
						
						<?php $x=0; $xvalues = []; $yvalues = []; $colour = []; $maxmarks = 0;

						$detail=$db->getRows("select * from subject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' $wheresection  and session='".$_SESSION["admin"]["session"]."' order by id");
						
						foreach($detail as $d){ $final=""; $rowobtain = 0; $isNA = false;
						
						$chksubjectoptional = 0;
						if($chkclassoptional > 0){
						    $chksubjectoptional = $db->getVal("select id from optional_subject where idno='$aryForm[idno]' and subject='$d[subject]' and session='".$_SESSION["admin"]["session"]."' ");
						}
						
						if($chksubjectoptional == 0){
						
						?><tr id="row_<?php echo $aryForm["idno"]."_".$x; ?>">
								<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo ucfirst($d["subject"]) ?></td>
								<?php
								foreach($examtype as $etype){

							    $examterm = $db->getRow("select * from examterm where name='$etype[exam]' and class='".$_POST["class"]."' $wheresection  and session='".$_SESSION["admin"]["session"]."' ");

								$examResultData=$db->getRow("select * from exammarks where idno='".$aryForm["idno"]."' and type='".$etype['exam']."' and subject='".$d["subject"]."'");

                                // if($examResultData["obtain"]!="NA" && $isNA==false){
                                
								if($examterm['term_percent']==""){
    								    array_push($yvalues, (int)$examResultData['obtain']);
    								    
    								    if($maxmarks < $examResultData['total']){
    								        $maxmarks = $examResultData['total'];
    								    }
							    ?>
        							<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo $examResultData['total']; $finalTotal+= $examResultData["total"]; ?> </td>
        							<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo $examResultData['obtain']; $finalObtain+=  $examResultData["obtain"]; $rowobtain+=$examResultData["obtain"]; ?> </td>
							    <?php
    							    
    							} else {
								?>
								<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;">
								 <?php
								 $obtain = number_format($examResultData["obtain"]*$examterm['term_percent']/$examResultData["total"],1)+0;
								 if($examResultData["obtain"]=="AB"){
                                    echo $examResultData["obtain"];
								 } else {
								     echo round($obtain);
								     $rowobtain+=round($obtain);
								 }

								$finalTotal+=   $examterm['term_percent'];
                                $finalObtain+=  round($obtain);

                                ?>
								</td>
								<?php
							        $maxmarks = $examterm['term_percent'];
        							}
								
								//  }
								// else {
								//     $isNA = true;
								//     echo "<td style='border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;'>0</td>";
								// }
								}

								?>

								<td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;"><?php echo $rowobtain; ?></td>
								<td style="border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">
								    <?php
								        echo FindGrades($rowobtain,$rowtotal);
                                        if($isNA==false){
                                            array_push($xvalues, $d['subject']);
            								array_push($yvalues, (int) round($rowobtain));
        							        $color = FindColour((int) round($rowobtain),$rowtotal);
        							        array_push($colour, $color);
                                        }
								    ?>
								</td>
							</tr>
							<?php //if($isNA){ ?>
							<script>
							 //   document.getElementById("row_<?php echo $aryForm[idno].'_'.$x; ?>").style.display = 'none';
				 			</script>
						<?php //}
														$x++;
						}
						}
						?>
						</table>
						
                        <div style="width:900px;float:left;color:transparent;">.</div>
        				<br>
                        <?php
                            $data = $db->getRow("select * from attendance_remark where idno='$aryForm[idno]' and class='$_POST[class]' and type='$_POST[term]' and session='".$_SESSION["admin"]["session"]."' ");
                        ?>
                        <div style="width:100%;float:left;">
            				<table class="table" style="width:100%;" cellspacing="0">
            				    <tr>
            				        <td style="text-align:center;color:#000;border-left: 2px solid #D5DA13;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">TOTAL MARKS</td>
            				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;"><?PHP echo $finalObtain." / ".$finalTotal; ?></td>
            				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">PERCENTAGE</td>
            				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">
            				            <?php 
            				            $percentage = number_format(($finalObtain*100/ $finalTotal),2);
                        				$percentArray[$aryForm[idno]]= $percentage;

            				            echo number_format(($finalObtain*100/ $finalTotal),2)."%"; ?></td>
            				        <?php if($data['view_attendance']==1){ ?>
            				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">Attendance</td>
            				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;"><?php echo $data['present']."/".$data['working_days']; ?></td>
            				        <?php } ?>
            				    </tr>
            				</table>
                        </div>
        				<br> 

                <?php if($examtype[0]["co_scholastic_view"] == 1) { ?>
                <div style="width:37%;float:left;">
                    <?php
                        $record = $db->getRows("select type from criteria where class='$_REQUEST[class]' and session='".$_SESSION["admin"]["session"]."' group by type ");
                        foreach($record as $row){ 
                    ?>
                        <table style="width:100%;border:solid 2px #D5DA13;" cellpadding="5">
                            <tr>
                                <th style="text-align:left;border-bottom:solid 2px #D5DA13;font-size:15px;"><?php echo $row['type']; ?></th>
                                <th style="text-align:center;border-bottom:solid 2px #D5DA13;border-left:solid 2px #D5DA13;font-size:15px;width:80px;">Grade</th>
                            </tr>
                            <?php $names = $db->getRows("select * from criteria where type='$row[type]' and class='$_REQUEST[class]' and session='".$_SESSION["admin"]["session"]."' ");
                                foreach($names as $name){
                                    $coscholastic = $db->getVal("select co_scholastic from term_permission where term='$_POST[term]' and class='".$_POST["class"]."'  ");
                                    $termname = $db->getVal("select name from examname where id='$coscholastic' ");
                                    $grade=$db->getVal("select grade from examcriteriarecord where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$aryForm["idno"]." and subcriteria='".$name["name"]."' and criteria='".$row['type']."' and session='".$_SESSION["admin"]["session"]."' and term='$termname' ");
                                    // $grade=$db->getVal("select grade from examcriteriarecord where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$aryForm["idno"]." and subcriteria='".$name["name"]."' and criteria='".$row['type']."' and session='".$_SESSION["admin"]["session"]."' and term='".$_POST["term"]."'");
                                    $gradecolor = $db->getVal("select colour from grade where grade='$grade' and session='".$_SESSION["admin"]["session"]."' ");
                                ?>
                                <tr>
                                    <td style="text-align:left;border-bottom:dashed 1px #000;color:#000;"><?php echo $name['name']; ?></td>
                                    <td style="text-align:center;border-bottom:dashed 1px #000;border-left:solid 2px #D5DA13;color:#fff;width:80px;"><label style="text-align:center;width:25px;height:20px;color:#fff;background:<?php echo $gradecolor; ?>"><?php echo $grade; ?></label></td>
                                </tr>
                            <?php } ?> 
                            
                        </table>
 
                    <?php }
                    if($_POST['term']=='Term2'){ ?>
                    <br>
                    <label>Promote : 
                    <?php
                        $promote = $db->getRow("select * from result_message where class='$_POST[class]' ");
                        $pclass = $db->getRow("select * from promote where class='$_POST[class]' ");
                        if($promote['promote']==1){
                            if($percentage>=$promote['percent']){
                                echo "Promoted to next class - $pclass[promoteclass]";
                            } else {
                                echo "Detained to same class - $pclass[class]";
                            }
                        } else {
                            echo "Promoted to next class - $pclass[promoteclass]";
                        }
                    ?>
                    </label>
                    <?php }
                    if($data['view_remark']==1){ ?>
                    <label>Remark : <?php echo $data['remark']; ?></label>
                    <?php } ?>
                    
                    <label>Rank : <span id="rank_<?php echo $aryForm['idno']; ?>"></span></label>
                    
                </div>
                <?php } ?>
				<div style="width:61%;float:right;overflow:visible;">
				    <?php
				        for($j = 0; $j <= $maxmarks; $j++){
    				        array_push($yvalues,$j);
    				        $j=$j+10;
				        }
                    ?>
				    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
				    <canvas id="myChart<?php echo $he; ?>" class="chartdiv" style="width:100%;max-width:600px;z-index:999999;max-height:250px;"></canvas>

				    <script>
                        var xValues = <?php echo json_encode($xvalues); ?>;
                        var yValues = <?php echo json_encode($yvalues); ?>;
                        var barColors = <?php echo json_encode($colour); ?>;
                        
                        new Chart("myChart<?php echo $he; ?>", { 
                          type: "bar",
                          data: {
                            labels: xValues,
                            datasets: [{
                              backgroundColor: barColors,
                              data: yValues
                            }]
                          },
                          options: {
                            legend: {display: false},
                            title: {
                              display: true,
                              text: "Graphical Result"
                            },
                            scales: {
                                xAxes: [{
                                    barPercentage: 0.4,
                                }]
                            }
                          }
                        });
                    </script>
                    <br>
                    <?php if($examtype[0]["co_scholastic_view"] == 1) { ?>
                    <?php $allgrades = $db->getRows("select * from grade"); 
                        foreach($allgrades as $gd){ ?>
                            <div class="icardbg" style="padding:10px;width:30%;display:inline-block;margin:0px;">
                                <label style="display:inline-block;width:20px;text-align:center;height:20px;color:#fff;background:<?php echo $gd['colour']; ?>"><?php echo $gd['grade']; ?></label> 
                                <label style="display:inline-block;"><?php echo $gd['detail']; ?></label>
                            </div>
                        <?php } }?>
				</div>


            <?php } ?>

            <?php if($_POST['term']=="Final")
            { ?>
                    <table style="width:100%; font-size:14px;border:2px solid #D5DA13;text-align: center;color: #000;font-weight: 800;margin-top:-10px;" cellpadding="3" cellspacing="0">
						<tr style="font-weight: 800;padding: 5px;color: #000;">
							<td style="border-right:2px solid #D5DA13;border-bottom: 1px solid #D5DA13;padding:5px;color:#000;">SUBJECTS</td>
							<?php $finalTotal = 0; $term1total = 0; $term1obtain = 0; $term2total = 0; $term2obtain = 0;
							$examtype=$db->getRows("select * from term_permission  where school_id='".$_SESSION["admin"]["school_id"]."' and term='Term1' and class='$_POST[class]' order by term, ordering ");
    							if(is_array($examtype) && count($examtype)>0){
    								 $finalObtain=0; $rowtotal = 0; $term1rowtotal = 0;
    								foreach($examtype as $etype){

    								$examterm = $db->getRow("select * from examterm where name='$etype[exam]' and class='".$_POST["class"]."' $wheresection  and session='".$_SESSION["admin"]["session"]."' ");

    								?>
    								<td style="border-right:2px solid #D5DA13;border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">
                                    <?php
                                        echo $etype["exam"];
            							if($examterm['term_percent']!=""){
            							    echo "<br>(".$examterm['term_percent'].")";
            							    $term1rowtotal+=$examterm['term_percent'];
            							}
            						 ?>
                                   </td>
    							<?php }
    							    }   ?>
                             <td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;">Total<br>(<?php echo $term1rowtotal; ?>)</td>
							<td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;">GRADE</td>

                            <?php
							$examtype=$db->getRows("select * from term_permission  where school_id='".$_SESSION["admin"]["school_id"]."' and term='Term2' and class='$_POST[class]' order by term, ordering ");
    							if(is_array($examtype) && count($examtype)>0){
    								 $finalObtain=0; $term2rowtotal = 0;
    								foreach($examtype as $etype){

    								$examterm = $db->getRow("select * from examterm where name='$etype[exam]' and class='".$_POST["class"]."' $wheresection  and session='".$_SESSION["admin"]["session"]."' ");

    								?>
    								<td style="border-right:2px solid #D5DA13;border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">
                                    <?php
                                        echo $etype["exam"];
            							if($examterm['term_percent']!=""){
            							    echo "<br>(".$examterm['term_percent'].")";
            							 $term2rowtotal+=$examterm['term_percent'];
            							}
            						 ?>
                                   </td>
    							<?php }
    							    }   ?>
                            <td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;">Total<br>(<?php echo $term2rowtotal; ?>)</td>
							<td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;">GRADE</td>

                            <td style="border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">OVERALL TOTAL<br>(<?php echo $term1rowtotal+$term2rowtotal; ?>)</td>

						</tr>
						
						<?php $x=0; $xvalues = []; $yvalues = []; $colour = []; $maxmarks = 0;

						$detail=$db->getRows("select * from subject where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["class"]."' $wheresection  and session='".$_SESSION["admin"]["session"]."' order by id");

						foreach($detail as $d){ $final=""; $rowobtainterm1 = 0; $isNA = false;
						
						$chksubjectoptional = 0;
						if($chkclassoptional > 0){
						    $chksubjectoptional = $db->getVal("select id from optional_subject where idno='$aryForm[idno]' and subject='$d[subject]' and session='".$_SESSION["admin"]["session"]."' ");
						}
						
						if($chksubjectoptional == 0){
						
						?><tr id="row_<?php echo $aryForm["idno"]."_".$x; ?>">
								<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo ucfirst($d["subject"]) ?></td>
								<?php
								$examtype=$db->getRows("select * from term_permission  where school_id='".$_SESSION["admin"]["school_id"]."' and term='Term1' and class='$_POST[class]' order by term, ordering ");
								foreach($examtype as $etype){

							    $examterm = $db->getRow("select * from examterm where name='$etype[exam]' and class='".$_POST["class"]."' $wheresection  and session='".$_SESSION["admin"]["session"]."' ");
                                
								$examResultData=$db->getRow("select * from exammarks where idno='".$aryForm["idno"]."' and type='".$etype['exam']."' and subject='".$d["subject"]."'");

                                    // if($examResultData["obtain"]!="NA" && $isNA==false){
    
    								if($examterm['term_percent']==""){
        								
        								    if($maxmarks < $examResultData['total']){
        								        $maxmarks = $examResultData['total'];
        								    }
    							    ?>
            							<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo $examResultData['total'];
            							?> </td>
            							<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo $examResultData['obtain']; 
            							$finalObtain+=  $examResultData["obtain"]; 
            							$rowobtainterm1+=$examResultData["obtain"]; ?> </td>
    							    <?php
        							    
        							} else {
    								?>
    								<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;">
    								 <?php
    								 $obtain = number_format($examResultData["obtain"]*$examterm['term_percent']/$examResultData["total"],1)+0;
    								 if($examResultData["obtain"]=="AB"){
                                        echo $examResultData["obtain"];
    								 } else {
    								     echo round($obtain);
    								     $rowobtainterm1+=round($obtain);
    								 }
    
                                    $finalObtain+=  round($obtain);
    
                                    ?>
    								</td>
    								<?php
    							        $maxmarks = $examterm['term_percent'];
            							}
    								
    				// 		}
    				// 				else {
    				// 				    $isNA = true;
    				// 				    echo "<td style='border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;'>0</td>";
    				// 				}
								}

								?>

								<td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;"><?php echo $rowobtainterm1; ?></td>
								<td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;">
								    <?php
								        echo FindGrades($rowobtainterm1,$term1rowtotal);
    							        $color = FindColour((int) round($rowobtainterm1),$term1rowtotal);
    							        $term1obtain+=$rowobtainterm1;
    							        
    							     //   if($isNA==false){
    							            $term1total+=$term1rowtotal;
    							     //   }
								    ?>
								</td>

								<?php $rowobtainterm2 = 0;
								$examtype=$db->getRows("select * from term_permission  where school_id='".$_SESSION["admin"]["school_id"]."' and term='Term2' and class='$_POST[class]' order by term, ordering ");
								foreach($examtype as $etype){

							    $examterm = $db->getRow("select * from examterm where name='$etype[exam]' and class='".$_POST["class"]."' $wheresection  and session='".$_SESSION["admin"]["session"]."' ");
                                
								$examResultData=$db->getRow("select * from exammarks where idno='".$aryForm["idno"]."' and type='".$etype['exam']."' and subject='".$d["subject"]."'");

                                    // if($examResultData["obtain"]!="NA" && $isNA==false){
    
    								if($examterm['term_percent']==""){
        								    array_push($yvalues, (int)$examResultData['obtain']);
        								    
        								    if($maxmarks < $examResultData['total']){
        								        $maxmarks = $examResultData['total'];
        								    }
    							    ?>
            							<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo $examResultData['total']; 
            							?> </td>
            							<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo $examResultData['obtain']; 
            							$finalObtain+=  $examResultData["obtain"]; 
            							$rowobtainterm2+=$examResultData["obtain"]; ?> </td>
    							    <?php
        							    
        							} else {
    								?>
    								<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;">
    								 <?php
    								 $obtain = number_format($examResultData["obtain"]*$examterm['term_percent']/$examResultData["total"],1)+0;
    								 if($examResultData["obtain"]=="AB"){
                                        echo $examResultData["obtain"];
    								 } else {
    								     echo round($obtain);
    								     $rowobtainterm2+=round($obtain);
    								 }
    
                                    $finalObtain+=  round($obtain);
    
                                    ?>
    								</td>
    								<?php
    							        $maxmarks = $examterm['term_percent'];
            							}
    								
    				// 		}
    				// 				else {
    				// 				    $isNA = true;
    				// 				    echo "<td style='border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;'>0</td>";
    				// 				}
								}

								?>

								<td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;"><?php echo $rowobtainterm2; ?></td>
								<td style="border-bottom: 2px solid #D5DA13;border-right: 2px solid #D5DA13;padding:5px;color:#000;">
								    <?php
								        echo FindGrades($rowobtainterm2,$term2rowtotal);


    							        $term2obtain+=$rowobtainterm2;
    							        
    							     //   if($isNA==false){
    							            $term2total+=$term2rowtotal;
    							            array_push($xvalues, $d['subject']);
    							            
            								array_push($yvalues, (int) round($rowobtainterm2));
        							        $color = FindColour((int) round($rowobtainterm2),$term2rowtotal);
        							        array_push($colour, $color);

    							     //   }
								    ?>
								</td>
                                <td style="border-bottom: 2px solid #D5DA13;padding:5px;color:#000;"><?php echo $rowobtainterm1+$rowobtainterm2; $finalTotal+=$term1rowtotal+$term2rowtotal ?></td>
							</tr>
						<?php //if($isNA){ ?>
							<script>
							    //document.getElementById("row_<?php echo $aryForm[idno].'_'.$x; ?>").style.display = 'none';
							</script>
						<?php //}
							$x++;
						}
						}
						?> 
						</table>
 
                        <div style="width:900px;float:left;color:transparent;">.</div>
        				<br> 
                        <?php
                            $data = $db->getRow("select * from attendance_remark where idno='$aryForm[idno]' and class='$_POST[class]' and type='Term2' and session='".$_SESSION["admin"]["session"]."' ");
                            $workingdays = $db->getVal("select sum(working_days) from attendance_remark where idno='$aryForm[idno]' and class='$_POST[class]' and (type='Term1' or type='Term2') and session='".$_SESSION["admin"]["session"]."' ");
                            $totalpresent = $db->getVal("select sum(present) from attendance_remark where idno='$aryForm[idno]' and class='$_POST[class]' and (type='Term1' or type='Term2') and session='".$_SESSION["admin"]["session"]."' ");
                        ?>
                        <div style="width:100%;float:left;">
            				<table class="table" style="width:100%;" cellspacing="0">
            				    <tr>
            				        <td style="text-align:center;color:#000;border-left: 2px solid #D5DA13;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">TERM1</td>
            				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;"><?PHP echo $term1obtain." / ".$term1total." - ".number_format(($term1obtain*100/ $term1total),2)."%"; ?></td>
            				        <td style="text-align:center;color:#000;border-left: 2px solid #D5DA13;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">TERM2</td>
            				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;"><?PHP echo $term2obtain." / ".$term2total." - ".number_format(($term2obtain*100/ $term2total),2)."%"; $percentage = number_format(($term2obtain*100/ $term2total),2); ?></td>
            				        <?php if($data['view_attendance']==1){ ?>
            				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;">Attendance</td>
            				        <td style="text-align:center;color:#000;border-top: 2px solid #D5DA13;border-right: 2px solid #D5DA13;border-bottom: 2px solid #D5DA13;"><?php echo $totalpresent."/".$workingdays; ?></td>
            				        <?php } ?>
            				    </tr>
            				</table>
                        </div>
        				<br> 

                <?php if($examtype[0]["co_scholastic_view"] == 1) { ?>
                <div style="width:37%;float:left;">
                    <?php
                        $record = $db->getRows("select type from criteria where class='$_REQUEST[class]' and session='".$_SESSION["admin"]["session"]."' group by type ");
                        foreach($record as $row){ 
                    ?>
                        <table style="width:100%;border:solid 2px #D5DA13;" cellpadding="5">
                            <tr>
                                <th style="text-align:left;border-bottom:solid 2px #D5DA13;font-size:15px;"><?php echo $row['type']; ?></th>
                                <th style="text-align:center;border-bottom:solid 2px #D5DA13;border-left:solid 2px #D5DA13;font-size:15px;width:80px;">Grade</th>
                            </tr>
                            <?php $names = $db->getRows("select * from criteria where type='$row[type]' and class='$_REQUEST[class]' and session='".$_SESSION["admin"]["session"]."' ");
                                foreach($names as $name){
                                    // $coscholastic = $db->getVal("select co_scholastic from term_permission where term='Term2' ");
                                    $coscholastic = $db->getVal("select co_scholastic from term_permission where term='Term2' and class='".$_POST["class"]."'  ");
                                    $termname = $db->getVal("select name from examname where id='$coscholastic' ");
                                    $grade=$db->getVal("select grade from examcriteriarecord where school_id='".$_SESSION["admin"]["school_id"]."' and idno=".$aryForm["idno"]." and subcriteria='".$name["name"]."' and criteria='".$row['type']."' and session='".$_SESSION["admin"]["session"]."' and term='$termname' ");
                                    $gradecolor = $db->getVal("select colour from grade where grade='$grade' and session='".$_SESSION["admin"]["session"]."' ");
                                ?>
                                <tr>
                                    <td style="text-align:left;border-bottom:dashed 1px #000;color:#000;"><?php echo $name['name']; ?></td>
                                    <td style="text-align:center;border-bottom:dashed 1px #000;border-left:solid 2px #D5DA13;color:#fff;width:80px;"><label style="text-align:center;width:25px;height:20px;color:#fff;background:<?php echo $gradecolor; ?>"><?php echo $grade; ?></label></td>
                                </tr>
                            <?php } ?>
                            
                        </table>
                    <?php }
                    ?>
                    <br>
                    <label>Promote : 
                    <?php
                        $promote = $db->getRow("select * from result_message where class='$_POST[class]' ");
                        $pclass = $db->getRow("select * from promote where class='$_POST[class]' ");
                        if($promote['promote']==1){
                            if($percentage>=$promote['percent']){
                                echo "Promoted to next class - $pclass[promoteclass]";
                            } else {
                                echo "Detained to same class - $pclass[class]";
                            }
                        } else {
                            echo "Promoted to next class - $pclass[promoteclass]";
                        }
                    ?>
                    </label>
                    <?php
                    if($data['view_remark']==1){ ?>
                    <label>Remark: <?php echo $data['remark']; ?></label>
                    <?php } ?>
                    
                    <label>Rank: <span id="rank_<?php echo $aryForm['idno']; ?>"></span></label>
                    
                </div>
                <?php } ?>
				<div style="width:61%;float:right;overflow:visible;">
				    <?php
				        for($j = 0; $j <= $maxmarks; $j++){
    				        array_push($yvalues,$j);
    				        $j=$j+10;
				        }
                    ?>
				    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
				    <canvas id="myChart<?php echo $he; ?>" class="chartdiv" style="width:100%;max-width:600px;z-index:999999;max-height:250px;"></canvas>

				    <script>
                        var xValues = <?php echo json_encode($xvalues); ?>;
                        var yValues = <?php echo json_encode($yvalues); ?>;
                        var barColors = <?php echo json_encode($colour); ?>;
                        
                        new Chart("myChart<?php echo $he; ?>", { 
                          type: "bar",
                          data: {
                            labels: xValues,
                            datasets: [{
                              backgroundColor: barColors,
                              data: yValues
                            }]
                          },
                          options: {
                            legend: {display: false},
                            title: {
                              display: true,
                              text: "Graphical Result"
                            },
                            scales: {
                                xAxes: [{
                                    barPercentage: 0.4,
                                }]
                            }
                          }
                        });
                    </script>
                    <br>
                    <?php if($examtype[0]["co_scholastic_view"] == 1) { ?>
                    <?php $allgrades = $db->getRows("select * from grade");
                        foreach($allgrades as $gd){ ?>
                            <div class="icardbg" style="padding:10px;width:30%;display:inline-block;margin:0px;">
                                <label style="display:inline-block;width:20px;text-align:center;height:20px;color:#fff;background:<?php echo $gd['colour']; ?>"><?php echo $gd['grade']; ?></label> 
                                <label style="display:inline-block;"><?php echo $gd['detail']; ?></label>
                            </div>
                        <?php } }?>
				</div>

                        
            <?php } ?>
 
                <br>

				 <div>
                    <?php
				        $wheresign = "";
                            if(isset($_POST["section"]) && $_POST["section"]!=""){
                    			$wheresign=" AND section='".$_POST["section"]."' ";
                            }
				     
				            $principal = $db->getVal("select sign from sign where type='Principal' "); 
    				       $teacher = $db->getVal("select sign from sign where type='Class Teacher' and class='$_POST[class]' $wheresign "); 
				     ?>
					   <table style="width:100%; text-align:center;font-size:14px;font-weight: 800;" cellpadding="0" cellspacing="2" >
						<tr style="vertical-align: bottom;">
						  <td style="color:#000;text-align:left;padding-left:20px;padding-top:50px;">
						       <?php if($teacher!=""){ ?>
						          <img src="<?php echo URL_ROOT."uploads/sign/$teacher" ;?>" style="display:bloc;height:50px;padding-bottom:20px;" /><br>
						      <?php } ?>
						      <span style="border-top:solid 1px;padding-top:10px;">Class Teacher's Signature</span></td>
						  <td style="color:#000;text-align:right;padding-right:20px;padding-top:50px;">
						      <?php if($principal!=""){ ?>
						          <img src="<?php echo URL_ROOT."uploads/sign/$principal" ;?>" style="display:bloc;height:50px;padding-bottom:20px;" /><br>
						      <?php } ?>
						      <span style="border-top:solid 1px;padding-top:10px;">Principal's Signature</span></td>
						</tr>
					  </table>
				</div>

			  </div>
			</div>
		  </div>
		</form>
		<?php
		}
			?>
	   
	  </div>
	  </div>
    <style>
        @media print {
        .mydiv {page-break-after: always;}
        
        .chartdiv {display:block; z-index:99999;}
        }
    </style>
	  
	<?php
    arsort($percentArray);
    /*
    $i = 1;    
    foreach($percentArray as $key => $value){
        ?>
        <script>
            $("#rank_"+<?php echo $key; ?>).html(<?php echo $i; ?>);
        </script>
        <?
        $i++;
    }
    */
		}}

	    $unique = array_unique($percentArray);

	    $temp = []; $j = 1;
	    foreach($unique as $un){
	        $temp["$un"] = $j;
	        $j++;
	    }

        foreach($percentArray as $key => $val){
            foreach($temp as $key1 => $val1){
                if($val==$key1){
                    ?>
                    <script>
                        $("#rank_"+<?php echo $key; ?>).html(<?php echo $val1; ?>);
                    </script>
                    <?
                }
            }
            
        }


}

 elseif(isset($_POST['promote'])){
     
     
     	?><form method="post" action="">
		<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
					<h3><i class="icon-cog"></i>Add Result of students</h3>
                    <div>
						<button type="submit" name="submihostel" class="btn btn-primary"> Save </button>
					</div>
				</div>
			<div class="well no-padding">
            	<div class="controls controls-row">
		<?php
		$allclass=$db->getRows("select * from class ");
		if(is_array($allclass) && count($allclass)>0){
			?><table class="table">
			<tr>
                <td>SNo.</td><td>class</td><td>Promotion Class</td>
            </tr>
			<?php $i=0;
			foreach($allclass as $stu){ $i++;
				?>
				<tr>
                	<td><?php echo $i; ?></td>
                	<td><?php echo $stu["class"]; ?></td>
					<input type="hidden" name="idno[]" required  value="<?php echo $stu["class"] ?>" readonly="readonly" />

							<td><?php //echo $db->getLastQuery(); ?>
                            <select name="pra[]">
                            	<option value="">Select Grade</option>
                            	<?php
                        			$pclass=$db->getRow("select * from promote where class ='".$stu["class"]."' ");
                        			echo $ss="select * from promote where class ='".$stu["class"]."' ";
                                	$class=$db->getRows("select * from class ");
                                	if(is_array($class) && count($class)>0){
    									foreach($class as $cls){
    										?><option <?php if($cls["class"]==$pclass["promoteclass"])echo 'selected'; ?> value="<?php echo $cls["class"] ?>"><?php echo $cls["class"] ?></option><?php
    									}
    								}
								?>
                            </select>
                            </td>
               </tr>
				<?php
			}
			?></table><?php
		}
		?></div></div></div></div></form><?php
     
 }

?> 
<style>th{color:#000;}td, th{line-height: 1;} table{margin:2px;}</style>

<?php
}

function FindGrades($obtain,$total){
	$per=round(($obtain*100)/$total);
	global $db;
	$data=$db->getRows("select * from examgrade  where school_id='".$_SESSION["admin"]["school_id"]."' ");
	if(is_array($data) && count($data)>0){
		foreach($data as $d){
			if($per>=$d["minrange"] && $per<=$d["maxrange"]){
				return "<div style='background:$d[colour];width:25px;height:20px;color:white;padding-top:5px;text-align:center;margin:auto;'>$d[grade]</div>"; 
			}
		}
	}
	return $per;
}

function FindColour($obtain,$total){
	$per=round(($obtain*100)/$total);
	global $db;
	$data=$db->getRows("select * from examgrade  where school_id='1' ");
	if(is_array($data) && count($data)>0){
		foreach($data as $d){
			if($per>=$d["minrange"] && $per<=$d["maxrange"]){
				return $d['colour'];
			}
		}
	}
	return $per;
}



?>
</div>
<div id="canvas"></div>
<script src="http://html2canvas.hertzen.com/build/html2canvas.js"></script>
<script>

</script>
