<?php

include('phpClasses/Course.php');
$course = new Course();
include('phpClasses/CourseModule.php');

$courseMod = new CourseModule();

if (isset($_POST['coscode_check'])) {
    $cos_code = filter_var($_POST['coscode'], FILTER_SANITIZE_STRING);
    $avail = $course->checkCsCode($cos_code);
    if ($avail == 'taken') {
        echo "taken";
        exit();
    } else {
        echo 'not_taken';

        exit();
    }
    exit();
}

if (isset($_POST['save'])) {
    $cos_code = filter_var($_POST['coscode'], FILTER_SANITIZE_STRING);
    $ses_code = filter_var($_POST['sescode'], FILTER_SANITIZE_STRING);
    
    $avail = $course->checkCsCode($cos_code);

    if ($avail == 'taken') {
        //echo "exists course";
        $avail2 = $courseMod->chkCsModule($cos_code, $ses_code);
        if ($avail2 == 'exist') {
            echo ("assigned");
            exit();
        } else {
            $courseMod->createCsModule($cos_code, $ses_code);
            echo ("saved");

            exit();
        }
    } else {
        echo 'Not Exist';
        exit();
    }
}
?>
