<?php

session_start();
include_once "../config/database.php";
include_once '../security/session_config.php';
include_once '../security/csrf.php';
include_once "../resources/assessment.php";
include_once '../config/properties.php';

$assmt_path = $properties['jdbc.admin'] . $properties['jdbc.assessment_folder'];

$csrf_token = $_POST['csrf_token'];
validate_csrf_request($csrf_token);

$assessment = new Assessment();
$aid = $_POST['aid'];
$oid = $_POST['oid'];
$result = $_POST['result'];
$assessment->assessment_id = $aid;
$assessment->offering_id = $oid;
$assessment->last_updated_ts = $date_time;
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' )) {
    if (isset($_SESSION['uid'])) {
        $id = $_SESSION['uid'];
    }
    $assessment->user_id = $id;
    if (isset($_POST['question_id'])) {
        $question_id = $_POST['question_id'];
        $options = $_POST['options'];
        $allopt = $_POST['allopt'];
        $out = array();
        $out1 = array();
        $assessment->question_id = $question_id;

        $stmt = $assessment->get_all_option_desc();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);
            if ($option_desc != "") {
                array_push($out, $option_desc);
            } else {
                array_push($out, $option_image);
            }
        }

        $options_var = "option_id in (" . $options . ")";
        $stmt = $assessment->get_feedback($options_var);
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);
            array_push($out1, $feedback);
        }

        echo json_encode(array(
            "answer" => $out,
            "feedback" => $out1
        ));
        //changes 
        if ($result == 0) {
            $lesson_id = $assessment->getLessonId();
            $assessment->lesson_id = $lesson_id;
            $m = 0;
            $allopt = explode(',', $allopt);
            $option = explode(',', $options);
            foreach ($allopt as $opt) {
                $m = $m + 1;
                $response_id = '';
                if (in_array($opt, $option)) {
                    $response_id = "Y";
                }
                $assessment->sequence_id = $m;
                $assessment->option_id = $opt;
                $assessment->user_response = $response_id;
                if ($assessment->insert_cyu_quiz_response()) {
                    
                }
            }
            $marks = 0;
            $stmt1 = $assessment->get_marks_question();
            if ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
                extract($row1);
                if ($question_type_id == "1" || $question_type_id == "2") {
                    $op_final_sr = null;
                    $stmt2 = $assessment->get_option_id();
                    while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
                        extract($row2);
                        $in = '';
                        if ($op_final_sr != "") {
                            $in = ",";
                        }
                        $op_final_sr = $op_final_sr . $in . $option_id;
                    }
                    if ($options == $op_final_sr) {
                        //insert values to quiz_response_mark with actual marks
                        if ($assessment->insert_cyu_quiz_marks($question_marks)) {
                            
                        }
                    } else {
                        if ($assessment->insert_cyu_quiz_marks(0)) {
                            
                        }
                    }
                }
            }
        }
    } else if (isset($_POST['question_id1'])) {
        $question_id = $_POST['question_id1'];
        $options = $_POST['options'];
        $selected = $_POST['selectedopt'];

        $out = array();
        $out1 = array();
        $assessment->question_id = $question_id;
        $lesson_id = $assessment->getLessonId();
        $assessment->lesson_id = $lesson_id;
        $m = 0;
        $op_final_sr = null;
        $stmt = $assessment->get_answer_option_MF();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);
            array_push($out, $answer_option);
            $op_final_sr = $op_final_sr . '' . $answer_option;
        }

//        $options_var = "option_id in (" . $options . ")";
//        $stmt = $assessment->get_feedback($options_var);
//        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
//            extract($row);
//            array_push($out1, $feedback);
//        }

        $html = "";
        $html .= "<div class='table-responsive mt-20'>";
        $html .= "<h3> " . _('CA0112') . " :</h3>";
        $html .= "<table class='table_mf_ques w-1/1'>";
        $html .= "<colgroup>
                            <col style='width:30%'>
                            <col style='width:30%'>
                            <col style='width:30%'>
                        </colgroup>";
        $html .= "<thead>
                            <tr>
                                <th>" . _("AS0128") . "</th>
                                <th>" . _("AS0129") . "</th>
                                <th>" . _("AS0115") . "</th>
                            </tr>
                        </thead>";
        $html .= "<tbody>";
        $stmt = $assessment->get_answer_option_MF();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);
            $html .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
            $html .= "<td class='text-16'>" . htmlentities($option_desc);
            if ($option_image != "") {
                $html .= "<div class='relative assessment_image_div_option mt-5'>";
                $html .= "<a href='$assmt_path/$aid/$option_image' class='w-1/1 js-gallery' data-gallery='gallery2'>";
                $html .= "<img class='' src='$assmt_path/$aid/$option_image' alt='image'>";
                $html .= "<div class='absolute-full-center d-flex justify-center items-center'></div>";
                $html .= "</a>";
                $html .= "</div>";
            }
            $html .= '</td>';
            $assessment->answer_option = $answer_option;
            $stmt1 = $assessment->get_correct_option();
            if ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
                extract($row1);
                $html .= '<td style="padding:10px;">' . htmlentities($option_desc) . '</td>';
            }
            $html .= "<td>" . htmlentities($feedback) . "</td>";
            $html .= '</tr>';
        }
        $html .= '</table>';

        echo json_encode(array(
            "answer" => $out,
//            "feedback" => $out1,
            "correct_answer" => $html
        ));
        //changes 
        if ($result == 0) {
            $selectedopt = explode(',', $selected);
            $options = explode(',', $options);
            $op_final = null;
            foreach ($selectedopt as $opt) {
                $op_final = $op_final . '' . $opt;
            }
            foreach ($options as $opt) {
                $selected_opt = $selectedopt[$m];
                $m = $m + 1;
                $assessment->sequence_id = $m;
                $assessment->option_id = $opt;
                $assessment->user_response = $selected_opt;
                if ($assessment->insert_cyu_quiz_response()) {
                    
                }
            }
            $stmt1 = $assessment->get_marks_question();
            if ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
                extract($row1);
                if ($op_final == $op_final_sr) {
                    if ($assessment->insert_cyu_quiz_marks($question_marks)) {
                        
                    }
                } else {
                    if ($assessment->insert_cyu_quiz_marks(0)) {
                        
                    }
                }
            }
        }
    } else if (isset($_POST['question_id2'])) {
        $question_id = $_POST['question_id2'];
        $options = $_POST['options'];
        $answers = $_POST['answers'];

        $results_correct_wrong = array();
        $answers_db = array();
        $feedbacks = array();

        $assessment->question_id = $question_id;
        $lesson_id = $assessment->getLessonId();
        $assessment->lesson_id = $lesson_id;
        $m = 0;
        $ans_options = explode(",", $answers);
        $opt_options = explode(",", $options);
        foreach ($ans_options as $key => $answer) {
            $m = $m + 1;
            $assessment->sequence_id = $m;
            $assessment->user_response = '';
            $assessment->option_id = $opt_options[$key];
            $stmt1 = $assessment->get_fib_options_cyu($answer);
            if ($stmt1->rowCount() > 0) {
                if ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
                    $assessment->fib_answer = $answer;
                    array_push($results_correct_wrong, "Y");
                    array_push($answers_db, '');
                    //insert cyu_fib_question_response
                    if ($result == 0) {
                        if ($assessment->insert_cyu_fib_quiz_response()) {
                            
                        }
                    }
                }
            } else {
                array_push($results_correct_wrong, "N");
                $stmt2 = $assessment->get_fib_options_cyu_result();
                if ($stmt2->rowCount() > 0) {
                    if ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
                        $assessment->fib_answer = $answer;
                        extract($row2);
                        array_push($answers_db, $option_desc);
                        //insert cyu_fib_question_response
                        if ($result == 0) {
                            if ($assessment->insert_cyu_fib_quiz_response()) {
                                
                            }
                        }
                    }
                }
            }
        }

        $options_var = "option_id in (" . $options . ")";
        $stmt = $assessment->get_feedback($options_var);
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);
            array_push($feedbacks, $feedback);
        }

        echo json_encode(array(
            "result" => $results_correct_wrong,
            "answer" => $answers_db,
            "feedback" => $feedbacks
        ));
        if ($result == 0) {
            $stmt1 = $assessment->get_marks_question();
            if ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
                extract($row1);
                $result_contains_N = in_array('N', $results_correct_wrong);
                if ($result_contains_N) {
                    if ($assessment->insert_cyu_quiz_marks(0)) {
                        
                    }
                } else {
                    if ($assessment->insert_cyu_quiz_marks($question_marks)) {
                        
                    }
                }
            }
        }
    }
} else {
    exit('Restricted Access');
}
?>