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

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