Skip to content

Commit 2615ce5

Browse files
committed
Properly copying exercise file links when assignment is created or file is cloned.
1 parent 871e6a8 commit 2615ce5

3 files changed

Lines changed: 58 additions & 7 deletions

File tree

app/model/entity/Assignment.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

app/model/entity/Exercise.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public static function create(
219219

220220
public static function forkFrom(Exercise $exercise, User $user, Group $group): Exercise
221221
{
222-
return new self(
222+
$newExercise = new self(
223223
1,
224224
$exercise->difficulty,
225225
$exercise->localizedTexts,
@@ -242,7 +242,19 @@ public static function forkFrom(Exercise $exercise, User $user, Group $group): E
242242
$exercise->solutionSizeLimit
243243
);
244244

245-
// TODO handle file link duplication
245+
// handle file link duplication
246+
foreach ($exercise->fileLinks as $link) {
247+
$newLink = ExerciseFileLink::createForExercise(
248+
$link->getKey(),
249+
$link->getExerciseFile(),
250+
$newExercise,
251+
$link->getRequiredRole(),
252+
$link->getSaveName()
253+
);
254+
$newExercise->fileLinks->add($newLink);
255+
}
256+
257+
return $newExercise;
246258
}
247259

248260
public function setRuntimeEnvironments(Collection $runtimeEnvironments): void

tests/helpers/FileStorage.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ class TestFileStorage extends Tester\TestCase
866866
PresenterTestHelper::loginDefaultAdmin($this->container);
867867
$user = $this->users->getByEmail(PresenterTestHelper::ADMIN_LOGIN);
868868
$partialFile = new UploadedPartialFile("foo", 9, $user);
869-
$uploadedFile = new UploadedFile("foo", new DateTime(), 9, $user);
869+
$uploadedFile = new UploadedFile("foo", new DateTime(), 9, $user, true);
870870
self::setPropertyOfObject($partialFile, 'id', '123');
871871
self::setPropertyOfObject($uploadedFile, 'id', '123');
872872

0 commit comments

Comments
 (0)