<?php
namespace App\Controller\Front\Scholar\Lesson;
use App\Entity\Account\User;
use App\Entity\Scholar\Lesson\Lesson;
use App\Entity\Scholar\Lesson\LessonReview;
use App\Entity\Scholar\Lesson\SharedLesson;
use App\Entity\Scholar\Module\Module;
use App\Entity\Scholar\Training\Training;
use App\Form\Scholar\LessonReviewType;
use App\Repository\Scholar\LessonReviewRepository;
use App\Service\Scholar\LessonService;
use App\Service\Scholar\ScholarNavigationService;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class ShowLessonReviewController extends AbstractController
{
public function __construct(
private LessonService $lessonService,
private EntityManagerInterface $entityManager,
private ScholarNavigationService $scholarNavigationService,
)
{
}
#[Route(path: '/training/{trainingId}/module/{moduleId}/lesson/{lessonId}/review', name: 'front_channel_lesson_show_review', methods: ['GET', 'POST'])]
#[ParamConverter('training', options: ['id' => 'trainingId'])]
#[ParamConverter('module', options: ['id' => 'moduleId'])]
#[ParamConverter('lesson', options: ['id' => 'lessonId'])]
public function __invoke(Request $request, Training $training, Module $module, Lesson $lesson): Response
{
/** @var ?User $user */
$user = $this->getUser();
if ($user === null) {
throw new AccessDeniedException('Access denied.');
}
if (!$this->isGranted('FRONT_ACCESS_REVIEW', [
'training' => $training,
'module' => $module,
'lesson' => $lesson,
])) {
throw $this->createAccessDeniedException();
}
$channel = $training->getOwnerChannel();
if (!$lesson->isLessonReviewActivated()) {
throw new NotFoundHttpException();
}
$lessonReview = $this->lessonService->getLessonReviewForLesson($lesson, $user);
$form = $this->createForm(LessonReviewType::class, $lessonReview);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* The learner can't change his lessonReview
*/
if ($lessonReview->getId() !== null) {
throw new NotFoundHttpException();
}
$this->entityManager->persist($lessonReview);
$this->entityManager->flush();
$nextInfos = $this->scholarNavigationService->getAfterLessonReview($training, $module, $lesson);
if ($nextInfos) {
return $this->redirect($nextInfos['link']);
}
return $this->redirectToRoute('front_channel_training_show', [
'id' => $training->getId(),
]);
}
return $this->render('Front/Scholar/Lesson/show_review.html.twig', [
'channel' => $channel,
'training' => $training,
'module' => $module,
'lesson' => $lesson,
'lessonReview' => $lessonReview->getId() === null ? null : $lessonReview,
'form' => $form->createView(),
]);
}
}