MDL-43987 core: Remove port numbers in cleanremoteaddr
[moodle.git] / mod / lesson / pagetypes / matching.php
blob7c28403be3fc482add50a73d566d77f0a34a603c
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);
57 return $mform->display();
60 protected function make_answer_form($attempt=null) {
61 global $USER, $CFG;
62 // don't shuffle answers (could be an option??)
63 $getanswers = array_slice($this->get_answers(), 2);
65 $answers = array();
66 foreach ($getanswers as $getanswer) {
67 $answers[$getanswer->id] = $getanswer;
70 $responses = array();
71 foreach ($answers as $answer) {
72 // get all the response
73 if ($answer->response != null) {
74 $responses[] = trim($answer->response);
78 $responseoptions = array(''=>get_string('choosedots'));
79 if (!empty($responses)) {
80 shuffle($responses);
81 foreach ($responses as $response) {
82 $responseoptions[htmlspecialchars($response)] = $response;
85 if (isset($USER->modattempts[$this->lesson->id]) && !empty($attempt->useranswer)) {
86 $useranswers = explode(',', $attempt->useranswer);
87 $t = 0;
88 } else {
89 $useranswers = array();
92 $action = $CFG->wwwroot.'/mod/lesson/continue.php';
93 $params = array('answers'=>$answers, 'useranswers'=>$useranswers, 'responseoptions'=>$responseoptions, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents());
94 $mform = new lesson_display_answer_form_matching($action, $params);
95 return $mform;
98 public function create_answers($properties) {
99 global $DB, $PAGE;
100 // now add the answers
101 $newanswer = new stdClass;
102 $newanswer->lessonid = $this->lesson->id;
103 $newanswer->pageid = $this->properties->id;
104 $newanswer->timecreated = $this->properties->timecreated;
106 $cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course);
107 $context = context_module::instance($cm->id);
109 $answers = array();
111 // need to add two to offset correct response and wrong response
112 $this->lesson->maxanswers = $this->lesson->maxanswers + 2;
113 for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
114 $answer = clone($newanswer);
115 if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
116 $answer->answer = $properties->answer_editor[$i]['text'];
117 $answer->answerformat = $properties->answer_editor[$i]['format'];
119 if (!empty($properties->response_editor[$i])) {
120 $answer->response = $properties->response_editor[$i];
121 $answer->responseformat = 0;
124 if (isset($properties->jumpto[$i])) {
125 $answer->jumpto = $properties->jumpto[$i];
127 if ($this->lesson->custom && isset($properties->score[$i])) {
128 $answer->score = $properties->score[$i];
131 if (isset($answer->answer) && $answer->answer != '') {
132 $answer->id = $DB->insert_record("lesson_answers", $answer);
133 $this->save_answers_files($context, $PAGE->course->maxbytes,
134 $answer, $properties->answer_editor[$i]);
135 $answers[$answer->id] = new lesson_page_answer($answer);
136 } else if ($i < 2) {
137 $answer->id = $DB->insert_record("lesson_answers", $answer);
138 $answers[$answer->id] = new lesson_page_answer($answer);
139 } else {
140 break;
143 $this->answers = $answers;
144 return $answers;
147 public function check_answer() {
148 global $CFG, $PAGE;
150 $formattextdefoptions = new stdClass();
151 $formattextdefoptions->noclean = true;
152 $formattextdefoptions->para = false;
154 $result = parent::check_answer();
156 $mform = $this->make_answer_form();
158 $data = $mform->get_data();
159 require_sesskey();
161 if (!$data) {
162 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id)));
165 $response = $data->response;
166 $getanswers = $this->get_answers();
167 foreach ($getanswers as $key => $answer) {
168 $getanswers[$key] = parent::rewrite_answers_urls($answer);
171 $correct = array_shift($getanswers);
172 $wrong = array_shift($getanswers);
174 $answers = array();
175 foreach ($getanswers as $key => $answer) {
176 if ($answer->answer !== '' or $answer->response !== '') {
177 $answers[$answer->id] = $answer;
181 // get the user's exact responses for record keeping
182 $hits = 0;
183 $userresponse = array();
184 foreach ($response as $id => $value) {
185 if ($value == '') {
186 $result->noanswer = true;
187 return $result;
189 $value = htmlspecialchars_decode($value);
190 $userresponse[] = $value;
191 // Make sure the user's answer exists in question's answer
192 if (array_key_exists($id, $answers)) {
193 $answer = $answers[$id];
194 $result->studentanswer .= '<br />'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions).' = '.$value;
195 if (trim($answer->response) == trim($value)) {
196 $hits++;
201 $result->userresponse = implode(",", $userresponse);
203 if ($hits == count($answers)) {
204 $result->correctanswer = true;
205 $result->response = format_text($correct->answer, $correct->answerformat, $formattextdefoptions);
206 $result->answerid = $correct->id;
207 $result->newpageid = $correct->jumpto;
208 } else {
209 $result->correctanswer = false;
210 $result->response = format_text($wrong->answer, $wrong->answerformat, $formattextdefoptions);
211 $result->answerid = $wrong->id;
212 $result->newpageid = $wrong->jumpto;
215 return $result;
218 public function option_description_string() {
219 return get_string("firstanswershould", "lesson");
222 public function display_answers(html_table $table) {
223 $answers = $this->get_answers();
224 $options = new stdClass;
225 $options->noclean = true;
226 $options->para = false;
227 $i = 1;
228 $n = 0;
230 foreach ($answers as $answer) {
231 $answer = parent::rewrite_answers_urls($answer);
232 if ($n < 2) {
233 if ($answer->answer != null) {
234 $cells = array();
235 if ($n == 0) {
236 $cells[] = "<span class=\"label\">".get_string("correctresponse", "lesson").'</span>';
237 } else {
238 $cells[] = "<span class=\"label\">".get_string("wrongresponse", "lesson").'</span>';
240 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
241 $table->data[] = new html_table_row($cells);
244 if ($n == 0) {
245 $cells = array();
246 $cells[] = '<span class="label">'.get_string("correctanswerscore", "lesson")."</span>: ";
247 $cells[] = $answer->score;
248 $table->data[] = new html_table_row($cells);
250 $cells = array();
251 $cells[] = '<span class="label">'.get_string("correctanswerjump", "lesson")."</span>: ";
252 $cells[] = $this->get_jump_name($answer->jumpto);
253 $table->data[] = new html_table_row($cells);
254 } elseif ($n == 1) {
255 $cells = array();
256 $cells[] = '<span class="label">'.get_string("wronganswerscore", "lesson")."</span>: ";
257 $cells[] = $answer->score;
258 $table->data[] = new html_table_row($cells);
260 $cells = array();
261 $cells[] = '<span class="label">'.get_string("wronganswerjump", "lesson")."</span>: ";
262 $cells[] = $this->get_jump_name($answer->jumpto);
263 $table->data[] = new html_table_row($cells);
266 if ($n === 0){
267 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
269 $n++;
270 $i--;
271 } else {
272 $cells = array();
273 if ($this->lesson->custom && $answer->score > 0) {
274 // if the score is > 0, then it is correct
275 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
276 } else if ($this->lesson->custom) {
277 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
278 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
279 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
280 } else {
281 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
283 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
284 $table->data[] = new html_table_row($cells);
286 $cells = array();
287 $cells[] = '<span class="label">'.get_string("matchesanswer", "lesson")." $i</span>: ";
288 $cells[] = format_text($answer->response, $answer->responseformat, $options);
289 $table->data[] = new html_table_row($cells);
291 $i++;
293 return $table;
296 * Updates the page and its answers
298 * @global moodle_database $DB
299 * @global moodle_page $PAGE
300 * @param stdClass $properties
301 * @return bool
303 public function update($properties, $context = null, $maxbytes = null) {
304 global $DB, $PAGE;
305 $answers = $this->get_answers();
306 $properties->id = $this->properties->id;
307 $properties->lessonid = $this->lesson->id;
308 $properties->timemodified = time();
309 $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);
310 $DB->update_record("lesson_pages", $properties);
312 // need to add two to offset correct response and wrong response
313 $this->lesson->maxanswers += 2;
314 for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
315 if (!array_key_exists($i, $this->answers)) {
316 $this->answers[$i] = new stdClass;
317 $this->answers[$i]->lessonid = $this->lesson->id;
318 $this->answers[$i]->pageid = $this->id;
319 $this->answers[$i]->timecreated = $this->timecreated;
322 if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
323 $this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
324 $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
326 if (!empty($properties->response_editor[$i])) {
327 $this->answers[$i]->response = $properties->response_editor[$i];
328 $this->answers[$i]->responseformat = 0;
331 if (isset($properties->jumpto[$i])) {
332 $this->answers[$i]->jumpto = $properties->jumpto[$i];
334 if ($this->lesson->custom && isset($properties->score[$i])) {
335 $this->answers[$i]->score = $properties->score[$i];
338 // we don't need to check for isset here because properties called it's own isset method.
339 if ($this->answers[$i]->answer != '') {
340 if (!isset($this->answers[$i]->id)) {
341 $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
342 } else {
343 $DB->update_record("lesson_answers", $this->answers[$i]->properties());
345 // Save files in answers and responses.
346 $this->save_answers_files($context, $maxbytes, $this->answers[$i],
347 $properties->answer_editor[$i], $properties->response_editor[$i]);
348 } else if ($i < 2) {
349 if (!isset($this->answers[$i]->id)) {
350 $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
351 } else {
352 $DB->update_record("lesson_answers", $this->answers[$i]->properties());
355 // Save files in answers and responses.
356 $this->save_answers_files( $context, $maxbytes, $this->answers[$i],
357 $properties->answer_editor[$i], $properties->response_editor[$i]);
358 } else if (isset($this->answers[$i]->id)) {
359 $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
360 unset($this->answers[$i]);
363 return true;
365 public function stats(array &$pagestats, $tries) {
366 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
367 $temp = $tries[$this->lesson->maxattempts - 1];
368 } else {
369 // else, user attempted the question less than the max, so grab the last one
370 $temp = end($tries);
372 if ($temp->correct) {
373 if (isset($pagestats[$temp->pageid]["correct"])) {
374 $pagestats[$temp->pageid]["correct"]++;
375 } else {
376 $pagestats[$temp->pageid]["correct"] = 1;
379 if (isset($pagestats[$temp->pageid]["total"])) {
380 $pagestats[$temp->pageid]["total"]++;
381 } else {
382 $pagestats[$temp->pageid]["total"] = 1;
384 return true;
386 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
387 $answers = array();
388 foreach ($this->get_answers() as $answer) {
389 $answers[$answer->id] = $answer;
391 $formattextdefoptions = new stdClass;
392 $formattextdefoptions->para = false; //I'll use it widely in this page
393 foreach ($answers as $answer) {
394 if ($n == 0 && $useranswer != null && $useranswer->correct) {
395 if ($answer->response == null && $useranswer != null) {
396 $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
397 } else {
398 $answerdata->response = $answer->response;
400 if ($this->lesson->custom) {
401 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
402 } else {
403 $answerdata->score = get_string("receivedcredit", "lesson");
405 } elseif ($n == 1 && $useranswer != null && !$useranswer->correct) {
406 if ($answer->response == null && $useranswer != null) {
407 $answerdata->response = get_string("thatsthewronganswer", "lesson");
408 } else {
409 $answerdata->response = $answer->response;
411 if ($this->lesson->custom) {
412 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
413 } else {
414 $answerdata->score = get_string("didnotreceivecredit", "lesson");
416 } elseif ($n > 1) {
417 $data = '<label class="accesshide" for="answer_' . $n . '">' . get_string('answer', 'lesson') . '</label>';
418 $data .= strip_tags(format_string($answer->answer)) . ' ';
419 if ($useranswer != null) {
420 $userresponse = explode(",", $useranswer->useranswer);
421 $data .= '<label class="accesshide" for="stu_answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>';
422 $data .= "<select id=\"stu_answer_response_" . $n . "\" disabled=\"disabled\"><option selected=\"selected\">";
423 if (array_key_exists($i, $userresponse)) {
424 $data .= $userresponse[$i];
426 $data .= "</option></select>";
427 } else {
428 $data .= '<label class="accesshide" for="answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>';
429 $data .= "<select id=\"answer_response_" . $n . "\" disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->response))."</option></select>";
432 if ($n == 2) {
433 if (isset($pagestats[$this->properties->id])) {
434 if (!array_key_exists('correct', $pagestats[$this->properties->id])) {
435 $pagestats[$this->properties->id]["correct"] = 0;
437 $percent = $pagestats[$this->properties->id]["correct"] / $pagestats[$this->properties->id]["total"] * 100;
438 $percent = round($percent, 2);
439 $percent .= "% ".get_string("answeredcorrectly", "lesson");
440 } else {
441 $percent = get_string("nooneansweredthisquestion", "lesson");
443 } else {
444 $percent = '';
447 $answerdata->answers[] = array($data, $percent);
448 $i++;
450 $n++;
451 $answerpage->answerdata = $answerdata;
453 return $answerpage;
455 public function get_jumps() {
456 global $DB;
457 // The jumps for matching question type are stored in the 1st and 2nd answer record.
458 $jumps = array();
459 if ($answers = $DB->get_records("lesson_answers", array("lessonid" => $this->lesson->id, "pageid" => $this->properties->id), 'id', '*', 0, 2)) {
460 foreach ($answers as $answer) {
461 $jumps[] = $this->get_jump_name($answer->jumpto);
463 } else {
464 $jumps[] = $this->get_jump_name($this->properties->nextpageid);
466 return $jumps;
470 class lesson_add_page_form_matching extends lesson_add_page_form_base {
472 public $qtype = 'matching';
473 public $qtypestring = 'matching';
475 public function custom_definition() {
477 $this->_form->addElement('header', 'correctresponse', get_string('correctresponse', 'lesson'));
478 $this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'),
479 array('rows' => '4', 'columns' => '80'),
480 array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
481 $this->_form->setType('answer_editor[0]', PARAM_RAW);
482 $this->_form->setDefault('answer_editor[0]', array('text' => '', 'format' => FORMAT_HTML));
483 $this->add_jumpto(0, get_string('correctanswerjump','lesson'), LESSON_NEXTPAGE);
484 $this->add_score(0, get_string("correctanswerscore", "lesson"), 1);
486 $this->_form->addElement('header', 'wrongresponse', get_string('wrongresponse', 'lesson'));
487 $this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'),
488 array('rows' => '4', 'columns' => '80'),
489 array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
490 $this->_form->setType('answer_editor[1]', PARAM_RAW);
491 $this->_form->setDefault('answer_editor[1]', array('text' => '', 'format' => FORMAT_HTML));
493 $this->add_jumpto(1, get_string('wronganswerjump','lesson'), LESSON_THISPAGE);
494 $this->add_score(1, get_string("wronganswerscore", "lesson"), 0);
496 for ($i = 2; $i < $this->_customdata['lesson']->maxanswers+2; $i++) {
497 $this->_form->addElement('header', 'matchingpair'.($i-1), get_string('matchingpair', 'lesson', $i-1));
498 $this->add_answer($i, null, ($i < 4));
499 $required = ($i < 4);
500 $label = get_string('matchesanswer','lesson');
501 $count = $i;
502 $this->_form->addElement('text', 'response_editor['.$count.']', $label, array('size'=>'50'));
503 $this->_form->setType('response_editor['.$count.']', PARAM_NOTAGS);
504 $this->_form->setDefault('response_editor['.$count.']', '');
505 if ($required) {
506 $this->_form->addRule('response_editor['.$count.']', get_string('required'), 'required', null, 'client');
512 class lesson_display_answer_form_matching extends moodleform {
514 public function definition() {
515 global $USER, $OUTPUT, $PAGE;
516 $mform = $this->_form;
517 $answers = $this->_customdata['answers'];
518 $useranswers = $this->_customdata['useranswers'];
519 $responseoptions = $this->_customdata['responseoptions'];
520 $lessonid = $this->_customdata['lessonid'];
521 $contents = $this->_customdata['contents'];
523 $mform->addElement('header', 'pageheader');
525 $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
527 $hasattempt = false;
528 $disabled = '';
529 if (isset($useranswers) && !empty($useranswers)) {
530 $hasattempt = true;
531 $disabled = array('disabled' => 'disabled');
534 $options = new stdClass;
535 $options->para = false;
536 $options->noclean = true;
538 $mform->addElement('hidden', 'id');
539 $mform->setType('id', PARAM_INT);
541 $mform->addElement('hidden', 'pageid');
542 $mform->setType('pageid', PARAM_INT);
544 $i = 0;
546 foreach ($answers as $answer) {
547 $mform->addElement('html', '<div class="answeroption">');
548 if ($answer->response != null) {
549 $responseid = 'response['.$answer->id.']';
550 if ($hasattempt) {
551 $responseid = 'response_'.$answer->id;
552 $mform->addElement('hidden', 'response['.$answer->id.']', htmlspecialchars($useranswers[$i]));
553 // Temporary fixed until MDL-38885 gets integrated
554 $mform->setType('response', PARAM_TEXT);
556 $context = context_module::instance($PAGE->cm->id);
557 $answer->answer = file_rewrite_pluginfile_urls($answer->answer, 'pluginfile.php', $context->id,
558 'mod_lesson', 'page_answers', $answer->id);
559 $mform->addElement('select', $responseid, format_text($answer->answer,$answer->answerformat,$options), $responseoptions, $disabled);
560 $mform->setType($responseid, PARAM_TEXT);
561 if ($hasattempt) {
562 $mform->setDefault($responseid, htmlspecialchars(trim($useranswers[$i])));
563 } else {
564 $mform->setDefault($responseid, 'answeroption');
567 $mform->addElement('html', '</div>');
568 $i++;
570 if ($hasattempt) {
571 $this->add_action_buttons(null, get_string("nextpage", "lesson"));
572 } else {
573 $this->add_action_buttons(null, get_string("submit", "lesson"));