<?php
namespace App\Controller\Front\Scholar\Lesson;
use App\Entity\Achievement\AchievedLesson;
use App\Entity\Achievement\AchievementTimeLogLesson;
use App\Entity\Scholar\Lesson\Lesson;
use App\Entity\Scholar\Module\Module;
use App\Entity\Scholar\Training\Training;
use App\Service\Achievement\AchievementManager;
use Doctrine\ORM\EntityManagerInterface;
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\Routing\Annotation\Route;
class ShowController extends AbstractController
{
public function __construct(
private AchievementManager $achievementManager,
private EntityManagerInterface $entityManager,
)
{
}
#[Route(path: '/training/{trainingId}/module/{moduleId}/lesson/{lessonId}', name: 'front_channel_lesson_show', methods: ['GET'])]
#[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
{
if (!$this->isGranted('FRONT_ACCESS', [
'training' => $training,
'module' => $module,
'lesson' => $lesson,
])) {
throw $this->createAccessDeniedException();
}
$exercices = $lesson->getExercises();
/** @var AchievedLesson $achievedLesson */
$achievedLesson = $this->achievementManager->getOrCreate($this->getUser(), $lesson);
if ($achievedLesson) {
$achievementTimeLog = new AchievementTimeLogLesson();
$achievementTimeLog->setAchievedLesson($achievedLesson)
->setLesson($lesson)
->setModule($module)
->setTraining($training)
->setStartAt(new \DateTime());
$this->entityManager->persist($achievementTimeLog);
$this->entityManager->flush();
}
return $this->render('Front/Scholar/Lesson/show.html.twig', [
'training' => $training,
'module' => $module,
'lesson' => $lesson,
'exercises' => $exercices,
'channel' => $training->getOwnerChannel(),
]);
}
}