Skip to content

Commit c7c46b2

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

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

app/V1Module/presenters/ExerciseFilesPresenter.php

+38-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use App\Model\Entity\SupplementaryExerciseFile;
1818
use App\Model\Entity\UploadedFile;
1919
use App\Model\Entity\AttachmentFile;
20+
use App\Model\Repository\Assignments;
2021
use App\Model\Repository\AttachmentFiles;
2122
use App\Model\Repository\Exercises;
2223
use App\Model\Entity\Exercise;
@@ -37,6 +38,12 @@ class ExerciseFilesPresenter extends BasePresenter
3738
*/
3839
public $exercises;
3940

41+
/**
42+
* @var Assignments
43+
* @inject
44+
*/
45+
public $assignments;
46+
4047
/**
4148
* @var UploadedFiles
4249
* @inject
@@ -345,7 +352,37 @@ public function actionDeleteAttachmentFile(string $id, string $fileId)
345352
$exercise->removeAttachmentFile($file);
346353
$this->exercises->flush();
347354

348-
$this->fileStorage->deleteAttachmentFile($file);
355+
$this->attachmentFiles->refresh($file);
356+
if ($file->getExercises()->isEmpty()) {
357+
// file has no attachments to exercises, let's check the assignments
358+
$isUsed = false;
359+
foreach ($file->getAssignments() as $assignment) {
360+
$group = $assignment->getGroup();
361+
if ($group && !$group->isArchived()) {
362+
$isUsed = true; // only non-archived assignments are considered relevant
363+
break;
364+
}
365+
}
366+
367+
if (!$isUsed) {
368+
$this->fileStorage->deleteAttachmentFile($file);
369+
370+
if ($file->getAssignments()->isEmpty()) {
371+
// only if no attachments exists (except for deleted ones)
372+
// remove all links to deleted entities and remove the file record
373+
foreach ($file->getExercisesAndIReallyMeanAllOkay() as $exercise) {
374+
$exercise->removeAttachmentFile($file);
375+
$this->exercises->persist($exercise, false);
376+
}
377+
foreach ($file->getAssignmentsAndIReallyMeanAllOkay() as $assignment) {
378+
$assignment->removeAttachmentFile($file);
379+
$this->assignments->persist($assignment, false);
380+
}
381+
382+
$this->attachmentFiles->remove($file);
383+
}
384+
}
385+
}
349386

350387
$this->sendSuccessResponse("OK");
351388
}

app/model/entity/AttachmentFile.php

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ function (Exercise $exercise) {
3232
);
3333
}
3434

35+
/**
36+
* @return Collection
37+
*/
38+
public function getExercisesAndIReallyMeanAllOkay()
39+
{
40+
return $this->exercises;
41+
}
42+
3543
/**
3644
* @ORM\ManyToMany(targetEntity="Assignment", mappedBy="attachmentFiles")
3745
*/
@@ -49,6 +57,13 @@ function (Assignment $assignment) {
4957
);
5058
}
5159

60+
/**
61+
* @return Collection
62+
*/
63+
public function getAssignmentsAndIReallyMeanAllOkay()
64+
{
65+
return $this->assignments;
66+
}
5267

5368
/**
5469
* AttachmentFile constructor.

0 commit comments

Comments
 (0)