<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//include connection file 
//include_once("db_cls_connect.php");
include_once("db_cls_connect.php");
//include('../partials/header.php');
$db = new dbObj();
$connString = $db->getConnstring();
/**
 * Description of Course
 *
 * @author Username
 */
class Course {

    public $courseId;
    public $courseDesc;
    public $courseUnit;
    public $department;

    public function __construct() {
        $db = new dbObj();
        $connString = $db->getConnstring();
        $this->conn = $connString;
    }

    public function getCourseId() {
        return $this->courseId;
    }

    public function getCourseDesc() {
        return $this->courseDesc;
    }

    public function getCourseUnit() {
        return $this->courseUnit;
    }

    public function getDepartment() {
        return $this->department;
    }

    public function setCourseId($courseId) {
        $this->courseId = $courseId;
    }

    public function setCourseDesc($courseDesc) {
        $this->courseDesc = $courseDesc;
    }

    public function setCourseUnit($courseUnit) {
        $this->courseUnit = $courseUnit;
    }

    public function setDepartment($department) {
        $this->department = $department;
    }

    public function checkCsCode($cosCode) {
        $cosCode = trim($cosCode);
        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM course WHERE CourseID='$cosCode'";


        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        if (mysqli_num_rows($query) > 0) {
            //echo 'taken';
            return 'taken';
        } else {

            return 'not_taken';
        }

        return "taken";
    }

    public function insertCourse($depcode, $coscode, $description, $cosunit) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "INSERT INTO course(CourseID, Description, UNIT, Departments_DepartmentCode) VALUES  (trim('$coscode'),trim('$description'), trim('$cosunit'), trim('$depcode'))";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }

    public function getAllCourses() {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = 'SELECT * FROM course';

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }

    public function getCourseLectureNotes($id) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM notes WHERE course_module_id='$id'";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }
    
    
    public function getCourseLectureVideos($id) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM course_videos_mapping WHERE course_module_id='$id'";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }
	
	    public function getCourseLectureSlides($id) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM course_slides_mapping WHERE course_module_id='$id'";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }
    
	public function getCourseLectureAssignments($id) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM course_assignments_mapping WHERE course_module_id='$id'";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }
    
    public function verifyCourseValidity($id) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM course WHERE CourseID='$id'";
        
        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
       
        if(mysqli_num_rows($query)>0){
            //valid
            return 1;
        }else{
            //wrong
            return 0;
        }
        
        
        
    }

    public function assignCourse($cosCode, $depCode, $lvlcode, $semcode, $catcode) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "INSERT INTO courselevelid(Departments_DepartmentCode, Elective, Level_Levelid, Course_CourseID, Semester) VALUES  (trim('$depCode'),trim('$catcode'), trim('$lvlcode'), trim('$cosCode'), trim('$semcode'))";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }

    public function checkCsAssign($cosCode, $depCode, $lvlcode, $semcode) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }
        $sql = "SELECT * FROM courselevelid WHERE Departments_DepartmentCode= '$depCode' AND Level_Levelid='$lvlcode' AND Course_CourseID='$cosCode' AND Semester='$semcode'";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        
        if (mysqli_num_rows($query) > 0) {
            //echo 'exist..';
            return 'exist';
        } else {
           
            return 'not_exist';
        }

        return "exist";
    }

}
