17
17
use App \Model \Entity \SupplementaryExerciseFile ;
18
18
use App \Model \Entity \UploadedFile ;
19
19
use App \Model \Entity \AttachmentFile ;
20
+ use App \Model \Repository \Assignments ;
20
21
use App \Model \Repository \AttachmentFiles ;
21
22
use App \Model \Repository \Exercises ;
22
23
use App \Model \Entity \Exercise ;
@@ -37,6 +38,12 @@ class ExerciseFilesPresenter extends BasePresenter
37
38
*/
38
39
public $ exercises ;
39
40
41
+ /**
42
+ * @var Assignments
43
+ * @inject
44
+ */
45
+ public $ assignments ;
46
+
40
47
/**
41
48
* @var UploadedFiles
42
49
* @inject
@@ -345,7 +352,37 @@ public function actionDeleteAttachmentFile(string $id, string $fileId)
345
352
$ exercise ->removeAttachmentFile ($ file );
346
353
$ this ->exercises ->flush ();
347
354
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
+ }
349
386
350
387
$ this ->sendSuccessResponse ("OK " );
351
388
}
0 commit comments