src/Controller/Front/Scholar/PracticalCase/ShowController.php line 140

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front\Scholar\PracticalCase;
  3. use App\Entity\Scholar\Lesson\Lesson;
  4. use App\Entity\Scholar\Module\Module;
  5. use App\Entity\Scholar\PracticalCase\PracticalCase;
  6. use App\Entity\Scholar\PracticalCase\PracticalCaseResponse;
  7. use App\Entity\Scholar\Training\Training;
  8. use App\Form\Scholar\PracticalCaseResponseType;
  9. use App\Repository\Scholar\PracticalCase\PracticalCaseUserRepository;
  10. use App\Service\ChannelUserData\CursusService;
  11. use App\Service\Scholar\LessonService;
  12. use App\Service\Scholar\PracticalCase\PracticalCaseManager;
  13. use App\Service\Scholar\PracticalCaseService;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Nellapp\Bundle\SDKBundle\Channel\Service\ConfigurationService;
  16. use Nellapp\Bundle\SDKBundle\Enum\Scholar\PracticalCaseUserStatusEnum;
  17. use Nellapp\Bundle\SDKBundle\Uploader\Service\NellappFileService;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. class ShowController extends AbstractController
  24. {
  25. public function __construct(
  26. private PracticalCaseUserRepository $practicalCaseUserRepository,
  27. private EntityManagerInterface $entityManager,
  28. private PracticalCaseService $practicalCaseService,
  29. private NellappFileService $nellappFileService,
  30. private LessonService $lessonService,
  31. private PracticalCaseManager $practicalCaseManager,
  32. private ConfigurationService $configurationService,
  33. private CursusService $cursusService,
  34. )
  35. {
  36. }
  37. #[Route(path: '/training/{trainingId}/module/{moduleId}/lesson/{lessonId}/practical-case/{practicalCaseId}', name: 'front_channel_practical_case_show')]
  38. #[Entity('training', options: ['id' => 'trainingId'])]
  39. #[Entity('module', options: ['id' => 'moduleId'])]
  40. #[Entity('lesson', options: ['id' => 'lessonId'])]
  41. #[Entity('practicalCase', options: ['id' => 'practicalCaseId'])]
  42. public function __invoke(Request $request, Training $training, Module $module, Lesson $lesson, PracticalCase $practicalCase): Response
  43. {
  44. if (!$this->isGranted('FRONT_ACCESS', [
  45. 'training' => $training,
  46. 'module' => $module,
  47. 'lesson' => $lesson,
  48. ])) {
  49. throw $this->createAccessDeniedException();
  50. }
  51. $channel = $training->getOwnerChannel();
  52. $enableCustomTime = $this->configurationService->isEnableCustomTimeOnPracticalCase($channel);
  53. $practicalCaseUser = $this->practicalCaseUserRepository->findByPracticalCaseAndUser($practicalCase, $this->getUser());
  54. if (!$practicalCaseUser) {
  55. $practicalCaseUser = $this->practicalCaseManager->createPracticalCaseUser($channel, $practicalCase, $training, $module, $this->getUser());
  56. }
  57. $practicalCaseResponses = $practicalCaseUser->getPracticalCaseResponses();
  58. $practicalCaseLastResponse = null;
  59. $withErrors = false;
  60. if (!$practicalCaseResponses->isEmpty()) {
  61. $status = $this->practicalCaseService->getPracticalCaseUserStatus($practicalCaseUser);
  62. /** @var PracticalCaseResponse $practicalCaseLastResponse */
  63. $practicalCaseLastResponse = $practicalCaseResponses->last();
  64. if ($status === PracticalCaseUserStatusEnum::WAITING) {
  65. $form = $this->createForm(PracticalCaseResponseType::class, $practicalCaseLastResponse, [
  66. 'enableCustomTime' => $enableCustomTime,
  67. ]);
  68. } else if ($status === PracticalCaseUserStatusEnum::REFUSED) {
  69. $newPracticalCaseResponse = $this->practicalCaseManager->createPracticalCaseResponse($channel, $practicalCaseUser, $practicalCaseLastResponse->getAssignedTo());
  70. $form = $this->createForm(PracticalCaseResponseType::class, $newPracticalCaseResponse, [
  71. 'enableCustomTime' => $enableCustomTime,
  72. ]);
  73. } else {
  74. $form = null;
  75. }
  76. } else {
  77. $newPracticalCaseResponse = $this->practicalCaseManager->createPracticalCaseResponse($channel, $practicalCaseUser, $this->lessonService->getLessonReferralTrainer($lesson));
  78. $form = $this->createForm(PracticalCaseResponseType::class, $newPracticalCaseResponse, [
  79. 'enableCustomTime' => $enableCustomTime,
  80. ]);
  81. }
  82. if ($form) {
  83. $form->handleRequest($request);
  84. if ($form->isSubmitted() && $form->isValid()) {
  85. /** @var PracticalCaseResponse $practicalCaseResponseSubmitted */
  86. $practicalCaseResponseSubmitted = $form->getData();
  87. $practicalCaseUser->addPracticalCaseResponse($practicalCaseResponseSubmitted);
  88. if ($filePracticalCaseResponse = $practicalCaseResponseSubmitted->getFile()) {
  89. if (!empty($filePracticalCaseResponse->getFilename())) {
  90. try {
  91. $this->nellappFileService->createNellappFile($filePracticalCaseResponse);
  92. } catch (\Exception) {
  93. $practicalCaseResponseSubmitted->setFile(null);
  94. }
  95. } else {
  96. $this->entityManager->remove($filePracticalCaseResponse);
  97. $practicalCaseResponseSubmitted->setFile(null);
  98. }
  99. } else {
  100. $practicalCaseResponseSubmitted->setFile(null);
  101. }
  102. if ($practicalCase->isAskForReferentCorrection()) {
  103. $practicalCaseResponseSubmitted->setAssignableStatusTodo();
  104. } else {
  105. $practicalCaseResponseSubmitted->setAssignableStatusDone();
  106. }
  107. $practicalCaseResponseSubmitted->setStatus(0);
  108. $this->entityManager->persist($practicalCaseUser);
  109. $this->entityManager->flush();
  110. if ($practicalCaseResponseSubmitted->getFile() !== null) {
  111. try {
  112. $this->nellappFileService->uploadWaitingFile($practicalCaseResponseSubmitted->getFile());
  113. } catch (\Exception) {
  114. }
  115. }
  116. return $this->redirectToRoute('front_channel_practical_case_show', [
  117. 'practicalCaseId' => $practicalCase->getId(),
  118. 'lessonId' => $lesson->getId(),
  119. 'moduleId' => $module->getId(),
  120. 'trainingId' => $training->getId(),
  121. ]);
  122. } else if ($form->isSubmitted()) {
  123. $withErrors = true;
  124. }
  125. }
  126. return $this->render('Front/Scholar/PracticalCase/show.html.twig', [
  127. 'role' => 'learner',
  128. 'training' => $training,
  129. 'module' => $module,
  130. 'lesson' => $lesson,
  131. 'practicalCase' => $practicalCase,
  132. 'practicalCaseResponses' => $practicalCaseResponses,
  133. 'practicalCaseLastResponse' => $practicalCaseLastResponse,
  134. 'form' => $form?->createView(),
  135. 'canSeeCorrection' => $this->practicalCaseService->isDone($practicalCaseUser),
  136. 'sideNavOpened' => true,
  137. 'formErrors' => $withErrors,
  138. 'cursus' => $this->cursusService->getCursusByTraining($training, $this->getUser()),
  139. ]);
  140. }
  141. }