<?php
session_start();
$ex=$_SESSION['eexam'];
$pid=$_SESSION['ppupil'];

include('../include/config.php');

    $date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
    $timenow=$date->format('Y-m-d H:i:s');
    $timenow2=$date->format('Y-m-d');
    $month=$date->format('M');
    
    $feepaid='00';
    $y=$yr;
    if($y=='' || $y==null)
    {$yr=$date->format('Y');}
    else
    {
     $yr=$y;
    }
    
    if($tr=='' || $tr==null)
    {
        $tr='Term One'; 
    }else
    {
    }
    
    if($pid=='' || $pid==null)
    {
      $pid='0';
    }else
    {
    }
    
    $result = mysqli_query($con,"SELECT * from pupils where count='$pid'");
    while($row = $result->fetch_assoc()) 
    {
    $nam=$row["Names"];
    $admno=$row["AdmNo"];
    $claz=$row["Claz"];
    }
    
    $result = mysqli_query($con,"SELECT * from Exam where id='$ex'");
    while($row = $result->fetch_assoc()) 
    {
    $tit=strtoupper($row["Title"]);
    $pe=$row["Period"];
    $year=$row["Year"];
    }
    
    $query = mysqli_query($con,"SELECT Classteachers.Teacher as tea, Staffs.Name as tnam from Classteachers left join Staffs ON Staffs.id=Classteachers.Teacher where Classteachers.Clazzz='$claz'");
    while($row = $query->fetch_assoc()) 
    {
    $teaname=$row["tnam"];
    }
   
     $resultt = mysqli_query($con,"SELECT * from MarksScore where PID='$pid' AND EID='$ex'");
    while($row = $resultt->fetch_assoc()) 
    {
    $La=$row["Lang"];
    $Co=$row["Comp"];
    $Lu=$row["Lugha"];
    $In=$row["Insha"];
    $Ma=$row["Maths"];
    $Sc=$row["Scie"];
    $Ss=$row["SS"];
    $SCr=$row["SSCRE"];
    $Cr=$row["CRE"];
    $To=$row["Total"];
    $Re=$row["Remark"];
    
    $En=$La+$Co;
    $Ki=$Lu+$In;
    }
    
    $result = mysqli_query($con,"SELECT * FROM pupils where Claz='$claz' AND  Status='Active'");
    $num_rows1 = mysqli_num_rows($result);
    {
    $allpupils=htmlentities($num_rows1); 
    }
   
    $sql = "select * from Fees2 where count='$autono' AND pid='$pid'";
    $result = $con->query($sql);
    if ($result->num_rows > 0) 
    {
   
    }else
    {
     $feebala='0';
    }

  //CBC

$RM='';
$RE='';
$RK='';
$RI='';  
$RC='';

$RMP='';
$REP='';
$RKP='';
$RIP='';  
$RCP='';


$ex=$_SESSION['eexam'];
$pid=$_SESSION['ppupil'];

$query=mysqli_query($con,"select CBC1.*,pupils.Names as nm from CBC1 left join pupils ON pupils.count=CBC1.PID where CBC1.EID='$ex' AND CBC1.PID='$pid'");
$cnt=1;
$pos=1;
$lastscore='';
while($row=mysqli_fetch_array($query))
{
$ma=$row["Math"]; 
$eng=$row["Eng"]; 
$kis=$row["Kis"]; 
$in=$row["ILA"]; 
$cre=$row["CRE"]; 
$cra=$row["CrA"]; 
$ea=$row["EA"];
$trubb=$row["TRub"];
$tmks=$row["TMks"];

$ma2=$row["Ruma"]; 
$eng2=$row["Ruen"]; 
$kis2=$row["Ruks"]; 
$cre2=$row["CRE1"]; 
$cra2=$row["CA1"]; 
$ea2=$row["EA1"];

$ma1='';
$eng1='';
$kis1='';
$cre1='';
$cra1='';
$ea1='';


if($ma2=='BE')
{
  $ma1=1;  
}else if($ma2=='ME')
{
    $ma1=3;
}else if($ma2=='AE')
{
    $ma1=2;
}else if($ma2=='EE')
{
    $ma1=4;
}


if($eng2=='BE')
{
  $eng1=1;  
}else if($ma2=='ME')
{
    $eng1=3;
}else if($eng2=='AE')
{
    $eng1=2;
}else if($eng2=='EE')
{
    $eng1=4;
}


if($kis2=='BE')
{
  $kis1=1;  
}else if($kis2=='ME')
{
    $kis1=3;
}else if($kis2=='AE')
{
    $kis1=2;
}else if($kis2=='EE')
{
    $kis1=4;
}



if($cre2=='BE')
{
  $cre1=1;  
}else if($cre2=='ME')
{
    $cre1=3;
}else if($cre2=='AE')
{
    $cre1=2;
}else if($cre2=='EE')
{
    $cre1=4;
}


if($cra2=='BE')
{
  $cra1=1;  
}else if($cra2=='ME')
{
    $cra1=3;
}else if($cra2=='AE')
{
    $cra1=2;
}else if($cra2=='EE')
{
    $cra1=4;
}


if($ea2=='BE')
{
  $ea1=1;  
}else if($ea2=='ME')
{
    $ea1=3;
}else if($ea2=='AE')
{
    $ea1=2;
}else if($ea2=='EE')
{
    $ea1=4;
}



$ca=$row["TMks"];
$trub=$row["TRub"];

$poi='';
if($trub=='BE'){$poi='1';}
else if($trub=='AE'){$poi='2';}
else if($trub=='ME'){$poi='3';}
else if($trub=='EE'){$poi='4';}

if($trub=='BE'){$trub='B.E';}
else if($trub=='AE'){$trub='A.E';}
else if($trub=='ME'){$trub='M.E';}
else if($trub=='EE'){$trub='E.E';}


//Math Grading
if($ma > 24 && $ma<31){$RM='E.E';$RMP='4';}
else if($ma > 14 && $ma<25){$RM='M.E';$RMP='3';}
else if($ma > 9 && $ma<15){$RM='A.E';$RMP='2';}
else if($ma > 0 && $ma<10){$RM='B.E';$RMP='1';}else
{
   $RM='Invalid';$RMP='';
}

//English Grading
if($eng > 35 && $eng<51){$RE='E.E';$REP='4';}
else if($eng > 24 && $eng<36){$RE='M.E';$REP='3';}
else if($eng > 11 && $eng<25){$RE='A.E';$REP='2';}
else if($eng > 0 && $eng<12){$RE='B.E';$REP='1';}else
{
   $RE='Invalid';$REP='';
}

//Kiswahili Grading
if($kis > 35 && $kis<51){$RK='E.E';$RKP='4';}
else if($kis > 24 && $kis<36){$RK='M.E';$RKP='3';}
else if($kis > 11 && $kis<25){$RK='A.E';$RKP='2';}
else if($kis > 0 && $kis<12){$RK='B.E';$RKP='1';}else
{
   $RK='Invalid';$RKP='';
}

//Integrated L Grading
if($in > 39 && $in<51){$RK='E.E';$RIP='4';}
else if($in > 24 && $in<40){$RI='M.E';$RIP='3';}
else if($in > 10 && $in<25){$RI='A.E';$RIP='2';}
else if($in > 0 && $in<10){$RI='B.E';$RIP='1';}else
{
   $RI='Invalid';$RIP='';
}

//Creative Arts
if($ca > 39 && $ca<51){$RC='E.E';$RCP='4';}
else if($ca > 24 && $ca<40){$RC='M.E';$RCP='3';}
else if($ca > 10 && $ca<25){$RC='A.E';$RCP='2';}
else if($ca > 0 && $ca<10){$RC='B.E';$RCP='1';}else
{
$RC='Invalid';$RCP='';
}
$RCPP = intval($RCP);
$RIPP = intval($RIP);
$RMPP = intval($RMP);
$REPP = intval($REP);
$RKPP = intval($RKP);
$TP=$RCPP+$RIPP+$RKPP+$REPP+$RMPP;
$TP=$TP/5;
$ova='';

if($TP==1){$ova='B.E';}
else if($TP==2){$ova='E.E';}
else if($TP==3){$ova='M.E';}
else if($TP==4){$ova='E.E';}
}
  
 require_once 'dompdf/vendor/autoload.php';
 use Dompdf\Dompdf;
$html='
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <title>Exam Result: '.$nam.'</title>
  <meta content="" name="description">
  <meta content="" name="keywords">
  
  <link href="https://fonts.gstatic.com" rel="preconnect">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <!-- Vendor CSS Files -->
 
   <link rel="stylesheet" href="data/css/bootstrap.min.css">
  <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
  <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <link href="assets/vendor/quill/quill.snow.css" rel="stylesheet">
  <link rel="shortcut icon" href="../images/logo.ico">
</head>
<body style="border:1px solid #004d1a;border-radius:5px;padding:5px;border-style: double;outline-offset: -5px;">
    <div>
     <center> <h6 style="margin-top:5px;color:#0099cc" class="text-red" >CATHOLIC DIOCESE OF MURANGA </h6> </center>
   <center> <h3 style="margin-top:-20px" class="text-red" >BISHOP KAIRO EDUCATION CENTRE </h3> </center>
   <center> <h4 style="margin-top:-20px" class="text-red" >EXCELLENCE IS IN GODLINESS </h4> </center>
  
<img style="position:absolute;margin-top:-80px;margin-bottom:10px;max-height:90px" src="https://bishopkairoeducationcentre.sc.ke/QB-Beta/data/logo.png" alt="" class="" />
<br />
   <label style="margin-top:-20px" class="text-red" ><strong>Telephone:</strong> 0728 966 221, 0714 647 708 </label>
   <br />
  <label style="margin-top:-20px" class="text-red" ><strong>Email:</strong> cdmbishopkairo@gmail.com </label>
  
  <label style="position:absolute;margin-left:220px" class="text-red" ><strong>P.O. Box:</strong> 747- 10200 Murang'."'".'a </label>
  <hr />

<div style="">
 NAME: <strong> <u>'.$nam.' </u></strong> <br /> CLASS:   <strong><u>'.$claz.'</u></strong> <br /> ADM NO:  <strong><u>'.$admno.'</u>  <br /> CLASS TEACHER:  <strong><u>'.$teaname.'</u></strong> 
 
       <center><h3><u>REPORT FORM </u></h3></center>
        <u><h4 style="position:absolute;margin-top:-5px"><strong style="color:#00661a;">'.$tit.'</strong> <strong style="margin-left:50px">Year:  <span>'.$year.'</span></strong></h4></u>
    
    <table style="border:1px solid black;position:relative;margin-top:20px">
       <thead>
        <tr style="background-color:#000;color:#FFF">
         <th>SUBJECT </th> <th>PERFORMANCE <br  /> </th> <th>RUBRICS</th> <th>POINTS</th>
        </tr>
       </thead>
        <tbody>
         <tr style="border:1px solid black">
            <td  style="border:1px solid black">MATH </td>
            <td  style="border:1px solid black"><strong>'.$ma.'</strong></td>
            <td  style="border:1px solid black"><strong>'.$ma2.'</strong></td>
            <td  style="border:1px solid black"><strong>'.$ma1.'</strong></td>
         </tr> 
        <tr style="border:1px solid black">
            <td  style="border:1px solid black"> ENGLISH </td>
            <td  style="border:1px solid black"> <strong>'.$eng.' </strong></td>
            <td  style="border:1px solid black"><strong>'.$eng2.'</strong></td>
            <td  style="border:1px solid black"><strong>'.$eng1.'</strong></td>
        </tr>

         <tr>
            <td  style="border:1px solid black"> KISWAHILI </td>
            <td  style="border:1px solid black"> <strong>'.$kis.'  </strong></td>
            <td  style="border:1px solid black"><strong>'.$kis2.'</strong></td>
            <td  style="border:1px solid black"><strong>'.$kis1.'</strong></td>
         </tr>
          <tr>
            <td  style="border:1px solid black"> CRE</td>
            <td  style="border:1px solid black"> <strong>'.$cre.'  </strong></td>
            <td  style="border:1px solid black"><strong>'.$cre2.'</strong></td>
            <td  style="border:1px solid black"><strong>'.$cre1.'</strong></td>
         </tr>
         
          <tr>
            <td  style="border:1px solid black"> CREATIVE ARTS</td>
            <td  style="border:1px solid black"> <strong>'.$cra.'  </strong></td>
            <td  style="border:1px solid black"><strong>'.$cra2.'</strong></td>
            <td  style="border:1px solid black"><strong>'.$cra1.'</strong></td>
         </tr>
         
          <tr>
            <td  style="border:1px solid black"> ENVIRONMENTAL ACTIVITIES</td>
            <td  style="border:1px solid black"> <strong>'.$ea.'  </strong></td>
            <td  style="border:1px solid black"><strong>'.$ea2.'</strong></td>
            <td  style="border:1px solid black"><strong>'.$ea1.'</strong></td>
         </tr>
         <tr>
         <td colspan="4">
            <hr />
        </td>
        </tr>
         <tr>
            <td  style="border:1px solid black" colspan=""> OVERALL PERFORMANCE </td>
            <td  style="border:1px solid black"><strong style="color:green"> '.$tmks.' </strong></td>
            <td  style="border:1px solid black"><strong style="color:green"> '.$trubb.' </strong></td>
            <td  style="border:1px solid black"><strong style="color:green">'.$poi.'</strong></td>
         </tr>
        <tr>
         <td colspan="4"><hr /></td>
        </tr>
        </tbody>
         <hr />
     </table>
</div>

<p>Class Teacher Remarks :</p> 
<p>.............................................................................................................................................................................</p>
<p>.............................................................................................................................................................................</p>
<br />
<p>SIGN .......................................................................                </p>

<h4>Grading System </h4>

<h5><u>Rubrics System</u></h5>
<table style="font-size:11px">
<tr>
 <th>Rubrics</th>
 <th>Points</th>
</tr>
<tr>
 <td>Exceeding expectations (E.E) <td>
 <td><strong>4</strong></td>
</tr>

<tr>
 <td>Meeting Expectations (M.E) <td>
 <td><strong>3</strong></td>
</tr>

<tr>
 <td>Approaching Expectations (A.E) <td>
 <td><strong>2</strong></td>
</tr>

<tr>
 <td>Below Expectations (B.E) <td>
 <td><strong>1</strong></td>
</tr>
</table>

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

  <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="assets/vendor/quill/quill.min.js"></script>
  <script src="assets/vendor/simple-datatables/simple-datatables.js"></script>
  <script src="assets/vendor/tinymce/tinymce.min.js"></script>
  <script src="assets/vendor/php-email-form/validate.js"></script>
  <!-- Template Main JS File -->
  <script src="assets/js/main.js"></script>
  <script src="assets/js/jQuery-2.1.4.min.js"></script>
</body>
</html>'; 
$name=$nam.'.php';
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$options = $dompdf->getOptions(); 
$options->set(array('isRemoteEnabled' => true));
$dompdf->setOptions($options);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($name,['Attachment' => 0]);