<?php

include_once "../../../config/settings.php";
include_once "../../../controllers/DataBaseController.php";
include_once "../../../classes/Answer.php";
include_once "../../../classes/Question.php";

class AnswerOptionsJSON extends DataBaseController{

    public function AnswerOptionsJSON(){

        parent::DataBaseController();

        if (isset($_POST["insert"])) {
            if (!isset($_POST["id_question"])) {
                echo json_encode(false);
                exit();
            }

            if (!isset($_POST["answer"])) {
                echo json_encode(false);
                exit();
            }

            if ($_POST["answer"]=="") {
                echo json_encode(false);
                exit();
            }

            if (!isset($_POST["is_correct"])) {
                $is_correct= 0;
            }else{
                if ($_POST["is_correct"]==1)
                    $is_correct= 1;
                else
                    $is_correct = 0;
            }



            $answer = new Answer();
            $answer->addNewAnswer((int)$_POST["id_question"], $_POST["answer"],  $is_correct);


            echo json_encode(true);
            exit();
        }elseif (isset($_POST["get_list"])) {

            if (!isset($_POST["id_question"])) {
                echo json_encode(false);
                exit();
            }

            $question = new Question();

            //return question list
            $answers = $question->getAnswers((int)$_POST["id_question"]);
            echo json_encode($answers);

            exit();
        }elseif (isset($_POST["remove"])) {
            if (!isset($_POST["id_answer"])) {
                echo json_encode(false);
                exit();
            }


            $answer = new Answer();
            //return student list

            $answer->removeAnswer((int)$_POST["id_answer"]);
            echo json_encode(true);
            exit();
        }



    }
}

new AnswerOptionsJSON();

?>