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

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