$questionsSet = getTestQuestions($_SESSION['courseId'], $numQuestions);
     if (empty($questionsSet)) {
         redirect('resource/error');
         exit;
     }
     $jsonQuestions = formatQuestionsAsJson($questionsSet);
     if (($testId = createTest($jsonQuestions)) != false) {
         $_SESSION['active_test_id'] = $testId;
         redirect('taketest');
     }
 } elseif (isset($_GET['resume'])) {
     //unset($_SESSION['active_test_id']);
     $testId = numDecrypt($_GET['resume']);
     // redeclare active test id session
     $_SESSION['active_test_id'] = $testId;
     $_SESSION['courseId'] = getTestById($testId)['course_id'];
     //perform queries upon resume
     $userId = $_SESSION['user_id'];
     $startedAt = date('Y-m-d H:i:s');
     $sql = "UPDATE users SET paused_tests = paused_tests-1 WHERE id = '{$userId}'";
     $sql2 = "UPDATE test_info SET  paused = 0, started_at= '{$startedAt}' WHERE test_id = '{$testId}'";
     if (mysql_query($sql) && mysql_query($sql2)) {
         redirect('taketest');
     }
 }
 /*elseif(isset($_GET['resume']) && !isset($_SESSION['active_test_id']))
   {
       $testId = numDecrypt($_GET['resume']);
       $_SESSION['active_test_id'] = $testId;
       redirect('taketest');
   }*/
    $sql = "UPDATE test_info SET json = '{$updatedJson}', time_left = '{$remaining}' WHERE test_id = {$testId}";
    $queryRun = mysql_query($sql);
    $response = [];
    $response['timer'] = $timer;
    $response['started_at'] = $startedAt;
    $response['duration'] = $duration;
    //$response['test_status'] = $status;
    $response['remaining'] = $remaining;
    $response['time_left'] = $timeLeft;
    echo json_encode($response);
}
if (isset($_POST['pause'])) {
    $userId = $_SESSION['user_id'];
    $testId = $_SESSION['active_test_id'];
    $courseId = $_SESSION['courseId'];
    $test = getTestById($testId);
    $duration = $test['duration'];
    $startedAt = $test['started_at'];
    list($remaining, $timer) = countDown($startedAt, $duration);
    $newDuration = $remaining / 60;
    //converts time from seconds to minnutes
    $query = select("SELECT * FROM test_info WHERE test_id ='{$testId}'")[0];
    if ($query['paused'] == 0) {
        mysql_query("UPDATE users SET paused_tests = paused_tests + 1 WHERE id = '{$userId}'");
        $sql = "UPDATE test_info SET duration = '{$newDuration}', course_id ='{$courseId}', paused = 1 WHERE test_id = '{$testId}'";
        mysql_query($sql);
    } else {
        /*$sql = "UPDATE test_info SET duration = '$newDuration', status = 1  WHERE test_id = '{$testId}'";
          mysql_query($sql);*/
    }
}
function updateTestQuestion($testId)
{
    $test = getTestById($testId);
    $testQuestions = json_decode($test['json']);
    $updatedTestQuestions = [];
    $skipFirst = true;
    foreach ($testQuestions as $key => $value) {
        $updatedTestQuestions[$key] = null;
    }
    $updatedJson = json_encode($updatedTestQuestions, true);
    $sql = "UPDATE test_info SET json = '{$updatedJson}' WHERE test_id = {$testId}";
    if (mysql_query($sql)) {
        return true;
    }
    return false;
}
require_once '../core/init.php';
auth();
//protects this page against unauthenticated users
/* $d = select("SELECT * FROM questions WHERE category_id=15");
   print_array($d);die();*/
if (!isset($_SESSION['start'])) {
    redirect('resource/error2');
    exit;
}
// initialise variables for the page
// $courseId    = $_SESSION['courseId'];
//$deptId      = $_SESSION['deptId'];
$testId = $_SESSION['active_test_id'];
$timeLeft = getTestById($testId)['time_left'];
$startedAt = getTestById($testId)['started_at'];
$testDuration = getTestById($testId)['duration'];
//list($optionId, $testData) =   getQuestionFromJson($testId);
$numQ = $numQuestions;
$index = 0;
$counter = 1;
if (isset($_GET['question']) && ($_GET['question'] > 0 && $_GET['question'] < $numQ)) {
    $index = (int) $_GET['question'];
    $counter += $index;
    $questionId = getJsonQuestionId($index);
} else {
    $questionId = getJsonQuestionId($index);
}
list($optionId, $testData) = getQuestion($testId, $questionId);
//$department = getDepartment($deptId)[0];
//$course     = getCourse($courseId)[0];
?>