<?php include('parent/callMe.php');?>
<?php include('Sessioncheck.php');?>
<?php   
  
   if($_REQUEST['class_id']!=NULL){  
  
      if($_REQUEST['class_id']!=NULL){
        $m->where("class_id", $_REQUEST['class_id']);  
      }      
      if($_REQUEST['session']!=NULL){
        $m->where("session_id", $_REQUEST['session']);        
      }
      if($_REQUEST['section']!="All"){
        $m->where("section_id", $_REQUEST['section']);        
      }
     
       $m->where("status", '0');
       $m->where("branch_id", $_SESSION['branch']);

       $WorkAssignListings = $m->get("edu_student_details");
 }
        $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']);
        $sessionID=$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 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 $sessionID[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 } ?>
                <?php if($_GET['cell_no']==1){?> 
               <th width="8%" class=" center" > Cell Number </th>  
               <?php } ?>
                <th width="2%" class=" center">  </th>  

         <?php 
              
              $new_counter=0;
              // printing all the relevant subjects of each class
                   
              $m->where("branch_id", $_SESSION['branch']);
              $m->where("session_id", $_REQUEST['session']);
              $m->where("class_id", $_REQUEST['class_id']);
              $m->where("section_id", $WorkAssignListings[0]['section_id']);
              /*$m->orderBy("subject_id","asc");*/

              $teacher_sbj = $m->get("eduportal_class_subjects");

              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", $WorkAssignListings[0]['section_id']);
                $m->where("subject_id", $teacher_sb['subject_id']);

                $tmarks = $m->get("eduportal_exam_marksheet"); 

               
                ?>

                 <th width="8%" class=" center"> <?php echo $subjects[0]['subject']."<br>".$tmarks[0]['total_marks']; ?> </th> 
             
             <?php  $new_counter++;}  ?> 
       
               <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();

     // getting all the marks of students and other data

      foreach($WorkAssignListings as $WorkAssignListVal)
      { 
        $total_marks;
        $total_obt;
     

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

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

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

        $m->where("status", 0);
        $m->where("branch_id", $_SESSION['branch']);
        $m->where("student_id", $WorkAssignListVal['student_id']);
        $students = $m->get("edu_student_details");


        $m->where("subject_id", $WorkAssignListVal['subject_id']);
        $subjects=$m->get("eduportal_subject_config");

        // getting status of students marks


          $m->where("branch_id", $_SESSION['branch']);
          $m->where("term_id", $_REQUEST['term_id']);
          $m->where("session_id", $_REQUEST['session']);
          $m->where("student_id", $students[0]['student_id']);
          $sms_status = $m->get("eduportal_exam_marksheet");



              

          // printing all the relevant subjects of each class
                     
              $m->where("branch_id", $_SESSION['branch']);
              $m->where("session_id", $_REQUEST['session']);
              $m->where("class_id", $_REQUEST['class_id']);
              $m->where("section_id", $WorkAssignListVal['section_id']);

              $teacher_sbj = $m->get("eduportal_class_subjects");
              $sbs_marks="";
              foreach($teacher_sbj as $teacher_sb) {

                $m->where("branch_id", $_SESSION['branch']);
                $m->where("term_id", $_REQUEST['term_id']);
                $m->where("session_id", $_REQUEST['session']);
                $m->where("subject_id", $teacher_sb['subject_id']);
                $m->where("student_id", $students[0]['student_id']);
                $marks = $m->get("eduportal_exam_marksheet"); 

                $total_marks+=$marks[0]['total_marks'];
                $total_obt+=$marks[0]['obtained_marks'];

                $sbs_marks=$marks[0]['obtained_marks'].",".$sbs_marks;
              
              
                } // closing subjects marks loop
                
             $avg=($total_obt/$total_marks)*100;
             $avg=$avg+0;
             // getting average of total marks
               $grade = getGrade($avg,$m);      

               $new_array['name_cell']=$students[0]['first_name'];
               $new_array['roll']=$students[0]['candidate_roll_no'];
               $new_array['cell_no']=$students[0]['cell_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']=$grade;
               $new_array['branch_id']=$_SESSION['branch'];
               $new_array['session']=session_id();
               $new_array['sms']=$sms_status[0]['sms_status'];
               $new_array['student_id']=$WorkAssignListVal['student_id'];
               /*$new_array['cell_no']=$students[0]['cell_no'];*/
        
        
              // print_r($new_array);            
               $m->insert("eduportal_exam_temp_marks",$new_array); 
  

  $total_marks=0; $total_obt=0; 

  }// closing main loop    
    
    
    

           $query="select * from eduportal_exam_temp_marks where branch_id=".$_SESSION['branch']." order by percantage desc";
           $std_data=$m->query($query); 
          // echo $query;
   
           $position;
           
           for($i=0;$i<count($std_data);$i++){
            $postition=ordinal($count);

     
   ?>

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

                  <?php if($_GET['cell_no']==1){?>
                  <td class=" center"> <?php echo $std_data[$i]['cell_no'];?> </td>
                <?php } ?>
                  <?php 
                  $sbs=explode(",",$std_data[$i]['sub_marks']);
                   
                   //echo print_r(array_reverse($sbs))."="; 
                   $sbs=array_reverse($sbs);
                  $condition=count($sbs);
                  $ii=0;
                  foreach($sbs as $sb) { 

                 /*   $percentage = ($sb/$tmarks[0]['total_marks'])*100;

                    $grade1 = getGrade($percentage,$m);*/

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

                  <td width="8%" style="text-align:center;<?php if(!is_numeric($sb) && $sb!=NULL){echo 'background-color:gray;';} ?><?php if($grade1=="F"){echo "background-color:#E26867;color:white;";}?>"> <?  echo $sb; ?> </td> 
                   <?php  $ii++;  } 
                   ?>
             

                  <td class=" center">  <?php echo $std_data[$i]['total_marks'];?>  </td>
                  <?php if($_GET['obtained']==1){?>                  
                  <td class=" center" style="border-right: 1px solid black;<?php if($std_data[$i]['grade']=="F"){echo "background-color:#E26867;color:white;";}?>">  <?php echo $std_data[$i]['obt_marks'];?> </td>
                  <?php } ?>
                  <?php if($_GET['percentage']==1){?> 
                  <td class=" center">  <?php echo number_format($std_data[$i]['percantage'],1);?>% </td> <?php } ?>
                  <?php if($_GET['grade']==1){?>
                  <td class=" center" style="" > <?php echo $std_data[$i]['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>
