 <?php  
  ob_start();
 session_start();
include ('../includes/connection.php');  
$class_id=$_SESSION['class_id'];
$marks_id=$_GET['id'];
$query= mysqli_query($conn,"SELECT exam_name  FROM marks_srms WHERE class_id = '$class_id' AND marks_id='$marks_id'")or die(mysqli_error());
 $exam_1 = mysqli_fetch_array($query); 
$exam=$exam_1['exam_name'];

 $count_st= mysqli_query($conn,"SELECT COUNT(marks_id) AS Total_st FROM marks_srms WHERE class_id = '$class_id' AND exam_name='$exam' ")or die(mysqli_error());
$count = mysqli_fetch_array($count_st); 

$query= mysqli_query($conn,"SELECT *, sum(sub_1+sub_2+sub_3+sub_4+sub_5+sub_6+sub_7) as total,student_srms.student_roll_no,student_srms.student_name,s.average,s.marks_id, (SELECT COUNT(sr.marks_id) FROM marks_srms AS sr WHERE sr.average > s.average AND sr.class_id = '$class_id' AND sr.exam_name='$exam') + 1 AS rank FROM student_srms,class_srms,marks_srms AS s WHERE s.class_id = '$class_id' AND student_srms.student_roll_no = s.student_roll_no and student_srms.class_id=class_srms.class_id AND s.exam_name='$exam' and s.marks_id='$marks_id'")or die(mysqli_error());
 $q = mysqli_fetch_array($query); 

// $unity_total="14";
//  $gpa= mysqli_query($conn,"SELECT student_srms.student_name, ROUND(sum(marks_srms.grade_un * 14)/ 14,1) AS GPA FROM student_srms,marks_srms WHERE marks_srms.class_id = '1' AND student_srms.student_id=marks_srms.student_id AND marks_srms.exam_name='$exam' and marks_srms.marks_id='$marks_id'")or die(mysqli_error());
//  $s_gpa = mysqli_fetch_array($gpa); 

 $html .= '<center> <img src="../../assets/img/banner3.png" width="500"></center>'; 
 $html .= '<hr>'; 
 $html .= ' <style>
    table, tr,th, td {
    border: 1px solid black;
    border-collapse: collapse;
    padding: 6px; 
} 
 
</style>'; 
 $html .= '
  
<center><p><b>Roll No. - </b>' . $q['student_roll_no'].'...<b>Class Name - </b>' . $q['class_name'] .'...<b>Parent Mobile- </b>' . $q['student_email_id'] .'</p><p><b>Gender - </b>' . $q['student_gender'] .'....<b> YEAR - </b>' . $q['Year'] .'</p> </center>';

$html .= ' <table>
          <tr>
            <th rowspan="3" class=""></th>
            <th >EXAMINATION</th>
            <th colspan="4">TERMINAL SUMMARY</th> 
          </tr> 
          <tr> 
            <td >FIRST TERMINAL</td>
            <td rowspan="2">Terminal Exam</td>
            <td rowspan="3">Total</td>
            <td rowspan="3">Grade</td>
            <td rowspan="3">Rank</td> 
          </tr> 
          <tr> 
            <td>' . $q['exam_name'] .'</td> 
          </tr> 

          <tr> 
            <td><b>Out Of</b></td>
            <td>100%</td>  
            <td><center>100%</center></td> 
          </tr> ';     
          $query1= mysqli_query($conn,"SELECT student_srms.student_roll_no,student_srms.student_name,s.sub_1,s.marks_id, (SELECT COUNT(sr.marks_id) FROM marks_srms AS sr WHERE sr.sub_1 > s.sub_1 AND sr.class_id = '$class_id' AND sr.exam_name='$exam') + 1 AS rank FROM student_srms,class_srms,marks_srms AS s WHERE s.class_id = '$class_id' AND student_srms.student_roll_no = s.student_roll_no AND s.exam_name='$exam' and student_srms.class_id=class_srms.class_id and s.marks_id='$marks_id'")or die(mysqli_error());
          $sub_1 = mysqli_fetch_array($query1);
          $average_1 =$sub_1['sub_1'];
          if ($average_1>= 91) {
                $grade_1="A"; 
              }elseif ($average_1>= 81) {
                $grade_1="B"; 
              }elseif ($average_1>= 71) {
                $grade_1="C"; 
              }elseif ($average_1>= 61) {
                $grade_1="D"; 
              }elseif ($average_1>= 51) {
                $grade_1="E"; 
              }else{
                $grade_1="F"; 
              } 

              if ($grade_1== "A") {
                $grade_unity_1= 5; 
              }elseif ($grade_1== "B") {
                $grade_unity_1= 4; 
              }elseif ($grade_1== "C") {
                $grade_unity_1= 3; 
              }elseif ($grade_1== "D") {
                $grade_unity_1= 2; 
              }elseif ($grade_1== "E") {
                $grade_unity_1= 1; 
              }else{
                $grade_unity_1="FAIL"; 
              } 
$html .= ' <tr> 
              <td>Mathematics</td>
              <td>'. $q['sub_1'].' </td>
              <td><center>--</center></td>
              <td><center>'. $sub_1['sub_1'] .'</center></td>
              <td><center>'. $grade_1 .'</center></td>
              <td><center>'. $sub_1['rank'] .'</center></td>  
            </tr>';     
          $query1= mysqli_query($conn,"SELECT student_srms.student_roll_no,student_srms.student_name,s.sub_2,s.marks_id, (SELECT COUNT(sr.marks_id) FROM marks_srms AS sr WHERE sr.sub_2 > s.sub_2 AND sr.class_id = '$class_id' AND sr.exam_name='$exam') + 1 AS rank FROM student_srms,class_srms,marks_srms AS s WHERE s.class_id = '$class_id' AND student_srms.student_roll_no = s.student_roll_no AND s.exam_name='$exam' and student_srms.class_id=class_srms.class_id and s.marks_id='$marks_id'")or die(mysqli_error());
          $sub_2 = mysqli_fetch_array($query1);
          $average_2 =$sub_2['sub_2'];
          if ($average_2>= 91) {
                $grade_2="A"; 
              }elseif ($average_2>= 81) {
                $grade_2="B"; 
              }elseif ($average_2>= 71) {
                $grade_2="C"; 
              }elseif ($average_2>= 61) {
                $grade_2="D"; 
              }elseif ($average_2>= 51) {
                $grade_2="E"; 
              }else{
                $grade_2="F"; 
              } 

              if ($grade_2== "A") {
                $grade_unity_2= 5; 
              }elseif ($grade_2== "B") {
                $grade_unity_2= 4; 
              }elseif ($grade_2== "C") {
                $grade_unity_2= 3; 
              }elseif ($grade_2== "D") {
                $grade_unity_2= 2; 
              }elseif ($grade_2== "E") {
                $grade_unity_2= 1; 
              }else{
                $grade_unity_2="FAIL"; 
              } 
$html .= ' <tr> 
              <td>English</td>
              <td>'. $q['sub_2'] .'</td>
              <td><center>--</center></td>
             <td><center>'. $sub_2['sub_2'] .'</center></td>
              <td><center>'. $grade_2 .'</center></td>
              <td><center>'. $sub_2['rank'] .'</center></td>    
            </tr> ';

          $query1= mysqli_query($conn,"SELECT student_srms.student_roll_no,student_srms.student_name,s.sub_3,s.marks_id, (SELECT COUNT(sr.marks_id) FROM marks_srms AS sr WHERE sr.sub_3 > s.sub_3 AND sr.class_id = '$class_id' AND sr.exam_name='$exam') + 1 AS rank FROM student_srms,class_srms,marks_srms AS s WHERE s.class_id = '$class_id' AND student_srms.student_roll_no = s.student_roll_no AND s.exam_name='$exam' and student_srms.class_id=class_srms.class_id and s.marks_id='$marks_id'")or die(mysqli_error());
          $sub_3 = mysqli_fetch_array($query1);
          $average_3 =$sub_3['sub_3'];
          if ($average_3>= 91) {
                $grade_3="A"; 
              }elseif ($average_3>= 81) {
                $grade_3="B"; 
              }elseif ($average_3>= 71) {
                $grade_3="C"; 
              }elseif ($average_3>= 61) {
                $grade_3="D"; 
              }elseif ($average_3>= 51) {
                $grade_3="E"; 
              }else{
                $grade_3="F"; 
              } 

              if ($grade_3== "A") {
                $grade_unity_3= 5; 
              }elseif ($grade_3== "B") {
                $grade_unity_3= 4; 
              }elseif ($grade_3== "C") {
                $grade_unity_3= 3; 
              }elseif ($grade_3== "D") {
                $grade_unity_3= 2; 
              }elseif ($grade_3== "E") {
                $grade_unity_3= 1; 
              }else{
                $grade_unity_3="FAIL"; 
              } 
$html .= ' <tr> 
              <td>Kiswahili</td>
              <td>'. $q['sub_3'] .'</td>
              <td><center>--</center></td>
              <td><center>'. $sub_3['sub_3'] .'</center></td>
              <td><center>'. $grade_3.'</center></td>
              <td><center>'. $sub_3['rank'] .'</center></td>   
            </tr>';     
          $query1= mysqli_query($conn,"SELECT student_srms.student_roll_no,student_srms.student_name,s.sub_4,s.marks_id, (SELECT COUNT(sr.marks_id) FROM marks_srms AS sr WHERE sr.sub_4 > s.sub_4 AND sr.class_id = '$class_id' AND sr.exam_name='$exam') + 1 AS rank FROM student_srms,class_srms,marks_srms AS s WHERE s.class_id = '$class_id' AND student_srms.student_roll_no = s.student_roll_no AND s.exam_name='$exam' and student_srms.class_id=class_srms.class_id and s.marks_id='$marks_id'")or die(mysqli_error());
          $sub_4 = mysqli_fetch_array($query1);
          $average_4 =$sub_4['sub_4'];
          if ($average_4>= 91) {
                $grade_4="A"; 
              }elseif ($average_4>= 81) {
                $grade_4="B"; 
              }elseif ($average_4>= 71) {
                $grade_4="C"; 
              }elseif ($average_4>= 61) {
                $grade_4="D"; 
              }elseif ($average_4>= 51) {
                $grade_4="E"; 
              }else{
                $grade_4="F"; 
              } 

              if ($grade_4== "A") {
                $grade_unity_4= 5; 
              }elseif ($grade_4== "B") {
                $grade_unity_4= 4; 
              }elseif ($grade_4== "C") {
                $grade_unity_4= 3; 
              }elseif ($grade_4== "D") {
                $grade_unity_4= 2; 
              }elseif ($grade_4== "E") {
                $grade_unity_4= 1; 
              }else{
                $grade_unity_4="FAIL"; 
              } 
$html .= '<tr> 
              <td>Science & Technology</td>
              <td>'. $q['sub_4'] .'</td>
              <td><center>--</center></td>
              <td><center>'. $sub_4['sub_4'] .'</center></td>
              <td><center>'. $grade_4 .'</center></td>
              <td><center>'. $sub_4['rank'] .'</center></td>   
            </tr>';      
          $query1= mysqli_query($conn,"SELECT student_srms.student_roll_no,student_srms.student_name,s.sub_5,s.marks_id, (SELECT COUNT(sr.marks_id) FROM marks_srms AS sr WHERE sr.sub_5 > s.sub_5 AND sr.class_id = '$class_id' AND sr.exam_name='$exam') + 1 AS rank FROM student_srms,class_srms,marks_srms AS s WHERE s.class_id = '$class_id' AND student_srms.student_roll_no = s.student_roll_no AND s.exam_name='$exam' and student_srms.class_id=class_srms.class_id and s.marks_id='$marks_id'")or die(mysqli_error());
          $sub_5 = mysqli_fetch_array($query1);
          $average_5 =$sub_5['sub_5'];
          if ($average_5>= 91) {
                $grade_5="A"; 
              }elseif ($average_5>= 81) {
                $grade_5="B"; 
              }elseif ($average_5>= 71) {
                $grade_5="C"; 
              }elseif ($average_5>= 61) {
                $grade_5="D"; 
              }elseif ($average_5>= 51) {
                $grade_5="E"; 
              }else{
                $grade_5="F"; 
              } 

              if ($grade_5== "A") {
                $grade_unity_5= 5; 
              }elseif ($grade_5== "B") {
                $grade_unity_5= 4; 
              }elseif ($grade_5== "C") {
                $grade_unity_5= 3; 
              }elseif ($grade_5== "D") {
                $grade_unity_5= 2; 
              }elseif ($grade_5== "E") {
                $grade_unity_5= 1; 
              }else{
                $grade_unity_5="FAIL"; 
              } 
$html .= '<tr> 
              <td>Civic & Moral education</td>
              <td>'. $q['sub_5'] .'</td>
              <td><center>--</center></td>
              <td><center>'. $sub_5['sub_5'] .'</center></td>
              <td><center>'. $grade_5 .'</center></td>
              <td><center>'. $sub_5['rank'] .'</center></td>   
            </tr> ';    
          $query1= mysqli_query($conn,"SELECT student_srms.student_roll_no,student_srms.student_name,s.sub_6,s.marks_id, (SELECT COUNT(sr.marks_id) FROM marks_srms AS sr WHERE sr.sub_6 > s.sub_6 AND sr.class_id = '$class_id' AND sr.exam_name='$exam') + 1 AS rank FROM student_srms,class_srms,marks_srms AS s WHERE s.class_id = '$class_id' AND student_srms.student_roll_no = s.student_roll_no AND s.exam_name='$exam' and student_srms.class_id=class_srms.class_id and s.marks_id='$marks_id'")or die(mysqli_error());
          $sub_6 = mysqli_fetch_array($query1);
          $average_6 =$sub_6['sub_6'];
          if ($average_6>= 91) {
                $grade_6="A"; 
              }elseif ($average_6>= 81) {
                $grade_6="B"; 
              }elseif ($average_6>= 71) {
                $grade_6="C"; 
              }elseif ($average_6>= 61) {
                $grade_6="D"; 
              }elseif ($average_6>= 51) {
                $grade_6="E"; 
              }else{
                $grade_6="F"; 
              } 

              if ($grade_6== "A") {
                $grade_unity_6= 5; 
              }elseif ($grade_6== "B") {
                $grade_unity_6= 4; 
              }elseif ($grade_6== "C") {
                $grade_unity_6= 3; 
              }elseif ($grade_6== "D") {
                $grade_unity_6= 2; 
              }elseif ($grade_6== "E") {
                $grade_unity_6= 1; 
              }else{
                $grade_unity_6="FAIL"; 
              } 
$html .= '<tr> 
              <td>Social Study</td>
              <td>'. $q['sub_6'] .'</td>
              <td><center>--</center></td>
              <td><center>'. $sub_6['sub_6'] .'</center></td>
              <td><center>'. $grade_6 .'</center></td>
              <td><center>'. $sub_6['rank'] .'</center></td>  
            </tr>';     
          $query1= mysqli_query($conn,"SELECT student_srms.student_roll_no,student_srms.student_name,s.sub_7,s.marks_id, (SELECT COUNT(sr.marks_id) FROM marks_srms AS sr WHERE sr.sub_7 > s.sub_7 AND sr.class_id = '$class_id' AND sr.exam_name='$exam') + 1 AS rank FROM student_srms,class_srms,marks_srms AS s WHERE s.class_id = '$class_id' AND student_srms.student_roll_no = s.student_roll_no AND s.exam_name='$exam' and student_srms.class_id=class_srms.class_id and s.marks_id='$marks_id'")or die(mysqli_error());
          $sub_7 = mysqli_fetch_array($query1);
          $average_7 =$sub_7['sub_7'];
          if ($average_7>= 91) {
                $grade_7="A"; 
              }elseif ($average_7>= 81) {
                $grade_7="B"; 
              }elseif ($average_7>= 71) {
                $grade_7="C"; 
              }elseif ($average_7>= 61) {
                $grade_7="D"; 
              }elseif ($average_7>= 51) {
                $grade_7="E"; 
              }else{
                $grade_7="F"; 
              } 

             if ($grade_7== "A") {
                $grade_unity_7= 5; 
              }elseif ($grade_7== "B") {
                $grade_unity_7= 4; 
              }elseif ($grade_7== "C") {
                $grade_unity_7= 3; 
              }elseif ($grade_7== "D") {
                $grade_unity_7= 2; 
              }elseif ($grade_7== "E") {
                $grade_unity_7= 1; 
              }else{
                $grade_unity_7="FAIL"; 
              } 

              $GPA = round((($grade_unity_1*2) +($grade_unity_2*2)+($grade_unity_3*2)+($grade_unity_4*2)+($grade_unity_5*2) + ($grade_unity_6*2) + ($grade_unity_7*2))/14,1);
$html .= '<tr> 
              <td>Vocation Skills</td>
              <td>'. $q['sub_7'] .'</td>
              <td><center>--</center></td>
              <td><center>'. $sub_7['sub_7'] .'</center></td>
              <td><center>'. $grade_7 .'</center></td>
              <td><center>'. $sub_7['rank'] .'</center></td>   
            </tr>  
          <tr> 
            <td>Total</td>
            <td>'. $q['total'] .'</td> 
            <td colspan="4" rowspan="4"><br><p>Total :<i style="color: red;">---In progress---</i></p><p> Average :<i style="color: red;">---In progress---</i></p><p> Grade :<i style="color: red;">---In progress---</i></p> <p>Rank:<i style="color: red;">---In progress---</i>out of<i style="color: red;">---In progress---</i>pupil</p>
            </td> 
          </tr>
          <tr> 
            <td>Average</td>
            <td>'. $q['average'] .'</td> 
          </tr>
          <tr> 
            <td>GPA</td>
            <td>'. $GPA .'</td>  
          </tr> 
          <tr> 
            <td>Rank</td>
            <td>'. $q['rank'] .' Out Of '. $count['Total_st'] .' Pupil</td> 
          </tr> 
          <tr> 
            <td colspan="6">Class Teacher Remarks <br><br><br></td> 
          </tr>
          <tr> 
            <td colspan="6">Signed <br> Class Teacher_______________Head of school____________Parent_______________ 
            </td> 
    </tr>  
</table> '; 
$html .= '<p><b >ACADEMIC COMMENT:</b> _______________________________________________________________________________________';

require_once '../../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf(); 
$dompdf->loadHtml($html); 
$dompdf->setPaper('A4', 'portrait'); 
$dompdf->render();
$dompdf->stream(' '. $q['student_name'] . '.pdf');
?>