1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2014-2016 Arnaud Bienvenu |
4
|
|
|
* |
5
|
|
|
* This file is part of Kyela. |
6
|
|
|
|
7
|
|
|
* Kyela is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
|
12
|
|
|
* Kyela is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with Kyela. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Abienvenu\KyelaBundle\Controller; |
23
|
|
|
|
24
|
|
|
use Abienvenu\KyelaBundle\Entity\Choice; |
25
|
|
|
use Abienvenu\KyelaBundle\Entity\Event; |
26
|
|
|
use Abienvenu\KyelaBundle\Entity\Participant; |
27
|
|
|
use Abienvenu\KyelaBundle\Entity\Poll; |
28
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
29
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
30
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
31
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
32
|
|
|
use Abienvenu\KyelaBundle\Entity\Participation; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Participation controller. |
36
|
|
|
* |
37
|
|
|
* @Route("/participation") |
38
|
|
|
*/ |
39
|
|
|
class ParticipationController extends Controller |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Displays interactive participation table |
43
|
|
|
* |
44
|
|
|
* @Method("GET") |
45
|
|
|
* @Template() |
46
|
|
|
*/ |
47
|
|
|
public function showAction(Poll $poll, $isFuture) |
48
|
|
|
{ |
49
|
|
|
$em = $this->getDoctrine()->getManager(); |
50
|
|
|
$events = $em->getRepository('KyelaBundle:Event')->getFutureOrPastEvents($poll, $isFuture); |
51
|
|
|
$choices = $em->getRepository("KyelaBundle:Choice")->getOrderedChoices($poll); |
52
|
|
|
$participationsArray = []; |
53
|
|
|
foreach ($events as $event) |
54
|
|
|
{ |
55
|
|
|
foreach ($event->getParticipations() as $participation) |
56
|
|
|
{ |
57
|
|
|
$accessKey = "{$event->getId()}-{$participation->getParticipant()->getId()}"; |
58
|
|
|
$participationsArray[$accessKey] = $participation; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return [ |
62
|
|
|
'poll' => $poll, |
63
|
|
|
'choices' => $choices, |
64
|
|
|
'events' => $events, |
65
|
|
|
'participations' => $participationsArray, |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates a new Participation on the fly. |
71
|
|
|
* |
72
|
|
|
* @Route("/new/{event}/{participant}/{choice}", name="participation_new") |
73
|
|
|
* @Method("GET") |
74
|
|
|
*/ |
75
|
|
|
public function newAction(Event $event, Participant $participant, Choice $choice) |
76
|
|
|
{ |
77
|
|
|
$em = $this->getDoctrine()->getManager(); |
78
|
|
|
|
79
|
|
|
// If participation exists, editAction should have been called, not newAction |
80
|
|
|
// But just in case, we look for an existing participation |
81
|
|
|
$participation = $em->getRepository('KyelaBundle:Participation')->findOneBy(['participant' => $participant->getId(), 'event' => $event->getId()]); |
82
|
|
|
if (!$participation) |
83
|
|
|
{ |
84
|
|
|
$participation = new Participation(); |
85
|
|
|
} |
86
|
|
|
$participation->setEvent($event); |
87
|
|
|
$participation->setParticipant($participant); |
88
|
|
|
$participation->setChoice($choice); |
89
|
|
|
|
90
|
|
|
$em->persist($participation); |
91
|
|
|
$em->flush(); |
92
|
|
|
return $this->render( |
93
|
|
|
'KyelaBundle:Participation:_cell.html.twig', |
94
|
|
|
['participation' => $participation, 'choices' => $event->getPoll()->getChoices(), 'event' => $event, 'participant' => $participant]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Edits a Participation on the fly |
99
|
|
|
* |
100
|
|
|
* @Route("/edit/{participation}/{newChoice}", name="participation_edit") |
101
|
|
|
* @Method("GET") |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function editAction(Participation $participation, Choice $newChoice) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$em = $this->getDoctrine()->getManager(); |
106
|
|
|
|
107
|
|
|
$participation->setChoice($newChoice); |
108
|
|
|
$em->flush(); |
109
|
|
|
return $this->render( |
110
|
|
|
'KyelaBundle:Participation:_cell.html.twig', |
111
|
|
|
[ |
112
|
|
|
'participation' => $participation, |
113
|
|
|
'choices' => $newChoice->getPoll()->getChoices(), |
114
|
|
|
'event' => $participation->getEvent(), |
115
|
|
|
'participant' => $participation->getParticipant() |
116
|
|
|
] |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Removes a Participation on the fly |
122
|
|
|
* |
123
|
|
|
* @Route("/delete/{participation}", name="participation_delete") |
124
|
|
|
* @Method("GET") |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function deleteAction(Participation $participation) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$em = $this->getDoctrine()->getManager(); |
129
|
|
|
$em->remove($participation); |
130
|
|
|
$em->flush(); |
131
|
|
|
return $this->render( |
132
|
|
|
'KyelaBundle:Participation:_cell.html.twig', |
133
|
|
|
[ |
134
|
|
|
'participation' => null, |
135
|
|
|
'choices' => $participation->getEvent()->getPoll()->getChoices(), |
136
|
|
|
'event' => $participation->getEvent(), |
137
|
|
|
'participant' => $participation->getParticipant() |
138
|
|
|
] |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.