src/Controller/QuestionnaireController.php line 36

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. //            todo : here -> choose questionnaire !
  56.             return $this->redirectToRoute('questionnaire_answer', ['position' => 1'questionnaire' => $respondent->getRank()->getId()]);
  57.         }
  58.         return $this->render('questionnaire/start.html.twig', [
  59.             'form' => $form->createView()
  60.         ]);
  61.     }
  62.     /**
  63.      * Affiche la question en position $position pour le rĂ©pondant en session
  64.      * @Route("/questionnaire/repondre/{position}/{questionnaire}", name="questionnaire_answer")
  65.      */
  66.     public function answer(Request $request$position null$questionnaire 1): Response
  67.     {
  68.         $question $this->doctrine->getRepository(Question::class)->findOneBy([
  69.             'position' => $position,
  70.             'questionnaire' => $questionnaire
  71.         ]);
  72.         $nbTotalQuestions $this->doctrine->getRepository(Question::class)->count([]);
  73.         $session $this->requestStack->getSession();
  74.         $respondentId $session->get('respondentId');
  75.         $respondent $this->doctrine->getRepository(Respondent::class)->find($respondentId);
  76.         $answer $this->doctrine->getRepository(Answer::class)->findOneByQuestionAndRespondant($question$respondent);
  77.         if(null === $answer){
  78.             $answer = new Answer();
  79.             $answer->setRespondent($respondent);
  80.         }
  81.         $form $this->createForm(AnswerType::class, $answer, ['question' => $question]);
  82.         $form->handleRequest($request);
  83.         if ($form->isSubmitted() && $form->isValid()) {
  84.             $entityManager $this->doctrine->getManager();
  85.             $entityManager->persist($answer);
  86.             $entityManager->flush();
  87.             if($position == $nbTotalQuestions){
  88.                 $respondent->setEndTime(new \DateTimeImmutable());
  89.                 $entityManager->persist($respondent);
  90.                 $entityManager->flush();
  91.                 return $this->redirectToRoute('results');
  92.             }
  93.             return $this->redirectToRoute('questionnaire_answer', ['position' => $position+1]);
  94.         }
  95.         return $this->render('questionnaire/question.html.twig', [
  96.             'question' => $question,
  97.             'position' => $position,
  98.             'nbTotalQuestions' => $nbTotalQuestions,
  99.             'form' => $form->createView()
  100.         ]);
  101.     }
  102. }