another missing global
[moodle.git] / mod / lesson / pagetypes / essay.php
CommitLineData
0a4abb73
SH
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 *
cc3dbaaa
PS
21 * @package mod
22 * @subpackage lesson
23 * @copyright 2009 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0a4abb73
SH
25 **/
26
1e7f8ea2
PS
27defined('MOODLE_INTERNAL') || die();
28
29/** Essay question type */
0a4abb73
SH
30define("LESSON_PAGE_ESSAY", "10");
31
32class lesson_page_type_essay extends lesson_page {
33
34 protected $type = lesson_page::TYPE_QUESTION;
35 protected $typeidstring = 'essay';
36 protected $typeid = LESSON_PAGE_ESSAY;
37 protected $string = null;
38
39 public function get_typeid() {
40 return $this->typeid;
41 }
42 public function get_typestring() {
43 if ($this->string===null) {
44 $this->string = get_string($this->typeidstring, 'lesson');
45 }
46 return $this->string;
47 }
48 public function get_idstring() {
49 return $this->typeidstring;
50 }
51 public function display($renderer, $attempt) {
4f96fe55 52 global $PAGE, $CFG, $USER;
0a4abb73
SH
53
54 $mform = new lesson_display_answer_form_essay($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
55
56 $data = new stdClass;
57 $data->id = $PAGE->cm->id;
58 $data->pageid = $this->properties->id;
59 if (isset($USER->modattempts[$this->lesson->id])) {
60 $essayinfo = unserialize($attempt->useranswer);
61 $data->answer = array('text'=>$essayinfo->answer, 'format'=>FORMAT_HTML);
62 }
63 $mform->set_data($data);
64 return $mform->display();
65 }
66 public function create_answers($properties) {
67 global $DB;
68 // now add the answers
69 $newanswer = new stdClass;
70 $newanswer->lessonid = $this->lesson->id;
71 $newanswer->pageid = $this->properties->id;
72 $newanswer->timecreated = $this->properties->timecreated;
73
74 if (isset($properties->jumpto[0])) {
75 $newanswer->jumpto = $properties->jumpto[0];
76 }
77 if (isset($properties->score[0])) {
78 $newanswer->score = $properties->score[0];
79 }
80 $newanswer->id = $DB->insert_record("lesson_answers", $newanswer);
81 $answers = array($newanswer->id => new lesson_page_answer($newanswer));
82 $this->answers = $answers;
83 return $answers;
84 }
85 public function check_answer() {
86 global $PAGE, $CFG;
87 $result = parent::check_answer();
88 $result->isessayquestion = true;
89
90 $mform = new lesson_display_answer_form_essay($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
91 $data = $mform->get_data();
92 require_sesskey();
93
94 if (!$data) {
a6855934 95 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id)));
0a4abb73
SH
96 }
97
98 $studentanswer = $data->answer['text'];
99 if (trim($studentanswer) === '') {
100 $result->noanswer = true;
101 return $result;
102 }
2f67a9b3 103
0a4abb73
SH
104 $answers = $this->get_answers();
105 foreach ($answers as $answer) {
106 $result->answerid = $answer->id;
107 $result->newpageid = $answer->jumpto;
108 }
109
110 $userresponse = new stdClass;
111 $userresponse->sent=0;
112 $userresponse->graded = 0;
113 $userresponse->score = 0;
114 $userresponse->answer = $studentanswer;
115 $userresponse->response = "";
116 $result->userresponse = serialize($userresponse);
117
118 $result->studentanswer = s($studentanswer);
119 return $result;
120 }
121 public function update($properties) {
122 global $DB, $PAGE;
123 $answers = $this->get_answers();
124 $properties->id = $this->properties->id;
125 $properties->lessonid = $this->lesson->id;
64f93798 126 $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);
0a4abb73
SH
127 $DB->update_record("lesson_pages", $properties);
128
129 if (!array_key_exists(0, $this->answers)) {
130 $this->answers[0] = new stdClass;
131 $this->answers[0]->lessonid = $this->lesson->id;
132 $this->answers[0]->pageid = $this->id;
133 $this->answers[0]->timecreated = $this->timecreated;
134 }
135 if (isset($properties->jumpto[0])) {
136 $this->answers[0]->jumpto = $properties->jumpto[0];
137 }
138 if (isset($properties->score[0])) {
139 $this->answers[0]->score = $properties->score[0];
140 }
141 if (!isset($this->answers[0]->id)) {
142 $this->answers[0]->id = $DB->insert_record("lesson_answers", $this->answers[0]);
143 } else {
144 $DB->update_record("lesson_answers", $this->answers[0]->properties());
145 }
146
147 return true;
148 }
149 public function stats(array &$pagestats, $tries) {
150 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
151 $temp = $tries[$this->lesson->maxattempts - 1];
152 } else {
153 // else, user attempted the question less than the max, so grab the last one
154 $temp = end($tries);
155 }
156 $essayinfo = unserialize($temp->useranswer);
157 if ($essayinfo->graded) {
158 if (isset($pagestats[$temp->pageid])) {
159 $essaystats = $pagestats[$temp->pageid];
160 $essaystats->totalscore += $essayinfo->score;
161 $essaystats->total++;
162 $pagestats[$temp->pageid] = $essaystats;
163 } else {
164 $essaystats->totalscore = $essayinfo->score;
165 $essaystats->total = 1;
166 $pagestats[$temp->pageid] = $essaystats;
167 }
168 }
169 return true;
170 }
171 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
172 $answers = $this->get_answers();
173 $formattextdefoptions = new stdClass;
174 $formattextdefoptions->para = false; //I'll use it widely in this page
175 foreach ($answers as $answer) {
176 if ($useranswer != NULL) {
177 $essayinfo = unserialize($useranswer->useranswer);
178 if ($essayinfo->response == NULL) {
179 $answerdata->response = get_string("nocommentyet", "lesson");
180 } else {
181 $answerdata->response = s($essayinfo->response);
182 }
183 if (isset($pagestats[$this->properties->id])) {
184 $percent = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total * 100;
185 $percent = round($percent, 2);
186 $percent = get_string("averagescore", "lesson").": ". $percent ."%";
187 } else {
188 // dont think this should ever be reached....
189 $percent = get_string("nooneansweredthisquestion", "lesson");
190 }
191 if ($essayinfo->graded) {
192 if ($this->lesson->custom) {
193 $answerdata->score = get_string("pointsearned", "lesson").": ".$essayinfo->score;
194 } elseif ($essayinfo->score) {
195 $answerdata->score = get_string("receivedcredit", "lesson");
196 } else {
197 $answerdata->score = get_string("didnotreceivecredit", "lesson");
198 }
199 } else {
200 $answerdata->score = get_string("havenotgradedyet", "lesson");
201 }
202 } else {
203 $essayinfo->answer = get_string("didnotanswerquestion", "lesson");
204 }
205
206 if (isset($pagestats[$this->properties->id])) {
207 $avescore = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total;
208 $avescore = round($avescore, 2);
209 $avescore = get_string("averagescore", "lesson").": ". $avescore ;
210 } else {
211 // dont think this should ever be reached....
212 $avescore = get_string("nooneansweredthisquestion", "lesson");
213 }
214 $answerdata->answers[] = array(s($essayinfo->answer), $avescore);
215 $answerpage->answerdata = $answerdata;
216 }
217 return $answerpage;
218 }
219 public function is_unanswered($nretakes) {
220 global $DB, $USER;
221 if (!$DB->count_records("lesson_attempts", array('pageid'=>$thispage->id, 'userid'=>$USER->id, 'retry'=>$nretakes))) {
222 return true;
223 }
224 return false;
225 }
226 public function requires_manual_grading() {
227 return true;
228 }
229 public function get_earnedscore($answers, $attempt) {
230 $essayinfo = unserialize($attempt->useranswer);
231 return $essayinfo->score;
232 }
233}
234
235class lesson_add_page_form_essay extends lesson_add_page_form_base {
236
237 public $qtype = 'essay';
238 public $qtypestring = 'essay';
239
240 public function custom_definition() {
241
242 $this->add_jumpto(0);
243 $this->add_score(0, null, 1);
244
245 }
246}
247
248class lesson_display_answer_form_essay extends moodleform {
249
250 public function definition() {
251 global $USER, $OUTPUT;
252 $mform = $this->_form;
253 $contents = $this->_customdata['contents'];
254
255 $mform->addElement('header', 'pageheader', $OUTPUT->box($contents, 'contents'));
256
257 $options = new stdClass;
258 $options->para = false;
259 $options->noclean = true;
260
261 $mform->addElement('hidden', 'id');
262 $mform->setType('id', PARAM_INT);
263
264 $mform->addElement('hidden', 'pageid');
265 $mform->setType('pageid', PARAM_INT);
266
267 $mform->addElement('editor', 'answer', get_string('youranswer', 'lesson'), null, null);
b8f8bbc8 268 $mform->setType('answer', PARAM_RAW);
0a4abb73
SH
269
270 $this->add_action_buttons(null, get_string("pleaseenteryouranswerinthebox", "lesson"));
271 }
272
273}