- <?php
- namespace App\Controller;
- use App\Entity\Answer;
- use App\Entity\Question;
- use App\Entity\Rank;
- use App\Entity\Respondent;
- use App\Form\AnswerType;
- use App\Form\RespondentType;
- use Doctrine\Persistence\ManagerRegistry;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Routing\Annotation\Route;
- class QuestionnaireController extends AbstractController
- {
-     private $doctrine;
-     private $requestStack;
-     public function __construct(
-         ManagerRegistry $doctrine,
-         RequestStack $requestStack
-     )
-     {
-         $this->doctrine = $doctrine;
-         $this->requestStack = $requestStack;
-     }
-     /**
-      * Affiche la page d'intro du questionnaire : formulaire profil du rĂ©pondent en session
-      * @Route("/questionnaire/intro", name="questionnaire_start")
-      */
-     public function start(Request $request): Response
-     {
-         $session = $this->requestStack->getSession();
-         $respondentId = $session->get('respondentId');
-         $respondent = null;
-         if($respondentId){
-             $respondent = $this->doctrine->getRepository(Respondent::class)->find($respondentId);
-         }
-         if(null === $respondent){
-             $respondent = new Respondent();
-             $rank = $this->doctrine->getRepository(Rank::class)->findAll()[0];
-             $respondent->setRank($rank);
-         }
-         $form = $this->createForm(RespondentType::class, $respondent);
-         $form->handleRequest($request);
-         if ($form->isSubmitted() && $form->isValid()) {
-             $entityManager = $this->doctrine->getManager();
-             if(null === $respondent->getStartTime()){
-                 $respondent->setStartTime(new \DateTimeImmutable());
-             }
-             $entityManager->persist($respondent);
-             $entityManager->flush();
-             $session = $this->requestStack->getSession();
-             $session->set('respondentId', $respondent->getId());
-             return $this->redirectToRoute('questionnaire_answer', ['position' => 1]);
-         }
-         return $this->render('questionnaire/start.html.twig', [
-             'form' => $form->createView()
-         ]);
-     }
-     /**
-      * Affiche la question en position $position pour le rĂ©pondant en session
-      * @Route("/questionnaire/repondre/{position}", name="questionnaire_answer")
-      */
-     public function answer(Request $request, $position = null): Response
-     {
-         $question = $this->doctrine->getRepository(Question::class)->findOneByPosition($position);
-         $nbTotalQuestions = $this->doctrine->getRepository(Question::class)->count([]);
-         $session = $this->requestStack->getSession();
-         $respondentId = $session->get('respondentId');
-         $respondent = $this->doctrine->getRepository(Respondent::class)->find($respondentId);
-         $answer = $this->doctrine->getRepository(Answer::class)->findOneByQuestionAndRespondant($question, $respondent);
-         if(null === $answer){
-             $answer = new Answer();
-             $answer->setRespondent($respondent);
-         }
-         $form = $this->createForm(AnswerType::class, $answer, ['question' => $question]);
-         $form->handleRequest($request);
-         if ($form->isSubmitted() && $form->isValid()) {
-             $entityManager = $this->doctrine->getManager();
-             $entityManager->persist($answer);
-             $entityManager->flush();
-             if($position == $nbTotalQuestions){
-                 $respondent->setEndTime(new \DateTimeImmutable());
-                 $entityManager->persist($respondent);
-                 $entityManager->flush();
-                 return $this->redirectToRoute('results');
-             }
-             return $this->redirectToRoute('questionnaire_answer', ['position' => $position+1]);
-         }
-         return $this->render('questionnaire/question.html.twig', [
-             'question' => $question,
-             'position' => $position,
-             'nbTotalQuestions' => $nbTotalQuestions,
-             'form' => $form->createView()
-         ]);
-     }
- }
-