<?php
include_once("_nemo.list.cls.php");

//NOT IN USE ANYMORE
class DContent extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey)
   {
      $this->Filters[frSearch]->tag = "input";
      $this->Filters[frSearch]->html->value = "";
      $this->Filters[frSearch]->html->type = "text";
      $this->Filters[frSearch]->html->class = "controlText";

      parent::__construct($DataKey);
   }

   public function getList($strFilename)
   {
      global $SystemSettings;
      //print_rr($strFilename);
      $Where = "";

      if(!isset($strFilename))
      {
         if($this->Filters[frSearch]->html->value != "")
         {
            $like = "LIKE(". $this->db->qs("%".$this->Filters[frSearch]->html->value."%") .")";
            $Where .= " AND (modDownloadContent.UniqueID $like
               OR modDownloadContent.strFilename $like
               OR tblCourse.strCourse $like
               OR tblCourse.strCourseCode $like
               OR modModule.strModuleName $like )";
         }
      }
      else
      {
         $Where = " AND modDownloadContent.strFilename = '$strFilename'";
      }

      $strPathLearner = $SystemSettings[DownloadableContentDirLearner];
      // GET LIST
      $this->ListSQL("
         SELECT   modDownloadContent.DownloadID,
                  tblCourse.strCourse AS Course,
                  tblCourse.strCourseCode AS Code,
                  modModule.strModuleName AS module,
                  'Note' AS Type,
                  modDownloadContent.strFilename AS Filename
         FROM modDownloadContent
            INNER JOIN modNote ON modNote.NoteID = modDownloadContent.UniqueID
            INNER JOIN modModule ON modNote.refModuleID = modModule.ModuleID
            LEFT JOIN tblCourse ON modModule.refCourseID = tblCourse.CourseID
         WHERE 1=1 $Where

         UNION ALL

         SELECT   modDownloadContent.DownloadID,
                  tblCourse.strCourse AS Course,
                  tblCourse.strCourseCode AS Code,
                  modModule.strModuleName AS module,
                  'Practical' AS Type,
                  modDownloadContent.strFilename AS Filename
         FROM modDownloadContent
            INNER JOIN modPractical ON modPractical.PracticalID = modDownloadContent.UniqueID
            INNER JOIN modModule ON modPractical.refModuleID = modModule.ModuleID
            LEFT JOIN tblCourse ON modModule.refCourseID = tblCourse.CourseID
         WHERE 1=1 $Where

         UNION ALL

         SELECT   modDownloadContent.DownloadID,
                  tblCourse.strCourse AS Course,
                  tblCourse.strCourseCode AS Code,
                  modModule.strModuleName AS module,
                  'Assignment' AS Type,
                  modDownloadContent.strFilename AS Filename
         FROM modDownloadContent
            INNER JOIN modAssignment ON modAssignment.AssignmentID = modDownloadContent.UniqueID
            INNER JOIN modModule ON modAssignment.refModuleID = modModule.ModuleID
            LEFT JOIN tblCourse ON modModule.refCourseID = tblCourse.CourseID
         WHERE 1=1 $Where

         ORDER BY DownloadID Asc", 0);

      return $this->renderTable("Downloadable Content Database");
   }

   public static function Update()
   {
      global $SystemSettings;
//print_rr($_POST);
      if($_POST[chkEnableArticles] == "on")
      {
         foreach($_POST[chkSelect] as $key => $value)
         {
            $db = new NemoDatabase("modDownloadContent", $key, null, 0);
            $db->Fields["strFilename"] = $_POST[chosenArticle];

            $result = $db->Save();

            $message = "Content Updated. ";

         }
      }
      else if($_FILES['fuDC']['name'] != "")
      {
         foreach($_POST[chkSelect] as $key => $value)
         {
            $db = new NemoDatabase("modDownloadContent", $key, null, 0);

            $strOldFilename = $db->Fields["strFilename"];
            $db->Fields["strFilename"] =
            $strFileName = $db->Fields["UniqueID"] ."_". $_FILES["fuDC"]["name"];
            $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;

            $strPath = $SystemSettings[DownloadableContentDirAdmin];

            chmod($_FILES['fuDC']['tmp_name'] , 0777);
            if(move_uploaded_file($_FILES['fuDC']['tmp_name'], $strPath . $strFileName))
            {
               if($strOldFilename != "" && $strOldFilename != $strFileName)
               {
                  unlink($strPath.$strOldFilename);
               }

               $message = "Content Updated. ";
            }

            $result = $db->Save();


         }
         if($result->Error == 1)
               return $result->Message;
            else
               return $message;
      }
      else
      {
         $message = "No File Selected. ";
         return $message;
      }
   }

   public static function Save(&$ArticleID)
   {
      global $SystemSettings;
      $db = new NemoDatabase("tblArticle", $ArticleID, null, 0);
      $strOldFilename = nCopy($db->Fields["strFilename"]);

      $db->SetValues($_POST);

      if($_FILES['strPath']['name'] != "")
      {
         $strPath = $SystemSettings[DownloadableContentDirAdmin];

         $db->Fields["strPath"] = $strFileName = $_FILES["strPath"]["name"];

         chmod($_FILES['strPath']['tmp_name'] , 0777);
         if(move_uploaded_file($_FILES['strPath']['tmp_name'], $strPath . $strFileName) && $strOldFilename != "")
            unlink($strPath.$strOldFilename);
      }

      $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;

      $result = $db->Save();



   }


}
?>
