ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilFSStorageExercise.php
Go to the documentation of this file.
1 <?php
2 
23 {
24  protected int $ass_id;
25  protected string $submission_path;
26  protected string $tmp_path;
27  protected string $feedb_path;
28  protected string $multi_feedback_upload_path;
29  protected string $peer_review_upload_path;
30 
31  public function __construct(
32  int $a_container_id = 0,
33  int $a_ass_id = 0
34  ) {
35  $this->ass_id = $a_ass_id;
36  parent::__construct(self::STORAGE_DATA, true, $a_container_id);
37  }
38 
42  public function init(): bool
43  {
44  if (parent::init()) {
45  if ($this->ass_id > 0) {
46  $this->submission_path = $this->getAbsolutePath() . "/subm_" . $this->ass_id;
47  $this->tmp_path = $this->getAbsolutePath() . "/tmp_" . $this->ass_id;
48  $this->feedb_path = $this->getAbsolutePath() . "/feedb_" . $this->ass_id;
49  $this->multi_feedback_upload_path = $this->getAbsolutePath() . "/mfb_up_" . $this->ass_id;
50  $this->peer_review_upload_path = $this->getAbsolutePath() . "/peer_up_" . $this->ass_id;
51  $this->path .= "/ass_" . $this->ass_id;
52  }
53  } else {
54  return false;
55  }
56  return true;
57  }
58 
59  protected function getPathPostfix(): string
60  {
61  return 'exc';
62  }
63 
64  protected function getPathPrefix(): string
65  {
66  return 'ilExercise';
67  }
68 
69  public function getAbsoluteSubmissionPath(): string
70  {
72  }
73 
74  public function getTempPath(): string
75  {
76  return $this->tmp_path;
77  }
78 
79  public function getFeedbackPath(
80  string $a_user_id
81  ): string {
82  $path = $this->feedb_path . "/" . $a_user_id;
83  if (!file_exists($path)) {
85  }
86  return $path;
87  }
88 
89  public function getGlobalFeedbackPath(): string
90  {
91  $path = $this->feedb_path . "/0";
92  if (!file_exists($path)) {
94  }
95  return $path;
96  }
97 
102  public function getMultiFeedbackUploadPath(
103  int $a_user_id
104  ): string {
105  $path = $this->multi_feedback_upload_path . "/" . $a_user_id;
106  if (!file_exists($path)) {
108  }
109  return $path;
110  }
111 
116  public function getPeerReviewUploadPath(
117  int $a_peer_id,
118  int $a_giver_id,
119  ?int $a_crit_id = null
120  ): string {
121  $path = $this->peer_review_upload_path . "/" . $a_peer_id . "/" . $a_giver_id . "/";
122 
123  if ((int) $a_crit_id !== 0) {
124  $path .= (int) $a_crit_id . "/";
125  }
126  if (!file_exists($path)) {
128  }
129  return $path;
130  }
131 
135  public function create(): void
136  {
137  parent::create();
138  if (!file_exists($this->submission_path)) {
139  ilFileUtils::makeDirParents($this->submission_path);
140  }
141  if (!file_exists($this->tmp_path)) {
142  ilFileUtils::makeDirParents($this->tmp_path);
143  }
144  if (!file_exists($this->feedb_path)) {
145  ilFileUtils::makeDirParents($this->feedb_path);
146  }
147  }
148 
149  public function getFiles(): array
150  {
151  $files = array();
152  if (!is_dir($this->path)) {
153  return $files;
154  }
155 
156  $dp = opendir($this->path);
157  while ($file = readdir($dp)) {
158  if (!is_dir($this->path . '/' . $file)) {
159  $files[] = array(
160  'name' => $file,
161  'size' => filesize($this->path . '/' . $file),
162  'ctime' => filectime($this->path . '/' . $file),
163  'fullpath' => $this->path . '/' . $file);
164  }
165  }
166  closedir($dp);
167  return ilArrayUtil::sortArray($files, "name", "asc");
168  }
169 
170 
174 
184  public function uploadFile(
185  array $a_http_post_file,
186  int $user_id,
187  bool $is_unziped = false
188  ): ?array {
189  $this->create();
190  // TODO:
191  // CHECK UPLOAD LIMIT
192 
193  //
194  $result = null;
195  if (isset($a_http_post_file) && $a_http_post_file['size']) {
196  $filename = $a_http_post_file['name'];
197 
199  // replace whitespaces with underscores
200  $filename = preg_replace("/\s/", "_", $filename);
201  // remove all special characters
202  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
203 
204  if (!is_dir($savepath = $this->getAbsoluteSubmissionPath())) {
205  ilFileUtils::makeDir($savepath);
206  }
207  $savepath .= '/' . $user_id;
208  if (!is_dir($savepath)) {
209  ilFileUtils::makeDir($savepath);
210  }
211 
212  // CHECK IF FILE PATH EXISTS
213  if (!is_dir($savepath)) {
214  ilFileUtils::makeDir($savepath);
215  }
216  $now = getdate();
217  $prefix = sprintf(
218  "%04d%02d%02d%02d%02d%02d",
219  $now["year"],
220  $now["mon"],
221  $now["mday"],
222  $now["hours"],
223  $now["minutes"],
224  $now["seconds"]
225  );
226 
227  if (!$is_unziped) {
229  $a_http_post_file["tmp_name"],
230  $prefix . "_" . $filename,
231  $savepath . "/" . $prefix . "_" . $filename
232  );
233  } else {
235  $a_http_post_file['tmp_name'],
236  $savepath . "/" . $prefix . "_" . $filename
237  );
238  }
239 
240  if (is_file($savepath . "/" . $prefix . "_" . $filename)) {
241  $result = array(
242  "filename" => $prefix . "_" . $filename,
243  "fullname" => $savepath . "/" . $prefix . "_" . $filename,
244  "mimetype" => ilObjMediaObject::getMimeType($savepath . "/" . $prefix . "_" . $filename)
245  );
246  }
247  }
248  return $result;
249  }
250 
254  public function getFeedbackFiles(
255  string $a_user_id
256  ): array {
257  $files = array();
258 
259  if ($a_user_id === "t") { // team assignment without team, see #36253
260  return[];
261  }
262 
263  $dir = $this->getFeedbackPath($a_user_id);
264  if (is_dir($dir)) {
265  $dp = opendir($dir);
266  while ($file = readdir($dp)) {
267  if (!is_dir($this->path . '/' . $file) && substr($file, 0, 1) != ".") {
268  $files[] = $file;
269  }
270  }
271  }
272  return $files;
273  }
274 
275  public function countFeedbackFiles(
276  string $a_user_id
277  ): int {
278  $fbf = $this->getFeedbackFiles($a_user_id);
279  return count($fbf);
280  }
281 
282  public function getAssignmentFilePath(string $a_file): string
283  {
284  return $this->getAbsolutePath() . "/" . $a_file;
285  }
286 
287  public function getFeedbackFilePath(
288  string $a_user_id,
289  string $a_file
290  ): string {
291  $dir = $this->getFeedbackPath($a_user_id);
292  return $dir . "/" . $a_file;
293  }
294 
298  public function uploadAssignmentFiles(
299  array $a_files
300  ): void {
301  if (is_array($a_files["name"])) {
302  foreach ($a_files["name"] as $k => $name) {
303  if ($name != "") {
304  $tmp_name = $a_files["tmp_name"][$k];
306  $tmp_name,
307  basename($name),
308  $this->getAbsolutePath() . DIRECTORY_SEPARATOR . basename($name),
309  false
310  );
311  }
312  }
313  }
314  }
315 }
getFeedbackPath(string $a_user_id)
init()
Append ass_<ass_id> to path (assignment id)
getMultiFeedbackUploadPath(int $a_user_id)
Get multi feedback upload path (each uploader handled in a separate path)
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.
if($format !==null) $name
Definition: metadata.php:247
countFeedbackFiles(string $a_user_id)
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...
__construct(Container $dic, ilPlugin $plugin)
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 ...
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)