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