[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 * 21 * @package mod_lesson 22 * @copyright 2009 Sam Hemelryk 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 **/ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 /** Matching question type */ 29 define("LESSON_PAGE_MATCHING", "5"); 30 31 class lesson_page_type_matching extends lesson_page { 32 33 protected $type = lesson_page::TYPE_QUESTION; 34 protected $typeid = LESSON_PAGE_MATCHING; 35 protected $typeidstring = 'matching'; 36 protected $string = null; 37 38 public function get_typeid() { 39 return $this->typeid; 40 } 41 public function get_typestring() { 42 if ($this->string===null) { 43 $this->string = get_string($this->typeidstring, 'lesson'); 44 } 45 return $this->string; 46 } 47 public function get_idstring() { 48 return $this->typeidstring; 49 } 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(); 58 } 59 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); 64 65 $answers = array(); 66 foreach ($getanswers as $getanswer) { 67 $answers[$getanswer->id] = $getanswer; 68 } 69 70 $responses = array(); 71 foreach ($answers as $answer) { 72 // get all the response 73 if ($answer->response != null) { 74 $responses[] = trim($answer->response); 75 } 76 } 77 78 $responseoptions = array(''=>get_string('choosedots')); 79 if (!empty($responses)) { 80 shuffle($responses); 81 foreach ($responses as $response) { 82 $responseoptions[htmlspecialchars($response)] = $response; 83 } 84 } 85 if (isset($USER->modattempts[$this->lesson->id]) && !empty($attempt->useranswer)) { 86 $useranswers = explode(',', $attempt->useranswer); 87 $t = 0; 88 } else { 89 $useranswers = array(); 90 } 91 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; 96 } 97 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; 105 106 $cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course); 107 $context = context_module::instance($cm->id); 108 109 $answers = array(); 110 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']; 118 } 119 if (!empty($properties->response_editor[$i])) { 120 $answer->response = $properties->response_editor[$i]; 121 $answer->responseformat = 0; 122 } 123 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 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; 141 } 142 } 143 $this->answers = $answers; 144 return $answers; 145 } 146 147 public function check_answer() { 148 global $CFG, $PAGE; 149 150 $formattextdefoptions = new stdClass(); 151 $formattextdefoptions->noclean = true; 152 $formattextdefoptions->para = false; 153 154 $result = parent::check_answer(); 155 156 $mform = $this->make_answer_form(); 157 158 $data = $mform->get_data(); 159 require_sesskey(); 160 161 if (!$data) { 162 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id))); 163 } 164 165 $response = $data->response; 166 $getanswers = $this->get_answers(); 167 foreach ($getanswers as $key => $answer) { 168 $getanswers[$key] = parent::rewrite_answers_urls($answer); 169 } 170 171 $correct = array_shift($getanswers); 172 $wrong = array_shift($getanswers); 173 174 $answers = array(); 175 foreach ($getanswers as $key => $answer) { 176 if ($answer->answer !== '' or $answer->response !== '') { 177 $answers[$answer->id] = $answer; 178 } 179 } 180 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; 188 } 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++; 197 } 198 } 199 } 200 201 $result->userresponse = implode(",", $userresponse); 202 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; 213 } 214 215 return $result; 216 } 217 218 public function option_description_string() { 219 return get_string("firstanswershould", "lesson"); 220 } 221 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; 229 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>'; 239 } 240 $cells[] = format_text($answer->answer, $answer->answerformat, $options); 241 $table->data[] = new html_table_row($cells); 242 } 243 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); 249 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); 259 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); 264 } 265 266 if ($n === 0){ 267 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;'; 268 } 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"; 282 } 283 $cells[] = format_text($answer->answer, $answer->answerformat, $options); 284 $table->data[] = new html_table_row($cells); 285 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); 290 } 291 $i++; 292 } 293 return $table; 294 } 295 /** 296 * Updates the page and its answers 297 * 298 * @global moodle_database $DB 299 * @global moodle_page $PAGE 300 * @param stdClass $properties 301 * @return bool 302 */ 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); 311 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; 320 } 321 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']; 325 } 326 if (!empty($properties->response_editor[$i])) { 327 $this->answers[$i]->response = $properties->response_editor[$i]; 328 $this->answers[$i]->responseformat = 0; 329 } 330 331 if (isset($properties->jumpto[$i])) { 332 $this->answers[$i]->jumpto = $properties->jumpto[$i]; 333 } 334 if ($this->lesson->custom && isset($properties->score[$i])) { 335 $this->answers[$i]->score = $properties->score[$i]; 336 } 337 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()); 344 } 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()); 353 } 354 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]); 361 } 362 } 363 return true; 364 } 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); 371 } 372 if ($temp->correct) { 373 if (isset($pagestats[$temp->pageid]["correct"])) { 374 $pagestats[$temp->pageid]["correct"]++; 375 } else { 376 $pagestats[$temp->pageid]["correct"] = 1; 377 } 378 } 379 if (isset($pagestats[$temp->pageid]["total"])) { 380 $pagestats[$temp->pageid]["total"]++; 381 } else { 382 $pagestats[$temp->pageid]["total"] = 1; 383 } 384 return true; 385 } 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; 390 } 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; 399 } 400 if ($this->lesson->custom) { 401 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; 402 } else { 403 $answerdata->score = get_string("receivedcredit", "lesson"); 404 } 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; 410 } 411 if ($this->lesson->custom) { 412 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; 413 } else { 414 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 415 } 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]; 425 } 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>"; 430 } 431 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; 436 } 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"); 442 } 443 } else { 444 $percent = ''; 445 } 446 447 $answerdata->answers[] = array($data, $percent); 448 $i++; 449 } 450 $n++; 451 $answerpage->answerdata = $answerdata; 452 } 453 return $answerpage; 454 } 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); 462 } 463 } else { 464 $jumps[] = $this->get_jump_name($this->properties->nextpageid); 465 } 466 return $jumps; 467 } 468 } 469 470 class lesson_add_page_form_matching extends lesson_add_page_form_base { 471 472 public $qtype = 'matching'; 473 public $qtypestring = 'matching'; 474 475 public function custom_definition() { 476 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); 485 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)); 492 493 $this->add_jumpto(1, get_string('wronganswerjump','lesson'), LESSON_THISPAGE); 494 $this->add_score(1, get_string("wronganswerscore", "lesson"), 0); 495 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'); 507 } 508 } 509 } 510 } 511 512 class lesson_display_answer_form_matching extends moodleform { 513 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']; 522 523 $mform->addElement('header', 'pageheader'); 524 525 $mform->addElement('html', $OUTPUT->container($contents, 'contents')); 526 527 $hasattempt = false; 528 $disabled = ''; 529 if (isset($useranswers) && !empty($useranswers)) { 530 $hasattempt = true; 531 $disabled = array('disabled' => 'disabled'); 532 } 533 534 $options = new stdClass; 535 $options->para = false; 536 $options->noclean = true; 537 538 $mform->addElement('hidden', 'id'); 539 $mform->setType('id', PARAM_INT); 540 541 $mform->addElement('hidden', 'pageid'); 542 $mform->setType('pageid', PARAM_INT); 543 544 $i = 0; 545 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); 555 } 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'); 565 } 566 } 567 $mform->addElement('html', '</div>'); 568 $i++; 569 } 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")); 574 } 575 } 576 577 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |