<?php

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

class QuestionOptionsJSON extends DataBaseController{

    public function QuestionOptionsJSON(){

        parent::DataBaseController();

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

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


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

            $question = new Question();
            $question->insertQuestion((int)$_POST["id_test"], (float)$_POST["points"], $_POST["question"]);


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

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

            $test = new Test();

            //return question list
            $questions = $test->getQuestions((int)$_POST["id_test"]);
            echo json_encode($questions);

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


            $question = new Question();
            //return student list

            $question->removeQuestion((int)$_POST["id_question"]);
            echo json_encode(true);
            exit();
        }



    }
}

new QuestionOptionsJSON();

?>