src/Controller/QuestionnaireController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\FirstQuestionList;
  4. use App\Entity\FirstQuestionListEs;
  5. use App\Entity\FirstQuestionListPr;
  6. use App\Entity\MainQuestionEs;
  7. use App\Entity\MainQuestionPt;
  8. use App\Entity\SecondQuestionList;
  9. use App\Entity\SecondQuestionListEs;
  10. use App\Entity\SecondQuestionListPr;
  11. use App\Entity\ThirdQuestionList;
  12. use App\Entity\MainQuestion;
  13. use App\Entity\ThirdQuestionListEs;
  14. use App\Entity\ThirdQuestionListPr;
  15. use App\Form\MainFormType;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. #[Route('/{_locale<%app.supported_locales%>}/questionnaire'name'app-questionnaire-')]
  22. class QuestionnaireController extends AbstractController
  23. {
  24.     #[Route('/clear'name'clear')]
  25.     public function indexclear(Request $requestEntityManagerInterface $entityManager): Response
  26.     {
  27.         $session $request->getSession();
  28.         $session->clear();
  29.         return $this->redirectToRoute('app-questionnaire-index');
  30.     }
  31.     #[Route('/'name'index')]
  32.     public function index(Request $requestEntityManagerInterface $entityManager): Response
  33.     {
  34.         $local $request->getLocale(); // get la locale
  35.         $session $request->getSession();
  36.         $session->clear();
  37.         switch ($local) {
  38.             case "fr":
  39.                 $mainQuestion $entityManager->getRepository(MainQuestion::class)->findAll();
  40.                 break;
  41.             case "es":
  42.                 $mainQuestion $entityManager->getRepository(MainQuestionEs::class)->findAll();
  43.                 break;
  44.             case "pt":
  45.                 $mainQuestion $entityManager->getRepository(MainQuestionPt::class)->findAll();
  46.                 break;
  47.             default:
  48.                 $mainQuestion null;
  49.         }
  50.         $form $this->createForm(MainFormType::class, ['selectedQuestion' => $mainQuestion[0], 'local' => $local'firstQuestion' => true]);
  51.         $form->handleRequest($request);
  52.         if ($form->isSubmitted() && $form->isValid()) {
  53.             $expertise $form->getData()['expertise'];
  54.             $session->set('selectedPath'$expertise);
  55.             //redirect sur fonction index avec un argument ou on passe le selectedPath
  56.             return $this->redirectToRoute('app-questionnaire-path', ['selectedPath' => $expertise]);
  57.         }
  58.         return $this->renderForm('questionnaire/' $local '/index.html.twig', [
  59.             'controller_name' => 'QuestionnaireController',
  60.             'form' => $form,
  61.             'question' => $mainQuestion[0]->getContent(),
  62.             'selectedQuestionTips' => null,
  63.             'count' => $session->get("count"),
  64.             'countPercent' => null,
  65.             'countTotal' => 1,
  66.             'isFirst' => 1,
  67.         ]);
  68.     }
  69.     #[Route('/{selectedPath}'name'path')]
  70.     public function indexPath($selectedPathRequest $requestEntityManagerInterface $entityManager): Response
  71.     {
  72.         $local $request->getLocale(); // get la local
  73.         $session $request->getSession();
  74.         $count $session->has('count') ? (int)$session->get('count') : 0;
  75.         $questionList null;
  76.         switch ($selectedPath) {
  77.             case :
  78.                 switch ($local) {
  79.                     case "fr":
  80.                         $questionList $entityManager->getRepository(FirstQuestionList::class)->findAll();
  81.                         break;
  82.                     case "es":
  83.                         $questionList $entityManager->getRepository(FirstQuestionListEs::class)->findAll();
  84.                         break;
  85.                     case "pt":
  86.                         $questionList $entityManager->getRepository(FirstQuestionListPr::class)->findAll();
  87.                         break;
  88.                 }
  89.                 break;
  90.             case :
  91.                 switch ($local) {
  92.                     case "fr":
  93.                         $questionList $entityManager->getRepository(SecondQuestionList::class)->findAll();
  94.                         break;
  95.                     case "es":
  96.                         $questionList $entityManager->getRepository(SecondQuestionListEs::class)->findAll();
  97.                         break;
  98.                     case "pt":
  99.                         $questionList $entityManager->getRepository(SecondQuestionListPr::class)->findAll();
  100.                         break;
  101.                 }
  102.                 break;
  103.             case :
  104.                 switch ($local) {
  105.                     case "fr":
  106.                         $questionList $entityManager->getRepository(ThirdQuestionList::class)->findAll();
  107.                         break;
  108.                     case "es":
  109.                         $questionList $entityManager->getRepository(ThirdQuestionListEs::class)->findAll();
  110.                         break;
  111.                     case "pt":
  112.                         $questionList $entityManager->getRepository(ThirdQuestionListPr::class)->findAll();
  113.                         break;
  114.                 }
  115.                 break;
  116.         }
  117.         $countPercent = (($count 1) / (count($questionList) + 1)) * 100;
  118.         if ($count count($questionList) - 1) {
  119.             return $this->redirectToRoute('app-results-index');
  120.         }
  121.         $selectedQuestion $questionList[$count];
  122.         // Get les aides a la rĂ©ponse dans un array '(initial encour avancĂ©)
  123.         $selectedQuestionTips = [
  124.             $selectedQuestion->getInitial(),
  125.             $selectedQuestion->getEncours(),
  126.             $selectedQuestion->getAvance(),
  127.         ];
  128.         $form $this->createForm(MainFormType::class, ['selectedQuestion' => $selectedQuestion'local' => $local]);
  129.         $form->handleRequest($request);
  130.         if ($form->isSubmitted() && $form->isValid()) {
  131.             $session $request->getSession();
  132.             $count $session->has('count') ? (int)$session->get('count') : 0;
  133.             $count++;
  134.             $session->set('count'$count);
  135.             $expertise = ($form->getData()['expertise']);
  136.             $dimension $selectedQuestion->getDimension();
  137.             $compArray $session->get($dimension);
  138.             $compArray[] = $expertise;
  139.             $session->set($dimension$compArray);
  140.             return $this->redirectToRoute('app-questionnaire-path', [
  141.                 'selectedPath' => strval$selectedPath ) ,
  142.             ]);
  143.         }
  144.         return $this->renderForm('questionnaire/' $local '/index.html.twig', [
  145.             'selectedPath' => $selectedPath,
  146.             'controller_name' => 'QuestionnaireController',
  147.             'form' => $form,
  148.             'question' => $selectedQuestion->getContent(),
  149.             'selectedQuestionTips' => $selectedQuestionTips,
  150.             'count' => $session->get("count"),
  151.             'countPercent' => $countPercent,
  152.             'countTotal' => count($questionList),
  153.         ]);
  154.     }
  155. }