<?php
include('../include/config.php');
error_reporting(0);
session_start();
$iduser=$_SESSION['user_idd'];
$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');
$em='';
$stu='';

if(isset($_POST['id'])) 
{
$id=addslashes($_POST['id']);
$ma=addslashes($_POST['ma']);
$en=addslashes($_POST['en']);
$kis=addslashes($_POST['kis']);
$bib=addslashes($_POST['cre']);
$cra=addslashes($_POST['ca']);
$sos=addslashes($_POST['ss']);
$tec=addslashes($_POST['st']);
$agi=addslashes($_POST['agi']);

$op=addslashes($_POST['op']);


$pid='';
$que=mysqli_query($con,"select * from CBC2 where id='$id'");
while($row=mysqli_fetch_array($que))
{
$grd=addslashes($row['Grad']);
$exa=addslashes($row['EID']);
$pid=addslashes($row['PID']);
}

$query=mysqli_query($con,"select * from examsetting where Grad='$grd' AND examid='$exa'");
$num=mysqli_fetch_array($query);
if($num>0)
{
 //Fech out of marks
$que=mysqli_query($con,"select * from examsetting where Grad='$grd' AND examid='$exa'");
while($row=mysqli_fetch_array($que))
{
$maa=addslashes($row['MA']);
$enn=addslashes($row['EN']);
$kss=addslashes($row['KS']);
$crE=addslashes($row['CRE']);
$ca=addslashes($row['CA']);
$ss=addslashes($row['SS']);
$st=addslashes($row['ST']);
$an=addslashes($row['AN']);
}
  
 if($maa=='' || $enn=='' || $kss=='' || $crE=='' || $ca=='' || $ss=='' || $st=='' || $an=='')
 {
    echo 'You must set the TOTAL marks for each exam in '.$grd;
 }else
 {
    $m1= (int)$ma;
    $m2= (int)$maa;

    $en1=(int)$en;
    $en2=(int)$enn;
    
    $ks1=(int)$kis;
    $ks2=(int)$kss;

    $bib1=(int)$bib;
    $cre2=(int)$crE;

    $sos1=(int)$sos;
    $ss2=(int)$ss;
    
    $cra1=(int)$cra;
    $ca2=(int)$ca;
    
    $tec1=(int)$tec;
    $st2=(int)$st;
    
    $agi1=(int)$agi;
    $an2=(int)$an;
   
 if($m1>$m2 || $en1>$en2 || $ks1>$ks2 || $bib1>$cre2 || $sos1>$ss2 || $cra1>$ca2 || $tec1>$st2 || $agi1>$an2) 
 {
   echo "Marks error !! Ensure Learner's score does not exceed total marks that can be awarded to each subject"; 
 }else
 {
     
  $tot1= $m1+$en1+$ks1+$bib1+$cra1+$agi1+$tec1+$sos1;
  $tot2= $m2+$en2+$ks2+$cre2+$ss2+$ca2+$st2+$an2;
  
 $perma=$m1*100/$m2;
 $perma=number_format($perma,0);
 
 $peren=$en1*100/$en2;
 $peren=number_format($peren,0);
 
 $perks=$ks1*100/$ks2;
 $perks=number_format($perks,0);
 
 $percre=$bib1*100/$cre2;
 $percre=number_format($percre,0);
 
 $perca=$cra1*100/$ca2;
 $perca=number_format($perca,0);
 
 $peran=$agi1*100/$an2;
 $peran=number_format($peran,0);
 
 $perst=$tec1*100/$st2;
 $perst=number_format($perst,0);
 
 $perss=$sos1*100/$ss2;
 $perss=number_format($perss,0);
 
 $pertot=$tot1*100/$tot2;
 $pertot=number_format($pertot,0);   
     
 $query=mysqli_query($con,"select Rubrics2.*,(SELECT Poi FROM Rubrics2 where '$perma' BETWEEN Mini AND Maxi) as mapoi,(SELECT Lev FROM Rubrics2 where '$perma' BETWEEN Mini AND Maxi) as malev,
                                             (SELECT Poi FROM Rubrics2 where '$peren' BETWEEN Mini AND Maxi) as enpoi,(SELECT Lev FROM Rubrics2 where '$peren' BETWEEN Mini AND Maxi) as enlev,
                                             (SELECT Poi FROM Rubrics2 where '$perks' BETWEEN Mini AND Maxi) as kspoi,(SELECT Lev FROM Rubrics2 where '$perks' BETWEEN Mini AND Maxi) as kslev,
                                             (SELECT Poi FROM Rubrics2 where '$percre' BETWEEN Mini AND Maxi) as crepoi,(SELECT Lev FROM Rubrics2 where '$percre' BETWEEN Mini AND Maxi) as crelev,
                                             (SELECT Poi FROM Rubrics2 where '$perca' BETWEEN Mini AND Maxi) as capoi,(SELECT Lev FROM Rubrics2 where '$perca' BETWEEN Mini AND Maxi) as calev,
                                             (SELECT Poi FROM Rubrics2 where '$peran' BETWEEN Mini AND Maxi) as anpoi,(SELECT Lev FROM Rubrics2 where '$peran' BETWEEN Mini AND Maxi) as anlev,
                                             (SELECT Poi FROM Rubrics2 where '$perst' BETWEEN Mini AND Maxi) as stpoi,(SELECT Lev FROM Rubrics2 where '$perst' BETWEEN Mini AND Maxi) as stlev,
                                             (SELECT Poi FROM Rubrics2 where '$perss' BETWEEN Mini AND Maxi) as sspoi,(SELECT Lev FROM Rubrics2 where '$perss' BETWEEN Mini AND Maxi) as sslev,
                                             (SELECT Poi FROM Rubrics2 where '$pertot' BETWEEN Mini AND Maxi) as totpoi,(SELECT Lev FROM Rubrics2 where '$pertot' BETWEEN Mini AND Maxi) as totlev
                                             from Rubrics2");
  while($row=mysqli_fetch_array($query))
{
    $malev=$row['malev']; 
    $mapo=$row['mapoi'];
    $enlev=$row['enlev'];
    $enpoi=$row['enpoi'];
    $kspoi=$row['kspoi'];
    $kslev=$row['kslev'];
    $crepoi=$row['crepoi'];
    $crelev=$row['crelev'];
    $capoi=$row['capoi'];
    $calev=$row['calev'];
    $anpoi=$row['anpoi'];
    $anlev=$row['anlev'];
    $stpoi=$row['stpoi'];
    $stlev=$row['stlev'];
    $sspoi=$row['sspoi'];
    $sslev=$row['sslev'];
    
    $totpoi=$row['totpoi'];
    $totlev=$row['totlev'];
}

$query=mysqli_query($con,"select * FROM pupils where count='$pid'");
while($row=mysqli_fetch_array($query))
{
$stu=addslashes($row['Names']);
}

$query=mysqli_query($con,"select * from CBC2 where Grad='$grd' AND EID='$exa'");
$num=mysqli_fetch_array($query);
if($num>0)
{
$sql=mysqli_query($con,"UPDATE CBC2 SET Eng='$en',Math='$ma',Kis='$kis',AN='$agi',ST='$tec',SS='$sos',CA='$cra',CRE='$bib',
KS1='$kslev',CR1='$crelev',EN1='$enlev',SS1='$sslev',CA1='$calev',ST1='$stlev',AN1='$anlev',MA1='$malev',TMks='$tot1',TRub='$totlev',TTP='$totpoi',
`MP`='$mapo', `EP`='$enpoi', `KP`='$kspoi', `CREP`='$crepoi', `CAP`='$capoi', `SSP`='$sspoi', `STP`='$stpoi', `ANP`='$anpoi' WHERE id='$id'");
if($sql)
{
echo "success";
$msg='updated marks for '.$stu;
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','update','$month')");
}else
{
echo 'Unable to save marks';
}    
}else
{
    
}
}}
}else
{
    echo 'You must set TOTAL exam marks to be awaded (OUT OF) for the selected exam in '.$grd;
}
}