<?php

session_start();
include_once "../config/database.php";
include_once "../resources/assessment.php";

// initialize objects
$assessment = new Assessment();
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' )) {
    $oid = $_GET['oid'];
    $aid = $_GET['aid'];
    $id = $_SESSION['uid'];
    $flag = "0";
    $curr = null;
    $mattm = null;

    $assessment->offering_id = $oid;
    $assessment->user_id = $id;

    $stmt = $assessment->get_result();
    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        if ($result == "complete") {
            $flag = "1";
        }
    }

    $attempt_no = $assessment->get_attempt_no();
    if ($attempt_no == null) {
        $curr++;
    } else {
        $curr = $attempt_no + 1;
    }

    $stmt = $assessment->get_offer_max_attempt();
    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        $mattm = $max_attempts;
    }
    if ($curr > $mattm) {
        $flag = "1";
    }
    echo $flag;
} else {
    exit('Restricted Access');
}
?>

