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

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