<?php include('parent/callMe.php');?>
<?php include('Sessioncheck.php');?>
<?php   
  
ini_set('max_execution_time', '300'); //300 seconds = 5 minutes
ini_set('max_execution_time', '0'); // for infinite time of execution 

$branch=$_SESSION['branch'];
$term=$_REQUEST['term_id'];
$session=$_REQUEST['session'];
$section=$_REQUEST['section'];
$class=$_REQUEST['class_id'];



 if($_REQUEST['class_id']!=NULL){  
  
      $qrr = "SELECT * FROM  eduportal_exam_marksheet  WHERE branch_id='$branch' and term_id='$term' and session_id='$session' and class_id='$class' and section_id='$section' group by student_id";
//print_r($qrr);
      $WorkAssignListings = $m->query($qrr);

  }


        $m->where("class_id", $_REQUEST['class_id']);
        $class=$m->get("edu_class_config");

        $m->where("section_id", $WorkAssignListings[0]['section_id']);
        $section=$m->get("edu_adm_section_detail");
       

        $m->where("session_id", $_REQUEST['session']);
        $session=$m->get("edu_session_config");


       // getting branch details

        $m->where("branch_id", $_SESSION['branch']);
        $branch=$m->get("edu_branch_config");
   
       $m->where("term_id", $_REQUEST['term_id']);
       $terms = $m->get("eduportal_exam_terms");


function ordinal($number) {
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    if ((($number % 100) >= 11) && (($number%100) <= 13))
        return $number. 'th';
    else
        return $number. $ends[$number % 10];
}
?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Print Old Result Sheet</title>
<link rel="icon" type="image/png" href="img/favicon.png" sizes="32x32" />
<script type="text/javascript">
// window.print();
</script>
<style type="text/css">

tr th {border:1px solid black;text-align:center;background-color:lightgray;font-size:14px;}
tr td {border-bottom:1px solid black;border-left:1px solid black;font-size:16px;}
  
.border{border:1px solid black;}
.center{text-align:center;}
.alignment{padding-left:1%;text-align:left;}



@media print {
    #Header, #Footer { display: none !important; }
}
@media screen
   {
      p.bodyText {font-family:verdana, arial, sans-serif;}
   }

   @media print
   {
      p.bodyText {font-family:georgia, times, serif;}
   }
   @media screen, print
   {
      p.bodyText {font-size:10pt}
   }
   @media print {
  a[href]:after {
    content: none !important;
  }
}
.center{text-align:center;}
</style>
</head>

<body style="font-size:12px;padding-right:1%;padding-left:1%;padding-top:3%;padding-bottom:1%;">

  <div style="width:20%;font-size:13px;text-align:center;float:left;height:80px;">

         <img src="<?php echo DOMAIN.AgentDirectory;?>img/<?php echo $branch[0]['logo']; ?>" style="height:85px;width:85px;margin-top: -13px;" />

  </div>

<div style="text-align:center;font-size:25px;font-weight:bold;margin:0 auto;"><?php echo $branch[0]['Branch_Name'];?> 
</div>

<br>

<div style="text-align:center;font-size:20px;font-weight:bold;"> RESULT SHEET

 </div>

<hr style="width:100%;">

<div style="width:33%;float:left;font-size:22px;">  <b> Class Name:</b> 

<?php echo $class[0]['Class_Name'];?> 

</div>

<div style="width:33%; float:left;font-size:22px; "> <b> Paper:</b> <?php echo $terms[0]['term_title'];?> </div>

<div style="text-align:center;width:33%;float:left;font-size:22px;"> <b> Session:</b> <?php echo $session[0]['session_year'];?> </div>
<!-- 
<div style="text-align: right;width:20%;float:right;"> <b> Printed:</b> <?php echo date("d-M-Y");?> </div>


<div style="text-align:right;width:20%;float:right;"> <b> Total Strength:</b> <?php echo count($WorkAssignListings);?> </div>
 -->
<br>
   


    <table style="width:100%;font-size:16px;" cellspacing="0">
          
          <thead>
            <tr>
                <th width="3%" class=" center">S.No</th>
                <th width="20%" class=" center">Name</th>
                <?php if($_GET['rollno']==1){?>
                <th width="7%" class=" center">R.NO</th>
                <?php } ?>
                <th width="2%" class=" center">  </th>  

        <?php 
                $new_counter=0;
              // printing all the relevant subjects of each class
            $branch=$_SESSION['branch'];
            $term=$_REQUEST['term_id'];
            $session=$_REQUEST['session'];
            $section=$_REQUEST['section'];
            $class=$_REQUEST['class_id'];

              $qrr1 = "SELECT * FROM  eduportal_exam_marksheet  WHERE branch_id='$branch' and term_id='$term' and session_id='$session' and class_id='$class' and section_id='$section' group by subject_id";

             //print_r($qrr1);
            $teacher_sbj = $m->query($qrr1);

              foreach($teacher_sbj as $teacher_sb) {

               
                $m->where("subject_id", $teacher_sb['subject_id']);
                $subjects = $m->get("eduportal_subject_config");
   
  
                // getting subject total marks

                $m->where("branch_id", $_SESSION['branch']);
                $m->where("term_id", $_REQUEST['term_id']);
                $m->where("session_id", $_REQUEST['session']);
                $m->where("class_id", $_REQUEST['class_id']);
                $m->where("section_id", $_REQUEST['section']);
                $m->where("subject_id", $teacher_sb['subject_id']);
                $tmarks = $m->get("eduportal_exam_marksheet"); 

               
                ?>

                 <th width="6%" class="center"> <?php echo $subjects[0]['subject']."<br>".$tmarks[0]['total_marks']; ?> </th> 
                       
         <?php  }  ?> 
       
               <th width="8%" class=" center"> Total </th> 
               <?php if($_GET['obtained']==1){?> 
               <th width="8%" class=" center"> Obtained </th>  
               <?php } ?>
               <?php if($_GET['percentage']==1){?> 
               <th width="8%" class=" center"> % </th> 
               <?php } ?> 
               <?php if($_GET['grade']==1){?> 
               <th width="8%" class=" center" > Grade </th>  
               <?php } ?>
               <?php if($_GET['position']==1){?>
               <th width="8%" class=" center" style="border-right:1px solid gray;"> <!-- Position --> </th>  
               <?php } ?>
                 
            </tr>
        </thead>
        <tbody>


   
     
       
      <?php 
     $count=1;
     $SR=1;
     $new_array = array();

   
      foreach($WorkAssignListings as $WorkAssignListVal)
      { 
        $total_marks;
        $total_obt;
     
                 
        
          $m->where("branch_id", $_SESSION['branch']);
          $m->where("student_id", $WorkAssignListVal['student_id']);
          $studentsDetails = $m->get("edu_student_details");

          // printing all the relevant subjects of each class
          
             $StdId=$WorkAssignListVal['student_id'];

          $qrr2 = "SELECT * FROM  eduportal_exam_marksheet  WHERE branch_id='$branch' and term_id='$term' and session_id='$session' and class_id='$class' and section_id='$section' group by subject_id";
          
         // print_r($qrr);
          $teacher_sbj = $m->query($qrr2);
                     
              $sbs_marks="";
              $sbs_tmarks="";
              $sbs_title="";
              $sbs_msg1;


              $student_marks;
  
              foreach($teacher_sbj as $teacher_sb) {

                $m->where("term_id", $_REQUEST['term_id']);
                $m->where("session_id", $_REQUEST['session']);
                $m->where("subject_id", $teacher_sb['subject_id']);
                $m->where("branch_id", $_SESSION['branch']);
                $m->where("student_id", $WorkAssignListVal['student_id']);
                $marks = $m->get("eduportal_exam_marksheet"); 
//print_r($marks);
                $total_marks+=$marks[0]['total_marks'];
                $total_obt+=$marks[0]['obtained_marks'];
                $sbs_marks=$marks[0]['obtained_marks'].",".$sbs_marks;              
                

                  if($marks[0]['obtained_marks']!=NULL && $marks[0]['obtained_marks']!=""){
                  
                  $val=mb_substr($teacher_sb['subject'], 0, 3, "UTF-8")." ".$marks[0]['obtained_marks']."/".$marks[0]['total_marks'];
                  $sbs_msg1=$val.",".$sbs_msg1;              
    
                  }


                /*$sbs_tmarks=$marks[0]['total_marks'].",".$sbs_tmarks;              
                $sbs_title=substr($teacher_sb['subject'],0,2).",".$sbs_title;              */
              
                } // closing subjects marks loop
              

             $avg=($total_obt/$total_marks)*100;
             $avg=$avg+0;
             // getting average of total marks
             $query="SELECT grade_id,grade_title FROM eduportal_exam_school_grades WHERE percantage_start_range <=".round($avg)." and percantage_end_range >=".round($avg)." and branch_id=".$_SESSION['branch'];
           
            //echo $query."===".$WorkAssignListVal['first_name']."====".$avg."<br><br>";            

             $average =  $m->query($query);
        
               $new_array['first_name']=$studentsDetails[0]['first_name'];
               $new_array['candidate_roll_no']=$studentsDetails[0]['candidate_roll_no'];
               $new_array['sub_marks']=$sbs_marks;
               $new_array['total_marks']=$total_marks;
               $new_array['obt_marks']=$total_obt;
               $new_array['percantage']=number_format($avg,2);
               $new_array['grade']=$average[0]['grade_title'];
               $new_array['branch_id']=$_SESSION['branch'];
               $new_array['session']=session_id();
               $new_array['sms_status']=$sms_status[0]['sms_status'];
               $new_array['student_id']=$studentsDetails[0]['student_id'];
               $new_array['cell_no']=$studentsDetails[0]['cell_no'];
               $new_array['sbs_total']=$sbs_msg1;

        
        
            //  print_r($new_array)."<br><br>";            
              // $m->insert("eduportal_old_exam_marks",$new_array); 

              // $response_data = json_encode($new_array);

              //echo  "<pre>".print_r($response_data)."</pre>"; 
              //  echo  $response_data;

                $student_marks[]=$new_array;
  

  $total_marks=0; $total_obt=0;  $sbs_msg1="";

  }// closing main loop    


    
// check duplicate records 

    $m->where ("term_id", $_REQUEST['term_id']);

    $m->where ("session_id", $_REQUEST['session'][$i]);

    $m->where ("class_id", $_REQUEST['class_id']);

    $m->where ("section_id", $_REQUEST['section']);    

    $termRecord=$m->get('eduportal_old_exam_marks_json');


    if(count($termRecord)>0){

        //  continue;

        } else {
    

    $response_data = array(
      'term_id' => $_REQUEST['term_id'],
      'session_id' => $_REQUEST['session'],
      'class_id' => $_REQUEST['class_id'],
      'section_id' => $_REQUEST['section'],
      'students_marks' => json_encode($student_marks)
    );
     
    $m->insert ('eduportal_old_exam_marks_json', $response_data);

    }


 

   $termID=$_REQUEST['term_id'];
   $sessionID=$_REQUEST['session'];
   $classID=$_REQUEST['class_id'];
   $sectionID=$_REQUEST['section'];   

   $resultset = $m->query("select * from eduportal_old_exam_marks_json where term_id='$termID' and session_id='$sessionID' and class_id='$classID' and section_id='$sectionID'");

 
  $StudentsMarks=json_decode($resultset[0]['students_marks'], true);

   foreach($StudentsMarks as list("grade" => $grade, "cell_no" => $cell_no, "session" => $session, "branch_id" => $branch_id, "obt_marks" => $obt_marks, "sbs_total" => $sbs_total, "sub_marks" => $sub_marks, "first_name" => $first_name, "percantage" => $percantage, "sms_status" => $sms_status, "student_id" => $student_id, "total_marks" => $total_marks, "candidate_roll_no" => $candidate_roll_no ) ) { 
     
 // echo "{$sub_marks}"."<br>";
//echo "<pre>"; print_r($datastd); echo "</pre>";
  //  echo "<br>page";

    $m->where("student_id", $student_id);
    $studentName=$m->get("edu_student_details");

   ?>

       <tr>  
       
                  <td class=" center"> <?php echo $SR;  $SR++;?> </td>
                  <td style="padding-left:1%;"><?php echo $studentName[0]['first_name'];  ?></td>
                  <?php if($_GET['rollno']==1){?>
                  <td class=" center"> <?php echo $candidate_roll_no;?> </td>
                <?php } ?>

                  <?php 
                   
                    $sbs1=explode(",",$sub_marks);
                   
                   //echo print_r(array_reverse($sbs))."="; 
                    $sbs1=array_reverse($sbs1);
                    $condition=count($sbs1);
                    $ii=0;
                  foreach($sbs1 as $sb1) { 

                    /*if($ii==$condition){break;}*/
                    ?>

                  <td width="8%" style="text-align:center;"> <?php  echo $sb1; ?> </td> 
                   <?php  $ii++;  } 
                   ?>

                  <td class=" center">  <?php echo $total_marks;?>  </td>
                  <?php if($_GET['obtained']==1){?>                  
                  <td class=" center" style="border-right: 1px solid black;">  <?php echo $obt_marks;?> </td>
                  <?php } ?>
                  <?php if($_GET['percentage']==1){?> 
                  <td class=" center">  <?php echo number_format($percantage,1);?>% </td> <?php } ?>
                  <?php if($_GET['grade']==1){?>
                  <td class=" center" > <?php echo $grade;?> </td>
                  <?php } ?>
                  <?php if($_GET['position']==1){?>
                  <td class=" center" style="border-right:1px solid gray;"> <?php 


         /*    if($std_data[$i]['obt_marks']==$std_data[$i-1]['obt_marks']){
                $postition=ordinal($count-1);

              }else {
                
                $count+=1;
              }    
                   
                  if($std_data[$i]['grade']!="F"){

                    echo $postition; 
                  }*/
                   

                   ?> 


                   </td>
                 <?php } ?>

                                 
              </tr>




<?php
      } 

  //$m->delete("eduportal_exam_temp_marks"); 

      ?>



           
            
           
        </tbody>
        
    </table>

    

</body>
</html>
