<?php
/*

ALTER TABLE `modModule` DROP FOREIGN KEY `modModule.strCourseCode`;
ALTER TABLE `modModule` ADD CONSTRAINT `modModule.strCourseCode` FOREIGN KEY ( `refCourseID` ) REFERENCES `tblCourse` ( `CourseID` ) ON DELETE CASCADE ON UPDATE CASCADE

 *
 */

include_once("_nemo.list.cls.php");

class Module extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey)
   {
      parent::__construct($DataKey);
   }

   public function getList($frCourseID=0)
   {
      global $SystemSettings;

      $Where = "";

      if($frCourseID != 0)
      {
         $sqlSelect = ", concat('<input type=checkbox value=\"checked\" id=xchkSelect name=\"chkSelect[',ModuleID,']\" >')  as 'Select'";
         $this->isSelectable = 0;
         $Where .= " AND modModule.refCourseID = '$frCourseID'";
      }

      $this->ListSQL("
         SELECT modModule.ModuleID, modModule.strModuleName AS 'Module Name', modModule.intModuleNumber AS 'Number', modModule.dtReleaseDate AS 'Release Date', blnActive AS Active, strLastUser AS 'Last User', dtLastEdit AS 'Last Edit'$sqlSelect
         FROM modModule
         WHERE 1=1 $Where
         ORDER BY intModuleNumber, strModuleName",0,"module.php");

      //print_rr($this->Columns["Select"]);
      $this->Columns["Select"]->html->align = "center";
      $this->Columns["Select"]->html->width = "1%";
      return $this->renderTable("Course Modules List");
   }

   public function getCourseTypeModuleList($frCourseTypeID=0)
   {
      global $SystemSettings;

      $Where = "";

      $this->ListSQL("
         SELECT tblCourseTypeModule.CourseTypeModuleID, tblCourseTypeModule.strModuleName AS 'Module Name', tblCourseTypeModule.intModuleNumber AS 'Number', tblCourseTypeModule.strDefaultReleaseSchedule AS 'Default Release Schedule', tblCourseTypeModule.blnActive AS Active, tblCourseTypeModule.strLastUser AS 'Last User', tblCourseTypeModule.dtLastEdit AS 'Last Edit'
         FROM tblCourseTypeModule
         WHERE tblCourseTypeModule.refCourseTypeID = $frCourseTypeID
         ORDER BY intModuleNumber, strModuleName;",0,"course.type.module.php");
      return $this->renderTable("Default Modules List");
   }

   public static function Save(&$ModuleID)
   {
      $db = new NemoDatabase("modModule", $ModuleID, null, 0);

      $db->SetValues($_POST);

      $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;

      $result = $db->Save();

      if($ModuleID == 0) $ModuleID = $db->ID[ModuleID];

      if($result->Error == 1)
         return $result->Message;
      else
         return "Details Saved. ";
   }

   public static function Delete($chkSelect)
   {
      global $xdb;
      //print_rr($chkSelect);
      if(count($chkSelect) > 0){
      foreach($chkSelect as $key => $value)
      {
         $xdb->doQuery("DELETE FROM modModule WHERE ModuleID = ". $xdb->qs($key));
      }
         return "Records Deleted. ";
      }else{
         return "No Records Selected. ";
      }
   }

   public static function SaveCTM(&$CourseTypeModuleID)
   {
      $db = new NemoDatabase("tblCourseTypeModule", $CourseTypeModuleID, null, 0);

      $db->SetValues($_POST);

      $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;

      $result = $db->Save();

      if($CourseTypeModuleID == 0) $CourseTypeModuleID = $db->ID[CourseTypeModuleID];

      if($result->Error == 1)
         return $result->Message;
      else
         return "Details Saved. ";
   }

   public static function DeleteCTM($chkSelect)
   {
      global $xdb;
      //print_rr($chkSelect);
      if(count($chkSelect) > 0){
      foreach($chkSelect as $key => $value)
      {
         $xdb->doQuery("DELETE FROM tblCourseTypeModule WHERE CourseTypeModuleID = ". $xdb->qs($key));
      }
         return "Records Deleted. ";
      }
   }

   public static function DeleteChildren($chkSelect)
   {
      global $xdb;
      if(count($chkSelect) > 0){

         $strUniqueID = "'". implode("','", array_keys($chkSelect)) ."'";

         $xdb->doQuery("DELETE FROM modVideo WHERE VideoID IN ($strUniqueID)");
         $xdb->doQuery("DELETE FROM modNote WHERE NoteID IN ($strUniqueID)");
         $xdb->doQuery("DELETE FROM modPractical WHERE PracticalID IN ($strUniqueID)");
         $xdb->doQuery("DELETE FROM modAssignment WHERE AssignmentID IN ($strUniqueID)");

         $xdb->doQuery("DELETE FROM modMilestone WHERE UniqueID IN ($strUniqueID)");
         $xdb->doQuery("DELETE FROM modDownloadContent WHERE UniqueID IN ($strUniqueID)");

         return "Records Deleted. ";
      }
   }
}
?>
