<?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='';

if(isset($_POST['id']))
{
$tit=addslashes($_POST['title']);
$sd=addslashes($_POST['sd']);
$ed=addslashes($_POST['ed']);
$peri=addslashes($_POST['peri']);
$id=addslashes($_POST['id']);
$pv=addslashes($_POST['pview']);

$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' AND id !='$id'");
$num=mysqli_fetch_array($query);
if($num>0)
{
echo "One exam can not be saved more than once";
}else
{
$sql=mysqli_query($con,"UPDATE Exam SET Title='$tit',Period='$peri',View='$pv',StartDate='$sd',EndDate='$ed',Month='$mm',Year='$yrr' WHERE id='$id'");
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';
}  
}
}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
{
    
$query=mysqli_query($con,"select * from Exam ORDER BY id DESC limit 1");
while($row=mysqli_fetch_array($query))
{ 
$autono=$row["AutoNo"];
}
$auton=$autono+1;    
    
$sql=mysqli_query($con,"INSERT INTO Exam(Title,Period,StartDate,EndDate,Month,Year,AutoNo) values('$tit','$peri','$sd','$ed','$mm','$yrr','$auton')");
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';
} 





}
}
?>