diff --git a/src/Controller/Backoffice/QuestionBankController.php b/src/Controller/Backoffice/QuestionBankController.php index 494db85..afb48fb 100644 --- a/src/Controller/Backoffice/QuestionBankController.php +++ b/src/Controller/Backoffice/QuestionBankController.php @@ -150,6 +150,15 @@ class QuestionBankController extends AbstractController { $this->assertSameSeason($season, $bankQuestion->season); + $hasLockedUsages = $bankQuestion->usages->exists( + static fn (int $key, BankQuestionUsage $usage): bool => $usage->quiz->isLocked(), + ); + if ($hasLockedUsages) { + $this->addFlash(FlashType::Danger, $this->translator->trans('This question cannot be deleted because it is used in a locked or active quiz')); + + return $this->redirectToRoute('tvdt_backoffice_question_bank', ['seasonCode' => $season->seasonCode]); + } + $this->em->remove($bankQuestion); $this->em->flush(); @@ -299,13 +308,14 @@ class QuestionBankController extends AbstractController throw new NotFoundHttpException(); } - if ($usage->quiz->hasStartedCandidates()) { - $this->addFlash(FlashType::Danger, $this->translator->trans('This quiz has already been filled in and can no longer be altered')); + if ($usage->quiz->isLocked()) { + $this->addFlash(FlashType::Danger, $this->translator->trans('This quiz can no longer be altered')); return $this->redirectToRoute('tvdt_backoffice_question_bank', ['seasonCode' => $season->seasonCode]); } $this->questionBankService->syncToQuiz($bankQuestion, $usage); + $this->em->flush(); $this->addFlash(FlashType::Success, $this->translator->trans('Question synced to quiz %quiz%', ['%quiz%' => $usage->quiz->name])); return $this->redirectToRoute('tvdt_backoffice_question_bank', ['seasonCode' => $season->seasonCode]); @@ -329,14 +339,20 @@ class QuestionBankController extends AbstractController private function syncUsagesAfterEdit(BankQuestion $bankQuestion): void { $pendingNames = []; + $synced = false; foreach ($bankQuestion->usages as $usage) { - if (!$usage->quiz->isFinalized()) { + if (!$usage->quiz->isLocked()) { $this->questionBankService->syncToQuiz($bankQuestion, $usage); - } elseif (!$usage->quiz->hasStartedCandidates()) { + $synced = true; + } else { $pendingNames[] = $usage->quiz->name; } } + if ($synced) { + $this->em->flush(); + } + if ([] !== $pendingNames) { $this->addFlash( FlashType::Warning, diff --git a/src/Controller/Backoffice/QuizController.php b/src/Controller/Backoffice/QuizController.php index 80025ba..57c77fc 100644 --- a/src/Controller/Backoffice/QuizController.php +++ b/src/Controller/Backoffice/QuizController.php @@ -297,8 +297,6 @@ class QuizController extends AbstractController { try { $this->quizRepository->clearQuiz($quiz); - $quiz->finalizedAt = null; - $this->em->flush(); $this->addFlash(FlashType::Success, $this->translator->trans('Quiz cleared and no longer finalized')); } catch (ErrorClearingQuizException) { $this->addFlash(FlashType::Danger, $this->translator->trans('Error clearing quiz')); @@ -323,6 +321,8 @@ class QuizController extends AbstractController $quiz->finalizedAt = new DateTimeImmutable(); $this->em->flush(); $this->addFlash(FlashType::Success, $this->translator->trans('Quiz finalized')); + } else { + $this->addFlash(FlashType::Warning, $this->translator->trans('The quiz is already finalized')); } return $this->redirectToRoute('tvdt_backoffice_quiz', ['seasonCode' => $quiz->season->seasonCode, 'quiz' => $quiz->id]); diff --git a/src/Controller/Backoffice/SeasonController.php b/src/Controller/Backoffice/SeasonController.php index 0141620..f9c127d 100644 --- a/src/Controller/Backoffice/SeasonController.php +++ b/src/Controller/Backoffice/SeasonController.php @@ -161,7 +161,7 @@ class SeasonController extends AbstractController public function addBlankQuiz(Request $request, Season $season): Response { $form = $this->createFormBuilder(new Quiz()) - ->add('name', TextType::class, ['label' => 'Quiz name']) + ->add('name', TextType::class, ['label' => $this->translator->trans('Quiz name'), 'translation_domain' => false]) ->add('save', SubmitType::class, ['label' => 'Create']) ->getForm(); diff --git a/src/Entity/BankQuestion.php b/src/Entity/BankQuestion.php index 83b9124..24309d7 100644 --- a/src/Entity/BankQuestion.php +++ b/src/Entity/BankQuestion.php @@ -131,6 +131,6 @@ class BankQuestion implements \Stringable public function __toString(): string { - return $this->question ?? ''; + return $this->question; } } diff --git a/src/Repository/QuizRepository.php b/src/Repository/QuizRepository.php index 43dbf80..b573ae6 100644 --- a/src/Repository/QuizRepository.php +++ b/src/Repository/QuizRepository.php @@ -72,6 +72,20 @@ class QuizRepository extends ServiceEntityRepository DQL) ->setParameter('quiz', $quiz) ->execute(); + + $em->createQuery(<<setParameter('quiz', $quiz) + ->execute(); + + $em->createQuery(<<setParameter('quiz', $quiz) + ->execute(); } // @codeCoverageIgnoreStart catch (\Throwable $throwable) { diff --git a/src/Service/QuestionBankService.php b/src/Service/QuestionBankService.php index 96f86ce..d3abdab 100644 --- a/src/Service/QuestionBankService.php +++ b/src/Service/QuestionBankService.php @@ -97,8 +97,6 @@ final readonly class QuestionBankService $answer = $this->objectMapper->map($bankAnswer, Answer::class); $question->addAnswer($answer); } - - $this->entityManager->flush(); } /** Remove the quiz copy created by this usage and delete the usage record. */