<?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");
 }


       // 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">
<!-- Favicons-->
<link rel="icon" type="image/png" href="img/favicon.png" sizes="32x32" />
<title> Student Report Card</title>
<!-- Favicons-->
<link rel="icon" type="image/png" href="img/favicon.png" sizes="32x32" />

<script type="text/javascript">
  //window.print();
</script>
<style> 

.section{width:94%; height:28.5cm;float:left; /*background-color:lightgray;border: 1px solid black;*/margin:0 auto;padding:1%;margin-left:1%;}
 
 tr td {
    
    padding:1.2%;
 }

  tr th {
    padding:2%;
    font-size:14px;
    background:lightgray; 
 }
.tdalign{text-align:center;border:1px solid black;}
  

body {
  background: rgb(204,204,204);
  font-weight:; 
}
page {
  background: white;
  display: block;
  margin: 0 auto;
  margin-bottom: 0.5cm;
  border:1px solid black;
  box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);
  /*padding:1%;*/
}
page[size="A4"] {  
  width: 21cm;
  height: 29.7cm; 
}
page[size="A4"][layout="landscape"] {
  width: 29.7cm;
  height: 21cm;  
}
page[size="A3"] {
  width: 29.7cm;
  height: 42cm;
}
page[size="A3"][layout="landscape"] {
  width: 42cm;
  height: 29.7cm;  
}
page[size="A5"] {
  width: 14.8cm;
  height: 21cm;
}
page[size="A5"][layout="landscape"] {
  width: 21cm;
  height: 14.8cm;  
}
@media print {
  body, page {
    margin: 0;
    box-shadow: none;
    border:none;
  
  }
}
  </style>


</head>
<body >
  
     <?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", $_REQUEST['class_id']);
        $class=$m->get("edu_class_config");

    
        $m->where("session_id", $_REQUEST['session']);
        $session=$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=".$WorkAssignListVal['section_id']." and csb.branch_id=".$_SESSION['branch']." and csb.subject_id=sb.subject_id";

         // echo $qrr; 

              $teacher_sbj = $m->query($qrr);
              $sbs_marks="";
              $sbs_tmarks="";
              $sbs_title="";
              $sbs_msg1;

  
              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", $WorkAssignListVal['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;              
                

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


               
                } // closing subjects marks loop
                
             $avg=($total_obt/$total_marks)*100;
             $avg=intVal($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']=$WorkAssignListVal['first_name'];
               $new_array['roll']=$WorkAssignListVal['candidate_roll_no'];
               $new_array['ref_no']=$WorkAssignListVal['ref_no'];
               $new_array['sub_marks']=$sbs_marks;
               $new_array['total_marks']=$total_marks;
               $new_array['obt_marks']=round($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']=$sms_status[0]['sms_status'];
               $new_array['student_id']=$WorkAssignListVal['student_id'];
               $new_array['cell_no']=$WorkAssignListVal['cell_no'];
               $new_array['sbs_total']=$sbs_msg1;
               $new_array['last_name']=$WorkAssignListVal['last_name'];
               if(isset($WorkAssignListVal['image'])){
                $new_array['img']=$WorkAssignListVal['image'];
                }else {
                $new_array['img']=""; 
                }

               $new_array['class_id']=$WorkAssignListVal['class_id'];
               $new_array['section_id']=$WorkAssignListVal['section_id'];
               
        
        
              // print_r($new_array);            
               $m->insert("eduportal_exam_temp_marks",$new_array); 
  

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

  }// closing main loop    
    



  $query="select * from eduportal_exam_temp_marks where branch_id=".$_SESSION['branch']." order by obt_marks desc";
  $std_data=$m->query($query); 
  // echo $query;

  //print_r($std_data);

  $pos=1;

  for($i=0;$i<count($std_data);$i++) { ?>


<page size="A4"> 

 <!-- Vouchere Fist -->
<div class="section">
 
  <div style="width:100%;float:left;">
  
      <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:110px;width:110px;" />

      </div>


      
      <div style="width:60%; float:left;text-align:center;">

         <span style="font-size: 30px;font-family: Times New Roman;font-weight: bold;">  <?php echo $branch[0]['Branch_Name'];?> </span>


      </div>


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

          <?php if($std_data[$i]['img']!=NULL){ ?>
      
      <img src="<?php echo DOMAIN.AgentDirectory;?>students_pics/<?php echo $std_data[$i]['img'];?>" id="preview" class="img-thumbnail" style="width:120px;height:120px;"   />

       <?php }else { ?>   

           <img src="https://image.flaticon.com/icons/png/512/67/67902.png" id="preview" height="100px" class="img-thumbnail" width="100px"  />


       <?php } ?> 
      </div>

</div>



   <div style="width:100%; text-align:center;font-size:20px;">  
    
   <strong><u> Student Result Card of (<?php echo $terms[0]['term_title'];?>) </u>  </strong> </div>

   <br>


   <table width="100%" style="border: 2px solid black; border-radius: 15px; padding:2%;" cellspacing="0"  >  
   
   
      <tr>  
         <td width="15%" style="text-align:left;"> Student Name:   </td>
         <td width="35%" style="border-bottom: 1px solid black;text-align:center;">  <?php echo $std_data[$i]['name_cell'];  ?></td>

         <td width="15%" style="text-align:left;"> Father's Name:   </td>
         <td width="35%" style="border-bottom: 1px solid black;text-align:center;">  <?php echo $std_data[$i]['last_name'];?></td>


      </tr>
   
      <tr>  
         <td width="15%" style="text-align:left;"> Class:   </td>
         <td width="35%" style="border-bottom: 1px solid black;text-align:center;"> 
         <?php echo $class[0]['Class_Name'];?> </td>

         <td width="15%" style="text-align:left;"> Section:   </td>
          <td width="35%" style="border-bottom: 1px solid black;text-align:center;"><?php 


        $m->where("section_id", $std_data[$i]['section_id']);
        $sect=$m->get("edu_adm_section_detail");   echo $sect[0]['section_name'];?> </td>



      </tr>
   
        <tr>  
         <td width="15%" style="text-align:left;"> Adm No:   </td>
         <td width="35%" style="border-bottom: 1px solid black;text-align:center;">  <?php echo $std_data[$i]['ref_no'];?></td> 

         <td width="15%">  Roll No:  </td>
         <td width="45%" style="border-bottom: 1px solid black;text-align:center;">  <?php echo $std_data[$i]['roll'];?> </td>


      </tr>
   
  
    
   
   </table>

   <br>



   <table width="100%" style="font-size:18px;" cellspacing="0">  
   
   
         <tr>  
         
            <th width="8%"  class="tdalign" style="border-top-left-radius:15px;">S.NO </th>
            <th width="31%"  class="tdalign"> SUBJECTS </th>
            <th width="12%"  class="tdalign"> TOTAL  </th>
            <th width="12%"  class="tdalign">  OBTAINED  </th>
            <th width="12%"  class="tdalign">  PERCENTAGE </th>
            <?php if($_GET['grade']==1){?>
            <th width="10%"  class="tdalign" style="border-top-right-radius:15px; text-align:left;">Grade</th> 
            <?php } ?>
            
         </tr>
<?php 

              // 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=".$std_data[$i]['class_id']." and csb.section_id=".$std_data[$i]['section_id']." and csb.branch_id=".$_SESSION['branch']." and csb.subject_id=sb.subject_id";

         // echo $qrr; 

              $teacher_sbj = $m->query($qrr);
         
                 $counter=1;
                 $status;
                 $remarksTeacher;
              foreach($teacher_sbj as $teacher_sb) {


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



                $m->where("branch_id", $_SESSION['branch']);
                $m->where("session_id", $_REQUEST['session']);
                $m->where("term_id", $_REQUEST['term_id']);
                $m->where("class_id", $std_data[$i]['class_id']);
                $m->where("section_id", $std_data[$i]['section_id']);
                $m->where("subject_id", $teacher_sb['subject_id']);
                $m->where("student_id", $std_data[$i]['student_id']);
                $marks = $m->get("eduportal_exam_marksheet"); 
              
              $total_marks+=$marks[0]['total_marks']+0;
              $total_obt+=$marks[0]['obtained_marks']+0;
             // $percentage = $total_obt*100/$total_marks;
              $percentage = ($marks[0]['obtained_marks']/$marks[0]['total_marks'])*100;
              

       
        
          $grade = getGrade($percentage,$m); 

           if ($grade == "F") {


              $status = 1;

              $remarksTeacher = 1;
           }



           ?>

   
         <tr>  
         
            <td   class="tdalign"> <?php echo $counter; $counter++; ?> </td>
            <td   style="border:1px solid black; padding-left:3%;text-align:left;"> <?php echo $subjects[0]['subject']; ?> </td>
            <td  class="tdalign"> <?php echo $marks[0]['total_marks']; ?> </td>
            <td   class="tdalign" style="<?php if($grade=="F"){echo "background-color:#E26867;color:white;";}?>"> <?php echo $marks[0]['obtained_marks']; ?> </td>
            <td  class="tdalign">
              <?php if($_GET['percantage']==1){?> 
             <?php if($marks[0]['obtained_marks'] !=NULL ){
             echo number_format(($marks[0]['obtained_marks']/$marks[0]['total_marks'])*100,1)."%";
              } } ?>
            </td>
            <?php if($_GET['grade']==1){?> 
            <td style="border: 1px solid black;text-align: center;"><?php  echo $grade; }?></td> 
                 
            
         </tr>


   <?php     } ?>

      <tr>  
         
            <th  colspan="2" style="text-align:right;font-weight:bold;border:1px solid black;border-bottom-left-radius:15px;">TOTAL </th>
            <th   class="tdalign"><?php echo $std_data[$i]['total_marks']; ?> </th>
            <th  class="tdalign"><?php echo $std_data[$i]['obt_marks']; ?> </th>
            <th  colspan="2" class="tdalign" style="border-bottom-right-radius:15px;">
            <?php if($_GET['percantage']==1){?>
            <?php echo number_format($std_data[$i]['obt_marks']/$std_data[$i]['total_marks']*100,1)."%"; } ?> 



      </th>
         </tr>
  </table>
    <br>
         
     
     <table width="100%" style="font-size:18px;" cellspacing="0">  
         <tr>  
         
           <td colspan="" style="text-align:center;border:1px solid black;border-top-left-radius:15px;">
           <?php if($_GET['sec_pos']==1){?>
            <b>Position </b> &nbsp;&nbsp;&nbsp;  <!-- <b>Out Of</b> --> <br>
            <?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; 
} ?> <!-- <span style="margin-left:40%;"> <?php echo count($std_data); ?></span> --> </td>
        
      <td style="text-align:center;border:1px solid black;">
      <?php if($_GET['over_all']==1){?>
        <b>Grade</b> <br>
      <?php 

            $avg=$std_data[$i]['obt_marks']/$std_data[$i]['total_marks']*100;

       
           $getGradeRemarks=explode(",",getGradeRemarks($avg,$m));

            $grade = $getGradeRemarks[0]; 

            if ($status == 1) {
              
           }else {

                  echo $grade;
           }

           $status="";

           }  //print_r($getGradeRemarks);

               

            ?>
      </td>
   
      <td style="text-align:center;border:1px solid black;">
      <?php if($_GET['remarks']==1){?>
        <b>Remarks</b><br>
        <?php if ($remarksTeacher == 1){ echo "Fail"; } else { echo $getGradeRemarks[1]; } $remarksTeacher=""; }?></td>
     
      <td style="text-align:center;border:1px solid black;border-top-right-radius:15px;">
        <?php if($_GET['issuing_date']==1){?>
        <b>Issuing Date</b> <br> <?php echo date("d-M-Y"); }?> 
         </td>
            
         </tr>

    <?php if($_GET['teacher_remarks']==1) { ?>
    <tr>  
        
      <td class="tdalign"style="border-bottom-left-radius:15px;"><b>Teacher Remarks</b> </td>
      <td colspan="5" style="text-align:center;border:1px solid black;border-bottom-right-radius:15px;font-size:12px;"><?php echo $getGradeRemarks[2];?> </td>
    </tr>
    <?php } ?> 
   </table>
   
   <?php if ($_GET['attendance']=="Yes") { ?>
  

    <table  class="table table-striped table-bordered"style="width:100%;float: left;">
                      <thead>
                        <tr>
                          <th colspan="5">Attendance Details</th>
                        </tr>
                        <tr>
                          
                          <th width="20%">Presents</th>
                          <th width="20%">Absents</th>
                          <th width="20%">Leave </th>
                          <th width="20%">Short Leave</th>
                          <th width="20%">Late</th>
                          
                        </tr>
                      </thead>
                      <tbody>

                        
<?php  $count5=1;

$branchID=$_SESSION['branch'];
$STDid=$std_data[$i]['student_id']; 
$date=date('Y-m');

$qrp="SELECT count(*) as Present FROM eduportal_attendance_detail WHERE student_id='$STDid' and attendance='P' and branch_Id='$branchID' and date like '$date-%'";
$Presents=$m->query($qrp);

$qra="SELECT count(*) as Absent FROM eduportal_attendance_detail WHERE student_id='$STDid' and attendance='A' and branch_Id='$branchID' and date like '$date-%'";
$Absents=$m->query($qra);

$qrl="SELECT count(*) as Leavestd FROM eduportal_attendance_detail WHERE student_id='$STDid' and attendance='L' and branch_Id='$branchID' and date like '$date-%'";
//print_r($qrl);
$Leave=$m->query($qrl);

$qrsl="SELECT count(*) as ShortLeave FROM eduportal_attendance_detail WHERE student_id='$STDid' and attendance='SL' and branch_Id='$branchID' and date like '$date-%'";
$ShortLeave=$m->query($qrsl);

$qrla="SELECT count(*) as Late FROM eduportal_attendance_detail WHERE student_id='$STDid' and attendance='LA' and branch_Id='$branchID' and date like '$date-%'";
$Late=$m->query($qrla);

?>
                        <tr>
                          <td style="text-align:center;"><?php echo $Presents[0]['Present'];?></td>
                          <td style="text-align:center;"><?php echo $Absents[0]['Absent'];?></td>
                          <td style="text-align:center;"><?php echo $Leave[0]['Leavestd'];?></td>
                          <td style="text-align:center;"><?php echo $ShortLeave[0]['ShortLeave'];?></td>
                          <td style="border-right: 1px solid lightgray;text-align:center;"><?php echo $Late[0]['Late'];?></td>
                         
                                                                           

                         

                        </tr>

                      

                      </tbody>
                    </table>
  <?php } ?> 
   <br><br><br>
      <table width="100%" style="font-size:18px;"  cellspacing="0">  
   
   
         <tr>  
         <td  style="text-align:center;border-bottom:1px solid black;"> </td>

         <td   >   </td>

         <td  style="text-align:center;border-bottom:1px solid black;color:white;">   </td>
         
         </tr>

         <tr>  
         <td width="30%"  style="text-align:center;font-weight:bold;"> Teacher's Signature </td> 
         <td width="30%"  style="text-align:center;">  </td> 
         <td width="30%"  style="text-align:center;font-weight:bold;">  Principal's Signature </td> 

         
         </tr>

         <tr>  
            
            <td width="30%" colspan="3"  style="text-align:center;"> 

               <span style="font-size:12px;"><!--  Software Developed By: Aqib Majeed (Software Engineer) 03016138728  --> </span>
            
            </td> 
         
         </tr>


   </table>
  
</div>
  
</page>

<?php
}

 $m->delete("eduportal_exam_temp_marks"); 

?>

</body>
</html>
