/
SurveyController.php
135 lines (120 loc) · 4.55 KB
/
SurveyController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\Survey;
use AppBundle\Entity\SurveyAnswer;
use AppBundle\Entity\SurveyResult;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
/**
* Class SurveyController
* @Route("/survey")
*/
class SurveyController extends Controller
{
/**
* @Route("/", name="survey_list")
*/
public function listAction(Request $request)
{
$answerForm = [];
if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
$newSurveys = $this->getDoctrine()->getRepository('AppBundle:Survey')->getAllActiveSurveys();
} else {
$newSurveys = $this->findNewSurveys();
foreach ($newSurveys as $survey) {
$answers = $survey->getSurveyAnswers()->getValues();
foreach ($answers as $answer) {
$answerForm[$answer->getId()] = $this->createAnswerForm($answer)->createView();
}
}
}
return $this->render('AppBundle:Front:survey.html.twig', ['newSurveys' => $newSurveys, 'answerForm' => $answerForm]);
}
/**
* @Route("/stat", name="survey_statistics")
*/
public function statisticsAction()
{
if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
throw $this->createAccessDeniedException();
}
$em = $this->getDoctrine()->getManager();
$statList = $em->getRepository('AppBundle:Survey')->getAllActiveVoteSurveys($this->getUser());
$countUsers = $em->getRepository('AppBundle:User')->countUsers();
$percent = $countUsers / 100;
return $this->render('@App/Front/surveyStat.html.twig', ['statList' => $statList, 'percent' => $percent]);
}
/**
* @param SurveyAnswer $id
* @Route("/result/{id}", name="survey_result", requirements={"id": "\d+"}))
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function resultAction(SurveyAnswer $id)
{
$result = new SurveyResult();
$em = $this->getDoctrine()->getManager();
$answer = $em->getRepository('AppBundle:SurveyAnswer')->findOneBy(['id' => $id]);
if (!$answer) {
throw $this->createNotFoundException('Unable to find answer');
}
$count = $answer->getCount();
$answer->setCount($count + 1);
$result->setUser($this->getUser());
$result->setAnswer($answer);
$result->setSurvey($answer->getSurvey());
$em->persist($result);
$em->flush();
return $this->redirectToRoute('survey_list');
}
private function findNewSurveys()
{
$em = $this->getDoctrine()->getManager();
$surveyList = $em->getRepository('AppBundle:Survey')->getAllActiveSurveys();
$vote = true;
$newSurveys = [];
/** @var Survey $survey */
foreach ($surveyList as $survey) {
if ($survey->getSurveyResult()->getValues()) {
/** @var SurveyResult $result */
foreach ($survey->getSurveyResult()->getValues() as $result) {
$currentUser = $this->getUser()->getid();
if ($result->getUser()) {
$userFromResult = $result->getUser()->getId();
} else {
$userFromResult = null;
}
if ($userFromResult == $currentUser) {
$vote = true;
break;
} else {
$vote = false;
/* user not vote */
}
}
if (!$vote) {
$newSurveys[$survey->getId()] = $survey;
}
} else {
$newSurveys[$survey->getId()] = $survey;
}
}
return $newSurveys;
}
/**
* @param SurveyAnswer $answer
* @return \Symfony\Component\Form\Form
*/
private function createAnswerForm($answer)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('survey_result', ['id' => $answer->getId()]))
->setMethod('POST')
->add('submit', SubmitType::class, [
'label' => $answer->getanswer(),
'attr' => ['class' => 'btn btn-sm btn-success']
])
->getForm();
}
}