<?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']);
$cas=addslashes($_POST['cas']);

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

$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 CBC where Grad='$grd' AND EID='$exa'");
$num=mysqli_fetch_array($query);
if($num>0)
{
$sql=mysqli_query($con,"UPDATE CBC SET Eng='$en',Math='$ma',Kis='$kis',ILA='$ila',CA='$cas' 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 CBC(PID,Grad,EID) SELECT count,Claz, EID FROM pupils WHERE Claz='$grd' AND Status='Active'");


$sql=mysqli_query($con,"UPDATE CBC SET Eng='$en',Math='$ma',Kis='$kis',ILA='$ila',CA='$cas' 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
{
$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';
}  
}
}
?>