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();
}
         }
         for ($i = 0; $i < count($mult_checks); $i++) {
             $multID_answerID[$mult_checks[$i][1]] = array();
         }
         foreach ($multID_answerID as $key => $value) {
             for ($y = 0; $y < count($mult_checks); $y++) {
                 if ($key == $mult_checks[$y][1]) {
                     array_push($multID_answerID[$key], $mult_checks[$y][0]);
                 }
             }
         }
     }
 }
 if (isset($_POST['question_keyword_answer_id']) && isset($_POST['keyword_answers'])) {
     $question_keyword_answer_id = fix_input_array($_POST['question_keyword_answer_id']);
     $keyword_answers = fix_input_array($_POST['keyword_answers']);
     if (count($keyword_answers) > 0) {
         for ($i = 0; $i < count($question_keyword_answer_id); $i++) {
             $keyword_answers[$i] = preg_replace("/[^A-Za-z0-9 ]/", '', $keyword_answers[$i]);
             // remove non alphanumeric characters
             $keyword_answers[$i] = strtoupper($keyword_answers[$i]);
             $kwID_answer[$question_keyword_answer_id[$i]] = array_unique(explode(' ', $keyword_answers[$i]));
             $kwID_answer[$question_keyword_answer_id[$i]] = array_values($kwID_answer[$question_keyword_answer_id[$i]]);
         }
     }
 }
 if (isset($_POST['num_of_questions']) && isset($_POST['num_of_correct_answers']) && isset($_POST['questionID_order'])) {
     $num_of_questions = fix_string($_POST['num_of_questions']);
     $num_of_correct_answers = fix_string($_POST['num_of_correct_answers']);
     $questionID_order = fix_string($_POST['questionID_order']);
     $questionID_order = explode(',', $questionID_order);
// create a new poll question
// JavaScript needed to generate the number of answers
// however php will still check that the fields are valid
// check that - user is logged in - that inputs are valid
session_start();
require_once "../functions/sql_functions.php";
require_once "../functions/session_functions.php";
require_once "../functions/input.php";
require_once "../classes/poll.php";
require_once "../classes/question_file.php";
require_once "../classes/question_insert.php";
if ($_SERVER['REQUEST_METHOD'] == 'POST' && logged_in()) {
    $question = fix_string($_POST['question']);
    $answers = fix_input_array($_POST['answer']);
    $categories = fix_input_array($_POST['categories']);
    arraytoupper($categories);
    // ensure that all strings in array are uppercase to avoid unnessasary duplicates
    $user = $_SESSION['user'];
    if (!question_valid($question) && !input_array_valid($answers) && !input_array_valid($categories)) {
        go_home();
    }
    // failed to create new question
    $conn = get_conn();
    $poll_question = new Poll($conn);
    $poll_question->set_question($question);
    $poll_question->set_categories($categories);
    $poll_question->set_answers($answers);
    $poll_question->set_user($user);
    $result = $poll_question->save();
    $conn->close();