MDL-61391 forum: Improve semantics for permalinks
[moodle.git] / mod / lesson / pagetypes / essay.php
blob992200d475454836379905fd7aecdd4cef2f7cdc
1 <?php
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.
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/>.
18 /**
19 * Essay
21 * @package mod_lesson
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
26 defined('MOODLE_INTERNAL') || die();
28 /** Essay question type */
29 define("LESSON_PAGE_ESSAY", "10");
31 class lesson_page_type_essay extends lesson_page {
33 protected $type = lesson_page::TYPE_QUESTION;
34 protected $typeidstring = 'essay';
35 protected $typeid = LESSON_PAGE_ESSAY;
36 protected $string = null;
38 public function get_typeid() {
39 return $this->typeid;
41 public function get_typestring() {
42 if ($this->string===null) {
43 $this->string = get_string($this->typeidstring, 'lesson');
45 return $this->string;
47 public function get_idstring() {
48 return $this->typeidstring;
51 /**
52 * Unserialize attempt useranswer and add missing responseformat if needed
53 * for compatibility with old records.
55 * @param string $useranswer serialized object
56 * @return object
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;
64 return $essayinfo;
67 public function display($renderer, $attempt) {
68 global $PAGE, $CFG, $USER;
70 $mform = new lesson_display_answer_form_essay($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
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;
79 $mform->set_data($data);
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()
90 $event = \mod_lesson\event\question_viewed::create($eventparams);
91 $event->trigger();
92 return $mform->display();
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;
102 if (isset($properties->jumpto[0])) {
103 $newanswer->jumpto = $properties->jumpto[0];
105 if (isset($properties->score[0])) {
106 $newanswer->score = $properties->score[0];
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;
113 public function check_answer() {
114 global $PAGE, $CFG;
115 $result = parent::check_answer();
116 $result->isessayquestion = true;
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();
122 if (!$data) {
123 $result->inmediatejump = true;
124 $result->newpageid = $this->properties->id;
125 return $result;
128 if (is_array($data->answer)) {
129 $studentanswer = $data->answer['text'];
130 $studentanswerformat = $data->answer['format'];
131 } else {
132 $studentanswer = $data->answer;
133 $studentanswerformat = FORMAT_HTML;
136 if (trim($studentanswer) === '') {
137 $result->noanswer = true;
138 return $result;
141 $answers = $this->get_answers();
142 foreach ($answers as $answer) {
143 $result->answerid = $answer->id;
144 $result->newpageid = $answer->jumpto;
147 $userresponse = new stdClass;
148 $userresponse->sent=0;
149 $userresponse->graded = 0;
150 $userresponse->score = 0;
151 $userresponse->answer = $studentanswer;
152 $userresponse->answerformat = $studentanswerformat;
153 $userresponse->response = '';
154 $userresponse->responseformat = FORMAT_HTML;
155 $result->userresponse = serialize($userresponse);
156 $result->studentanswerformat = $studentanswerformat;
157 $result->studentanswer = $studentanswer;
158 return $result;
160 public function update($properties, $context = null, $maxbytes = null) {
161 global $DB, $PAGE;
162 $answers = $this->get_answers();
163 $properties->id = $this->properties->id;
164 $properties->lessonid = $this->lesson->id;
165 $properties->timemodified = time();
166 $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);
167 $DB->update_record("lesson_pages", $properties);
169 // Trigger an event: page updated.
170 \mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger();
172 if (!array_key_exists(0, $this->answers)) {
173 $this->answers[0] = new stdClass;
174 $this->answers[0]->lessonid = $this->lesson->id;
175 $this->answers[0]->pageid = $this->id;
176 $this->answers[0]->timecreated = $this->timecreated;
178 if (isset($properties->jumpto[0])) {
179 $this->answers[0]->jumpto = $properties->jumpto[0];
181 if (isset($properties->score[0])) {
182 $this->answers[0]->score = $properties->score[0];
184 if (!isset($this->answers[0]->id)) {
185 $this->answers[0]->id = $DB->insert_record("lesson_answers", $this->answers[0]);
186 } else {
187 $DB->update_record("lesson_answers", $this->answers[0]->properties());
190 return true;
192 public function stats(array &$pagestats, $tries) {
193 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
194 $temp = $tries[$this->lesson->maxattempts - 1];
195 } else {
196 // else, user attempted the question less than the max, so grab the last one
197 $temp = end($tries);
199 $essayinfo = self::extract_useranswer($temp->useranswer);
200 if ($essayinfo->graded) {
201 if (isset($pagestats[$temp->pageid])) {
202 $essaystats = $pagestats[$temp->pageid];
203 $essaystats->totalscore += $essayinfo->score;
204 $essaystats->total++;
205 $pagestats[$temp->pageid] = $essaystats;
206 } else {
207 $essaystats = new stdClass();
208 $essaystats->totalscore = $essayinfo->score;
209 $essaystats->total = 1;
210 $pagestats[$temp->pageid] = $essaystats;
213 return true;
215 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
216 $formattextdefoptions = new stdClass();
217 $formattextdefoptions->noclean = true;
218 $formattextdefoptions->para = false;
219 $formattextdefoptions->context = $answerpage->context;
220 $answers = $this->get_answers();
222 foreach ($answers as $answer) {
223 if ($useranswer != null) {
224 $essayinfo = self::extract_useranswer($useranswer->useranswer);
225 if ($essayinfo->response == null) {
226 $answerdata->response = get_string("nocommentyet", "lesson");
227 } else {
228 $essayinfo->response = file_rewrite_pluginfile_urls($essayinfo->response, 'pluginfile.php',
229 $answerpage->context->id, 'mod_lesson', 'essay_responses', $useranswer->id);
230 $answerdata->response = format_text($essayinfo->response, $essayinfo->responseformat, $formattextdefoptions);
232 if (isset($pagestats[$this->properties->id])) {
233 $percent = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total * 100;
234 $percent = round($percent, 2);
235 $percent = get_string("averagescore", "lesson").": ". $percent ."%";
236 } else {
237 // dont think this should ever be reached....
238 $percent = get_string("nooneansweredthisquestion", "lesson");
240 if ($essayinfo->graded) {
241 if ($this->lesson->custom) {
242 $answerdata->score = get_string("pointsearned", "lesson").": ".$essayinfo->score;
243 } elseif ($essayinfo->score) {
244 $answerdata->score = get_string("receivedcredit", "lesson");
245 } else {
246 $answerdata->score = get_string("didnotreceivecredit", "lesson");
248 } else {
249 $answerdata->score = get_string("havenotgradedyet", "lesson");
251 } else {
252 $essayinfo = new stdClass();
253 $essayinfo->answer = get_string("didnotanswerquestion", "lesson");
254 $essayinfo->answerformat = null;
257 if (isset($pagestats[$this->properties->id])) {
258 $avescore = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total;
259 $avescore = round($avescore, 2);
260 $avescore = get_string("averagescore", "lesson").": ". $avescore ;
261 } else {
262 // dont think this should ever be reached....
263 $avescore = get_string("nooneansweredthisquestion", "lesson");
265 // This is the student's answer so it should be cleaned.
266 $answerdata->answers[] = array(format_text($essayinfo->answer, $essayinfo->answerformat,
267 array('para' => true, 'context' => $answerpage->context)), $avescore);
268 $answerpage->answerdata = $answerdata;
270 return $answerpage;
272 public function is_unanswered($nretakes) {
273 global $DB, $USER;
274 if (!$DB->count_records("lesson_attempts", array('pageid'=>$this->properties->id, 'userid'=>$USER->id, 'retry'=>$nretakes))) {
275 return true;
277 return false;
279 public function requires_manual_grading() {
280 return true;
282 public function get_earnedscore($answers, $attempt) {
283 $essayinfo = self::extract_useranswer($attempt->useranswer);
284 return $essayinfo->score;
288 class lesson_add_page_form_essay extends lesson_add_page_form_base {
290 public $qtype = 'essay';
291 public $qtypestring = 'essay';
293 public function custom_definition() {
295 $this->add_jumpto(0);
296 $this->add_score(0, null, 1);
301 class lesson_display_answer_form_essay extends moodleform {
303 public function definition() {
304 global $USER, $OUTPUT;
305 $mform = $this->_form;
306 $contents = $this->_customdata['contents'];
308 $hasattempt = false;
309 $attrs = '';
310 $useranswer = '';
311 $useranswerraw = '';
312 if (isset($this->_customdata['lessonid'])) {
313 $lessonid = $this->_customdata['lessonid'];
314 if (isset($USER->modattempts[$lessonid]->useranswer) && !empty($USER->modattempts[$lessonid]->useranswer)) {
315 $attrs = array('disabled' => 'disabled');
316 $hasattempt = true;
317 $useranswertemp = lesson_page_type_essay::extract_useranswer($USER->modattempts[$lessonid]->useranswer);
318 $useranswer = htmlspecialchars_decode($useranswertemp->answer, ENT_QUOTES);
319 $useranswerraw = $useranswertemp->answer;
323 // Disable shortforms.
324 $mform->setDisableShortforms();
326 $mform->addElement('header', 'pageheader');
328 $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
330 $options = new stdClass;
331 $options->para = false;
332 $options->noclean = true;
334 $mform->addElement('hidden', 'id');
335 $mform->setType('id', PARAM_INT);
337 $mform->addElement('hidden', 'pageid');
338 $mform->setType('pageid', PARAM_INT);
340 if ($hasattempt) {
341 $mform->addElement('hidden', 'answer', $useranswerraw);
342 $mform->setType('answer', PARAM_RAW);
343 $mform->addElement('html', $OUTPUT->container(get_string('youranswer', 'lesson'), 'youranswer'));
344 $mform->addElement('html', $OUTPUT->container($useranswer, 'reviewessay'));
345 $this->add_action_buttons(null, get_string("nextpage", "lesson"));
346 } else {
347 $mform->addElement('editor', 'answer', get_string('youranswer', 'lesson'), null, null);
348 $mform->setType('answer', PARAM_RAW);
349 $this->add_action_buttons(null, get_string("submit", "lesson"));