f15eb92c |
1 | <?php // $Id$ |
2 | /** |
3 | * Provides the interface for grading essay questions |
4 | * |
5 | * @version $Id$ |
6 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
7 | * @package lesson |
8 | **/ |
9 | |
10 | require_once('../../config.php'); |
11 | require_once('locallib.php'); |
12 | require_once('lib.php'); |
13 | |
14 | $id = required_param('id', PARAM_INT); // Course Module ID |
15 | $mode = optional_param('mode', 'display', PARAM_ALPHA); // for eacherview action todo use user pref |
16 | |
17 | list($cm, $course, $lesson) = lesson_get_basics($id); |
18 | |
19 | require_login($course->id, false, $cm); |
20 | |
21 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); |
22 | |
23 | require_capability('mod/lesson:edit'); |
24 | |
25 | /// Handle any preprocessing before header is printed - based on $mode |
26 | switch ($mode) { |
27 | case 'display': // Default view - get the necessary data |
28 | // Get lesson pages that are essay |
29 | if ($pages = get_records_select('lesson_pages', "lessonid = $lesson->id AND qtype = ".LESSON_ESSAY)) { |
30 | // Get only the attempts that are in response to essay questions |
31 | if ($essayattempts = get_records_select('lesson_attempts', 'pageid IN('.implode(',', array_keys($pages)).')')) { |
32 | // Get all the users who have taken this lesson, order by their last name |
33 | if (!$users = get_records_sql("SELECT DISTINCT u.* |
34 | FROM {$CFG->prefix}user u, |
35 | {$CFG->prefix}lesson_attempts a |
36 | WHERE a.lessonid = '$lesson->id' and |
37 | u.id = a.userid |
38 | ORDER BY u.lastname")) { |
39 | error('Error: could not find users'); |
40 | } |
41 | } else { |
42 | $mode = 'none'; // not displaying anything |
43 | lesson_set_message(get_string('noonehasanswered', 'lesson')); |
44 | } |
45 | } else { |
46 | $mode = 'none'; // not displaying anything |
47 | lesson_set_message(get_string('noessayquestionsfound', 'lesson')); |
48 | } |
49 | break; |
50 | case 'grade': // Grading form - get the necessary data |
51 | confirm_sesskey(); |
52 | |
53 | $attemptid = required_param('attemptid', PARAM_INT); |
54 | |
55 | if (!$attempt = get_record('lesson_attempts', 'id', $attemptid)) { |
56 | error('Error: could not find attempt'); |
57 | } |
58 | if (!$page = get_record('lesson_pages', 'id', $attempt->pageid)) { |
59 | error('Error: could not find lesson page'); |
60 | } |
61 | if (!$user = get_record('user', 'id', $attempt->userid)) { |
62 | error('Error: could not find users'); |
63 | } |
64 | if (!$answer = get_record('lesson_answers', 'lessonid', $lesson->id, 'pageid', $page->id)) { |
65 | error('Error: could not find answer'); |
66 | } |
67 | break; |
68 | case 'update': |
69 | if (confirm_sesskey() and $form = data_submitted($CFG->wwwroot.'/mod/lesson/essay.php')) { |
70 | if (optional_param('cancel', 0)) { |
71 | redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id"); |
72 | } |
73 | |
74 | $attemptid = required_param('attemptid', PARAM_INT); |
75 | |
76 | if (!$attempt = get_record('lesson_attempts', 'id', $attemptid)) { |
77 | error('Error: could not find essay'); |
78 | } |
79 | if (!$grades = get_records_select('lesson_grades', "lessonid = $lesson->id and userid = $attempt->userid", 'completed', '*', $attempt->retry, 1)) { |
80 | error('Error: could not find grades'); |
81 | } |
82 | |
83 | $essayinfo = new stdClass; |
84 | $essayinfo = unserialize($attempt->useranswer); |
85 | |
86 | $essayinfo->graded = 1; |
87 | $essayinfo->score = clean_param($form->score, PARAM_INT); |
88 | $essayinfo->response = stripslashes_safe($form->response); |
89 | $essayinfo->sent = 0; |
90 | if (!$lesson->custom && $essayinfo->score == 1) { |
91 | $attempt->correct = 1; |
92 | } else { |
93 | $attempt->correct = 0; |
94 | } |
95 | |
96 | $attempt->useranswer = addslashes(serialize($essayinfo)); |
97 | |
98 | if (!update_record('lesson_attempts', $attempt)) { |
99 | error('Could not update essay score'); |
100 | } |
101 | |
102 | // Get grade information |
103 | $grade = current($grades); |
104 | $gradeinfo = lesson_grade($lesson, $attempt->retry, $attempt->userid); |
105 | |
106 | // Set and update |
107 | $updategrade->id = $grade->id; |
108 | $updategrade->grade = $gradeinfo->grade; |
109 | if(update_record('lesson_grades', $updategrade)) { |
110 | // Log it |
111 | add_to_log($course->id, 'lesson', 'update grade', "essay.php?id=$cm->id", $lesson->name, $cm->id); |
112 | |
113 | redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id", get_string('updatesuccess', 'lesson')); |
114 | } else { |
115 | redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id", get_string('updatefailed', 'lesson')); |
116 | } |
117 | } else { |
118 | error('Something is wrong with the form data'); |
119 | } |
120 | break; |
121 | case 'email': // Sending an email(s) to a single user or all |
122 | confirm_sesskey(); |
123 | |
124 | // Get our users (could be singular) |
125 | if ($userid = optional_param('userid', 0, PARAM_INT)) { |
126 | $queryadd = " AND userid = $userid"; |
127 | if (! $users = get_records('user', 'id', $userid)) { |
128 | error('Error: could not find users'); |
129 | } |
130 | } else { |
131 | $queryadd = ''; |
132 | if (!$users = get_records_sql("SELECT DISTINCT u.* |
133 | FROM {$CFG->prefix}user u, |
134 | {$CFG->prefix}lesson_attempts a |
135 | WHERE a.lessonid = '$lesson->id' and |
136 | u.id = a.userid |
137 | ORDER BY u.lastname")) { |
138 | error('Error: could not find users'); |
139 | } |
140 | } |
141 | |
142 | // Get lesson pages that are essay |
143 | if (!$pages = get_records_select('lesson_pages', "lessonid = $lesson->id AND qtype = ".LESSON_ESSAY)) { |
144 | error('Error: could not find lesson pages'); |
145 | } |
146 | |
147 | // Get only the attempts that are in response to essay questions |
148 | $pageids = implode(',', array_keys($pages)); // all the pageids in comma seperated list |
149 | if (!$attempts = get_records_select('lesson_attempts', "pageid IN($pageids)".$queryadd)) { |
150 | error ('No one has answered essay questions yet...'); |
151 | } |
152 | // Get the answers |
153 | if (!$answers = get_records_select('lesson_answers', "lessonid = $lesson->id AND pageid IN($pageids)", '', 'pageid, score')) { |
154 | error ('Could not find answer records.'); |
155 | } |
156 | $options = new stdClass; |
157 | $options->noclean = true; |
158 | |
159 | foreach ($attempts as $attempt) { |
160 | $essayinfo = unserialize($attempt->useranswer); |
161 | if ($essayinfo->graded and !$essayinfo->sent) { |
162 | $subject = get_string('essayemailsubject', 'lesson', format_string($pages[$attempt->pageid]->title,true)); |
163 | $message = get_string('question', 'lesson').':<br>'; |
164 | $message .= format_text($pages[$attempt->pageid]->contents, FORMAT_MOODLE, $options); |
165 | $message .= '<br><br>'; |
166 | $message .= get_string('yourresponse', 'lesson').':<br>'; |
167 | $message .= format_text(stripslashes($essayinfo->answer)); |
168 | $message .= '<br><br>'; |
169 | $message .= get_string('commentswithname', 'lesson', $USER).':<br>'; |
170 | $message .= format_text(stripslashes($essayinfo->response), FORMAT_MOODLE, $options); |
171 | $message .= '<br><br>'; |
172 | $grades = get_records_select('lesson_grades', "lessonid = $lesson->id and userid = $attempt->userid", 'completed', '*', $attempt->retry, 1); |
173 | $grade = current($grades); |
174 | if ($lesson->custom) { |
175 | $points->score = $essayinfo->score; |
176 | $points->outof = $answers[$attempt->pageid]->score; |
177 | $message .= get_string('youhavereceived', 'lesson', $points); |
178 | } else { |
179 | $points->score = $essayinfo->score; |
180 | $points->outof = 1; |
181 | $message .= get_string('youhavereceived', 'lesson', $points); |
182 | } |
183 | $message .= '<br><br>'; |
184 | $message .= get_string('yourgradeisnow', 'lesson', $grade->grade).'%.'; |
185 | |
186 | $plaintxt = format_text_email($message, FORMAT_HTML); |
187 | |
188 | if(email_to_user($users[$attempt->userid], $USER, $subject, $plaintxt, $message)) { |
189 | $essayinfo->sent = 1; |
190 | $attempt->useranswer = addslashes(serialize($essayinfo)); |
191 | update_record('lesson_attempts', $attempt); |
192 | // Log it |
193 | 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); |
194 | } else { |
195 | error('Emailing Failed'); |
196 | } |
197 | } |
198 | } |
199 | redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id", get_string('emailsuccess', 'lesson')); |
200 | break; |
201 | } |
202 | |
203 | // Log it |
204 | add_to_log($course->id, 'lesson', 'view grade', "essay.php?id=$cm->id", get_string('manualgrading', 'lesson'), $cm->id); |
205 | |
206 | lesson_print_header($cm, $course, $lesson, 'essay'); |
207 | |
208 | switch ($mode) { |
209 | case 'display': |
210 | // Expects $user, $essayattempts and $pages to be set already |
211 | |
212 | // Group all the essays by userid |
213 | $studentessays = array(); |
214 | foreach ($essayattempts as $essay) { |
215 | // Not very nice :) but basically |
216 | // this organizes the essays so we know how many |
217 | // times a student answered an essay per try and per page |
218 | $studentessays[$essay->userid][$essay->pageid][$essay->retry][] = $essay; |
219 | } |
220 | |
221 | // Setup table |
222 | $table = new stdClass; |
223 | $table->head = array($course->students, get_string('essays', 'lesson'), get_string('email', 'lesson')); |
224 | $table->align = array('left', 'left', 'left'); |
225 | $table->wrap = array('nowrap', 'nowrap', 'nowrap'); |
226 | |
227 | // Get the student ids of the users who have answered the essay question |
228 | $userids = array_keys($studentessays); |
229 | |
230 | // Cycle through all the students |
231 | foreach ($userids as $userid) { |
232 | $studentname = fullname($users[$userid], true); |
233 | $essaylinks = array(); |
234 | |
235 | // Number of attempts on the lesson |
236 | $attempts = count_records('lesson_grades', 'userid', $userid, 'lessonid', $lesson->id); |
237 | |
238 | // Go through each essay page |
239 | foreach ($studentessays[$userid] as $page => $tries) { |
240 | $count = 0; |
241 | |
242 | // Go through each attempt per page |
243 | foreach($tries as $try) { |
244 | if ($count == $attempts) { |
245 | break; // Stop displaying essays (attempt not completed) |
246 | } |
247 | $count++; |
248 | |
249 | // Make sure they didn't answer it more than the max number of attmepts |
250 | if (count($try) > $lesson->maxattempts) { |
251 | $essay = $try[$lesson->maxattempts-1]; |
252 | } else { |
253 | $essay = end($try); |
254 | } |
255 | |
256 | // Start processing the attempt |
257 | $essayinfo = unserialize($essay->useranswer); |
258 | |
259 | // Different colors for all the states of an essay (graded, if sent, not graded) |
260 | if (!$essayinfo->graded) { |
261 | $class = ' class="graded"'; |
262 | } elseif (!$essayinfo->sent) { |
263 | $class = ' class="sent"'; |
264 | } else { |
265 | $class = ' class="ungraded"'; |
266 | } |
267 | // link for each essay |
268 | $essaylinks[] = "<a$class href=\"$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id&mode=grade&attemptid=$essay->id&sesskey=".sesskey().'">'.userdate($essay->timeseen, get_string('strftimedatetime')).' '.format_string($pages[$essay->pageid]->title,true).'</a>'; |
269 | } |
270 | } |
271 | // email link for this user |
272 | $emaillink = "<a href=\"$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id&mode=email&userid=$userid&sesskey=".sesskey().'">'.get_string('emailgradedessays', 'lesson').'</a>'; |
273 | |
274 | $table->data[] = array(print_user_picture($userid, $course->id, $users[$userid]->picture, 0, true).$studentname, implode("<br />\n", $essaylinks), $emaillink); |
275 | } |
276 | // email link for all users |
277 | $emailalllink = "<a href=\"$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id&mode=email&sesskey=".sesskey().'">'.get_string('emailallgradedessays', 'lesson').'</a>'; |
278 | |
279 | $table->data[] = array(' ', ' ', $emailalllink); |
280 | |
281 | print_table($table); |
282 | break; |
283 | case 'grade': |
284 | // Grading form |
285 | // Expects the following to be set: $attemptid, $answer, $user, $page, $attempt |
286 | |
287 | echo '<div class="grade"> |
288 | <form name="essaygrade" method="post" action="'.$CFG->wwwroot.'/mod/lesson/essay.php"> |
289 | <input type="hidden" name="id" value="'.$cm->id.'" /> |
290 | <input type="hidden" name="mode" value="update" /> |
291 | <input type="hidden" name="attemptid" value="'.$attemptid.'" /> |
292 | <input type="hidden" name="sesskey" value="'.sesskey().'" />'; |
293 | |
294 | // All tables will have these settings |
295 | $table = new stdClass; |
296 | $table->align = array('left'); |
297 | $table->wrap = array(); |
298 | $table->width = '50%'; |
299 | $table->size = array('100%'); |
300 | $table->class = 'generaltable gradetable'; |
301 | |
302 | // Print the question |
303 | $table->head = array(get_string('question', 'lesson')); |
304 | $options = new stdClass; |
305 | $options->noclean = true; |
306 | $table->data[] = array(format_text($page->contents, FORMAT_MOODLE, $options)); |
307 | |
308 | print_table($table); |
309 | |
310 | unset($table->data); |
311 | |
312 | // Now the user's answer |
313 | $essayinfo = unserialize($attempt->useranswer); |
314 | |
315 | $table->head = array(get_string('studentresponse', 'lesson', fullname($user, true))); |
316 | $table->data[] = array(format_text(stripslashes($essayinfo->answer))); |
317 | |
318 | print_table($table); |
319 | |
320 | unset($table->data); |
321 | |
322 | // Now a response box and grade drop-down for grader |
323 | $table->head = array(get_string('comments', 'lesson')); |
324 | $table->data[] = array(print_textarea(false, 15, 60, 0, 0, 'response', format_text($essayinfo->response, FORMAT_PLAIN, $options), $course->id, true)); |
325 | $options = array(); |
326 | if ($lesson->custom) { |
327 | for ($i=$answer->score; $i>=0; $i--) { |
328 | $options[$i] = $i; |
329 | } |
330 | } else { |
331 | $options[0] = get_string('nocredit', 'lesson'); |
332 | $options[1] = get_string('credit', 'lesson'); |
333 | } |
334 | $table->data[] = array(get_string('essayscore', 'lesson').': '.choose_from_menu($options, 'score', $essayinfo->score, '', '', '', true)); |
335 | |
336 | print_table($table); |
337 | echo '<div class="buttons"> |
338 | <input type="submit" name="cancel" value="'.get_string('cancel').'" /> |
339 | <input type="submit" value="'.get_string('savechanges').'" /> |
340 | </div> |
341 | </form> |
342 | </div>'; |
343 | break; |
344 | } |
345 | |
346 | print_footer($course); |
347 | ?> |