@@ -76,9 +76,15 @@ private function __construct(
7676 $ this ->configurationType = $ exercise ->getConfigurationType ();
7777 $ this ->exerciseFiles = $ exercise ->getExerciseFiles ();
7878 $ this ->attachmentFiles = $ exercise ->getAttachmentFiles ();
79- $ this ->fileLinks = new ArrayCollection (); // TODO properly copy from exercise
79+ $ this ->fileLinks = new ArrayCollection ();
8080 $ this ->solutionFilesLimit = $ exercise ->getSolutionFilesLimit ();
8181 $ this ->solutionSizeLimit = $ exercise ->getSolutionSizeLimit ();
82+
83+ // copy file links from exercise
84+ foreach ($ exercise ->getFileLinks () as $ link ) {
85+ $ newLink = ExerciseFileLink::copyForAssignment ($ link , $ this );
86+ $ this ->fileLinks ->add ($ newLink );
87+ }
8288 }
8389
8490 public static function assignToGroup (
@@ -454,16 +460,43 @@ function ($key, ExerciseTest $test) use ($exercise) {
454460 public function areExerciseFilesInSync (): bool
455461 {
456462 $ exercise = $ this ->getExercise ();
457- return $ exercise
458- && $ this ->getExerciseFiles ()->count ()
459- === $ exercise ->getExerciseFiles ()->count ()
463+ return $ exercise && $ this ->getExerciseFiles ()->count () === $ exercise ->getExerciseFiles ()->count ()
460464 && $ this ->getExerciseFiles ()->forAll (
461465 function ($ key , ExerciseFile $ file ) use ($ exercise ) {
462466 return $ exercise ->getExerciseFiles ()->contains ($ file );
463467 }
464468 );
465469 }
466470
471+ public function areExerciseFileLinksInSync (): bool
472+ {
473+ $ exercise = $ this ->getExercise ();
474+ if (!$ exercise || $ this ->getFileLinks ()->count () !== $ exercise ->getFileLinks ()->count ()) {
475+ return false ;
476+ }
477+
478+ // build index where keys are exercise file IDs
479+ $ fileIndex = [];
480+ foreach ($ this ->getFileLinks () as $ link ) {
481+ $ fileIndex [$ link ->getExerciseFile ()->getId ()] = $ link ;
482+ }
483+
484+ // verify that all exercise links are present and has the same data
485+ foreach ($ exercise ->getFileLinks () as $ link ) {
486+ $ ourLink = $ fileIndex [$ link ->getExerciseFile ()->getId ()] ?? null ;
487+ if (
488+ $ ourLink === null
489+ || $ ourLink ->getKey () !== $ link ->getKey ()
490+ || $ ourLink ->getRequiredRole () !== $ link ->getRequiredRole ()
491+ || $ ourLink ->getSaveName () !== $ link ->getSaveName ()
492+ ) {
493+ return false ;
494+ }
495+ }
496+
497+ return true ;
498+ }
499+
467500 public function areAttachmentFilesInSync (): bool
468501 {
469502 $ exercise = $ this ->getExercise ();
@@ -537,6 +570,12 @@ public function syncWithExercise()
537570 $ this ->attachmentFiles ->add ($ file );
538571 }
539572
573+ $ this ->fileLinks ->clear ();
574+ foreach ($ exercise ->getFileLinks () as $ link ) {
575+ $ newLink = ExerciseFileLink::copyForAssignment ($ link , $ this );
576+ $ this ->fileLinks ->add ($ newLink );
577+ }
578+
540579 $ this ->runtimeEnvironments ->clear ();
541580 foreach ($ exercise ->getRuntimeEnvironments () as $ env ) {
542581 $ this ->runtimeEnvironments ->add ($ env );
0 commit comments