ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCourseFile.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=0);
26 {
27  private int $course_id = 0;
28  private int $file_id = 0;
29  private string $file_name = '';
30  private string $file_type = '';
31  private int $file_size = 0;
32  private string $tmp_name = '';
33  private int $error_code = 0;
34 
35  protected ilDBInterface $db;
36  protected ilLanguage $lng;
38  protected ?ilFSStorageCourse $fss_storage = null;
39 
40  public function __construct(int $a_file_id = 0)
41  {
42  global $DIC;
43 
44  $this->lng = $DIC->language();
45  $this->db = $DIC->database();
46  $this->error = $DIC['ilErr'];
47  $this->file_id = $a_file_id;
48  $this->__read();
49  }
50 
51  public static function _cloneFiles(int $a_source_id, int $a_target_id): void
52  {
53  $source = new ilFSStorageCourse($a_source_id);
54 
55  foreach (ilCourseFile::_readFilesByCourse($a_source_id) as $file_obj) {
56  $new_file = new ilCourseFile();
57  $new_file->setCourseId($a_target_id);
58  $new_file->setFileName($file_obj->getFileName());
59  $new_file->setFileSize($file_obj->getFileSize());
60  $new_file->setFileType($file_obj->getFileType());
61  $new_file->create(false);
62 
63  $target = new ilFSStorageCourse($a_target_id);
64  $target->initInfoDirectory();
65  $source->copyFile(
66  $file_obj->getAbsolutePath(),
67  $new_file->fss_storage->getInfoDirectory() . '/' . $new_file->getFileId()
68  );
69  }
70  }
71 
72  public function setFileId(int $a_id): void
73  {
74  $this->file_id = $a_id;
75  }
76 
77  public function getFileId(): int
78  {
79  return $this->file_id;
80  }
81 
82  public function getCourseId(): int
83  {
84  return $this->course_id;
85  }
86 
87  public function setCourseId(int $a_course_id): void
88  {
89  $this->course_id = $a_course_id;
90  }
91 
92  public function setFileName(string $a_name): void
93  {
94  $this->file_name = $a_name;
95  }
96 
97  public function getFileName(): string
98  {
99  return $this->file_name;
100  }
101 
102  public function setFileType(string $a_type): void
103  {
104  $this->file_type = $a_type;
105  }
106 
107  public function getFileType(): string
108  {
109  return $this->file_type;
110  }
111 
112  public function setFileSize(int $a_size): void
113  {
114  $this->file_size = $a_size;
115  }
116 
117  public function getFileSize(): int
118  {
119  return $this->file_size;
120  }
121 
122  public function setTemporaryName(string $a_name): void
123  {
124  $this->tmp_name = $a_name;
125  }
126 
127  public function getTemporaryName(): string
128  {
129  return $this->tmp_name;
130  }
131 
132  public function setErrorCode(int $a_code): void
133  {
134  $this->error_code = $a_code;
135  }
136 
137  public function getErrorCode(): int
138  {
139  return $this->error_code;
140  }
141 
142  public function getAbsolutePath(): string
143  {
144  // workaround for "secured" files.
145  if (!$this->fss_storage instanceof \ilFSStorageCourse) {
146  return '';
147  }
148 
149  $file = $this->fss_storage->getInfoDirectory() . '/' . $this->getFileId();
150  if (!file_exists($file)) {
151  $file = $this->fss_storage->getInfoDirectory() . '/' . $this->getFileId() . '.sec';
152  }
153  if (file_exists($file)) {
154  return $file;
155  }
156  return '';
157  }
158 
159  public function getInfoDirectory(): string
160  {
161  if (is_object($this->fss_storage)) {
162  return $this->fss_storage->getInfoDirectory();
163  }
164  return '';
165  }
166 
167  public function validate(): bool
168  {
169  switch ($this->getErrorCode()) {
170  case UPLOAD_ERR_INI_SIZE:
171  $this->error->appendMessage($this->lng->txt('file_upload_ini_size'));
172  return false;
173  case UPLOAD_ERR_FORM_SIZE:
174  $this->error->appendMessage($this->lng->txt('file_upload_form_size'));
175  return false;
176 
177  case UPLOAD_ERR_PARTIAL:
178  $this->error->appendMessage($this->lng->txt('file_upload_only_partial'));
179  return false;
180 
181  case UPLOAD_ERR_NO_TMP_DIR:
182  $this->error->appendMessage($this->lng->txt('file_upload_no_tmp_dir'));
183  return false;
184 
185  case UPLOAD_ERR_NO_FILE:
186  return false;
187 
188  case UPLOAD_ERR_OK:
189  default:
190  return true;
191  }
192  }
193 
194  public function create(bool $a_upload = true): bool
195  {
196  if ($this->getErrorCode() != 0) {
197  return false;
198  }
199  $next_id = $this->db->nextId('crs_file');
200  $query = "INSERT INTO crs_file (file_id,course_id,file_name,file_size,file_type) " .
201  "VALUES( " .
202  $this->db->quote($next_id, 'integer') . ", " .
203  $this->db->quote($this->getCourseId(), 'integer') . ", " .
204  $this->db->quote($this->getFileName(), 'text') . ", " .
205  $this->db->quote($this->getFileSize(), 'integer') . ", " .
206  $this->db->quote($this->getFileType(), 'text') . " " .
207  ")";
208  $res = $this->db->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  // now create file
217  $this->getTemporaryName(),
218  $this->getFileName(),
219  $this->fss_storage->getInfoDirectory() . '/' . $this->getFileId()
220  );
221  }
222  return true;
223  }
224 
225  public function delete(): void
226  {
227  // Delete db entry
228  $query = "DELETE FROM crs_file " .
229  "WHERE file_id = " . $this->db->quote($this->getFileId(), 'integer') . "";
230  $res = $this->db->manipulate($query);
231 
232  // Delete file
233  if (file_exists($this->getAbsolutePath())) {
234  unlink($this->getAbsolutePath());
235  }
236  }
237 
238  public static function _deleteByCourse(int $a_course_id): void
239  {
240  global $DIC;
241 
242  $ilDB = $DIC['ilDB'];
243 
244  // delete all course ids and delete assigned files
245  $query = "DELETE FROM crs_file " .
246  "WHERE course_id = " . $ilDB->quote($a_course_id, 'integer') . "";
247  $res = $ilDB->manipulate($query);
248  }
249 
254  public static function _readFilesByCourse(int $a_course_id): array
255  {
256  global $DIC;
257 
258  $ilDB = $DIC->database();
259  $query = "SELECT * FROM crs_file " .
260  "WHERE course_id = " . $ilDB->quote($a_course_id, 'integer') . "";
261 
262  $res = $ilDB->query($query);
263  $files = [];
264  while ($row = $ilDB->fetchObject($res)) {
265  $files[] = new ilCourseFile((int) $row->file_id);
266  }
267  return $files;
268  }
269 
270  public function __read(): void
271  {
272  if (!$this->file_id) {
273  return;
274  }
275 
276  // read file data
277  $query = "SELECT * FROM crs_file WHERE file_id = " . $this->db->quote($this->file_id, 'integer');
278  $res = $this->db->query($query);
279  while ($row = $this->db->fetchObject($res)) {
280  $this->setFileName((string) $row->file_name);
281  $this->setFileSize((int) $row->file_size);
282  $this->setFileType((string) $row->file_type);
283  $this->setCourseId((int) $row->course_id);
284  }
285  $this->fss_storage = new ilFSStorageCourse($this->getCourseId());
286  }
287 }
$res
Definition: ltiservices.php:69
ilDBInterface $db
ilFSStorageCourse $fss_storage
setTemporaryName(string $a_name)
setErrorCode(int $a_code)
setFileName(string $a_name)
setFileType(string $a_type)
static _readFilesByCourse(int $a_course_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setCourseId(int $a_course_id)
global $DIC
Definition: feed.php:28
create(bool $a_upload=true)
static _deleteByCourse(int $a_course_id)
setFileId(int $a_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(int $a_file_id=0)
setFileSize(int $a_size)
$query
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
ilErrorHandling $error
static _cloneFiles(int $a_source_id, int $a_target_id)
Error Handling & global info handling uses PEAR error class.
$source
Definition: metadata.php:93