ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilCourseFile.php
Go to the documentation of this file.
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 
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 
51  public function __construct($a_file_id = null)
52  {
53  global $ilErr,$ilDB,$lng;
54 
55  $this->ilErr = $ilErr;
56  $this->db = $ilDB;
57  $this->lng = $lng;
58 
59  $this->file_id = $a_file_id;
60  $this->__read();
61  }
62 
72  public static function _cloneFiles($a_source_id,$a_target_id)
73  {
74  $source = new ilFSStorageCourse($a_source_id);
75 
76  foreach(ilCourseFile::_readFilesByCourse($a_source_id) as $file_obj)
77  {
78  $new_file = new ilCourseFile();
79  $new_file->setCourseId($a_target_id);
80  $new_file->setFileName($file_obj->getFileName());
81  $new_file->setFileSize($file_obj->getFileSize());
82  $new_file->setFileType($file_obj->getFileType());
83  $new_file->create(false);
84 
85  $target = new ilFSStorageCourse($a_target_id);
86  $target->initInfoDirectory();
87  $source->copyFile($file_obj->getAbsolutePath(),$new_file->getAbsolutePath());
88  }
89  }
90 
91  function setFileId($a_id)
92  {
93  $this->file_id = $a_id;
94  }
95  function getFileId()
96  {
97  return $this->file_id;
98  }
99 
100  function getCourseId()
101  {
102  return $this->course_id;
103  }
104  function setCourseId($a_course_id)
105  {
106  $this->course_id = $a_course_id;
107  }
108 
109  function setFileName($a_name)
110  {
111  $this->file_name = $a_name;
112  }
113  function getFileName()
114  {
115  return $this->file_name;
116  }
118  {
119  $this->file_type = $a_type;
120  }
121  function getFileType()
122  {
123  return $this->file_type;
124  }
125  function setFileSize($a_size)
126  {
127  $this->file_size = $a_size;
128  }
129  function getFileSize()
130  {
131  return $this->file_size;
132  }
133  function setTemporaryName($a_name)
134  {
135  $this->tmp_name = $a_name;
136  }
137  function getTemporaryName()
138  {
139  return $this->tmp_name;
140  }
141  function setErrorCode($a_code)
142  {
143  $this->error_code = $a_code;
144  }
145  function getErrorCode()
146  {
147  return $this->error_code;
148  }
149 
150  function getAbsolutePath()
151  {
152  if(is_object($this->fss_storage))
153  {
154  return $this->fss_storage->getInfoDirectory().'/'.$this->getFileId();
155  }
156  return false;
157  }
158 
159  function validate()
160  {
161  switch($this->getErrorCode())
162  {
163  case UPLOAD_ERR_INI_SIZE:
164  $this->ilErr->appendMessage($this->lng->txt('file_upload_ini_size'));
165  break;
166  case UPLOAD_ERR_FORM_SIZE:
167  $this->ilErr->appendMessage($this->lng->txt('file_upload_form_size'));
168  break;
169 
170  case UPLOAD_ERR_PARTIAL:
171  $this->ilErr->appendMessage($this->lng->txt('file_upload_only_partial'));
172  break;
173 
174  case UPLOAD_ERR_NO_TMP_DIR:
175  $this->ilErr->appendMessage($this->lng->txt('file_upload_no_tmp_dir'));
176  break;
177 
178  // not possible with php 4
179  #case UPLOAD_ERR_CANT_WRITE:
180  # $this->ilErr->appendMessage($this->lng->txt('file_upload_no_write'));
181  # break;
182 
183  case UPLOAD_ERR_OK:
184  case UPLOAD_ERR_NO_FILE:
185  default:
186  return true;
187  }
188  }
189 
190  function create($a_upload = true)
191  {
192  global $ilDB;
193 
194  if($this->getErrorCode() != 0)
195  {
196  return false;
197  }
198 
199  $next_id = $ilDB->nextId('crs_file');
200  $query = "INSERT INTO crs_file (file_id,course_id,file_name,file_size,file_type) ".
201  "VALUES( ".
202  $ilDB->quote($next_id,'integer').", ".
203  $ilDB->quote($this->getCourseId(),'integer').", ".
204  $ilDB->quote($this->getFileName(),'text').", ".
205  $ilDB->quote($this->getFileSize(),'integer').", ".
206  $ilDB->quote($this->getFileType(),'text')." ".
207  ")";
208  $res = $ilDB->manipulate($query);
209  $this->setFileId($next_id);
210 
211  $this->fss_storage = new ilFSStorageCourse($this->getCourseId());
212  $this->fss_storage->initInfoDirectory();
213 
214  if($a_upload)
215  {
216  // now create file
218  $this->getFileName(),
219  $this->fss_storage->getInfoDirectory().'/'.$this->getFileId());
220 
221  }
222  return true;
223  }
224 
225  function delete()
226  {
227  global $ilDB;
228 
229  // Delete db entry
230  $query = "DELETE FROM crs_file ".
231  "WHERE file_id = ".$ilDB->quote($this->getFileId(),'integer')."";
232  $res = $ilDB->manipulate($query);
233 
234  // Delete file
235  if(file_exists($this->getAbsolutePath()))
236  {
237  unlink($this->getAbsolutePath());
238  }
239 
240  return true;
241  }
242 
243  static function _deleteByCourse($a_course_id)
244  {
245  global $ilDB;
246 
247  // delete all course ids and delete assigned files
248  $query = "DELETE FROM crs_file ".
249  "WHERE course_id = ".$ilDB->quote($a_course_id,'integer')."";
250  $res = $ilDB->manipulate($query);
251 
252  return true;
253  }
254 
255 
260  public static function _readFilesByCourse($a_course_id)
261  {
262  global $ilDB;
263 
264  $query = "SELECT * FROM crs_file ".
265  "WHERE course_id = ".$ilDB->quote($a_course_id,'integer')."";
266 
267  $res = $ilDB->query($query);
268  while($row = $ilDB->fetchObject($res))
269  {
270  $files[] = new ilCourseFile($row->file_id);
271  }
272  return is_array($files) ? $files : array();
273  }
274 
275  function __read()
276  {
277  global $ilDB;
278 
279  if(!$this->file_id)
280  {
281  return true;
282  }
283 
284  // read file data
285  $query = "SELECT * FROM crs_file WHERE file_id = ".$ilDB->quote($this->file_id,'integer');
286  $res = $this->db->query($query);
287  while($row = $ilDB->fetchObject($res))
288  {
289  $this->setFileName($row->file_name);
290  $this->setFileSize($row->file_size);
291  $this->setFileType($row->file_type);
292  $this->setCourseId($row->course_id);
293  }
294  $this->fss_storage = new ilFSStorageCourse($this->getCourseId());
295  return true;
296  }
297 
298 }
299 ?>
$files
Definition: add-vimline.php:18
setTemporaryName($a_name)
__construct($a_file_id=null)
Constructor.
$a_type
Definition: workflow.php:93
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
create($a_upload=true)
static _readFilesByCourse($a_course_id)
Create styles array
The data for the language used.
setCourseId($a_course_id)
static _deleteByCourse($a_course_id)
static _cloneFiles($a_source_id, $a_target_id)
Clone course files.