<?php include('parent/callMe.php');?>
<?php include('Sessioncheck.php');?>
  <?php   
    $NameOfTerms=explode(",",$_REQUEST['terms']);

  if( $_REQUEST['section']!=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']!=NULL){
        $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", $_REQUEST['section']);
        $section=$m->get("edu_adm_section_detail");
       

        $m->where("session_id", $_REQUEST['session']);
        $sessionID=$m->get("edu_session_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 Multi Test Reports</title>
<link rel="icon" type="image/png" href="img/favicon.png" sizes="32x32" />
<script type="text/javascript">
  //window.print();
</script>
<style type="text/css">

	.border{border:1px solid black;}
	.center{text-align:center;}
	.alignment{padding-left:1%;text-align:left;}

	tr th {border:1px solid black; padding:1%;}
	tr td {border-top:1px solid gray; padding:0.5%;border-left:1px solid gray;border-bottom:1px solid gray;text-align:center;}

     body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        background-color: #FAFAFA;
        font: 12pt "Tahoma";
    }
    * {
        box-sizing: border-box;
        -moz-box-sizing: border-box;
    }
    .page {
        width: 297mm;
        min-height:auto;        
        padding: 20mm;
        margin: 10mm auto;
        border: 1px #D3D3D3 solid;
        border-radius: 5px;
        background: white;
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    }
    .subpage {
        width: 257mm;
        height:auto;
        
    }
    
    @page {
        margin: 0;
    }
    @media print {
        html, body {
            width:297mm;
            height:auto;        
        }
        .page {
            margin: 0;
            border: initial;
            border-radius: initial;
            width: initial;
            min-height: initial;
            box-shadow: initial;
            background: initial;
            page-break-after: blue;
        }
    }


  </style>



</head>
<body style="font-size:13px;">


<div class="book">
  <div class="page">
      <div class="subpage">



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

<br>
<div style="text-align:center;font-size:14px;font-weight:bold;"> <h1>Multi Test Reports</h1> </div>

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

<div style="width:20%;float:left;">  <b> Class Name:</b> <?php echo $class[0]['Class_Name']."-".$section[0]['section_name'];?> </div>


<div style="text-align:center;width:20%;float:left;"> <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>

 
    <table id="listofstudents" cellspacing="0" cellpadding="0" style="width:100%;font-size:11px;">
          
          <thead>
            <tr>
                <th width="2%">S.No</th>
                <th width="10%">Name</th>
                <th width="5%">R.NO</th>

 <?php 
    $sessionID=$_REQUEST['session'];
    foreach ($NameOfTerms as $NameOfTerm) {
      
      $m->where("term_id", $NameOfTerm);
      $m->where("branch_id", $_SESSION['branch']);
      $m->where("session_id", $sessionID);
      $TermTitles = $m->get("eduportal_exam_terms");

       // echo $TermTitles[0]['term_title'];
       

 

         $b=$_SESSION['branch'];
         $trm=$TermTitles[0]['term_id'];
         $clas=$_REQUEST['class_id'];
         
         $query="select SUM(obtained_marks)as obt,SUM(total_marks) as tm from eduportal_exam_marksheet where branch_id='$b' AND term_id='$trm' AND class_id='$clas'";
               
         $marks = $m->query($query);

         $avg=0+intVal($marks[0]['obt'])/intVal($marks[0]['tm'])*100;
        
        // break loop if terms printing three times

          if($marks[0]['tm']==NULL){
           continue;
          }
          else{
            
            $count++; 
          }
  
      ?>          
             <th width="8%" style="text-align:center;"> <?php echo $TermTitles[0]['term_title']; ?> </th> 
             
             <?php }  ?> 

               <th width="8%" style="text-align:center;"> Total Aggregate </th>  
               <th width="8%" style="text-align:center;"> Positions </th> 
               
            
                 
            </tr>
        </thead>
        <tbody>
  
     
     <?php 
     $counter=1;
     $total_marks=0;
     
      foreach($WorkAssignListings as $WorkAssignListVal)
      { 
        
        $total_obt;
     
echo $WorkAssignListVal['student_id'];
        $m->where("status", 0);
        $m->where("branch_id", $_SESSION['branch']);
        $m->where("student_id", $WorkAssignListVal['student_id']);
        $students = $m->get("edu_student_details");


      ?>

        <tr>
          

<?php $marking=0;

   $percentages="";
     $sessionID=$_REQUEST['session'];
    foreach ($NameOfTerms as $term_s) {
      
      $m->where("term_id", $term_s);
      $m->where("branch_id", $_SESSION['branch']);
        $m->where("session_id", $sessionID);
        $TermId = $m->get("eduportal_exam_terms");

     
       $b=$_SESSION['branch'];
       $student_id=$WorkAssignListVal['student_id'];

       $query="select SUM(obtained_marks)as obt,SUM(total_marks) as tm,sms_status from eduportal_exam_marksheet where branch_id='$b' AND term_id='$term_s' AND student_id='$student_id' AND class_id=".$_REQUEST['class_id'];
       $marks = $m->query($query);


        if(count($marks)>0){
          $avg=0+intVal($marks[0]['obt'])/intVal($marks[0]['tm'])*100;
           $percentages=$percentages.",".$avg;

        } else {
          $avg=0;
        }
        
        if($marks[0]['tm']==NULL){
         continue;
        }
              

        $total_marks=$total_marks+$avg;
         

      ?>          
    
   
   <?php  }
   // now calculating aggregate

    $aggregate=($count*100);


   $new_array['std_name']=$students[0]['first_name']." ".$students[0]['last_name'];
   $new_array['std_rollno']=$students[0]['candidate_roll_no'];
   $new_array['percentages']=$percentages;
   $new_array['total_aggregate']=($total_marks/$aggregate)*100;
   $new_array['branch_id']=$_SESSION['branch'];
           
   $m->insert("eduportal_multi_test_positions_temp",$new_array); 

   $percentages="";

    ?> 

<?php $total_marks=0; 

}

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

   $query="select * from eduportal_multi_test_positions_temp where branch_id=".$_SESSION['branch']."  GROUP by pos_temp_id order by AVG(total_aggregate) desc";

  // echo $query;

   $std_data=$m->query($query); 
  }
 //  echo $query;

  // print_r($std_data);
   
   $position;
             
  $count=1;
  $Serial=1;


  for($i=0;$i<count($std_data);$i++){
     
   
   ?>
                  <td style="text-align: center;"> <?php echo $Serial; ?> </td>
                  <td style=""><?php echo $std_data[$i]['std_name'];  ?></td>
                  <td style=""><?php echo $std_data[$i]['std_rollno'];  ?></td>
                 
                   <?php 
                   
                    $sbs1=explode(",",$std_data[$i]['percentages']);
                   //print_r($sbs1);
                   //echo print_r(array_reverse($sbs))."="; 
                    //$sbs1=array_reverse($sbs1);
                    $condition=count($sbs1);
                    $ii=0;
                  foreach($sbs1 as $sb1) { 
                    if($sb1==NULL || $sb1==""){
                      continue;
                    }

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

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


                  <td style="text-align:center;"><?php echo number_format($std_data[$i]['total_aggregate'],1);  ?> %</td>

                   <td style="text-align: center;border-right: 1px solid;"> <?php 

                   $postition=ordinal($count);


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

                    }else {
                      
                      $count+=1;
                    }    


                         echo $postition; 

                   ?> 


                   </td>
       
              </tr>
              

  <?php  $Serial++;
  
      } 

      $m->delete("eduportal_multi_test_positions_temp"); 
      ?>

   
        </tbody>
        
    </table>
  

        </div>    
    </div>      
   </div>


</body>
</html>
