<?php
namespace App\Controller;
use App\Entity\FirstQuestionList;
use App\Entity\FirstQuestionListEs;
use App\Entity\FirstQuestionListPr;
use App\Entity\MainQuestionEs;
use App\Entity\MainQuestionPt;
use App\Entity\SecondQuestionList;
use App\Entity\SecondQuestionListEs;
use App\Entity\SecondQuestionListPr;
use App\Entity\ThirdQuestionList;
use App\Entity\MainQuestion;
use App\Entity\ThirdQuestionListEs;
use App\Entity\ThirdQuestionListPr;
use App\Form\MainFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/{_locale<%app.supported_locales%>}/questionnaire', name: 'app-questionnaire-')]
class QuestionnaireController extends AbstractController
{
#[Route('/clear', name: 'clear')]
public function indexclear(Request $request, EntityManagerInterface $entityManager): Response
{
$session = $request->getSession();
$session->clear();
return $this->redirectToRoute('app-questionnaire-index');
}
#[Route('/', name: 'index')]
public function index(Request $request, EntityManagerInterface $entityManager): Response
{
$local = $request->getLocale(); // get la locale
$session = $request->getSession();
$session->clear();
switch ($local) {
case "fr":
$mainQuestion = $entityManager->getRepository(MainQuestion::class)->findAll();
break;
case "es":
$mainQuestion = $entityManager->getRepository(MainQuestionEs::class)->findAll();
break;
case "pt":
$mainQuestion = $entityManager->getRepository(MainQuestionPt::class)->findAll();
break;
default:
$mainQuestion = null;
}
$form = $this->createForm(MainFormType::class, ['selectedQuestion' => $mainQuestion[0], 'local' => $local, 'firstQuestion' => true]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$expertise = $form->getData()['expertise'];
$session->set('selectedPath', $expertise);
//redirect sur fonction index avec un argument ou on passe le selectedPath
return $this->redirectToRoute('app-questionnaire-path', ['selectedPath' => $expertise]);
}
return $this->renderForm('questionnaire/' . $local . '/index.html.twig', [
'controller_name' => 'QuestionnaireController',
'form' => $form,
'question' => $mainQuestion[0]->getContent(),
'selectedQuestionTips' => null,
'count' => $session->get("count"),
'countPercent' => null,
'countTotal' => 1,
'isFirst' => 1,
]);
}
#[Route('/{selectedPath}', name: 'path')]
public function indexPath($selectedPath, Request $request, EntityManagerInterface $entityManager): Response
{
$local = $request->getLocale(); // get la local
$session = $request->getSession();
$count = $session->has('count') ? (int)$session->get('count') : 0;
$questionList = null;
switch ($selectedPath) {
case 1 :
switch ($local) {
case "fr":
$questionList = $entityManager->getRepository(FirstQuestionList::class)->findAll();
break;
case "es":
$questionList = $entityManager->getRepository(FirstQuestionListEs::class)->findAll();
break;
case "pt":
$questionList = $entityManager->getRepository(FirstQuestionListPr::class)->findAll();
break;
}
break;
case 2 :
switch ($local) {
case "fr":
$questionList = $entityManager->getRepository(SecondQuestionList::class)->findAll();
break;
case "es":
$questionList = $entityManager->getRepository(SecondQuestionListEs::class)->findAll();
break;
case "pt":
$questionList = $entityManager->getRepository(SecondQuestionListPr::class)->findAll();
break;
}
break;
case 3 :
switch ($local) {
case "fr":
$questionList = $entityManager->getRepository(ThirdQuestionList::class)->findAll();
break;
case "es":
$questionList = $entityManager->getRepository(ThirdQuestionListEs::class)->findAll();
break;
case "pt":
$questionList = $entityManager->getRepository(ThirdQuestionListPr::class)->findAll();
break;
}
break;
}
$countPercent = (($count + 1) / (count($questionList) + 1)) * 100;
if ($count > count($questionList) - 1) {
return $this->redirectToRoute('app-results-index');
}
$selectedQuestion = $questionList[$count];
// Get les aides a la réponse dans un array '(initial encour avancé)
$selectedQuestionTips = [
$selectedQuestion->getInitial(),
$selectedQuestion->getEncours(),
$selectedQuestion->getAvance(),
];
$form = $this->createForm(MainFormType::class, ['selectedQuestion' => $selectedQuestion, 'local' => $local]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$session = $request->getSession();
$count = $session->has('count') ? (int)$session->get('count') : 0;
$count++;
$session->set('count', $count);
$expertise = ($form->getData()['expertise']);
$dimension = $selectedQuestion->getDimension();
$compArray = $session->get($dimension);
$compArray[] = $expertise;
$session->set($dimension, $compArray);
return $this->redirectToRoute('app-questionnaire-path', [
'selectedPath' => strval( $selectedPath ) ,
]);
}
return $this->renderForm('questionnaire/' . $local . '/index.html.twig', [
'selectedPath' => $selectedPath,
'controller_name' => 'QuestionnaireController',
'form' => $form,
'question' => $selectedQuestion->getContent(),
'selectedQuestionTips' => $selectedQuestionTips,
'count' => $session->get("count"),
'countPercent' => $countPercent,
'countTotal' => count($questionList),
]);
}
}