<?php
/*

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

ALTER TABLE `modForum` DROP FOREIGN KEY `modForum.strLearner`;
ALTER TABLE `modForum` ADD CONSTRAINT `modForum.strLearner` FOREIGN KEY ( `refLearnerID` ) REFERENCES `tblLearner` ( `LearnerID` ) ON DELETE CASCADE ON UPDATE CASCADE

 *
 */

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

class Forum extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey)
   {

      parent::__construct($DataKey);

      $this->isSelectable = 
      $this->isPageable =
      $this->isSortable = 0; //subLIST!!
   }

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

      $Where = "";

      if($frModuleID != 0)
      {
         $Where .= " AND modForum.refModuleID = '$frModuleID'";
      }

      $this->ListSQL("
         SELECT modForum.ForumID, CASE WHEN tblLearner.LearnerID = -1 THEN sysUser.strUser ELSE tblLearner.strLearner END AS 'Learner/User'
            , modForum.strTitle AS Title, Count(modReply.ForumID) AS 'No Posts', modForum.dtLastEdit AS 'First Posted', Max(modReply.dtLastEdit) AS 'Latest Post'
         FROM ((modForum INNER JOIN tblLearner ON modForum.refLearnerID = tblLearner.LearnerID) LEFT JOIN modForum AS modReply ON modForum.ForumID = modReply.refForumID) LEFT JOIN sysUser ON modForum.refUserID = sysUser.UserID
         WHERE modForum.refForumID=0 $Where
         GROUP BY modForum.ForumID, tblLearner.strLearner, modForum.strTitle, modForum.dtLastEdit
         ORDER BY Max(modReply.dtLastEdit) DESC , modForum.dtLastEdit DESC",0,"mod.forum.php");
      return $this->renderTable("Forum Post List");

   }

   public function getPosts($ForumID)
   {
      global $SystemSettings, $xdb, $BR, $frModuleID;

      $Where = "";
      
      $this->isSelectable = 
      $this->isPageable =
      $this->isSortable = 0;

      $this->ListSQL("
         SELECT modForum.dtPost AS 'Date', CASE WHEN tblLearner.LearnerID = -1 THEN sysUser.strUser ELSE tblLearner.strLearner END AS 'Learner/User'
            , REPLACE(modForum.txtPost,CHAR(13),'$BR') AS Post, modForum.blnActive as Active
            , concat('<input type=checkbox name=chkSelect[', ForumID,'] value=1 />') as 'Select'
         FROM sysUser RIGHT JOIN (modForum INNER JOIN tblLearner ON modForum.refLearnerID = tblLearner.LearnerID) ON sysUser.UserID = modForum.refUserID
         WHERE refForumID= ". $xdb->qs($ForumID) ."
         ORDER BY modForum.blnActive DESC, modForum.dtPost DESC",0);
      
      $this->Columns["Select"]->html->align = "center";
      return $this->renderTable("Forum Post List");

   }

   public static function Save(&$ForumID)
   {
      //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("modForum", $ForumID, null, 0); //set Uninque restraint: none

      $db->SetValues($_POST);

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

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

      if($result->Error == 1)
         return $result->Message;
      else
         return "Details Saved. ";
   }
   
   public static function SavePost(&$ForumID)
   {
      global $DT;
      //note: the UID prefic needs to be passed to the db.save(), else it will still gen a 6digit UID. UID = CourseTypeCode + Obf, e.g. "ABCNOT123456"
      $db = new NemoDatabase("modForum", 0, null, 0); //set Uninque restraint: none

      //$db->SetValues($_POST);
      $db->Fields[refModuleID] = $_POST[refModuleID];
      $db->Fields[refLearnerID] = -1; // "system" ie not a learner
      $db->Fields[refUserID] = $_SESSION['USER']->ID;
      $db->Fields[refForumID] = $ForumID;
      $db->Fields[txtPost] = $_POST[txtReply];
      $db->Fields[dtPost] = $DT;
      $db->Fields[blnRead] = 1;
      $db->Fields[blnActive] = 1;
      $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;
//print_rr($db->Fields); die;
      $result = $db->Save(0,0);
//print_rr($result);
//print_rr($db);
      //if($ForumID == "") $ForumID = $db->ID[ForumID];

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

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

   public static function AddPostForm()
   {
      global $BR;
      
      return "
         <table name=tblNewPost id=tblNewPost class='tblNemoList' cellpadding=0 cellspacing=1 width='100%'>
            <caption>Add a Post</caption>
            <tr>
               <td>
                  <textarea name=txtReply id=txtReply class='controlText controlWideMax' style='height: 100px; width: 100%;' ></textarea>$BR
                  $BR
                  <input class='btn fr controlButton' type='submit' name='Action' value='Reply' />
                  $BR
                  $BR
               </td>
            </tr>         
         </table>
         $BR";
   }
}
?>
