MDL-27386 lesson fix mform->score not saving (wip)
[moodle.git] / mod / lesson / essay.php
CommitLineData
86342d63 1<?php
0a4abb73
SH
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
f15eb92c 18/**
19 * Provides the interface for grading essay questions
20 *
cc3dbaaa
PS
21 * @package mod
22 * @subpackage lesson
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f15eb92c 25 **/
26
0a4abb73
SH
27require_once('../../config.php');
28require_once($CFG->dirroot.'/mod/lesson/locallib.php');
29require_once($CFG->dirroot.'/mod/lesson/essay_form.php');
30require_once($CFG->libdir.'/eventslib.php');
31
32$id = required_param('id', PARAM_INT); // Course Module ID
33$mode = optional_param('mode', 'display', PARAM_ALPHA);
b3daa926
PS
34
35$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);;
36$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
37$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
38
0a4abb73
SH
39require_login($course, false, $cm);
40$context = get_context_instance(CONTEXT_MODULE, $cm->id);
41require_capability('mod/lesson:edit', $context);
42
a6855934 43$url = new moodle_url('/mod/lesson/essay.php', array('id'=>$id));
0a4abb73
SH
44if ($mode !== 'display') {
45 $url->param('mode', $mode);
46}
47$PAGE->set_url($url);
86342d63 48
b38f0baa
AB
49$attemptid = optional_param('attemptid', PARAM_INT);
50$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid));
51$answer = $DB->get_record('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $attempt->pageid));
52$scoreoptions = array();
53if ($lesson->custom) {
54 $i = $answer->score;
55 while ($i >= 0) {
56 $scoreoptions[$i] = (string)$i;
57 $i--;
58 }
59} else {
60 $scoreoptions[0] = get_string('nocredit', 'lesson');
61 $scoreoptions[1] = get_string('credit', 'lesson');
62}
63
f15eb92c 64/// Handle any preprocessing before header is printed - based on $mode
0a4abb73
SH
65switch ($mode) {
66 case 'grade':
67 // Grading form - get the necessary data
68 require_sesskey();
69
70 $attemptid = required_param('attemptid', PARAM_INT);
71
72 if (!$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid))) {
73 print_error('cannotfindattempt', 'lesson');
74 }
0a4abb73
SH
75 if (!$user = $DB->get_record('user', array('id' => $attempt->userid))) {
76 print_error('cannotfinduser', 'lesson');
77 }
b38f0baa 78 if (!$answer = $DB->record_exists('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $attempt->pageid))) {
0a4abb73
SH
79 print_error('cannotfindanswer', 'lesson');
80 }
81 break;
82
83 case 'update':
84 require_sesskey();
b38f0baa
AB
85 $attemptid = required_param('attemptid', PARAM_INT);
86 $attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid), '*', MUST_EXIST);
87 $user = $DB->get_record('user', array('id' => $attempt->userid), '*', MUST_EXIST);
88 $mform = new essay_grading_form(null, array('scoreoptions'=>$scoreoptions, 'user'=>$user));
89 if ($mform->is_cancelled()) {
90 redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id");
91 }
0a4abb73 92 if ($form = $mform->get_data()) {
b38f0baa 93 error_log(print_r($form,true));
0a4abb73 94
0a4abb73
SH
95 if (!$grades = $DB->get_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$attempt->userid), 'completed', '*', $attempt->retry, 1)) {
96 print_error('cannotfindgrade', 'lesson');
f15eb92c 97 }
86342d63 98
0a4abb73
SH
99 $essayinfo = new stdClass;
100 $essayinfo = unserialize($attempt->useranswer);
f15eb92c 101
0a4abb73 102 $essayinfo->graded = 1;
f672e3e9 103 $essayinfo->score = $score;
0a4abb73
SH
104 $essayinfo->response = clean_param($form->response, PARAM_RAW);
105 $essayinfo->sent = 0;
106 if (!$lesson->custom && $essayinfo->score == 1) {
107 $attempt->correct = 1;
108 } else {
109 $attempt->correct = 0;
110 }
f15eb92c 111
0a4abb73 112 $attempt->useranswer = serialize($essayinfo);
f15eb92c 113
0a4abb73 114 $DB->update_record('lesson_attempts', $attempt);
f15eb92c 115
0a4abb73
SH
116 // Get grade information
117 $grade = current($grades);
118 $gradeinfo = lesson_grade($lesson, $attempt->retry, $attempt->userid);
86342d63 119
0a4abb73 120 // Set and update
39790bd8 121 $updategrade = new stdClass();
0a4abb73
SH
122 $updategrade->id = $grade->id;
123 $updategrade->grade = $gradeinfo->grade;
124 $DB->update_record('lesson_grades', $updategrade);
125 // Log it
126 add_to_log($course->id, 'lesson', 'update grade', "essay.php?id=$cm->id", $lesson->name, $cm->id);
86342d63 127
0a4abb73 128 $lesson->add_message(get_string('changessaved'), 'notifysuccess');
86342d63 129
0a4abb73
SH
130 // update central gradebook
131 lesson_update_grades($lesson, $grade->userid);
92bcca38 132
a6855934 133 redirect(new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id)));
0a4abb73
SH
134 } else {
135 print_error('invalidformdata');
136 }
137 break;
138 case 'email':
139 // Sending an email(s) to a single user or all
140 require_sesskey();
92bcca38 141
0a4abb73
SH
142 // Get our users (could be singular)
143 if ($userid = optional_param('userid', 0, PARAM_INT)) {
144 $queryadd = " AND userid = ?";
145 if (! $users = $DB->get_records('user', array('id' => $userid))) {
146 print_error('cannotfinduser', 'lesson');
f15eb92c 147 }
0a4abb73
SH
148 } else {
149 $queryadd = '';
150 $params = array ("lessonid" => $lesson->id);
e07c51c4
EL
151 // Need to use inner view to avoid distinct + text
152 if (!$users = $DB->get_records_sql("
153 SELECT u.*
154 FROM {user} u
155 JOIN (
156 SELECT DISTINCT u.id
157 FROM {user} u,
158 {lesson_attempts} a
159 WHERE a.lessonid = :lessonid and
160 u.id = a.userid) ui ON (u.id = ui.id)
161 ORDER BY u.lastname", $params)) {
0a4abb73 162 print_error('cannotfinduser', 'lesson');
f15eb92c 163 }
0a4abb73 164 }
f15eb92c 165
0a4abb73
SH
166 $pages = $lesson->load_all_pages();
167 foreach ($pages as $key=>$page) {
168 if ($page->qtype !== LESSON_PAGE_ESSAY) {
169 unset($pages[$key]);
f15eb92c 170 }
0a4abb73
SH
171 }
172
173 // Get only the attempts that are in response to essay questions
174 list($usql, $params) = $DB->get_in_or_equal(array_keys($pages));
175 if (!empty($queryadd)) {
176 $params[] = $userid;
177 }
178 if (!$attempts = $DB->get_records_select('lesson_attempts', "pageid $usql".$queryadd, $params)) {
179 print_error('nooneansweredthisquestion', 'lesson');
180 }
181 // Get the answers
182 list($answerUsql, $parameters) = $DB->get_in_or_equal(array_keys($pages));
183 array_unshift($parameters, $lesson->id);
184 if (!$answers = $DB->get_records_select('lesson_answers', "lessonid = ? AND pageid $answerUsql", $parameters, '', 'pageid, score')) {
185 print_error('cannotfindanswer', 'lesson');
186 }
187 $options = new stdClass;
188 $options->noclean = true;
189
190 foreach ($attempts as $attempt) {
191 $essayinfo = unserialize($attempt->useranswer);
192 if ($essayinfo->graded && !$essayinfo->sent) {
193 // Holds values for the essayemailsubject string for the email message
194 $a = new stdClass;
195
196 // Set the grade
197 $grades = $DB->get_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$attempt->userid), 'completed', '*', $attempt->retry, 1);
198 $grade = current($grades);
199 $a->newgrade = $grade->grade;
200
201 // Set the points
202 if ($lesson->custom) {
203 $a->earned = $essayinfo->score;
204 $a->outof = $answers[$attempt->pageid]->score;
205 } else {
206 $a->earned = $essayinfo->score;
207 $a->outof = 1;
208 }
f15eb92c 209
0a4abb73
SH
210 // Set rest of the message values
211 $currentpage = $lesson->load_page($attempt->pageid);
01c37ef1 212 $a->question = format_text($currentpage->contents, $currentpage->contentsformat, $options);
0a4abb73
SH
213 $a->response = s($essayinfo->answer);
214 $a->comment = s($essayinfo->response);
215
216 // Fetch message HTML and plain text formats
217 $message = get_string('essayemailmessage2', 'lesson', $a);
218 $plaintext = format_text_email($message, FORMAT_HTML);
219
220 // Subject
221 $subject = get_string('essayemailsubject', 'lesson', format_string($pages[$attempt->pageid]->title,true));
222
39790bd8 223 $eventdata = new stdClass();
0a4abb73
SH
224 $eventdata->modulename = 'lesson';
225 $eventdata->userfrom = $USER;
226 $eventdata->userto = $users[$attempt->userid];
227 $eventdata->subject = $subject;
228 $eventdata->fullmessage = $plaintext;
229 $eventdata->fullmessageformat = FORMAT_PLAIN;
230 $eventdata->fullmessagehtml = $message;
231 $eventdata->smallmessage = '';
232
233 // Required for messaging framework
234 $eventdata->component = 'mod_lesson';
235 $eventdata->name = 'graded_essay';
2f67a9b3 236
0a4abb73
SH
237 message_send($eventdata);
238 $essayinfo->sent = 1;
239 $attempt->useranswer = serialize($essayinfo);
240 $DB->update_record('lesson_attempts', $attempt);
241 // Log it
242 add_to_log($course->id, 'lesson', 'update email essay grade', "essay.php?id=$cm->id", format_string($pages[$attempt->pageid]->title,true).': '.fullname($users[$attempt->userid]), $cm->id);
646fc290 243 }
0a4abb73
SH
244 }
245 $lesson->add_message(get_string('emailsuccess', 'lesson'), 'notifysuccess');
a6855934 246 redirect(new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id)));
0a4abb73
SH
247 break;
248 case 'display': // Default view - get the necessary data
249 default:
250 // Get lesson pages that are essay
251 $pages = $lesson->load_all_pages();
252 foreach ($pages as $key=>$page) {
253 if ($page->qtype !== LESSON_PAGE_ESSAY) {
254 unset($pages[$key]);
f15eb92c 255 }
0a4abb73
SH
256 }
257 if (count($pages) > 0) {
258 $params = array ("lessonid" => $lesson->id, "qtype" => LESSON_PAGE_ESSAY);
259 // Get only the attempts that are in response to essay questions
260 list($usql, $parameters) = $DB->get_in_or_equal(array_keys($pages));
261 if ($essayattempts = $DB->get_records_select('lesson_attempts', 'pageid '.$usql, $parameters)) {
262 // Get all the users who have taken this lesson, order by their last name
eae1948c 263 $ufields = user_picture::fields('u');
98da6021 264 if (!empty($cm->groupingid)) {
0a4abb73 265 $params["groupinid"] = $cm->groupingid;
eae1948c 266 $sql = "SELECT DISTINCT $ufields
0a4abb73
SH
267 FROM {lesson_attempts} a
268 INNER JOIN {user} u ON u.id = a.userid
269 INNER JOIN {groups_members} gm ON gm.userid = u.id
270 INNER JOIN {groupings_groups} gg ON gm.groupid = :groupinid
271 WHERE a.lessonid = :lessonid
272 ORDER BY u.lastname";
273 } else {
eae1948c 274 $sql = "SELECT DISTINCT $ufields
0a4abb73
SH
275 FROM {user} u,
276 {lesson_attempts} a
277 WHERE a.lessonid = :lessonid and
278 u.id = a.userid
279 ORDER BY u.lastname";
280 }
281 if (!$users = $DB->get_records_sql($sql, $params)) {
282 $mode = 'none'; // not displaying anything
283 $lesson->add_message(get_string('noonehasanswered', 'lesson'));
284 }
285 } else {
286 $mode = 'none'; // not displaying anything
287 $lesson->add_message(get_string('noonehasanswered', 'lesson'));
f15eb92c 288 }
0a4abb73
SH
289 } else {
290 $mode = 'none'; // not displaying anything
291 $lesson->add_message(get_string('noessayquestionsfound', 'lesson'));
292 }
293 break;
294}
295// Log it
296add_to_log($course->id, 'lesson', 'view grade', "essay.php?id=$cm->id", get_string('manualgrading', 'lesson'), $cm->id);
297
649cf95d 298$lessonoutput = $PAGE->get_renderer('mod_lesson');
448052a5 299echo $lessonoutput->header($lesson, $cm, 'essay');
0a4abb73
SH
300
301switch ($mode) {
302 case 'display':
303 // Expects $user, $essayattempts and $pages to be set already
304
305 // Group all the essays by userid
306 $studentessays = array();
307 foreach ($essayattempts as $essay) {
308 // Not very nice :) but basically
309 // this organizes the essays so we know how many
310 // times a student answered an essay per try and per page
311 $studentessays[$essay->userid][$essay->pageid][$essay->retry][] = $essay;
312 }
313
314 // Setup table
315 $table = new html_table();
316 $table->head = array(get_string('name'), get_string('essays', 'lesson'), get_string('email', 'lesson'));
16be8974 317 $table->attributes['class'] = 'standardtable generaltable';
0a4abb73
SH
318 $table->align = array('left', 'left', 'left');
319 $table->wrap = array('nowrap', 'nowrap', '');
320
321 // Cycle through all the students
322 foreach (array_keys($studentessays) as $userid) {
323 $studentname = fullname($users[$userid], true);
324 $essaylinks = array();
325
326 // Number of attempts on the lesson
327 $attempts = $DB->count_records('lesson_grades', array('userid'=>$userid, 'lessonid'=>$lesson->id));
328
329 // Go through each essay page
330 foreach ($studentessays[$userid] as $page => $tries) {
331 $count = 0;
332
333 // Go through each attempt per page
334 foreach($tries as $try) {
335 if ($count == $attempts) {
336 break; // Stop displaying essays (attempt not completed)
337 }
338 $count++;
339
340 // Make sure they didn't answer it more than the max number of attmepts
341 if (count($try) > $lesson->maxattempts) {
342 $essay = $try[$lesson->maxattempts-1];
f15eb92c 343 } else {
0a4abb73 344 $essay = end($try);
f15eb92c 345 }
9101efd3 346
0a4abb73
SH
347 // Start processing the attempt
348 $essayinfo = unserialize($essay->useranswer);
349
350 // link for each essay
a6855934 351 $url = new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id,'mode'=>'grade','attemptid'=>$essay->id,'sesskey'=>sesskey()));
9bf16314 352 $attributes = array();
0a4abb73
SH
353 // Different colors for all the states of an essay (graded, if sent, not graded)
354 if (!$essayinfo->graded) {
9bf16314 355 $attributes['class'] = "graded";
0a4abb73 356 } elseif (!$essayinfo->sent) {
9bf16314 357 $attributes['class'] = "sent";
0a4abb73 358 } else {
9bf16314 359 $attributes['class'] = "ungraded";
0a4abb73 360 }
9bf16314 361 $essaylinks[] = html_writer::link($url, userdate($essay->timeseen, get_string('strftimedatetime')).' '.format_string($pages[$essay->pageid]->title,true), $attributes);
f15eb92c 362 }
363 }
0a4abb73 364 // email link for this user
a6855934 365 $url = new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id,'mode'=>'email','userid'=>$userid,'sesskey'=>sesskey()));
57cd3739 366 $emaillink = html_writer::link($url, get_string('emailgradedessays', 'lesson'));
86342d63 367
812dbaf7 368 $table->data[] = array($OUTPUT->user_picture($users[$userid], array('courseid'=>$course->id)).$studentname, implode("<br />", $essaylinks), $emaillink);
0a4abb73 369 }
f15eb92c 370
0a4abb73 371 // email link for all users
a6855934 372 $url = new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id,'mode'=>'email','sesskey'=>sesskey()));
57cd3739 373 $emailalllink = html_writer::link($url, get_string('emailallgradedessays', 'lesson'));
86342d63 374
0a4abb73
SH
375 $table->data[] = array(' ', ' ', $emailalllink);
376
16be8974 377 echo html_writer::table($table);
0a4abb73
SH
378 break;
379 case 'grade':
380 // Grading form
381 // Expects the following to be set: $attemptid, $answer, $user, $page, $attempt
86342d63 382
0a4abb73
SH
383
384 $essayinfo = unserialize($attempt->useranswer);
b38f0baa
AB
385
386 $mform = new essay_grading_form(null, array('scoreoptions'=>$scoreoptions, 'user'=>$user));
0a4abb73
SH
387
388 $data = new stdClass;
389 $data->id = $cm->id;
390 $data->attemptid = $attemptid;
391 $data->score = $essayinfo->score;
392 $data->studentanswer = format_string($essayinfo->answer, FORMAT_MOODLE);
393 $data->response = $essayinfo->response;
394 $mform->set_data($data);
395
396 $mform->display();
397 break;
398}
399
400echo $OUTPUT->footer();