[ 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 * True/false 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 /** True/False question type */ 29 define("LESSON_PAGE_TRUEFALSE", "2"); 30 31 class lesson_page_type_truefalse extends lesson_page { 32 33 protected $type = lesson_page::TYPE_QUESTION; 34 protected $typeidstring = 'truefalse'; 35 protected $typeid = LESSON_PAGE_TRUEFALSE; 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 $answers = $this->get_answers(); 53 foreach ($answers as $key => $answer) { 54 $answers[$key] = parent::rewrite_answers_urls($answer); 55 } 56 shuffle($answers); 57 58 $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents(), 'attempt'=>$attempt); 59 $mform = new lesson_display_answer_form_truefalse($CFG->wwwroot.'/mod/lesson/continue.php', $params); 60 $data = new stdClass; 61 $data->id = $PAGE->cm->id; 62 $data->pageid = $this->properties->id; 63 $mform->set_data($data); 64 return $mform->display(); 65 } 66 public function check_answer() { 67 global $DB, $CFG; 68 $formattextdefoptions = new stdClass(); 69 $formattextdefoptions->noclean = true; 70 $formattextdefoptions->para = false; 71 72 $answers = $this->get_answers(); 73 shuffle($answers); 74 $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents()); 75 $mform = new lesson_display_answer_form_truefalse($CFG->wwwroot.'/mod/lesson/continue.php', $params); 76 $data = $mform->get_data(); 77 require_sesskey(); 78 79 $result = parent::check_answer(); 80 81 if (empty($data->answerid)) { 82 $result->noanswer = true; 83 return $result; 84 } 85 $result->answerid = $data->answerid; 86 $answer = $DB->get_record("lesson_answers", array("id" => $result->answerid), '*', MUST_EXIST); 87 $answer = parent::rewrite_answers_urls($answer); 88 if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) { 89 $result->correctanswer = true; 90 } 91 if ($this->lesson->custom) { 92 if ($answer->score > 0) { 93 $result->correctanswer = true; 94 } else { 95 $result->correctanswer = false; 96 } 97 } 98 $result->newpageid = $answer->jumpto; 99 $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions); 100 $result->studentanswer = $result->userresponse = $answer->answer; 101 return $result; 102 } 103 104 public function display_answers(html_table $table) { 105 $answers = $this->get_answers(); 106 $options = new stdClass(); 107 $options->noclean = true; 108 $options->para = false; 109 $i = 1; 110 foreach ($answers as $answer) { 111 $answer = parent::rewrite_answers_urls($answer); 112 $cells = array(); 113 if ($this->lesson->custom && $answer->score > 0) { 114 // if the score is > 0, then it is correct 115 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; 116 } else if ($this->lesson->custom) { 117 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n"; 118 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) { 119 // underline correct answers 120 $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n"; 121 } else { 122 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; 123 } 124 $cells[] = format_text($answer->answer, $answer->answerformat, $options); 125 $table->data[] = new html_table_row($cells); 126 127 $cells = array(); 128 $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>"; 129 $cells[] = format_text($answer->response, $answer->responseformat, $options); 130 $table->data[] = new html_table_row($cells); 131 132 $cells = array(); 133 $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>'; 134 $cells[] = $answer->score; 135 $table->data[] = new html_table_row($cells); 136 137 $cells = array(); 138 $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>'; 139 $cells[] = $this->get_jump_name($answer->jumpto); 140 $table->data[] = new html_table_row($cells); 141 142 if ($i === 1){ 143 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;'; 144 } 145 146 $i++; 147 } 148 return $table; 149 } 150 151 /** 152 * Updates the page and its answers 153 * 154 * @global moodle_database $DB 155 * @global moodle_page $PAGE 156 * @param stdClass $properties 157 * @return bool 158 */ 159 public function update($properties, $context = null, $maxbytes = null) { 160 global $DB, $PAGE; 161 $answers = $this->get_answers(); 162 $properties->id = $this->properties->id; 163 $properties->lessonid = $this->lesson->id; 164 $properties->timemodified = time(); 165 $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); 166 $DB->update_record("lesson_pages", $properties); 167 168 // need to reset offset for correct and wrong responses 169 $this->lesson->maxanswers = 2; 170 for ($i = 0; $i < $this->lesson->maxanswers; $i++) { 171 if (!array_key_exists($i, $this->answers)) { 172 $this->answers[$i] = new stdClass; 173 $this->answers[$i]->lessonid = $this->lesson->id; 174 $this->answers[$i]->pageid = $this->id; 175 $this->answers[$i]->timecreated = $this->timecreated; 176 } 177 178 if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) { 179 $this->answers[$i]->answer = $properties->answer_editor[$i]['text']; 180 $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format']; 181 } 182 183 if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) { 184 $this->answers[$i]->response = $properties->response_editor[$i]['text']; 185 $this->answers[$i]->responseformat = $properties->response_editor[$i]['format']; 186 } 187 188 // we don't need to check for isset here because properties called it's own isset method. 189 if ($this->answers[$i]->answer != '') { 190 if (isset($properties->jumpto[$i])) { 191 $this->answers[$i]->jumpto = $properties->jumpto[$i]; 192 } 193 if ($this->lesson->custom && isset($properties->score[$i])) { 194 $this->answers[$i]->score = $properties->score[$i]; 195 } 196 if (!isset($this->answers[$i]->id)) { 197 $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]); 198 } else { 199 $DB->update_record("lesson_answers", $this->answers[$i]->properties()); 200 } 201 // Save files in answers and responses. 202 $this->save_answers_files($context, $maxbytes, $this->answers[$i], 203 $properties->answer_editor[$i], $properties->response_editor[$i]); 204 } else if (isset($this->answers[$i]->id)) { 205 $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id)); 206 unset($this->answers[$i]); 207 } 208 } 209 return true; 210 } 211 212 public function stats(array &$pagestats, $tries) { 213 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt 214 $temp = $tries[$this->lesson->maxattempts - 1]; 215 } else { 216 // else, user attempted the question less than the max, so grab the last one 217 $temp = end($tries); 218 } 219 if ($this->properties->qoption) { 220 $userresponse = explode(",", $temp->useranswer); 221 foreach ($userresponse as $response) { 222 if (isset($pagestats[$temp->pageid][$response])) { 223 $pagestats[$temp->pageid][$response]++; 224 } else { 225 $pagestats[$temp->pageid][$response] = 1; 226 } 227 } 228 } else { 229 if (isset($pagestats[$temp->pageid][$temp->answerid])) { 230 $pagestats[$temp->pageid][$temp->answerid]++; 231 } else { 232 $pagestats[$temp->pageid][$temp->answerid] = 1; 233 } 234 } 235 if (isset($pagestats[$temp->pageid]["total"])) { 236 $pagestats[$temp->pageid]["total"]++; 237 } else { 238 $pagestats[$temp->pageid]["total"] = 1; 239 } 240 return true; 241 } 242 243 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) { 244 $answers = $this->get_answers(); 245 $formattextdefoptions = new stdClass(); //I'll use it widely in this page 246 $formattextdefoptions->para = false; 247 $formattextdefoptions->noclean = true; 248 foreach ($answers as $answer) { 249 $answer = parent::rewrite_answers_urls($answer); 250 if ($this->properties->qoption) { 251 if ($useranswer == null) { 252 $userresponse = array(); 253 } else { 254 $userresponse = explode(",", $useranswer->useranswer); 255 } 256 if (in_array($answer->id, $userresponse)) { 257 // make checked 258 $data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />"; 259 if (!isset($answerdata->response)) { 260 if ($answer->response == null) { 261 if ($useranswer->correct) { 262 $answerdata->response = get_string("thatsthecorrectanswer", "lesson"); 263 } else { 264 $answerdata->response = get_string("thatsthewronganswer", "lesson"); 265 } 266 } else { 267 $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions); 268 } 269 } 270 if (!isset($answerdata->score)) { 271 if ($this->lesson->custom) { 272 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; 273 } elseif ($useranswer->correct) { 274 $answerdata->score = get_string("receivedcredit", "lesson"); 275 } else { 276 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 277 } 278 } 279 } else { 280 // unchecked 281 $data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />"; 282 } 283 if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) { 284 $data .= "<div class=highlight>".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>"; 285 } else { 286 $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions); 287 } 288 } else { 289 if ($useranswer != null and $answer->id == $useranswer->answerid) { 290 // make checked 291 $data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />"; 292 if ($answer->response == null) { 293 if ($useranswer->correct) { 294 $answerdata->response = get_string("thatsthecorrectanswer", "lesson"); 295 } else { 296 $answerdata->response = get_string("thatsthewronganswer", "lesson"); 297 } 298 } else { 299 $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions); 300 } 301 if ($this->lesson->custom) { 302 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; 303 } elseif ($useranswer->correct) { 304 $answerdata->score = get_string("receivedcredit", "lesson"); 305 } else { 306 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 307 } 308 } else { 309 // unchecked 310 $data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />"; 311 } 312 if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) { 313 $data .= "<div class=\"highlight\">".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>"; 314 } else { 315 $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions); 316 } 317 } 318 if (isset($pagestats[$this->properties->id][$answer->id])) { 319 $percent = $pagestats[$this->properties->id][$answer->id] / $pagestats[$this->properties->id]["total"] * 100; 320 $percent = round($percent, 2); 321 $percent .= "% ".get_string("checkedthisone", "lesson"); 322 } else { 323 $percent = get_string("noonecheckedthis", "lesson"); 324 } 325 326 $answerdata->answers[] = array($data, $percent); 327 $answerpage->answerdata = $answerdata; 328 } 329 return $answerpage; 330 } 331 } 332 333 class lesson_add_page_form_truefalse extends lesson_add_page_form_base { 334 335 public $qtype = 'truefalse'; 336 public $qtypestring = 'truefalse'; 337 338 public function custom_definition() { 339 $this->_form->addElement('header', 'answertitle0', get_string('correctresponse', 'lesson')); 340 $this->add_answer(0, null, true); 341 $this->add_response(0); 342 $this->add_jumpto(0, get_string('correctanswerjump', 'lesson'), LESSON_NEXTPAGE); 343 $this->add_score(0, get_string('correctanswerscore', 'lesson'), 1); 344 345 $this->_form->addElement('header', 'answertitle1', get_string('wrongresponse', 'lesson')); 346 $this->add_answer(1, null, true); 347 $this->add_response(1); 348 $this->add_jumpto(1, get_string('wronganswerjump', 'lesson'), LESSON_THISPAGE); 349 $this->add_score(1, get_string('wronganswerscore', 'lesson'), 0); 350 } 351 } 352 353 class lesson_display_answer_form_truefalse extends moodleform { 354 355 public function definition() { 356 global $USER, $OUTPUT; 357 $mform = $this->_form; 358 $answers = $this->_customdata['answers']; 359 $lessonid = $this->_customdata['lessonid']; 360 $contents = $this->_customdata['contents']; 361 if (array_key_exists('attempt', $this->_customdata)) { 362 $attempt = $this->_customdata['attempt']; 363 } else { 364 $attempt = new stdClass(); 365 $attempt->answerid = null; 366 } 367 368 $mform->addElement('header', 'pageheader'); 369 370 $mform->addElement('html', $OUTPUT->container($contents, 'contents')); 371 372 $hasattempt = false; 373 $disabled = ''; 374 if (isset($USER->modattempts[$lessonid]) && !empty($USER->modattempts[$lessonid])) { 375 $hasattempt = true; 376 $disabled = array('disabled' => 'disabled'); 377 } 378 379 $options = new stdClass(); 380 $options->para = false; 381 $options->noclean = true; 382 383 $mform->addElement('hidden', 'id'); 384 $mform->setType('id', PARAM_INT); 385 386 $mform->addElement('hidden', 'pageid'); 387 $mform->setType('pageid', PARAM_INT); 388 389 $i = 0; 390 foreach ($answers as $answer) { 391 $mform->addElement('html', '<div class="answeroption">'); 392 $ansid = 'answerid'; 393 if ($hasattempt) { 394 $ansid = 'answer_id'; 395 } 396 397 $mform->addElement('radio', $ansid, null, format_text($answer->answer, $answer->answerformat, $options), $answer->id, $disabled); 398 $mform->setType($ansid, PARAM_INT); 399 if ($hasattempt && $answer->id == $USER->modattempts[$lessonid]->answerid) { 400 $mform->setDefault($ansid, $attempt->answerid); 401 $mform->addElement('hidden', 'answerid', $answer->id); 402 $mform->setType('answerid', PARAM_INT); 403 } 404 $mform->addElement('html', '</div>'); 405 $i++; 406 } 407 408 if ($hasattempt) { 409 $this->add_action_buttons(null, get_string("nextpage", "lesson")); 410 } else { 411 $this->add_action_buttons(null, get_string("submit", "lesson")); 412 } 413 414 } 415 416 }
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 |