• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Modules/Course/classes/class.ilCourseFile.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 include_once('Modules/Course/classes/class.ilFSStorageCourse.php');
00025 
00035 class ilCourseFile
00036 {
00037         var $ilErr;
00038         var $ilDB;
00039         var $tree;
00040         var $lng;
00041 
00042         var $course_id = null;
00043         var $file_id = null;
00044         
00045         private $fss_storage = null;
00046 
00047         function ilCourseFile($a_file_id = null)
00048         {
00049                 global $ilErr,$ilDB,$lng;
00050 
00051                 $this->ilErr =& $ilErr;
00052                 $this->db  =& $ilDB;
00053                 $this->lng =& $lng;
00054 
00055                 $this->file_id = $a_file_id;
00056                 $this->__read();
00057         }
00058         
00068         public static function _cloneFiles($a_source_id,$a_target_id)
00069         {
00070                 $source = new ilFSStorageCourse($a_source_id);
00071 
00072                 foreach(ilCourseFile::_readFilesByCourse($a_source_id) as $file_obj)
00073                 {
00074                         $new_file = new ilCourseFile();
00075                         $new_file->setCourseId($a_target_id);
00076                         $new_file->setFileName($file_obj->getFileName());
00077                         $new_file->setFileSize($file_obj->getFileSize());
00078                         $new_file->setFileType($file_obj->getFileType());
00079                         $new_file->create(false);
00080 
00081                         $target = new ilFSStorageCourse($a_target_id);
00082                         $target->initInfoDirectory();
00083                         $source->copyFile($file_obj->getAbsolutePath(),$new_file->getAbsolutePath());
00084                 }
00085         }
00086 
00087         function setFileId($a_id)
00088         {
00089                 $this->file_id = $a_id;
00090         }
00091         function getFileId()
00092         {
00093                 return $this->file_id;
00094         }
00095 
00096         function getCourseId()
00097         {
00098                 return $this->course_id;
00099         }
00100         function setCourseId($a_course_id)
00101         {
00102                 $this->course_id = $a_course_id;
00103         }
00104 
00105         function setFileName($a_name)
00106         {
00107                 $this->file_name = $a_name;
00108         }
00109         function getFileName()
00110         {
00111                 return $this->file_name;
00112         }
00113         function setFileType($a_type)
00114         {
00115                 $this->file_type = $a_type;
00116         }
00117         function getFileType()
00118         {
00119                 return $this->file_type;
00120         }
00121         function setFileSize($a_size)
00122         {
00123                 $this->file_size = $a_size;
00124         }
00125         function getFileSize()
00126         {
00127                 return $this->file_size;
00128         }
00129         function setTemporaryName($a_name)
00130         {
00131                 $this->tmp_name = $a_name;
00132         }
00133         function getTemporaryName()
00134         {
00135                 return $this->tmp_name;
00136         }
00137         function setErrorCode($a_code)
00138         {
00139                 $this->error_code = $a_code;
00140         }
00141         function getErrorCode()
00142         {
00143                 return $this->error_code;
00144         }
00145         
00146         function getAbsolutePath()
00147         {
00148                 if(is_object($this->fss_storage))
00149                 {
00150                         return $this->fss_storage->getInfoDirectory().'/'.$this->getFileId();
00151                 }
00152                 return false;
00153         }
00154 
00155         function validate()
00156         {
00157                 switch($this->getErrorCode())
00158                 {
00159                         case UPLOAD_ERR_INI_SIZE:
00160                                 $this->ilErr->appendMessage($this->lng->txt('file_upload_ini_size'));
00161                                 break;
00162                         case UPLOAD_ERR_FORM_SIZE:
00163                                 $this->ilErr->appendMessage($this->lng->txt('file_upload_form_size'));
00164                                 break;
00165 
00166                         case UPLOAD_ERR_PARTIAL:
00167                                 $this->ilErr->appendMessage($this->lng->txt('file_upload_only_partial'));
00168                                 break;
00169 
00170                         case UPLOAD_ERR_NO_TMP_DIR:
00171                                 $this->ilErr->appendMessage($this->lng->txt('file_upload_no_tmp_dir'));
00172                                 break;
00173 
00174                                 // not possible with php 4
00175                         #case UPLOAD_ERR_CANT_WRITE:
00176                         #       $this->ilErr->appendMessage($this->lng->txt('file_upload_no_write'));
00177                         #       break;
00178 
00179                         case UPLOAD_ERR_OK:
00180                         case UPLOAD_ERR_NO_FILE:
00181                         default:
00182                                 return true;
00183                 }
00184         }
00185 
00186         function create($a_upload = true)
00187         {
00188                 global $ilDB;
00189                 
00190                 if($this->getErrorCode() != 0)
00191                 {
00192                         return false;
00193                 }
00194 
00195                 $query = "INSERT INTO crs_file ".
00196                         "SET course_id = ".$ilDB->quote($this->getCourseId()).", ".
00197                         "file_name = ".$ilDB->quote($this->getFileName()).", ".
00198                         "file_size = ".$ilDB->quote($this->getFileSize()).", ".
00199                         "file_type = ".$ilDB->quote($this->getFileType())." ";
00200                 
00201                 $res = $this->db->query($query);
00202                 $this->setFileId($this->db->getLastInsertId());
00203 
00204                 $this->fss_storage = new ilFSStorageCourse($this->getCourseId());
00205                 $this->fss_storage->initInfoDirectory();
00206 
00207                 if($a_upload)
00208                 {
00209                         // now create file
00210                         ilUtil::moveUploadedFile($this->getTemporaryName(),
00211                                 $this->getFileName(),
00212                                 $this->fss_storage->getInfoDirectory().'/'.$this->getFileId());
00213                         
00214                 }
00215                 return true;
00216         }
00217 
00218         function delete()
00219         {
00220                 global $ilDB;
00221                 
00222                 // Delete db entry
00223                 $query = "DELETE FROM crs_file ".
00224                         "WHERE file_id = ".$ilDB->quote($this->getFileId())."";
00225                 $this->db->query($query);
00226 
00227                 // Delete file
00228                 unlink($this->getAbsolutePath());
00229 
00230                 return true;
00231         }
00232                 
00233         function _deleteByCourse($a_course_id)
00234         {
00235                 global $ilDB;
00236 
00237                 // delete all course ids and delete assigned files
00238                 $query = "DELETE FROM crs_file ".
00239                         "WHERE course_id = ".$ilDB->quote($a_course_id)."";
00240                 $res = $ilDB->query($query);
00241 
00242                 return true;
00243         }
00244 
00245         function &_readFilesByCourse($a_course_id)
00246         {
00247                 global $ilDB;
00248 
00249                 $query = "SELECT * FROM crs_file ".
00250                         "WHERE course_id = ".$ilDB->quote($a_course_id)."";
00251 
00252                 $res = $ilDB->query($query);
00253                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00254                 {
00255                         $files[] =& new ilCourseFile($row->file_id);
00256                 }
00257                 return is_array($files) ? $files : array();
00258         }
00259 
00260         function __read()
00261         {
00262                 if(!$this->file_id)
00263                 {
00264                         return true;
00265                 }
00266 
00267                 // read file data
00268                 $query = "SELECT * FROM crs_file WHERE file_id = '".$this->file_id."'";
00269                 $res = $this->db->query($query);
00270                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00271                 {
00272                         $this->setFileName($row->file_name);
00273                         $this->setFileSize($row->file_size);
00274                         $this->setFileType($row->file_type);
00275                         $this->setCourseId($row->course_id);
00276                 }
00277                 $this->fss_storage = new ilFSStorageCourse($this->getCourseId());
00278                 return true;
00279         }
00280                 
00281 }
00282 ?>

Generated on Fri Dec 13 2013 17:56:49 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1