<?php
namespace App\Service\Scholar;
use App\Entity\Exercise\Exercise;
use App\Entity\Exercise\User\ExerciseResult;
use App\Entity\Scholar\Chapter\Chapter;
use App\Entity\Scholar\Lesson\Lesson;
use App\Entity\Scholar\Module\Module;
use App\Entity\Scholar\PracticalCase\PracticalCase;
use App\Entity\Scholar\Training\Training;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class ScholarNavigationService
{
public function __construct(
private TrainingService $trainingService,
private LessonService $lessonService,
private UrlGeneratorInterface $urlGenerator,
private TranslatorInterface $translator,
private ScholarNavigationSecurityService $scholarNavigationSecurityService,
)
{
}
public function getAfterLessonSummary(Training $training, Module $module, Lesson $lesson): array|null
{
return $this->getAfterChapter($training, $module, $lesson, null);
}
public function getAfterChapter(Training $training, Module $module, Lesson $lesson, ?Chapter $chapter): array|null
{
$nextChapter = $this->lessonService->getNextChapter($chapter, $lesson);
if ($nextChapter) {
return $this->getInformationsForChapter($training, $module, $lesson, $nextChapter);
}
return $this->getAfterExerciseResult($training, $module, $lesson, null);
}
public function getAfterExerciseResult(Training $training, Module $module, Lesson $lesson, ?ExerciseResult $exerciseResult): array|null
{
$exercise = $exerciseResult?->getExercise();
if ($exerciseResult !== null && !$exerciseResult->isSuccessful()) {
return $this->getInformationsForLessonSummary($training, $module, $lesson);
}
$nextExercise = $this->lessonService->getNextExercise($exercise, $lesson);
if ($nextExercise) {
return $this->getInformationsForExercise($training, $module, $lesson, $nextExercise);
}
$practicalCase = $lesson->getActivePracticalCase();
if ($practicalCase) {
return $this->getInformationsForPracticalCase($training, $module, $lesson, $practicalCase);
}
$lessonReview = $lesson->isLessonReviewActivated();
if ($lessonReview) {
return $this->getInformationsForLessonReview($training, $module, $lesson);
}
return $this->getAfterLessonReview($training, $module, $lesson);
}
public function getAfterLessonReview(Training $training, Module $module, Lesson $lesson): array|null
{
$nextModuleLesson = $this->trainingService->getNextModuleLesson($training, $module, $lesson);
if ($nextModuleLesson) {
return $this->getInformationsForLessonSummary($training, $nextModuleLesson->getModule(), $nextModuleLesson->getLesson());
}
return null;
}
public function getBeforeLessonSummary(Training $training, Module $module, Lesson $lesson): array|null
{
$previousModuleLesson = $this->trainingService->getPreviousModuleLesson($training, $module, $lesson);
if ($previousModuleLesson) {
return $this->getBeforeLessonReview($training, $previousModuleLesson->getModule(), $previousModuleLesson->getLesson());
}
return null;
}
public function getBeforeChapter(Training $training, Module $module, Lesson $lesson, ?Chapter $chapter): array|null
{
$previousChapter = $this->lessonService->getPreviousChapter($chapter, $lesson);
if ($previousChapter) {
return $this->getInformationsForChapter($training, $module, $lesson, $previousChapter);
}
return $this->getInformationsForLessonSummary($training, $module, $lesson);
}
public function getBeforeExercise(Training $training, Module $module, Lesson $lesson, ?Exercise $exercise): array|null
{
$previousExercise = $this->lessonService->getPreviousExercise($exercise, $lesson);
if ($previousExercise) {
return $this->getInformationsForExercise($training, $module, $lesson, $previousExercise);
}
return $this->getBeforeChapter($training, $module, $lesson, null);
}
public function getBeforeLessonReview(Training $training, Module $module, Lesson $lesson): array|null
{
if ($lesson->isLessonReviewActivated()) {
return $this->getInformationsForLessonReview($training, $module, $lesson);
}
return $this->getBeforeExercise($training, $module, $lesson, null);
}
private function getInformationsForLessonSummary(Training $training, Module $module, Lesson $lesson): array
{
$path = null;
if ($this->scholarNavigationSecurityService->canAccessLesson($training, $module, $lesson)) {
$path = $this->urlGenerator->generate('front_channel_lesson_show', [
'trainingId' => $training->getId(),
'moduleId' => $module->getId(),
'lessonId' => $lesson->getId(),
]);
}
return $this->generateInformations(
$path,
$this->translator->trans('navigation.lesson.label') . $lesson->getName()
);
}
private function getInformationsForChapter(Training $training, Module $module, Lesson $lesson, Chapter $chapter): array
{
$path = null;
if ($this->scholarNavigationSecurityService->canAccessLesson($training, $module, $lesson)) {
$path = $this->urlGenerator->generate('front_channel_chapter_show', [
'trainingId' => $training->getId(),
'moduleId' => $module->getId(),
'lessonId' => $lesson->getId(),
'chapterId' => $chapter->getId()
]);
}
return $this->generateInformations(
$path,
$this->translator->trans('navigation.chapter.label') . $chapter->getName()
);
}
private function getInformationsForExercise(Training $training, Module $module, Lesson $lesson, Exercise $exercise): array
{
$path = null;
if ($this->scholarNavigationSecurityService->canAccessLesson($training, $module, $lesson)) {
$path = $this->urlGenerator->generate('front_channel_exercise_show', [
'trainingId' => $training->getId(),
'moduleId' => $module->getId(),
'lessonId' => $lesson->getId(),
'exerciseId' => $exercise->getId()
]);
}
return $this->generateInformations(
$path,
$this->translator->trans('navigation.exercise.label') . $exercise->getName()
);
}
private function getInformationsForPracticalCase(Training $training, Module $module, Lesson $lesson, PracticalCase $practicalCase): array
{
$path = null;
if ($this->scholarNavigationSecurityService->canAccessLesson($training, $module, $lesson)) {
$path = $this->urlGenerator->generate('front_channel_practical_case_show', [
'trainingId' => $training->getId(),
'moduleId' => $module->getId(),
'lessonId' => $lesson->getId(),
'practicalCaseId' => $practicalCase->getId()
]);
}
return $this->generateInformations(
$path,
$this->translator->trans('navigation.practicalCase.label') . $practicalCase->getTitle()
);
}
private function getInformationsForLessonReview(Training $training, Module $module, Lesson $lesson): array
{
$path = null;
if ($this->scholarNavigationSecurityService->canAccessLessonReview($training, $module, $lesson)) {
$path = $this->urlGenerator->generate('front_channel_lesson_show_review', [
'trainingId' => $training->getId(),
'moduleId' => $module->getId(),
'lessonId' => $lesson->getId(),
]);
}
return $this->generateInformations(
$path,
$this->translator->trans('navigation.lesson_review.label')
);
}
private function generateInformations(?string $link, string $title): array
{
return [
'link' => $link,
'title' => $title,
];
}
}