src/Service/Scholar/ScholarNavigationService.php line 151

Open in your IDE?
  1. <?php
  2. namespace App\Service\Scholar;
  3. use App\Entity\Exercise\Exercise;
  4. use App\Entity\Exercise\User\ExerciseResult;
  5. use App\Entity\Scholar\Chapter\Chapter;
  6. use App\Entity\Scholar\Lesson\Lesson;
  7. use App\Entity\Scholar\Module\Module;
  8. use App\Entity\Scholar\PracticalCase\PracticalCase;
  9. use App\Entity\Scholar\Training\Training;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class ScholarNavigationService
  13. {
  14. public function __construct(
  15. private TrainingService $trainingService,
  16. private LessonService $lessonService,
  17. private UrlGeneratorInterface $urlGenerator,
  18. private TranslatorInterface $translator,
  19. private ScholarNavigationSecurityService $scholarNavigationSecurityService,
  20. )
  21. {
  22. }
  23. public function getAfterLessonSummary(Training $training, Module $module, Lesson $lesson): array|null
  24. {
  25. return $this->getAfterChapter($training, $module, $lesson, null);
  26. }
  27. public function getAfterChapter(Training $training, Module $module, Lesson $lesson, ?Chapter $chapter): array|null
  28. {
  29. $nextChapter = $this->lessonService->getNextChapter($chapter, $lesson);
  30. if ($nextChapter) {
  31. return $this->getInformationsForChapter($training, $module, $lesson, $nextChapter);
  32. }
  33. return $this->getAfterExerciseResult($training, $module, $lesson, null);
  34. }
  35. public function getAfterExerciseResult(Training $training, Module $module, Lesson $lesson, ?ExerciseResult $exerciseResult): array|null
  36. {
  37. $exercise = $exerciseResult?->getExercise();
  38. if ($exerciseResult !== null && !$exerciseResult->isSuccessful()) {
  39. return $this->getInformationsForLessonSummary($training, $module, $lesson);
  40. }
  41. $nextExercise = $this->lessonService->getNextExercise($exercise, $lesson);
  42. if ($nextExercise) {
  43. return $this->getInformationsForExercise($training, $module, $lesson, $nextExercise);
  44. }
  45. $practicalCase = $lesson->getActivePracticalCase();
  46. if ($practicalCase) {
  47. return $this->getInformationsForPracticalCase($training, $module, $lesson, $practicalCase);
  48. }
  49. $lessonReview = $lesson->isLessonReviewActivated();
  50. if ($lessonReview) {
  51. return $this->getInformationsForLessonReview($training, $module, $lesson);
  52. }
  53. return $this->getAfterLessonReview($training, $module, $lesson);
  54. }
  55. public function getAfterLessonReview(Training $training, Module $module, Lesson $lesson): array|null
  56. {
  57. $nextModuleLesson = $this->trainingService->getNextModuleLesson($training, $module, $lesson);
  58. if ($nextModuleLesson) {
  59. return $this->getInformationsForLessonSummary($training, $nextModuleLesson->getModule(), $nextModuleLesson->getLesson());
  60. }
  61. return null;
  62. }
  63. public function getBeforeLessonSummary(Training $training, Module $module, Lesson $lesson): array|null
  64. {
  65. $previousModuleLesson = $this->trainingService->getPreviousModuleLesson($training, $module, $lesson);
  66. if ($previousModuleLesson) {
  67. return $this->getBeforeLessonReview($training, $previousModuleLesson->getModule(), $previousModuleLesson->getLesson());
  68. }
  69. return null;
  70. }
  71. public function getBeforeChapter(Training $training, Module $module, Lesson $lesson, ?Chapter $chapter): array|null
  72. {
  73. $previousChapter = $this->lessonService->getPreviousChapter($chapter, $lesson);
  74. if ($previousChapter) {
  75. return $this->getInformationsForChapter($training, $module, $lesson, $previousChapter);
  76. }
  77. return $this->getInformationsForLessonSummary($training, $module, $lesson);
  78. }
  79. public function getBeforeExercise(Training $training, Module $module, Lesson $lesson, ?Exercise $exercise): array|null
  80. {
  81. $previousExercise = $this->lessonService->getPreviousExercise($exercise, $lesson);
  82. if ($previousExercise) {
  83. return $this->getInformationsForExercise($training, $module, $lesson, $previousExercise);
  84. }
  85. return $this->getBeforeChapter($training, $module, $lesson, null);
  86. }
  87. public function getBeforeLessonReview(Training $training, Module $module, Lesson $lesson): array|null
  88. {
  89. if ($lesson->isLessonReviewActivated()) {
  90. return $this->getInformationsForLessonReview($training, $module, $lesson);
  91. }
  92. return $this->getBeforeExercise($training, $module, $lesson, null);
  93. }
  94. private function getInformationsForLessonSummary(Training $training, Module $module, Lesson $lesson): array
  95. {
  96. $path = null;
  97. if ($this->scholarNavigationSecurityService->canAccessLesson($training, $module, $lesson)) {
  98. $path = $this->urlGenerator->generate('front_channel_lesson_show', [
  99. 'trainingId' => $training->getId(),
  100. 'moduleId' => $module->getId(),
  101. 'lessonId' => $lesson->getId(),
  102. ]);
  103. }
  104. return $this->generateInformations(
  105. $path,
  106. $this->translator->trans('navigation.lesson.label') . $lesson->getName()
  107. );
  108. }
  109. private function getInformationsForChapter(Training $training, Module $module, Lesson $lesson, Chapter $chapter): array
  110. {
  111. $path = null;
  112. if ($this->scholarNavigationSecurityService->canAccessLesson($training, $module, $lesson)) {
  113. $path = $this->urlGenerator->generate('front_channel_chapter_show', [
  114. 'trainingId' => $training->getId(),
  115. 'moduleId' => $module->getId(),
  116. 'lessonId' => $lesson->getId(),
  117. 'chapterId' => $chapter->getId()
  118. ]);
  119. }
  120. return $this->generateInformations(
  121. $path,
  122. $this->translator->trans('navigation.chapter.label') . $chapter->getName()
  123. );
  124. }
  125. private function getInformationsForExercise(Training $training, Module $module, Lesson $lesson, Exercise $exercise): array
  126. {
  127. $path = null;
  128. if ($this->scholarNavigationSecurityService->canAccessLesson($training, $module, $lesson)) {
  129. $path = $this->urlGenerator->generate('front_channel_exercise_show', [
  130. 'trainingId' => $training->getId(),
  131. 'moduleId' => $module->getId(),
  132. 'lessonId' => $lesson->getId(),
  133. 'exerciseId' => $exercise->getId()
  134. ]);
  135. }
  136. return $this->generateInformations(
  137. $path,
  138. $this->translator->trans('navigation.exercise.label') . $exercise->getName()
  139. );
  140. }
  141. private function getInformationsForPracticalCase(Training $training, Module $module, Lesson $lesson, PracticalCase $practicalCase): array
  142. {
  143. $path = null;
  144. if ($this->scholarNavigationSecurityService->canAccessLesson($training, $module, $lesson)) {
  145. $path = $this->urlGenerator->generate('front_channel_practical_case_show', [
  146. 'trainingId' => $training->getId(),
  147. 'moduleId' => $module->getId(),
  148. 'lessonId' => $lesson->getId(),
  149. 'practicalCaseId' => $practicalCase->getId()
  150. ]);
  151. }
  152. return $this->generateInformations(
  153. $path,
  154. $this->translator->trans('navigation.practicalCase.label') . $practicalCase->getTitle()
  155. );
  156. }
  157. private function getInformationsForLessonReview(Training $training, Module $module, Lesson $lesson): array
  158. {
  159. $path = null;
  160. if ($this->scholarNavigationSecurityService->canAccessLessonReview($training, $module, $lesson)) {
  161. $path = $this->urlGenerator->generate('front_channel_lesson_show_review', [
  162. 'trainingId' => $training->getId(),
  163. 'moduleId' => $module->getId(),
  164. 'lessonId' => $lesson->getId(),
  165. ]);
  166. }
  167. return $this->generateInformations(
  168. $path,
  169. $this->translator->trans('navigation.lesson_review.label')
  170. );
  171. }
  172. private function generateInformations(?string $link, string $title): array
  173. {
  174. return [
  175. 'link' => $link,
  176. 'title' => $title,
  177. ];
  178. }
  179. }