mod-assignment MDL-21106 Changed itemid for online assignment from assignment->id...
[moodle.git] / mod / lesson / essay.php
CommitLineData
86342d63 1<?php
f15eb92c 2/**
3 * Provides the interface for grading essay questions
4 *
f15eb92c 5 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
6 * @package lesson
7 **/
8
9 require_once('../../config.php');
10 require_once('locallib.php');
11 require_once('lib.php');
3b120e46 12 require_once($CFG->libdir.'/eventslib.php');
f15eb92c 13
fb992d71 14 $id = required_param('id', PARAM_INT); // Course Module ID
15 $mode = optional_param('mode', 'display', PARAM_ALPHA);
f15eb92c 16
17 list($cm, $course, $lesson) = lesson_get_basics($id);
86342d63 18
f15eb92c 19 require_login($course->id, false, $cm);
c6949936 20
21 $url = new moodle_url($CFG->wwwroot.'/mod/lesson/essay.php', array('id'=>$id));
22 if ($mode !== 'display') {
23 $url->param('mode', $mode);
24 }
25 $PAGE->set_url($url);
26 $PAGE->navbar->add(get_string('manualgrading','lesson'));
27
f15eb92c 28 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
86342d63 29
d927bb55 30 require_capability('mod/lesson:edit', $context);
86342d63 31
f15eb92c 32/// Handle any preprocessing before header is printed - based on $mode
33 switch ($mode) {
34 case 'display': // Default view - get the necessary data
35 // Get lesson pages that are essay
646fc290 36 $params = array ("lessonid" => $lesson->id, "qtype" => LESSON_ESSAY);
37 if ($pages = $DB->get_records_select('lesson_pages', "lessonid = :lessonid AND qtype = :qtype", $params)) {
f15eb92c 38 // Get only the attempts that are in response to essay questions
646fc290 39 list($usql, $parameters) = $DB->get_in_or_equal(array_keys($pages));
40 if ($essayattempts = $DB->get_records_select('lesson_attempts', 'pageid $usql', $parameters)) {
f15eb92c 41 // Get all the users who have taken this lesson, order by their last name
0ffa2c95 42 if (!empty($CFG->enablegroupings) && !empty($cm->groupingid)) {
e57bc529 43 $params["groupinid"] = $cm->groupingid;
0ffa2c95 44 $sql = "SELECT DISTINCT u.*
86342d63 45 FROM {lesson_attempts} a
646fc290 46 INNER JOIN {user} u ON u.id = a.userid
47 INNER JOIN {groups_members} gm ON gm.userid = u.id
48 INNER JOIN {groupings_groups} gg ON gm.groupid = :groupinid
49 WHERE a.lessonid = :lessonid
0ffa2c95 50 ORDER BY u.lastname";
51 } else {
52 $sql = "SELECT u.*
646fc290 53 FROM {user} u,
54 {lesson_attempts} a
55 WHERE a.lessonid = :lessonid and
0ffa2c95 56 u.id = a.userid
57 ORDER BY u.lastname";
58 }
e57bc529 59 if (!$users = $DB->get_records_sql($sql, $params)) {
0ffa2c95 60 $mode = 'none'; // not displaying anything
61 lesson_set_message(get_string('noonehasanswered', 'lesson'));
f15eb92c 62 }
63 } else {
64 $mode = 'none'; // not displaying anything
65 lesson_set_message(get_string('noonehasanswered', 'lesson'));
66 }
67 } else {
68 $mode = 'none'; // not displaying anything
69 lesson_set_message(get_string('noessayquestionsfound', 'lesson'));
70 }
71 break;
72 case 'grade': // Grading form - get the necessary data
dfc133c0 73 require_sesskey();
86342d63 74
f15eb92c 75 $attemptid = required_param('attemptid', PARAM_INT);
76
646fc290 77 if (!$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid))) {
86f93345 78 print_error('cannotfindattempt', 'lesson');
f15eb92c 79 }
646fc290 80 if (!$page = $DB->get_record('lesson_pages', array('id' => $attempt->pageid))) {
86f93345 81 print_error('cannotfindpages', 'lesson');
f15eb92c 82 }
646fc290 83 if (!$user = $DB->get_record('user', array('id' => $attempt->userid))) {
86f93345 84 print_error('cannotfinduser', 'lesson');
f15eb92c 85 }
646fc290 86 if (!$answer = $DB->get_record('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page->id))) {
86f93345 87 print_error('cannotfindanswer', 'lesson');
f15eb92c 88 }
89 break;
90 case 'update':
294ce987 91 if (confirm_sesskey() and $form = data_submitted()) {
92ebcabe 92 if (optional_param('cancel', 0, PARAM_RAW)) {
f15eb92c 93 redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id");
94 }
86342d63 95
f15eb92c 96 $attemptid = required_param('attemptid', PARAM_INT);
86342d63 97
646fc290 98 if (!$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid))) {
86f93345 99 print_error('cannotfindattempt', 'lesson');
f15eb92c 100 }
646fc290 101 $params = array ("lessonid" => $lesson->id, "userid" => $attempt->userid);
102 if (!$grades = $DB->get_records_select('lesson_grades', "lessonid = :lessonid and userid = :userid", $params, 'completed', '*', $attempt->retry, 1)) {
86f93345 103 print_error('cannotfindgrade', 'lesson');
f15eb92c 104 }
105
106 $essayinfo = new stdClass;
107 $essayinfo = unserialize($attempt->useranswer);
108
109 $essayinfo->graded = 1;
110 $essayinfo->score = clean_param($form->score, PARAM_INT);
294ce987 111 $essayinfo->response = clean_param($form->response, PARAM_RAW);
f15eb92c 112 $essayinfo->sent = 0;
113 if (!$lesson->custom && $essayinfo->score == 1) {
114 $attempt->correct = 1;
115 } else {
116 $attempt->correct = 0;
117 }
118
294ce987 119 $attempt->useranswer = serialize($essayinfo);
f15eb92c 120
a8c31db2 121 $DB->update_record('lesson_attempts', $attempt);
86342d63 122
f15eb92c 123 // Get grade information
124 $grade = current($grades);
125 $gradeinfo = lesson_grade($lesson, $attempt->retry, $attempt->userid);
86342d63 126
f15eb92c 127 // Set and update
128 $updategrade->id = $grade->id;
129 $updategrade->grade = $gradeinfo->grade;
a8c31db2 130 $DB->update_record('lesson_grades', $updategrade);
131 // Log it
132 add_to_log($course->id, 'lesson', 'update grade', "essay.php?id=$cm->id", $lesson->name, $cm->id);
86342d63 133
a8c31db2 134 lesson_set_message(get_string('changessaved'), 'notifysuccess');
92bcca38 135
136 // update central gradebook
137 lesson_update_grades($lesson, $grade->userid);
138
260a56b1 139 redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id");
f15eb92c 140 } else {
86f93345 141 print_error('invalidformdata');
f15eb92c 142 }
143 break;
144 case 'email': // Sending an email(s) to a single user or all
dfc133c0 145 require_sesskey();
86342d63 146
f15eb92c 147 // Get our users (could be singular)
148 if ($userid = optional_param('userid', 0, PARAM_INT)) {
646fc290 149 $queryadd = " AND userid = :userid";
150 if (! $users = $DB->get_records('user', array('id' => $userid))) {
86f93345 151 print_error('cannotfinduser', 'lesson');
f15eb92c 152 }
153 } else {
154 $queryadd = '';
646fc290 155 $params = array ("lessonid" => $lesson->id);
156 if (!$users = $DB->get_records_sql("SELECT u.*
157 FROM {user} u,
158 {lesson_attempts} a
159 WHERE a.lessonid = :lessonid and
f15eb92c 160 u.id = a.userid
646fc290 161 ORDER BY u.lastname", $params)) {
86f93345 162 print_error('cannotfinduser', 'lesson');
f15eb92c 163 }
164 }
165
166 // Get lesson pages that are essay
646fc290 167 $params = array ("lessonid" => $lesson->id, "qtype" => LESSON_ESSAY);
168 if (!$pages = $DB->get_records_select('lesson_pages', "lessonid = :lessonid AND qtype = :qtype", $params)) {
86f93345 169 print_error('cannotfindpages', 'lesson');
f15eb92c 170 }
171
172 // Get only the attempts that are in response to essay questions
646fc290 173 list($usql, $params) = $DB->get_in_or_equal(array_keys($pages));
174 if (isset($queryadd) && $queryadd!='') {
175 $params["userid"] = $userid;
176 }
177 if (!$attempts = $DB->get_records_select('lesson_attempts', "pageid $usql".$queryadd, $params)) {
771dc7b2 178 print_error('nooneansweredthisquestion', 'lesson');
f15eb92c 179 }
180 // Get the answers
646fc290 181 list($answerUsql, $parameters) = $DB->get_in_or_equal(array_keys($pages));
182 $parameters["lessonid"] = $lesson->id;
183 if (!$answers = $DB->get_records_select('lesson_answers', "lessonid = :lessonid AND pageid $answerUsql", $parameters, '', 'pageid, score')) {
771dc7b2 184 print_error('cannotfindanswer', 'lesson');
f15eb92c 185 }
186 $options = new stdClass;
187 $options->noclean = true;
86342d63 188
f15eb92c 189 foreach ($attempts as $attempt) {
190 $essayinfo = unserialize($attempt->useranswer);
260a56b1 191 if ($essayinfo->graded and !$essayinfo->sent) {
192 // Holds values for the essayemailsubject string for the email message
193 $a = new stdClass;
86342d63 194
260a56b1 195 // Set the grade
646fc290 196 $params = array ("lessonid" => $lesson->id, "userid" => $attempt->userid);
197 $grades = $DB->get_records_select('lesson_grades', "lessonid = :lessonid and userid = :userid", $params, 'completed', '*', $attempt->retry, 1);
260a56b1 198 $grade = current($grades);
199 $a->newgrade = $grade->grade;
86342d63 200
260a56b1 201 // Set the points
f15eb92c 202 if ($lesson->custom) {
260a56b1 203 $a->earned = $essayinfo->score;
204 $a->outof = $answers[$attempt->pageid]->score;
f15eb92c 205 } else {
260a56b1 206 $a->earned = $essayinfo->score;
207 $a->outof = 1;
f15eb92c 208 }
9101efd3 209
260a56b1 210 // Set rest of the message values
211 $a->question = format_text($pages[$attempt->pageid]->contents, FORMAT_MOODLE, $options);
294ce987 212 $a->response = s($essayinfo->answer);
107470cb 213 $a->comment = s($essayinfo->response);
9101efd3 214
260a56b1 215 // Fetch message HTML and plain text formats
9101efd3 216 $message = get_string('essayemailmessage2', 'lesson', $a);
f15eb92c 217 $plaintxt = format_text_email($message, FORMAT_HTML);
218
260a56b1 219 // Subject
220 $subject = get_string('essayemailsubject', 'lesson', format_string($pages[$attempt->pageid]->title,true));
86342d63 221
3b120e46 222 $eventdata = new object();
223 $eventdata->modulename = 'lesson';
224 $eventdata->userfrom = $USER;
225 $eventdata->userto = $users[$attempt->userid];
226 $eventdata->subject = $subject;
227 $eventdata->fullmessage = $plaintext;
228 $eventdata->fullmessageformat = FORMAT_PLAIN;
229 $eventdata->fullmessagehtml = $message;
230 $eventdata->smallmessage = '';
7c7d3afa
PS
231 message_send($eventdata);
232 $essayinfo->sent = 1;
233 $attempt->useranswer = serialize($essayinfo);
234 $DB->update_record('lesson_attempts', $attempt);
235 // Log it
236 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);
f15eb92c 237 }
238 }
260a56b1 239 lesson_set_message(get_string('emailsuccess', 'lesson'), 'notifysuccess');
240 redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id");
f15eb92c 241 break;
242 }
86342d63 243
f15eb92c 244 // Log it
245 add_to_log($course->id, 'lesson', 'view grade', "essay.php?id=$cm->id", get_string('manualgrading', 'lesson'), $cm->id);
86342d63 246
f15eb92c 247 lesson_print_header($cm, $course, $lesson, 'essay');
86342d63 248
f15eb92c 249 switch ($mode) {
250 case 'display':
251 // Expects $user, $essayattempts and $pages to be set already
86342d63 252
f15eb92c 253 // Group all the essays by userid
254 $studentessays = array();
255 foreach ($essayattempts as $essay) {
256 // Not very nice :) but basically
86342d63 257 // this organizes the essays so we know how many
f15eb92c 258 // times a student answered an essay per try and per page
86342d63 259 $studentessays[$essay->userid][$essay->pageid][$essay->retry][] = $essay;
f15eb92c 260 }
86342d63 261
f15eb92c 262 // Setup table
3ab269ed 263 $table = new html_table();
9101efd3 264 $table->head = array(get_string('name'), get_string('essays', 'lesson'), get_string('email', 'lesson'));
f15eb92c 265 $table->align = array('left', 'left', 'left');
266 $table->wrap = array('nowrap', 'nowrap', 'nowrap');
267
268 // Get the student ids of the users who have answered the essay question
269 $userids = array_keys($studentessays);
270
271 // Cycle through all the students
272 foreach ($userids as $userid) {
273 $studentname = fullname($users[$userid], true);
274 $essaylinks = array();
275
276 // Number of attempts on the lesson
318e3745 277 $attempts = $DB->count_records('lesson_grades', array('userid'=>$userid, 'lessonid'=>$lesson->id));
f15eb92c 278
279 // Go through each essay page
280 foreach ($studentessays[$userid] as $page => $tries) {
281 $count = 0;
282
283 // Go through each attempt per page
284 foreach($tries as $try) {
285 if ($count == $attempts) {
286 break; // Stop displaying essays (attempt not completed)
287 }
288 $count++;
289
290 // Make sure they didn't answer it more than the max number of attmepts
291 if (count($try) > $lesson->maxattempts) {
292 $essay = $try[$lesson->maxattempts-1];
293 } else {
294 $essay = end($try);
295 }
86342d63 296
f15eb92c 297 // Start processing the attempt
298 $essayinfo = unserialize($essay->useranswer);
86342d63 299
f15eb92c 300 // Different colors for all the states of an essay (graded, if sent, not graded)
301 if (!$essayinfo->graded) {
302 $class = ' class="graded"';
303 } elseif (!$essayinfo->sent) {
304 $class = ' class="sent"';
305 } else {
306 $class = ' class="ungraded"';
307 }
308 // link for each essay
309 $essaylinks[] = "<a$class href=\"$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id&amp;mode=grade&amp;attemptid=$essay->id&amp;sesskey=".sesskey().'">'.userdate($essay->timeseen, get_string('strftimedatetime')).' '.format_string($pages[$essay->pageid]->title,true).'</a>';
310 }
311 }
312 // email link for this user
313 $emaillink = "<a href=\"$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id&amp;mode=email&amp;userid=$userid&amp;sesskey=".sesskey().'">'.get_string('emailgradedessays', 'lesson').'</a>';
314
3ab269ed 315 $table->data[] = array($OUTPUT->user_picture(moodle_user_picture::make($users[$userid], $course->id)).$studentname, implode("<br />\n", $essaylinks), $emaillink);
f15eb92c 316 }
317 // email link for all users
318 $emailalllink = "<a href=\"$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id&amp;mode=email&amp;sesskey=".sesskey().'">'.get_string('emailallgradedessays', 'lesson').'</a>';
319
320 $table->data[] = array(' ', ' ', $emailalllink);
86342d63 321
3ab269ed 322 echo $OUTPUT->table($table);
f15eb92c 323 break;
324 case 'grade':
325 // Grading form
326 // Expects the following to be set: $attemptid, $answer, $user, $page, $attempt
327
328 echo '<div class="grade">
b7dc2256 329 <form id="essaygrade" method="post" action="'.$CFG->wwwroot.'/mod/lesson/essay.php">
f15eb92c 330 <input type="hidden" name="id" value="'.$cm->id.'" />
331 <input type="hidden" name="mode" value="update" />
332 <input type="hidden" name="attemptid" value="'.$attemptid.'" />
86342d63 333 <input type="hidden" name="sesskey" value="'.sesskey().'" />';
f15eb92c 334
335 // All tables will have these settings
3ab269ed 336 $originaltable = new html_table();
337 $originaltable->align = array('left');
338 $originaltable->wrap = array();
339 $originaltable->width = '50%';
340 $originaltable->size = array('100%');
341 $originaltable->add_class('generaltable gradetable');
f15eb92c 342
343 // Print the question
3ab269ed 344 $table = clone($originaltable);
f15eb92c 345 $table->head = array(get_string('question', 'lesson'));
346 $options = new stdClass;
347 $options->noclean = true;
348 $table->data[] = array(format_text($page->contents, FORMAT_MOODLE, $options));
349
3ab269ed 350 echo $OUTPUT->table($table);
86342d63 351
f15eb92c 352 // Now the user's answer
353 $essayinfo = unserialize($attempt->useranswer);
86342d63 354
3ab269ed 355 $table = clone($originaltable);
356 $table = new html_table();
f15eb92c 357 $table->head = array(get_string('studentresponse', 'lesson', fullname($user, true)));
294ce987 358 $table->data[] = array(s($essayinfo->answer));
f15eb92c 359
3ab269ed 360 echo $OUTPUT->table($table);
f15eb92c 361
362 // Now a response box and grade drop-down for grader
3ab269ed 363 $table = clone($originaltable);
f15eb92c 364 $table->head = array(get_string('comments', 'lesson'));
107470cb 365 $table->data[] = array(print_textarea(false, 15, 60, 0, 0, 'response', $essayinfo->response, $course->id, true));
f15eb92c 366 $options = array();
367 if ($lesson->custom) {
368 for ($i=$answer->score; $i>=0; $i--) {
369 $options[$i] = $i;
370 }
371 } else {
372 $options[0] = get_string('nocredit', 'lesson');
373 $options[1] = get_string('credit', 'lesson');
374 }
955ee33d 375 $select = html_select::make($options, 'score', $essayinfo->score, false);
376 $select->nothingvalue = '';
377 $table->data[] = array(get_string('essayscore', 'lesson').': '.$OUTPUT->select($select));
f15eb92c 378
3ab269ed 379 echo $OUTPUT->table($table);
f15eb92c 380 echo '<div class="buttons">
381 <input type="submit" name="cancel" value="'.get_string('cancel').'" />
382 <input type="submit" value="'.get_string('savechanges').'" />
383 </div>
384 </form>
385 </div>';
386 break;
387 }
86342d63 388
f7e6b862 389 echo $OUTPUT->footer();
86342d63 390