emergency release 2.2dev
[moodle.git] / mod / lesson / pagetypes / matching.php
CommitLineData
0a4abb73
SH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Matching
20 *
cc3dbaaa
PS
21 * @package mod
22 * @subpackage lesson
23 * @copyright 2009 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0a4abb73
SH
25 **/
26
1e7f8ea2
PS
27defined('MOODLE_INTERNAL') || die();
28
0a4abb73
SH
29/** Matching question type */
30define("LESSON_PAGE_MATCHING", "5");
31
32class lesson_page_type_matching extends lesson_page {
33
34 protected $type = lesson_page::TYPE_QUESTION;
35 protected $typeid = LESSON_PAGE_MATCHING;
36 protected $typeidstring = 'matching';
37 protected $string = null;
38
39 public function get_typeid() {
40 return $this->typeid;
41 }
42 public function get_typestring() {
43 if ($this->string===null) {
44 $this->string = get_string($this->typeidstring, 'lesson');
45 }
46 return $this->string;
47 }
48 public function get_idstring() {
49 return $this->typeidstring;
50 }
51 public function display($renderer, $attempt) {
52 global $USER, $CFG, $PAGE;
53 $mform = $this->make_answer_form($attempt);
54 $data = new stdClass;
55 $data->id = $PAGE->cm->id;
56 $data->pageid = $this->properties->id;
57 $mform->set_data($data);
58 return $mform->display();
59 }
60
61 protected function make_answer_form($attempt=null) {
62 global $USER, $CFG;
4fb3f253 63 // don't shuffle answers (could be an option??)
57a4bd03
RW
64 $getanswers = array_slice($this->get_answers(), 2);
65
57a4bd03
RW
66 $answers = array();
67 foreach ($getanswers as $getanswer) {
68 $answers[$getanswer->id] = $getanswer;
69 }
70
0a4abb73
SH
71 $responses = array();
72 foreach ($answers as $answer) {
73 // get all the response
74 if ($answer->response != NULL) {
75 $responses[] = trim($answer->response);
76 }
77 }
78
04932409 79 $responseoptions = array(''=>get_string('choosedots'));
0a4abb73
SH
80 if (!empty($responses)) {
81 shuffle($responses);
82 $responses = array_unique($responses);
83 foreach ($responses as $response) {
84 $responseoptions[htmlspecialchars(trim($response))] = $response;
85 }
86 }
87 if (isset($USER->modattempts[$this->lesson->id]) && !empty($attempt->useranswer)) {
88 $useranswers = explode(',', $attempt->useranswer);
89 $t = 0;
90 } else {
91 $useranswers = array();
92 }
93
94 $action = $CFG->wwwroot.'/mod/lesson/continue.php';
95 $params = array('answers'=>$answers, 'useranswers'=>$useranswers, 'responseoptions'=>$responseoptions, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents());
96 $mform = new lesson_display_answer_form_matching($action, $params);
97 return $mform;
98 }
99
100 public function create_answers($properties) {
101 global $DB;
102 // now add the answers
103 $newanswer = new stdClass;
104 $newanswer->lessonid = $this->lesson->id;
105 $newanswer->pageid = $this->properties->id;
106 $newanswer->timecreated = $this->properties->timecreated;
107
108 $answers = array();
109
110 // need to add two to offset correct response and wrong response
111 $this->lesson->maxanswers = $this->lesson->maxanswers + 2;
112 for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
113 $answer = clone($newanswer);
981debb7 114 if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
01c37ef1
PS
115 $answer->answer = $properties->answer_editor[$i]['text'];
116 $answer->answerformat = $properties->answer_editor[$i]['format'];
981debb7
SH
117 }
118 if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
119 $answer->response = $properties->response_editor[$i]['text'];
120 $answer->responseformat = $properties->response_editor[$i]['format'];
121 }
122
6d84e82f 123 if (isset($answer->answer) && $answer->answer != '') {
0a4abb73
SH
124 if (isset($properties->jumpto[$i])) {
125 $answer->jumpto = $properties->jumpto[$i];
126 }
127 if ($this->lesson->custom && isset($properties->score[$i])) {
128 $answer->score = $properties->score[$i];
129 }
130 $answer->id = $DB->insert_record("lesson_answers", $answer);
131 $answers[$answer->id] = new lesson_page_answer($answer);
132 } else if ($i < 2) {
133 $answer->id = $DB->insert_record("lesson_answers", $answer);
134 $answers[$answer->id] = new lesson_page_answer($answer);
135 } else {
136 break;
137 }
138 }
139 $this->answers = $answers;
140 return $answers;
141 }
142
143 public function check_answer() {
df0442c6 144 global $CFG, $PAGE;
01c37ef1 145
39790bd8 146 $formattextdefoptions = new stdClass();
01c37ef1
PS
147 $formattextdefoptions->noclean = true;
148 $formattextdefoptions->para = false;
149
0a4abb73
SH
150 $result = parent::check_answer();
151
152 $mform = $this->make_answer_form();
153
154 $data = $mform->get_data();
155 require_sesskey();
156
157 if (!$data) {
a6855934 158 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id)));
0a4abb73 159 }
2f67a9b3 160
0a4abb73
SH
161 $response = $data->response;
162 if (!is_array($response)) {
163 $result->noanswer = true;
164 return $result;
165 }
a675ada5 166
0a4abb73 167 $answers = $this->get_answers();
a675ada5
PS
168
169 $correct = array_shift($answers);
170 $wrong = array_shift($answers);
171
172 foreach ($answers as $key=>$answer) {
173 if ($answer->answer === '' or $answer->response === '') {
174 // incomplete option!
175 unset($answers[$key]);
0a4abb73 176 }
0a4abb73
SH
177 }
178 // get he users exact responses for record keeping
a675ada5 179 $hits = 0;
0a4abb73
SH
180 $userresponse = array();
181 foreach ($response as $key => $value) {
182 foreach($answers as $answer) {
a675ada5 183 if ($value === $answer->response) {
0a4abb73
SH
184 $userresponse[] = $answer->id;
185 }
186 if ((int)$answer->id === (int)$key) {
01c37ef1 187 $result->studentanswer .= '<br />'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions).' = '.$value;
0a4abb73 188 }
a675ada5
PS
189 if ((int)$answer->id === (int)$key and $value === $answer->response) {
190 $hits++;
191 }
0a4abb73
SH
192 }
193 }
194 $result->userresponse = implode(",", $userresponse);
195
a675ada5 196 if ($hits == count($answers)) {
0a4abb73 197 $result->correctanswer = true;
a675ada5
PS
198 $result->response = format_text($correct->answer, $correct->answerformat, $formattextdefoptions);
199 $result->answerid = $correct->id;
200 $result->newpageid = $correct->jumpto;
0a4abb73 201 } else {
a675ada5
PS
202 $result->correctanswer = false;
203 $result->response = format_text($wrong->answer, $wrong->answerformat, $formattextdefoptions);
204 $result->answerid = $wrong->id;
205 $result->newpageid = $wrong->jumpto;
0a4abb73 206 }
a675ada5 207
0a4abb73
SH
208 return $result;
209 }
210
211 public function option_description_string() {
212 return get_string("firstanswershould", "lesson");
213 }
214
215 public function display_answers(html_table $table) {
216 $answers = $this->get_answers();
217 $options = new stdClass;
218 $options->noclean = true;
219 $options->para = false;
220 $i = 1;
221 $n = 0;
2f67a9b3 222
0a4abb73
SH
223 foreach ($answers as $answer) {
224 if ($n < 2) {
225 if ($answer->answer != NULL) {
226 $cells = array();
227 if ($n == 0) {
228 $cells[] = "<span class=\"label\">".get_string("correctresponse", "lesson").'</span>';
229 } else {
230 $cells[] = "<span class=\"label\">".get_string("wrongresponse", "lesson").'</span>';
231 }
01c37ef1 232 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
8cea545e 233 $table->data[] = new html_table_row($cells);
0a4abb73
SH
234 }
235 $n++;
236 $i--;
237 } else {
238 $cells = array();
239 if ($this->lesson->custom && $answer->score > 0) {
240 // if the score is > 0, then it is correct
241 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
242 } else if ($this->lesson->custom) {
243 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
244 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
245 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
246 } else {
247 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
248 }
01c37ef1 249 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
8cea545e 250 $table->data[] = new html_table_row($cells);
0a4abb73
SH
251
252 $cells = array();
253 $cells[] = '<span class="label">'.get_string("matchesanswer", "lesson")." $i</span>: ";
01c37ef1 254 $cells[] = format_text($answer->response, $answer->responseformat, $options);
8cea545e 255 $table->data[] = new html_table_row($cells);
0a4abb73
SH
256 }
257
258 if ($i == 1) {
259 $cells = array();
260 $cells[] = '<span class="label">'.get_string("correctanswerscore", "lesson")." $i</span>: ";
261 $cells[] = $answer->score;
8cea545e 262 $table->data[] = new html_table_row($cells);
0a4abb73
SH
263
264 $cells = array();
265 $cells[] = '<span class="label">'.get_string("correctanswerjump", "lesson")." $i</span>: ";
266 $cells[] = $this->get_jump_name($answer->jumpto);
8cea545e 267 $table->data[] = new html_table_row($cells);
0a4abb73
SH
268 } elseif ($i == 2) {
269 $cells = array();
270 $cells[] = '<span class="label">'.get_string("wronganswerscore", "lesson")." $i</span>: ";
271 $cells[] = $answer->score;
8cea545e 272 $table->data[] = new html_table_row($cells);
0a4abb73
SH
273
274 $cells = array();
275 $cells[] = '<span class="label">'.get_string("wronganswerjump", "lesson")." $i</span>: ";
276 $cells[] = $this->get_jump_name($answer->jumpto);
8cea545e 277 $table->data[] = new html_table_row($cells);
0a4abb73
SH
278 }
279
280 if ($i === 1){
281 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
282 }
283
284 $i++;
285 }
286 return $table;
287 }
981debb7
SH
288 /**
289 * Updates the page and its answers
290 *
291 * @global moodle_database $DB
292 * @global moodle_page $PAGE
293 * @param stdClass $properties
294 * @return bool
295 */
0a4abb73
SH
296 public function update($properties) {
297 global $DB, $PAGE;
298 $answers = $this->get_answers();
299 $properties->id = $this->properties->id;
300 $properties->lessonid = $this->lesson->id;
64f93798 301 $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
0a4abb73
SH
302 $DB->update_record("lesson_pages", $properties);
303
304 // need to add two to offset correct response and wrong response
305 $this->lesson->maxanswers += 2;
306 for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
307 if (!array_key_exists($i, $this->answers)) {
308 $this->answers[$i] = new stdClass;
309 $this->answers[$i]->lessonid = $this->lesson->id;
310 $this->answers[$i]->pageid = $this->id;
311 $this->answers[$i]->timecreated = $this->timecreated;
312 }
981debb7
SH
313
314 if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
01c37ef1
PS
315 $this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
316 $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
981debb7
SH
317 }
318 if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
319 $this->answers[$i]->response = $properties->response_editor[$i]['text'];
320 $this->answers[$i]->responseformat = $properties->response_editor[$i]['format'];
321 }
322
6d84e82f
RW
323 // we don't need to check for isset here because properties called it's own isset method.
324 if ($this->answers[$i]->answer != '') {
0a4abb73
SH
325 if (isset($properties->jumpto[$i])) {
326 $this->answers[$i]->jumpto = $properties->jumpto[$i];
327 }
328 if ($this->lesson->custom && isset($properties->score[$i])) {
329 $this->answers[$i]->score = $properties->score[$i];
330 }
331 if (!isset($this->answers[$i]->id)) {
332 $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
333 } else {
334 $DB->update_record("lesson_answers", $this->answers[$i]->properties());
335 }
336
337 } else if ($i < 2) {
338 if (!isset($this->answers[$i]->id)) {
339 $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
340 } else {
341 $DB->update_record("lesson_answers", $this->answers[$i]->properties());
342 }
343
981debb7
SH
344 } else if (isset($this->answers[$i]->id)) {
345 $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
346 unset($this->answers[$i]);
0a4abb73
SH
347 }
348 }
349 return true;
350 }
351 public function stats(array &$pagestats, $tries) {
352 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
353 $temp = $tries[$this->lesson->maxattempts - 1];
354 } else {
355 // else, user attempted the question less than the max, so grab the last one
356 $temp = end($tries);
357 }
358 if ($temp->correct) {
359 if (isset($pagestats[$temp->pageid]["correct"])) {
360 $pagestats[$temp->pageid]["correct"]++;
361 } else {
362 $pagestats[$temp->pageid]["correct"] = 1;
363 }
364 }
365 if (isset($pagestats[$temp->pageid]["total"])) {
366 $pagestats[$temp->pageid]["total"]++;
367 } else {
368 $pagestats[$temp->pageid]["total"] = 1;
369 }
370 return true;
371 }
372 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
373 $answers = array();
374 foreach ($this->get_answers() as $answer) {
375 $answers[$answer->id] = $answer;
376 }
377 $formattextdefoptions = new stdClass;
378 $formattextdefoptions->para = false; //I'll use it widely in this page
379 foreach ($answers as $answer) {
380 if ($n == 0 && $useranswer != NULL && $useranswer->correct) {
381 if ($answer->response == NULL && $useranswer != NULL) {
382 $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
383 } else {
384 $answerdata->response = $answer->response;
385 }
c90ba16f 386 if ($this->lesson->custom) {
6d3ae910
AB
387 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
388 } else {
389 $answerdata->score = get_string("receivedcredit", "lesson");
390 }
0a4abb73
SH
391 } elseif ($n == 1 && $useranswer != NULL && !$useranswer->correct) {
392 if ($answer->response == NULL && $useranswer != NULL) {
393 $answerdata->response = get_string("thatsthewronganswer", "lesson");
394 } else {
395 $answerdata->response = $answer->response;
396 }
c90ba16f
RW
397 if ($this->lesson->custom) {
398 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
399 } else {
400 $answerdata->score = get_string("didnotreceivecredit", "lesson");
0a4abb73 401 }
c90ba16f 402 } elseif ($n > 1) {
0a4abb73
SH
403 $data = "<select disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->answer))."</option></select>";
404 if ($useranswer != NULL) {
405 $userresponse = explode(",", $useranswer->useranswer);
981debb7
SH
406 $data .= "<select disabled=\"disabled\"><option selected=\"selected\">";
407 if (array_key_exists($i, $userresponse)) {
408 $data .= strip_tags(format_string($answers[$userresponse[$i]]->response));
409 }
410 $data .= "</option></select>";
0a4abb73
SH
411 } else {
412 $data .= "<select disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->response))."</option></select>";
413 }
414
415 if ($n == 2) {
416 if (isset($pagestats[$this->properties->id])) {
417 if (!array_key_exists('correct', $pagestats[$this->properties->id])) {
418 $pagestats[$this->properties->id]["correct"] = 0;
419 }
420 $percent = $pagestats[$this->properties->id]["correct"] / $pagestats[$this->properties->id]["total"] * 100;
421 $percent = round($percent, 2);
422 $percent .= "% ".get_string("answeredcorrectly", "lesson");
423 } else {
424 $percent = get_string("nooneansweredthisquestion", "lesson");
425 }
426 } else {
c90ba16f 427 $percent = '';
0a4abb73
SH
428 }
429
430 $answerdata->answers[] = array($data, $percent);
431 $i++;
432 }
433 $n++;
434 $answerpage->answerdata = $answerdata;
435 }
436 return $answerpage;
437 }
438 public function get_jumps() {
439 global $DB;
86d99db3 440 // The jumps for matching question type are stored in the 1st and 2nd answer record.
0a4abb73 441 $jumps = array();
04932409 442 if ($answers = $DB->get_records("lesson_answers", array("lessonid" => $this->lesson->id, "pageid" => $this->properties->id), 'id', '*', 0, 2)) {
0a4abb73
SH
443 foreach ($answers as $answer) {
444 $jumps[] = $this->get_jump_name($answer->jumpto);
445 }
9170dea7
SH
446 } else {
447 $jumps[] = $this->get_jump_name($this->properties->nextpageid);
0a4abb73
SH
448 }
449 return $jumps;
450 }
451}
452
453class lesson_add_page_form_matching extends lesson_add_page_form_base {
454
455 public $qtype = 'matching';
456 public $qtypestring = 'matching';
457
458 public function custom_definition() {
459
460 $this->_form->addElement('header', 'correctresponse', get_string('correctresponse', 'lesson'));
cb2c1963 461 $this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'), array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
76f5f66f 462 $this->add_jumpto(0, get_string('correctanswerjump','lesson'), LESSON_NEXTPAGE);
a675ada5 463 $this->add_score(0, get_string("correctanswerscore", "lesson"));
0a4abb73
SH
464
465 $this->_form->addElement('header', 'wrongresponse', get_string('wrongresponse', 'lesson'));
cb2c1963 466 $this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'), array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
76f5f66f 467 $this->add_jumpto(1, get_string('wronganswerjump','lesson'), LESSON_THISPAGE);
a675ada5 468 $this->add_score(1, get_string("wronganswerscore", "lesson"));
0a4abb73
SH
469
470 for ($i = 2; $i < $this->_customdata['lesson']->maxanswers+2; $i++) {
471 $this->_form->addElement('header', 'matchingpair'.($i-1), get_string('matchingpair', 'lesson', $i-1));
a675ada5
PS
472 $this->add_answer($i, NULL, ($i < 4));
473 $this->add_response($i, get_string('matchesanswer','lesson'), ($i < 4));
0a4abb73
SH
474 }
475 }
476}
477
478
479class lesson_display_answer_form_matching extends moodleform {
480
481 public function definition() {
482 global $USER, $OUTPUT;
483 $mform = $this->_form;
484 $answers = $this->_customdata['answers'];
485 $useranswers = $this->_customdata['useranswers'];
486 $responseoptions = $this->_customdata['responseoptions'];
487 $lessonid = $this->_customdata['lessonid'];
488 $contents = $this->_customdata['contents'];
489
ffdf7f8a
DM
490 $mform->addElement('header', 'pageheader');
491
492 $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
0a4abb73 493
abd5c24e
RW
494 $hasattempt = false;
495 $disabled = '';
496 if (isset($useranswers) && !empty($useranswers)) {
497 $hasattempt = true;
498 $disabled = array('disabled' => 'disabled');
499 }
500
0a4abb73
SH
501 $options = new stdClass;
502 $options->para = false;
503 $options->noclean = true;
504
505 $mform->addElement('hidden', 'id');
506 $mform->setType('id', PARAM_INT);
507
508 $mform->addElement('hidden', 'pageid');
509 $mform->setType('pageid', PARAM_INT);
510
511 $i = 0;
512 foreach ($answers as $answer) {
513 $mform->addElement('html', '<div class="answeroption">');
514 if ($answer->response != NULL) {
abd5c24e
RW
515 $responseid = 'response['.$answer->id.']';
516 if ($hasattempt) {
517 $responseid = 'response_'.$answer->id;
518 $mform->addElement('hidden', 'response['.$answer->id.']', htmlspecialchars(trim($answers[$useranswers[$i]]->response)));
519 $mform->setType('response['.$answer->id.']', PARAM_TEXT);
520 }
521 $mform->addElement('select', $responseid, format_text($answer->answer,$answer->answerformat,$options), $responseoptions, $disabled);
522 $mform->setType($responseid, PARAM_TEXT);
523 if ($hasattempt) {
524 $mform->setDefault($responseid, htmlspecialchars(trim($answers[$useranswers[$i]]->response))); //TODO: this is suspicious
0a4abb73 525 } else {
abd5c24e 526 $mform->setDefault($responseid, 'answeroption');
0a4abb73
SH
527 }
528 }
529 $mform->addElement('html', '</div>');
530 $i++;
531 }
abd5c24e
RW
532 if ($hasattempt) {
533 $this->add_action_buttons(null, get_string("nextpage", "lesson"));
534 } else {
535 $this->add_action_buttons(null, get_string("submit", "lesson"));
536 }
0a4abb73
SH
537 }
538
ffdf7f8a 539}