<?php

if (isset($_SESSION['exam_allowed']) && $_SESSION['exam_allowed'] == "YES") {

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$stmt = $conn->prepare("SELECT * FROM tbl_school_info");
$stmt->execute();
$result = $stmt->fetchAll();

foreach($result as $row) {

$currency = $row[6];
date_default_timezone_set($row[5]);

}

}catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}


$examtime = $_SESSION['exam_time'];
$examtitle = $_SESSION['exam_title'];
$exam_id = $_SESSION['current_examid'];
$student_retake = $_SESSION['student_retake'];

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$stmt = $conn->prepare("SELECT * FROM tbl_asessment_records WHERE student = :me AND exam = :exam");
$stmt->bindParam(':me', $_SESSION['id']);
$stmt->bindParam(':exam', $exam_id);
$stmt->execute();
$result = $stmt->fetchAll();
$rec = count($result);

if ($rec > 0) {

foreach ($result as $row) {
$questions = explode(",",$row[6]);
$answers = unserialize($row[7]);
$marking_scheme = explode(",",$row[8]);
$current_time = date('Y-m-d G:i:s');
$end_time = $row[4];
$submitted = $row[9];
}


}else{
$minutes_to_add = $_SESSION['exam_time'];
$current_time = date('Y-m-d H:i:s');
$time = new DateTime($current_time);
$time->add(new DateInterval('PT' . $minutes_to_add . 'M'));
$end_time = $time->format('Y-m-d G:i:s');
$questions = array();
$answers = array();
$marking_scheme = array();

$lim = $_SESSION['questions'];
$stmt = $conn->prepare("SELECT id,exam FROM tbl_questions WHERE exam = ? ORDER BY rand() LIMIT $lim");
$stmt->execute([$_SESSION['current_examid']]);
$result = $stmt->fetchAll();
foreach($result as $row) {
array_push($questions,$row[0]);
}
shuffle($questions);

foreach($questions as $question) {

$answers[$question] = "";
$stmt = $conn->prepare("SELECT id,answer,exam FROM tbl_questions WHERE exam = ? AND id = ?");
$stmt->execute([$_SESSION['current_examid'], $question]);
$result = $stmt->fetchAll();
foreach($result as $row) {
array_push($marking_scheme,$row[1]);
}

}

$questions = implode(",",$questions);
$answers = serialize($answers);
$marking_scheme = implode(",",$marking_scheme);

$stmt = $conn->prepare("INSERT INTO tbl_asessment_records (student, exam, start_time, end_time, questions, answers, marking_scheme) VALUES (?,?,?,?,?,?,?)");
$stmt->execute([$_SESSION['id'], $_SESSION['current_examid'], $current_time, $end_time, $questions, $answers, $marking_scheme]);

$questions = explode(",",$questions);
$answers = unserialize($answers);
$marking_scheme = explode(",",$marking_scheme);
$submitted = 0;

}

if ($submitted > 0) {
$ex_id = $exam_id;
if ($student_retake > 0) {
$stmt = $conn->prepare("DELETE FROM tbl_asessment_records WHERE student = :std AND exam = :exam");
$stmt->bindParam(':std', $_SESSION['id']);
$stmt->bindParam(':exam', $ex_id);
$stmt->execute();
header("location:assessment");
}else{
header("location:view-exam?node=$ex_id");
}

}else{
$myDateTime = DateTime::createFromFormat('Y-m-d G:i:s', $end_time);
$newDateString = $myDateTime->format('M d, Y H:i:s');

$to_time = strtotime($end_time);
$from_time = strtotime($current_time);
$min_left =  round(($to_time - $from_time) / 60);

}

if ($min_left < 1) {
$ex_id = $_SESSION['id'];
header("location:view-exam?node=$ex_id");
}
}catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}


}else{

header("location:./");


}

?>
