MDL-55327 mod_lesson: Fix for copying essay and match.
[moodle.git] / mod / lesson / pagetypes / matching.php
bloba4260f2c814c66ec0214bfcd9640c4191153671a
1 <?php
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.
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/>.
18 /**
19 * Matching
21 * @package mod_lesson
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
26 defined('MOODLE_INTERNAL') || die();
28 /** Matching question type */
29 define("LESSON_PAGE_MATCHING", "5");
31 class lesson_page_type_matching extends lesson_page {
33 protected $type = lesson_page::TYPE_QUESTION;
34 protected $typeid = LESSON_PAGE_MATCHING;
35 protected $typeidstring = 'matching';
36 protected $string = null;
38 public function get_typeid() {
39 return $this->typeid;
41 public function get_typestring() {
42 if ($this->string===null) {
43 $this->string = get_string($this->typeidstring, 'lesson');
45 return $this->string;
47 public function get_idstring() {
48 return $this->typeidstring;
50 public function display($renderer, $attempt) {
51 global $USER, $CFG, $PAGE;
52 $mform = $this->make_answer_form($attempt);
53 $data = new stdClass;
54 $data->id = $PAGE->cm->id;
55 $data->pageid = $this->properties->id;
56 $mform->set_data($data);
58 // Trigger an event question viewed.
59 $eventparams = array(
60 'context' => context_module::instance($PAGE->cm->id),
61 'objectid' => $this->properties->id,
62 'other' => array(
63 'pagetype' => $this->get_typestring()
67 $event = \mod_lesson\event\question_viewed::create($eventparams);
68 $event->trigger();
69 return $mform->display();
72 protected function make_answer_form($attempt=null) {
73 global $USER, $CFG;
74 // don't shuffle answers (could be an option??)
75 $getanswers = array_slice($this->get_answers(), 2);
77 $answers = array();
78 foreach ($getanswers as $getanswer) {
79 $answers[$getanswer->id] = $getanswer;
82 $responses = array();
83 foreach ($answers as $answer) {
84 // get all the response
85 if ($answer->response != null) {
86 $responses[] = trim($answer->response);
90 $responseoptions = array(''=>get_string('choosedots'));
91 if (!empty($responses)) {
92 shuffle($responses);
93 foreach ($responses as $response) {
94 $responseoptions[htmlspecialchars($response)] = $response;
97 if (isset($USER->modattempts[$this->lesson->id]) && !empty($attempt->useranswer)) {
98 $useranswers = explode(',', $attempt->useranswer);
99 $t = 0;
100 } else {
101 $useranswers = array();
104 $action = $CFG->wwwroot.'/mod/lesson/continue.php';
105 $params = array('answers'=>$answers, 'useranswers'=>$useranswers, 'responseoptions'=>$responseoptions, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents());
106 $mform = new lesson_display_answer_form_matching($action, $params);
107 return $mform;
110 public function create_answers($properties) {
111 global $DB, $PAGE;
112 // now add the answers
113 $newanswer = new stdClass;
114 $newanswer->lessonid = $this->lesson->id;
115 $newanswer->pageid = $this->properties->id;
116 $newanswer->timecreated = $this->properties->timecreated;
118 $cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course);
119 $context = context_module::instance($cm->id);
121 // Check for duplicate response format.
122 $duplicateresponse = array();
123 if (is_array($properties->response_editor[0])) {
124 foreach ($properties->response_editor as $response) {
125 $duplicateresponse[] = $response['text'];
127 $properties->response_editor = $duplicateresponse;
130 $answers = array();
132 // need to add two to offset correct response and wrong response
133 $this->lesson->maxanswers = $this->lesson->maxanswers + 2;
134 for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
135 $answer = clone($newanswer);
136 if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
137 $answer->answer = $properties->answer_editor[$i]['text'];
138 $answer->answerformat = $properties->answer_editor[$i]['format'];
140 if (!empty($properties->response_editor[$i])) {
141 $answer->response = $properties->response_editor[$i];
142 $answer->responseformat = 0;
145 if (isset($properties->jumpto[$i])) {
146 $answer->jumpto = $properties->jumpto[$i];
148 if ($this->lesson->custom && isset($properties->score[$i])) {
149 $answer->score = $properties->score[$i];
152 if (isset($answer->answer) && $answer->answer != '') {
153 $answer->id = $DB->insert_record("lesson_answers", $answer);
154 $this->save_answers_files($context, $PAGE->course->maxbytes,
155 $answer, $properties->answer_editor[$i]);
156 $answers[$answer->id] = new lesson_page_answer($answer);
157 } else if ($i < 2) {
158 $answer->id = $DB->insert_record("lesson_answers", $answer);
159 $answers[$answer->id] = new lesson_page_answer($answer);
160 } else {
161 break;
164 $this->answers = $answers;
165 return $answers;
168 public function check_answer() {
169 global $CFG, $PAGE;
171 $formattextdefoptions = new stdClass();
172 $formattextdefoptions->noclean = true;
173 $formattextdefoptions->para = false;
175 $result = parent::check_answer();
177 $mform = $this->make_answer_form();
179 $data = $mform->get_data();
180 require_sesskey();
182 if (!$data) {
183 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id)));
186 $response = $data->response;
187 $getanswers = $this->get_answers();
188 foreach ($getanswers as $key => $answer) {
189 $getanswers[$key] = parent::rewrite_answers_urls($answer);
192 $correct = array_shift($getanswers);
193 $wrong = array_shift($getanswers);
195 $answers = array();
196 foreach ($getanswers as $key => $answer) {
197 if ($answer->answer !== '' or $answer->response !== '') {
198 $answers[$answer->id] = $answer;
202 // get the user's exact responses for record keeping
203 $hits = 0;
204 $userresponse = array();
205 $result->studentanswerformat = FORMAT_HTML;
206 foreach ($response as $id => $value) {
207 if ($value == '') {
208 $result->noanswer = true;
209 return $result;
211 $value = htmlspecialchars_decode($value);
212 $userresponse[] = $value;
213 // Make sure the user's answer exists in question's answer
214 if (array_key_exists($id, $answers)) {
215 $answer = $answers[$id];
216 $result->studentanswer .= '<br />'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions).' = '.$value;
217 if (trim($answer->response) == trim($value)) {
218 $hits++;
223 $result->userresponse = implode(",", $userresponse);
225 if ($hits == count($answers)) {
226 $result->correctanswer = true;
227 $result->response = format_text($correct->answer, $correct->answerformat, $formattextdefoptions);
228 $result->answerid = $correct->id;
229 $result->newpageid = $correct->jumpto;
230 } else {
231 $result->correctanswer = false;
232 $result->response = format_text($wrong->answer, $wrong->answerformat, $formattextdefoptions);
233 $result->answerid = $wrong->id;
234 $result->newpageid = $wrong->jumpto;
237 return $result;
240 public function option_description_string() {
241 return get_string("firstanswershould", "lesson");
244 public function display_answers(html_table $table) {
245 $answers = $this->get_answers();
246 $options = new stdClass;
247 $options->noclean = true;
248 $options->para = false;
249 $i = 1;
250 $n = 0;
252 foreach ($answers as $answer) {
253 $answer = parent::rewrite_answers_urls($answer);
254 if ($n < 2) {
255 if ($answer->answer != null) {
256 $cells = array();
257 if ($n == 0) {
258 $cells[] = "<span class=\"label\">".get_string("correctresponse", "lesson").'</span>';
259 } else {
260 $cells[] = "<span class=\"label\">".get_string("wrongresponse", "lesson").'</span>';
262 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
263 $table->data[] = new html_table_row($cells);
266 if ($n == 0) {
267 $cells = array();
268 $cells[] = '<span class="label">'.get_string("correctanswerscore", "lesson")."</span>: ";
269 $cells[] = $answer->score;
270 $table->data[] = new html_table_row($cells);
272 $cells = array();
273 $cells[] = '<span class="label">'.get_string("correctanswerjump", "lesson")."</span>: ";
274 $cells[] = $this->get_jump_name($answer->jumpto);
275 $table->data[] = new html_table_row($cells);
276 } elseif ($n == 1) {
277 $cells = array();
278 $cells[] = '<span class="label">'.get_string("wronganswerscore", "lesson")."</span>: ";
279 $cells[] = $answer->score;
280 $table->data[] = new html_table_row($cells);
282 $cells = array();
283 $cells[] = '<span class="label">'.get_string("wronganswerjump", "lesson")."</span>: ";
284 $cells[] = $this->get_jump_name($answer->jumpto);
285 $table->data[] = new html_table_row($cells);
288 if ($n === 0){
289 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
291 $n++;
292 $i--;
293 } else {
294 $cells = array();
295 if ($this->lesson->custom && $answer->score > 0) {
296 // if the score is > 0, then it is correct
297 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
298 } else if ($this->lesson->custom) {
299 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
300 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
301 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
302 } else {
303 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
305 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
306 $table->data[] = new html_table_row($cells);
308 $cells = array();
309 $cells[] = '<span class="label">'.get_string("matchesanswer", "lesson")." $i</span>: ";
310 $cells[] = format_text($answer->response, $answer->responseformat, $options);
311 $table->data[] = new html_table_row($cells);
313 $i++;
315 return $table;
318 * Updates the page and its answers
320 * @global moodle_database $DB
321 * @global moodle_page $PAGE
322 * @param stdClass $properties
323 * @return bool
325 public function update($properties, $context = null, $maxbytes = null) {
326 global $DB, $PAGE;
327 $answers = $this->get_answers();
328 $properties->id = $this->properties->id;
329 $properties->lessonid = $this->lesson->id;
330 $properties->timemodified = time();
331 $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
332 $DB->update_record("lesson_pages", $properties);
334 // Trigger an event: page updated.
335 \mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger();
337 // need to add two to offset correct response and wrong response
338 $this->lesson->maxanswers += 2;
339 for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
340 if (!array_key_exists($i, $this->answers)) {
341 $this->answers[$i] = new stdClass;
342 $this->answers[$i]->lessonid = $this->lesson->id;
343 $this->answers[$i]->pageid = $this->id;
344 $this->answers[$i]->timecreated = $this->timecreated;
347 if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
348 $this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
349 $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
351 if (!empty($properties->response_editor[$i])) {
352 $this->answers[$i]->response = $properties->response_editor[$i];
353 $this->answers[$i]->responseformat = 0;
356 if (isset($properties->jumpto[$i])) {
357 $this->answers[$i]->jumpto = $properties->jumpto[$i];
359 if ($this->lesson->custom && isset($properties->score[$i])) {
360 $this->answers[$i]->score = $properties->score[$i];
363 // we don't need to check for isset here because properties called it's own isset method.
364 if ($this->answers[$i]->answer != '') {
365 if (!isset($this->answers[$i]->id)) {
366 $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
367 } else {
368 $DB->update_record("lesson_answers", $this->answers[$i]->properties());
370 // Save files in answers (no response_editor for matching questions).
371 $this->save_answers_files($context, $maxbytes, $this->answers[$i], $properties->answer_editor[$i]);
372 } else if ($i < 2) {
373 if (!isset($this->answers[$i]->id)) {
374 $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
375 } else {
376 $DB->update_record("lesson_answers", $this->answers[$i]->properties());
379 // Save files in answers (no response_editor for matching questions).
380 $this->save_answers_files($context, $maxbytes, $this->answers[$i], $properties->answer_editor[$i]);
381 } else if (isset($this->answers[$i]->id)) {
382 $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
383 unset($this->answers[$i]);
386 return true;
388 public function stats(array &$pagestats, $tries) {
389 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
390 $temp = $tries[$this->lesson->maxattempts - 1];
391 } else {
392 // else, user attempted the question less than the max, so grab the last one
393 $temp = end($tries);
395 if ($temp->correct) {
396 if (isset($pagestats[$temp->pageid]["correct"])) {
397 $pagestats[$temp->pageid]["correct"]++;
398 } else {
399 $pagestats[$temp->pageid]["correct"] = 1;
402 if (isset($pagestats[$temp->pageid]["total"])) {
403 $pagestats[$temp->pageid]["total"]++;
404 } else {
405 $pagestats[$temp->pageid]["total"] = 1;
407 return true;
409 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
410 $answers = array();
411 foreach ($this->get_answers() as $answer) {
412 $answers[$answer->id] = $answer;
414 $formattextdefoptions = new stdClass;
415 $formattextdefoptions->para = false; //I'll use it widely in this page
416 foreach ($answers as $answer) {
417 if ($n == 0 && $useranswer != null && $useranswer->correct) {
418 if ($answer->response == null && $useranswer != null) {
419 $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
420 } else {
421 $answerdata->response = $answer->response;
423 if ($this->lesson->custom) {
424 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
425 } else {
426 $answerdata->score = get_string("receivedcredit", "lesson");
428 } elseif ($n == 1 && $useranswer != null && !$useranswer->correct) {
429 if ($answer->response == null && $useranswer != null) {
430 $answerdata->response = get_string("thatsthewronganswer", "lesson");
431 } else {
432 $answerdata->response = $answer->response;
434 if ($this->lesson->custom) {
435 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
436 } else {
437 $answerdata->score = get_string("didnotreceivecredit", "lesson");
439 } elseif ($n > 1) {
440 $data = '<label class="accesshide" for="answer_' . $n . '">' . get_string('answer', 'lesson') . '</label>';
441 $data .= strip_tags(format_string($answer->answer)) . ' ';
442 if ($useranswer != null) {
443 $userresponse = explode(",", $useranswer->useranswer);
444 $data .= '<label class="accesshide" for="stu_answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>';
445 $data .= "<select id=\"stu_answer_response_" . $n . "\" disabled=\"disabled\"><option selected=\"selected\">";
446 if (array_key_exists($i, $userresponse)) {
447 $data .= $userresponse[$i];
449 $data .= "</option></select>";
450 } else {
451 $data .= '<label class="accesshide" for="answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>';
452 $data .= "<select id=\"answer_response_" . $n . "\" disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->response))."</option></select>";
455 if ($n == 2) {
456 if (isset($pagestats[$this->properties->id])) {
457 if (!array_key_exists('correct', $pagestats[$this->properties->id])) {
458 $pagestats[$this->properties->id]["correct"] = 0;
460 $percent = $pagestats[$this->properties->id]["correct"] / $pagestats[$this->properties->id]["total"] * 100;
461 $percent = round($percent, 2);
462 $percent .= "% ".get_string("answeredcorrectly", "lesson");
463 } else {
464 $percent = get_string("nooneansweredthisquestion", "lesson");
466 } else {
467 $percent = '';
470 $answerdata->answers[] = array($data, $percent);
471 $i++;
473 $n++;
474 $answerpage->answerdata = $answerdata;
476 return $answerpage;
478 public function get_jumps() {
479 global $DB;
480 // The jumps for matching question type are stored in the 1st and 2nd answer record.
481 $jumps = array();
482 if ($answers = $DB->get_records("lesson_answers", array("lessonid" => $this->lesson->id, "pageid" => $this->properties->id), 'id', '*', 0, 2)) {
483 foreach ($answers as $answer) {
484 $jumps[] = $this->get_jump_name($answer->jumpto);
486 } else {
487 $jumps[] = $this->get_jump_name($this->properties->nextpageid);
489 return $jumps;
493 class lesson_add_page_form_matching extends lesson_add_page_form_base {
495 public $qtype = 'matching';
496 public $qtypestring = 'matching';
497 protected $answerformat = LESSON_ANSWER_HTML;
498 protected $responseformat = '';
500 public function custom_definition() {
502 $this->_form->addElement('header', 'correctresponse', get_string('correctresponse', 'lesson'));
503 $this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'),
504 array('rows' => '4', 'columns' => '80'),
505 array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
506 $this->_form->setType('answer_editor[0]', PARAM_RAW);
507 $this->_form->setDefault('answer_editor[0]', array('text' => '', 'format' => FORMAT_HTML));
508 $this->add_jumpto(0, get_string('correctanswerjump','lesson'), LESSON_NEXTPAGE);
509 $this->add_score(0, get_string("correctanswerscore", "lesson"), 1);
511 $this->_form->addElement('header', 'wrongresponse', get_string('wrongresponse', 'lesson'));
512 $this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'),
513 array('rows' => '4', 'columns' => '80'),
514 array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
515 $this->_form->setType('answer_editor[1]', PARAM_RAW);
516 $this->_form->setDefault('answer_editor[1]', array('text' => '', 'format' => FORMAT_HTML));
518 $this->add_jumpto(1, get_string('wronganswerjump','lesson'), LESSON_THISPAGE);
519 $this->add_score(1, get_string("wronganswerscore", "lesson"), 0);
521 for ($i = 2; $i < $this->_customdata['lesson']->maxanswers+2; $i++) {
522 $this->_form->addElement('header', 'matchingpair'.($i-1), get_string('matchingpair', 'lesson', $i-1));
523 $this->add_answer($i, null, ($i < 4), LESSON_ANSWER_HTML);
524 $required = ($i < 4);
525 $label = get_string('matchesanswer','lesson');
526 $count = $i;
527 $this->_form->addElement('text', 'response_editor['.$count.']', $label, array('size'=>'50'));
528 $this->_form->setType('response_editor['.$count.']', PARAM_NOTAGS);
529 $this->_form->setDefault('response_editor['.$count.']', '');
530 if ($required) {
531 $this->_form->addRule('response_editor['.$count.']', get_string('required'), 'required', null, 'client');
537 class lesson_display_answer_form_matching extends moodleform {
539 public function definition() {
540 global $USER, $OUTPUT, $PAGE;
541 $mform = $this->_form;
542 $answers = $this->_customdata['answers'];
543 $useranswers = $this->_customdata['useranswers'];
544 $responseoptions = $this->_customdata['responseoptions'];
545 $lessonid = $this->_customdata['lessonid'];
546 $contents = $this->_customdata['contents'];
548 // Disable shortforms.
549 $mform->setDisableShortforms();
551 $mform->addElement('header', 'pageheader');
553 $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
555 $hasattempt = false;
556 $disabled = '';
557 if (isset($useranswers) && !empty($useranswers)) {
558 $hasattempt = true;
559 $disabled = array('disabled' => 'disabled');
562 $options = new stdClass;
563 $options->para = false;
564 $options->noclean = true;
566 $mform->addElement('hidden', 'id');
567 $mform->setType('id', PARAM_INT);
569 $mform->addElement('hidden', 'pageid');
570 $mform->setType('pageid', PARAM_INT);
572 $i = 0;
574 foreach ($answers as $answer) {
575 $mform->addElement('html', '<div class="answeroption">');
576 if ($answer->response != null) {
577 $responseid = 'response['.$answer->id.']';
578 if ($hasattempt) {
579 $responseid = 'response_'.$answer->id;
580 $mform->addElement('hidden', 'response['.$answer->id.']', htmlspecialchars($useranswers[$i]));
581 // Temporary fixed until MDL-38885 gets integrated
582 $mform->setType('response', PARAM_TEXT);
584 $answer = lesson_page_type_matching::rewrite_answers_urls($answer);
585 $mform->addElement('select', $responseid, format_text($answer->answer,$answer->answerformat,$options), $responseoptions, $disabled);
586 $mform->setType($responseid, PARAM_TEXT);
587 if ($hasattempt) {
588 $mform->setDefault($responseid, htmlspecialchars(trim($useranswers[$i])));
589 } else {
590 $mform->setDefault($responseid, 'answeroption');
593 $mform->addElement('html', '</div>');
594 $i++;
596 if ($hasattempt) {
597 $this->add_action_buttons(null, get_string("nextpage", "lesson"));
598 } else {
599 $this->add_action_buttons(null, get_string("submit", "lesson"));