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 |
|
if ($survey->isReviewed() == false) { |
|
|
|
|
78
|
1 |
|
$survey->setReviewed(true); |
79
|
1 |
|
$em->persist($survey); |
80
|
1 |
|
$em->flush(); |
81
|
|
|
} |
82
|
1 |
|
$answers = $em->getRepository(SurveyAnswer::class)->findAnswersBySurvey($survey); |
83
|
1 |
View Code Duplication |
if ($answers) { |
|
|
|
|
84
|
1 |
|
foreach ($answers as $answer) { |
85
|
1 |
|
$questions[] = $answer->getQuestion()->getId(); |
|
|
|
|
86
|
1 |
|
$contents[] = $answer->getContent(); |
|
|
|
|
87
|
|
|
} |
88
|
1 |
|
$questionAnswer = array_combine($questions, $contents); |
|
|
|
|
89
|
|
|
} |
90
|
1 |
|
if (!$answers) { |
91
|
|
|
$questionAnswer = null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return [ |
95
|
1 |
|
'survey' => $survey, 'question_answers' => $questionAnswer, |
|
|
|
|
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Request $request, SurveyType $surveyType |
|
|
|
|
101
|
|
|
* @Route("/surveys/create/{survey_type}", name="survey_create") |
102
|
|
|
* @ParamConverter("surveyType", options={"mapping": {"survey_type": "name"}}) |
103
|
|
|
*/ |
104
|
1 |
|
public function surveyCreateAction(Request $request, SurveyType $surveyType) |
105
|
|
|
{ |
106
|
1 |
|
$survey = new Survey(); |
107
|
1 |
|
$survey->setType($surveyType); |
108
|
1 |
|
$form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey); |
109
|
1 |
|
$form->handleRequest($request); |
110
|
|
|
|
111
|
1 |
View Code Duplication |
if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|
112
|
|
|
$em = $this->getDoctrine()->getManager(); |
113
|
|
|
$em->persist($survey); |
114
|
|
|
$em->flush(); |
115
|
|
|
|
116
|
|
|
return $this->redirectToRoute('surveys'); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return $this->render('@App/surveyform.html.twig', array( |
120
|
1 |
|
'form' => $form->createView(), |
121
|
|
|
)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Request $request, Survey $survey |
|
|
|
|
126
|
|
|
* @Route("/surveys/delete/{id}", requirements={"id": "\d+"}, name="survey_delete") |
127
|
|
|
* @ParamConverter("survey", class="AppBundle\Entity\Survey\Survey") |
128
|
|
|
*/ |
129
|
1 |
|
public function surveyDeleteAction(Request $request, Survey $survey) |
130
|
|
|
{ |
131
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
132
|
1 |
|
$answers = $em->getRepository(SurveyAnswer::class)->findAnswersBySurvey($survey); |
133
|
1 |
View Code Duplication |
if ($answers) { |
|
|
|
|
134
|
1 |
|
foreach ($answers as $answer) { |
135
|
1 |
|
$questions[] = $answer->getQuestion()->getId(); |
|
|
|
|
136
|
1 |
|
$contents[] = $answer->getContent(); |
|
|
|
|
137
|
|
|
} |
138
|
1 |
|
$questionAnswer = array_combine($questions, $contents); |
|
|
|
|
139
|
|
|
} |
140
|
1 |
|
if (!$answers) { |
141
|
|
|
$questionAnswer = null; |
142
|
|
|
} |
143
|
1 |
|
$form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey); |
144
|
1 |
|
$form->handleRequest($request); |
145
|
|
|
|
146
|
1 |
View Code Duplication |
if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|
147
|
|
|
$em = $this->getDoctrine()->getManager(); |
148
|
|
|
$em->remove($survey); |
149
|
|
|
$em->flush(); |
150
|
|
|
|
151
|
|
|
return $this->redirectToRoute('surveys'); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return $this->render('@App/survey.html.twig', array( |
155
|
1 |
|
'form' => $form->createView(), 'survey' => $survey, 'question_answers' => $questionAnswer, |
|
|
|
|
156
|
|
|
)); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.