<?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']);

     
       $param = array('student_id','ref_no','first_name','last_name','cell_no','branch_id','class_id','candidate_roll_no','section_id','session_id','status');
       $WorkAssignListings = $m->get("edu_student_details",null,$param);

       //print_r($WorkAssignListings);
 }
      
        $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>
   <?php include('RootMe/css_links.php');?>
<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:13px;}
tr td {border-bottom:1px solid black;border-left:1px solid black;font-size:12px;}
  
.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:50%;float:left;font-size:16px;">  <b> Class/Section:</b> 

<?php echo getClassName($m,$_REQUEST['class_id']);?>/<?php echo getSectionName($m,$_REQUEST['section']);?>  




</div>

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

<div style="text-align:center;width:25%;float:left;font-size:16px;"> <b> Session:</b> <?php echo $sessionID[0]['session_year'];?> </div>






<br><br>
   


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

                <?php if($_GET['admwise']==1){?>
                <th width="5%" class=" center">ADM</th>
                <?php } ?>

                <?php if($_GET['cell_no']==1){?> 
               <th width="5%" class=" center" > Cell Number </th>  
               <?php } ?>
                
         <?php 
              
              $new_counter=0;
              // printing all the relevant subjects of each class
                            
              


             $qrr="SELECT sb.subject_id,sb.subject FROM eduportal_exam_marksheet as m, eduportal_subject_config as sb where m.term_id=".$_REQUEST['term_id']." and m.class_id=".$_REQUEST['class_id']." and m.section_id=".$WorkAssignListings[0]['section_id']." and m.session_id=".$_REQUEST['session']." and m.branch_id=".$_SESSION['branch']." and sb.subject_id=m.subject_id group by m.subject_id";

           //  $qrr="SELECT csb.*,sb.* FROM eduportal_class_subjects as csb,eduportal_subject_config as sb WHERE csb.session_id=".$_REQUEST['session']." and csb.class_id=".$_REQUEST['class_id']." and csb.section_id=".$WorkAssignListings[0]['section_id']." and csb.branch_id=".$_SESSION['branch']." and csb.subject_id=sb.subject_id order by csb.subject_order asc";

               $teacher_sbj = $m->query($qrr);

              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']);
             

                $param7 = array('total_marks');
                $tmarks = $m->get("eduportal_exam_marksheet",null,$param7);*/

                $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="5%" class=" center"> <?php echo $subjects[0]['subject']."<br>".$tmarks[0]['total_marks']; ?> </th> 
             
             <?php  $new_counter++;}  ?> 
       
               <th width="5%" class=" center"> Total </th> 
               <?php if($_GET['obtained']==1){?> 
               <th width="5%" class=" center"> Obtained </th>  
               <?php } ?>
               <?php if($_GET['percentage']==1){?> 
               <th width="5%" class=" center"> % </th> 
               <?php } ?> 
               <?php if($_GET['grade']==1){?> 
               <th width="5%" class=" center" > Grade </th>  
               <?php } ?>
               <?php if($_GET['position']==1 && $_REQUEST['order']!='Rollno' && $_GET['order']!="Alpha" ){?>
               <th width="5%" 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("session_id", $WorkAssignListVal['session_id']);
        $sessionID=$m->get("edu_session_config");
        


        $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", $WorkAssignListVal['student_id']);
          $sms_status = $m->get("eduportal_exam_marksheet");



              

          // printing all the relevant subjects of each class
                     
              //$qrr="SELECT csb.*,sb.* FROM eduportal_class_subjects as csb,eduportal_subject_config as sb WHERE csb.session_id=".$_REQUEST['session']." and csb.class_id=".$_REQUEST['class_id']." and csb.section_id=".$WorkAssignListings[0]['section_id']." and csb.branch_id=".$_SESSION['branch']." and csb.subject_id=sb.subject_id";

            
              $sbs_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']);
                $m->where("class_id", $_REQUEST['class_id']);
                $m->where("section_id", $WorkAssignListVal['section_id']);
                $marks = $m->get("eduportal_exam_marksheet"); 

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

               if ($marks[0]['obtained_marks']=="") {
                 
                 $sbs_marks=" ".",".$sbs_marks;

               } else {

                $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']=$WorkAssignListVal['first_name'];
               $new_array['last_name']=$WorkAssignListVal['last_name'];
               $new_array['roll']=$WorkAssignListVal['candidate_roll_no'];
               $new_array['ref_no']=$WorkAssignListVal['ref_no'];
               $new_array['cell_no']=$WorkAssignListVal['cell_no'];
               $new_array['sub_marks']=rtrim($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']=$WorkAssignListVal['cell_no'];*/
        
        
              // print_r($new_array);            
               $m->insert("eduportal_exam_temp_marks",$new_array); 
  

  $total_marks=0; $total_obt=0; 

  }// closing main loop    
    
    
    

    if($_GET['order']=="Rollno"){

        $query="select * from eduportal_exam_temp_marks where branch_id=".$_SESSION['branch']." ORDER BY CONVERT(SUBSTRING(roll,3),UNSIGNED) DESC";

    } elseif($_GET['order']=="admwise") {

       $query="select * from eduportal_exam_temp_marks where branch_id=".$_SESSION['branch']." order by CAST(ref_no as unsigned)  asc";
      //print_r($query);
    } elseif($_GET['order']=="Alpha") {

       $query="select * from eduportal_exam_temp_marks where branch_id=".$_SESSION['branch']." order by name_cell asc";
 
    } elseif($_GET['order']=="position"){

    $query="select * from eduportal_exam_temp_marks where branch_id=".$_SESSION['branch']." order by cast(obt_marks as decimal(38,10)) DESC";
    }

           $std_data=$m->query($query); 
          // echo $query;
   
           $position;
           
           for($i=0;$i<count($std_data);$i++){

            $postition=ordinal($count);

      $GrandTotal+=$std_data[$i]['total_marks'];
      $GrandObtained+=$std_data[$i]['obt_marks'];
   ?>

       <tr>   
       
                  <td class=" center" style="padding:3px;"> <?php echo $SR;  $SR++;?> </td>
                  <td style="padding-left:1%;"><?php echo $std_data[$i]['name_cell']." / ".$std_data[$i]['last_name'];  ?></td>

                  <?php if($_GET['rollno']==1){?>

                  <td class=" center"> <?php echo $std_data[$i]['roll'];?> </td>

                <?php } ?>

                <?php if($_GET['admwise']==1){?>

                  <td class=" center"> <?php echo $std_data[$i]['ref_no'];?> </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:lightgray;';} ?><?php if($grade1=="F"){echo "background-color:#efa7a7;color:black;";}?>"> <?  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:#efa7a7;color:black;";}?>">  <?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="border-right:1px solid gray;" > <?php echo $std_data[$i]['grade'];?> </td>
                  <?php } ?>
                  <?php if($_GET['position']==1 && $_REQUEST['order']!='Rollno' && $_GET['order']!="Alpha"){?>
                  <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
      } 

   $countnumber="0";
   $fixcounter="2";
   if ($_REQUEST['rollno']=='1') {
     $countnumber=1;
   }

   if ($_REQUEST['cell_no']=='1') {
     $countnumber=1;
   }

   if ($_REQUEST['admwise']=='1') {
     $countnumber=1;
   }

   if ($_REQUEST['rollno']=='1' AND $_REQUEST['admwise']=='1') {
     $countnumber=2;
   }


   if ($_REQUEST['cell_no']=='1' AND $_REQUEST['admwise']=='1') {
     $countnumber=2;
   }

   
   if ($_REQUEST['rollno']=='1' AND $_REQUEST['cell_no']=='1') {
     $countnumber=2;
   }

   if ($_REQUEST['rollno']=='1' AND $_REQUEST['admwise']=='1' AND $_REQUEST['cell_no']=='1') {
     $countnumber=3;
   }

//echo $countnumber;

      ?>

      <tr >
        <th colspan="<?php echo $fixcounter+$countnumber;?>" style="text-align: center;background-color: lightgray; padding: 8px;">Subject Wise Percentage</th>
        <?php 
        foreach($teacher_sbj as $teacher_sb) {

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

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



                $branchID=$_SESSION['branch'];
                $termID=$_REQUEST['term_id'];
                $SessionIDs=$_REQUEST['session'];
                $SubjectID=$teacher_sb['subject_id'];
                $ClassIDs=$_REQUEST['class_id'];
                $sectionIDs=$_REQUEST['section'];

                $qrqr="SELECT SUM(total_marks) as TM, SUM(obtained_marks) as OM FROM eduportal_exam_marksheet WHERE branch_id='$branchID' and term_id='$termID' and session_id='$SessionIDs' and subject_id='$SubjectID' and class_id='$ClassIDs' and section_id='$sectionIDs'"; 
                //print($qrqr);
                $TOMarks = $m->query($qrqr);
               

         ?>

        <th style="text-align: center;"><?php echo number_format($TOMarks[0]['OM']*100/$TOMarks[0]['TM']);?>% </th>


        <?php } ?>
        <th colspan="3" style="text-align: center;background-color: lightgray;">Class Percentage</th>
        <th colspan="2" style="border-right: 1px solid black;text-align: center;"><?php echo number_format($GrandObtained*100/$GrandTotal);?>%</th>
      </tr>



      <tr >
        <th colspan="<?php echo $fixcounter+$countnumber;?>" style="text-align: center;background-color: lightgray; padding: 8px;">Total Pass / Fail</th>
        <?php 

               $qrr="SELECT sb.subject_id,sb.subject, m.obtained_marks, m.total_marks FROM eduportal_exam_marksheet as m, eduportal_subject_config as sb where m.term_id=".$_REQUEST['term_id']." and m.class_id=".$_REQUEST['class_id']." and m.section_id=".$WorkAssignListings[0]['section_id']." and m.session_id=".$_REQUEST['session']." and m.branch_id=".$_SESSION['branch']." and sb.subject_id=m.subject_id group by m.subject_id";

                 $class_sbj = $m->query($qrr);

              foreach($class_sbj as $class_sbjs) {         

              $SubjectIDs=$class_sbjs['subject_id'];

                $qrqr1="SELECT obtained_marks, total_marks FROM eduportal_exam_marksheet WHERE branch_id='$branchID' and term_id='$termID' and session_id='$SessionIDs' and subject_id='$SubjectIDs' and class_id='$ClassIDs' and section_id='$sectionIDs'"; 

                $TOMarks1 = $m->query($qrqr1);

    $total=0;
    $total2=0;
    foreach($TOMarks1 as $WorkAssignListVal1)
      { 

        $avg1=($WorkAssignListVal1['obtained_marks']/$WorkAssignListVal1['total_marks'])*100;
        // now find the right grade of each subject

       $query2="SELECT grade_id,grade_title FROM eduportal_exam_school_grades WHERE percantage_start_range <=".$avg1." and percantage_end_range >=".$avg1." and branch_id=".$_SESSION['branch'];
       $grades2 =  $m->query($query2);

       if($grades2[0]['grade_title']=="F"){
         $total++;
       }
       else {
        $total2++; 
       }

      }
               

$m->delete("eduportal_exam_temp_marks");
/*$qr="delete * from eduportal_exam_temp_marks where branch_id=".$_SESSION['branch'];
echo $qr;
$m->query($qr);*/  ?>

        <th style="text-align: center;"><?php echo $total2." / ".$total;?> </th>


        <?php  } ?>
        <th colspan="3" style="text-align: center;background-color: lightgray;"><!-- Class Percentage --></th>
        <th colspan="2" style="border-right: 1px solid black;text-align: center;"><?php //echo number_format($GrandObtained*100/$GrandTotal);?></th>
      </tr>

           
            
           
        </tbody>
        
    </table>
<?php include('RootMe/js_linksscript.php');?>

  <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
   
   <script src="js/FileSaver.min.js" type="text/javascript"></script>
  <script src="js/tableexport.min.js" type="text/javascript"></script>

  <script type="text/javascript">
    $('#excelexpo').tableExport();
  </script>
</body>
</html>
