1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Api; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\SurveyAnswer; |
6
|
|
|
use AppBundle\Entity\Survey; |
7
|
|
|
use AppBundle\Entity\SurveyQuestion; |
8
|
|
|
use Mcfedr\JsonFormBundle\Controller\JsonController; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
class SurveyController extends JsonController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @Route("/surveys", name="api_surveys") |
18
|
|
|
* @Method("GET") |
19
|
|
|
*/ |
20
|
2 |
|
public function apiSurveysAction() |
21
|
|
|
{ |
22
|
2 |
|
$user = $this->getUser(); |
23
|
2 |
|
if (!$user) { |
24
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
25
|
|
|
} |
26
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
27
|
2 |
|
$surveys = $em->getRepository(Survey::class)->findSurveyByUser($user); |
28
|
2 |
|
if (!$surveys) { |
29
|
|
|
return $this->json(['message' => 'No surveys'], 404); |
30
|
|
|
} |
31
|
2 |
|
$serializer = $this->get('serializer'); |
32
|
|
|
|
33
|
2 |
|
$json = $serializer->normalize( |
34
|
|
|
$surveys, |
35
|
2 |
|
null, |
36
|
2 |
|
array('groups' => array('group1')) |
37
|
|
|
); |
38
|
|
|
|
39
|
2 |
|
return $this->json(['surveys' => $json], 200); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Survey $survey |
44
|
|
|
* @Route("/surveys/{id}", name="api_survey") |
45
|
|
|
* @Method("GET") |
46
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
47
|
|
|
*/ |
48
|
|
|
public function apiSurveyAction(Survey $survey) |
49
|
|
|
{ |
50
|
|
|
$user = $this->getUser(); |
51
|
|
|
if (!$user) { |
52
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!$survey) { |
56
|
|
|
return $this->json(['message' => 'No surveys'], 404); |
57
|
|
|
} |
58
|
|
|
$serializer = $this->get('serializer'); |
59
|
|
|
$jsonSurvey = $serializer->normalize( |
60
|
|
|
$survey, |
61
|
|
|
null, |
62
|
|
|
array('groups' => array('group1', 'group2')) |
63
|
|
|
); |
64
|
|
|
if ($survey->getStatus() == 'submited') { |
65
|
|
|
$answers = $survey->getAnswers(); |
66
|
|
|
$jsonAnswers = $serializer->normalize( |
67
|
|
|
$answers, |
68
|
|
|
null, |
69
|
|
|
array('groups' => array('group3')) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return $this->json(['survey' => $jsonSurvey, 'answers' => $jsonAnswers], 200); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->json(['survey' => $jsonSurvey], 200); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Request $request, Survey $survey |
|
|
|
|
80
|
|
|
* @Route("/surveys/{id}", name="api_survey_update") |
81
|
|
|
* @Method("PUT") |
82
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
83
|
|
|
*/ |
84
|
1 |
|
public function apiSurveyUpdateAction(Request $request, Survey $survey) |
85
|
|
|
{ |
86
|
1 |
|
$user = $this->getUser(); |
87
|
1 |
|
if (!$user) { |
88
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
89
|
|
|
} |
90
|
1 |
|
if (!$survey || $survey->getStatus() == 'submited') { |
91
|
|
|
return $this->json(['message' => 'No survey'], 404); |
92
|
|
|
} |
93
|
1 |
|
foreach ($survey->getType()->getSections() as $section) { |
94
|
1 |
|
foreach ($section->getQuestions() as $question) { |
95
|
1 |
|
$questKey[] = $question->getId(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
1 |
|
$data = json_decode($request->getContent(), true); |
99
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
100
|
1 |
|
foreach ($questKey as $key) { |
|
|
|
|
101
|
1 |
|
$answer = $data[$key]; |
102
|
1 |
|
if ($answer == null) { |
103
|
|
|
return $this->json(['message' => 'Survey is not filled out'], 400); |
104
|
|
|
} |
105
|
1 |
|
$question = $em->getRepository(SurveyQuestion::class)->find($key); |
106
|
1 |
|
$variants = $question->getVariants(); |
107
|
1 |
|
if (count($variants) > 0 & !in_array($answer, $variants)) { |
108
|
|
|
return $this->json(['message' => 'Wrong answer variant.'], 400); |
109
|
|
|
} |
110
|
1 |
|
$newAnswer = new SurveyAnswer(); |
111
|
1 |
|
$newAnswer->setSurvey($survey); |
112
|
1 |
|
$newAnswer->setQuestion($question); |
113
|
1 |
|
$newAnswer->setContent($answer); |
114
|
1 |
|
$em->persist($newAnswer); |
115
|
|
|
} |
116
|
1 |
|
$survey->setStatus('submited'); |
117
|
1 |
|
$em->flush(); |
118
|
|
|
|
119
|
1 |
|
return $this->json(['message' => 'Survey updated'], 200); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.