Skip to content

Commit b83751e

Browse files
committed
Fixing problem with exercise file links removal during exercise-assignment sync.
1 parent 862985a commit b83751e

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

app/model/entity/Assignment.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ class Assignment extends AssignmentBase implements IExercise
2525
use ExerciseData;
2626

2727
/**
28-
* @ORM\OneToMany(targetEntity="ExerciseFileLink", mappedBy="assignment", cascade={"persist"})
28+
* @ORM\OneToMany(targetEntity="ExerciseFileLink", mappedBy="assignment", cascade={"persist", "remove"},
29+
* orphanRemoval=true)
2930
* @var Collection<ExerciseFileLink>
3031
*/
31-
protected $fileLinks;
32+
protected $exerciseFileLinks;
3233

3334
/**
3435
* @return Collection<ExerciseFileLink>
3536
*/
3637
public function getFileLinks(): Collection
3738
{
38-
return $this->fileLinks;
39+
return $this->exerciseFileLinks;
3940
}
4041

4142
private function __construct(
@@ -91,7 +92,7 @@ private function __construct(
9192
$this->configurationType = $exercise->getConfigurationType();
9293
$this->exerciseFiles = $exercise->getExerciseFiles();
9394
$this->attachmentFiles = $exercise->getAttachmentFiles();
94-
$this->fileLinks = new ArrayCollection();
95+
$this->exerciseFileLinks = new ArrayCollection();
9596
$this->solutionFilesLimit = $exercise->getSolutionFilesLimit();
9697
$this->solutionSizeLimit = $exercise->getSolutionSizeLimit();
9798
}
@@ -125,7 +126,7 @@ public static function assignToGroup(
125126
// copy file links from exercise
126127
foreach ($exercise->getFileLinks() as $link) {
127128
$newLink = ExerciseFileLink::copyForAssignment($link, $assignment);
128-
$assignment->fileLinks->add($newLink);
129+
$assignment->exerciseFileLinks->add($newLink);
129130
}
130131

131132
return $assignment;
@@ -599,10 +600,10 @@ public function syncWithExercise(array $options = []): void
599600
}
600601

601602
if ($syncOptions->fileLinks) {
602-
$this->fileLinks->clear();
603+
$this->exerciseFileLinks->clear();
603604
foreach ($exercise->getFileLinks() as $link) {
604605
$newLink = ExerciseFileLink::copyForAssignment($link, $this);
605-
$this->fileLinks->add($newLink);
606+
$this->exerciseFileLinks->add($newLink);
606607
}
607608
}
608609

app/model/entity/Exercise.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class Exercise implements IExercise
112112
protected $archivedAt = null;
113113

114114
/**
115-
* @ORM\OneToMany(targetEntity="ExerciseFileLink", mappedBy="exercise", cascade={"persist"})
115+
* @ORM\OneToMany(targetEntity="ExerciseFileLink", mappedBy="exercise", cascade={"persist", "remove"},
116+
* orphanRemoval=true)
116117
* @var Collection<ExerciseFileLink>
117118
*/
118119
protected $fileLinks;

app/model/entity/ExerciseFileLink.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ExerciseFileLink implements JsonSerializable
5959
protected $exercise;
6060

6161
/**
62-
* @ORM\ManyToOne(targetEntity="Assignment")
62+
* @ORM\ManyToOne(targetEntity="Assignment", inversedBy="exerciseFileLinks")
6363
* @ORM\JoinColumn(onDelete="CASCADE")
6464
*/
6565
protected $assignment;

tests/Presenters/AssignmentsPresenter.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ class TestAssignmentsPresenter extends Tester\TestCase
841841

842842
Assert::same($assignment->getId(), $data["id"]);
843843
Assert::same($newExerciseLimits, $assignment->getLimitsByEnvironmentAndHwGroup($environment, $hwGroup));
844+
$this->presenter->assignments->refresh($assignment);
844845
Assert::count(1, $assignment->getFileLinks());
845846
$newLink = $assignment->getFileLinks()->first();
846847
Assert::equal("NEW", $newLink->getKey());

0 commit comments

Comments
 (0)