Skip to content

Commit 64edca8

Browse files
committed
Restricting rules when an attachment file can actually be deleted (correctly handling exercise cloning and shared links).
1 parent ead27f3 commit 64edca8

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

app/V1Module/presenters/ExerciseFilesPresenter.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,28 @@ public function actionDeleteAttachmentFile(string $id, string $fileId)
345345
$exercise->removeAttachmentFile($file);
346346
$this->exercises->flush();
347347

348-
$this->fileStorage->deleteAttachmentFile($file);
348+
$this->attachmentFiles->refresh($file);
349+
if ($file->getExercises()->isEmpty()) {
350+
// file has no attachments to exercises, let's check the assignments
351+
$isUsed = false;
352+
foreach ($file->getAssignments() as $assignment) {
353+
$group = $assignment->getGroup();
354+
if ($group && !$group->isArchived()) {
355+
$isUsed = true; // only non-archived assignments are considered relevant
356+
break;
357+
}
358+
}
359+
360+
if (!$isUsed) {
361+
$this->fileStorage->deleteAttachmentFile($file);
362+
if ($file->getAssignments()->isEmpty()) {
363+
// only if no attachments exists (at all)
364+
$file->detachAllConnections(); // remove lingering connections to deleted entities
365+
$this->attachmentFiles->flush($file);
366+
$this->attachmentFiles->remove($file);
367+
}
368+
}
369+
}
349370

350371
$this->sendSuccessResponse("OK");
351372
}

app/model/entity/AttachmentFile.php

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ function (Assignment $assignment) {
4949
);
5050
}
5151

52+
/**
53+
* Remove all links to exercises and assignments (even to deleted ones).
54+
*/
55+
public function detachAllConnections()
56+
{
57+
$this->exercises->clear();
58+
$this->assignments->clear();
59+
}
5260

5361
/**
5462
* AttachmentFile constructor.

0 commit comments

Comments
 (0)