-
Notifications
You must be signed in to change notification settings - Fork 1
/
CBQuizPage.php
397 lines (337 loc) · 12.3 KB
/
CBQuizPage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');
/**
* Contao Open Source CMS
* Copyright (C) 2005-2011 Leo Feyer
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, please visit the Free
* Software Foundation website at <http://www.gnu.org/licenses/>.
*
* PHP version 5
* @copyright Winans Creative 2011, Helmut SchottmÙller 2009
* @author Blair Winans <blair@winanscreative.com>
* @author Fred Bliss <fred.bliss@intelligentspark.com>
* @author Adam Fisher <adam@winanscreative.com>
* @author Includes code from survey_ce module from Helmut SchottmÙller <typolight@aurealis.de>
* @license http://opensource.org/licenses/lgpl-3.0.html
*/
class CBQuizPage extends CBPage
{
/**
* Page template
*/
protected $strTemplate = 'cb_quizpage_default';
/**
* Question Sequence
* @var array
*/
protected $arrQuestionSequence;
/**
* Quiz Object
* @var array
*/
public $objQuiz;
/**
* Name of the temporary quiz cookie
* @var string
*/
protected $strCookie = 'CB_TEMP_QUIZDATA';
/**
*Review status
* @var bool
*/
protected $blnReview;
/**
* Construct the object
*/
public function __construct($arrData, $objQuiz, $intCurrpage, $blnReview)
{
parent::__construct($arrData, $objQuiz, $intCurrpage);
$this->blnReview = $blnReview;
$this->objQuiz = $objQuiz;
}
/**
* Set an object property
*
* @access public
* @param string
* @param mixed
*/
public function __set($strKey, $varValue)
{
$this->arrData[$strKey] = $varValue;
}
/**
* Return an object property
*
* @access public
* @param string
* @return mixed
*/
public function __get($strKey)
{
return $this->arrData[$strKey];
}
/**
* Generate the page
*/
public function generate($objModule)
{
$strTemplate = $this->page_template ? $this->page_template : $this->strTemplate;
$objTemplate = new FrontendTemplate($strTemplate);
$objTemplate->questions = array();
$arrQuestions = $this->buildQuestions();
if(is_array($arrQuestions))
{
$objTemplate->questions = $arrQuestions;
}
else
{
$objTemplate->text = $arrQuestions;
}
$objTemplate->buttons = $this->CourseBuilder->getButtons($this);
//Standard config
$objTemplate->enctype ='application/x-www-form-urlencoded';
$objTemplate->formId = $this->CourseBuilder->getFormID();
$objTemplate->pageID = $this->intCurrpage;
$objTemplate->sectionID = 'quiz|' . $this->objQuiz->id;
$objTemplate->parentID = $this->CourseBuilder->Quiz->pid;
$objTemplate->action = ampersand($this->Environment->request, true);
$objTemplate->formSubmit = $this->CourseBuilder->getFormID();
$objTemplate->score = $this->intCurrpage > $this->total_pages ? $this->CourseBuilder->Quiz->score : '';
$objTemplate->showCourseNav = ($this->objQuiz->final && $this->strStatus == 'failure' && $this->objQuiz->reviewable && $this->objQuiz->canretake) ? true : ((!$this->objQuiz->final && $this->intCurrpage > $this->total_pages) ? true : false);
if($this->CourseBuilder->Course->coursenavmodule)
{
$objTemplate->courseNav = $this->getFrontendModule($this->CourseBuilder->Course->coursenavmodule);
}
$objTemplate->wrongMsg = $GLOBALS['TL_LANG']['CB']['MISC']['wrong'];
$objTemplate->question_label = $GLOBALS['TL_LANG']['CB']['MISC']['question_label'];
$objTemplate->response_label = $GLOBALS['TL_LANG']['CB']['MISC']['response_label'];
$objTemplate->correct_label = $GLOBALS['TL_LANG']['CB']['MISC']['correct_label'];
$objTemplate->wrong = $this->intCurrpage > $this->total_pages ? $this->CourseBuilder->Quiz->getWrong() : array();
return $objTemplate->parse();
}
/**
* Create an array of widgets containing the questions on a given page
*
* @param array
* @param boolean
*/
protected function buildQuestions()
{
$this->CourseBuilder->Quiz->lastpage = $this->intCurrpage;
if($this->intCurrpage > $this->CourseBuilder->Quiz->maxpage)
{
$this->CourseBuilder->Quiz->maxpage = $this->intCurrpage;
}
//Checks for Introduction/Success/Failure
if($this->intCurrpage==0 && $this->Input->post('FORM_SUBMIT') != $this->CourseBuilder->getFormID())
{
return $this->objQuiz->introduction;
}
elseif($this->intCurrpage > $this->total_pages)
{
//Set score and log attempt/status. Check for division by zero
$this->CourseBuilder->Quiz->score = $this->CourseBuilder->Quiz->total > 0 ? ($this->CourseBuilder->Quiz->correct/$this->CourseBuilder->Quiz->total)*100 : 0;
$this->strStatus = ($this->CourseBuilder->Quiz->score >= $this->objQuiz->passing_score ? 'success' : 'failure');
$this->CourseBuilder->Quiz->status = ($this->strStatus == 'success' || ($this->objQuiz->canretake && !$this->objQuiz->final) ? 'complete' : ($this->objQuiz->final ? 'in_progress' : 'skipped'));
if($this->strStatus == 'failure' && !$this->objQuiz->final && $this->objQuiz->canretake)
{
$this->CourseBuilder->Quiz->maxpage = 0;
$this->CourseBuilder->Quiz->lastpage = 0;
}
if ($this->CourseBuilder->Quiz->status == 'complete')
{
if ($this->objQuiz->final && $this->strStatus == 'success')
{
$this->CourseBuilder->Quiz->lastitem = 0;
$this->reload(); //If this is the final quiz, just reload to let the Course display a success/fail message unless
}
elseif(!$this->objQuiz->final)
{
$this->CourseBuilder->Quiz->lastitem = 0;
}
}
//Return success/failure
return $this->objQuiz->{$this->strStatus};
}
$arrPageQuestions = array();
$intQuestionNum = 1;
$arrQuestions = $this->getQuestions();
foreach ($arrQuestions as $question)
{
$strClass = $GLOBALS['CB_QUESTION'][$question['questiontype']];
// Continue if the class is not defined
if (!$this->classFileExists($strClass))
{
continue;
}
$objWidget = new $strClass();
$objWidget->quizdata = $question;
$objWidget->absoluteNumber = $this->getQuestionSequence($question['id'], $this->objQuiz->id);
$objWidget->pageQuestionNumber = $intQuestionNum;
$objWidget->pageNumber = $this->intCurrpage;
if ($this->Input->post('FORM_SUBMIT') == $this->CourseBuilder->getFormID() && $this->Input->post('SECTION_ID')=='quiz|' . $this->objQuiz->id)
{
//Do not require mandatory answers on previous button or if the currentPage equals 0
if($this->Input->post('prev') && $this->Input->post('prev') == $GLOBALS['TL_LANG']['CB']['buttonprevpage'] || $this->intCurrpage==0)
{
$objWidget->mandatory = false;
}
$objWidget->validate();
if ($objWidget->hasErrors())
{
$this->doNotSubmit = true;
}
//Write responses to db
//@todo - Add in ability to handle text ("other") responses
$arrChoices = deserialize($question['choices']);
$boolValid = ($arrChoices['answer']==($objWidget->value['value']-1)) ? true : false;
$this->CourseBuilder->Quiz->addResponse( $question['id'], $objWidget->value, $boolValid );
}
else
{
if ($this->CourseBuilder->Quiz->question[$question['id']])
{
$objWidget->value = $this->CourseBuilder->Quiz->question[$question['id']];
}
}
array_push($arrPageQuestions, $objWidget);
$intQuestionNum++;
}
if ($this->Input->post('FORM_SUBMIT') == $this->CourseBuilder->getFormID() && !$this->doNotSubmit && $this->Input->post('SECTION_ID')=='quiz|' . $this->objQuiz->id)
{
// HOOK: pass validated questions to callback functions
if (isset($GLOBALS['TL_HOOKS']['quizQuestionsValidated']) && is_array($GLOBALS['TL_HOOKS']['quizQuestionsValidated']))
{
foreach ($GLOBALS['TL_HOOKS']['quizQuestionsValidated'] as $callback)
{
$this->import($callback[0]);
$this->$callback[0]->$callback[1]($arrPageQuestions, $this);
}
}
if($this->Input->post('prev') && $this->Input->post('prev') == $GLOBALS['TL_LANG']['CB']['buttonprevpage'])
{
$this->CourseBuilder->moveBackward('Quiz', $this->total_pages);
}
else
{
$this->CourseBuilder->moveForward('Quiz', $this->total_pages);
}
}
else
{
// HOOK: pass loaded questions to callback functions
if (isset($GLOBALS['TL_HOOKS']['quizQuestionsLoaded']) && is_array($GLOBALS['TL_HOOKS']['quizQuestionsLoaded']))
{
foreach ($GLOBALS['TL_HOOKS']['quizQuestionsLoaded'] as $callback)
{
$this->import($callback[0]);
$this->$callback[0]->$callback[1]($arrPageQuestions, $this);
}
}
}
return $arrPageQuestions;
}
/**
* Return the total position of a question in the sequence of the quiz
*
* @param array
* @param boolean
*/
protected function getQuestionSequence($intQuestionID, $intQuizID)
{
if ($intQuestionID > 0 && $intQuizID > 0)
{
if (!count($this->arrQuestionSequence))
{
$strOrderby = "ORDER BY tl_cb_quizpage.sorting, tl_cb_quizquestion.sorting";
if( $this->objQuiz->randomize && strlen( $this->CourseBuilder->Quiz->sequencekey ) )
{
$arrKeys = trimsplit(',',$this->CourseBuilder->Quiz->sequencekey);
$strOrderby = "ORDER BY FIELD(tl_cb_quizquestion.id,". implode(',',$arrKeys).")";
}
$this->arrQuestionSequence = $this->Database->prepare("SELECT tl_cb_quizquestion.id FROM tl_cb_quizquestion, tl_cb_quizpage WHERE tl_cb_quizquestion.pid = tl_cb_quizpage.id AND tl_cb_quizpage.pid =? ". $strOrderby)
->executeUncached($intQuizID)
->fetchEach('id');
}
return array_search($intQuestionID, $this->arrQuestionSequence) + 1;
}
else
{
return 0;
}
}
/**
* Return the array of questions for this page
*
* @return array
*/
protected function getQuestions( )
{
$arrQuestions = array();
//Check for randomization
if( $this->objQuiz->randomize && $this->intCurrpage > 0 )
{
if( strlen( $this->CourseBuilder->Quiz->sequencekey ) ) //Check for existing sequence key
{
$arrKeys = trimsplit(',',$this->CourseBuilder->Quiz->sequencekey);
if( $this->objQuiz->questionsPerPage > 0 )
{
$arrChunks = array_chunk( $arrKeys, $this->objQuiz->questionsPerPage );
$arrKeys = $arrChunks[$this->intCurrpage-1];
}
//Thanks to http://optimmysql.blogspot.com/2007/09/customized-order-by-sequence-small-hack.html for this customized order-by trick
$arrQuestions = $this->Database->prepare("SELECT * FROM tl_cb_quizquestion WHERE id IN(".implode(',',$arrKeys).") ORDER BY FIELD(id,". implode(',',$arrKeys).")")
->execute()
->fetchAllAssoc();
}
else
{
//Get count of all pages
$arrPages = $this->Database->prepare("SELECT * FROM tl_cb_quizpage WHERE pid=? ORDER BY sorting ASC")->execute( $this->objQuiz->id )->fetchEach('id');
if( count($arrPages) )
{
//Need to get all questions for this Quiz
//@todo - FInd a more random method - See http://www.greggdev.com/web/articles.php?id=6
$objQuestions = $this->Database->prepare("SELECT * FROM tl_cb_quizquestion WHERE pid IN(".implode(',',$arrPages).") ORDER BY RAND()");
//Check for need to limit
if( $this->objQuiz->questionsPerPage > 0 )
{
$intTotal = (int) $this->objQuiz->questionsPerPage * (int) count( $arrPages );
$objQuestions->limit( $intTotal );
$blnChunk = true;
}
//Execute query
$objQuestions = $objQuestions->execute();
$arrQuestions = $objQuestions->fetchAllAssoc();
$arrIDs = $objQuestions->fetchEach('id');
if( $blnChunk )
{
$arrChunks = array_chunk( $arrQuestions, $this->objQuiz->questionsPerPage );
$arrQuestions = $arrChunks[$this->intCurrpage-1];
}
//Now we set the sequence key so we can retrieve the same random set the next time
$this->CourseBuilder->Quiz->sequencekey = implode(',',$arrIDs);
}
}
}
else //Use default
{
$arrQuestions = $this->Database->prepare("SELECT * FROM tl_cb_quizquestion WHERE pid=? ORDER BY sorting ASC")
->execute($this->id)
->fetchAllAssoc();
}
return $arrQuestions;
}
}