<?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']);
$cre=addslashes($_POST['cre']);
$cra=addslashes($_POST['cra']);
$eva=addslashes($_POST['eva']);
$op=addslashes($_POST['op']);

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

$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']);
$ea=addslashes($row['EA']);
}

 if($maa=='' || $enn=='' || $kss=='' || $crE=='' || $ca=='' || $ea=='')
 {
    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;
  
  $cre2=(int)$crE;
  $cre1=(int)$cre;
  
  $ca2=(int)$ca;
  $ca1=(int)$cra;
  
  $ea2=(int)$ea;
  $ea1=(int)$eva;
  
     
 if($m1>$m2 || $en1>$en2 || $ks1>$ks2  || $cre1>$cre2 || $ca1>$ca2 || $ea1>$ea2) 
 {
   echo "Marks error !! Ensure pupil's score does not exceed total marks that can be awarded to each subject"; 
 }else
 {
  
  $tot1= $m1+$en1+$ks1+$cre1+$ca1+$ea1;
  $tot2= $m2+$en2+$ks2+$cre2+$ca2+$ea2;

 $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=$cre1*100/$cre2;
 $percre=number_format($percre,0);
 
 $perca=$ca1*100/$ca2;
 $perca=number_format($perca,0);
 
 $perea=$ea1*100/$ea2;
 $perea=number_format($perea,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 '$perea' BETWEEN Mini AND Maxi) as eapoi,(SELECT Lev FROM Rubrics2 where '$perea' BETWEEN Mini AND Maxi) as ealev,
                                             (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'];
 $ealev=$row['ealev'];
 $eapoi=$row['eapoi'];
 $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 CBC1 where Grad='$grd' AND EID='$exa'");
$num=mysqli_fetch_array($query);
if($num>0)
{
//$sql=mysqli_query($con,"UPDATE cbc1 SET Eng='$en',Math='$ma',Kis='$kis',Ruma='$rm',Ruen='$re',Ruks='$rk',Rula='$rla',TRub='$rub',TMks='$op' WHERE EID='$exa' AND PID='$pid'");
$sql=mysqli_query($con,"UPDATE CBC1 SET Eng='$en',Math='$ma',Kis='$kis',Ruma='$malev',CRE='$cre',CrA='$cra',EA='$eva',CA1='$calev',Ruen='$enlev',Ruks='$kslev',EA1='$ealev',TRub='$totlev',TMks='$tot1',TTP='$totpoi',CRE1='$crelev',
MP='$mapo',EP='$enpoi',KP='$kspoi',CREP='$crepoi',CAP='$capoi',EAP='$eapoi' WHERE mid='$id'");
if($sql)
{
$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')");

echo "success";
}else
{
echo 'Unable to save marks';
}    
}else
{
$sqll=mysqli_query($con,"UPDATE pupils SET EID='$exa' where Claz='$grd'");

$query3=mysqli_query($con,"INSERT CBC1(PID,Grad,EID) SELECT count,Claz, EID FROM pupils WHERE Claz='$grd' AND Status='Active'");

$sql=mysqli_query($con,"UPDATE CBC1 SET Eng='$en',Math='$ma',Kis='$kis',Ruma='$malev',CRE='$cre',CrA='$cra',EA='$eva',CA1='$calev',Ruen='$enlev',Ruks='$kslev',EA1='$ealev',TRub='$totlev',TMks='$tot1',TTP='$totpoi',CRE1='$crelev',MP='$mapo',EP='$enpoi',KP='$kspoi',CREP='$crepoi',CAP='$capoi',EAP='$eapoi' WHERE EID='$exa' AND PID='$pid'");

if($sql)
{
$msg='added marks for '.$stu;
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','save','$month')");

echo "success";
}else
{
echo 'Unable to save marks';
} 
}


}
 }
}else
{
    echo 'You must set TOTAL exam marks to be awaded (OUT OF) for the selected exam in '.$grd;
}
}