<?php
/*

ALTER TABLE `modPractical` DROP FOREIGN KEY `modPractical.strModuleName`;
ALTER TABLE `modPractical` ADD CONSTRAINT `modPractical.strModuleName` FOREIGN KEY ( `refModuleID` ) REFERENCES `modModule` ( `ModuleID` ) ON DELETE CASCADE ON UPDATE CASCADE

NOTE UID = CourseCode + NOT + Obf = "ABCNOT123456"
 *
 */

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

class Practical extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey)
   {

      parent::__construct($DataKey);

      $this->isSelectable = 1;
      $this->isPageable =
      $this->isSortable = 0;
   }

   public function getList($frModuleID=0, $frStatus="")
   {
      global $SystemSettings;

      $Where = "";

      if($frModuleID != 0)
      {
         $Where .= " AND modPractical.refModuleID = '$frModuleID'";
      }
      if($frStatus != "")
      {
         $WhereS .= " AND modMilestone.strStatus='$frStatus'";
      }
      $this->ListSQL("
         SELECT PracticalID, strPractical AS Title, (SELECT COUNT(MilestoneID) FROM modMilestone WHERE UniqueID = PracticalID $WhereS ) AS 'No $frStatus', CASE WHEN Count(modDownloadContent.DownloadID) > 0 THEN 'Yes' ELSE 'No' END AS 'Downloadable Content', modPractical.blnActive AS Active, modPractical.strLastUser AS 'Last User', modPractical.dtLastEdit AS 'Last Edit'
         FROM modDownloadContent RIGHT JOIN modPractical ON modDownloadContent.UniqueID = PracticalID
         WHERE 1=1 $Where
         GROUP BY PracticalID, strPractical, modPractical.blnActive, modPractical.strLastUser, modPractical.dtLastEdit
         ORDER BY strPractical",0,"mod.practical.php");
      return $this->renderTable("Template List");

   }

   public static function Save(&$PracticalID, $strUniqueID_Prefix)
   {
      //note: the UID prefic needs to be passed to the db.save(), else it will still gen a 6digit UID. UID = CourseTypeCode + Module Object Type + Obf, e.g. "ABCNOT123456"
      $db = new NemoDatabase("modPractical", $PracticalID, array("refModuleID","strPractical"), 0); //set Uninque restraint @ refModuleID & strPractical

      $db->SetValues($_POST);

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

      $result = $db->Save(0,0,$strUniqueID_Prefix);
//print_rr($result);
//print_rr($db);
      if($PracticalID == "") $PracticalID = $db->ID[PracticalID];

      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 modPractical WHERE PracticalID = ". $xdb->qs($key));
      }
         return "Records Deleted.";
      }else{
         return "No Records Selected.";
      }
   }

}
?>
