[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * Provides the interface for grading essay questions 20 * 21 * @package mod_lesson 22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 **/ 25 26 require_once('../../config.php'); 27 require_once($CFG->dirroot.'/mod/lesson/locallib.php'); 28 require_once($CFG->dirroot.'/mod/lesson/pagetypes/essay.php'); 29 require_once($CFG->dirroot.'/mod/lesson/essay_form.php'); 30 require_once($CFG->libdir.'/eventslib.php'); 31 32 $id = required_param('id', PARAM_INT); // Course Module ID 33 $mode = optional_param('mode', 'display', PARAM_ALPHA); 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 $dblesson = $DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST); 38 $lesson = new lesson($dblesson); 39 40 require_login($course, false, $cm); 41 $context = context_module::instance($cm->id); 42 require_capability('mod/lesson:grade', $context); 43 44 $url = new moodle_url('/mod/lesson/essay.php', array('id'=>$id)); 45 if ($mode !== 'display') { 46 $url->param('mode', $mode); 47 } 48 $PAGE->set_url($url); 49 50 $currentgroup = groups_get_activity_group($cm, true); 51 52 $attempt = new stdClass(); 53 $user = new stdClass(); 54 $attemptid = optional_param('attemptid', 0, PARAM_INT); 55 56 $formattextdefoptions = new stdClass(); 57 $formattextdefoptions->noclean = true; 58 $formattextdefoptions->para = false; 59 $formattextdefoptions->context = $context; 60 61 if ($attemptid > 0) { 62 $attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid)); 63 $answer = $DB->get_record('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $attempt->pageid)); 64 $user = $DB->get_record('user', array('id' => $attempt->userid)); 65 // Apply overrides. 66 $lesson->update_effective_access($user->id); 67 $scoreoptions = array(); 68 if ($lesson->custom) { 69 $i = $answer->score; 70 while ($i >= 0) { 71 $scoreoptions[$i] = (string)$i; 72 $i--; 73 } 74 } else { 75 $scoreoptions[0] = get_string('nocredit', 'lesson'); 76 $scoreoptions[1] = get_string('credit', 'lesson'); 77 } 78 } 79 80 /// Handle any preprocessing before header is printed - based on $mode 81 switch ($mode) { 82 case 'grade': 83 // Grading form - get the necessary data 84 require_sesskey(); 85 86 if (empty($attempt)) { 87 print_error('cannotfindattempt', 'lesson'); 88 } 89 if (empty($user)) { 90 print_error('cannotfinduser', 'lesson'); 91 } 92 if (empty($answer)) { 93 print_error('cannotfindanswer', 'lesson'); 94 } 95 break; 96 97 case 'update': 98 require_sesskey(); 99 100 if (empty($attempt)) { 101 print_error('cannotfindattempt', 'lesson'); 102 } 103 if (empty($user)) { 104 print_error('cannotfinduser', 'lesson'); 105 } 106 107 $editoroptions = array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 108 'maxbytes' => $CFG->maxbytes, 'context' => $context); 109 $essayinfo = lesson_page_type_essay::extract_useranswer($attempt->useranswer); 110 $essayinfo = file_prepare_standard_editor($essayinfo, 'response', $editoroptions, $context, 111 'mod_lesson', 'essay_responses', $attempt->id); 112 $mform = new essay_grading_form(null, array('scoreoptions' => $scoreoptions, 'user' => $user)); 113 $mform->set_data($essayinfo); 114 if ($mform->is_cancelled()) { 115 redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id"); 116 } 117 if ($form = $mform->get_data()) { 118 if (!$grades = $DB->get_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$attempt->userid), 'completed', '*', $attempt->retry, 1)) { 119 print_error('cannotfindgrade', 'lesson'); 120 } 121 122 $essayinfo->graded = 1; 123 $essayinfo->score = $form->score; 124 $form = file_postupdate_standard_editor($form, 'response', $editoroptions, $context, 125 'mod_lesson', 'essay_responses', $attempt->id); 126 $essayinfo->response = $form->response; 127 $essayinfo->responseformat = $form->responseformat; 128 $essayinfo->sent = 0; 129 if (!$lesson->custom && $essayinfo->score == 1) { 130 $attempt->correct = 1; 131 } else { 132 $attempt->correct = 0; 133 } 134 135 $attempt->useranswer = serialize($essayinfo); 136 137 $DB->update_record('lesson_attempts', $attempt); 138 139 // Get grade information 140 $grade = current($grades); 141 $gradeinfo = lesson_grade($lesson, $attempt->retry, $attempt->userid); 142 143 // Set and update 144 $updategrade = new stdClass(); 145 $updategrade->id = $grade->id; 146 $updategrade->grade = $gradeinfo->grade; 147 $DB->update_record('lesson_grades', $updategrade); 148 149 $params = array( 150 'context' => $context, 151 'objectid' => $grade->id, 152 'courseid' => $course->id, 153 'relateduserid' => $attempt->userid, 154 'other' => array( 155 'lessonid' => $lesson->id, 156 'attemptid' => $attemptid 157 ) 158 ); 159 $event = \mod_lesson\event\essay_assessed::create($params); 160 $event->add_record_snapshot('lesson', $dblesson); 161 $event->trigger(); 162 163 $lesson->add_message(get_string('changessaved'), 'notifysuccess'); 164 165 // update central gradebook 166 lesson_update_grades($lesson, $grade->userid); 167 168 redirect(new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id))); 169 } else { 170 print_error('invalidformdata'); 171 } 172 break; 173 case 'email': 174 // Sending an email(s) to a single user or all 175 require_sesskey(); 176 177 // Get our users (could be singular) 178 if ($userid = optional_param('userid', 0, PARAM_INT)) { 179 $queryadd = " AND userid = ?"; 180 if (! $users = $DB->get_records('user', array('id' => $userid))) { 181 print_error('cannotfinduser', 'lesson'); 182 } 183 } else { 184 $queryadd = ''; 185 186 // If group selected, only send to group members. 187 list($esql, $params) = get_enrolled_sql($context, '', $currentgroup, true); 188 list($sort, $sortparams) = users_order_by_sql('u'); 189 $params['lessonid'] = $lesson->id; 190 191 // Need to use inner view to avoid distinct + text 192 if (!$users = $DB->get_records_sql(" 193 SELECT u.* 194 FROM {user} u 195 JOIN ( 196 SELECT DISTINCT userid 197 FROM {lesson_attempts} 198 WHERE lessonid = :lessonid 199 ) ui ON u.id = ui.userid 200 JOIN ($esql) ue ON ue.id = u.id 201 ORDER BY $sort", $params)) { 202 print_error('cannotfinduser', 'lesson'); 203 } 204 } 205 206 $pages = $lesson->load_all_pages(); 207 foreach ($pages as $key=>$page) { 208 if ($page->qtype != LESSON_PAGE_ESSAY) { 209 unset($pages[$key]); 210 } 211 } 212 213 // Get only the attempts that are in response to essay questions 214 list($usql, $params) = $DB->get_in_or_equal(array_keys($pages)); 215 if (!empty($queryadd)) { 216 $params[] = $userid; 217 } 218 if (!$attempts = $DB->get_records_select('lesson_attempts', "pageid $usql".$queryadd, $params)) { 219 print_error('nooneansweredthisquestion', 'lesson'); 220 } 221 // Get the answers 222 list($answerUsql, $parameters) = $DB->get_in_or_equal(array_keys($pages)); 223 array_unshift($parameters, $lesson->id); 224 if (!$answers = $DB->get_records_select('lesson_answers', "lessonid = ? AND pageid $answerUsql", $parameters, '', 'pageid, score')) { 225 print_error('cannotfindanswer', 'lesson'); 226 } 227 228 foreach ($attempts as $attempt) { 229 $essayinfo = lesson_page_type_essay::extract_useranswer($attempt->useranswer); 230 if ($essayinfo->graded && !$essayinfo->sent) { 231 // Holds values for the essayemailsubject string for the email message 232 $a = new stdClass; 233 234 // Set the grade 235 $grades = $DB->get_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$attempt->userid), 'completed', '*', $attempt->retry, 1); 236 $grade = current($grades); 237 $a->newgrade = $grade->grade; 238 239 // Set the points 240 if ($lesson->custom) { 241 $a->earned = $essayinfo->score; 242 $a->outof = $answers[$attempt->pageid]->score; 243 } else { 244 $a->earned = $essayinfo->score; 245 $a->outof = 1; 246 } 247 248 // Set rest of the message values 249 $currentpage = $lesson->load_page($attempt->pageid); 250 $a->question = format_text($currentpage->contents, $currentpage->contentsformat, $formattextdefoptions); 251 $a->response = format_text($essayinfo->answer, $essayinfo->answerformat, 252 array('context' => $context, 'para' => true)); 253 $a->comment = $essayinfo->response; 254 $a->comment = file_rewrite_pluginfile_urls($a->comment, 'pluginfile.php', $context->id, 255 'mod_lesson', 'essay_responses', $attempt->id); 256 $a->comment = format_text($a->comment, $essayinfo->responseformat, $formattextdefoptions); 257 $a->lesson = format_string($lesson->name, true); 258 259 // Fetch message HTML and plain text formats 260 $message = get_string('essayemailmessage2', 'lesson', $a); 261 $plaintext = format_text_email($message, FORMAT_HTML); 262 263 // Subject 264 $subject = get_string('essayemailsubject', 'lesson'); 265 266 $eventdata = new stdClass(); 267 $eventdata->modulename = 'lesson'; 268 $eventdata->userfrom = $USER; 269 $eventdata->userto = $users[$attempt->userid]; 270 $eventdata->subject = $subject; 271 $eventdata->fullmessage = $plaintext; 272 $eventdata->fullmessageformat = FORMAT_PLAIN; 273 $eventdata->fullmessagehtml = $message; 274 $eventdata->smallmessage = ''; 275 276 // Required for messaging framework 277 $eventdata->component = 'mod_lesson'; 278 $eventdata->name = 'graded_essay'; 279 280 message_send($eventdata); 281 $essayinfo->sent = 1; 282 $attempt->useranswer = serialize($essayinfo); 283 $DB->update_record('lesson_attempts', $attempt); 284 } 285 } 286 $lesson->add_message(get_string('emailsuccess', 'lesson'), 'notifysuccess'); 287 redirect(new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id))); 288 break; 289 case 'display': // Default view - get the necessary data 290 default: 291 // Get lesson pages that are essay 292 $pages = $lesson->load_all_pages(); 293 foreach ($pages as $key=>$page) { 294 if ($page->qtype != LESSON_PAGE_ESSAY) { 295 unset($pages[$key]); 296 } 297 } 298 if (count($pages) > 0) { 299 // Get only the attempts that are in response to essay questions 300 list($usql, $parameters) = $DB->get_in_or_equal(array_keys($pages), SQL_PARAMS_NAMED); 301 // If group selected, only get group members attempts. 302 list($esql, $params) = get_enrolled_sql($context, '', $currentgroup, true); 303 $parameters = array_merge($params, $parameters); 304 305 $sql = "SELECT a.* 306 FROM {lesson_attempts} a 307 JOIN ($esql) ue ON a.userid = ue.id 308 WHERE pageid $usql"; 309 if ($essayattempts = $DB->get_records_sql($sql, $parameters)) { 310 $ufields = user_picture::fields('u'); 311 // Get all the users who have taken this lesson. 312 list($sort, $sortparams) = users_order_by_sql('u'); 313 314 $params['lessonid'] = $lesson->id; 315 $sql = "SELECT DISTINCT $ufields 316 FROM {user} u 317 JOIN {lesson_attempts} a ON u.id = a.userid 318 JOIN ($esql) ue ON ue.id = a.userid 319 WHERE a.lessonid = :lessonid 320 ORDER BY $sort"; 321 if (!$users = $DB->get_records_sql($sql, $params)) { 322 $mode = 'none'; // not displaying anything 323 if (!empty($currentgroup)) { 324 $groupname = groups_get_group_name($currentgroup); 325 $lesson->add_message(get_string('noonehasansweredgroup', 'lesson', $groupname)); 326 } else { 327 $lesson->add_message(get_string('noonehasanswered', 'lesson')); 328 } 329 } 330 } else { 331 $mode = 'none'; // not displaying anything 332 if (!empty($currentgroup)) { 333 $groupname = groups_get_group_name($currentgroup); 334 $lesson->add_message(get_string('noonehasansweredgroup', 'lesson', $groupname)); 335 } else { 336 $lesson->add_message(get_string('noonehasanswered', 'lesson')); 337 } 338 } 339 } else { 340 $mode = 'none'; // not displaying anything 341 $lesson->add_message(get_string('noessayquestionsfound', 'lesson')); 342 } 343 break; 344 } 345 346 $lessonoutput = $PAGE->get_renderer('mod_lesson'); 347 echo $lessonoutput->header($lesson, $cm, 'essay', false, null, get_string('manualgrading', 'lesson')); 348 349 switch ($mode) { 350 case 'display': 351 groups_print_activity_menu($cm, $url); 352 // Expects $user, $essayattempts and $pages to be set already 353 354 // Group all the essays by userid 355 $studentessays = array(); 356 foreach ($essayattempts as $essay) { 357 // Not very nice :) but basically 358 // this organizes the essays so we know how many 359 // times a student answered an essay per try and per page 360 $studentessays[$essay->userid][$essay->pageid][$essay->retry][] = $essay; 361 } 362 363 // Setup table 364 $table = new html_table(); 365 $table->head = array(get_string('name'), get_string('essays', 'lesson'), get_string('email', 'lesson')); 366 $table->attributes['class'] = 'standardtable generaltable'; 367 $table->align = array('left', 'left', 'left'); 368 $table->wrap = array('nowrap', 'nowrap', ''); 369 370 // Cycle through all the students 371 foreach (array_keys($studentessays) as $userid) { 372 $studentname = fullname($users[$userid], true); 373 $essaylinks = array(); 374 375 // Number of attempts on the lesson 376 $attempts = $DB->count_records('lesson_grades', array('userid'=>$userid, 'lessonid'=>$lesson->id)); 377 378 // Go through each essay page 379 foreach ($studentessays[$userid] as $page => $tries) { 380 $count = 0; 381 382 // Go through each attempt per page 383 foreach($tries as $try) { 384 if ($count == $attempts) { 385 break; // Stop displaying essays (attempt not completed) 386 } 387 $count++; 388 389 // Make sure they didn't answer it more than the max number of attmepts 390 if (count($try) > $lesson->maxattempts) { 391 $essay = $try[$lesson->maxattempts-1]; 392 } else { 393 $essay = end($try); 394 } 395 396 // Start processing the attempt 397 $essayinfo = lesson_page_type_essay::extract_useranswer($essay->useranswer); 398 399 // link for each essay 400 $url = new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id,'mode'=>'grade','attemptid'=>$essay->id,'sesskey'=>sesskey())); 401 $attributes = array(); 402 // Different colors for all the states of an essay (graded, if sent, not graded) 403 if (!$essayinfo->graded) { 404 $attributes['class'] = "essayungraded"; 405 } elseif (!$essayinfo->sent) { 406 $attributes['class'] = "essaygraded"; 407 } else { 408 $attributes['class'] = "essaysent"; 409 } 410 $essaylinks[] = html_writer::link($url, userdate($essay->timeseen, get_string('strftimedatetime')).' '.format_string($pages[$essay->pageid]->title,true), $attributes); 411 } 412 } 413 // email link for this user 414 $url = new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id,'mode'=>'email','userid'=>$userid,'sesskey'=>sesskey())); 415 $emaillink = html_writer::link($url, get_string('emailgradedessays', 'lesson')); 416 417 $table->data[] = array($OUTPUT->user_picture($users[$userid], array('courseid'=>$course->id)).$studentname, implode("<br />", $essaylinks), $emaillink); 418 } 419 420 // email link for all users 421 $url = new moodle_url('/mod/lesson/essay.php', array('id'=>$cm->id,'mode'=>'email','sesskey'=>sesskey())); 422 $emailalllink = html_writer::link($url, get_string('emailallgradedessays', 'lesson')); 423 424 $table->data[] = array(' ', ' ', $emailalllink); 425 426 echo html_writer::table($table); 427 break; 428 case 'grade': 429 // Trigger the essay grade viewed event. 430 $event = \mod_lesson\event\essay_attempt_viewed::create(array( 431 'objectid' => $attempt->id, 432 'relateduserid' => $attempt->userid, 433 'context' => $context, 434 'courseid' => $course->id, 435 )); 436 $event->add_record_snapshot('lesson_attempts', $attempt); 437 $event->trigger(); 438 439 // Grading form 440 // Expects the following to be set: $attemptid, $answer, $user, $page, $attempt 441 $essayinfo = lesson_page_type_essay::extract_useranswer($attempt->useranswer); 442 $currentpage = $lesson->load_page($attempt->pageid); 443 444 $mform = new essay_grading_form(null, array('scoreoptions'=>$scoreoptions, 'user'=>$user)); 445 $data = new stdClass; 446 $data->id = $cm->id; 447 $data->attemptid = $attemptid; 448 $data->score = $essayinfo->score; 449 $data->question = format_text($currentpage->contents, $currentpage->contentsformat, $formattextdefoptions); 450 $data->studentanswer = format_text($essayinfo->answer, $essayinfo->answerformat, 451 array('context' => $context, 'para' => true)); 452 $data->response = $essayinfo->response; 453 $data->responseformat = $essayinfo->responseformat; 454 $editoroptions = array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 455 'maxbytes' => $CFG->maxbytes, 'context' => $context); 456 $data = file_prepare_standard_editor($data, 'response', $editoroptions, $context, 457 'mod_lesson', 'essay_responses', $attempt->id); 458 $mform->set_data($data); 459 460 $mform->display(); 461 break; 462 default: 463 groups_print_activity_menu($cm, $url); 464 break; 465 } 466 467 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |