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

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\Scholar\LessonService;
  9. use App\Service\Scholar\ScholarNavigationService;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  18. class ShowLessonReviewController extends AbstractController
  19. {
  20.     public function __construct(
  21.         private LessonService $lessonService,
  22.         private EntityManagerInterface   $entityManager,
  23.         private ScholarNavigationService $scholarNavigationService,
  24.     )
  25.     {
  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 $requestTraining $trainingModule $moduleLesson $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.         ]);
  77.     }
  78. }