1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use PlaygroundGame\Entity\Quiz; |
6
|
|
|
use PlaygroundGame\Entity\QuizQuestion; |
7
|
|
|
use PlaygroundGame\Controller\Admin\GameController; |
8
|
|
|
use PlaygroundGame\Service\Game as AdminGameService; |
9
|
|
|
use Zend\View\Model\ViewModel; |
10
|
|
|
use Zend\Paginator\Paginator; |
11
|
|
|
|
12
|
|
|
class QuizController extends GameController |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var GameService |
16
|
|
|
*/ |
17
|
|
|
protected $adminGameService; |
18
|
|
|
protected $quizReplyAnswerMapper; |
19
|
|
|
protected $quizReplyMapper; |
20
|
|
|
|
21
|
|
|
public function listQuestionAction() |
22
|
|
|
{ |
23
|
|
|
$service = $this->getAdminGameService(); |
24
|
|
|
$quizId = $this->getEvent()->getRouteMatch()->getParam('quizId'); |
25
|
|
|
if (!$quizId) { |
26
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
27
|
|
|
} |
28
|
|
|
$quiz = $service->getGameMapper()->findById($quizId); |
29
|
|
|
$questions = $service->getQuizQuestionMapper()->findByGameId($quizId); |
30
|
|
|
|
31
|
|
|
if (is_array($questions)) { |
32
|
|
|
$paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($questions)); |
33
|
|
|
} else { |
34
|
|
|
$paginator = $questions; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$paginator->setItemCountPerPage(10); |
38
|
|
|
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
39
|
|
|
|
40
|
|
|
return array( |
41
|
|
|
'questions' => $paginator, |
42
|
|
|
'quiz_id' => $quizId, |
43
|
|
|
'quiz' => $quiz, |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function addQuestionAction() |
48
|
|
|
{ |
49
|
|
|
$viewModel = new ViewModel(); |
50
|
|
|
$viewModel->setTemplate('playground-game/quiz/question'); |
51
|
|
|
$service = $this->getAdminGameService(); |
52
|
|
|
$quizId = $this->getEvent()->getRouteMatch()->getParam('quizId'); |
53
|
|
|
|
54
|
|
|
if (!$quizId) { |
55
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_quizquestion_form'); |
59
|
|
|
$form->get('submit')->setAttribute('label', 'Ajouter'); |
60
|
|
|
$form->get('quiz_id')->setAttribute('value', $quizId); |
61
|
|
|
$form->setAttribute( |
62
|
|
|
'action', |
63
|
|
|
$this->url()->fromRoute('admin/playgroundgame/quiz-question-add', array('quizId' => $quizId)) |
64
|
|
|
); |
65
|
|
|
$form->setAttribute('method', 'post'); |
66
|
|
|
|
67
|
|
|
$question = new QuizQuestion(); |
68
|
|
|
$form->bind($question); |
69
|
|
|
|
70
|
|
View Code Duplication |
if ($this->getRequest()->isPost()) { |
|
|
|
|
71
|
|
|
$data = array_replace_recursive( |
72
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
73
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$question = $service->createQuestion($data); |
77
|
|
|
if ($question) { |
78
|
|
|
// Redirect to list of games |
79
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was created'); |
80
|
|
|
|
81
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId)); |
82
|
|
|
} else { // Creation failed |
83
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
84
|
|
|
'The question was not updated - create at least one good answer' |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $viewModel->setVariables(array('form' => $form, 'quiz_id' => $quizId, 'question_id' => 0)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function editQuestionAction() |
93
|
|
|
{ |
94
|
|
|
$service = $this->getAdminGameService(); |
95
|
|
|
$viewModel = new ViewModel(); |
96
|
|
|
$viewModel->setTemplate('playground-game/quiz/question'); |
97
|
|
|
|
98
|
|
|
$questionId = $this->getEvent()->getRouteMatch()->getParam('questionId'); |
99
|
|
|
if (!$questionId) { |
100
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
101
|
|
|
} |
102
|
|
|
$question = $service->getQuizQuestionMapper()->findById($questionId); |
103
|
|
|
$quizId = $question->getQuiz()->getId(); |
104
|
|
|
|
105
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_quizquestion_form'); |
106
|
|
|
$form->get('submit')->setAttribute('label', 'Mettre à jour'); |
107
|
|
|
$form->get('quiz_id')->setAttribute('value', $quizId); |
108
|
|
|
$form->setAttribute( |
109
|
|
|
'action', |
110
|
|
|
$this->url()->fromRoute('admin/playgroundgame/quiz-question-edit', array('questionId' => $questionId)) |
111
|
|
|
); |
112
|
|
|
$form->setAttribute('method', 'post'); |
113
|
|
|
|
114
|
|
|
$form->bind($question); |
115
|
|
|
|
116
|
|
View Code Duplication |
if ($this->getRequest()->isPost()) { |
|
|
|
|
117
|
|
|
$data = array_replace_recursive( |
118
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
119
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$question = $service->updateQuestion($data, $question); |
123
|
|
|
if ($question) { |
124
|
|
|
// Redirect to list of games |
125
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was updated'); |
126
|
|
|
|
127
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId)); |
128
|
|
|
} else { |
129
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
130
|
|
|
'The question was not updated - create at least one good answer' |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $viewModel->setVariables(array('form' => $form, 'quiz_id' => $quizId, 'question_id' => $questionId)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function removeQuestionAction() |
139
|
|
|
{ |
140
|
|
|
$service = $this->getAdminGameService(); |
141
|
|
|
$questionId = $this->getEvent()->getRouteMatch()->getParam('questionId'); |
142
|
|
|
if (!$questionId) { |
143
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
144
|
|
|
} |
145
|
|
|
$question = $service->getQuizQuestionMapper()->findById($questionId); |
146
|
|
|
$quizId = $question->getQuiz()->getId(); |
147
|
|
|
|
148
|
|
|
$service->getQuizQuestionMapper()->remove($question); |
149
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was created'); |
150
|
|
|
|
151
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
View Code Duplication |
public function createQuizAction() |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$service = $this->getAdminGameService(); |
157
|
|
|
$viewModel = new ViewModel(); |
158
|
|
|
$viewModel->setTemplate('playground-game/quiz/quiz'); |
159
|
|
|
|
160
|
|
|
$gameForm = new ViewModel(); |
161
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
162
|
|
|
|
163
|
|
|
$quiz = new Quiz(); |
164
|
|
|
|
165
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_quiz_form'); |
166
|
|
|
$form->bind($quiz); |
167
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
168
|
|
|
$form->setAttribute('action', $this->url()->fromRoute('admin/playgroundgame/create-quiz', array('gameId' => 0))); |
169
|
|
|
$form->setAttribute('method', 'post'); |
170
|
|
|
|
171
|
|
|
$request = $this->getRequest(); |
172
|
|
|
if ($request->isPost()) { |
173
|
|
|
$data = array_replace_recursive( |
174
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
175
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
176
|
|
|
); |
177
|
|
|
if (empty($data['prizes'])) { |
178
|
|
|
$data['prizes'] = array(); |
179
|
|
|
} |
180
|
|
|
$game = $service->create($data, $quiz, 'playgroundgame_quiz_form'); |
181
|
|
|
if ($game) { |
182
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
183
|
|
|
|
184
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $quiz)); |
188
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
189
|
|
|
|
190
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Create quiz')); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
View Code Duplication |
public function editQuizAction() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$service = $this->getAdminGameService(); |
196
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
197
|
|
|
|
198
|
|
|
if (!$gameId) { |
199
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/create-quiz'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$game = $service->getGameMapper()->findById($gameId); |
203
|
|
|
$viewModel = new ViewModel(); |
204
|
|
|
$viewModel->setTemplate('playground-game/quiz/quiz'); |
205
|
|
|
|
206
|
|
|
$gameForm = new ViewModel(); |
207
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
208
|
|
|
|
209
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_quiz_form'); |
210
|
|
|
$form->setAttribute('action', $this->url()->fromRoute('admin/playgroundgame/edit-quiz', array('gameId' => $gameId))); |
211
|
|
|
$form->setAttribute('method', 'post'); |
212
|
|
|
|
213
|
|
|
if ($game->getFbAppId()) { |
214
|
|
|
$appIds = $form->get('fbAppId')->getOption('value_options'); |
215
|
|
|
$appIds[$game->getFbAppId()] = $game->getFbAppId(); |
216
|
|
|
$form->get('fbAppId')->setAttribute('options', $appIds); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$gameOptions = $this->getAdminGameService()->getOptions(); |
220
|
|
|
$gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $game->getId(). '.css'; |
221
|
|
|
if (is_file($gameStylesheet)) { |
222
|
|
|
$values = $form->get('stylesheet')->getValueOptions(); |
223
|
|
|
$values[$gameStylesheet] = 'Style personnalisé de ce jeu'; |
224
|
|
|
|
225
|
|
|
$form->get('stylesheet')->setAttribute('options', $values); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$form->bind($game); |
229
|
|
|
|
230
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
231
|
|
|
$data = array_replace_recursive( |
232
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
233
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
234
|
|
|
); |
235
|
|
|
if (empty($data['prizes'])) { |
236
|
|
|
$data['prizes'] = array(); |
237
|
|
|
} |
238
|
|
|
$result = $service->edit($data, $game, 'playgroundgame_quiz_form'); |
239
|
|
|
|
240
|
|
|
if ($result) { |
241
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $game)); |
246
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
247
|
|
|
|
248
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Edit quiz')); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function getAdminGameService() |
252
|
|
|
{ |
253
|
|
|
if (!$this->adminGameService) { |
254
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_quiz_service'); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this->adminGameService; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function setAdminGameService(AdminGameService $adminGameService) |
261
|
|
|
{ |
262
|
|
|
$this->adminGameService = $adminGameService; |
|
|
|
|
263
|
|
|
|
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function getQuizReplyAnswerMapper() |
268
|
|
|
{ |
269
|
|
|
if (!$this->quizReplyAnswerMapper) { |
270
|
|
|
$this->quizReplyAnswerMapper = $this->getServiceLocator()->get('playgroundgame_quizreplyanswer_mapper'); |
271
|
|
|
} |
272
|
|
|
return $this->quizReplyAnswerMapper; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function getQuizReplyMapper() |
276
|
|
|
{ |
277
|
|
|
if (!$this->quizReplyMapper) { |
278
|
|
|
$this->quizReplyMapper = $this->getServiceLocator()->get('playgroundgame_quizreply_mapper'); |
279
|
|
|
} |
280
|
|
|
return $this->quizReplyMapper; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: