|
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
|
|
14
|
|
|
class SurveyController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @Route("/surveys", name="api_surveys") |
|
18
|
|
|
* @Method("GET") |
|
19
|
|
|
*/ |
|
20
|
|
|
public function apiSurveysAction() |
|
21
|
|
|
{ |
|
22
|
|
|
$user = $this->getUser(); |
|
23
|
|
|
if (!$user) { |
|
24
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
|
25
|
|
|
} |
|
26
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
27
|
|
|
$surveys = $em->getRepository(Survey::class)->findSurveyByUser($user); |
|
28
|
|
|
if (!$surveys) { |
|
29
|
|
|
return $this->json(['message' => 'No surveys'], 404); |
|
30
|
|
|
} |
|
31
|
|
|
$serializer = $this->get('serializer'); |
|
32
|
|
|
|
|
33
|
|
|
$json = $serializer->normalize( |
|
34
|
|
|
$surveys, |
|
35
|
|
|
null, |
|
36
|
|
|
array('groups' => array('group1')) |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
return $this->json($json); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Survey $survey |
|
44
|
|
|
* @Route("/survey/{id}", name="api_survey_byid") |
|
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
|
|
|
$json = null; |
|
60
|
|
|
if ($survey->getStatus() == 'submited') { |
|
61
|
|
|
$json = $serializer->normalize( |
|
62
|
|
|
$survey, |
|
63
|
|
|
null, |
|
64
|
|
|
array('groups' => array('group1', 'group2')) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
if ($survey->getStatus() == 'current') { |
|
68
|
|
|
$json = $serializer->normalize( |
|
69
|
|
|
$survey, |
|
70
|
|
|
null, |
|
71
|
|
|
array('groups' => array('group1', 'group3')) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->json($json); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Request $request, Survey $survey |
|
|
|
|
|
|
80
|
|
|
* @Route("/survey/update/{id}", name="api_survey_update") |
|
81
|
|
|
* @Method("POST") |
|
82
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
|
83
|
|
|
*/ |
|
84
|
|
|
public function apiSurveyUpdateAction(Request $request, Survey $survey) |
|
85
|
|
|
{ |
|
86
|
|
|
$user = $this->getUser(); |
|
87
|
|
|
if (!$user) { |
|
88
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
|
89
|
|
|
} |
|
90
|
|
|
if (!$survey || $survey->getStatus() == 'submited') { |
|
91
|
|
|
return $this->json(['message' => 'No survey'], 404); |
|
92
|
|
|
} |
|
93
|
|
|
$questKey = array(); |
|
94
|
|
|
foreach ($survey->getType()->getQuestions() as $question) { |
|
95
|
|
|
$questKey[] = $question->getId(); |
|
96
|
|
|
} |
|
97
|
|
|
$data = json_decode($request->getContent(), true); |
|
98
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
99
|
|
|
foreach ($questKey as $key) { |
|
100
|
|
|
$answer = $data[$key]; |
|
101
|
|
|
if ($answer == null) { |
|
102
|
|
|
return $this->json(['message' => 'Wrong question id'], 400); |
|
103
|
|
|
} |
|
104
|
|
|
$newAnswer = new SurveyAnswer(); |
|
105
|
|
|
$newAnswer->setSurvey($survey); |
|
106
|
|
|
$newAnswer->setQuestion($em->getRepository(SurveyQuestion::class)->find($key)); |
|
|
|
|
|
|
107
|
|
|
$newAnswer->setContent($answer); |
|
108
|
|
|
$em->persist($newAnswer); |
|
109
|
|
|
} |
|
110
|
|
|
$survey->setStatus('submited'); |
|
111
|
|
|
$em->flush(); |
|
112
|
|
|
|
|
113
|
|
|
return $this->json(['message' => 'Survey updated'], 200); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.