[ 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 * Essay 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 /** Essay question type */ 29 define("LESSON_PAGE_ESSAY", "10"); 30 31 class lesson_page_type_essay extends lesson_page { 32 33 protected $type = lesson_page::TYPE_QUESTION; 34 protected $typeidstring = 'essay'; 35 protected $typeid = LESSON_PAGE_ESSAY; 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 51 /** 52 * Unserialize attempt useranswer and add missing responseformat if needed 53 * for compatibility with old records. 54 * 55 * @param string $useranswer serialized object 56 * @return object 57 */ 58 static public function extract_useranswer($useranswer) { 59 $essayinfo = unserialize($useranswer); 60 if (!isset($essayinfo->responseformat)) { 61 $essayinfo->response = text_to_html($essayinfo->response, false, false); 62 $essayinfo->responseformat = FORMAT_HTML; 63 } 64 return $essayinfo; 65 } 66 67 public function display($renderer, $attempt) { 68 global $PAGE, $CFG, $USER; 69 70 $mform = new lesson_display_answer_form_essay($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id)); 71 72 $data = new stdClass; 73 $data->id = $PAGE->cm->id; 74 $data->pageid = $this->properties->id; 75 if (isset($USER->modattempts[$this->lesson->id])) { 76 $essayinfo = self::extract_useranswer($attempt->useranswer); 77 $data->answer = $essayinfo->answer; 78 } 79 $mform->set_data($data); 80 81 // Trigger an event question viewed. 82 $eventparams = array( 83 'context' => context_module::instance($PAGE->cm->id), 84 'objectid' => $this->properties->id, 85 'other' => array( 86 'pagetype' => $this->get_typestring() 87 ) 88 ); 89 90 $event = \mod_lesson\event\question_viewed::create($eventparams); 91 $event->trigger(); 92 return $mform->display(); 93 } 94 public function create_answers($properties) { 95 global $DB; 96 // now add the answers 97 $newanswer = new stdClass; 98 $newanswer->lessonid = $this->lesson->id; 99 $newanswer->pageid = $this->properties->id; 100 $newanswer->timecreated = $this->properties->timecreated; 101 102 if (isset($properties->jumpto[0])) { 103 $newanswer->jumpto = $properties->jumpto[0]; 104 } 105 if (isset($properties->score[0])) { 106 $newanswer->score = $properties->score[0]; 107 } 108 $newanswer->id = $DB->insert_record("lesson_answers", $newanswer); 109 $answers = array($newanswer->id => new lesson_page_answer($newanswer)); 110 $this->answers = $answers; 111 return $answers; 112 } 113 public function check_answer() { 114 global $PAGE, $CFG; 115 $result = parent::check_answer(); 116 $result->isessayquestion = true; 117 118 $mform = new lesson_display_answer_form_essay($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents())); 119 $data = $mform->get_data(); 120 require_sesskey(); 121 122 if (!$data) { 123 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id))); 124 } 125 126 if (is_array($data->answer)) { 127 $studentanswer = $data->answer['text']; 128 $studentanswerformat = $data->answer['format']; 129 } else { 130 $studentanswer = $data->answer; 131 $studentanswerformat = FORMAT_HTML; 132 } 133 134 if (trim($studentanswer) === '') { 135 $result->noanswer = true; 136 return $result; 137 } 138 139 $answers = $this->get_answers(); 140 foreach ($answers as $answer) { 141 $result->answerid = $answer->id; 142 $result->newpageid = $answer->jumpto; 143 } 144 145 $userresponse = new stdClass; 146 $userresponse->sent=0; 147 $userresponse->graded = 0; 148 $userresponse->score = 0; 149 $userresponse->answer = $studentanswer; 150 $userresponse->answerformat = $studentanswerformat; 151 $userresponse->response = ''; 152 $userresponse->responseformat = FORMAT_HTML; 153 $result->userresponse = serialize($userresponse); 154 $result->studentanswerformat = $studentanswerformat; 155 $result->studentanswer = $studentanswer; 156 return $result; 157 } 158 public function update($properties, $context = null, $maxbytes = null) { 159 global $DB, $PAGE; 160 $answers = $this->get_answers(); 161 $properties->id = $this->properties->id; 162 $properties->lessonid = $this->lesson->id; 163 $properties->timemodified = time(); 164 $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); 165 $DB->update_record("lesson_pages", $properties); 166 167 // Trigger an event: page updated. 168 \mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger(); 169 170 if (!array_key_exists(0, $this->answers)) { 171 $this->answers[0] = new stdClass; 172 $this->answers[0]->lessonid = $this->lesson->id; 173 $this->answers[0]->pageid = $this->id; 174 $this->answers[0]->timecreated = $this->timecreated; 175 } 176 if (isset($properties->jumpto[0])) { 177 $this->answers[0]->jumpto = $properties->jumpto[0]; 178 } 179 if (isset($properties->score[0])) { 180 $this->answers[0]->score = $properties->score[0]; 181 } 182 if (!isset($this->answers[0]->id)) { 183 $this->answers[0]->id = $DB->insert_record("lesson_answers", $this->answers[0]); 184 } else { 185 $DB->update_record("lesson_answers", $this->answers[0]->properties()); 186 } 187 188 return true; 189 } 190 public function stats(array &$pagestats, $tries) { 191 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt 192 $temp = $tries[$this->lesson->maxattempts - 1]; 193 } else { 194 // else, user attempted the question less than the max, so grab the last one 195 $temp = end($tries); 196 } 197 $essayinfo = self::extract_useranswer($temp->useranswer); 198 if ($essayinfo->graded) { 199 if (isset($pagestats[$temp->pageid])) { 200 $essaystats = $pagestats[$temp->pageid]; 201 $essaystats->totalscore += $essayinfo->score; 202 $essaystats->total++; 203 $pagestats[$temp->pageid] = $essaystats; 204 } else { 205 $essaystats = new stdClass(); 206 $essaystats->totalscore = $essayinfo->score; 207 $essaystats->total = 1; 208 $pagestats[$temp->pageid] = $essaystats; 209 } 210 } 211 return true; 212 } 213 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) { 214 $formattextdefoptions = new stdClass(); 215 $formattextdefoptions->noclean = true; 216 $formattextdefoptions->para = false; 217 $formattextdefoptions->context = $answerpage->context; 218 $answers = $this->get_answers(); 219 220 foreach ($answers as $answer) { 221 if ($useranswer != null) { 222 $essayinfo = self::extract_useranswer($useranswer->useranswer); 223 if ($essayinfo->response == null) { 224 $answerdata->response = get_string("nocommentyet", "lesson"); 225 } else { 226 $essayinfo->response = file_rewrite_pluginfile_urls($essayinfo->response, 'pluginfile.php', 227 $answerpage->context->id, 'mod_lesson', 'essay_responses', $useranswer->id); 228 $answerdata->response = format_text($essayinfo->response, $essayinfo->responseformat, $formattextdefoptions); 229 } 230 if (isset($pagestats[$this->properties->id])) { 231 $percent = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total * 100; 232 $percent = round($percent, 2); 233 $percent = get_string("averagescore", "lesson").": ". $percent ."%"; 234 } else { 235 // dont think this should ever be reached.... 236 $percent = get_string("nooneansweredthisquestion", "lesson"); 237 } 238 if ($essayinfo->graded) { 239 if ($this->lesson->custom) { 240 $answerdata->score = get_string("pointsearned", "lesson").": ".$essayinfo->score; 241 } elseif ($essayinfo->score) { 242 $answerdata->score = get_string("receivedcredit", "lesson"); 243 } else { 244 $answerdata->score = get_string("didnotreceivecredit", "lesson"); 245 } 246 } else { 247 $answerdata->score = get_string("havenotgradedyet", "lesson"); 248 } 249 } else { 250 $essayinfo = new stdClass(); 251 $essayinfo->answer = get_string("didnotanswerquestion", "lesson"); 252 $essayinfo->answerformat = null; 253 } 254 255 if (isset($pagestats[$this->properties->id])) { 256 $avescore = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total; 257 $avescore = round($avescore, 2); 258 $avescore = get_string("averagescore", "lesson").": ". $avescore ; 259 } else { 260 // dont think this should ever be reached.... 261 $avescore = get_string("nooneansweredthisquestion", "lesson"); 262 } 263 // This is the student's answer so it should be cleaned. 264 $answerdata->answers[] = array(format_text($essayinfo->answer, $essayinfo->answerformat, 265 array('para' => true, 'context' => $answerpage->context)), $avescore); 266 $answerpage->answerdata = $answerdata; 267 } 268 return $answerpage; 269 } 270 public function is_unanswered($nretakes) { 271 global $DB, $USER; 272 if (!$DB->count_records("lesson_attempts", array('pageid'=>$this->properties->id, 'userid'=>$USER->id, 'retry'=>$nretakes))) { 273 return true; 274 } 275 return false; 276 } 277 public function requires_manual_grading() { 278 return true; 279 } 280 public function get_earnedscore($answers, $attempt) { 281 $essayinfo = self::extract_useranswer($attempt->useranswer); 282 return $essayinfo->score; 283 } 284 } 285 286 class lesson_add_page_form_essay extends lesson_add_page_form_base { 287 288 public $qtype = 'essay'; 289 public $qtypestring = 'essay'; 290 291 public function custom_definition() { 292 293 $this->add_jumpto(0); 294 $this->add_score(0, null, 1); 295 296 } 297 } 298 299 class lesson_display_answer_form_essay extends moodleform { 300 301 public function definition() { 302 global $USER, $OUTPUT; 303 $mform = $this->_form; 304 $contents = $this->_customdata['contents']; 305 306 $hasattempt = false; 307 $attrs = ''; 308 $useranswer = ''; 309 $useranswerraw = ''; 310 if (isset($this->_customdata['lessonid'])) { 311 $lessonid = $this->_customdata['lessonid']; 312 if (isset($USER->modattempts[$lessonid]->useranswer) && !empty($USER->modattempts[$lessonid]->useranswer)) { 313 $attrs = array('disabled' => 'disabled'); 314 $hasattempt = true; 315 $useranswertemp = lesson_page_type_essay::extract_useranswer($USER->modattempts[$lessonid]->useranswer); 316 $useranswer = htmlspecialchars_decode($useranswertemp->answer, ENT_QUOTES); 317 $useranswerraw = $useranswertemp->answer; 318 } 319 } 320 321 // Disable shortforms. 322 $mform->setDisableShortforms(); 323 324 $mform->addElement('header', 'pageheader'); 325 326 $mform->addElement('html', $OUTPUT->container($contents, 'contents')); 327 328 $options = new stdClass; 329 $options->para = false; 330 $options->noclean = true; 331 332 $mform->addElement('hidden', 'id'); 333 $mform->setType('id', PARAM_INT); 334 335 $mform->addElement('hidden', 'pageid'); 336 $mform->setType('pageid', PARAM_INT); 337 338 if ($hasattempt) { 339 $mform->addElement('hidden', 'answer', $useranswerraw); 340 $mform->setType('answer', PARAM_RAW); 341 $mform->addElement('html', $OUTPUT->container(get_string('youranswer', 'lesson'), 'youranswer')); 342 $mform->addElement('html', $OUTPUT->container($useranswer, 'reviewessay')); 343 $this->add_action_buttons(null, get_string("nextpage", "lesson")); 344 } else { 345 $mform->addElement('editor', 'answer', get_string('youranswer', 'lesson'), null, null); 346 $mform->setType('answer', PARAM_RAW); 347 $this->add_action_buttons(null, get_string("submit", "lesson")); 348 } 349 } 350 }
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 |