<?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['pid']))
{
$pid=addslashes($_POST['pid']);
$ma=addslashes($_POST['ma']);
$en=addslashes($_POST['en']);
$kis=addslashes($_POST['kis']);
$ila=addslashes($_POST['ila']);
$cre=addslashes($_POST['cre']);
$craa=addslashes($_POST['craa']);
$eva=addslashes($_POST['eva']);

$exa=addslashes($_POST['exa']);
$grd=addslashes($_POST['grd']);

$rm=addslashes($_POST['rm']);
$re=addslashes($_POST['re']);
$rk=addslashes($_POST['rk']);
$rla=addslashes($_POST['rla']);

$cr=addslashes($_POST['cr']);
$cra=addslashes($_POST['cra']);
$ev=addslashes($_POST['ev']);

$mk=addslashes($_POST['mk']);
$rub=addslashes($_POST['rub']);


$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=='' || $cr=='' || $ca=='' || $ea=='')
 {
    echo 'You must set all total marks for every subject in this 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)$craa;
  
  $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 individual 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 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',ILA='$ila',Ruma='$rm',Ruen='$re',Ruks='$rk',Rula='$rla',TRub='$rub',TMks='$mk' WHERE EID='$exa' AND PID='$pid'");

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 "Marks updated";
}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',ILA='$ila',Ruma='$rm',Ruen='$re',Ruks='$rk',Rula='$rla',TRub='$rub',TMks='$mk' 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;
}

}if(isset($_POST['id']))
{
$id=addslashes($_POST['id']);
echo $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']);
$op2=addslashes($_POST['op2']);


//$exa=addslashes($_POST['exa']);
//$grd=addslashes($_POST['grd']);

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

//$mk=addslashes($_POST['mk']);
//$rub=addslashes($_POST['rub']); 


$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=='' || $cr=='' || $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
 {
$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='$mk' WHERE EID='$exa' AND PID='$pid'");
$sql=mysqli_query($con,"UPDATE CBC1 SET Eng='$en',Math='$ma',Kis='$kis',Ruma='$rm' 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',ILA='$ila',Ruma='$rm',Ruen='$re',Ruks='$rk',TRub='$rub',TMks='$mk' 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;
}











  
    
}
else
{
$tit=addslashes($_POST['tit']);
$sd=addslashes($_POST['sd']);
$ed=addslashes($_POST['ed']);
$peri=addslashes($_POST['peri']);


$yrr=substr($sd, 0, 4);
$mm=substr($sd, 5, 2);

if($mm=='01'){$mm='January';}
if($mm=='02'){$mm='February';}
if($mm=='03'){$mm='March';}
if($mm=='04'){$mm='April';}
if($mm=='05'){$mm='May';}
if($mm=='06'){$mm='June';}
if($mm=='07'){$mm='July';}
if($mm=='08'){$mm='August';}
if($mm=='09'){$mm='September';}
if($mm=='10'){$mm='October';}
if($mm=='11'){$mm='November';}
if($mm=='12'){$mm='December';}


$query=mysqli_query($con,"select * from Exam where Title='$tit' AND  StartDate='$sd'");
$num=mysqli_fetch_array($query);
if($num>0)
{
echo "One exam can not be saved more than once";
}else
{
$sql=mysqli_query($con,"INSERT INTO Exam(Title,Period,StartDate,EndDate,Month,Year) values('$tit','$peri','$sd','$ed','$mm','$yrr')");
if($sql)
{
$msg='added new exam';
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','save','$month')");

echo "Successfully saved";
}else
{
echo 'Unable to save';
}  
}
}
?>



