<?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']);
$rbib=addslashes($_POST['rcre']);
$cra=addslashes($_POST['ca']);
$rca=addslashes($_POST['rca']);
$sos=addslashes($_POST['ss']);
$rss=addslashes($_POST['rss']);
$tec=addslashes($_POST['st']);
$rtec=addslashes($_POST['rst']);
$agi=addslashes($_POST['agi']);
$ragi=addslashes($_POST['ragi']);

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


$rm=addslashes($_POST['rm']);
$re=addslashes($_POST['re']);
$rk=addslashes($_POST['rk']);
//$craa=addslashes($_POST['craa']);
//$ev=addslashes($_POST['ev']);
//$rcre=addslashes($_POST['rub']);

$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
 {
$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 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 CBC2 SET Eng='$en',Math='$ma',Kis='$kis',MA1='$rm',EN1='$re',KS1='$rk',CRE='$bib',CR1='$rbib',SS='$sos',SS1='$rss',CA='$cra',CA1='$rca',ST='$tec',ST1='$rtec',AN='$agi',AN1='$ragi',TMks='$op',TRub='$op2' 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;
}
}