<?php include('parent/callMe.php');?>
<?php include('Sessioncheck.php');?>
<?php   
  
       // getting branch details

        $m->where("branch_id", $_SESSION['branch']);
        $branch1=$m->get("edu_branch_config");

        $m->where("term_id", $_REQUEST['term_id']);
        $TermsData=$m->get("eduportal_exam_terms");

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

$sessionIDs=$_REQUEST['session'];
$termID=$_REQUEST['term_id'];
$subjectID=$_REQUEST['subjects'];
$branchID=$_SESSION['branch'];
$cls=$_REQUEST['cls_id'];

if($_REQUEST['term_id']!=NULL){  
  
    if ($_REQUEST['cls_id']=="All") {
      $qrr="SELECT * FROM eduportal_exam_marksheet  WHERE session_id='$sessionIDs' and term_id='$termID' and subject_id='$subjectID' and branch_id='$branchID' LIMIT 50";
    } else {

      $qrr="SELECT * FROM eduportal_exam_marksheet  WHERE session_id='$sessionIDs' and term_id='$termID' and subject_id='$subjectID' and class_id='$cls' and branch_id='$branchID' LIMIT 50";
      
      }
       $ToperWiseDatas = $m->query($qrr);
    
  }



       // getting branch details

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


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 Subject Wise Topper Report</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-left:1px solid black;border-top:1px solid black;font-size:14px;}
tr td {border-bottom:1px solid black;border-left:1px solid black;font-size:16px;}
  
.border{border:1px solid black;}

.alignment{padding-left:1%;text-align:left;}

.borderbottom{
  border-bottom:1px solid black;
}



@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="text-align:center;font-size:24px;font-weight:bold;margin:0 auto;"><?php echo $branch1[0]['Branch_Name'];?> 
</div>

<br>

<div style="text-align:center;font-size:18px;font-weight:bold;"> Subject Wise Topper Sheet

 </div>

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



<div style="width:33%; float:left;font-size:18px; margin-left:10px; "> <b> Term:</b> <?php echo $TermsData[0]['term_title'];?> </div>

<div style="text-align:center;width:33%;float:right;font-size:18px;"> <b> Session:</b> <?php echo $sessionData[0]['session_year']; ?> </div>

<br>
   


    <form class="form-style-7" action="" method="get"  onSubmit="return student_check();"  enctype="multipart/form-data">


  
  
  <input type="hidden"  name="branch" value="<?php echo $_SESSION['branch']; ?>" />
  <input type="hidden" id="session"  name="session" value="<?php echo $_GET['session']; ?>" />
  <input type="hidden"  name="month" value="<?php echo $_GET['month']; ?>" />

    <table class="table table-bordered"  style="width:100%;font-size:10px;">
          
          <thead>
            <tr>
                <th width="3%" class=" center">S.No</th>


                <th width="20%" class=" center">Student Name</th>
                <th width="7%" class=" center">Father Name</th>                
                <th width="8%" class=" center"> Class/Section </th>  
                <th width="8%" class=" center"> Total Marks </th>  
                <th width="8%" class=" center"> Obtained Marks</th>
                <th width="8%" class=" center"> Percentage</th>
                <th width="8%" class=" center" > Grade </th>  
                <th width="5%" class=" center" style="border-right: 1px solid black; "> Position </th>               
  

            </tr>
        </thead>
        <tbody>
   
     
       
     <?php 
     $count=1;
     $SR=1;
     $new_array = array();

      $sbs_marks="";
      $sbs_tmarks="";
      $sbs_title="";
      $sbs_msg1;

     // getting all the marks of students and other data
 
      foreach($ToperWiseDatas as $WorkAssignListVal)
      { 
        $total_marks;
        $total_obt;
            
          $m->where("branch_id", $_SESSION['branch']);
          $m->where("student_id", $WorkAssignListVal['student_id']);         
          $StudentsData = $m->get("edu_student_details");

   //  echo $WorkAssignListVal['student_id'];


                $total_marks=$WorkAssignListVal['total_marks'];
                $total_obt=$WorkAssignListVal['obtained_marks'];

                $sbs_marks=$WorkAssignListVal['obtained_marks'];              
                

                  if($WorkAssignListVal['obtained_marks']!=NULL && $WorkAssignListVal['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;              
    
                  }


             $avg=($total_obt/$total_marks)*100;
             $avg=$avg+0;
             // getting average of total marks
             

             $query="SELECT * FROM eduportal_exam_school_grades WHERE percantage_start_range <=".round($avg)." and percantage_end_range >=".round($avg)." and branch_id=".$_SESSION['branch'];
     
             $average =  $m->query($query);

             
            
   
        
               $new_array['name_cell']=$StudentsData[0]['first_name'];
               $new_array['last_name']=$StudentsData[0]['last_name'];
               $new_array['roll']=$StudentsData[0]['candidate_roll_no'];
               $new_array['ref_no']=$StudentsData[0]['ref_no'];
               $new_array['class_id']=$StudentsData[0]['class_id'];
               $new_array['section_id']=$StudentsData[0]['section_id'];
               $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['student_id']=$WorkAssignListVal['student_id'];
               $new_array['cell_no']=$StudentsData[0]['cell_no'];
              // $new_array['sbs_total']=$sbs_msg1;

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

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



if($_REQUEST['term_id']!=NULL){  
     

   $query="select * from eduportal_subjectwise_toper_temp 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++){
     
     
        $m->where("class_id", $std_data[$i]['class_id']);
        $class=$m->get("edu_class_config");

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

?>  
  <tr>
    
    <td class=" center"> <?php echo $SR;  $SR++;?> </td>
        <td style="padding-left:1%;"><?php echo $std_data[$i]['name_cell'];  ?></td>
        <td class="center"> <?php echo $std_data[$i]['last_name'];?> </td>
        <td class="center"> <?php echo $class[0]['Class_Name']." / ".$section[0]['section_name'];?> </td>
        <td class="center"> <?php echo $std_data[$i]['total_marks'];?> </td>
        <td class="center"> <?php echo $std_data[$i]['obt_marks'];?> </td>
        <td class="center"> <?php echo $std_data[$i]['percantage']."%";?> </td>
        <td class="center"> <?php echo $std_data[$i]['grade'];?> </td>
        <td class=" center" style="border-right: 1px solid black; " > <?php 

             $postition=ordinal($count);


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

              }else {
                
                $count+=1;
              }    


                   echo $postition; 



          $sms=$std_data[$i]['name_cell']." (".$class[0]['Class_Name']."-".trim($terms[0]['term_title']).") ".$std_data[$i]['sbs_total']." TM:".$std_data[$i]['obt_marks']."/".$std_data[$i]['total_marks']." Position:".$postition."
          ".$brid[0]['msg_name'];   
              // echo $sms."<br>";

                   ?> 


                   </td>


  </tr>

<?php }

$m->delete("eduportal_subjectwise_toper_temp"); 

 ?>
        </tbody>
        
    </table>

</form>

    

</body>
</html>
