<?php
/*


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

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

class Milestone extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey=null)
   {

      parent::__construct($DataKey);

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

   public function getList($frUniqueID, $frStatus="", $CourseID="")
   {
      global $SystemSettings, $xdb;

      //$row = $xdb->getRowSQL("SELECT strType FROM modMilestone WHERE UniqueID=". $xdb->qs($frUniqueID)." GROUP BY strType");

      $Where = "";
      $UserID = $_SESSION["USER"]->ID;

      $rowBusinessCoach = $xdb->getRowSQL("SELECT * FROM sysUser WHERE UserID = '$UserID'",0);

      if($rowBusinessCoach->blnPracticeConsultant == 1 && $CourseID == 612){
         $Where .= " AND refBusinessCoachID = '$UserID' ";
      }

      if($frStatus != "")
      {
         $Where .= " AND modMilestone.strStatus = ". $xdb->qs($frStatus);
      }

      $Where .= " AND modMilestone.UniqueID = ". $xdb->qs($frUniqueID);

      $this->ListSQL("
         SELECT tblCourseLearner.strLearner AS Learner, modMilestone.strStatus AS Status, modMilestone.strLastUser AS 'Last User', modMilestone.dtLastEdit AS 'Last Edit'
         FROM (
            SELECT tblLearner.LearnerID, tblLearner.strLearner, tblLearner.refBusinessCoachID
            FROM tblLicence INNER JOIN tblLearner ON tblLicence.refLearnerID = tblLearner.LearnerID
            WHERE tblLicence.strStatus='Active'
            GROUP BY tblLearner.LearnerID, tblLearner.strLearner
            ) AS tblCourseLearner INNER JOIN modMilestone ON modMilestone.refLearnerID = tblCourseLearner.LearnerID
         WHERE 1=1 $Where
         GROUP BY tblCourseLearner.strLearner, modMilestone.strStatus, modMilestone.strLastUser, modMilestone.dtLastEdit
         ORDER BY modMilestone.strStatus, tblCourseLearner.strLearner",0);

      return $this->renderTable("Milestones $frStatus");
   }

   public function getListOther($frModuleID, $frUniqueID, $frStatus="")
   {
      global $SystemSettings, $xdb;

      //$row = $xdb->getRowSQL("SELECT strType FROM modMilestone WHERE UniqueID=". $xdb->qs($frUniqueID)." GROUP BY strType");

      $Where = "";

      if($frStatus != "")
      {
         $WhereS .= " AND (modMilestone.strStatus <> ". $xdb->qs($frStatus) ." )";
      }

      $Where .= " AND modAssessment.refModuleID = ". $xdb->qs($frModuleID) ."
                  AND (modMilestone.UniqueID = ". $xdb->qs($frUniqueID) ." $WhereS) OR modMilestone.UniqueID is NULL OR modMilestone.strStatus is NULL";

      $this->ListSQL("
         SELECT tblLearner.strLearner AS Learner, modMilestone.strStatus AS Status, modMilestone.strLastUser AS 'Last User', modMilestone.dtLastEdit AS 'Last Edit'
         FROM modAssessment INNER JOIN (modMilestone RIGHT JOIN tblLearner ON modMilestone.refLearnerID = tblLearner.LearnerID) ON modAssessment.refLearnerID = tblLearner.LearnerID
         WHERE 1=1 $Where
         ORDER BY strLearner, strStatus",1);

      return $this->renderTable("Milestones Other");
   }

   public static function CreateMilestones($refModuleID, $UniqueID, $strType, $strStatus="n/a")
   {
      global $SystemSettings, $xdb;

      if($strType == "Upload")
         $strType = "Assignment";

      if($UniqueID != "")
      {
         if($strStatus != ""){
            $colStatus = ", strStatus";
            $valStatus = ", '$strStatus' AS strStatus";
         }

         $xdb->doQuery("INSERT IGNORE INTO modMilestone ( UniqueID, refModuleID, refLearnerID, strType $colStatus )
            SELECT '$UniqueID' AS UniqueID, modAssessment.refModuleID, tblLearner.LearnerID, '$strType' AS strType $valStatus
            FROM tblLicence INNER JOIN ((modAssessment INNER JOIN tblLearner ON modAssessment.refLearnerID = tblLearner.LearnerID) INNER JOIN modModule ON modAssessment.refModuleID = modModule.ModuleID) ON (tblLicence.refCourseID = modModule.refCourseID) AND (tblLicence.refLearnerID = tblLearner.LearnerID)
            WHERE tblLicence.strStatus='Active' AND tblLearner.blnActive = 1
            GROUP BY modAssessment.refModuleID, tblLearner.LearnerID
            HAVING modAssessment.refModuleID=". $xdb->qs($refModuleID),0); //NEEDS an Assessment record for the Module your busy with!

         //print_rr($xdb);
         if($xdb->Rows >> 0)
            return $xdb->Rows ." Milestone(s) added. ";
      }
   }

   public static function DeleteMilestones($UniqueID)
   {
      global $SystemSettings, $xdb;

      $xdb->doQuery("DELETE FROM modMilestone WHERE UniqueID = ". $xdb->qs($UniqueID));

      //print_rr($xdb);
      return $xdb->Rows ." Milestone(s) deleted. ";
   }
}
?>
