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\SurveyFilter; |
9
|
|
|
use AppBundle\Form\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
|
1 |
|
public function surveysAction(Request $request) |
25
|
|
|
{ |
26
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
27
|
1 |
|
$surveyTypes = $em->getRepository(SurveyType::class)->findAll(); |
28
|
1 |
|
$paginator = $this->get('knp_paginator'); |
29
|
1 |
|
$surveys = $em->getRepository(Survey::class)->findSurveyByStatus('submited'); |
30
|
1 |
|
$filter = new SurveyFilter(); |
31
|
1 |
|
$filter->setStart($surveys[count($surveys) - 1]->getUpdatedAt()); |
32
|
1 |
|
$filter->setEnd($surveys[0]->getUpdatedAt()); |
33
|
1 |
|
$form = $this->createForm(SurveyFilterType::class, $filter); |
34
|
1 |
|
$form->handleRequest($request); |
35
|
1 |
|
if ($form->isSubmitted() && $form->isValid()) { |
36
|
1 |
|
$surveys = $em->getRepository(Survey::class)->findSurveyByParams($filter); |
37
|
1 |
|
$pagination = $paginator->paginate($surveys, $request->query->getInt('page', 1), 10); |
38
|
|
|
|
39
|
|
|
return [ |
40
|
1 |
|
'surveys' => $pagination, 'survey_types' => $surveyTypes, |
41
|
1 |
|
'form' => $form->createView(), 'filter_type' => $filter->getType(), |
42
|
|
|
]; |
43
|
|
|
} |
44
|
1 |
|
$pagination = $paginator->paginate($surveys, $request->query->getInt('page', 1), 10); |
45
|
1 |
|
$types = array_fill_keys(['int', 'sp', 'ex', 'sr'], false); |
46
|
1 |
|
foreach ($pagination as $item) { |
47
|
1 |
|
if ($item->getType()->getName() == 'internship') { |
48
|
1 |
|
$types['int'] = true; |
49
|
|
|
} |
50
|
1 |
|
if ($item->getType()->getName() == 'speaker') { |
51
|
|
|
$types['sp'] = true; |
52
|
|
|
} |
53
|
1 |
|
if ($item->getType()->getName() == 'exit') { |
54
|
|
|
$types['ex'] = true; |
55
|
|
|
} |
56
|
1 |
|
if ($item->getType()->getName() == 'supervisor') { |
57
|
|
|
$types['sr'] = true; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return [ |
62
|
1 |
|
'surveys' => $pagination, 'survey_types' => $surveyTypes, 'types' => $types, 'form' => $form->createView(), |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Survey $survey |
68
|
|
|
* @Route("/surveys/{id}", name="survey") |
69
|
|
|
* @Template("@App/survey.html.twig") |
70
|
|
|
* @ParamConverter("survey", class="AppBundle\Entity\Survey\Survey") |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
1 |
|
public function surveyAction(Survey $survey) |
75
|
|
|
{ |
76
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
77
|
1 |
|
$answers = $em->getRepository(SurveyAnswer::class)->findAnswersBySurvey($survey); |
78
|
1 |
View Code Duplication |
if ($answers) { |
|
|
|
|
79
|
1 |
|
foreach ($answers as $answer) { |
80
|
1 |
|
$questions[] = $answer->getQuestion()->getId(); |
|
|
|
|
81
|
1 |
|
$contents[] = $answer->getContent(); |
|
|
|
|
82
|
|
|
} |
83
|
1 |
|
$questionAnswer = array_combine($questions, $contents); |
|
|
|
|
84
|
|
|
} |
85
|
1 |
|
if (!$answers) { |
86
|
|
|
$questionAnswer = null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return [ |
90
|
1 |
|
'survey' => $survey, 'question_answers' => $questionAnswer, |
|
|
|
|
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Request $request, SurveyType $surveyType |
|
|
|
|
96
|
|
|
* @Route("/surveys/create/{survey_type}", name="survey_create") |
97
|
|
|
* @ParamConverter("surveyType", options={"mapping": {"survey_type": "name"}}) |
98
|
|
|
*/ |
99
|
1 |
|
public function surveyCreateAction(Request $request, SurveyType $surveyType) |
100
|
|
|
{ |
101
|
1 |
|
$survey = new Survey(); |
102
|
1 |
|
$survey->setType($surveyType); |
103
|
1 |
|
$form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey); |
104
|
1 |
|
$form->handleRequest($request); |
105
|
|
|
|
106
|
1 |
View Code Duplication |
if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|
107
|
|
|
$em = $this->getDoctrine()->getManager(); |
108
|
|
|
$em->persist($survey); |
109
|
|
|
$em->flush(); |
110
|
|
|
|
111
|
|
|
return $this->redirectToRoute('surveys'); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $this->render('@App/surveyform.html.twig', array( |
115
|
1 |
|
'form' => $form->createView(), |
116
|
|
|
)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Request $request, Survey $survey |
|
|
|
|
121
|
|
|
* @Route("/surveys/delete/{id}", name="survey_delete") |
122
|
|
|
* @ParamConverter("survey", class="AppBundle\Entity\Survey\Survey") |
123
|
|
|
*/ |
124
|
1 |
|
public function surveyDeleteAction(Request $request, Survey $survey) |
125
|
|
|
{ |
126
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
127
|
1 |
|
$answers = $em->getRepository(SurveyAnswer::class)->findAnswersBySurvey($survey); |
128
|
1 |
View Code Duplication |
if ($answers) { |
|
|
|
|
129
|
1 |
|
foreach ($answers as $answer) { |
130
|
1 |
|
$questions[] = $answer->getQuestion()->getId(); |
|
|
|
|
131
|
1 |
|
$contents[] = $answer->getContent(); |
|
|
|
|
132
|
|
|
} |
133
|
1 |
|
$questionAnswer = array_combine($questions, $contents); |
|
|
|
|
134
|
|
|
} |
135
|
1 |
|
if (!$answers) { |
136
|
|
|
$questionAnswer = null; |
137
|
|
|
} |
138
|
1 |
|
$form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey); |
139
|
1 |
|
$form->handleRequest($request); |
140
|
|
|
|
141
|
1 |
View Code Duplication |
if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|
142
|
|
|
$em = $this->getDoctrine()->getManager(); |
143
|
|
|
$em->remove($survey); |
144
|
|
|
$em->flush(); |
145
|
|
|
|
146
|
|
|
return $this->redirectToRoute('surveys'); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return $this->render('@App/survey.html.twig', array( |
150
|
1 |
|
'form' => $form->createView(), 'survey' => $survey, 'question_answers' => $questionAnswer, |
|
|
|
|
151
|
|
|
)); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.