ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilFSStorageExercise.php
Go to the documentation of this file.
1 <?php
2 
23 {
24  protected string $relative_submission_path;
25  protected \ILIAS\FileUpload\FileUpload $upload;
26  protected int $ass_id;
27  protected string $submission_path;
28  protected string $tmp_path;
29  protected string $feedb_path;
30  protected string $multi_feedback_upload_path;
31  protected string $peer_review_upload_path;
32 
33  public function __construct(
34  int $a_container_id = 0,
35  int $a_ass_id = 0
36  ) {
37  global $DIC;
38 
39  $this->ass_id = $a_ass_id;
40  parent::__construct(self::STORAGE_DATA, true, $a_container_id);
41  $this->upload = $DIC->upload();
42  }
43 
44  protected function getRelativeSubmissionPath(): string
45  {
47  }
48 
52  public function init(): bool
53  {
54  if (parent::init()) {
55  if ($this->ass_id > 0) {
56  $this->submission_path = $this->getAbsolutePath() . "/subm_" . $this->ass_id;
57  $this->relative_submission_path = $this->path . "/subm_" . $this->ass_id;
58  ;
59  $this->tmp_path = $this->getAbsolutePath() . "/tmp_" . $this->ass_id;
60  $this->feedb_path = $this->getAbsolutePath() . "/feedb_" . $this->ass_id;
61  $this->multi_feedback_upload_path = $this->getAbsolutePath() . "/mfb_up_" . $this->ass_id;
62  $this->peer_review_upload_path = $this->getAbsolutePath() . "/peer_up_" . $this->ass_id;
63  $this->path .= "/ass_" . $this->ass_id;
64  }
65  } else {
66  return false;
67  }
68  return true;
69  }
70 
71  protected function getPathPostfix(): string
72  {
73  return 'exc';
74  }
75 
76  protected function getPathPrefix(): string
77  {
78  return 'ilExercise';
79  }
80 
81  public function getAbsoluteSubmissionPath(): string
82  {
84  }
85 
86  public function getTempPath(): string
87  {
88  return $this->tmp_path;
89  }
90 
91  public function getFeedbackPath(
92  string $a_user_id
93  ): string {
94  $path = $this->feedb_path . "/" . $a_user_id;
95  if (!file_exists($path)) {
97  }
98  return $path;
99  }
100 
101  public function getGlobalFeedbackPath(): string
102  {
103  $path = $this->feedb_path . "/0";
104  if (!file_exists($path)) {
106  }
107  return $path;
108  }
109 
114  public function getMultiFeedbackUploadPath(
115  int $a_user_id
116  ): string {
117  $path = $this->multi_feedback_upload_path . "/" . $a_user_id;
118  if (!file_exists($path)) {
120  }
121  return $path;
122  }
123 
128  public function getPeerReviewUploadPath(
129  int $a_peer_id,
130  int $a_giver_id,
131  ?int $a_crit_id = null
132  ): string {
133  $path = $this->peer_review_upload_path . "/" . $a_peer_id . "/" . $a_giver_id . "/";
134 
135  if ((int) $a_crit_id !== 0) {
136  $path .= (int) $a_crit_id . "/";
137  }
138  if (!file_exists($path)) {
140  }
141  return $path;
142  }
143 
147  public function create(): void
148  {
149  parent::create();
150  if (!file_exists($this->submission_path)) {
151  ilFileUtils::makeDirParents($this->submission_path);
152  }
153  if (!file_exists($this->tmp_path)) {
154  ilFileUtils::makeDirParents($this->tmp_path);
155  }
156  if (!file_exists($this->feedb_path)) {
157  ilFileUtils::makeDirParents($this->feedb_path);
158  }
159  }
160 
161  public function getFiles(): array
162  {
163  $files = array();
164  if (!is_dir($this->path)) {
165  return $files;
166  }
167 
168  $dp = opendir($this->path);
169  while ($file = readdir($dp)) {
170  if (!is_dir($this->path . '/' . $file)) {
171  $files[] = array(
172  'name' => $file,
173  'size' => filesize($this->path . '/' . $file),
174  'ctime' => filectime($this->path . '/' . $file),
175  'fullpath' => $this->path . '/' . $file);
176  }
177  }
178  closedir($dp);
179  return ilArrayUtil::sortArray($files, "name", "asc");
180  }
181 
182 
186 
197  public function uploadFile(
198  array $a_http_post_file,
199  int $user_id,
200  bool $is_unziped = false
201  ): ?array {
202  $this->create();
203  // TODO:
204  // CHECK UPLOAD LIMIT
205 
206  //
207  $result = null;
208  if (isset($a_http_post_file) && $a_http_post_file['size']) {
209  $filename = $a_http_post_file['name'];
210 
212 
213  if (!is_dir($savepath = $this->getAbsoluteSubmissionPath())) {
214  ilFileUtils::makeDir($savepath);
215  }
216  $savepath .= '/' . $user_id;
217  if (!is_dir($savepath)) {
218  ilFileUtils::makeDir($savepath);
219  }
220 
221  // CHECK IF FILE PATH EXISTS
222  if (!is_dir($savepath)) {
223  ilFileUtils::makeDir($savepath);
224  }
225  $now = getdate();
226  $prefix = sprintf(
227  "%04d%02d%02d%02d%02d%02d",
228  $now["year"],
229  $now["mon"],
230  $now["mday"],
231  $now["hours"],
232  $now["minutes"],
233  $now["seconds"]
234  );
235 
236  if (!$is_unziped) {
238  $a_http_post_file["tmp_name"],
239  $prefix . "_" . $filename,
240  $savepath . "/" . $prefix . "_" . $filename
241  );
242  } else {
244  $a_http_post_file['tmp_name'],
245  $savepath . "/" . $prefix . "_" . $filename
246  );
247  }
248 
249  if (is_file($savepath . "/" . $prefix . "_" . $filename)) {
250  $result = array(
251  "filename" => $prefix . "_" . $filename,
252  "fullname" => $savepath . "/" . $prefix . "_" . $filename,
253  "mimetype" => ilObjMediaObject::getMimeType($savepath . "/" . $prefix . "_" . $filename)
254  );
255  }
256  }
257  return $result;
258  }
259 
260  protected function getStorageFilename(string $filename): string
261  {
262  global $DIC;
263 
264  $filename = ilFileUtils::getValidFilename($filename);
265  // replace whitespaces with underscores
266  $filename = preg_replace("/\s/", "_", $filename);
267  $filename = (new \ilFileServicesPolicy($DIC->fileServiceSettings()))->ascii($filename);
268  // remove all special characters
269  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
270  return $filename;
271  }
272 
282  public function addFileUpload(
283  \ILIAS\FileUpload\DTO\UploadResult $result,
284  int $user_id
285  ): ?array {
286  $this->create();
287 
288  $filename = $result->getName();
290 
291  $savepath = $this->getRelativeSubmissionPath();
292  $savepath .= '/' . $user_id;
293 
294  $now = getdate();
295  $prefix = sprintf(
296  "%04d%02d%02d%02d%02d%02d",
297  $now["year"],
298  $now["mon"],
299  $now["mday"],
300  $now["hours"],
301  $now["minutes"],
302  $now["seconds"]
303  );
304 
305  $this->upload->moveOneFileTo(
306  $result,
307  $savepath,
308  \ILIAS\FileUpload\Location::STORAGE,
309  $prefix . "_" . $filename,
310  true
311  );
312 
313  $result = array(
314  "filename" => $prefix . "_" . $filename,
315  "fullname" => $savepath . "/" . $prefix . "_" . $filename,
316  "mimetype" => $result->getMimeType()
317  );
318 
319  return $result;
320  }
321 
325  public function getFeedbackFiles(
326  string $a_user_id
327  ): array {
328  $files = array();
329 
330  if ($a_user_id === "t") { // team assignment without team, see #36253
331  return[];
332  }
333 
334  $dir = $this->getFeedbackPath($a_user_id);
335  if (is_dir($dir)) {
336  $dp = opendir($dir);
337  while ($file = readdir($dp)) {
338  if (!is_dir($this->path . '/' . $file) && substr($file, 0, 1) != ".") {
339  $files[] = $file;
340  }
341  }
342  }
343  return $files;
344  }
345 
346  public function countFeedbackFiles(
347  string $a_user_id
348  ): int {
349  $fbf = $this->getFeedbackFiles($a_user_id);
350  return count($fbf);
351  }
352 
353  public function getAssignmentFilePath(string $a_file): string
354  {
355  return $this->getAbsolutePath() . "/" . $a_file;
356  }
357 
358  public function getFeedbackFilePath(
359  string $a_user_id,
360  string $a_file
361  ): string {
362  $dir = $this->getFeedbackPath($a_user_id);
363  return $dir . "/" . $a_file;
364  }
365 
369  public function uploadAssignmentFiles(
370  array $a_files
371  ): void {
372  if (is_array($a_files["name"])) {
373  foreach ($a_files["name"] as $k => $name) {
374  if ($name != "") {
375  $tmp_name = $a_files["tmp_name"][$k];
377  $tmp_name,
378  basename($name),
379  $this->getAbsolutePath() . DIRECTORY_SEPARATOR . basename($name),
380  false
381  );
382  }
383  }
384  }
385  }
386 }
getFeedbackPath(string $a_user_id)
getStorageFilename(string $filename)
init()
Append ass_<ass_id> to path (assignment id)
addFileUpload(\ILIAS\FileUpload\DTO\UploadResult $result, int $user_id)
store delivered file in filesystem
getMultiFeedbackUploadPath(int $a_user_id)
Get multi feedback upload path (each uploader handled in a separate path)
Class ChatMainBarProvider .
getFeedbackFilePath(string $a_user_id, string $a_file)
static getValidFilename(string $a_filename)
getPeerReviewUploadPath(int $a_peer_id, int $a_giver_id, ?int $a_crit_id=null)
Get pear review upload path (each peer handled in a separate path)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
global $DIC
Definition: feed.php:28
countFeedbackFiles(string $a_user_id)
__construct(VocabulariesInterface $vocabularies)
static getMimeType(string $a_file, bool $a_external=false)
get mime type for file
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
$filename
Definition: buildRTE.php:78
getAbsolutePath()
Calculates the full path on the filesystem.
__construct(int $a_container_id=0, int $a_ass_id=0)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static rename(string $a_source, string $a_target)
getFeedbackFiles(string $a_user_id)
uploadFile(array $a_http_post_file, int $user_id, bool $is_unziped=false)
store delivered file in filesystem
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
ILIAS FileUpload FileUpload $upload
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)