|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00030 define("LESSON_PAGE_TRUEFALSE", "2"); 00031 00032 class lesson_page_type_truefalse extends lesson_page { 00033 00034 protected $type = lesson_page::TYPE_QUESTION; 00035 protected $typeidstring = 'truefalse'; 00036 protected $typeid = LESSON_PAGE_TRUEFALSE; 00037 protected $string = null; 00038 00039 public function get_typeid() { 00040 return $this->typeid; 00041 } 00042 public function get_typestring() { 00043 if ($this->string===null) { 00044 $this->string = get_string($this->typeidstring, 'lesson'); 00045 } 00046 return $this->string; 00047 } 00048 public function get_idstring() { 00049 return $this->typeidstring; 00050 } 00051 public function display($renderer, $attempt) { 00052 global $USER, $CFG, $PAGE; 00053 $answers = $this->get_answers(); 00054 shuffle($answers); 00055 00056 $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents(), 'attempt'=>$attempt); 00057 $mform = new lesson_display_answer_form_truefalse($CFG->wwwroot.'/mod/lesson/continue.php', $params); 00058 $data = new stdClass; 00059 $data->id = $PAGE->cm->id; 00060 $data->pageid = $this->properties->id; 00061 $mform->set_data($data); 00062 return $mform->display(); 00063 } 00064 public function check_answer() { 00065 global $DB, $CFG; 00066 $formattextdefoptions = new stdClass(); 00067 $formattextdefoptions->noclean = true; 00068 $formattextdefoptions->para = false; 00069 00070 $answers = $this->get_answers(); 00071 shuffle($answers); 00072 $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents()); 00073 $mform = new lesson_display_answer_form_truefalse($CFG->wwwroot.'/mod/lesson/continue.php', $params); 00074 $data = $mform->get_data(); 00075 require_sesskey(); 00076 00077 $result = parent::check_answer(); 00078 00079 $answerid = $data->answerid; 00080 if ($answerid === false) { 00081 $result->noanswer = true; 00082 return $result; 00083 } 00084 $result->answerid = $answerid; 00085 $answer = $DB->get_record("lesson_answers", array("id" => $result->answerid), '*', MUST_EXIST); 00086 if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) { 00087 $result->correctanswer = true; 00088 } 00089 if ($this->lesson->custom) { 00090 if ($answer->score > 0) { 00091 $result->correctanswer = true; 00092 } else { 00093 $result->correctanswer = false; 00094 } 00095 } 00096 $result->newpageid = $answer->jumpto; 00097 $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions); 00098 $result->studentanswer = $result->userresponse = $answer->answer; 00099 return $result; 00100 } 00101 00102 public function display_answers(html_table $table) { 00103 $answers = $this->get_answers(); 00104 $options = new stdClass(); 00105 $options->noclean = true; 00106 $options->para = false; 00107 $i = 1; 00108 foreach ($answers as $answer) { 00109 $cells = array(); 00110 if ($this->lesson->custom && $answer->score > 0) { 00111 // if the score is > 0, then it is correct 00112 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; 00113 } else if ($this->lesson->custom) { 00114 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n"; 00115 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) { 00116 // underline correct answers 00117 $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n"; 00118 } else { 00119 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; 00120 } 00121 $cells[] = format_text($answer->answer, $answer->answerformat, $options); 00122 $table->data[] = new html_table_row($cells); 00123 00124 $cells = array(); 00125 $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>"; 00126 $cells[] = format_text($answer->response, $answer->responseformat, $options); 00127 $table->data[] = new html_table_row($cells); 00128 00129 $cells = array(); 00130 $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>'; 00131 $cells[] = $answer->score; 00132 $table->data[] = new html_table_row($cells); 00133 00134 $cells = array(); 00135 $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>'; 00136 $cells[] = $this->get_jump_name($answer->jumpto); 00137 $table->data[] = new html_table_row($cells); 00138 00139 if ($i === 1){ 00140 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;'; 00141 } 00142 00143 $i++; 00144 } 00145 return $table; 00146 } 00147 00156 public function update($properties) { 00157 global $DB, $PAGE; 00158 $answers = $this->get_answers(); 00159 $properties->id = $this->properties->id; 00160 $properties->lessonid = $this->lesson->id; 00161 $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); 00162 $DB->update_record("lesson_pages", $properties); 00163 00164 // need to reset offset for correct and wrong responses 00165 $this->lesson->maxanswers = 2; 00166 for ($i = 0; $i < $this->lesson->maxanswers; $i++) { 00167 if (!array_key_exists($i, $this->answers)) { 00168 $this->answers[$i] = new stdClass; 00169 $this->answers[$i]->lessonid = $this->lesson->id; 00170 $this->answers[$i]->pageid = $this->id; 00171 $this->answers[$i]->timecreated = $this->timecreated; 00172 } 00173 00174 if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) { 00175 $this->answers[$i]->answer = $properties->answer_editor[$i]['text']; 00176 $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format']; 00177 } 00178 00179 if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) { 00180 $this->answers[$i]->response = $properties->response_editor[$i]['text']; 00181 $this->answers[$i]->responseformat = $properties->response_editor[$i]['format']; 00182 } 00183 00184 // we don't need to check for isset here because properties called it's own isset method. 00185 if ($this->answers[$i]->answer != '') { 00186 if (isset($properties->jumpto[$i])) { 00187 $this->answers[$i]->jumpto = $properties->jumpto[$i]; 00188 } 00189 if ($this->lesson->custom && isset($properties->score[$i])) { 00190 $this->answers[$i]->score = $properties->score[$i]; 00191 } 00192 if (!isset($this->answers[$i]->id)) { 00193 $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]); 00194 } else { 00195 $DB->update_record("lesson_answers", $this->answers[$i]->properties()); 00196 } 00197 } else if (isset($this->answers[$i]->id)) { 00198 $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id)); 00199 unset($this->answers[$i]); 00200 } 00201 } 00202 return true; 00203 } 00204 00205 public function stats(array &$pagestats, $tries) { 00206 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt 00207 $temp = $tries[$this->lesson->maxattempts - 1]; 00208 } else { 00209 // else, user attempted the question less than the max, so grab the last one 00210 $temp = end($tries); 00211 } 00212 if ($this->properties->qoption) { 00213 $userresponse = explode(",", $temp->useranswer); 00214 foreach ($userresponse as $response) { 00215 if (isset($pagestats[$temp->pageid][$response])) { 00216 $pagestats[$temp->pageid][$response]++; 00217 } else { 00218 $pagestats[$temp->pageid][$response] = 1; 00219 } 00220 } 00221 } else { 00222 if (isset($pagestats[$temp->pageid][$temp->answerid])) { 00223 $pagestats[$temp->pageid][$temp->answerid]++; 00224 } else { 00225 $pagestats[$temp->pageid][$temp->answerid] = 1; 00226 } 00227 } 00228 if (isset($pagestats[$temp->pageid]["total"])) { 00229 $pagestats[$temp->pageid]["total"]++; 00230 } else { 00231 $pagestats[$temp->pageid]["total"] = 1; 00232 } 00233 return true; 00234 } 00235 00236 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) { 00237 $answers = $this->get_answers(); 00238 $formattextdefoptions = new stdClass(); //I'll use it widely in this page 00239 $formattextdefoptions->para = false; 00240 $formattextdefoptions->noclean = true; 00241 foreach ($answers as $answer) { 00242 if ($this->properties->qoption) { 00243 if ($useranswer == NULL) { 00244 $userresponse = array(); 00245 } else { 00246 $userresponse = explode(",", $useranswer->useranswer); 00247 } 00248 if (in_array($answer->id, $userresponse)) { 00249 // make checked 00250 $data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />"; 00251 if (!isset($answerdata->response)) { 00252 if ($answer->response == NULL) { 00253 if ($useranswer->correct) { 00254 $answerdata->response = get_string("thatsthecorrectanswer", "lesson"); 00255 } else { 00256 $answerdata->response = get_string("thatsthewronganswer", "lesson"); 00257 } 00258 } else { 00259 $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions); 00260 } 00261 } 00262 if (!isset($answerdata->score)) { 00263 if ($this->lesson->custom) { 00264 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; 00265 } elseif ($useranswer->correct) { 00266 $answerdata->score = get_string("receivedcredit", "lesson"); 00267 } else { 00268 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 00269 } 00270 } 00271 } else { 00272 // unchecked 00273 $data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />"; 00274 } 00275 if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) { 00276 $data .= "<div class=highlight>".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>"; 00277 } else { 00278 $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions); 00279 } 00280 } else { 00281 if ($useranswer != NULL and $answer->id == $useranswer->answerid) { 00282 // make checked 00283 $data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />"; 00284 if ($answer->response == NULL) { 00285 if ($useranswer->correct) { 00286 $answerdata->response = get_string("thatsthecorrectanswer", "lesson"); 00287 } else { 00288 $answerdata->response = get_string("thatsthewronganswer", "lesson"); 00289 } 00290 } else { 00291 $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions); 00292 } 00293 if ($this->lesson->custom) { 00294 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; 00295 } elseif ($useranswer->correct) { 00296 $answerdata->score = get_string("receivedcredit", "lesson"); 00297 } else { 00298 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 00299 } 00300 } else { 00301 // unchecked 00302 $data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />"; 00303 } 00304 if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) { 00305 $data .= "<div class=\"highlight\">".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>"; 00306 } else { 00307 $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions); 00308 } 00309 } 00310 if (isset($pagestats[$this->properties->id][$answer->id])) { 00311 $percent = $pagestats[$this->properties->id][$answer->id] / $pagestats[$this->properties->id]["total"] * 100; 00312 $percent = round($percent, 2); 00313 $percent .= "% ".get_string("checkedthisone", "lesson"); 00314 } else { 00315 $percent = get_string("noonecheckedthis", "lesson"); 00316 } 00317 00318 $answerdata->answers[] = array($data, $percent); 00319 $answerpage->answerdata = $answerdata; 00320 } 00321 return $answerpage; 00322 } 00323 } 00324 00325 class lesson_add_page_form_truefalse extends lesson_add_page_form_base { 00326 00327 public $qtype = 'truefalse'; 00328 public $qtypestring = 'truefalse'; 00329 00330 public function custom_definition() { 00331 $this->_form->addElement('header', 'answertitle0', get_string('correctresponse', 'lesson')); 00332 $this->add_answer(0, NULL, true); 00333 $this->add_response(0); 00334 $this->add_jumpto(0, get_string('correctanswerjump', 'lesson'), LESSON_NEXTPAGE); 00335 $this->add_score(0, get_string('correctanswerscore', 'lesson'), 1); 00336 00337 $this->_form->addElement('header', 'answertitle1', get_string('wrongresponse', 'lesson')); 00338 $this->add_answer(1, NULL, true); 00339 $this->add_response(1); 00340 $this->add_jumpto(1, get_string('wronganswerjump', 'lesson'), LESSON_THISPAGE); 00341 $this->add_score(1, get_string('wronganswerscore', 'lesson'), 0); 00342 } 00343 } 00344 00345 class lesson_display_answer_form_truefalse extends moodleform { 00346 00347 public function definition() { 00348 global $USER, $OUTPUT; 00349 $mform = $this->_form; 00350 $answers = $this->_customdata['answers']; 00351 $lessonid = $this->_customdata['lessonid']; 00352 $contents = $this->_customdata['contents']; 00353 if (array_key_exists('attempt', $this->_customdata)) { 00354 $attempt = $this->_customdata['attempt']; 00355 } else { 00356 $attempt = new stdClass(); 00357 $attempt->answerid = null; 00358 } 00359 00360 $mform->addElement('header', 'pageheader'); 00361 00362 $mform->addElement('html', $OUTPUT->container($contents, 'contents')); 00363 00364 $hasattempt = false; 00365 $disabled = ''; 00366 if (isset($USER->modattempts[$lessonid]) && !empty($USER->modattempts[$lessonid])) { 00367 $hasattempt = true; 00368 $disabled = array('disabled' => 'disabled'); 00369 } 00370 00371 $options = new stdClass(); 00372 $options->para = false; 00373 $options->noclean = true; 00374 00375 $mform->addElement('hidden', 'id'); 00376 $mform->setType('id', PARAM_INT); 00377 00378 $mform->addElement('hidden', 'pageid'); 00379 $mform->setType('pageid', PARAM_INT); 00380 00381 $i = 0; 00382 foreach ($answers as $answer) { 00383 $mform->addElement('html', '<div class="answeroption">'); 00384 $ansid = 'answerid'; 00385 if ($hasattempt) { 00386 $ansid = 'answer_id'; 00387 } 00388 00389 $mform->addElement('radio', $ansid, null, format_text($answer->answer, $answer->answerformat, $options), $answer->id, $disabled); 00390 $mform->setType($ansid, PARAM_INT); 00391 if ($hasattempt && $answer->id == $USER->modattempts[$lessonid]->answerid) { 00392 $mform->setDefault($ansid, $attempt->answerid); 00393 $mform->addElement('hidden', 'answerid', $answer->id); 00394 $mform->setType('answerid', PARAM_INT); 00395 } 00396 $mform->addElement('html', '</div>'); 00397 $i++; 00398 } 00399 00400 if ($hasattempt) { 00401 $this->add_action_buttons(null, get_string("nextpage", "lesson")); 00402 } else { 00403 $this->add_action_buttons(null, get_string("submit", "lesson")); 00404 } 00405 00406 } 00407 00408 }