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