mirrored from git://git.moodle.org/moodle.git
/
multichoice.php
574 lines (516 loc) · 24.2 KB
/
multichoice.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Multichoice
*
* @package mod
* @subpackage lesson
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
defined('MOODLE_INTERNAL') || die();
/** Multichoice question type */
define("LESSON_PAGE_MULTICHOICE", "3");
class lesson_page_type_multichoice extends lesson_page {
protected $type = lesson_page::TYPE_QUESTION;
protected $typeidstring = 'multichoice';
protected $typeid = LESSON_PAGE_MULTICHOICE;
protected $string = null;
public function get_typeid() {
return $this->typeid;
}
public function get_typestring() {
if ($this->string===null) {
$this->string = get_string($this->typeidstring, 'lesson');
}
return $this->string;
}
public function get_idstring() {
return $this->typeidstring;
}
/**
* Gets an array of the jumps used by the answers of this page
*
* @return array
*/
public function get_jumps() {
global $DB;
$jumps = array();
if ($answers = $this->get_answers()) {
foreach ($answers as $answer) {
if ($answer->answer === '') {
// show only jumps for real branches (==have description)
continue;
}
$jumps[] = $this->get_jump_name($answer->jumpto);
}
} else {
// We get here is the lesson was created on a Moodle 1.9 site and
// the lesson contains question pages without any answers.
$jumps[] = $this->get_jump_name($this->properties->nextpageid);
}
return $jumps;
}
public function get_used_answers() {
$answers = $this->get_answers();
foreach ($answers as $key=>$answer) {
if ($answer->answer === '') {
unset($answers[$key]);
}
}
return $answers;
}
public function display($renderer, $attempt) {
global $CFG, $PAGE;
$answers = $this->get_used_answers();
shuffle($answers);
$action = $CFG->wwwroot.'/mod/lesson/continue.php';
$params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents(), 'attempt'=>$attempt);
if ($this->properties->qoption) {
$mform = new lesson_display_answer_form_multichoice_multianswer($action, $params);
} else {
$mform = new lesson_display_answer_form_multichoice_singleanswer($action, $params);
}
$data = new stdClass;
$data->id = $PAGE->cm->id;
$data->pageid = $this->properties->id;
$mform->set_data($data);
return $mform->display();
}
public function check_answer() {
global $DB, $CFG, $PAGE;
$result = parent::check_answer();
$formattextdefoptions = new stdClass();
$formattextdefoptions->noclean = true;
$formattextdefoptions->para = false;
$answers = $this->get_used_answers();
shuffle($answers);
$action = $CFG->wwwroot.'/mod/lesson/continue.php';
$params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents());
if ($this->properties->qoption) {
$mform = new lesson_display_answer_form_multichoice_multianswer($action, $params);
} else {
$mform = new lesson_display_answer_form_multichoice_singleanswer($action, $params);
}
$data = $mform->get_data();
require_sesskey();
if (!$data) {
redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id)));
}
if ($this->properties->qoption) {
// MULTIANSWER allowed, user's answer is an array
if (empty($data->answer) || !is_array($data->answer)) {
$result->noanswer = true;
return $result;
}
$studentanswers = array();
foreach ($data->answer as $key=>$value) {
$studentanswers[] = (int)$key;
}
// get what the user answered
$result->userresponse = implode(",", $studentanswers);
// get the answers in a set order, the id order
$answers = $this->get_used_answers();
$ncorrect = 0;
$nhits = 0;
$responses = array();
$correctanswerid = 0;
$wronganswerid = 0;
// store student's answers for displaying on feedback page
$result->studentanswer = '';
foreach ($answers as $answer) {
foreach ($studentanswers as $answerid) {
if ($answerid == $answer->id) {
$result->studentanswer .= '<br />'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
if (trim(strip_tags($answer->response))) {
$responses[$answerid] = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
}
}
}
}
$correctpageid = null;
$wrongpageid = null;
// this is for custom scores. If score on answer is positive, it is correct
if ($this->lesson->custom) {
$ncorrect = 0;
$nhits = 0;
foreach ($answers as $answer) {
if ($answer->score > 0) {
$ncorrect++;
foreach ($studentanswers as $answerid) {
if ($answerid == $answer->id) {
$nhits++;
}
}
// save the first jumpto page id, may be needed!...
if (!isset($correctpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$correctpageid = $answer->jumpto;
}
// save the answer id for scoring
if ($correctanswerid == 0) {
$correctanswerid = $answer->id;
}
} else {
// save the first jumpto page id, may be needed!...
if (!isset($wrongpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$wrongpageid = $answer->jumpto;
}
// save the answer id for scoring
if ($wronganswerid == 0) {
$wronganswerid = $answer->id;
}
}
}
} else {
foreach ($answers as $answer) {
if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
$ncorrect++;
foreach ($studentanswers as $answerid) {
if ($answerid == $answer->id) {
$nhits++;
}
}
// save the first jumpto page id, may be needed!...
if (!isset($correctpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$correctpageid = $answer->jumpto;
}
// save the answer id for scoring
if ($correctanswerid == 0) {
$correctanswerid = $answer->id;
}
} else {
// save the first jumpto page id, may be needed!...
if (!isset($wrongpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$wrongpageid = $answer->jumpto;
}
// save the answer id for scoring
if ($wronganswerid == 0) {
$wronganswerid = $answer->id;
}
}
}
}
if ((count($studentanswers) == $ncorrect) and ($nhits == $ncorrect)) {
$result->correctanswer = true;
$result->response = implode('<br />', $responses);
$result->newpageid = $correctpageid;
$result->answerid = $correctanswerid;
} else {
$result->response = implode('<br />', $responses);
$result->newpageid = $wrongpageid;
$result->answerid = $wronganswerid;
}
} else {
// only one answer allowed
if (!isset($data->answerid) || (empty($data->answerid) && !is_int($data->answerid))) {
$result->noanswer = true;
return $result;
}
$result->answerid = $data->answerid;
if (!$answer = $DB->get_record("lesson_answers", array("id" => $result->answerid))) {
print_error("Continue: answer record not found");
}
if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
$result->correctanswer = true;
}
if ($this->lesson->custom) {
if ($answer->score > 0) {
$result->correctanswer = true;
} else {
$result->correctanswer = false;
}
}
$result->newpageid = $answer->jumpto;
$result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
$result->userresponse = format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
$result->studentanswer = $result->userresponse;
}
return $result;
}
public function option_description_string() {
if ($this->properties->qoption) {
return " - ".get_string("multianswer", "lesson");
}
return parent::option_description_string();
}
public function display_answers(html_table $table) {
$answers = $this->get_used_answers();
$options = new stdClass;
$options->noclean = true;
$options->para = false;
$i = 1;
foreach ($answers as $answer) {
$cells = array();
if ($this->lesson->custom && $answer->score > 0) {
// if the score is > 0, then it is correct
$cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
} else if ($this->lesson->custom) {
$cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
} else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
// underline correct answers
$cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n";
} else {
$cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
}
$cells[] = format_text($answer->answer, $answer->answerformat, $options);
$table->data[] = new html_table_row($cells);
$cells = array();
$cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
$cells[] = format_text($answer->response, $answer->responseformat, $options);
$table->data[] = new html_table_row($cells);
$cells = array();
$cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
$cells[] = $answer->score;
$table->data[] = new html_table_row($cells);
$cells = array();
$cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
$cells[] = $this->get_jump_name($answer->jumpto);
$table->data[] = new html_table_row($cells);
if ($i === 1){
$table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
}
$i++;
}
return $table;
}
public function stats(array &$pagestats, $tries) {
if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
$temp = $tries[$this->lesson->maxattempts - 1];
} else {
// else, user attempted the question less than the max, so grab the last one
$temp = end($tries);
}
if ($this->properties->qoption) {
$userresponse = explode(",", $temp->useranswer);
foreach ($userresponse as $response) {
if (isset($pagestats[$temp->pageid][$response])) {
$pagestats[$temp->pageid][$response]++;
} else {
$pagestats[$temp->pageid][$response] = 1;
}
}
} else {
if (isset($pagestats[$temp->pageid][$temp->answerid])) {
$pagestats[$temp->pageid][$temp->answerid]++;
} else {
$pagestats[$temp->pageid][$temp->answerid] = 1;
}
}
if (isset($pagestats[$temp->pageid]["total"])) {
$pagestats[$temp->pageid]["total"]++;
} else {
$pagestats[$temp->pageid]["total"] = 1;
}
return true;
}
public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
$answers = $this->get_used_answers();
$formattextdefoptions = new stdClass;
$formattextdefoptions->para = false; //I'll use it widely in this page
foreach ($answers as $answer) {
if ($this->properties->qoption) {
if ($useranswer == NULL) {
$userresponse = array();
} else {
$userresponse = explode(",", $useranswer->useranswer);
}
if (in_array($answer->id, $userresponse)) {
// make checked
$data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />";
if (!isset($answerdata->response)) {
if ($answer->response == NULL) {
if ($useranswer->correct) {
$answerdata->response = get_string("thatsthecorrectanswer", "lesson");
} else {
$answerdata->response = get_string("thatsthewronganswer", "lesson");
}
} else {
$answerdata->response = $answer->response;
}
}
if (!isset($answerdata->score)) {
if ($this->lesson->custom) {
$answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
} elseif ($useranswer->correct) {
$answerdata->score = get_string("receivedcredit", "lesson");
} else {
$answerdata->score = get_string("didnotreceivecredit", "lesson");
}
}
} else {
// unchecked
$data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />";
}
if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) {
$data = "<div class=highlight>".$data.' '.format_text($answer->answer,$answer->answerformat,$formattextdefoptions)."</div>";
} else {
$data .= format_text($answer->answer,$answer->answerformat,$formattextdefoptions);
}
} else {
if ($useranswer != NULL and $answer->id == $useranswer->answerid) {
// make checked
$data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />";
if ($answer->response == NULL) {
if ($useranswer->correct) {
$answerdata->response = get_string("thatsthecorrectanswer", "lesson");
} else {
$answerdata->response = get_string("thatsthewronganswer", "lesson");
}
} else {
$answerdata->response = $answer->response;
}
if ($this->lesson->custom) {
$answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
} elseif ($useranswer->correct) {
$answerdata->score = get_string("receivedcredit", "lesson");
} else {
$answerdata->score = get_string("didnotreceivecredit", "lesson");
}
} else {
// unchecked
$data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />";
}
if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) {
$data = "<div class=\"highlight\">".$data.' '.format_text($answer->answer,FORMAT_MOODLE,$formattextdefoptions)."</div>";
} else {
$data .= format_text($answer->answer,$answer->answerformat,$formattextdefoptions);
}
}
if (isset($pagestats[$this->properties->id][$answer->id])) {
$percent = $pagestats[$this->properties->id][$answer->id] / $pagestats[$this->properties->id]["total"] * 100;
$percent = round($percent, 2);
$percent .= "% ".get_string("checkedthisone", "lesson");
} else {
$percent = get_string("noonecheckedthis", "lesson");
}
$answerdata->answers[] = array($data, $percent);
$answerpage->answerdata = $answerdata;
}
return $answerpage;
}
}
class lesson_add_page_form_multichoice extends lesson_add_page_form_base {
public $qtype = 'multichoice';
public $qtypestring = 'multichoice';
public function custom_definition() {
$this->_form->addElement('checkbox', 'qoption', get_string('options', 'lesson'), get_string('multianswer', 'lesson'));
$this->_form->setDefault('qoption', 0);
$this->_form->addHelpButton('qoption', 'multianswer', 'lesson');
for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
$this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
$this->add_answer($i, NULL, ($i<2));
$this->add_response($i);
$this->add_jumpto($i, NULL, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
$this->add_score($i, null, ($i===0)?1:0);
}
}
}
class lesson_display_answer_form_multichoice_singleanswer extends moodleform {
public function definition() {
global $USER, $OUTPUT;
$mform = $this->_form;
$answers = $this->_customdata['answers'];
$lessonid = $this->_customdata['lessonid'];
$contents = $this->_customdata['contents'];
if (array_key_exists('attempt', $this->_customdata)) {
$attempt = $this->_customdata['attempt'];
} else {
$attempt = new stdClass();
$attempt->answerid = null;
}
$mform->addElement('header', 'pageheader');
$mform->addElement('html', $OUTPUT->container($contents, 'contents'));
$hasattempt = false;
$disabled = '';
if (isset($USER->modattempts[$lessonid]) && !empty($USER->modattempts[$lessonid])) {
$hasattempt = true;
$disabled = array('disabled' => 'disabled');
}
$options = new stdClass;
$options->para = false;
$options->noclean = true;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'pageid');
$mform->setType('pageid', PARAM_INT);
$i = 0;
foreach ($answers as $answer) {
$mform->addElement('html', '<div class="answeroption">');
$mform->addElement('radio','answerid',null,format_text($answer->answer, $answer->answerformat, $options),$answer->id, $disabled);
$mform->setType('answer'.$i, PARAM_INT);
if ($hasattempt && $answer->id == $USER->modattempts[$lessonid]->answerid) {
$mform->setDefault('answerid', $USER->modattempts[$lessonid]->answerid);
}
$mform->addElement('html', '</div>');
$i++;
}
if ($hasattempt) {
$this->add_action_buttons(null, get_string("nextpage", "lesson"));
} else {
$this->add_action_buttons(null, get_string("submit", "lesson"));
}
}
}
class lesson_display_answer_form_multichoice_multianswer extends moodleform {
public function definition() {
global $USER, $OUTPUT;
$mform = $this->_form;
$answers = $this->_customdata['answers'];
$lessonid = $this->_customdata['lessonid'];
$contents = $this->_customdata['contents'];
$mform->addElement('header', 'pageheader');
$mform->addElement('html', $OUTPUT->container($contents, 'contents'));
$hasattempt = false;
$disabled = '';
$useranswers = array();
if (isset($USER->modattempts[$lessonid]) && !empty($USER->modattempts[$lessonid])) {
$hasattempt = true;
$disabled = array('disabled' => 'disabled');
$useranswers = explode(',', $USER->modattempts[$lessonid]->useranswer);
}
$options = new stdClass;
$options->para = false;
$options->noclean = true;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'pageid');
$mform->setType('pageid', PARAM_INT);
foreach ($answers as $answer) {
$mform->addElement('html', '<div class="answeroption">');
$answerid = 'answer['.$answer->id.']';
if ($hasattempt && in_array($answer->id, $useranswers)) {
$answerid = 'answer_'.$answer->id;
$mform->addElement('hidden', 'answer['.$answer->id.']', $answer->answer);
$mform->setType('answer['.$answer->id.']', PARAM_TEXT);
$mform->setDefault($answerid, true);
$mform->setDefault('answer['.$answer->id.']', true);
}
// NOTE: our silly checkbox supports only value '1' - we can not use it like the radiobox above!!!!!!
$mform->addElement('checkbox', $answerid, null, format_text($answer->answer, $answer->answerformat, $options), $disabled);
$mform->setType($answerid, PARAM_INT);
$mform->addElement('html', '</div>');
}
if ($hasattempt) {
$this->add_action_buttons(null, get_string("nextpage", "lesson"));
} else {
$this->add_action_buttons(null, get_string("submit", "lesson"));
}
}
}