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( |
169
|
|
|
'action', |
170
|
|
|
$this->url()->fromRoute( |
171
|
|
|
'admin/playgroundgame/create-quiz', |
172
|
|
|
array('gameId' => 0) |
173
|
|
|
) |
174
|
|
|
); |
175
|
|
|
$form->setAttribute('method', 'post'); |
176
|
|
|
|
177
|
|
|
$request = $this->getRequest(); |
178
|
|
|
if ($request->isPost()) { |
179
|
|
|
$data = array_replace_recursive( |
180
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
181
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
182
|
|
|
); |
183
|
|
|
if (empty($data['prizes'])) { |
184
|
|
|
$data['prizes'] = array(); |
185
|
|
|
} |
186
|
|
|
$game = $service->create($data, $quiz, 'playgroundgame_quiz_form'); |
187
|
|
|
if ($game) { |
188
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
189
|
|
|
|
190
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $quiz)); |
194
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
195
|
|
|
|
196
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Create quiz')); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function editQuizAction() |
200
|
|
|
{ |
201
|
|
|
$this->checkGame(); |
202
|
|
|
|
203
|
|
|
return $this->editGame( |
204
|
|
|
'playground-game/quiz/quiz', |
205
|
|
|
'playgroundgame_quiz_form' |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function getAdminGameService() |
210
|
|
|
{ |
211
|
|
|
if (!$this->adminGameService) { |
212
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_quiz_service'); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $this->adminGameService; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function setAdminGameService(AdminGameService $adminGameService) |
219
|
|
|
{ |
220
|
|
|
$this->adminGameService = $adminGameService; |
|
|
|
|
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
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: