|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\View\Model\JsonModel; |
|
6
|
|
|
use PlaygroundGame\Entity\Quiz; |
|
7
|
|
|
use PlaygroundGame\Entity\QuizQuestion; |
|
8
|
|
|
use PlaygroundGame\Controller\Admin\GameController; |
|
9
|
|
|
use PlaygroundGame\Service\Game as AdminGameService; |
|
10
|
|
|
use Zend\View\Model\ViewModel; |
|
11
|
|
|
use Zend\Paginator\Paginator; |
|
12
|
|
|
|
|
13
|
|
|
class QuizController extends GameController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var \PlaygroundGame\Service\Game |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $adminGameService; |
|
19
|
|
|
protected $quizReplyAnswerMapper; |
|
20
|
|
|
protected $quizReplyMapper; |
|
21
|
|
|
|
|
22
|
|
|
public function listQuestionAction() |
|
23
|
|
|
{ |
|
24
|
|
|
$service = $this->getAdminGameService(); |
|
25
|
|
|
$quizId = $this->getEvent()->getRouteMatch()->getParam('quizId'); |
|
26
|
|
|
if (!$quizId) { |
|
27
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
|
28
|
|
|
} |
|
29
|
|
|
$quiz = $service->getGameMapper()->findById($quizId); |
|
30
|
|
|
$questions = $service->getQuizQuestionMapper()->findByGameId($quizId); |
|
31
|
|
|
|
|
32
|
|
|
if (is_array($questions)) { |
|
33
|
|
|
$paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($questions)); |
|
34
|
|
|
} else { |
|
35
|
|
|
$paginator = $questions; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$paginator->setItemCountPerPage(50); |
|
39
|
|
|
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
|
40
|
|
|
|
|
41
|
|
|
return array( |
|
42
|
|
|
'questions' => $paginator, |
|
43
|
|
|
'quiz_id' => $quizId, |
|
44
|
|
|
'quiz' => $quiz, |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function addQuestionAction() |
|
49
|
|
|
{ |
|
50
|
|
|
$viewModel = new ViewModel(); |
|
51
|
|
|
$viewModel->setTemplate('playground-game/quiz/question'); |
|
52
|
|
|
$service = $this->getAdminGameService(); |
|
53
|
|
|
$quizId = $this->getEvent()->getRouteMatch()->getParam('quizId'); |
|
54
|
|
|
|
|
55
|
|
|
if (!$quizId) { |
|
56
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_quizquestion_form'); |
|
60
|
|
|
$form->get('submit')->setAttribute('label', 'Ajouter'); |
|
61
|
|
|
$form->get('quiz_id')->setAttribute('value', $quizId); |
|
62
|
|
|
$form->setAttribute( |
|
63
|
|
|
'action', |
|
64
|
|
|
$this->url()->fromRoute('admin/playgroundgame/quiz-question-add', array('quizId' => $quizId)) |
|
65
|
|
|
); |
|
66
|
|
|
$form->setAttribute('method', 'post'); |
|
67
|
|
|
|
|
68
|
|
|
$question = new QuizQuestion(); |
|
69
|
|
|
$form->bind($question); |
|
70
|
|
|
|
|
71
|
|
View Code Duplication |
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
72
|
|
|
$data = array_replace_recursive( |
|
73
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
74
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$question = $service->createQuestion($data); |
|
78
|
|
|
if ($question) { |
|
79
|
|
|
// Redirect to list of games |
|
80
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was created'); |
|
81
|
|
|
|
|
82
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId)); |
|
83
|
|
|
} else { // Creation failed |
|
84
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
|
85
|
|
|
'The question was not updated - create at least one good answer' |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $viewModel->setVariables(array('form' => $form, 'quiz_id' => $quizId, 'question_id' => 0)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function editQuestionAction() |
|
94
|
|
|
{ |
|
95
|
|
|
$service = $this->getAdminGameService(); |
|
96
|
|
|
$viewModel = new ViewModel(); |
|
97
|
|
|
$viewModel->setTemplate('playground-game/quiz/question'); |
|
98
|
|
|
|
|
99
|
|
|
$questionId = $this->getEvent()->getRouteMatch()->getParam('questionId'); |
|
100
|
|
|
if (!$questionId) { |
|
101
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
|
102
|
|
|
} |
|
103
|
|
|
$question = $service->getQuizQuestionMapper()->findById($questionId); |
|
104
|
|
|
$quizId = $question->getQuiz()->getId(); |
|
105
|
|
|
|
|
106
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_quizquestion_form'); |
|
107
|
|
|
$form->get('submit')->setAttribute('label', 'Mettre à jour'); |
|
108
|
|
|
$form->get('quiz_id')->setAttribute('value', $quizId); |
|
109
|
|
|
$form->setAttribute( |
|
110
|
|
|
'action', |
|
111
|
|
|
$this->url()->fromRoute('admin/playgroundgame/quiz-question-edit', array('questionId' => $questionId)) |
|
112
|
|
|
); |
|
113
|
|
|
$form->setAttribute('method', 'post'); |
|
114
|
|
|
|
|
115
|
|
|
$form->bind($question); |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
118
|
|
|
$data = array_replace_recursive( |
|
119
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
120
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
$question = $service->updateQuestion($data, $question); |
|
124
|
|
|
if ($question) { |
|
125
|
|
|
// Redirect to list of games |
|
126
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was updated'); |
|
127
|
|
|
|
|
128
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId)); |
|
129
|
|
|
} else { |
|
130
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
|
131
|
|
|
'The question was not updated - create at least one good answer' |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $viewModel->setVariables(array('form' => $form, 'quiz_id' => $quizId, 'question_id' => $questionId)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function removeQuestionAction() |
|
140
|
|
|
{ |
|
141
|
|
|
$service = $this->getAdminGameService(); |
|
142
|
|
|
$questionId = $this->getEvent()->getRouteMatch()->getParam('questionId'); |
|
143
|
|
|
if (!$questionId) { |
|
144
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
|
145
|
|
|
} |
|
146
|
|
|
$question = $service->getQuizQuestionMapper()->findById($questionId); |
|
147
|
|
|
$quizId = $question->getQuiz()->getId(); |
|
148
|
|
|
|
|
149
|
|
|
$service->getQuizQuestionMapper()->remove($question); |
|
150
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was created'); |
|
151
|
|
|
|
|
152
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId)); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function sortQuestionAction() |
|
156
|
|
|
{ |
|
157
|
|
|
$result = $this->getAdminGameService()->sortQuestion($this->params()->fromQuery('order')); |
|
158
|
|
|
$model = new JsonModel(array( |
|
159
|
|
|
'success' => $result, |
|
160
|
|
|
)); |
|
161
|
|
|
return $model->setTerminal(true); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
View Code Duplication |
public function createQuizAction() |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$service = $this->getAdminGameService(); |
|
167
|
|
|
$viewModel = new ViewModel(); |
|
168
|
|
|
$viewModel->setTemplate('playground-game/quiz/quiz'); |
|
169
|
|
|
|
|
170
|
|
|
$gameForm = new ViewModel(); |
|
171
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
|
172
|
|
|
|
|
173
|
|
|
$quiz = new Quiz(); |
|
174
|
|
|
|
|
175
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_quiz_form'); |
|
176
|
|
|
$form->bind($quiz); |
|
177
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
|
178
|
|
|
$form->setAttribute( |
|
179
|
|
|
'action', |
|
180
|
|
|
$this->url()->fromRoute( |
|
181
|
|
|
'admin/playgroundgame/create-quiz', |
|
182
|
|
|
array('gameId' => 0) |
|
183
|
|
|
) |
|
184
|
|
|
); |
|
185
|
|
|
$form->setAttribute('method', 'post'); |
|
186
|
|
|
|
|
187
|
|
|
$request = $this->getRequest(); |
|
188
|
|
|
if ($request->isPost()) { |
|
189
|
|
|
$data = array_replace_recursive( |
|
190
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
191
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
192
|
|
|
); |
|
193
|
|
|
if (empty($data['prizes'])) { |
|
194
|
|
|
$data['prizes'] = array(); |
|
195
|
|
|
} |
|
196
|
|
|
$game = $service->createOrUpdate($data, $quiz, 'playgroundgame_quiz_form'); |
|
197
|
|
|
if ($game) { |
|
198
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
|
199
|
|
|
|
|
200
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $quiz)); |
|
204
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
|
205
|
|
|
|
|
206
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Create quiz')); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function editQuizAction() |
|
210
|
|
|
{ |
|
211
|
|
|
$this->checkGame(); |
|
212
|
|
|
|
|
213
|
|
|
return $this->editGame( |
|
214
|
|
|
'playground-game/quiz/quiz', |
|
215
|
|
|
'playgroundgame_quiz_form' |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function getAdminGameService() |
|
220
|
|
|
{ |
|
221
|
|
|
if (!$this->adminGameService) { |
|
222
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_quiz_service'); |
|
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $this->adminGameService; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function setAdminGameService(AdminGameService $adminGameService) |
|
229
|
|
|
{ |
|
230
|
|
|
$this->adminGameService = $adminGameService; |
|
231
|
|
|
|
|
232
|
|
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
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: