<?php

//include('partials/header.php');
include_once("db_cls_connect.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.
 */

/**
 * Description of CourseModule
 *
 * @author Username
 */
class CourseModule {

    private $courseModId;
    private $courseID;
    private $session;
    private $createdBy;

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

    public function getCourseModId() {
        return $this->courseModId;
    }

    public function getCourseID() {
        return $this->courseID;
    }

    public function getCreatedBy() {
        return $this->createdBy;
    }

    public function setCourseModId($courseModId) {
        $this->courseModId = $courseModId;
    }

    public function setCourseID($courseID) {
        $this->courseID = $courseID;
    }

    public function setCreatedBy($createdBy) {
        $this->createdBy = $createdBy;
    }

    public function chkCsModule($cosCode, $session) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }
        $sql = "SELECT * FROM coursemodule WHERE  session= '$session' AND Course_CourseID='$cosCode'";

        $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";
    }

    public function createCsModule($cosCode, $session) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }
//get the admin
        $yr1 = substr($session, 0, 4);
        $yr2 = substr($session, 5, 4);
        $moduleName = $cosCode . $yr1 . $yr2;
        $moduleName =  preg_replace('/\s+/','',$moduleName);
        
        $username = $_SESSION['user_session'];
        $date = date("d/m/Y");
        //get date
        //getname
        $sql = "INSERT INTO coursemodule(moduleName, session, Course_CourseID, createdBy, dateCreated) VALUES  (trim('$moduleName'),trim('$session'), trim('$cosCode'), trim('$username'), trim('$date'))";

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

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

    public function createModDirectory($moduleName) {
        $directoryName = 'CourseModules/';
        $directoryName1 = $directoryName.$moduleName.'/';
        
        echo $directoryName1;
//Check if the directory already exists.
        if (!is_dir($directoryName1)) {
            //Directory does not exist, so lets create it.
            mkdir($directoryName1, 0755 );//0755);
        }
        
        echo $directoryName1.'name' ;
        if (!is_dir($directoryName1.'notes')) {
            //Directory does not exist, so lets create it.
            mkdir($directoryName1.'notes', 0755 );//0755);
        }
        
        if (!is_dir($directoryName1.'videos')) {
            //Directory does not exist, so lets create it.
            mkdir($directoryName1.'videos', 0755);
        }
    }
    
    
     public function getCsModules($cosCode) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }
        $sql = "SELECT * FROM coursemodule WHERE  Course_CourseID='$cosCode'";

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

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

        
            return $query;
        
    }

}
