[ 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 * Numerical 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 /** Numerical question type */ 29 define("LESSON_PAGE_NUMERICAL", "8"); 30 31 class lesson_page_type_numerical extends lesson_page { 32 33 protected $type = lesson_page::TYPE_QUESTION; 34 protected $typeidstring = 'numerical'; 35 protected $typeid = LESSON_PAGE_NUMERICAL; 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 = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id)); 53 $data = new stdClass; 54 $data->id = $PAGE->cm->id; 55 $data->pageid = $this->properties->id; 56 if (isset($USER->modattempts[$this->lesson->id])) { 57 $data->answer = s($attempt->useranswer); 58 } 59 $mform->set_data($data); 60 return $mform->display(); 61 } 62 public function check_answer() { 63 global $CFG; 64 $result = parent::check_answer(); 65 66 $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents())); 67 $data = $mform->get_data(); 68 require_sesskey(); 69 70 // set defaults 71 $result->response = ''; 72 $result->newpageid = 0; 73 74 if (isset($data->answer)) { 75 // just doing default PARAM_RAW, not doing PARAM_INT because it could be a float 76 $result->useranswer = (float)$data->answer; 77 } else { 78 $result->noanswer = true; 79 return $result; 80 } 81 $result->studentanswer = $result->userresponse = $result->useranswer; 82 $answers = $this->get_answers(); 83 foreach ($answers as $answer) { 84 if (strpos($answer->answer, ':')) { 85 // there's a pairs of values 86 list($min, $max) = explode(':', $answer->answer); 87 $minimum = (float) $min; 88 $maximum = (float) $max; 89 } else { 90 // there's only one value 91 $minimum = (float) $answer->answer; 92 $maximum = $minimum; 93 } 94 if (($result->useranswer >= $minimum) && ($result->useranswer <= $maximum)) { 95 $result->newpageid = $answer->jumpto; 96 $result->response = trim($answer->response); 97 if ($this->lesson->jumpto_is_correct($this->properties->id, $result->newpageid)) { 98 $result->correctanswer = true; 99 } 100 if ($this->lesson->custom) { 101 if ($answer->score > 0) { 102 $result->correctanswer = true; 103 } else { 104 $result->correctanswer = false; 105 } 106 } 107 $result->answerid = $answer->id; 108 return $result; 109 } 110 } 111 return $result; 112 } 113 114 public function display_answers(html_table $table) { 115 $answers = $this->get_answers(); 116 $options = new stdClass; 117 $options->noclean = true; 118 $options->para = false; 119 $i = 1; 120 foreach ($answers as $answer) { 121 $answer = parent::rewrite_answers_urls($answer, false); 122 $cells = array(); 123 if ($this->lesson->custom && $answer->score > 0) { 124 // if the score is > 0, then it is correct 125 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; 126 } else if ($this->lesson->custom) { 127 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n"; 128 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) { 129 // underline correct answers 130 $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n"; 131 } else { 132 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; 133 } 134 $cells[] = format_text($answer->answer, $answer->answerformat, $options); 135 $table->data[] = new html_table_row($cells); 136 137 $cells = array(); 138 $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>"; 139 $cells[] = format_text($answer->response, $answer->responseformat, $options); 140 $table->data[] = new html_table_row($cells); 141 142 $cells = array(); 143 $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>'; 144 $cells[] = $answer->score; 145 $table->data[] = new html_table_row($cells); 146 147 $cells = array(); 148 $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>'; 149 $cells[] = $this->get_jump_name($answer->jumpto); 150 $table->data[] = new html_table_row($cells); 151 if ($i === 1){ 152 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;'; 153 } 154 $i++; 155 } 156 return $table; 157 } 158 public function stats(array &$pagestats, $tries) { 159 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt 160 $temp = $tries[$this->lesson->maxattempts - 1]; 161 } else { 162 // else, user attempted the question less than the max, so grab the last one 163 $temp = end($tries); 164 } 165 if (isset($pagestats[$temp->pageid][$temp->useranswer])) { 166 $pagestats[$temp->pageid][$temp->useranswer]++; 167 } else { 168 $pagestats[$temp->pageid][$temp->useranswer] = 1; 169 } 170 if (isset($pagestats[$temp->pageid]["total"])) { 171 $pagestats[$temp->pageid]["total"]++; 172 } else { 173 $pagestats[$temp->pageid]["total"] = 1; 174 } 175 return true; 176 } 177 178 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) { 179 $answers = $this->get_answers(); 180 $formattextdefoptions = new stdClass; 181 $formattextdefoptions->para = false; //I'll use it widely in this page 182 foreach ($answers as $answer) { 183 if ($useranswer == null && $i == 0) { 184 // I have the $i == 0 because it is easier to blast through it all at once. 185 if (isset($pagestats[$this->properties->id])) { 186 $stats = $pagestats[$this->properties->id]; 187 $total = $stats["total"]; 188 unset($stats["total"]); 189 foreach ($stats as $valentered => $ntimes) { 190 $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($valentered).'" />'; 191 $percent = $ntimes / $total * 100; 192 $percent = round($percent, 2); 193 $percent .= "% ".get_string("enteredthis", "lesson"); 194 $answerdata->answers[] = array($data, $percent); 195 } 196 } else { 197 $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " "); 198 } 199 $i++; 200 } else if ($useranswer != null && ($answer->id == $useranswer->answerid || ($answer == end($answers) && empty($answerdata)))) { 201 // get in here when what the user entered is not one of the answers 202 $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($useranswer->useranswer).'">'; 203 if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) { 204 $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100; 205 $percent = round($percent, 2); 206 $percent .= "% ".get_string("enteredthis", "lesson"); 207 } else { 208 $percent = get_string("nooneenteredthis", "lesson"); 209 } 210 $answerdata->answers[] = array($data, $percent); 211 212 if ($answer->id == $useranswer->answerid) { 213 if ($answer->response == null) { 214 if ($useranswer->correct) { 215 $answerdata->response = get_string("thatsthecorrectanswer", "lesson"); 216 } else { 217 $answerdata->response = get_string("thatsthewronganswer", "lesson"); 218 } 219 } else { 220 $answerdata->response = $answer->response; 221 } 222 if ($this->lesson->custom) { 223 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; 224 } elseif ($useranswer->correct) { 225 $answerdata->score = get_string("receivedcredit", "lesson"); 226 } else { 227 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 228 } 229 } else { 230 $answerdata->response = get_string("thatsthewronganswer", "lesson"); 231 if ($this->lesson->custom) { 232 $answerdata->score = get_string("pointsearned", "lesson").": 0"; 233 } else { 234 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 235 } 236 } 237 } 238 $answerpage->answerdata = $answerdata; 239 } 240 return $answerpage; 241 } 242 } 243 244 class lesson_add_page_form_numerical extends lesson_add_page_form_base { 245 246 public $qtype = 'numerical'; 247 public $qtypestring = 'numerical'; 248 249 public function custom_definition() { 250 for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) { 251 $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1)); 252 $this->add_answer($i, null, ($i < 1)); 253 $this->add_response($i); 254 $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE)); 255 $this->add_score($i, null, ($i===0)?1:0); 256 } 257 } 258 } 259 260 class lesson_display_answer_form_numerical extends moodleform { 261 262 public function definition() { 263 global $USER, $OUTPUT; 264 $mform = $this->_form; 265 $contents = $this->_customdata['contents']; 266 267 $mform->addElement('header', 'pageheader'); 268 269 $mform->addElement('html', $OUTPUT->container($contents, 'contents')); 270 271 $hasattempt = false; 272 $attrs = array('size'=>'50', 'maxlength'=>'200'); 273 if (isset($this->_customdata['lessonid'])) { 274 $lessonid = $this->_customdata['lessonid']; 275 if (isset($USER->modattempts[$lessonid]->useranswer)) { 276 $attrs['readonly'] = 'readonly'; 277 $hasattempt = true; 278 } 279 } 280 $options = new stdClass; 281 $options->para = false; 282 $options->noclean = true; 283 284 $mform->addElement('hidden', 'id'); 285 $mform->setType('id', PARAM_INT); 286 287 $mform->addElement('hidden', 'pageid'); 288 $mform->setType('pageid', PARAM_INT); 289 290 $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs); 291 $mform->setType('answer', PARAM_FLOAT); 292 293 if ($hasattempt) { 294 $this->add_action_buttons(null, get_string("nextpage", "lesson")); 295 } else { 296 $this->add_action_buttons(null, get_string("submit", "lesson")); 297 } 298 } 299 300 }
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 |