ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.SubmissionRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29
31{
32 protected const TABLE_NAME = "exc_returned";
33 protected $log;
34 protected \ilDBInterface $db;
35
36 public function __construct(
37 protected IRSSWrapper $irss,
38 protected InternalDataService $data,
39 ?\ilDBInterface $db = null
40 ) {
41 global $DIC;
42 $this->log = $DIC->logger()->exc();
43 $this->db = (is_null($db))
44 ? $DIC->database()
45 : $db;
46 }
47
48 public function getAllEntriesOfAssignment(int $ass_id): array
49 {
50 $recs = [];
51 $set = $this->db->queryF(
52 "SELECT * FROM exc_returned " .
53 " WHERE ass_id = %s ",
54 ["integer"],
55 [$ass_id]
56 );
57 while ($rec = $this->db->fetchAssoc($set)) {
58 $recs[] = $rec;
59 }
60 return $recs;
61 }
62
66 public function getSubmissionsOfTeam(
67 int $ass_id,
68 bool $type_uses_uploads,
69 bool $type_uses_print_versions,
70 int $team_id,
71 ?array $submit_ids = null,
72 bool $only_valid = false,
73 ?string $min_timestamp = null,
74 bool $print_versions = false
75 ): \Generator {
76 $where = " team_id = " . $this->db->quote($team_id, "integer") . " ";
77 yield from $this->getSubmissions(
78 $where,
79 $ass_id,
80 $type_uses_uploads,
81 $type_uses_print_versions,
82 $submit_ids,
83 $only_valid,
84 $min_timestamp,
85 $print_versions
86 );
87 }
88
92 public function getSubmissionsOfUsers(
93 int $ass_id,
94 bool $type_uses_uploads,
95 bool $type_uses_print_versions,
96 array $user_ids,
97 ?array $submit_ids = null,
98 bool $only_valid = false,
99 ?string $min_timestamp = null,
100 bool $print_versions = false
101 ): \Generator {
102 $where = " " . $this->db->in("user_id", $user_ids, false, "integer") . " ";
103 yield from $this->getSubmissions(
104 $where,
105 $ass_id,
106 $type_uses_uploads,
107 $type_uses_print_versions,
108 $submit_ids,
109 $only_valid,
110 $min_timestamp,
111 $print_versions
112 );
113 }
114
118 protected function getSubmissions(
119 string $where,
120 int $ass_id,
121 bool $type_uses_uploads,
122 bool $type_uses_print_versions,
123 ?array $submit_ids = null,
124 bool $only_valid = false,
125 ?string $min_timestamp = null,
126 bool $print_versions = false
127 ): \Generator {
128 $sql = "SELECT * FROM exc_returned" .
129 " WHERE ass_id = " . $this->db->quote($ass_id, "integer");
130 $sql .= " AND " . $where;
131
132 if ($submit_ids) {
133 $sql .= " AND " . $this->db->in("returned_id", $submit_ids, false, "integer");
134 }
135
136 if ($min_timestamp) {
137 $sql .= " AND ts > " . $this->db->quote($min_timestamp, "timestamp");
138 }
139
140 $result = $this->db->query($sql);
141 $delivered_files = array();
142 while ($row = $this->db->fetchAssoc($result)) {
143 // blog/portfolio/text submissions
144 if ($only_valid &&
145 !$row["filename"] &&
146 !(trim((string) $row["atext"]))) {
147 continue;
148 }
149
150 //$row["owner_id"] = $row["user_id"];
151 $row["timestamp"] = $row["ts"];
152 /*
153 $row["timestamp14"] = substr($row["ts"], 0, 4) .
154 substr($row["ts"], 5, 2) . substr($row["ts"], 8, 2) .
155 substr($row["ts"], 11, 2) . substr($row["ts"], 14, 2) .
156 substr($row["ts"], 17, 2);*/
157
158 $row["rid"] = (string) $row["rid"];
159
160 // see 22301, 22719
161 if ($row["rid"] !== "" || (!$type_uses_uploads)) {
162 $ok = true;
163
164 if ($type_uses_print_versions) {
165 $is_print_version = false;
166 if (str_ends_with($row["filetitle"], "print")) {
167 $is_print_version = true;
168 }
169 if (str_ends_with($row["filetitle"], "print.zip")) {
170 $is_print_version = true;
171 }
172 if ($is_print_version != $print_versions) {
173 $ok = false;
174 }
175 }
176 if ($ok) {
177 yield $this->getSubmissionFromRecord($row);
178 }
179 }
180 }
181 }
182
183 public function getById(
184 int $id
185 ): ?Submission {
186 $sql = "SELECT * FROM exc_returned" .
187 " WHERE returned_id = " . $this->db->quote($id, "integer");
188 $result = $this->db->query($sql);
189 if ($row = $this->db->fetchAssoc($result)) {
190 return $this->getSubmissionFromRecord($row);
191 }
192 return null;
193 }
194
195
196 public function getUserId(int $submission_id): int
197 {
198 $q = "SELECT user_id FROM exc_returned " .
199 " WHERE returned_id = " . $this->db->quote($submission_id, "integer");
200 $usr_set = $this->db->query($q);
201
202 $rec = $this->db->fetchAssoc($usr_set);
203 return (int) ($rec["user_id"] ?? 0);
204 }
205
206 public function getAllSubmissionIdsOfOwner(int $ass_id, int $user_id): array
207 {
208 $set = $this->db->queryF(
209 "SELECT returned_id FROM exc_returned " .
210 " WHERE ass_id = %s AND user_id = %s",
211 ["integer", "integer"],
213 );
214 $user_ids = [];
215 while ($rec = $this->db->fetchAssoc($set)) {
216 $user_ids[] = (int) $rec["returned_id"];
217 }
218 return $user_ids;
219 }
220
221
222 public function hasSubmissions(int $assignment_id): int
223 {
224 $query = "SELECT * FROM exc_returned " .
225 " WHERE ass_id = " . $this->db->quote($assignment_id, "integer") .
226 " AND (filename IS NOT NULL OR atext IS NOT NULL)" .
227 " AND ts IS NOT NULL";
228 $res = $this->db->query($query);
229 return $res->numRows();
230 }
231
232 // Update web_dir_access_time. It defines last HTML opening data.
233 public function updateWebDirAccessTime(int $assignment_id, int $member_id): void
234 {
235 $this->db->manipulate("UPDATE exc_returned " .
236 " SET web_dir_access_time = " . $this->db->quote(\ilUtil::now(), "timestamp") .
237 " WHERE ass_id = " . $this->db->quote($assignment_id, "integer") .
238 " AND user_id = " . $this->db->quote($member_id, "integer"));
239 }
240
247 public function getUserSubmissionState(int $user_id, array $assignment_ids): array
248 {
249 $db = $this->db;
250
251 $submitted = [];
252 foreach ($assignment_ids as $id) {
253 $submitted[(int) $id] = false;
254 }
255
256 $set = $db->queryF(
257 "SELECT ass_id FROM exc_returned " .
258 " WHERE " . $db->in("ass_id", $assignment_ids, false, "integer") .
259 " AND user_id = %s " .
260 " AND (filename IS NOT NULL OR atext IS NOT NULL)" .
261 " AND ts IS NOT NULL",
262 ["integer"],
263 [$user_id]
264 );
265 while ($rec = $db->fetchAssoc($set)) {
266 $submitted[(int) $rec["ass_id"]] = true;
267 }
268
269 $set = $db->queryF(
270 "SELECT ret.ass_id FROM exc_returned ret JOIN il_exc_team team " .
271 " ON (ret.team_id = team.id AND ret.ass_id = team.ass_id) " .
272 " WHERE " . $db->in("ret.ass_id", $assignment_ids, false, "integer") .
273 " AND team.user_id = %s " .
274 " AND (ret.filename IS NOT NULL OR ret.atext IS NOT NULL)" .
275 " AND ret.ts IS NOT NULL",
276 ["integer"],
277 [$user_id]
278 );
279 while ($rec = $db->fetchAssoc($set)) {
280 $submitted[(int) $rec["ass_id"]] = true;
281 }
282
283 return $submitted;
284 }
285
286 public function updateLate(
287 int $return_id,
288 bool $late
289 ): void {
290 $this->db->manipulate("UPDATE exc_returned" .
291 " SET late = " . $this->db->quote((int) $late, "integer") .
292 " WHERE returned_id = " . $this->db->quote($return_id, "integer"));
293 }
294
300 int $obj_id,
301 int $ass_id,
302 int $user_id = 0
303 ): int {
304 $db = $this->db;
305
306 $and = "";
307 if ($user_id > 0) {
308 $and = " AND user_id = " . $db->quote($user_id, "integer");
309 }
310
311 $set = $db->queryF(
312 "SELECT MAX(max_num) AS max" .
313 " FROM (SELECT COUNT(user_id) AS max_num FROM exc_returned" .
314 " WHERE obj_id= %s AND ass_id= %s " . $and . " AND mimetype IS NOT NULL" .
315 " GROUP BY user_id) AS COUNTS",
316 ["integer", "integer"],
317 [$obj_id, $ass_id]
318 );
319 $row = $db->fetchAssoc($set);
320 return (int) $row['max'];
321 }
322
327 public function getUsersWithSubmission(int $ass_id): array
328 {
329 $db = $this->db;
330 $user_ids = [];
331 $set = $db->query("SELECT DISTINCT(user_id)" .
332 " FROM exc_returned" .
333 " WHERE ass_id = " . $db->quote($ass_id, "integer") .
334 " AND (filename IS NOT NULL OR atext IS NOT NULL)");
335 while ($row = $db->fetchAssoc($set)) {
336 $user_ids[] = (int) $row["user_id"];
337 }
338 return $user_ids;
339 }
340
341 public function addLocalFile(
342 int $obj_id,
343 int $ass_id,
344 int $user_id,
345 int $team_id,
346 string $file,
347 string $filename,
348 bool $is_late,
349 ResourceStakeholder $stakeholder
350 ): bool {
351 $db = $this->db;
352 $rid = $this->irss->importLocalFile(
353 $file,
354 $filename,
355 $stakeholder
356 );
358
359 if ($rid !== "") {
360 $info = $this->irss->getResourceInfo($rid);
361 $next_id = $db->nextId("exc_returned");
362 $query = sprintf(
363 "INSERT INTO exc_returned " .
364 "(returned_id, obj_id, user_id, filename, filetitle, mimetype, ts, ass_id, late, team_id, rid) " .
365 "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
366 $db->quote($next_id, "integer"),
367 $db->quote($obj_id, "integer"),
368 $db->quote($user_id, "integer"),
369 $db->quote($filename, "text"),
370 $db->quote($filename, "text"),
371 $db->quote($info->getMimeType(), "text"),
372 $db->quote(\ilUtil::now(), "timestamp"),
373 $db->quote($ass_id, "integer"),
374 $db->quote($is_late, "integer"),
375 $db->quote($team_id, "integer"),
376 $db->quote($rid, "text")
377 );
378 $db->manipulate($query);
379 return true;
380 }
381 return false;
382 }
383
384 protected function getSubmissionFromRecord(array $rec): Submission
385 {
386 return $this->data->submission(
387 (int) $rec["returned_id"],
388 (int) $rec["ass_id"],
389 (int) $rec["user_id"],
390 (int) $rec["team_id"],
391 (string) $rec["filetitle"],
392 (string) $rec["atext"],
393 (string) $rec["rid"],
394 (string) $rec["mimetype"],
395 (string) $rec["ts"],
396 (bool) $rec["late"]
397 );
398 }
399
400 public function addUpload(
401 int $obj_id,
402 int $ass_id,
403 int $user_id,
404 int $team_id,
405 UploadResult $result,
406 string $filename,
407 bool $is_late,
408 ResourceStakeholder $stakeholder
409 ): bool {
410 $db = $this->db;
411 $rid = $this->irss->importFileFromUploadResult(
412 $result,
413 $stakeholder
414 );
416
417 if ($rid !== "") {
418 $info = $this->irss->getResourceInfo($rid);
419 $next_id = $db->nextId("exc_returned");
420 $query = sprintf(
421 "INSERT INTO exc_returned " .
422 "(returned_id, obj_id, user_id, filename, filetitle, mimetype, ts, ass_id, late, team_id, rid) " .
423 "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
424 $db->quote($next_id, "integer"),
425 $db->quote($obj_id, "integer"),
426 $db->quote($user_id, "integer"),
427 $db->quote($filename, "text"),
428 $db->quote($filename, "text"),
429 $db->quote($info->getMimeType(), "text"),
430 $db->quote(\ilUtil::now(), "timestamp"),
431 $db->quote($ass_id, "integer"),
432 $db->quote($is_late, "integer"),
433 $db->quote($team_id, "integer"),
434 $db->quote($rid, "text")
435 );
436 $db->manipulate($query);
437 return true;
438 }
439 return false;
440 }
441
445 public function addZipUpload(
446 int $obj_id,
447 int $ass_id,
448 int $user_id,
449 int $team_id,
450 UploadResult $result,
451 bool $is_late,
452 ResourceStakeholder $stakeholder,
453 int $remaining_allowed
454 ): array {
455 global $DIC;
456
457 $db = $this->db;
458 $filenames = [];
459 $this->log->debug("5");
460 $rid = $this->irss->importFileFromUploadResult(
461 $result,
462 $stakeholder
463 );
464 $this->log->debug("6");
465 $stream = $this->irss->stream($rid);
466
467 if ($remaining_allowed !== -1 &&
468 $remaining_allowed < $DIC->archives()->unzip($stream)->getAmountOfFiles()) {
469 throw new ilExcTooManyFilesSubmittedException("Too many files submitted.");
470 }
471
472 foreach ($DIC->archives()->unzip($stream)->getFileStreams() as $stream) {
473 $this->log->debug("7");
474 $rid = $this->irss->importStream(
475 $stream,
476 $stakeholder
477 );
478 $info = $this->irss->getResourceInfo($rid);
479 $filename = $info->getTitle();
480 $this->log->debug("8");
481 if ($rid !== "") {
482 $this->log->debug("9");
483 $info = $this->irss->getResourceInfo($rid);
484 $next_id = $db->nextId("exc_returned");
485 $query = sprintf(
486 "INSERT INTO exc_returned " .
487 "(returned_id, obj_id, user_id, filename, filetitle, mimetype, ts, ass_id, late, team_id, rid) " .
488 "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
489 $db->quote($next_id, "integer"),
490 $db->quote($obj_id, "integer"),
491 $db->quote($user_id, "integer"),
492 $db->quote($filename, "text"),
493 $db->quote($filename, "text"),
494 $db->quote($info->getMimeType(), "text"),
495 $db->quote(\ilUtil::now(), "timestamp"),
496 $db->quote($ass_id, "integer"),
497 $db->quote($is_late, "integer"),
498 $db->quote($team_id, "integer"),
499 $db->quote($rid, "text")
500 );
501 $db->manipulate($query);
502 $filenames[] = $filename;
503 }
504 }
505 return $filenames;
506 }
507
508 public function delete(
509 int $id,
510 ResourceStakeholder $stakeholder
511 ): void {
512 $set = $this->db->queryF(
513 "SELECT rid FROM exc_returned " .
514 " WHERE returned_id = %s ",
515 ["integer"],
516 [$id]
517 );
518 if ($rec = $this->db->fetchAssoc($set)) {
519 $rid = (string) ($rec["rid"] ?? "");
520 if ($rid !== "") {
521 $this->irss->deleteResource(
522 $rid,
523 $stakeholder
524 );
525 }
526 $this->db->manipulateF(
527 "DELETE FROM exc_returned WHERE " .
528 " returned_id = %s",
529 ["integer"],
530 [$id]
531 );
532 }
533 }
534
535 public function deliverFile(
536 int $ass_id,
537 int $user_id,
538 string $rid,
539 string $filetitle = ""
540 ): void {
541 if ($filetitle !== "") {
542 $this->irss->renameCurrentRevision($rid, $filetitle);
543 }
544 $this->irss->deliverFile($rid);
545 }
546
547 public function getStream(
548 int $ass_id,
549 string $rid
550 ): ?FileStream {
551 return $this->irss->stream($rid);
552 }
553
554}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$filename
Definition: buildRTE.php:78
Internal factory for data objects.
getSubmissionsOfTeam(int $ass_id, bool $type_uses_uploads, bool $type_uses_print_versions, int $team_id, ?array $submit_ids=null, bool $only_valid=false, ?string $min_timestamp=null, bool $print_versions=false)
getSubmissions(string $where, int $ass_id, bool $type_uses_uploads, bool $type_uses_print_versions, ?array $submit_ids=null, bool $only_valid=false, ?string $min_timestamp=null, bool $print_versions=false)
deliverFile(int $ass_id, int $user_id, string $rid, string $filetitle="")
addUpload(int $obj_id, int $ass_id, int $user_id, int $team_id, UploadResult $result, string $filename, bool $is_late, ResourceStakeholder $stakeholder)
__construct(protected IRSSWrapper $irss, protected InternalDataService $data, ?\ilDBInterface $db=null)
updateWebDirAccessTime(int $assignment_id, int $member_id)
getMaxAmountOfSubmittedFiles(int $obj_id, int $ass_id, int $user_id=0)
Get the number of max amount of files submitted by a single user in the assignment.
addZipUpload(int $obj_id, int $ass_id, int $user_id, int $team_id, UploadResult $result, bool $is_late, ResourceStakeholder $stakeholder, int $remaining_allowed)
getUsersWithSubmission(int $ass_id)
Get all user ids, that have submitted something.
addLocalFile(int $obj_id, int $ass_id, int $user_id, int $team_id, string $file, string $filename, bool $is_late, ResourceStakeholder $stakeholder)
getSubmissionsOfUsers(int $ass_id, bool $type_uses_uploads, bool $type_uses_print_versions, array $user_ids, ?array $submit_ids=null, bool $only_valid=false, ?string $min_timestamp=null, bool $print_versions=false)
getUserSubmissionState(int $user_id, array $assignment_ids)
Checks if a user has submitted anything for a number of assignments.
static getValidFilename(string $a_filename)
static now()
Return current timestamp in Y-m-d H:i:s format.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$info
Definition: entry_point.php:21
The base interface for all filesystem streams.
Definition: FileStream.php:32
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:23