<?php
namespace App\Controller\Front\Scholar\PracticalCase;
use App\Entity\Scholar\Lesson\Lesson;
use App\Entity\Scholar\Module\Module;
use App\Entity\Scholar\PracticalCase\PracticalCase;
use App\Entity\Scholar\PracticalCase\PracticalCaseResponse;
use App\Entity\Scholar\Training\Training;
use App\Form\Scholar\PracticalCaseResponseType;
use App\Repository\Scholar\PracticalCase\PracticalCaseUserRepository;
use App\Service\ChannelUserData\CursusService;
use App\Service\Scholar\LessonService;
use App\Service\Scholar\PracticalCase\PracticalCaseManager;
use App\Service\Scholar\PracticalCaseService;
use Doctrine\ORM\EntityManagerInterface;
use Nellapp\Bundle\SDKBundle\Channel\Service\ConfigurationService;
use Nellapp\Bundle\SDKBundle\Enum\Scholar\PracticalCaseUserStatusEnum;
use Nellapp\Bundle\SDKBundle\Uploader\Service\NellappFileService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ShowController extends AbstractController
{
public function __construct(
private PracticalCaseUserRepository $practicalCaseUserRepository,
private EntityManagerInterface $entityManager,
private PracticalCaseService $practicalCaseService,
private NellappFileService $nellappFileService,
private LessonService $lessonService,
private PracticalCaseManager $practicalCaseManager,
private ConfigurationService $configurationService,
private CursusService $cursusService,
)
{
}
#[Route(path: '/training/{trainingId}/module/{moduleId}/lesson/{lessonId}/practical-case/{practicalCaseId}', name: 'front_channel_practical_case_show')]
#[Entity('training', options: ['id' => 'trainingId'])]
#[Entity('module', options: ['id' => 'moduleId'])]
#[Entity('lesson', options: ['id' => 'lessonId'])]
#[Entity('practicalCase', options: ['id' => 'practicalCaseId'])]
public function __invoke(Request $request, Training $training, Module $module, Lesson $lesson, PracticalCase $practicalCase): Response
{
if (!$this->isGranted('FRONT_ACCESS', [
'training' => $training,
'module' => $module,
'lesson' => $lesson,
])) {
throw $this->createAccessDeniedException();
}
$channel = $training->getOwnerChannel();
$enableCustomTime = $this->configurationService->isEnableCustomTimeOnPracticalCase($channel);
$practicalCaseUser = $this->practicalCaseUserRepository->findByPracticalCaseAndUser($practicalCase, $this->getUser());
if (!$practicalCaseUser) {
$practicalCaseUser = $this->practicalCaseManager->createPracticalCaseUser($channel, $practicalCase, $training, $module, $this->getUser());
}
$practicalCaseResponses = $practicalCaseUser->getPracticalCaseResponses();
$practicalCaseLastResponse = null;
$withErrors = false;
if (!$practicalCaseResponses->isEmpty()) {
$status = $this->practicalCaseService->getPracticalCaseUserStatus($practicalCaseUser);
/** @var PracticalCaseResponse $practicalCaseLastResponse */
$practicalCaseLastResponse = $practicalCaseResponses->last();
if ($status === PracticalCaseUserStatusEnum::WAITING) {
$form = $this->createForm(PracticalCaseResponseType::class, $practicalCaseLastResponse, [
'enableCustomTime' => $enableCustomTime,
]);
} else if ($status === PracticalCaseUserStatusEnum::REFUSED) {
$newPracticalCaseResponse = $this->practicalCaseManager->createPracticalCaseResponse($channel, $practicalCaseUser, $practicalCaseLastResponse->getAssignedTo());
$form = $this->createForm(PracticalCaseResponseType::class, $newPracticalCaseResponse, [
'enableCustomTime' => $enableCustomTime,
]);
} else {
$form = null;
}
} else {
$newPracticalCaseResponse = $this->practicalCaseManager->createPracticalCaseResponse($channel, $practicalCaseUser, $this->lessonService->getLessonReferralTrainer($lesson));
$form = $this->createForm(PracticalCaseResponseType::class, $newPracticalCaseResponse, [
'enableCustomTime' => $enableCustomTime,
]);
}
if ($form) {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var PracticalCaseResponse $practicalCaseResponseSubmitted */
$practicalCaseResponseSubmitted = $form->getData();
$practicalCaseUser->addPracticalCaseResponse($practicalCaseResponseSubmitted);
if ($filePracticalCaseResponse = $practicalCaseResponseSubmitted->getFile()) {
if (!empty($filePracticalCaseResponse->getFilename())) {
try {
$this->nellappFileService->createNellappFile($filePracticalCaseResponse);
} catch (\Exception) {
$practicalCaseResponseSubmitted->setFile(null);
}
} else {
$this->entityManager->remove($filePracticalCaseResponse);
$practicalCaseResponseSubmitted->setFile(null);
}
} else {
$practicalCaseResponseSubmitted->setFile(null);
}
if ($practicalCase->isAskForReferentCorrection()) {
$practicalCaseResponseSubmitted->setAssignableStatusTodo();
} else {
$practicalCaseResponseSubmitted->setAssignableStatusDone();
}
$practicalCaseResponseSubmitted->setStatus(0);
$this->entityManager->persist($practicalCaseUser);
$this->entityManager->flush();
if ($practicalCaseResponseSubmitted->getFile() !== null) {
try {
$this->nellappFileService->uploadWaitingFile($practicalCaseResponseSubmitted->getFile());
} catch (\Exception) {
}
}
return $this->redirectToRoute('front_channel_practical_case_show', [
'practicalCaseId' => $practicalCase->getId(),
'lessonId' => $lesson->getId(),
'moduleId' => $module->getId(),
'trainingId' => $training->getId(),
]);
} else if ($form->isSubmitted()) {
$withErrors = true;
}
}
return $this->render('Front/Scholar/PracticalCase/show.html.twig', [
'role' => 'learner',
'training' => $training,
'module' => $module,
'lesson' => $lesson,
'practicalCase' => $practicalCase,
'practicalCaseResponses' => $practicalCaseResponses,
'practicalCaseLastResponse' => $practicalCaseLastResponse,
'form' => $form?->createView(),
'canSeeCorrection' => $this->practicalCaseService->isDone($practicalCaseUser),
'sideNavOpened' => true,
'formErrors' => $withErrors,
'cursus' => $this->cursusService->getCursusByTraining($training, $this->getUser()),
]);
}
}