<?php

session_start();
include_once "../config/database.php";
include_once "../security/session_config.php";
// include objects
include_once "../resources/assessment.php";
$conn = DBConnection();
$assessment = new Assessment();
$conn->beginTransaction();
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' )) {
    if (isset($_SESSION['uid'])) {
        $id = $_SESSION['uid'];
    }
    $oid = $_POST['oid'];
    $a_id = $_POST['assmt_id'];
    $attm = $_POST['attm'];
    $allanswers = $_POST['allanswers'];
    $times = $_POST['times'];
	$screen = $_POST['screen'];
//    $ass_flag=$_POST['ass_flag'];
    $sub_assmt_null = 0;
    //print_r($allanswers);
    //print_r($times);
    $marks = null;
    $total_marks = null;
    $m = 0;
    $assessment->offering_id = $oid;
    $assessment->user_id = $id;

	if($screen != ''){
	if ($assessment->insert_screen_count($oid,$id,$a_id,$attm,$screen,$date_time)) {
                    
    }
}

    foreach ($allanswers as $answer) {
        $m = $m + 1;
        $arr = explode('_', $answer, 2);
        $qid = $arr[0];
        if (isset($arr[1])) {
            $opt = $arr[1];
        } else {
            $opt = "";
        }

        $arr1 = explode('_', $opt);
	$arr1 = str_replace("%underscore","_",$arr1);

        if ($arr1[0] == "")
            array_shift($arr1);
        $arr2 = $arr1;

        $assessment->assessment_id = $a_id;
        $assessment->question_id = $qid;
        $stmt = $assessment->get_options_calc();
        $num = $stmt->rowCount();
        if($num >0 ){
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);
            if ($question_type == "MC" || $question_type == "MR") {
                $response_id = '';
                if (in_array($option_id, $arr1)) {
                    $response_id = "Y";
                }

                $assessment->offering_id = $oid;
                $assessment->user_id = $id;
                $assessment->attempt_no = $attm;
                $assessment->sequence_id = $m;
                $assessment->question_id = $qid;
                $assessment->option_id = $option_id;
                $assessment->user_response = $response_id;
                $assessment->last_updated_ts = $date_time;
                if ($assessment->insert_quiz_response()) {
                    
                } else {
                    $conn->rollBack();
                    echo 'error';
                    return -1;
                }
            } else if ($question_type == "FIB-E" || $question_type == "FIB-A") {
                if (!empty($arr2)) {
                    $assessment->offering_id = $oid;
                    $assessment->user_id = $id;
                    $assessment->attempt_no = $attm;
                    $assessment->sequence_id = $m;
                    $assessment->question_id = $qid;
                    $assessment->option_id = $option_id;
                    $assessment->fib_answer = $arr2[0];
                    $assessment->user_response = '';
                    $assessment->last_updated_ts = $date_time;
                    if ($assessment->insert_fib_quiz_response()) {
                        
                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }
                    array_shift($arr2);
                } else {
                    $assessment->offering_id = $oid;
                    $assessment->user_id = $id;
                    $assessment->attempt_no = $attm;
                    $assessment->sequence_id = $m;
                    $assessment->question_id = $qid;
                    $assessment->option_id = $option_id;
                    $assessment->fib_answer = '';
                    $assessment->user_response = '';
                    $assessment->last_updated_ts = $date_time;
                    if ($assessment->insert_fib_quiz_response()) {
                        
                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }
                }
            } else if ($question_type == "MF") {
                if (!empty($arr2)) {
                    $assessment->offering_id = $oid;
                    $assessment->user_id = $id;
                    $assessment->attempt_no = $attm;
                    $assessment->sequence_id = $m;
                    $assessment->question_id = $qid;
                    $assessment->option_id = $option_id;
                    $assessment->user_response = $arr2[0];
                    $assessment->last_updated_ts = $date_time;
                    if ($assessment->insert_quiz_response()) {
                        
                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }
                    array_shift($arr2);
                } else {
                    $assessment->offering_id = $oid;
                    $assessment->user_id = $id;
                    $assessment->attempt_no = $attm;
                    $assessment->sequence_id = $m;
                    $assessment->question_id = $qid;
                    $assessment->option_id = $option_id;
                    $assessment->user_response = '';
                    $assessment->last_updated_ts = $date_time;
                    if ($assessment->insert_quiz_response()) {
                        
                    } else {
                        echo 'error';
                        return -1;
                    }
                }
            }
        }//num row check ends
        }else{
            //need to check which type of question insert batch for AI type not AD
            
                if (!empty($arr2)) {
                    $sub_assmt_null=1;
                    $assessment->offering_id = $oid;
                    $assessment->user_id = $id;
                    $assessment->attempt_no = $attm;
                    $assessment->sequence_id = $m;
                    $assessment->question_id = $qid;
                    $assessment->assessment_id = $a_id;
                    $assessment->option_id = 0;
                    $assessment->subjective_answer = $arr2[0];
                    $assessment->user_response = '';
                    $assessment->last_updated_ts = $date_time;
                    if ($assessment->insert_sub_quiz_response()) {
                        
                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }
                    //insert ai_batch db
                    $sub_type=$assessment->get_sub_ques_type();                  
                    if($sub_type == "S-AI"){
                        if($assessment->insert_ai_batch()){

                        }
                    }
                    //update course_assignment status to awaited
                    if($assessment->update_course_result()){
                        
                    }                    
                    array_shift($arr2);
                } else {       
                    $assessment->offering_id = $oid;
                    $assessment->user_id = $id;
                    $assessment->attempt_no = $attm;
                    $assessment->sequence_id = $m;
                    $assessment->question_id = $qid;
                    $assessment->assessment_id = $a_id;
                    $assessment->option_id = 0;
                    $assessment->subjective_answer = '';
                    $assessment->user_response = '';
                    $assessment->last_updated_ts = $date_time;
                    if ($assessment->insert_sub_quiz_response()) {
                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }
                }
        }
        $op_final = null;
        foreach ($arr1 as $arr1t) {
            $op_final = $op_final . '' . $arr1t;
        }
        $stmt1 = $assessment->get_marks_question();
        if ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
            extract($row1);
            $total_marks = $total_marks + ($question_marks);
            if ($question_type_id == "1") {
                $op_final_sr = null;
                $stmt2 = $assessment->get_option_id();
                while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
                    extract($row2);
                    $op_final_sr = $op_final_sr . '' . $option_id;
                }
                if ($op_final == $op_final_sr) {
                    //insert values to quiz_response_mark with actual marks
                     if($assessment->insert_quiz_marks($question_marks)){
                        
                    }
                    $marks = $marks + ($question_marks);
                }else{
                     if($assessment->insert_quiz_marks(0)){
                        
                    }
                }
            } else if ($question_type_id == "2") {
                $op_final_sr = null;
                $stmt2 = $assessment->get_option_id();
                while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
                    extract($row2);
                    $op_final_sr = $op_final_sr . '' . $option_id;
                }
                if ($op_final == $op_final_sr) {
                    //insert values to quiz_response_mark with actual marks
                     if($assessment->insert_quiz_marks($question_marks)){
                        
                    }
                    $marks = $marks + ($question_marks);
                }else{
                     if($assessment->insert_quiz_marks(0)){
                        
                    }
                }
            } else if ($question_type_id == "3") {
                $op_final_sr = null;
                $stmt2 = $assessment->get_answer_option_MF();
                while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
                    extract($row2);
                    $op_final_sr = $op_final_sr . '' . $answer_option;
                }
                if ($op_final == $op_final_sr) {
                    //insert values to quiz_response_mark with actual marks
                    if($assessment->insert_quiz_marks($question_marks)){
                        
                    }
                    $marks = $marks + ($question_marks);
                }else{
                     if($assessment->insert_quiz_marks(0)){
                        
                    }
                }
            } else if ($question_type_id == "4") {
                $assessment->question_id = $qid;
                $assessment->attempt_no = $attm;
                $assessment->offering_id = $oid;
                $assessment->user_id = $id;
                $assessment->assessment_id = $a_id;
                $stmt_fib_e = $assessment->get_fib_e_options_calc();
                if ($stmt_fib_e == "0" || $stmt_fib_e == "") {
                    if($assessment->insert_quiz_marks(0)){
                        
                    }
                }else{
                    //insert values to quiz_response_mark with actual marks
                    if($assessment->insert_quiz_marks($question_marks)){
                        
                    }
                    $marks = $marks + ($question_marks);
                }
            } else if ($question_type_id == "5") {
                $assessment->question_id = $qid;
                $assessment->attempt_no = $attm;
                $assessment->offering_id = $oid;
                $assessment->user_id = $id;
                $assessment->assessment_id = $a_id;
                $assessment->last_updated_ts = $date_time;
                $stmt_fib_a = $assessment->get_fib_a_options_calc();
                if ($stmt_fib_a == "0" || $stmt_fib_a == "") {
                    if($assessment->insert_quiz_marks(0)){
                        
                    }
                }else{
                    //insert values to quiz_response_mark with actual marks
                    if($assessment->insert_quiz_marks($question_marks)){
                        
                    }
                    $marks = $marks + ($question_marks);
                }
            } else if ($question_type_id == "6") {
                $assessment->question_id = $qid;
                $assessment->attempt_no = $attm;
                $assessment->offering_id = $oid;
                $assessment->user_id = $id;
                $assessment->assessment_id = $a_id;
                $assessment->last_updated_ts = $date_time;
                $stmt_sub = $assessment->get_sub_options_calc();
                if($stmt_sub != ""){
                    
                }else{
                    if($assessment->insert_quiz_marks(0)){
                        
                    }
                }            
            } else if ($question_type_id == "7") {
                $assessment->question_id = $qid;
                $assessment->attempt_no = $attm;
                $assessment->offering_id = $oid;
                $assessment->user_id = $id;
                $assessment->assessment_id = $a_id;
                $assessment->last_updated_ts = $date_time;
                $stmt_sub = $assessment->get_sub_options_calc();
                 if($stmt_sub != ""){
                    
                }else{
                    if($assessment->insert_quiz_marks(0)){
                        
                    }
                }            
            }
        }

        $assessment->offering_id = $oid;
        $assessment->user_id = $id;
        $assessment->attempt_no = $attm;
        $assessment->sequence_id = $m;
        $assessment->question_id = $qid;
        $assessment->time = $times[$m - 1];
        $assessment->last_updated_ts = $date_time;

        if ($assessment->insert_times()) {
            
        } else {
            $conn->rollBack();
            echo 'error';
            return -1;
        }
    }
    $stmt2 = $assessment->get_passing_score();
    while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
        extract($row2);
        $p_scr = $passing_score;
    }
    $scr = ($marks * 100) / $total_marks;
    $res = null;
    if ($scr >= $p_scr) {
        $res = "Pass";
    } else {
        $res = "Fail";
    }
    
    //have to check $sub_assmt_null=false
    
    if($sub_assmt_null == 0){
        $val2 = $assessment->update_quiz_score($total_marks, $scr, $date_time, $attm);
        if ($val2) {

        } else {
            $conn->rollBack();
            echo 'error';
            return -1;
        }
        require '../cal_result.php';
    }
    $conn->commit();
    echo $sub_assmt_null;
} else {
    exit('Restricted Access');
}
?>