|
12 | 12 | use Doctrine\Common\Collections\Criteria; |
13 | 13 | use Gedmo\Mapping\Annotation as Gedmo; |
14 | 14 | use DateTime; |
| 15 | +use InvalidArgumentException; |
15 | 16 |
|
16 | 17 | /** |
17 | 18 | * @ORM\Entity |
@@ -535,64 +536,110 @@ function ($key, RuntimeEnvironment $env) use ($exercise) { |
535 | 536 | ); |
536 | 537 | } |
537 | 538 |
|
538 | | - public function syncWithExercise() |
| 539 | + public function syncWithExercise(array $options = []): void |
539 | 540 | { |
540 | 541 | $exercise = $this->getExercise(); |
541 | 542 | if ($exercise === null) { |
542 | 543 | // cannot sync exercise was deleted |
543 | 544 | return; |
544 | 545 | } |
545 | 546 |
|
546 | | - $this->mergeJudgeLogs = $exercise->getMergeJudgeLogs(); |
| 547 | + // figure out which parts to sync (all are synced if no options are given) |
| 548 | + $syncOptions = [ |
| 549 | + "configurationType" => !$options, |
| 550 | + "exerciseConfig" => !$options, |
| 551 | + "exerciseEnvironmentConfigs" => !$options, |
| 552 | + "exerciseTests" => !$options, |
| 553 | + "files" => !$options, |
| 554 | + "fileLinks" => !$options, |
| 555 | + "hardwareGroups" => !$options, |
| 556 | + "limits" => !$options, |
| 557 | + "localizedTexts" => !$options, |
| 558 | + "mergeJudgeLogs" => !$options, |
| 559 | + "runtimeEnvironments" => !$options, |
| 560 | + "scoreConfig" => !$options, |
| 561 | + ]; |
| 562 | + |
| 563 | + foreach ($options as $option) { |
| 564 | + if (!array_key_exists($option, $syncOptions)) { |
| 565 | + throw new InvalidArgumentException("Unknown sync option: $option"); |
| 566 | + } |
| 567 | + $syncOptions[$option] = true; |
| 568 | + } |
547 | 569 |
|
548 | | - $this->hardwareGroups->clear(); |
549 | | - foreach ($exercise->getHardwareGroups() as $group) { |
550 | | - $this->hardwareGroups->add($group); |
| 570 | + $syncOptions = (object)$syncOptions; |
| 571 | + |
| 572 | + if ($syncOptions->configurationType) { |
| 573 | + $this->configurationType = $exercise->getConfigurationType(); |
551 | 574 | } |
552 | 575 |
|
553 | | - $this->localizedTexts->clear(); |
554 | | - foreach ($exercise->getLocalizedTexts() as $text) { |
555 | | - $this->localizedTexts->add($text); |
| 576 | + if ($syncOptions->exerciseConfig) { |
| 577 | + $this->exerciseConfig = $exercise->getExerciseConfig(); |
556 | 578 | } |
557 | 579 |
|
558 | | - $this->exerciseConfig = $exercise->getExerciseConfig(); |
559 | | - $this->configurationType = $exercise->getConfigurationType(); |
560 | | - $this->scoreConfig = $exercise->getScoreConfig(); |
| 580 | + if ($syncOptions->exerciseEnvironmentConfigs) { |
| 581 | + $this->exerciseEnvironmentConfigs->clear(); |
| 582 | + foreach ($exercise->getExerciseEnvironmentConfigs() as $config) { |
| 583 | + $this->exerciseEnvironmentConfigs->add($config); |
| 584 | + } |
| 585 | + } |
561 | 586 |
|
562 | | - $this->exerciseEnvironmentConfigs->clear(); |
563 | | - foreach ($exercise->getExerciseEnvironmentConfigs() as $config) { |
564 | | - $this->exerciseEnvironmentConfigs->add($config); |
| 587 | + if ($syncOptions->exerciseTests) { |
| 588 | + $this->exerciseTests->clear(); |
| 589 | + foreach ($exercise->getExerciseTests() as $test) { |
| 590 | + $this->exerciseTests->add($test); |
| 591 | + } |
565 | 592 | } |
566 | 593 |
|
567 | | - $this->exerciseLimits->clear(); |
568 | | - foreach ($exercise->getExerciseLimits() as $limits) { |
569 | | - $this->exerciseLimits->add($limits); |
| 594 | + if ($syncOptions->files) { |
| 595 | + $this->exerciseFiles->clear(); |
| 596 | + foreach ($exercise->getExerciseFiles() as $file) { |
| 597 | + $this->exerciseFiles->add($file); |
| 598 | + } |
570 | 599 | } |
571 | 600 |
|
572 | | - $this->exerciseTests->clear(); |
573 | | - foreach ($exercise->getExerciseTests() as $test) { |
574 | | - $this->exerciseTests->add($test); |
| 601 | + if ($syncOptions->fileLinks) { |
| 602 | + $this->fileLinks->clear(); |
| 603 | + foreach ($exercise->getFileLinks() as $link) { |
| 604 | + $newLink = ExerciseFileLink::copyForAssignment($link, $this); |
| 605 | + $this->fileLinks->add($newLink); |
| 606 | + } |
575 | 607 | } |
576 | 608 |
|
577 | | - $this->exerciseFiles->clear(); |
578 | | - foreach ($exercise->getExerciseFiles() as $file) { |
579 | | - $this->exerciseFiles->add($file); |
| 609 | + if ($syncOptions->hardwareGroups) { |
| 610 | + $this->hardwareGroups->clear(); |
| 611 | + foreach ($exercise->getHardwareGroups() as $group) { |
| 612 | + $this->hardwareGroups->add($group); |
| 613 | + } |
580 | 614 | } |
581 | 615 |
|
582 | | - $this->attachmentFiles->clear(); |
583 | | - foreach ($exercise->getAttachmentFiles() as $file) { |
584 | | - $this->attachmentFiles->add($file); |
| 616 | + if ($syncOptions->limits) { |
| 617 | + $this->exerciseLimits->clear(); |
| 618 | + foreach ($exercise->getExerciseLimits() as $limits) { |
| 619 | + $this->exerciseLimits->add($limits); |
| 620 | + } |
585 | 621 | } |
586 | 622 |
|
587 | | - $this->fileLinks->clear(); |
588 | | - foreach ($exercise->getFileLinks() as $link) { |
589 | | - $newLink = ExerciseFileLink::copyForAssignment($link, $this); |
590 | | - $this->fileLinks->add($newLink); |
| 623 | + if ($syncOptions->localizedTexts) { |
| 624 | + $this->localizedTexts->clear(); |
| 625 | + foreach ($exercise->getLocalizedTexts() as $text) { |
| 626 | + $this->localizedTexts->add($text); |
| 627 | + } |
| 628 | + } |
| 629 | + |
| 630 | + if ($syncOptions->mergeJudgeLogs) { |
| 631 | + $this->mergeJudgeLogs = $exercise->getMergeJudgeLogs(); |
| 632 | + } |
| 633 | + |
| 634 | + if ($syncOptions->runtimeEnvironments) { |
| 635 | + $this->runtimeEnvironments->clear(); |
| 636 | + foreach ($exercise->getRuntimeEnvironments() as $env) { |
| 637 | + $this->runtimeEnvironments->add($env); |
| 638 | + } |
591 | 639 | } |
592 | 640 |
|
593 | | - $this->runtimeEnvironments->clear(); |
594 | | - foreach ($exercise->getRuntimeEnvironments() as $env) { |
595 | | - $this->runtimeEnvironments->add($env); |
| 641 | + if ($syncOptions->scoreConfig) { |
| 642 | + $this->scoreConfig = $exercise->getScoreConfig(); |
596 | 643 | } |
597 | 644 |
|
598 | 645 | $this->syncedAt = new DateTime(); |
|
0 commit comments