<?php

include_once "../../../config/settings.php";
include_once "../../../controllers/DataBaseController.php";
include_once "../../../classes/User.php";
include_once "../../../classes/Course.php";

class CoursesOptionsJSON extends DataBaseController{

    public function CoursesOptionsJSON(){

        parent::DataBaseController();

        if (isset($_POST["search"])) {
            //If name is empty or null return
            if (!isset($_POST["name"])) {
                echo json_encode('<p>You must put the name of the student</p>');
                exit();
            }
            if ($_POST["name"] == "") {
                echo json_encode('<p>You must put the name of the student</p>');
                exit();
            }


            $user = new User();
            //return student list
            $student_list = $user->getStudentsList($_POST["name"]);
            echo json_encode($student_list);

        }elseif (isset($_POST["insert"])) {
            if (!isset($_POST["id_user"])) {
                exit();
            }
            if (!isset($_POST["id_course"])) {
                exit();
            }
            $course = new course();
            //return student list
            if (!$course->hasCourseInscription((int)$_POST["id_user"], (int)$_POST["id_course"]))
                $course->addCourseInscription((int)$_POST["id_user"], (int)$_POST["id_course"]);
            echo json_encode(true);
            exit();
        }elseif (isset($_POST["get_list"])) {

            if (!isset($_POST["id_course"])) {
                exit();
            }

            $course = new course();

            //return student list
            $student_list = $course->getStudentInscriptions((int)$_POST["id_course"]);
            echo json_encode($student_list);

            exit();
        }elseif (isset($_POST["remove"])) {
            if (!isset($_POST["id_user"])) {
                exit();
            }
            if (!isset($_POST["id_course"])) {
                exit();
            }
            $course = new course();
            //return student list
            if ($course->hasCourseInscription((int)$_POST["id_user"], (int)$_POST["id_course"]))
                $course->removeCourseInscription((int)$_POST["id_user"], (int)$_POST["id_course"]);
            echo json_encode(true);
            exit();
        }



    }
}

new CoursesOptionsJSON();

?>