1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Service; |
4
|
|
|
|
5
|
|
|
use PlaygroundGame\Entity\QuizReply; |
6
|
|
|
use PlaygroundGame\Entity\QuizReplyAnswer; |
7
|
|
|
use Zend\ServiceManager\ServiceManagerAwareInterface; |
8
|
|
|
use PlaygroundGame\Mapper\GameInterface as GameMapperInterface; |
9
|
|
|
use Zend\Stdlib\ErrorHandler; |
10
|
|
|
|
11
|
|
|
class Quiz extends Game implements ServiceManagerAwareInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var QuizMapperInterface |
15
|
|
|
*/ |
16
|
|
|
protected $quizMapper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var QuizAnswerMapperInterface |
20
|
|
|
*/ |
21
|
|
|
protected $quizAnswerMapper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var QuizQuestionMapperInterface |
25
|
|
|
*/ |
26
|
|
|
protected $quizQuestionMapper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var QuizReplyMapperInterface |
30
|
|
|
*/ |
31
|
|
|
protected $quizReplyMapper; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var quizReplyAnswerMapper |
35
|
|
|
*/ |
36
|
|
|
protected $quizReplyAnswerMapper; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* |
41
|
|
|
* @param array $data |
42
|
|
|
* @return \PlaygroundGame\Entity\Game |
43
|
|
|
*/ |
44
|
|
|
public function createQuestion(array $data) |
45
|
|
|
{ |
46
|
|
|
$path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR; |
47
|
|
|
$media_url = $this->getOptions()->getMediaUrl() . '/'; |
48
|
|
|
|
49
|
|
|
$question = new \PlaygroundGame\Entity\QuizQuestion(); |
50
|
|
|
$form = $this->getServiceManager()->get('playgroundgame_quizquestion_form'); |
51
|
|
|
$form->bind($question); |
52
|
|
|
$form->setData($data); |
53
|
|
|
|
54
|
|
|
$quiz = $this->getGameMapper()->findById($data['quiz_id']); |
55
|
|
|
if (!$form->isValid()) { |
56
|
|
|
return false; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$question->setQuiz($quiz); |
60
|
|
|
|
61
|
|
|
// If question is a prediction, no need to calculate max good answers |
62
|
|
|
if (!$question->getPrediction()) { |
63
|
|
|
// Max points and correct answers calculation for the question |
64
|
|
|
if (!$question = $this->calculateMaxAnswersQuestion($question)) { |
|
|
|
|
65
|
|
|
return false; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Max points and correct answers recalculation for the quiz |
70
|
|
|
$quiz = $this->calculateMaxAnswersQuiz($question->getQuiz()); |
71
|
|
|
|
72
|
|
|
$this->getEventManager()->trigger(__FUNCTION__, $this, array('game' => $question, 'data' => $data)); |
73
|
|
|
$this->getQuizQuestionMapper()->insert($question); |
74
|
|
|
$this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('game' => $question, 'data' => $data)); |
75
|
|
|
|
76
|
|
View Code Duplication |
if (!empty($data['upload_image']['tmp_name'])) { |
|
|
|
|
77
|
|
|
ErrorHandler::start(); |
78
|
|
|
$data['upload_image']['name'] = $this->fileNewname( |
79
|
|
|
$path, |
80
|
|
|
$question->getId() . "-" . $data['upload_image']['name'] |
81
|
|
|
); |
82
|
|
|
move_uploaded_file($data['upload_image']['tmp_name'], $path . $data['upload_image']['name']); |
83
|
|
|
$question->setImage($media_url . $data['upload_image']['name']); |
84
|
|
|
ErrorHandler::stop(true); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->getQuizQuestionMapper()->update($question); |
88
|
|
|
$this->getQuizMapper()->update($quiz); |
89
|
|
|
|
90
|
|
|
return $question; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $data |
95
|
|
|
* @return \PlaygroundGame\Entity\Game |
96
|
|
|
*/ |
97
|
|
|
public function updateQuestion(array $data, $question) |
98
|
|
|
{ |
99
|
|
|
$path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR; |
100
|
|
|
$media_url = $this->getOptions()->getMediaUrl() . '/'; |
101
|
|
|
|
102
|
|
|
$form = $this->getServiceManager()->get('playgroundgame_quizquestion_form'); |
103
|
|
|
$form->bind($question); |
104
|
|
|
$form->setData($data); |
105
|
|
|
|
106
|
|
|
if (!$form->isValid()) { |
107
|
|
|
return false; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// If question is a prediction, no need to calculate max good answers |
111
|
|
|
if (!$question->getPrediction()) { |
112
|
|
|
// Max points and correct answers calculation for the question |
113
|
|
|
if (!$question = $this->calculateMaxAnswersQuestion($question)) { |
114
|
|
|
return false; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
if (!empty($data['upload_image']['tmp_name'])) { |
|
|
|
|
119
|
|
|
ErrorHandler::start(); |
120
|
|
|
$data['upload_image']['name'] = $this->fileNewname( |
121
|
|
|
$path, |
122
|
|
|
$question->getId() . "-" . $data['upload_image']['name'] |
123
|
|
|
); |
124
|
|
|
move_uploaded_file($data['upload_image']['tmp_name'], $path . $data['upload_image']['name']); |
125
|
|
|
$question->setImage($media_url . $data['upload_image']['name']); |
126
|
|
|
ErrorHandler::stop(true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
if (isset($data['delete_image']) && empty($data['upload_image']['tmp_name'])) { |
|
|
|
|
130
|
|
|
ErrorHandler::start(); |
131
|
|
|
$image = $question->getImage(); |
132
|
|
|
$image = str_replace($media_url, '', $image); |
133
|
|
|
if (file_exists($path .$image)) { |
134
|
|
|
unlink($path .$image); |
135
|
|
|
} |
136
|
|
|
$question->setImage(null); |
137
|
|
|
ErrorHandler::stop(true); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$i = 0; |
141
|
|
|
foreach ($question->getAnswers() as $answer) { |
142
|
|
|
if (!empty($data['answers'][$i]['upload_image']['tmp_name'])) { |
143
|
|
|
ErrorHandler::start(); |
144
|
|
|
$data['answers'][$i]['upload_image']['name'] = $this->fileNewname( |
145
|
|
|
$path, |
146
|
|
|
$question->getId() . "-" . $data['answers'][$i]['upload_image']['name'] |
147
|
|
|
); |
148
|
|
|
move_uploaded_file( |
149
|
|
|
$data['answers'][$i]['upload_image']['tmp_name'], |
150
|
|
|
$path . $data['answers'][$i]['upload_image']['name'] |
151
|
|
|
); |
152
|
|
|
$answer->setImage($media_url . $data['answers'][$i]['upload_image']['name']); |
153
|
|
|
ErrorHandler::stop(true); |
154
|
|
|
} |
155
|
|
|
$i++; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Max points and correct answers recalculation for the quiz |
159
|
|
|
$quiz = $this->calculateMaxAnswersQuiz($question->getQuiz()); |
160
|
|
|
|
161
|
|
|
// If the question was a pronostic, I update entries with the results ! |
162
|
|
|
if ($question->getPrediction()) { |
163
|
|
|
$this->updatePrediction($question); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->getEventManager()->trigger( |
167
|
|
|
__FUNCTION__, |
168
|
|
|
$this, |
169
|
|
|
array('question' => $question, 'data' => $data) |
170
|
|
|
); |
171
|
|
|
$this->getQuizQuestionMapper()->update($question); |
172
|
|
|
$this->getEventManager()->trigger( |
173
|
|
|
__FUNCTION__.'.post', |
174
|
|
|
$this, |
175
|
|
|
array('question' => $question, 'data' => $data) |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
$this->getQuizMapper()->update($quiz); |
179
|
|
|
|
180
|
|
|
return $question; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function findRepliesByGame($game) |
184
|
|
|
{ |
185
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
186
|
|
|
$qb = $em->createQueryBuilder(); |
187
|
|
|
$qb->select('r') |
188
|
|
|
->from('PlaygroundGame\Entity\QuizReply', 'r') |
189
|
|
|
->innerJoin('r.entry', 'e') |
190
|
|
|
->where('e.game = :game') |
191
|
|
|
->setParameter('game', $game); |
192
|
|
|
$query = $qb->getQuery(); |
193
|
|
|
|
194
|
|
|
$replies = $query->getResult(); |
195
|
|
|
|
196
|
|
|
return $replies; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function updatePrediction($question) |
200
|
|
|
{ |
201
|
|
|
set_time_limit(0); |
202
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
203
|
|
|
|
204
|
|
|
/* @var $dbal \Doctrine\DBAL\Connection */ |
205
|
|
|
$dbal = $em->getConnection(); |
206
|
|
|
|
207
|
|
|
$answers = $question->getAnswers($question->getQuiz()); |
208
|
|
|
|
209
|
|
|
// I update all the answers with points and correctness |
210
|
|
|
// Very fast (native query inside) |
211
|
|
|
if ($question->getType() == 2){ |
212
|
|
|
foreach ($answers as $answer) { |
213
|
|
|
$value = trim(strip_tags($answer->getAnswer())); |
214
|
|
|
$points = ($answer->getCorrect())? $answer->getPoints():0; |
215
|
|
|
$sql = " |
216
|
|
|
UPDATE game_quiz_reply_answer AS ra |
217
|
|
|
SET ra.points=IF(ra.answer='". $value ."', ". $points .", ra.points), |
218
|
|
|
ra.correct = IF(ra.answer='". $value ."', ". $answer->getCorrect() .", ra.correct) |
219
|
|
|
WHERE ra.question_id = " . $question->getId() ." |
220
|
|
|
"; |
221
|
|
|
|
222
|
|
|
$dbal->exec( $sql ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
} else { |
226
|
|
|
foreach ($answers as $answer) { |
227
|
|
|
$points = ($answer->getCorrect())? $answer->getPoints():0; |
228
|
|
|
$sql = " |
229
|
|
|
UPDATE game_quiz_reply_answer AS ra |
230
|
|
|
SET ra.points=" . $points .", |
231
|
|
|
ra.correct = " . $answer->getCorrect() ." |
232
|
|
|
WHERE ra.question_id = ". $question->getId() ." |
233
|
|
|
AND ra.answer_id = ". $answer->getId() ." |
234
|
|
|
"; |
235
|
|
|
|
236
|
|
|
$dbal->exec( $sql ); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// Entry update with points. WINNER as to be calculated also ! |
241
|
|
|
$sql = " |
242
|
|
|
UPDATE game_entry as e |
243
|
|
|
INNER JOIN |
244
|
|
|
( |
245
|
|
|
SELECT e.id, SUM(ra.points) as points, SUM(ra.correct) as correct |
246
|
|
|
FROM game_entry as e |
247
|
|
|
INNER JOIN game_quiz_reply AS r ON r.entry_id = e.id |
248
|
|
|
INNER JOIN game_quiz_reply_answer AS ra ON ra.reply_id = r.id |
249
|
|
|
GROUP BY e.id |
250
|
|
|
) i ON e.id = i.id |
251
|
|
|
SET e.points = i.points |
252
|
|
|
WHERE e.game_id = ". $question->getQuiz()->getId() . " |
253
|
|
|
"; |
254
|
|
|
|
255
|
|
|
$dbal->exec( $sql ); |
256
|
|
|
|
257
|
|
|
$this->getEventManager()->trigger( |
258
|
|
|
__FUNCTION__.'.post', |
259
|
|
|
$this, |
260
|
|
|
array('question' => $question) |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
/** |
264
|
|
|
* This function update the sort order of the questions in a Quiz |
265
|
|
|
* BEWARE : This function is time consuming (1s for 11 updates) |
266
|
|
|
* If you have many replies, switch to a batch |
267
|
|
|
* |
268
|
|
|
* To improve performance, usage of DQL update |
269
|
|
|
* http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html |
270
|
|
|
* |
271
|
|
|
* @param string $data |
|
|
|
|
272
|
|
|
* @return boolean |
273
|
|
|
*/ |
274
|
|
|
public function updatePredictionOld($question) |
275
|
|
|
{ |
276
|
|
|
set_time_limit(0); |
277
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
278
|
|
|
|
279
|
|
|
$replies = $this->findRepliesByGame($question->getQuiz()); |
280
|
|
|
|
281
|
|
|
$answers = $question->getAnswers($question->getQuiz()); |
282
|
|
|
|
283
|
|
|
$answersarray = array(); |
284
|
|
|
foreach ($answers as $answer) { |
285
|
|
|
$answersarray[$answer->getId()] = $answer; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
foreach ($replies as $reply) { |
289
|
|
|
$quizPoints = 0; |
290
|
|
|
$quizCorrectAnswers = 0; |
291
|
|
|
|
292
|
|
|
foreach ($reply->getAnswers() as $quizReplyAnswer) { |
293
|
|
|
if (2 != $question->getType() && $quizReplyAnswer->getQuestionId() === $question->getId()) { |
294
|
|
|
if ($answersarray[$quizReplyAnswer->getAnswerId()]) { |
295
|
|
|
$updatedAnswer = $answersarray[$quizReplyAnswer->getAnswerId()]; |
296
|
|
|
$quizReplyAnswer->setPoints($updatedAnswer->getPoints()); |
297
|
|
|
$quizReplyAnswer->setCorrect($updatedAnswer->getCorrect()); |
298
|
|
|
$q = $em->createQuery( |
299
|
|
|
'update PlaygroundGame\Entity\QuizReplyAnswer a SET a.points = ' . |
300
|
|
|
$updatedAnswer->getPoints() . ',a.correct=' . $updatedAnswer->getCorrect() . |
301
|
|
|
' WHERE a.id=' .$quizReplyAnswer->getId() |
302
|
|
|
); |
303
|
|
|
$q->execute(); |
304
|
|
|
} |
305
|
|
|
} elseif ($quizReplyAnswer->getQuestionId() === $question->getId()) { |
306
|
|
|
// question is a textarea |
307
|
|
|
// search for a matching answer |
308
|
|
|
foreach ($answers as $answer) { |
309
|
|
|
if (trim(strip_tags($answer->getAnswer())) == trim( |
310
|
|
|
strip_tags($quizReplyAnswer->getAnswer()) |
311
|
|
|
) |
312
|
|
|
) { |
313
|
|
|
$quizReplyAnswer->setPoints($answer->getPoints()); |
314
|
|
|
$quizReplyAnswer->setCorrect($answer->getCorrect()); |
315
|
|
|
$q = $em->createQuery( |
316
|
|
|
'update PlaygroundGame\Entity\QuizReplyAnswer a SET a.points = ' . |
317
|
|
|
$answer->getPoints() . ', a.correct='.$answer->getCorrect() |
318
|
|
|
. ' WHERE a.id=' .$quizReplyAnswer->getId() |
319
|
|
|
); |
320
|
|
|
$q->execute(); |
321
|
|
|
} else { |
322
|
|
|
$quizReplyAnswer->setPoints(0); |
323
|
|
|
$quizReplyAnswer->setCorrect(false); |
324
|
|
|
$q = $em->createQuery( |
325
|
|
|
'update PlaygroundGame\Entity\QuizReplyAnswer a SET a.points = 0, a.correct = false WHERE a.id=' . |
326
|
|
|
$quizReplyAnswer->getId() |
327
|
|
|
); |
328
|
|
|
$q->execute(); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
// The reply has been updated with correct answers and points for this question. |
334
|
|
|
// I count the whole set of points for this reply and update the entry |
335
|
|
|
if ($quizReplyAnswer->getCorrect()) { |
336
|
|
|
$quizPoints += $quizReplyAnswer->getPoints(); |
337
|
|
|
$quizCorrectAnswers += $quizReplyAnswer->getCorrect(); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
$winner = $this->isWinner($question->getQuiz(), $quizCorrectAnswers); |
342
|
|
|
$reply->getEntry()->setWinner($winner); |
343
|
|
|
$reply->getEntry()->setPoints($quizPoints); |
344
|
|
|
// The entry should be inactive : entry->setActive(false); |
345
|
|
|
$this->getEntryMapper()->update($reply->getEntry()); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$this->getEventManager()->trigger( |
349
|
|
|
__FUNCTION__.'.post', |
350
|
|
|
$this, |
351
|
|
|
array('question' => $question) |
352
|
|
|
); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* This function update the sort order of the questions in a Quiz |
357
|
|
|
* |
358
|
|
|
* @param string $data |
359
|
|
|
* @return boolean |
360
|
|
|
*/ |
361
|
|
|
public function sortQuestion($data) |
362
|
|
|
{ |
363
|
|
|
$arr = explode(",", $data); |
364
|
|
|
|
365
|
|
|
foreach ($arr as $k => $v) { |
366
|
|
|
$question = $this->getQuizQuestionMapper()->findById($v); |
367
|
|
|
$question->setPosition($k); |
368
|
|
|
$this->getQuizQuestionMapper()->update($question); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
return true; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return string |
376
|
|
|
*/ |
377
|
|
|
public function calculateMaxAnswersQuestion($question) |
378
|
|
|
{ |
379
|
|
|
$question_max_points = 0; |
380
|
|
|
$question_max_correct_answers = 0; |
381
|
|
|
// Closed question : Only one answer allowed |
382
|
|
|
if ($question->getType() == 0) { |
383
|
|
|
foreach ($question->getAnswers() as $answer) { |
384
|
|
|
if ($answer->getPoints() > $question_max_points) { |
385
|
|
|
$question_max_points = $answer->getPoints(); |
386
|
|
|
} |
387
|
|
|
if ($answer->getCorrect() && $question_max_correct_answers==0) { |
388
|
|
|
$question_max_correct_answers=1; |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
if ($question_max_correct_answers == 0) { |
392
|
|
|
return false; |
|
|
|
|
393
|
|
|
} |
394
|
|
|
// Closed question : Many answers allowed |
395
|
|
|
} elseif ($question->getType() == 1) { |
396
|
|
|
foreach ($question->getAnswers() as $answer) { |
397
|
|
|
$question_max_points += $answer->getPoints(); |
398
|
|
|
|
399
|
|
|
if ($answer->getCorrect()) { |
400
|
|
|
++$question_max_correct_answers; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
if ($question_max_correct_answers == 0) { |
404
|
|
|
return false; |
|
|
|
|
405
|
|
|
} |
406
|
|
|
// Not a question : A textarea to fill in |
407
|
|
|
} elseif ($question->getType() == 2) { |
408
|
|
|
$question_max_correct_answers = 0; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
$question->setMaxPoints($question_max_points); |
412
|
|
|
$question->setMaxCorrectAnswers($question_max_correct_answers); |
413
|
|
|
|
414
|
|
|
return $question; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
public function calculateMaxAnswersQuiz($quiz) |
418
|
|
|
{ |
419
|
|
|
$question_max_points = 0; |
420
|
|
|
$question_max_correct_answers = 0; |
421
|
|
|
foreach ($quiz->getQuestions() as $question) { |
422
|
|
|
$question_max_points += $question->getMaxPoints(); |
423
|
|
|
$question_max_correct_answers += $question->getMaxCorrectAnswers(); |
424
|
|
|
} |
425
|
|
|
$quiz->setMaxPoints($question_max_points); |
426
|
|
|
$quiz->setMaxCorrectAnswers($question_max_correct_answers); |
427
|
|
|
|
428
|
|
|
return $quiz; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
View Code Duplication |
public function getNumberCorrectAnswersQuiz($user, $count = 'count') |
|
|
|
|
432
|
|
|
{ |
433
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
434
|
|
|
|
435
|
|
|
$query = $em->createQuery( |
436
|
|
|
"SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e, PlaygroundGame\Entity\Game g |
437
|
|
|
WHERE e.user = :user |
438
|
|
|
AND g.classType = 'quiz' |
439
|
|
|
AND e.points > 0" |
440
|
|
|
); |
441
|
|
|
$query->setParameter('user', $user); |
442
|
|
|
$number = $query->getSingleScalarResult(); |
443
|
|
|
|
444
|
|
|
return $number; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
public function createQuizReply($data, $game, $user) |
448
|
|
|
{ |
449
|
|
|
// Si mon nb de participation est < au nb autorisé, j'ajoute une entry + reponses au quiz et points |
450
|
|
|
$quizReplyMapper = $this->getQuizReplyMapper(); |
451
|
|
|
$entryMapper = $this->getEntryMapper(); |
452
|
|
|
$entry = $this->findLastActiveEntry($game, $user); |
453
|
|
|
|
454
|
|
|
if (!$entry) { |
455
|
|
|
return false; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
$quizPoints = 0; |
459
|
|
|
$quizCorrectAnswers = 0; |
460
|
|
|
$maxCorrectAnswers = $game->getMaxCorrectAnswers(); |
461
|
|
|
$totalQuestions = 0; |
462
|
|
|
|
463
|
|
|
$quizReply = $this->getQuizReplyMapper()->getLastGameReply($entry); |
464
|
|
|
if (!$quizReply) { |
465
|
|
|
$quizReply = new QuizReply(); |
466
|
|
|
} else { |
467
|
|
|
$quizReplyAnswered = []; |
468
|
|
|
foreach ($quizReply->getAnswers() as $answer) { |
469
|
|
|
$quizReplyAnswered[$answer->getQuestionId()] = $answer; |
470
|
|
|
//$this->getQuizReplyAnswerMapper()->remove($answer); |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
foreach ($data as $group) { |
475
|
|
|
foreach ($group as $q => $a) { |
476
|
|
|
if (strlen($q) > 5 && strpos($q, '-data', strlen($q) - 5) !== false) { |
477
|
|
|
continue; // answer data is processed below |
478
|
|
|
} |
479
|
|
|
$question = $this->getQuizQuestionMapper()->findById((int) str_replace('q', '', $q)); |
480
|
|
|
++$totalQuestions; |
481
|
|
|
if (is_array($a)) { |
482
|
|
|
foreach ($a as $k => $answer_id) { |
483
|
|
|
$answer = $this->getQuizAnswerMapper()->findById($answer_id); |
484
|
|
View Code Duplication |
if ($answer) { |
|
|
|
|
485
|
|
|
if(isset($quizReplyAnswered[$question->getId()])){ |
486
|
|
|
$this->getQuizReplyAnswerMapper()->remove($quizReplyAnswered[$question->getId()]); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
$quizReplyAnswer = new QuizReplyAnswer(); |
490
|
|
|
$quizReplyAnswer->setAnswer($answer->getAnswer()); |
491
|
|
|
$quizReplyAnswer->setAnswerId($answer_id); |
492
|
|
|
$quizReplyAnswer->setQuestion($question->getQuestion()); |
493
|
|
|
$quizReplyAnswer->setQuestionId($question->getId()); |
494
|
|
|
$quizReplyAnswer->setPoints($answer->getPoints()); |
495
|
|
|
$quizReplyAnswer->setCorrect($answer->getCorrect()); |
496
|
|
|
|
497
|
|
|
$quizReply->addAnswer($quizReplyAnswer); |
498
|
|
|
|
499
|
|
|
$quizPoints += $answer->getPoints(); |
500
|
|
|
$quizCorrectAnswers += $answer->getCorrect(); |
501
|
|
|
|
502
|
|
|
if (isset($group[$q.'-'.$answer_id.'-data'])) { |
503
|
|
|
$quizReplyAnswer->setAnswerData($group[$q.'-'.$answer_id.'-data']); |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
} elseif ($question->getType() == 0 || $question->getType() == 1) { |
508
|
|
|
++$totalQuestions; |
509
|
|
|
$answer = $this->getQuizAnswerMapper()->findById($a); |
510
|
|
View Code Duplication |
if ($answer) { |
|
|
|
|
511
|
|
|
if(isset($quizReplyAnswered[$question->getId()])){ |
512
|
|
|
$this->getQuizReplyAnswerMapper()->remove($quizReplyAnswered[$question->getId()]); |
513
|
|
|
} |
514
|
|
|
$quizReplyAnswer = new QuizReplyAnswer(); |
515
|
|
|
$quizReplyAnswer->setAnswer($answer->getAnswer()); |
516
|
|
|
$quizReplyAnswer->setAnswerId($a); |
517
|
|
|
$quizReplyAnswer->setQuestion($question->getQuestion()); |
518
|
|
|
$quizReplyAnswer->setQuestionId($question->getId()); |
519
|
|
|
$quizReplyAnswer->setPoints($answer->getPoints()); |
520
|
|
|
$quizReplyAnswer->setCorrect($answer->getCorrect()); |
521
|
|
|
|
522
|
|
|
$quizReply->addAnswer($quizReplyAnswer); |
523
|
|
|
|
524
|
|
|
$quizPoints += $answer->getPoints(); |
525
|
|
|
$quizCorrectAnswers += $answer->getCorrect(); |
526
|
|
|
if (isset($group[$q.'-'.$a.'-data'])) { |
527
|
|
|
$quizReplyAnswer->setAnswerData($group[$q.'-'.$a.'-data']); |
528
|
|
|
} |
529
|
|
|
} |
530
|
|
|
} elseif ($question->getType() == 2) { |
531
|
|
|
++$totalQuestions; |
532
|
|
|
if(isset($quizReplyAnswered[$question->getId()])){ |
533
|
|
|
$this->getQuizReplyAnswerMapper()->remove($quizReplyAnswered[$question->getId()]); |
534
|
|
|
} |
535
|
|
|
$quizReplyAnswer = new QuizReplyAnswer(); |
536
|
|
|
$quizReplyAnswer->setAnswer($a); |
537
|
|
|
$quizReplyAnswer->setAnswerId(0); |
|
|
|
|
538
|
|
|
$quizReplyAnswer->setQuestion($question->getQuestion()); |
539
|
|
|
$quizReplyAnswer->setQuestionId($question->getId()); |
540
|
|
|
$quizReplyAnswer->setPoints(0); |
|
|
|
|
541
|
|
|
$quizReplyAnswer->setCorrect(0); |
|
|
|
|
542
|
|
|
|
543
|
|
|
$quizReply->addAnswer($quizReplyAnswer); |
544
|
|
|
|
545
|
|
|
$quizPoints += 0; |
546
|
|
|
$quizCorrectAnswers += 0; |
547
|
|
|
$qAnswers = $question->getAnswers(); |
548
|
|
|
foreach ($qAnswers as $qAnswer) { |
549
|
|
|
if (trim(strip_tags($a)) == trim(strip_tags($qAnswer->getAnswer()))) { |
550
|
|
|
$quizReplyAnswer->setPoints($qAnswer->getPoints()); |
551
|
|
|
$quizPoints += $qAnswer->getPoints(); |
552
|
|
|
$quizReplyAnswer->setCorrect($qAnswer->getCorrect()); |
553
|
|
|
$quizCorrectAnswers += $qAnswer->getCorrect(); |
554
|
|
|
break; |
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
if (isset($group[$q.'-'.$a.'-data'])) { |
559
|
|
|
$quizReplyAnswer->setAnswerData($group[$q.'-'.$a.'-data']); |
560
|
|
|
} |
561
|
|
|
} |
562
|
|
|
} |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
$winner = $this->isWinner($game, $quizCorrectAnswers); |
566
|
|
|
|
567
|
|
|
$entry->setWinner($winner); |
|
|
|
|
568
|
|
|
// Every winning participation is eligible to draw |
569
|
|
|
// Make this modifiable in the admin (choose who can participate to draw) |
570
|
|
|
$entry->setDrawable($winner); |
|
|
|
|
571
|
|
|
$entry->setPoints($quizPoints); |
|
|
|
|
572
|
|
|
$entry->setActive(false); |
|
|
|
|
573
|
|
|
$entry = $entryMapper->update($entry); |
574
|
|
|
|
575
|
|
|
$quizReply->setEntry($entry); |
576
|
|
|
$quizReply->setTotalCorrectAnswers($quizCorrectAnswers); |
577
|
|
|
$quizReply->setMaxCorrectAnswers($maxCorrectAnswers); |
578
|
|
|
$quizReply->setTotalQuestions($totalQuestions); |
579
|
|
|
|
580
|
|
|
$quizReplyMapper->insert($quizReply); |
581
|
|
|
|
582
|
|
|
$this->getEventManager()->trigger( |
583
|
|
|
__FUNCTION__.'.post', |
584
|
|
|
$this, |
585
|
|
|
array('user' => $user, 'entry' => $entry, 'reply' => $quizReply, 'game' => $game) |
586
|
|
|
); |
587
|
|
|
|
588
|
|
|
return $entry; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
public function isWinner($game, $quizCorrectAnswers = 0) |
592
|
|
|
{ |
593
|
|
|
// Pour déterminer le gagnant, je regarde le nombre max de reponses correctes possibles |
594
|
|
|
// dans le jeu, puis je calcule le ratio de bonnes réponses et le compare aux conditions |
595
|
|
|
// de victoire |
596
|
|
|
$winner = false; |
597
|
|
|
$maxCorrectAnswers = $game->getMaxCorrectAnswers(); |
598
|
|
|
if ($maxCorrectAnswers > 0) { |
599
|
|
|
$ratioCorrectAnswers = ($quizCorrectAnswers / $maxCorrectAnswers) * 100; |
600
|
|
|
} elseif ($game->getVictoryConditions() > 0) { |
601
|
|
|
// In the case I have a pronostic game for example |
602
|
|
|
$ratioCorrectAnswers = 0; |
603
|
|
|
} else { |
604
|
|
|
// In the case I want everybody to win |
605
|
|
|
$ratioCorrectAnswers = 100; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
if ($game->getVictoryConditions() >= 0) { |
609
|
|
|
if ($ratioCorrectAnswers >= $game->getVictoryConditions()) { |
610
|
|
|
$winner = true; |
611
|
|
|
} |
612
|
|
|
} |
613
|
|
|
return $winner; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
public function getEntriesHeader($game) |
617
|
|
|
{ |
618
|
|
|
$header = parent::getEntriesHeader($game); |
619
|
|
|
$header['totalCorrectAnswers'] = 1; |
620
|
|
|
|
621
|
|
|
return $header; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
View Code Duplication |
public function getEntriesQuery($game) |
|
|
|
|
625
|
|
|
{ |
626
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
627
|
|
|
|
628
|
|
|
$qb = $em->createQueryBuilder(); |
629
|
|
|
$qb->select(' |
630
|
|
|
r.id, |
631
|
|
|
u.username, |
632
|
|
|
u.title, |
633
|
|
|
u.firstname, |
634
|
|
|
u.lastname, |
635
|
|
|
u.email, |
636
|
|
|
u.optin, |
637
|
|
|
u.optinPartner, |
638
|
|
|
u.address, |
639
|
|
|
u.address2, |
640
|
|
|
u.postalCode, |
641
|
|
|
u.city, |
642
|
|
|
u.telephone, |
643
|
|
|
u.mobile, |
644
|
|
|
u.created_at, |
645
|
|
|
u.dob, |
646
|
|
|
e.winner, |
647
|
|
|
e.socialShares, |
648
|
|
|
e.playerData, |
649
|
|
|
e.updated_at, |
650
|
|
|
r.totalCorrectAnswers |
651
|
|
|
') |
652
|
|
|
->from('PlaygroundGame\Entity\QuizReply', 'r') |
653
|
|
|
->innerJoin('r.entry', 'e') |
654
|
|
|
->leftJoin('e.user', 'u') |
655
|
|
|
->where($qb->expr()->eq('e.game', ':game')); |
656
|
|
|
|
657
|
|
|
$qb->setParameter('game', $game); |
658
|
|
|
|
659
|
|
|
return $qb->getQuery(); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
public function getGameEntity() |
663
|
|
|
{ |
664
|
|
|
return new \PlaygroundGame\Entity\Quiz; |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* getQuizMapper |
669
|
|
|
* |
670
|
|
|
* @return QuizMapperInterface |
671
|
|
|
*/ |
672
|
|
|
public function getQuizMapper() |
673
|
|
|
{ |
674
|
|
|
if (null === $this->quizMapper) { |
675
|
|
|
$this->quizMapper = $this->getServiceManager()->get('playgroundgame_quiz_mapper'); |
|
|
|
|
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
return $this->quizMapper; |
|
|
|
|
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* setQuizMapper |
683
|
|
|
* |
684
|
|
|
* @param QuizMapperInterface $quizMapper |
685
|
|
|
* @return Game |
686
|
|
|
*/ |
687
|
|
|
public function setQuizMapper(GameMapperInterface $quizMapper) |
688
|
|
|
{ |
689
|
|
|
$this->quizMapper = $quizMapper; |
|
|
|
|
690
|
|
|
|
691
|
|
|
return $this; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* getQuizQuestionMapper |
696
|
|
|
* |
697
|
|
|
* @return QuizQuestionMapperInterface |
698
|
|
|
*/ |
699
|
|
|
public function getQuizQuestionMapper() |
700
|
|
|
{ |
701
|
|
|
if (null === $this->quizQuestionMapper) { |
702
|
|
|
$this->quizQuestionMapper = $this->getServiceManager()->get('playgroundgame_quizquestion_mapper'); |
|
|
|
|
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
return $this->quizQuestionMapper; |
|
|
|
|
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* setQuizQuestionMapper |
710
|
|
|
* |
711
|
|
|
* @param QuizQuestionMapperInterface $quizquestionMapper |
712
|
|
|
* @return Quiz |
713
|
|
|
*/ |
714
|
|
|
public function setQuizQuestionMapper($quizquestionMapper) |
715
|
|
|
{ |
716
|
|
|
$this->quizQuestionMapper = $quizquestionMapper; |
717
|
|
|
|
718
|
|
|
return $this; |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
/** |
722
|
|
|
* setQuizAnswerMapper |
723
|
|
|
* |
724
|
|
|
* @param QuizAnswerMapperInterface $quizAnswerMapper |
725
|
|
|
* @return Quiz |
726
|
|
|
*/ |
727
|
|
|
public function setQuizAnswerMapper($quizAnswerMapper) |
728
|
|
|
{ |
729
|
|
|
$this->quizAnswerMapper = $quizAnswerMapper; |
730
|
|
|
|
731
|
|
|
return $this; |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
/** |
735
|
|
|
* getQuizAnswerMapper |
736
|
|
|
* |
737
|
|
|
* @return QuizAnswerMapperInterface |
738
|
|
|
*/ |
739
|
|
|
public function getQuizAnswerMapper() |
740
|
|
|
{ |
741
|
|
|
if (null === $this->quizAnswerMapper) { |
742
|
|
|
$this->quizAnswerMapper = $this->getServiceManager()->get('playgroundgame_quizanswer_mapper'); |
|
|
|
|
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
return $this->quizAnswerMapper; |
|
|
|
|
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* getQuizReplyMapper |
750
|
|
|
* |
751
|
|
|
* @return QuizReplyMapperInterface |
752
|
|
|
*/ |
753
|
|
|
public function getQuizReplyMapper() |
754
|
|
|
{ |
755
|
|
|
if (null === $this->quizReplyMapper) { |
756
|
|
|
$this->quizReplyMapper = $this->getServiceManager()->get('playgroundgame_quizreply_mapper'); |
|
|
|
|
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
return $this->quizReplyMapper; |
|
|
|
|
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
/** |
763
|
|
|
* setQuizReplyMapper |
764
|
|
|
* |
765
|
|
|
* @param QuizReplyMapperInterface $quizreplyMapper |
766
|
|
|
* @return Quiz |
767
|
|
|
*/ |
768
|
|
|
public function setQuizReplyMapper($quizreplyMapper) |
769
|
|
|
{ |
770
|
|
|
$this->quizReplyMapper = $quizreplyMapper; |
771
|
|
|
|
772
|
|
|
return $this; |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
/** |
776
|
|
|
* getQuizReplyAnswerMapper |
777
|
|
|
* |
778
|
|
|
* @return QuizReplyAnswerMapper |
779
|
|
|
*/ |
780
|
|
|
public function getQuizReplyAnswerMapper() |
781
|
|
|
{ |
782
|
|
|
if (null === $this->quizReplyAnswerMapper) { |
783
|
|
|
$this->quizReplyAnswerMapper = $this->getServiceManager()->get('playgroundgame_quizreplyanswer_mapper'); |
|
|
|
|
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
return $this->quizReplyAnswerMapper; |
|
|
|
|
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* setQuizReplyAnswerMapper |
791
|
|
|
* |
792
|
|
|
* @param QuizReplyAnswerMapper $quizReplyAnswerMapper |
793
|
|
|
* @return Quiz |
794
|
|
|
*/ |
795
|
|
|
public function setQuizReplyAnswerMapper($quizReplyAnswerMapper) |
796
|
|
|
{ |
797
|
|
|
$this->quizReplyAnswerMapper = $quizReplyAnswerMapper; |
798
|
|
|
|
799
|
|
|
return $this; |
800
|
|
|
} |
801
|
|
|
} |
802
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.