[ Index ] |
PHP Cross Reference of Unnamed Project |
[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 * Short answer 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 /** Short answer question type */ 29 define("LESSON_PAGE_SHORTANSWER", "1"); 30 31 class lesson_page_type_shortanswer extends lesson_page { 32 33 protected $type = lesson_page::TYPE_QUESTION; 34 protected $typeidstring = 'shortanswer'; 35 protected $typeid = LESSON_PAGE_SHORTANSWER; 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 61 // Trigger an event question viewed. 62 $eventparams = array( 63 'context' => context_module::instance($PAGE->cm->id), 64 'objectid' => $this->properties->id, 65 'other' => array( 66 'pagetype' => $this->get_typestring() 67 ) 68 ); 69 70 $event = \mod_lesson\event\question_viewed::create($eventparams); 71 $event->trigger(); 72 return $mform->display(); 73 } 74 public function check_answer() { 75 global $CFG; 76 $result = parent::check_answer(); 77 78 $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents())); 79 $data = $mform->get_data(); 80 require_sesskey(); 81 82 $studentanswer = trim($data->answer); 83 if ($studentanswer === '') { 84 $result->noanswer = true; 85 return $result; 86 } 87 88 $i=0; 89 $answers = $this->get_answers(); 90 foreach ($answers as $answer) { 91 $answer = parent::rewrite_answers_urls($answer, false); 92 $i++; 93 // Applying PARAM_TEXT as it is applied to the answer submitted by the user. 94 $expectedanswer = clean_param($answer->answer, PARAM_TEXT); 95 $ismatch = false; 96 $markit = false; 97 $useregexp = ($this->qoption); 98 99 if ($useregexp) { //we are using 'normal analysis', which ignores case 100 $ignorecase = ''; 101 if (substr($expectedanswer, -2) == '/i') { 102 $expectedanswer = substr($expectedanswer, 0, -2); 103 $ignorecase = 'i'; 104 } 105 } else { 106 $expectedanswer = str_replace('*', '#####', $expectedanswer); 107 $expectedanswer = preg_quote($expectedanswer, '/'); 108 $expectedanswer = str_replace('#####', '.*', $expectedanswer); 109 } 110 // see if user typed in any of the correct answers 111 if ((!$this->lesson->custom && $this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) or ($this->lesson->custom && $answer->score > 0) ) { 112 if (!$useregexp) { // we are using 'normal analysis', which ignores case 113 if (preg_match('/^'.$expectedanswer.'$/i',$studentanswer)) { 114 $ismatch = true; 115 } 116 } else { 117 if (preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer)) { 118 $ismatch = true; 119 } 120 } 121 if ($ismatch == true) { 122 $result->correctanswer = true; 123 } 124 } else { 125 if (!$useregexp) { //we are using 'normal analysis' 126 // see if user typed in any of the wrong answers; don't worry about case 127 if (preg_match('/^'.$expectedanswer.'$/i',$studentanswer)) { 128 $ismatch = true; 129 } 130 } else { // we are using regular expressions analysis 131 $startcode = substr($expectedanswer,0,2); 132 switch ($startcode){ 133 //1- check for absence of required string in $studentanswer (coded by initial '--') 134 case "--": 135 $expectedanswer = substr($expectedanswer,2); 136 if (!preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer)) { 137 $ismatch = true; 138 } 139 break; 140 //2- check for code for marking wrong strings (coded by initial '++') 141 case "++": 142 $expectedanswer=substr($expectedanswer,2); 143 $markit = true; 144 //check for one or several matches 145 if (preg_match_all('/'.$expectedanswer.'/'.$ignorecase,$studentanswer, $matches)) { 146 $ismatch = true; 147 $nb = count($matches[0]); 148 $original = array(); 149 $marked = array(); 150 $fontStart = '<span class="incorrect matches">'; 151 $fontEnd = '</span>'; 152 for ($i = 0; $i < $nb; $i++) { 153 array_push($original,$matches[0][$i]); 154 array_push($marked,$fontStart.$matches[0][$i].$fontEnd); 155 } 156 $studentanswer = str_replace($original, $marked, $studentanswer); 157 } 158 break; 159 //3- check for wrong answers belonging neither to -- nor to ++ categories 160 default: 161 if (preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer, $matches)) { 162 $ismatch = true; 163 } 164 break; 165 } 166 $result->correctanswer = false; 167 } 168 } 169 if ($ismatch) { 170 $result->newpageid = $answer->jumpto; 171 $options = new stdClass(); 172 $options->para = false; 173 $result->response = format_text($answer->response, $answer->responseformat, $options); 174 $result->answerid = $answer->id; 175 break; // quit answer analysis immediately after a match has been found 176 } 177 } 178 $result->userresponse = $studentanswer; 179 //clean student answer as it goes to output. 180 $result->studentanswer = s($studentanswer); 181 return $result; 182 } 183 184 public function option_description_string() { 185 if ($this->properties->qoption) { 186 return " - ".get_string("casesensitive", "lesson"); 187 } 188 return parent::option_description_string(); 189 } 190 191 public function display_answers(html_table $table) { 192 $answers = $this->get_answers(); 193 $options = new stdClass; 194 $options->noclean = true; 195 $options->para = false; 196 $i = 1; 197 foreach ($answers as $answer) { 198 $answer = parent::rewrite_answers_urls($answer, false); 199 $cells = array(); 200 if ($this->lesson->custom && $answer->score > 0) { 201 // if the score is > 0, then it is correct 202 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; 203 } else if ($this->lesson->custom) { 204 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n"; 205 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) { 206 // underline correct answers 207 $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n"; 208 } else { 209 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; 210 } 211 $cells[] = format_text($answer->answer, $answer->answerformat, $options); 212 $table->data[] = new html_table_row($cells); 213 214 $cells = array(); 215 $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>"; 216 $cells[] = format_text($answer->response, $answer->responseformat, $options); 217 $table->data[] = new html_table_row($cells); 218 219 $cells = array(); 220 $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>'; 221 $cells[] = $answer->score; 222 $table->data[] = new html_table_row($cells); 223 224 $cells = array(); 225 $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>'; 226 $cells[] = $this->get_jump_name($answer->jumpto); 227 $table->data[] = new html_table_row($cells); 228 if ($i === 1){ 229 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;'; 230 } 231 $i++; 232 } 233 return $table; 234 } 235 public function stats(array &$pagestats, $tries) { 236 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt 237 $temp = $tries[$this->lesson->maxattempts - 1]; 238 } else { 239 // else, user attempted the question less than the max, so grab the last one 240 $temp = end($tries); 241 } 242 if (isset($pagestats[$temp->pageid][$temp->useranswer])) { 243 $pagestats[$temp->pageid][$temp->useranswer]++; 244 } else { 245 $pagestats[$temp->pageid][$temp->useranswer] = 1; 246 } 247 if (isset($pagestats[$temp->pageid]["total"])) { 248 $pagestats[$temp->pageid]["total"]++; 249 } else { 250 $pagestats[$temp->pageid]["total"] = 1; 251 } 252 return true; 253 } 254 255 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) { 256 global $PAGE; 257 258 $answers = $this->get_answers(); 259 $formattextdefoptions = new stdClass; 260 $formattextdefoptions->para = false; //I'll use it widely in this page 261 foreach ($answers as $answer) { 262 $answer = parent::rewrite_answers_urls($answer, false); 263 if ($useranswer == null && $i == 0) { 264 // I have the $i == 0 because it is easier to blast through it all at once. 265 if (isset($pagestats[$this->properties->id])) { 266 $stats = $pagestats[$this->properties->id]; 267 $total = $stats["total"]; 268 unset($stats["total"]); 269 foreach ($stats as $valentered => $ntimes) { 270 $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($valentered).'" />'; 271 $percent = $ntimes / $total * 100; 272 $percent = round($percent, 2); 273 $percent .= "% ".get_string("enteredthis", "lesson"); 274 $answerdata->answers[] = array($data, $percent); 275 } 276 } else { 277 $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " "); 278 } 279 $i++; 280 } else if ($useranswer != null && ($answer->id == $useranswer->answerid || $answer == end($answers))) { 281 // get in here when what the user entered is not one of the answers 282 $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($useranswer->useranswer).'">'; 283 if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) { 284 $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100; 285 $percent = round($percent, 2); 286 $percent .= "% ".get_string("enteredthis", "lesson"); 287 } else { 288 $percent = get_string("nooneenteredthis", "lesson"); 289 } 290 $answerdata->answers[] = array($data, $percent); 291 292 if ($answer->id == $useranswer->answerid) { 293 if ($answer->response == null) { 294 if ($useranswer->correct) { 295 $answerdata->response = get_string("thatsthecorrectanswer", "lesson"); 296 } else { 297 $answerdata->response = get_string("thatsthewronganswer", "lesson"); 298 } 299 } else { 300 $answerdata->response = $answer->response; 301 } 302 if ($this->lesson->custom) { 303 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; 304 } elseif ($useranswer->correct) { 305 $answerdata->score = get_string("receivedcredit", "lesson"); 306 } else { 307 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 308 } 309 // We have found the correct answer, do not process any more answers. 310 $answerpage->answerdata = $answerdata; 311 break; 312 } else { 313 $answerdata->response = get_string("thatsthewronganswer", "lesson"); 314 if ($this->lesson->custom) { 315 $answerdata->score = get_string("pointsearned", "lesson").": 0"; 316 } else { 317 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 318 } 319 } 320 } 321 $answerpage->answerdata = $answerdata; 322 } 323 return $answerpage; 324 } 325 } 326 327 328 class lesson_add_page_form_shortanswer extends lesson_add_page_form_base { 329 public $qtype = 'shortanswer'; 330 public $qtypestring = 'shortanswer'; 331 protected $answerformat = ''; 332 protected $responseformat = LESSON_ANSWER_HTML; 333 334 public function custom_definition() { 335 336 $this->_form->addElement('checkbox', 'qoption', get_string('options', 'lesson'), get_string('casesensitive', 'lesson')); //oh my, this is a regex option! 337 $this->_form->setDefault('qoption', 0); 338 $this->_form->addHelpButton('qoption', 'casesensitive', 'lesson'); 339 340 for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) { 341 $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1)); 342 // Only first answer is required. 343 $this->add_answer($i, null, ($i < 1)); 344 $this->add_response($i); 345 $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE)); 346 $this->add_score($i, null, ($i===0)?1:0); 347 } 348 } 349 } 350 351 class lesson_display_answer_form_shortanswer extends moodleform { 352 353 public function definition() { 354 global $OUTPUT, $USER; 355 $mform = $this->_form; 356 $contents = $this->_customdata['contents']; 357 358 $hasattempt = false; 359 $attrs = array('size'=>'50', 'maxlength'=>'200'); 360 if (isset($this->_customdata['lessonid'])) { 361 $lessonid = $this->_customdata['lessonid']; 362 if (isset($USER->modattempts[$lessonid]->useranswer)) { 363 $attrs['readonly'] = 'readonly'; 364 $hasattempt = true; 365 } 366 } 367 368 $placeholder = false; 369 if (preg_match('/_____+/', $contents, $matches)) { 370 $placeholder = $matches[0]; 371 $contentsparts = explode( $placeholder, $contents, 2); 372 $attrs['size'] = round(strlen($placeholder) * 1.1); 373 } 374 375 // Disable shortforms. 376 $mform->setDisableShortforms(); 377 378 $mform->addElement('header', 'pageheader'); 379 $mform->addElement('hidden', 'id'); 380 $mform->setType('id', PARAM_INT); 381 382 $mform->addElement('hidden', 'pageid'); 383 $mform->setType('pageid', PARAM_INT); 384 385 if ($placeholder) { 386 $contentsgroup = array(); 387 $contentsgroup[] = $mform->createElement('static', '', '', $contentsparts[0]); 388 $contentsgroup[] = $mform->createElement('text', 'answer', '', $attrs); 389 $contentsgroup[] = $mform->createElement('static', '', '', $contentsparts[1]); 390 $mform->addGroup($contentsgroup, '', '', '', false); 391 } else { 392 $mform->addElement('html', $OUTPUT->container($contents, 'contents')); 393 $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs); 394 395 } 396 $mform->setType('answer', PARAM_TEXT); 397 398 if ($hasattempt) { 399 $this->add_action_buttons(null, get_string("nextpage", "lesson")); 400 } else { 401 $this->add_action_buttons(null, get_string("submit", "lesson")); 402 } 403 } 404 405 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |