src/Controller/QuestionnaireController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Answer;
  4. use App\Entity\Question;
  5. use App\Entity\Rank;
  6. use App\Entity\Respondent;
  7. use App\Form\AnswerType;
  8. use App\Form\RespondentType;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class QuestionnaireController extends AbstractController
  16. {
  17.     private $doctrine;
  18.     private $requestStack;
  19.     public function __construct(
  20.         ManagerRegistry $doctrine,
  21.         RequestStack $requestStack
  22.     )
  23.     {
  24.         $this->doctrine $doctrine;
  25.         $this->requestStack $requestStack;
  26.     }
  27.     /**
  28.      * Affiche la page d'intro du questionnaire : formulaire profil du rĂ©pondent en session
  29.      * @Route("/questionnaire/intro", name="questionnaire_start")
  30.      */
  31.     public function start(Request $request): Response
  32.     {
  33.         $session $this->requestStack->getSession();
  34.         $respondentId $session->get('respondentId');
  35.         $respondent null;
  36.         if($respondentId){
  37.             $respondent $this->doctrine->getRepository(Respondent::class)->find($respondentId);
  38.         }
  39.         if(null === $respondent){
  40.             $respondent = new Respondent();
  41.             $rank $this->doctrine->getRepository(Rank::class)->findAll()[0];
  42.             $respondent->setRank($rank);
  43.         }
  44.         $form $this->createForm(RespondentType::class, $respondent);
  45.         $form->handleRequest($request);
  46.         if ($form->isSubmitted() && $form->isValid()) {
  47.             $entityManager $this->doctrine->getManager();
  48.             if(null === $respondent->getStartTime()){
  49.                 $respondent->setStartTime(new \DateTimeImmutable());
  50.             }
  51.             $entityManager->persist($respondent);
  52.             $entityManager->flush();
  53.             $session $this->requestStack->getSession();
  54.             $session->set('respondentId'$respondent->getId());
  55.             return $this->redirectToRoute('questionnaire_answer', ['position' => 1]);
  56.         }
  57.         return $this->render('questionnaire/start.html.twig', [
  58.             'form' => $form->createView()
  59.         ]);
  60.     }
  61.     /**
  62.      * Affiche la question en position $position pour le rĂ©pondant en session
  63.      * @Route("/questionnaire/repondre/{position}", name="questionnaire_answer")
  64.      */
  65.     public function answer(Request $request$position null): Response
  66.     {
  67.         $question $this->doctrine->getRepository(Question::class)->findOneByPosition($position);
  68.         $nbTotalQuestions $this->doctrine->getRepository(Question::class)->count([]);
  69.         $session $this->requestStack->getSession();
  70.         $respondentId $session->get('respondentId');
  71.         $respondent $this->doctrine->getRepository(Respondent::class)->find($respondentId);
  72.         $answer $this->doctrine->getRepository(Answer::class)->findOneByQuestionAndRespondant($question$respondent);
  73.         if(null === $answer){
  74.             $answer = new Answer();
  75.             $answer->setRespondent($respondent);
  76.         }
  77.         $form $this->createForm(AnswerType::class, $answer, ['question' => $question]);
  78.         $form->handleRequest($request);
  79.         if ($form->isSubmitted() && $form->isValid()) {
  80.             $entityManager $this->doctrine->getManager();
  81.             $entityManager->persist($answer);
  82.             $entityManager->flush();
  83.             if($position == $nbTotalQuestions){
  84.                 $respondent->setEndTime(new \DateTimeImmutable());
  85.                 $entityManager->persist($respondent);
  86.                 $entityManager->flush();
  87.                 return $this->redirectToRoute('results');
  88.             }
  89.             return $this->redirectToRoute('questionnaire_answer', ['position' => $position+1]);
  90.         }
  91.         return $this->render('questionnaire/question.html.twig', [
  92.             'question' => $question,
  93.             'position' => $position,
  94.             'nbTotalQuestions' => $nbTotalQuestions,
  95.             'form' => $form->createView()
  96.         ]);
  97.     }
  98. }