| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace AppBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use AppBundle\Entity\Survey\SurveyAnswer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use AppBundle\Entity\Survey\SurveyType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use AppBundle\Entity\Survey\Survey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use AppBundle\Entity\DTO\Filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use AppBundle\Form\DTO\SurveyFilterType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class SurveyController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @Route("/surveys", name="surveys") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @Template("@App/surveys.html.twig") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 2 |  |     public function surveysAction(Request $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 2 |  |         $em = $this->getDoctrine()->getManager(); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 2 |  |         $filter = new Filter(); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 2 |  |         $filterForm = $this->createForm(SurveyFilterType::class, $filter); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 2 |  |         $filterForm->handleRequest($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 2 |  |         $surveys = $em->getRepository(Survey::class)->selectSurveysByParams($filter); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 2 |  |         $types = $em->getRepository(SurveyType::class)->selectSurveyTypesByParams($filter); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 2 |  |             'surveys' => $this->get('knp_paginator') | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 2 |  |                 ->paginate( | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                     $surveys, | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 2 |  |                     $request->query->getInt('page', 1), | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 2 |  |                     20 | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                 ), | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 2 |  |             'types' => $types, | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 2 |  |             'filterForm' => $filterForm->createView(), | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param Survey $survey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @Route("/surveys/{id}", name="survey") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @Template("@App/survey.html.twig") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @ParamConverter("survey", class="AppBundle\Entity\Survey\Survey") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |     public function surveyAction(Survey $survey) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |         $em = $this->getDoctrine()->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         if ($survey->isReviewed() == false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |             $survey->setReviewed(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |             $em->persist($survey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |             $em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         $answers = $em->getRepository(SurveyAnswer::class)->findAnswersBySurvey($survey); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 64 | 1 | View Code Duplication |         if ($answers) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             foreach ($answers as $answer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |                 $questions[] = $answer->getQuestion()->getId(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |                 $contents[] = $answer->getContent(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |             $questionAnswer = array_combine($questions, $contents); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         if (!$answers) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $questionAnswer = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             'survey' => $survey, 'question_answers' => $questionAnswer, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param Request $request, SurveyType $surveyType | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @Route("/surveys/create/{survey_type}", name="survey_create") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @ParamConverter("surveyType", options={"mapping": {"survey_type": "name"}}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |     public function surveyCreateAction(Request $request, SurveyType $surveyType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         $survey = new Survey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         $survey->setType($surveyType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |         $form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         $form->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 92 | 1 | View Code Duplication |         if ($form->isSubmitted() && $form->isValid()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |             $em = $this->getDoctrine()->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             $em->persist($survey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |             $em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |             return $this->redirectToRoute('surveys'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |         return $this->render('@App/survey.html.twig', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |             'form' => $form->createView(), 'survey' => $survey, 'question_answers' => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param Request $request, Survey $survey | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @Route("/surveys/delete/{id}", requirements={"id": "\d+"}, name="survey_delete") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @ParamConverter("survey", class="AppBundle\Entity\Survey\Survey") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |     public function surveyDeleteAction(Request $request, Survey $survey) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         $em = $this->getDoctrine()->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         $answers = $em->getRepository(SurveyAnswer::class)->findAnswersBySurvey($survey); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 114 | 1 | View Code Duplication |         if ($answers) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |             foreach ($answers as $answer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |                 $questions[] = $answer->getQuestion()->getId(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |                 $contents[] = $answer->getContent(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |             $questionAnswer = array_combine($questions, $contents); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |         if (!$answers) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $questionAnswer = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |         $form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |         $form->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 127 | 1 | View Code Duplication |         if ($form->isSubmitted() && $form->isValid()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |             $em = $this->getDoctrine()->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |             $em->remove($survey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |             $em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |             return $this->redirectToRoute('surveys'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |         return $this->render('@App/survey.html.twig', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |             'form' => $form->createView(), 'survey' => $survey, 'question_answers' => $questionAnswer, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  | 
            
                        
When comparing two booleans, it is generally considered safer to use the strict comparison operator.