<?php 

$permission = $db->getRow("select * from examname where name='".$_GET['term']."' ");

$where="";$stsearch="";

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

if(isset($_GET["section"]) && $_GET["section"]!="")
			$where=" AND section='".$_GET["section"]."' ";
if(isset($_GET["idno"]) && $_GET["idno"]!="")
	$stsearch=" and idno='".$_GET["idno"]."' ";
	
    $allStudent=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_GET["class"]."' $where $stsearch and status=1 and issue_TC=0 order by name");
    
    	if(is_array($allStudent) && count($allStudent)>0){ $he=0;
    	$percentArray = array();
		foreach($allStudent as $aryForm){$mss++;$he++;
	  ?>

	  <div style="height:1195px;margin-bottom:20px;"  class="mydiv">
	  <div style="height:1290px;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($_GET["term"]!="final") $termWhere=" and term='".$_GET["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->getRow("select * from examterm  where school_id='".$_SESSION["admin"]["school_id"]."' $termWhere and class='".$_REQUEST["class"]."' $wheresection ");

        $examtype=$db->getRows("select * from term_permission  where school_id='".$_SESSION["admin"]["school_id"]."'  and class='$_GET[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="">
		  <div class="row-fluid">
			<div class="span12">
			  <div class="well" style="min-height:800px;">
			  <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="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 20px 3px 10px;float:right;"></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;margin-top:-9px;"> Report Card for Academic Session: <?php echo $_SESSION["admin"]["session"];?>  (<?php echo ucwords($_GET['term']); ?>)</div>

                <!--<h4 style="text-align:center;color:#fa0606;padding-left:50px;margin-top:5px;"><?php echo strtoupper($aryForm['name']); ?></h4>-->

                <h4 style="text-align:center;color:#fa0606;padding-top:5px;margin:auto;padding-left:0px;"><?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:-15px;position:absolute;height:110px;width:110px;border-radius:50%;border:solid 2px #d5da13;margin-left:-50px;" />
                            <?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($_GET['term']=="Term1" || $_GET['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='".$_GET["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='".$_GET["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='$_GET[class]' and type='$_GET[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='$_GET[term]' ");
                                    $coscholastic = $db->getVal("select co_scholastic from term_permission where term='$_GET[term]' and class='".$_GET["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='".$_GET["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>-->
                                        <label style="display:inline-block;width:20px;text-align:center;height:20px;color:#fff;background:<?php echo $gradecolor; ?>"><?php echo $grade; ?></label>
                                    </td>
                                </tr>
                            <?php } ?>
                            
                        </table>
                    <?php }
                    if($data['view_remark']==1){ ?>
                    <br>    <label>Remark: <?php echo $data['remark']; ?></label>
                    <?php } ?>
                    <br>
                    <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($_GET['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='$_GET[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='".$_GET["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='$_GET[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='".$_GET["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='$_GET[class]' order by term, ordering ");
								foreach($examtype as $etype){

							    $examterm = $db->getRow("select * from examterm where name='$etype[exam]' and class='".$_GET["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='$_GET[class]' order by term, ordering ");
								foreach($examtype as $etype){

							    $examterm = $db->getRow("select * from examterm where name='$etype[exam]' and class='".$_GET["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='$_GET[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='$_GET[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='$_GET[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(($finalObtain*100/ $finalTotal),2);
                        				$percentArray[$aryForm[idno]]= $percentage;
            				            
            				            ?></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='".$_GET["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>-->
                                        <label style="display:inline-block;width:20px;text-align:center;height:20px;color:#fff;background:<?php echo $gradecolor; ?>"><?php echo $grade; ?></label>
                                    </td>
                                </tr>
                            <?php } ?>
                            
                        </table>
                    <?php }
                    if($data['view_remark']==1){ ?>
                    <br>    <label>Remark: <?php echo $data['remark']; ?></label>
                    <?php } ?>
                    <br>
                    <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><br>
				 <div style="position:absolute;width:98%;margin-top:500px;float:left;">
				     <?php
				        $wheresign = "";
                            if(isset($_REQUEST["section"]) && $_REQUEST["section"]!=""){
                    			$wheresign=" AND section='".$_REQUEST["section"]."' ";
                            }
				     
				            $principal = $db->getVal("select sign from sign where type='Principal' "); 
    				       $teacher = $db->getVal("select sign from sign where type='Class Teacher' and class='$_REQUEST[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
		}
			?>
	   <style>
@media print {
.mydiv {page-break-after: always;}

.chartdiv {display:block; z-index:99999;}
}
</style>
	  </div>
	  </div>
	<?php 
	
    arsort($percentArray);

    /*
    $i = 1;    
    foreach($percentArray as $key => $value){
        ?>
        <script>
            document.getElementById('rank_'+<?php echo $key; ?>).innerHTML = <?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>
                        document.getElementById('rank_'+<?php echo $key; ?>).innerHTML = <?php echo $val1; ?>;
                    </script>
                    <?
                }
            }
            
        }


		}
	    
	}

?>
<script>
    // document.getElementById('rank').innerHTML = 'hello';
</script>
<?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;
}

?>


<script>
    setTimeout(function(){ window.print(); }, 2000);
    
</script>