src/Controller/Front/Scholar/Chapter/ChapterController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front\Scholar\Chapter;
  3. use App\Entity\Account\User;
  4. use App\Entity\Achievement\AchievedChapter;
  5. use App\Entity\Achievement\AchievementTimeLog;
  6. use App\Entity\Forum\ChapterMessage;
  7. use App\Entity\Scholar\Chapter\Chapter;
  8. use App\Entity\Scholar\Lesson\Lesson;
  9. use App\Entity\Scholar\Module\Module;
  10. use App\Entity\Scholar\Training\Training;
  11. use App\Service\Bunny\BunnyVideoPlayService;
  12. use App\Service\Bunny\BunnyVideoService;
  13. use App\Service\Forum\MessageManager;
  14. use App\Service\Scholar\LessonService;
  15. use Nellapp\Bundle\SDKBundle\Permission\Enum\ChannelUserTrainingPermissionEnum;
  16. use App\Form\Forum\MessageAnswerType;
  17. use App\Form\Forum\MessageType;
  18. use App\Repository\ChannelUserData\AbstractCursusRepository;
  19. use App\Repository\Forum\ChapterMessageRepository;
  20. use App\Service\Account\NotificationManager;
  21. use App\Service\Achievement\AchievementManager;
  22. use App\Service\ChannelUserData\CursusService;
  23. use App\Service\Scholar\StatusService;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  26. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpFoundation\Response;
  29. use Symfony\Component\Routing\Annotation\Route;
  30. class ChapterController extends AbstractController
  31. {
  32. public function __construct(
  33. private ChapterMessageRepository $messageRepository,
  34. private EntityManagerInterface $em,
  35. private NotificationManager $notificationManager,
  36. private AchievementManager $achievementManager,
  37. private MessageManager $messageManager,
  38. private LessonService $lessonService,
  39. private CursusService $cursusService,
  40. )
  41. {
  42. }
  43. #[Route(
  44. path: '/training/{trainingId}/module/{moduleId}/lesson/{lessonId}/chapter/{chapterId}',
  45. name: 'front_channel_chapter_show')
  46. ]
  47. #[ParamConverter('training', options: ['id' => 'trainingId'])]
  48. #[ParamConverter('module', options: ['id' => 'moduleId'])]
  49. #[ParamConverter('lesson', options: ['id' => 'lessonId'])]
  50. #[ParamConverter('chapter', options: ['id' => 'chapterId'])]
  51. public function __invoke(
  52. Request $request,
  53. Training $training,
  54. Module $module,
  55. Lesson $lesson,
  56. Chapter $chapter,
  57. ): Response
  58. {
  59. if (!$this->isGranted('FRONT_ACCESS', [
  60. 'training' => $training,
  61. 'module' => $module,
  62. 'lesson' => $lesson,
  63. ])) {
  64. throw $this->createAccessDeniedException();
  65. }
  66. if ($chapter->getLesson() !== $lesson) {
  67. throw $this->createNotFoundException();
  68. }
  69. /** @var ?User $user */
  70. $user = $this->getUser();
  71. /** @var AchievedChapter $achievedChapter */
  72. $achievedChapter = $this->achievementManager->getOrCreate($user, $chapter);
  73. if ($achievedChapter) {
  74. if (!$this->em->contains($chapter)) {
  75. $chapter = $this->em->find(Chapter::class, $chapter->getId());
  76. }
  77. if (!$this->em->contains($lesson)) {
  78. $lesson = $this->em->find(Lesson::class, $lesson->getId());
  79. }
  80. if (!$this->em->contains($module)) {
  81. $module = $this->em->find(Module::class, $module->getId());
  82. }
  83. if (!$this->em->contains($training)) {
  84. $training = $this->em->find(Training::class, $training->getId());
  85. }
  86. $achievementTimeLog = new AchievementTimeLog();
  87. $achievementTimeLog->setAchievedChapter($achievedChapter)
  88. ->setIpAddress($request->getClientIp())
  89. ->setChapter($chapter)
  90. ->setLesson($lesson)
  91. ->setModule($module)
  92. ->setTraining($training)
  93. ->setStartAt(new \DateTime());
  94. $this->em->persist($achievementTimeLog);
  95. $this->em->flush();
  96. }
  97. /**
  98. * Form for a message which is a new one
  99. */
  100. $message = $this->messageManager->createUserChapterMessage($user, $chapter, $module, $training);
  101. $form = $this->createForm(MessageType::class, $message);
  102. $form->handleRequest($request);
  103. if ($form->isSubmitted() && $form->isValid()) {
  104. $message->setAssignedTo($this->lessonService->getLessonReferralTrainer($lesson));
  105. // If the user that created the message is Manager -> DONE
  106. if ($this->isGranted(ChannelUserTrainingPermissionEnum::CHANNEL_USER_PERM_TRAINING_MANAGE_COMMENT, $training)) {
  107. $message->setAssignableStatusDone();
  108. } else {
  109. $message->setAssignableStatusTodo();
  110. }
  111. if ($form->get('parent-id')->getData() !== null) {
  112. $messageId = $form->get('parent-id')->getData();
  113. $messageParent = $this->messageRepository->find($messageId);
  114. $message->setParent($messageParent);
  115. // If the user that created the message is the assignee of the message parent -> DONE
  116. if ($this->getUser() && $messageParent->getAssignedTo() === $this->getUser()) {
  117. $message->setAssignableStatusDone();
  118. }
  119. // In case the user that created the message is the assignee of the parent message or Manager:
  120. // MessageParent -> DONE
  121. // All childs of message parent -> DONE
  122. if ($this->isGranted(ChannelUserTrainingPermissionEnum::CHANNEL_USER_PERM_TRAINING_MANAGE_COMMENT, $training)
  123. || $this->getUser() && $messageParent->getAssignedTo() === $this->getUser()
  124. ) {
  125. $messageParent->setAssignableStatusDone();
  126. $this->em->persist($messageParent);
  127. /** @var ChapterMessage $childMessage */
  128. foreach ($messageParent->getChildren() as $childMessage) {
  129. $childMessage->setAssignableStatusDone();
  130. $this->em->persist($childMessage);
  131. }
  132. }
  133. }
  134. $this->em->persist($message);
  135. $this->em->flush();
  136. if ($message->isTodo() && $user) {
  137. if ($form->get('parent-id')->getData() !== null && null !== ($messageUser = $messageParent->getUser())) {
  138. $this->notificationManager->addChapterResponseMessageNotification(
  139. $messageUser,
  140. $user,
  141. $message->getChapter(),
  142. $message,
  143. $training->getId(),
  144. $module->getId(),
  145. $lesson->getId()
  146. );
  147. } else {
  148. $this->notificationManager->addMessageNotificationToStaff($user, $chapter, $message, $module, $training);
  149. }
  150. }
  151. }
  152. /**
  153. * Form for a message which answer a question
  154. */
  155. $messageAnswer = $this->messageManager->createUserChapterMessage($user, $chapter, $module, $training);
  156. $formAnswerQuestion = $this->createForm(MessageAnswerType::class, $messageAnswer);
  157. $formAnswerQuestion->handleRequest($request);
  158. if ($formAnswerQuestion->isSubmitted() && $formAnswerQuestion->isValid()) {
  159. $messageAnswer->setAssignableStatusTodo();
  160. $messageAnswer->setAssignedTo($this->lessonService->getLessonReferralTrainer($lesson));
  161. if ($this->isGranted(ChannelUserTrainingPermissionEnum::CHANNEL_USER_PERM_TRAINING_MANAGE_COMMENT, $training)) {
  162. $messageAnswer->setAssignableStatusDone();
  163. } else {
  164. $messageAnswer->setAssignableStatusTodo();
  165. }
  166. if ($formAnswerQuestion->get('parent-id')->getData() !== null) {
  167. $messageId = $formAnswerQuestion->get('parent-id')->getData();
  168. $messageParent = $this->messageRepository->find($messageId);
  169. $messageAnswer->setParent($messageParent);
  170. if ($this->getUser() && $messageParent->getAssignedTo() === $this->getUser()) {
  171. $messageAnswer->setAssignableStatusDone();
  172. }
  173. if ($this->isGranted(ChannelUserTrainingPermissionEnum::CHANNEL_USER_PERM_TRAINING_MANAGE_COMMENT, $training)
  174. || $this->getUser() && $messageParent->getAssignedTo() === $this->getUser()
  175. ) {
  176. $messageParent->setAssignableStatusDone();
  177. }
  178. }
  179. $this->em->persist($messageAnswer);
  180. $this->em->flush();
  181. if ($user) {
  182. if ($formAnswerQuestion->get('parent-id')->getData() !== null && null !== ($messageUser = $messageParent->getUser())) {
  183. $this->notificationManager->addChapterResponseMessageNotification(
  184. $messageUser,
  185. $user,
  186. $messageAnswer->getChapter(),
  187. $messageAnswer,
  188. $training->getId(),
  189. $module->getId(),
  190. $lesson->getId()
  191. );
  192. } else {
  193. $this->notificationManager->addMessageNotificationToStaff(
  194. $user,
  195. $chapter,
  196. $messageAnswer,
  197. $module,
  198. $training
  199. );
  200. }
  201. }
  202. }
  203. $messages = $this->messageRepository->findByChapter($chapter);
  204. $isGrantedComment = false;
  205. if ($user !== null) {
  206. $isGrantedComment = $this->isGranted(ChannelUserTrainingPermissionEnum::CHANNEL_USER_PERM_TRAINING_MANAGE_COMMENT, $module);
  207. }
  208. return $this->render('Front/Scholar/Chapter/show.html.twig', [
  209. 'chapter' => $chapter,
  210. 'messages' => $messages,
  211. 'form' => $form->createView(),
  212. 'formAnswer' => $formAnswerQuestion->createView(),
  213. 'module' => $module,
  214. 'training' => $training,
  215. 'lesson' => $lesson,
  216. 'is_granted_comment' => $isGrantedComment,
  217. 'channel' => $training->getOwnerChannel(),
  218. 'cursus' => $this->cursusService->getCursusByTraining($training, $this->getUser()),
  219. ]);
  220. }
  221. }