Skip to content

Commit f3f4cb3

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

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

app/V1Module/presenters/ExerciseFilesPresenter.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,26 @@ 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+
$this->attachmentFiles->remove($file);
365+
}
366+
}
367+
}
349368

350369
$this->sendSuccessResponse("OK");
351370
}

0 commit comments

Comments
 (0)