ILIAS PHP Cross Reference Learning Management Systems

Source: /Modules/Course/classes/class.ilCourseFile.php - 287 lines - 7034 bytes - Summary - Text - Print

   1  <?php
   2  /*
   3      +-----------------------------------------------------------------------------+
   4      | ILIAS open source                                                           |
   5      +-----------------------------------------------------------------------------+
   6      | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
   7      |                                                                             |
   8      | This program is free software; you can redistribute it and/or               |
   9      | modify it under the terms of the GNU General Public License                 |
  10      | as published by the Free Software Foundation; either version 2              |
  11      | of the License, or (at your option) any later version.                      |
  12      |                                                                             |
  13      | This program is distributed in the hope that it will be useful,             |
  14      | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
  15      | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
  16      | GNU General Public License for more details.                                |
  17      |                                                                             |
  18      | You should have received a copy of the GNU General Public License           |
  19      | along with this program; if not, write to the Free Software                 |
  20      | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
  21      +-----------------------------------------------------------------------------+
  22  */
  23  
  24  include_once ('Modules/Course/classes/class.ilFSStorageCourse.php');
  25  
  26  /**
  27  *
  28  * @author Stefan Meyer <meyer@leifos.com> 
  29  * @version $Id: class.ilCourseFile.php 23143 2010-03-09 12:15:33Z smeyer $
  30  * 
  31  * @extends Object
  32  */
  33  
  34  
  35  class ilCourseFile
  36  {
  37      var $ilErr;
  38      var $ilDB;
  39      var $tree;
  40      var $lng;
  41  
  42      var $course_id = null;
  43      var $file_id = null;
  44      
  45      private $fss_storage = null;
  46  
  47  	function ilCourseFile($a_file_id = null)
  48      {
  49          global $ilErr,$ilDB,$lng;
  50  
  51          $this->ilErr =& $ilErr;
  52          $this->db  =& $ilDB;
  53          $this->lng =& $lng;
  54  
  55          $this->file_id = $a_file_id;
  56          $this->__read();
  57      }
  58      
  59      /**
  60       * Clone course files
  61       *
  62       * @access public
  63       * @static
  64       *
  65       * @param int source id
  66       * @param int target_id
  67       */
  68  	public static function _cloneFiles($a_source_id,$a_target_id)
  69      {
  70          $source = new ilFSStorageCourse($a_source_id);
  71  
  72          foreach(ilCourseFile::_readFilesByCourse($a_source_id) as $file_obj)
  73          {
  74              $new_file = new ilCourseFile();
  75              $new_file->setCourseId($a_target_id);
  76              $new_file->setFileName($file_obj->getFileName());
  77              $new_file->setFileSize($file_obj->getFileSize());
  78              $new_file->setFileType($file_obj->getFileType());
  79              $new_file->create(false);
  80  
  81              $target = new ilFSStorageCourse($a_target_id);
  82              $target->initInfoDirectory();
  83              $source->copyFile($file_obj->getAbsolutePath(),$new_file->getAbsolutePath());
  84          }
  85      }
  86  
  87  	function setFileId($a_id)
  88      {
  89          $this->file_id = $a_id;
  90      }
  91  	function getFileId()
  92      {
  93          return $this->file_id;
  94      }
  95  
  96  	function getCourseId()
  97      {
  98          return $this->course_id;
  99      }
 100  	function setCourseId($a_course_id)
 101      {
 102          $this->course_id = $a_course_id;
 103      }
 104  
 105  	function setFileName($a_name)
 106      {
 107          $this->file_name = $a_name;
 108      }
 109  	function getFileName()
 110      {
 111          return $this->file_name;
 112      }
 113  	function setFileType($a_type)
 114      {
 115          $this->file_type = $a_type;
 116      }
 117  	function getFileType()
 118      {
 119          return $this->file_type;
 120      }
 121  	function setFileSize($a_size)
 122      {
 123          $this->file_size = $a_size;
 124      }
 125  	function getFileSize()
 126      {
 127          return $this->file_size;
 128      }
 129  	function setTemporaryName($a_name)
 130      {
 131          $this->tmp_name = $a_name;
 132      }
 133  	function getTemporaryName()
 134      {
 135          return $this->tmp_name;
 136      }
 137  	function setErrorCode($a_code)
 138      {
 139          $this->error_code = $a_code;
 140      }
 141  	function getErrorCode()
 142      {
 143          return $this->error_code;
 144      }
 145      
 146  	function getAbsolutePath()
 147      {
 148          if(is_object($this->fss_storage))
 149          {
 150              return $this->fss_storage->getInfoDirectory().'/'.$this->getFileId();
 151          }
 152          return false;
 153      }
 154  
 155  	function validate()
 156      {
 157          switch($this->getErrorCode())
 158          {
 159              case UPLOAD_ERR_INI_SIZE:
 160                  $this->ilErr->appendMessage($this->lng->txt('file_upload_ini_size'));
 161                  break;
 162              case UPLOAD_ERR_FORM_SIZE:
 163                  $this->ilErr->appendMessage($this->lng->txt('file_upload_form_size'));
 164                  break;
 165  
 166              case UPLOAD_ERR_PARTIAL:
 167                  $this->ilErr->appendMessage($this->lng->txt('file_upload_only_partial'));
 168                  break;
 169  
 170              case UPLOAD_ERR_NO_TMP_DIR:
 171                  $this->ilErr->appendMessage($this->lng->txt('file_upload_no_tmp_dir'));
 172                  break;
 173  
 174                  // not possible with php 4
 175              #case UPLOAD_ERR_CANT_WRITE:
 176              #    $this->ilErr->appendMessage($this->lng->txt('file_upload_no_write'));
 177              #    break;
 178  
 179              case UPLOAD_ERR_OK:
 180              case UPLOAD_ERR_NO_FILE:
 181              default:
 182                  return true;
 183          }
 184      }
 185  
 186  	function create($a_upload = true)
 187      {
 188          global $ilDB;
 189          
 190          if($this->getErrorCode() != 0)
 191          {
 192              return false;
 193          }
 194  
 195          $next_id = $ilDB->nextId('crs_file');
 196          $query = "INSERT INTO crs_file (file_id,course_id,file_name,file_size,file_type) ".
 197              "VALUES( ".
 198              $ilDB->quote($next_id,'integer').", ".
 199              $ilDB->quote($this->getCourseId(),'integer').", ".
 200              $ilDB->quote($this->getFileName(),'text').", ".
 201              $ilDB->quote($this->getFileSize(),'integer').", ".
 202              $ilDB->quote($this->getFileType(),'text')." ".
 203              ")";
 204          $res = $ilDB->manipulate($query);
 205          $this->setFileId($next_id);
 206  
 207          $this->fss_storage = new ilFSStorageCourse($this->getCourseId());
 208          $this->fss_storage->initInfoDirectory();
 209  
 210          if($a_upload)
 211          {
 212              // now create file
 213              ilUtil::moveUploadedFile($this->getTemporaryName(),
 214                  $this->getFileName(),
 215                  $this->fss_storage->getInfoDirectory().'/'.$this->getFileId());
 216              
 217          }
 218          return true;
 219      }
 220  
 221  	function delete()
 222      {
 223          global $ilDB;
 224          
 225          // Delete db entry
 226          $query = "DELETE FROM crs_file ".
 227              "WHERE file_id = ".$ilDB->quote($this->getFileId(),'integer')."";
 228          $res = $ilDB->manipulate($query);
 229  
 230          // Delete file
 231          unlink($this->getAbsolutePath());
 232  
 233          return true;
 234      }
 235          
 236  	function _deleteByCourse($a_course_id)
 237      {
 238          global $ilDB;
 239  
 240          // delete all course ids and delete assigned files
 241          $query = "DELETE FROM crs_file ".
 242              "WHERE course_id = ".$ilDB->quote($a_course_id,'integer')."";
 243          $res = $ilDB->manipulate($query);
 244  
 245          return true;
 246      }
 247  
 248      function &_readFilesByCourse($a_course_id)
 249      {
 250          global $ilDB;
 251  
 252          $query = "SELECT * FROM crs_file ".
 253              "WHERE course_id = ".$ilDB->quote($a_course_id,'integer')."";
 254  
 255          $res = $ilDB->query($query);
 256          while($row = $ilDB->fetchObject($res))
 257          {
 258              $files[] =& new ilCourseFile($row->file_id);
 259          }
 260          return is_array($files) ? $files : array();
 261      }
 262  
 263  	function __read()
 264      {
 265          global $ilDB;
 266          
 267          if(!$this->file_id)
 268          {
 269              return true;
 270          }
 271  
 272          // read file data
 273          $query = "SELECT * FROM crs_file WHERE file_id = ".$ilDB->quote($this->file_id,'integer');
 274          $res = $this->db->query($query);
 275          while($row = $ilDB->fetchObject($res))
 276          {
 277              $this->setFileName($row->file_name);
 278              $this->setFileSize($row->file_size);
 279              $this->setFileType($row->file_type);
 280              $this->setCourseId($row->course_id);
 281          }
 282          $this->fss_storage = new ilFSStorageCourse($this->getCourseId());
 283          return true;
 284      }
 285          
 286  }
 287  ?>

title

Description

title

Description

title

Description

title

title

Body