src/Controller/Front/Scholar/Lesson/ShowLessonReviewController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front\Scholar\Lesson;
  3. use App\Entity\Account\User;
  4. use App\Entity\Scholar\Lesson\Lesson;
  5. use App\Entity\Scholar\Module\Module;
  6. use App\Entity\Scholar\Training\Training;
  7. use App\Form\Scholar\LessonReviewType;
  8. use App\Service\ChannelUserData\CursusService;
  9. use App\Service\Scholar\LessonService;
  10. use App\Service\Scholar\ScholarNavigationService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  19. class ShowLessonReviewController extends AbstractController
  20. {
  21. public function __construct(
  22. private LessonService $lessonService,
  23. private EntityManagerInterface $entityManager,
  24. private ScholarNavigationService $scholarNavigationService,
  25. private CursusService $cursusService,
  26. ) { }
  27. #[Route(path: '/training/{trainingId}/module/{moduleId}/lesson/{lessonId}/review', name: 'front_channel_lesson_show_review', methods: ['GET', 'POST'])]
  28. #[ParamConverter('training', options: ['id' => 'trainingId'])]
  29. #[ParamConverter('module', options: ['id' => 'moduleId'])]
  30. #[ParamConverter('lesson', options: ['id' => 'lessonId'])]
  31. public function __invoke(Request $request, Training $training, Module $module, Lesson $lesson): Response
  32. {
  33. /** @var ?User $user */
  34. $user = $this->getUser();
  35. if ($user === null) {
  36. throw new AccessDeniedException('Access denied.');
  37. }
  38. if (!$this->isGranted('FRONT_ACCESS_REVIEW', [
  39. 'training' => $training,
  40. 'module' => $module,
  41. 'lesson' => $lesson,
  42. ])) {
  43. throw $this->createAccessDeniedException();
  44. }
  45. $channel = $training->getOwnerChannel();
  46. if (!$lesson->isLessonReviewActivated()) {
  47. throw new NotFoundHttpException();
  48. }
  49. $lessonReview = $this->lessonService->getLessonReviewForLesson($lesson, $user);
  50. $form = $this->createForm(LessonReviewType::class, $lessonReview);
  51. $form->handleRequest($request);
  52. if ($form->isSubmitted() && $form->isValid()) {
  53. /**
  54. * The learner can't change his lessonReview
  55. */
  56. if ($lessonReview->getId() !== null) {
  57. throw new NotFoundHttpException();
  58. }
  59. $this->entityManager->persist($lessonReview);
  60. $this->entityManager->flush();
  61. $nextInfos = $this->scholarNavigationService->getAfterLessonReview($training, $module, $lesson);
  62. if ($nextInfos) {
  63. return $this->redirect($nextInfos['link']);
  64. }
  65. return $this->redirectToRoute('front_channel_training_show', [
  66. 'id' => $training->getId(),
  67. ]);
  68. }
  69. return $this->render('Front/Scholar/Lesson/show_review.html.twig', [
  70. 'channel' => $channel,
  71. 'training' => $training,
  72. 'module' => $module,
  73. 'lesson' => $lesson,
  74. 'lessonReview' => $lessonReview->getId() === null ? null : $lessonReview,
  75. 'form' => $form->createView(),
  76. 'cursus' => $this->cursusService->getCursusByTraining($training, $this->getUser()),
  77. ]);
  78. }
  79. }