require_once $_SERVER['DOCUMENT_ROOT'] . "/exam-simulator/private/functions/input.php";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $examID = fix_string($_POST['examID']);
    $question = fix_string($_POST['question']);
    $question_type = fix_string($_POST['questionType']);
    $questionID = fix_string($_POST['questionID']);
    edit_question($examID, $question, $questionID);
    switch ($question_type) {
        case 'mult_answers':
            $json_assoc_array = json_decode($_POST['answers']);
            $mult_answers = fix_input_assoc_array($json_assoc_array);
            // assoc array e.g {answer : 'true'}
            delete_answers($questionID, $question_type);
            create_multiple_choice_answers($questionID, $mult_answers);
            break;
        case 'single_answers':
            $answer = fix_string($_POST['answer']);
            delete_answers($questionID, $question_type);
            create_single_answer($answer, $questionID);
            break;
        case 'keyword_answers':
            $keywords = json_decode($_POST['answers']);
            $keywords = fix_input_array($keywords);
            remove_non_alphanumeric($keywords);
            delete_answers($questionID, $question_type);
            create_keyword_answers($questionID, $keywords);
            break;
    }
} else {
    go_home();
}
function create_category_from_json_array($data)
{
    $userName = (int) $_SESSION['userID'];
    $category_name = $data['category_name'];
    $number_of_exams = (int) $data['number_of_exams'];
    $catID = create_new_category($category_name, $number_of_exams);
    for ($i = 0; $i < $number_of_exams; $i++) {
        $examID = create_new_exam($data['exams'][$i]['exam_name'], $catID, $data['exams'][$i]['number_of_questions']);
        for ($y = 0; $y < $data['exams'][$i]['number_of_questions']; $y++) {
            $questionID = create_question($examID, $data['exams'][$i]['questions'][$y]['question_name'], $data['exams'][$i]['questions'][$y]['question_type'], $increment_questions = false);
            switch ($data['exams'][$i]['questions'][$y]['question_type']) {
                case 'single_answers':
                    create_single_answer($data['exams'][$i]['questions'][$y]['answers'], $questionID);
                    break;
                case 'mult_answers':
                    create_multiple_choice_answers_from_json($questionID, $data['exams'][$i]['questions'][$y]['answers']);
                    break;
                case 'keyword_answers':
                    create_keyword_answers($questionID, $data['exams'][$i]['questions'][$y]['answers']);
                    break;
            }
        }
    }
}