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();
    if (!$result) {
        // question not unique or something went wrong
        go_create();
    }
    // go back to create page
    go_find($poll_question->get_href());
} else {
    go_home();
}
require_once "../functions/input.php";
require_once "../classes/answered_poll.php";
if ($_SERVER['REQUEST_METHOD'] == "POST" && logged_in()) {
    $question_id = fix_string($_POST['id']);
    $username = $_SESSION['user'];
    $answer_id = fix_string($_POST['answer']);
    $rating = fix_string($_POST['rating']);
    $comment = fix_string($_POST['comment']);
    $location = fix_string($_POST['location']);
    if (!comment_left($comment)) {
        unset($comment);
    }
    $conn = get_conn();
    $answer = new AnsweredPoll($conn);
    $answer->set_id($question_id);
    $answer->answered_by($username);
    $answer->chosen_answer($answer_id);
    if (isset($comment)) {
        $answer->set_comment($comment);
    }
    $answer->set_rating($rating);
    if (!$answer->submit()) {
        // failure
        die('line 30');
    }
    $conn->close();
    go_find($location);
    // go back to the poll question once users answer has been insered into database
} else {
    go_home();
}