<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){
		?>
		<a href="<?php echo URL_ROOT; ?>print/?page_id=exam_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="printDivNoHeader('printableDiv')" class="btn btn-primary"> Print</button>-->
          <button type="button" name="fee" onClick="smsDivNoHeader()" class="btn btn-primary"> SMS</button>
          <?php
						}
						?>
          <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" id="term">
                                    <option value="">Term</option>
                                    <?php 
                                        $class=$db->getRows("select * from examname ");
                                        foreach($class as $cla){
                                            ?>
                                            <option value="<?php echo $cla["name"] ?>">
                                                <?php echo $cla["name"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </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="";

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

$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");

	if(is_array($allStudent) && count($allStudent)>0){ $he=0;
	$percentArray = array();
		foreach($allStudent as $aryForm){$mss++;$he++;

// 	  if($_POST["term"]=="PT1" || $_POST["term"]=="NOTE1" || $_POST["term"]=="SUBEN1" || $_POST["term"]=="HALFYEARLY" || $_POST["term"]=="PT2" || $_POST["term"]=="NOTE2" || $_POST["term"]=="SUBEN2"  ){
	  ?>
	  <!--one page <div style="height:942px">end --> 
	  <!--two page-->
	  <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 name='".$_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->getRow("select * from examterm  where session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."' $termWhere and class='".$_REQUEST["class"]."' $wheresection ");

		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: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="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>
				
				<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($examtype['percent']==""){
						    ?>
						<td style="border-right:2px solid #D5DA13;border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">TOTAL </td>    
						    <?php
						}

						 $finalObtain=0;$finalTotal=0;

							?>
						<td style="border-right:2px solid #D5DA13;border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">
                        <?php
                            echo $examtype["full_name"];
							if($examtype['percent']!=""){
							    echo "<br>(".$examtype['percent'].")";
							}

							 ?>
                       </td>

                       <td style="border-bottom: 2px solid #D5DA13;padding:5px;color:#000;">GRADE</td>

					</tr>

					<?php $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){ $fsAry=array();$final="";
						$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){
						
						$examResultData=$db->getRow("select * from exammarks where idno='".$aryForm["idno"]."' and type='".$examtype['name']."' and subject='".$d["subject"]."'");
						
				// 		if($examResultData["obtain"]!="NA"){
						
        				array_push($xvalues, $d['subject']);
						?><tr>
								<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;"><?php echo ucfirst($d["subject"]) ?></td>
								<?php

    								if($examtype['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"]; ?> </td>
									<td style="border-bottom: 2px solid #D5DA13;color:#000;">
    								    <?php
    								        echo FindGrades($examResultData["obtain"],$examResultData["total"]);

    								        $color = FindColour($examResultData["obtain"],$examResultData["total"]);
    								        array_push($colour, $color);
    								    ?>
    								</td>
							    <?php
    							    
    							} else {
								?>
								<td style="border-bottom: 2px solid #D5DA13;border-right:2px solid #D5DA13;color:#000;">

								 <?php
								 $obtain = number_format($examResultData["obtain"]*$examtype['percent']/$examResultData["total"],1)+0;
								 if($examResultData["obtain"]=="AB"){
                                    echo $examResultData["obtain"];
								 } else {
								     echo round($obtain);
								 }

								$finalTotal+=   $examtype['percent'];
                                $finalObtain+=  round($obtain);

                                ?>
								</td>
								<td style="border-bottom: 2px solid #D5DA13;text-align:center;color:#000;">
								    <?php
								        echo FindGrades($obtain,$examtype['percent']);
								    ?>
								</td>
								<?php
    								array_push($yvalues, (int) round($obtain));

							        $color = FindColour((int) round($obtain),$examtype['percent']);
							        array_push($colour, $color);
							        
							        $maxmarks = $examtype['percent'];
        							}
								?>
							</tr>
						<?php
				// 		}
						}
                        }
						?>

				</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($permission["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){
                                    $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($data['view_remark']==1){ ?>
                    <br>    <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>
                    
                    <?php if($permission["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>

                <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
		}
			?>
	   <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>
            $("#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>
                    <?
                }
            }
            
        }

	
}


	  
?> 
<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>

