Commit | Line | Data |
---|---|---|
bbd0e548 DW |
1 | <?php |
2 | // This file is part of Moodle - http://moodle.org/ | |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
18 | * This file contains the definition for the class assignment | |
19 | * | |
20 | * This class provides all the functionality for the new assign module. | |
21 | * | |
22 | * @package mod_assign | |
23 | * @copyright 2012 NetSpot {@link http://www.netspot.com.au} | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
25 | */ | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | /** | |
30 | * Assignment submission statuses | |
31 | */ | |
32 | define('ASSIGN_SUBMISSION_STATUS_DRAFT', 'draft'); // student thinks it is a draft | |
33 | define('ASSIGN_SUBMISSION_STATUS_SUBMITTED', 'submitted'); // student thinks it is finished | |
34 | ||
35 | /** | |
36 | * Search filters for grading page | |
37 | */ | |
38 | define('ASSIGN_FILTER_SUBMITTED', 'submitted'); | |
39 | define('ASSIGN_FILTER_SINGLE_USER', 'singleuser'); | |
40 | define('ASSIGN_FILTER_REQUIRE_GRADING', 'require_grading'); | |
41 | ||
bbd0e548 DW |
42 | /** Include accesslib.php */ |
43 | require_once($CFG->libdir.'/accesslib.php'); | |
44 | /** Include formslib.php */ | |
45 | require_once($CFG->libdir.'/formslib.php'); | |
46 | /** Include repository/lib.php */ | |
47 | require_once($CFG->dirroot . '/repository/lib.php'); | |
48 | /** Include local mod_form.php */ | |
49 | require_once($CFG->dirroot.'/mod/assign/mod_form.php'); | |
bbd0e548 DW |
50 | /** gradelib.php */ |
51 | require_once($CFG->libdir.'/gradelib.php'); | |
52 | /** grading lib.php */ | |
53 | require_once($CFG->dirroot.'/grade/grading/lib.php'); | |
54 | /** Include feedbackplugin.php */ | |
55 | require_once($CFG->dirroot.'/mod/assign/feedbackplugin.php'); | |
56 | /** Include submissionplugin.php */ | |
57 | require_once($CFG->dirroot.'/mod/assign/submissionplugin.php'); | |
58 | /** Include renderable.php */ | |
59 | require_once($CFG->dirroot.'/mod/assign/renderable.php'); | |
60 | /** Include gradingtable.php */ | |
61 | require_once($CFG->dirroot.'/mod/assign/gradingtable.php'); | |
62 | /** Include eventslib.php */ | |
63 | require_once($CFG->libdir.'/eventslib.php'); | |
64 | ||
65 | ||
66 | /** | |
67 | * Standard base class for mod_assign (assignment types). | |
68 | * | |
69 | * @package mod_assign | |
70 | * @copyright 2012 NetSpot {@link http://www.netspot.com.au} | |
71 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
72 | */ | |
73 | class assign { | |
74 | ||
75 | ||
76 | /** @var stdClass the assignment record that contains the global settings for this assign instance */ | |
77 | private $instance; | |
78 | ||
79 | /** @var context the context of the course module for this assign instance (or just the course if we are | |
80 | creating a new one) */ | |
81 | private $context; | |
82 | ||
83 | /** @var stdClass the course this assign instance belongs to */ | |
84 | private $course; | |
bc5a657b | 85 | |
cfc81f03 DW |
86 | /** @var stdClass the admin config for all assign instances */ |
87 | private $adminconfig; | |
88 | ||
bbd0e548 DW |
89 | |
90 | /** @var assign_renderer the custom renderer for this module */ | |
91 | private $output; | |
92 | ||
93 | /** @var stdClass the course module for this assign instance */ | |
94 | private $coursemodule; | |
95 | ||
96 | /** @var array cache for things like the coursemodule name or the scale menu - only lives for a single | |
97 | request */ | |
98 | private $cache; | |
99 | ||
100 | /** @var array list of the installed submission plugins */ | |
101 | private $submissionplugins; | |
102 | ||
103 | /** @var array list of the installed feedback plugins */ | |
104 | private $feedbackplugins; | |
105 | ||
106 | /** @var string action to be used to return to this page (without repeating any form submissions etc.) */ | |
107 | private $returnaction = 'view'; | |
108 | ||
109 | /** @var array params to be used to return to this page */ | |
110 | private $returnparams = array(); | |
111 | ||
112 | /** @var string modulename prevents excessive calls to get_string */ | |
f5b32abe | 113 | private static $modulename = null; |
bbd0e548 DW |
114 | |
115 | /** @var string modulenameplural prevents excessive calls to get_string */ | |
f5b32abe | 116 | private static $modulenameplural = null; |
bbd0e548 DW |
117 | |
118 | /** | |
119 | * Constructor for the base assign class | |
120 | * | |
121 | * @param mixed $coursemodulecontext context|null the course module context (or the course context if the coursemodule has not been created yet) | |
122 | * @param mixed $coursemodule the current course module if it was already loaded - otherwise this class will load one from the context as required | |
123 | * @param mixed $course the current course if it was already loaded - otherwise this class will load one from the context as required | |
124 | */ | |
125 | public function __construct($coursemodulecontext, $coursemodule, $course) { | |
126 | global $PAGE; | |
127 | ||
128 | $this->context = $coursemodulecontext; | |
129 | $this->coursemodule = $coursemodule; | |
130 | $this->course = $course; | |
131 | $this->cache = array(); // temporary cache only lives for a single request - used to reduce db lookups | |
132 | ||
133 | $this->submissionplugins = $this->load_plugins('assignsubmission'); | |
134 | $this->feedbackplugins = $this->load_plugins('assignfeedback'); | |
135 | $this->output = $PAGE->get_renderer('mod_assign'); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Set the action and parameters that can be used to return to the current page | |
140 | * | |
141 | * @param string $action The action for the current page | |
142 | * @param array $params An array of name value pairs which form the parameters to return to the current page | |
143 | * @return void | |
144 | */ | |
145 | public function register_return_link($action, $params) { | |
146 | $this->returnaction = $action; | |
147 | $this->returnparams = $params; | |
148 | } | |
149 | ||
150 | /** | |
151 | * Return an action that can be used to get back to the current page | |
152 | * @return string action | |
153 | */ | |
154 | public function get_return_action() { | |
155 | return $this->returnaction; | |
156 | } | |
157 | ||
158 | /** | |
159 | * Based on the current assignment settings should we display the intro | |
160 | * @return bool showintro | |
161 | */ | |
162 | private function show_intro() { | |
163 | if ($this->get_instance()->alwaysshowdescription || | |
164 | time() > $this->get_instance()->allowsubmissionsfromdate) { | |
165 | return true; | |
166 | } | |
167 | return false; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Return a list of parameters that can be used to get back to the current page | |
172 | * @return array params | |
173 | */ | |
174 | public function get_return_params() { | |
175 | return $this->returnparams; | |
176 | } | |
177 | ||
178 | /** | |
179 | * Set the submitted form data | |
180 | * @param stdClass $data The form data (instance) | |
181 | */ | |
182 | public function set_instance(stdClass $data) { | |
183 | $this->instance = $data; | |
184 | } | |
185 | ||
186 | /** | |
187 | * Set the context | |
188 | * @param context $context The new context | |
189 | */ | |
190 | public function set_context(context $context) { | |
191 | $this->context = $context; | |
192 | } | |
193 | ||
194 | /** | |
195 | * Set the course data | |
196 | * @param stdClass $course The course data | |
197 | */ | |
198 | public function set_course(stdClass $course) { | |
199 | $this->course = $course; | |
200 | } | |
201 | ||
202 | /** | |
203 | * get list of feedback plugins installed | |
204 | * @return array | |
205 | */ | |
206 | public function get_feedback_plugins() { | |
207 | return $this->feedbackplugins; | |
208 | } | |
209 | ||
210 | /** | |
211 | * get list of submission plugins installed | |
212 | * @return array | |
213 | */ | |
214 | public function get_submission_plugins() { | |
215 | return $this->submissionplugins; | |
216 | } | |
217 | ||
218 | ||
219 | /** | |
220 | * get a specific submission plugin by its type | |
221 | * @param string $subtype assignsubmission | assignfeedback | |
222 | * @param string $type | |
223 | * @return mixed assign_plugin|null | |
224 | */ | |
225 | private function get_plugin_by_type($subtype, $type) { | |
226 | $shortsubtype = substr($subtype, strlen('assign')); | |
227 | $name = $shortsubtype . 'plugins'; | |
228 | $pluginlist = $this->$name; | |
229 | foreach ($pluginlist as $plugin) { | |
230 | if ($plugin->get_type() == $type) { | |
231 | return $plugin; | |
232 | } | |
233 | } | |
234 | return null; | |
235 | } | |
236 | ||
237 | /** | |
238 | * Get a feedback plugin by type | |
239 | * @param string $type - The type of plugin e.g comments | |
240 | * @return mixed assign_feedback_plugin|null | |
241 | */ | |
242 | public function get_feedback_plugin_by_type($type) { | |
243 | return $this->get_plugin_by_type('assignfeedback', $type); | |
244 | } | |
245 | ||
246 | /** | |
247 | * Get a submission plugin by type | |
248 | * @param string $type - The type of plugin e.g comments | |
249 | * @return mixed assign_submission_plugin|null | |
250 | */ | |
251 | public function get_submission_plugin_by_type($type) { | |
252 | return $this->get_plugin_by_type('assignsubmission', $type); | |
253 | } | |
254 | ||
255 | /** | |
256 | * Load the plugins from the sub folders under subtype | |
257 | * @param string $subtype - either submission or feedback | |
258 | * @return array - The sorted list of plugins | |
259 | */ | |
260 | private function load_plugins($subtype) { | |
261 | global $CFG; | |
262 | $result = array(); | |
263 | ||
264 | $names = get_plugin_list($subtype); | |
265 | ||
266 | foreach ($names as $name => $path) { | |
267 | if (file_exists($path . '/locallib.php')) { | |
268 | require_once($path . '/locallib.php'); | |
269 | ||
270 | $shortsubtype = substr($subtype, strlen('assign')); | |
271 | $pluginclass = 'assign_' . $shortsubtype . '_' . $name; | |
272 | ||
273 | $plugin = new $pluginclass($this, $name); | |
274 | ||
275 | if ($plugin instanceof assign_plugin) { | |
276 | $idx = $plugin->get_sort_order(); | |
277 | while (array_key_exists($idx, $result)) $idx +=1; | |
278 | $result[$idx] = $plugin; | |
279 | } | |
280 | } | |
281 | } | |
282 | ksort($result); | |
283 | return $result; | |
284 | } | |
285 | ||
286 | ||
287 | /** | |
288 | * Display the assignment, used by view.php | |
289 | * | |
290 | * The assignment is displayed differently depending on your role, | |
291 | * the settings for the assignment and the status of the assignment. | |
292 | * @param string $action The current action if any. | |
293 | * @return void | |
294 | */ | |
295 | public function view($action='') { | |
296 | ||
297 | $o = ''; | |
298 | $mform = null; | |
299 | ||
300 | // handle form submissions first | |
301 | if ($action == 'savesubmission') { | |
302 | $action = 'editsubmission'; | |
303 | if ($this->process_save_submission($mform)) { | |
304 | $action = 'view'; | |
305 | } | |
306 | } else if ($action == 'lock') { | |
307 | $this->process_lock(); | |
308 | $action = 'grading'; | |
309 | } else if ($action == 'reverttodraft') { | |
310 | $this->process_revert_to_draft(); | |
311 | $action = 'grading'; | |
312 | } else if ($action == 'unlock') { | |
313 | $this->process_unlock(); | |
314 | $action = 'grading'; | |
315 | } else if ($action == 'confirmsubmit') { | |
316 | $this->process_submit_for_grading(); | |
317 | // save and show next button | |
318 | } else if ($action == 'batchgradingoperation') { | |
319 | $this->process_batch_grading_operation(); | |
320 | $action = 'grading'; | |
321 | } else if ($action == 'submitgrade') { | |
322 | if (optional_param('saveandshownext', null, PARAM_ALPHA)) { | |
323 | //save and show next | |
324 | $action = 'grade'; | |
325 | if ($this->process_save_grade($mform)) { | |
326 | $action = 'nextgrade'; | |
327 | } | |
328 | } else if (optional_param('nosaveandprevious', null, PARAM_ALPHA)) { | |
329 | $action = 'previousgrade'; | |
330 | } else if (optional_param('nosaveandnext', null, PARAM_ALPHA)) { | |
331 | //show next button | |
332 | $action = 'nextgrade'; | |
333 | } else if (optional_param('savegrade', null, PARAM_ALPHA)) { | |
334 | //save changes button | |
335 | $action = 'grade'; | |
336 | if ($this->process_save_grade($mform)) { | |
337 | $action = 'grading'; | |
338 | } | |
339 | } else { | |
340 | //cancel button | |
341 | $action = 'grading'; | |
342 | } | |
bf78ebd6 DW |
343 | }else if ($action == 'quickgrade') { |
344 | $message = $this->process_save_quick_grades(); | |
345 | $action = 'quickgradingresult'; | |
bbd0e548 DW |
346 | }else if ($action == 'saveoptions') { |
347 | $this->process_save_grading_options(); | |
348 | $action = 'grading'; | |
349 | } | |
350 | ||
351 | $returnparams = array('rownum'=>optional_param('rownum', 0, PARAM_INT)); | |
352 | $this->register_return_link($action, $returnparams); | |
353 | ||
354 | // now show the right view page | |
355 | if ($action == 'previousgrade') { | |
356 | $mform = null; | |
357 | $o .= $this->view_single_grade_page($mform, -1); | |
bf78ebd6 DW |
358 | } else if ($action == 'quickgradingresult') { |
359 | $mform = null; | |
360 | $o .= $this->view_quickgrading_result($message); | |
bbd0e548 DW |
361 | } else if ($action == 'nextgrade') { |
362 | $mform = null; | |
363 | $o .= $this->view_single_grade_page($mform, 1); | |
bbd0e548 DW |
364 | } else if ($action == 'grade') { |
365 | $o .= $this->view_single_grade_page($mform); | |
366 | } else if ($action == 'viewpluginassignfeedback') { | |
367 | $o .= $this->view_plugin_content('assignfeedback'); | |
368 | } else if ($action == 'viewpluginassignsubmission') { | |
369 | $o .= $this->view_plugin_content('assignsubmission'); | |
370 | } else if ($action == 'editsubmission') { | |
371 | $o .= $this->view_edit_submission_page($mform); | |
372 | } else if ($action == 'grading') { | |
373 | $o .= $this->view_grading_page(); | |
374 | } else if ($action == 'downloadall') { | |
375 | $o .= $this->download_submissions(); | |
376 | } else if ($action == 'submit') { | |
377 | $o .= $this->check_submit_for_grading(); | |
378 | } else { | |
379 | $o .= $this->view_submission_page(); | |
380 | } | |
381 | ||
382 | return $o; | |
383 | } | |
384 | ||
385 | ||
386 | /** | |
387 | * Add this instance to the database | |
388 | * | |
389 | * @param stdClass $formdata The data submitted from the form | |
390 | * @param bool $callplugins This is used to skip the plugin code | |
391 | * when upgrading an old assignment to a new one (the plugins get called manually) | |
392 | * @return mixed false if an error occurs or the int id of the new instance | |
393 | */ | |
394 | public function add_instance(stdClass $formdata, $callplugins) { | |
395 | global $DB; | |
396 | ||
397 | $err = ''; | |
398 | ||
399 | // add the database record | |
400 | $update = new stdClass(); | |
401 | $update->name = $formdata->name; | |
402 | $update->timemodified = time(); | |
403 | $update->timecreated = time(); | |
404 | $update->course = $formdata->course; | |
405 | $update->courseid = $formdata->course; | |
406 | $update->intro = $formdata->intro; | |
407 | $update->introformat = $formdata->introformat; | |
408 | $update->alwaysshowdescription = $formdata->alwaysshowdescription; | |
409 | $update->preventlatesubmissions = $formdata->preventlatesubmissions; | |
410 | $update->submissiondrafts = $formdata->submissiondrafts; | |
411 | $update->sendnotifications = $formdata->sendnotifications; | |
75f87a57 | 412 | $update->sendlatenotifications = $formdata->sendlatenotifications; |
bbd0e548 DW |
413 | $update->duedate = $formdata->duedate; |
414 | $update->allowsubmissionsfromdate = $formdata->allowsubmissionsfromdate; | |
415 | $update->grade = $formdata->grade; | |
416 | $returnid = $DB->insert_record('assign', $update); | |
417 | $this->instance = $DB->get_record('assign', array('id'=>$returnid), '*', MUST_EXIST); | |
418 | // cache the course record | |
419 | $this->course = $DB->get_record('course', array('id'=>$formdata->course), '*', MUST_EXIST); | |
420 | ||
421 | if ($callplugins) { | |
422 | // call save_settings hook for submission plugins | |
423 | foreach ($this->submissionplugins as $plugin) { | |
424 | if (!$this->update_plugin_instance($plugin, $formdata)) { | |
425 | print_error($plugin->get_error()); | |
426 | return false; | |
427 | } | |
428 | } | |
429 | foreach ($this->feedbackplugins as $plugin) { | |
430 | if (!$this->update_plugin_instance($plugin, $formdata)) { | |
431 | print_error($plugin->get_error()); | |
432 | return false; | |
433 | } | |
434 | } | |
435 | ||
436 | // in the case of upgrades the coursemodule has not been set so we need to wait before calling these two | |
437 | // TODO: add event to the calendar | |
438 | $this->update_calendar($formdata->coursemodule); | |
439 | // TODO: add the item in the gradebook | |
440 | $this->update_gradebook(false, $formdata->coursemodule); | |
441 | ||
442 | } | |
443 | ||
444 | $update = new stdClass(); | |
445 | $update->id = $this->get_instance()->id; | |
446 | $update->nosubmissions = (!$this->is_any_submission_plugin_enabled()) ? 1: 0; | |
447 | $DB->update_record('assign', $update); | |
448 | ||
449 | return $returnid; | |
450 | } | |
451 | ||
452 | /** | |
453 | * Delete all grades from the gradebook for this assignment | |
454 | * | |
455 | * @return bool | |
456 | */ | |
457 | private function delete_grades() { | |
458 | global $CFG; | |
459 | ||
460 | return grade_update('mod/assign', $this->get_course()->id, 'mod', 'assign', $this->get_instance()->id, 0, NULL, array('deleted'=>1)) == GRADE_UPDATE_OK; | |
461 | } | |
462 | ||
463 | /** | |
464 | * Delete this instance from the database | |
465 | * | |
466 | * @return bool false if an error occurs | |
467 | */ | |
468 | public function delete_instance() { | |
469 | global $DB; | |
470 | $result = true; | |
471 | ||
472 | foreach ($this->submissionplugins as $plugin) { | |
473 | if (!$plugin->delete_instance()) { | |
474 | print_error($plugin->get_error()); | |
475 | $result = false; | |
476 | } | |
477 | } | |
478 | foreach ($this->feedbackplugins as $plugin) { | |
479 | if (!$plugin->delete_instance()) { | |
480 | print_error($plugin->get_error()); | |
481 | $result = false; | |
482 | } | |
483 | } | |
484 | ||
485 | // delete files associated with this assignment | |
486 | $fs = get_file_storage(); | |
487 | if (! $fs->delete_area_files($this->context->id) ) { | |
488 | $result = false; | |
489 | } | |
490 | ||
491 | // delete_records will throw an exception if it fails - so no need for error checking here | |
492 | ||
493 | $DB->delete_records('assign_submission', array('assignment'=>$this->get_instance()->id)); | |
494 | $DB->delete_records('assign_grades', array('assignment'=>$this->get_instance()->id)); | |
495 | $DB->delete_records('assign_plugin_config', array('assignment'=>$this->get_instance()->id)); | |
496 | ||
497 | // delete items from the gradebook | |
498 | if (! $this->delete_grades()) { | |
499 | $result = false; | |
500 | } | |
501 | ||
502 | // delete the instance | |
503 | $DB->delete_records('assign', array('id'=>$this->get_instance()->id)); | |
504 | ||
505 | return $result; | |
506 | } | |
507 | ||
508 | /** | |
509 | * Update the settings for a single plugin | |
510 | * | |
511 | * @param assign_plugin $plugin The plugin to update | |
512 | * @param stdClass $formdata The form data | |
513 | * @return bool false if an error occurs | |
514 | */ | |
515 | private function update_plugin_instance(assign_plugin $plugin, stdClass $formdata) { | |
516 | if ($plugin->is_visible()) { | |
517 | $enabledname = $plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled'; | |
518 | if ($formdata->$enabledname) { | |
519 | $plugin->enable(); | |
520 | if (!$plugin->save_settings($formdata)) { | |
521 | print_error($plugin->get_error()); | |
522 | return false; | |
523 | } | |
524 | } else { | |
525 | $plugin->disable(); | |
526 | } | |
527 | } | |
528 | return true; | |
529 | } | |
530 | ||
531 | /** | |
532 | * Update the gradebook information for this assignment | |
533 | * | |
534 | * @param bool $reset If true, will reset all grades in the gradbook for this assignment | |
535 | * @param int $coursemoduleid This is required because it might not exist in the database yet | |
536 | * @return bool | |
537 | */ | |
538 | public function update_gradebook($reset, $coursemoduleid) { | |
539 | global $CFG; | |
540 | /** Include lib.php */ | |
541 | require_once($CFG->dirroot.'/mod/assign/lib.php'); | |
542 | $assign = clone $this->get_instance(); | |
543 | $assign->cmidnumber = $coursemoduleid; | |
544 | $param = null; | |
545 | if ($reset) { | |
546 | $param = 'reset'; | |
547 | } | |
548 | ||
549 | return assign_grade_item_update($assign, $param); | |
550 | } | |
551 | ||
cfc81f03 | 552 | /** Load and cache the admin config for this module |
bc5a657b | 553 | * |
cfc81f03 DW |
554 | * @return stdClass the plugin config |
555 | */ | |
556 | public function get_admin_config() { | |
557 | if ($this->adminconfig) { | |
558 | return $this->adminconfig; | |
559 | } | |
b11808c7 | 560 | $this->adminconfig = get_config('assign'); |
cfc81f03 DW |
561 | return $this->adminconfig; |
562 | } | |
563 | ||
bbd0e548 DW |
564 | |
565 | /** | |
566 | * Update the calendar entries for this assignment | |
567 | * | |
568 | * @param int $coursemoduleid - Required to pass this in because it might not exist in the database yet | |
569 | * @return bool | |
570 | */ | |
571 | public function update_calendar($coursemoduleid) { | |
572 | global $DB, $CFG; | |
573 | require_once($CFG->dirroot.'/calendar/lib.php'); | |
574 | ||
575 | // special case for add_instance as the coursemodule has not been set yet. | |
576 | ||
577 | if ($this->get_instance()->duedate) { | |
578 | $event = new stdClass(); | |
579 | ||
580 | if ($event->id = $DB->get_field('event', 'id', array('modulename'=>'assign', 'instance'=>$this->get_instance()->id))) { | |
581 | ||
582 | $event->name = $this->get_instance()->name; | |
583 | ||
584 | $event->description = format_module_intro('assign', $this->get_instance(), $coursemoduleid); | |
585 | $event->timestart = $this->get_instance()->duedate; | |
586 | ||
587 | $calendarevent = calendar_event::load($event->id); | |
588 | $calendarevent->update($event); | |
589 | } else { | |
590 | $event = new stdClass(); | |
591 | $event->name = $this->get_instance()->name; | |
592 | $event->description = format_module_intro('assign', $this->get_instance(), $coursemoduleid); | |
593 | $event->courseid = $this->get_instance()->course; | |
594 | $event->groupid = 0; | |
595 | $event->userid = 0; | |
596 | $event->modulename = 'assign'; | |
597 | $event->instance = $this->get_instance()->id; | |
598 | $event->eventtype = 'due'; | |
599 | $event->timestart = $this->get_instance()->duedate; | |
600 | $event->timeduration = 0; | |
601 | ||
602 | calendar_event::create($event); | |
603 | } | |
604 | } else { | |
605 | $DB->delete_records('event', array('modulename'=>'assign', 'instance'=>$this->get_instance()->id)); | |
606 | } | |
607 | } | |
608 | ||
609 | ||
610 | /** | |
611 | * Update this instance in the database | |
612 | * | |
613 | * @param stdClass $formdata - the data submitted from the form | |
614 | * @return bool false if an error occurs | |
615 | */ | |
616 | public function update_instance($formdata) { | |
617 | global $DB; | |
618 | ||
619 | $update = new stdClass(); | |
620 | $update->id = $formdata->instance; | |
621 | $update->name = $formdata->name; | |
622 | $update->timemodified = time(); | |
623 | $update->course = $formdata->course; | |
624 | $update->intro = $formdata->intro; | |
625 | $update->introformat = $formdata->introformat; | |
626 | $update->alwaysshowdescription = $formdata->alwaysshowdescription; | |
627 | $update->preventlatesubmissions = $formdata->preventlatesubmissions; | |
628 | $update->submissiondrafts = $formdata->submissiondrafts; | |
629 | $update->sendnotifications = $formdata->sendnotifications; | |
75f87a57 | 630 | $update->sendlatenotifications = $formdata->sendlatenotifications; |
bbd0e548 DW |
631 | $update->duedate = $formdata->duedate; |
632 | $update->allowsubmissionsfromdate = $formdata->allowsubmissionsfromdate; | |
633 | $update->grade = $formdata->grade; | |
634 | ||
635 | $result = $DB->update_record('assign', $update); | |
636 | $this->instance = $DB->get_record('assign', array('id'=>$update->id), '*', MUST_EXIST); | |
637 | ||
638 | // load the assignment so the plugins have access to it | |
639 | ||
640 | // call save_settings hook for submission plugins | |
641 | foreach ($this->submissionplugins as $plugin) { | |
642 | if (!$this->update_plugin_instance($plugin, $formdata)) { | |
643 | print_error($plugin->get_error()); | |
644 | return false; | |
645 | } | |
646 | } | |
647 | foreach ($this->feedbackplugins as $plugin) { | |
648 | if (!$this->update_plugin_instance($plugin, $formdata)) { | |
649 | print_error($plugin->get_error()); | |
650 | return false; | |
651 | } | |
652 | } | |
653 | ||
654 | ||
655 | // update the database record | |
656 | ||
657 | ||
658 | // update all the calendar events | |
659 | $this->update_calendar($this->get_course_module()->id); | |
660 | ||
661 | $this->update_gradebook(false, $this->get_course_module()->id); | |
662 | ||
663 | $update = new stdClass(); | |
664 | $update->id = $this->get_instance()->id; | |
665 | $update->nosubmissions = (!$this->is_any_submission_plugin_enabled()) ? 1: 0; | |
666 | $DB->update_record('assign', $update); | |
667 | ||
668 | ||
669 | ||
670 | ||
671 | ||
672 | return $result; | |
673 | } | |
674 | ||
675 | /** | |
676 | * add elements in grading plugin form | |
677 | * | |
678 | * @param mixed $grade stdClass|null | |
679 | * @param MoodleQuickForm $mform | |
680 | * @param stdClass $data | |
681 | * @return void | |
682 | */ | |
683 | private function add_plugin_grade_elements($grade, MoodleQuickForm $mform, stdClass $data) { | |
684 | foreach ($this->feedbackplugins as $plugin) { | |
685 | if ($plugin->is_enabled() && $plugin->is_visible()) { | |
686 | $mform->addElement('header', 'header_' . $plugin->get_type(), $plugin->get_name()); | |
687 | if (!$plugin->get_form_elements($grade, $mform, $data)) { | |
688 | $mform->removeElement('header_' . $plugin->get_type()); | |
689 | } | |
690 | } | |
691 | } | |
692 | } | |
693 | ||
694 | ||
695 | ||
696 | /** | |
697 | * Add one plugins settings to edit plugin form | |
698 | * | |
699 | * @param assign_plugin $plugin The plugin to add the settings from | |
700 | * @param MoodleQuickForm $mform The form to add the configuration settings to. This form is modified directly (not returned) | |
701 | * @return void | |
702 | */ | |
703 | private function add_plugin_settings(assign_plugin $plugin, MoodleQuickForm $mform) { | |
704 | global $CFG; | |
705 | if ($plugin->is_visible()) { | |
706 | // enabled | |
707 | //tied disableIf rule to this select element | |
708 | $mform->addElement('selectyesno', $plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', $plugin->get_name()); | |
709 | $mform->addHelpButton($plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', 'enabled', $plugin->get_subtype() . '_' . $plugin->get_type()); | |
710 | ||
bbd0e548 | 711 | |
cfc81f03 | 712 | $default = get_config($plugin->get_subtype() . '_' . $plugin->get_type(), 'default'); |
bbd0e548 DW |
713 | if ($plugin->get_config('enabled') !== false) { |
714 | $default = $plugin->is_enabled(); | |
715 | } | |
716 | $mform->setDefault($plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', $default); | |
717 | ||
718 | $plugin->get_settings($mform); | |
719 | ||
720 | } | |
721 | ||
722 | } | |
723 | ||
724 | ||
725 | /** | |
726 | * Add settings to edit plugin form | |
727 | * | |
728 | * @param MoodleQuickForm $mform The form to add the configuration settings to. This form is modified directly (not returned) | |
729 | * @return void | |
730 | */ | |
731 | public function add_all_plugin_settings(MoodleQuickForm $mform) { | |
732 | $mform->addElement('header', 'general', get_string('submissionsettings', 'assign')); | |
733 | ||
734 | foreach ($this->submissionplugins as $plugin) { | |
735 | $this->add_plugin_settings($plugin, $mform); | |
736 | ||
737 | } | |
738 | $mform->addElement('header', 'general', get_string('feedbacksettings', 'assign')); | |
739 | foreach ($this->feedbackplugins as $plugin) { | |
740 | $this->add_plugin_settings($plugin, $mform); | |
741 | } | |
742 | } | |
743 | ||
744 | /** | |
745 | * Allow each plugin an opportunity to update the defaultvalues | |
746 | * passed in to the settings form (needed to set up draft areas for | |
747 | * editor and filemanager elements) | |
748 | * @param array $defaultvalues | |
749 | */ | |
750 | public function plugin_data_preprocessing(&$defaultvalues) { | |
751 | foreach ($this->submissionplugins as $plugin) { | |
752 | if ($plugin->is_visible()) { | |
753 | $plugin->data_preprocessing($defaultvalues); | |
754 | } | |
755 | } | |
756 | foreach ($this->feedbackplugins as $plugin) { | |
757 | if ($plugin->is_visible()) { | |
758 | $plugin->data_preprocessing($defaultvalues); | |
759 | } | |
760 | } | |
761 | } | |
762 | ||
763 | /** | |
764 | * Get the name of the current module. | |
765 | * | |
766 | * @return string the module name (Assignment) | |
767 | */ | |
768 | protected function get_module_name() { | |
769 | if (isset(self::$modulename)) { | |
770 | return self::$modulename; | |
771 | } | |
772 | self::$modulename = get_string('modulename', 'assign'); | |
773 | return self::$modulename; | |
774 | } | |
775 | ||
776 | /** | |
777 | * Get the plural name of the current module. | |
778 | * | |
779 | * @return string the module name plural (Assignments) | |
780 | */ | |
781 | protected function get_module_name_plural() { | |
782 | if (isset(self::$modulenameplural)) { | |
783 | return self::$modulenameplural; | |
784 | } | |
785 | self::$modulenameplural = get_string('modulenameplural', 'assign'); | |
786 | return self::$modulenameplural; | |
787 | } | |
788 | ||
789 | /** | |
790 | * Has this assignment been constructed from an instance? | |
791 | * | |
792 | * @return bool | |
793 | */ | |
794 | public function has_instance() { | |
795 | return $this->instance || $this->get_course_module(); | |
796 | } | |
797 | ||
798 | /** | |
799 | * Get the settings for the current instance of this assignment | |
800 | * | |
801 | * @return stdClass The settings | |
802 | */ | |
803 | public function get_instance() { | |
804 | global $DB; | |
805 | if ($this->instance) { | |
806 | return $this->instance; | |
807 | } | |
808 | if ($this->get_course_module()) { | |
809 | $this->instance = $DB->get_record('assign', array('id' => $this->get_course_module()->instance), '*', MUST_EXIST); | |
810 | } | |
811 | if (!$this->instance) { | |
812 | throw new coding_exception('Improper use of the assignment class. Cannot load the assignment record.'); | |
813 | } | |
814 | return $this->instance; | |
815 | } | |
816 | ||
817 | /** | |
818 | * Get the context of the current course | |
819 | * @return mixed context|null The course context | |
820 | */ | |
821 | public function get_course_context() { | |
822 | if (!$this->context && !$this->course) { | |
823 | throw new coding_exception('Improper use of the assignment class. Cannot load the course context.'); | |
824 | } | |
825 | if ($this->context) { | |
826 | return $this->context->get_course_context(); | |
827 | } else { | |
828 | return context_course::instance($this->course->id); | |
829 | } | |
830 | } | |
831 | ||
832 | ||
833 | /** | |
834 | * Get the current course module | |
835 | * | |
836 | * @return mixed stdClass|null The course module | |
837 | */ | |
838 | public function get_course_module() { | |
839 | if ($this->coursemodule) { | |
840 | return $this->coursemodule; | |
841 | } | |
842 | if (!$this->context) { | |
843 | return null; | |
844 | } | |
845 | ||
846 | if ($this->context->contextlevel == CONTEXT_MODULE) { | |
847 | $this->coursemodule = get_coursemodule_from_id('assign', $this->context->instanceid, 0, false, MUST_EXIST); | |
848 | return $this->coursemodule; | |
849 | } | |
850 | return null; | |
851 | } | |
852 | ||
853 | /** | |
854 | * Get context module | |
855 | * | |
856 | * @return context | |
857 | */ | |
858 | public function get_context() { | |
859 | return $this->context; | |
860 | } | |
861 | ||
862 | /** | |
863 | * Get the current course | |
864 | * @return mixed stdClass|null The course | |
865 | */ | |
866 | public function get_course() { | |
867 | global $DB; | |
868 | if ($this->course) { | |
869 | return $this->course; | |
870 | } | |
871 | ||
872 | if (!$this->context) { | |
873 | return null; | |
874 | } | |
875 | $this->course = $DB->get_record('course', array('id' => $this->get_course_context()->instanceid), '*', MUST_EXIST); | |
876 | return $this->course; | |
877 | } | |
878 | ||
879 | /** | |
880 | * Return a grade in user-friendly form, whether it's a scale or not | |
881 | * | |
9682626e | 882 | * @param mixed $grade int|null |
bf78ebd6 | 883 | * @param boolean $editing Are we allowing changes to this grade? |
2a4fbc32 SH |
884 | * @param int $userid The user id the grade belongs to |
885 | * @param int $modified Timestamp from when the grade was last modified | |
bbd0e548 DW |
886 | * @return string User-friendly representation of grade |
887 | */ | |
bf78ebd6 | 888 | public function display_grade($grade, $editing, $userid=0, $modified=0) { |
bbd0e548 DW |
889 | global $DB; |
890 | ||
891 | static $scalegrades = array(); | |
892 | ||
2a4fbc32 SH |
893 | if ($this->get_instance()->grade >= 0) { |
894 | // Normal number | |
e7ade405 | 895 | if ($editing && $this->get_instance()->grade > 0) { |
2d8a9ce9 DW |
896 | if ($grade < 0) { |
897 | $displaygrade = ''; | |
898 | } else { | |
899 | $displaygrade = format_float($grade); | |
900 | } | |
502be9a0 | 901 | $o = '<input type="text" name="quickgrade_' . $userid . '" value="' . $displaygrade . '" size="6" maxlength="10" class="quickgrade"/>'; |
bf78ebd6 | 902 | $o .= ' / ' . format_float($this->get_instance()->grade,2); |
2a4fbc32 | 903 | $o .= '<input type="hidden" name="grademodified_' . $userid . '" value="' . $modified . '"/>'; |
bf78ebd6 | 904 | return $o; |
bbd0e548 | 905 | } else { |
a1e54f4d DW |
906 | if ($grade == -1 || $grade === null) { |
907 | return '-'; | |
908 | } else { | |
909 | return format_float(($grade),2) .' / '. format_float($this->get_instance()->grade,2); | |
910 | } | |
bbd0e548 DW |
911 | } |
912 | ||
2a4fbc32 SH |
913 | } else { |
914 | // Scale | |
bbd0e548 DW |
915 | if (empty($this->cache['scale'])) { |
916 | if ($scale = $DB->get_record('scale', array('id'=>-($this->get_instance()->grade)))) { | |
917 | $this->cache['scale'] = make_menu_from_list($scale->scale); | |
918 | } else { | |
919 | return '-'; | |
920 | } | |
921 | } | |
bf78ebd6 DW |
922 | if ($editing) { |
923 | $o = '<select name="quickgrade_' . $userid . '" class="quickgrade">'; | |
924 | $o .= '<option value="-1">' . get_string('nograde') . '</option>'; | |
925 | foreach ($this->cache['scale'] as $optionid => $option) { | |
926 | $selected = ''; | |
927 | if ($grade == $optionid) { | |
928 | $selected = 'selected="selected"'; | |
929 | } | |
930 | $o .= '<option value="' . $optionid . '" ' . $selected . '>' . $option . '</option>'; | |
931 | } | |
932 | $o .= '</select>'; | |
933 | $o .= '<input type="hidden" name="grademodified_' . $userid . '" value="' . $modified . '"/>'; | |
934 | return $o; | |
935 | } else { | |
936 | $scaleid = (int)$grade; | |
937 | if (isset($this->cache['scale'][$scaleid])) { | |
938 | return $this->cache['scale'][$scaleid]; | |
939 | } | |
940 | return '-'; | |
bbd0e548 | 941 | } |
bbd0e548 DW |
942 | } |
943 | } | |
944 | ||
945 | /** | |
946 | * Load a list of users enrolled in the current course with the specified permission and group (0 for no group) | |
947 | * | |
948 | * @param int $currentgroup | |
949 | * @param bool $idsonly | |
950 | * @return array List of user records | |
951 | */ | |
952 | public function list_participants($currentgroup, $idsonly) { | |
953 | if ($idsonly) { | |
954 | return get_enrolled_users($this->context, "mod/assign:submit", $currentgroup, 'u.id'); | |
955 | } else { | |
956 | return get_enrolled_users($this->context, "mod/assign:submit", $currentgroup); | |
957 | } | |
958 | } | |
959 | ||
960 | /** | |
961 | * Load a count of users enrolled in the current course with the specified permission and group (0 for no group) | |
962 | * | |
963 | * @param int $currentgroup | |
964 | * @return int number of matching users | |
965 | */ | |
966 | public function count_participants($currentgroup) { | |
967 | return count_enrolled_users($this->context, "mod/assign:submit", $currentgroup); | |
968 | } | |
969 | ||
f70079b9 DW |
970 | /** |
971 | * Load a count of users submissions in the current module that require grading | |
972 | * This means the submission modification time is more recent than the | |
973 | * grading modification time. | |
974 | * | |
975 | * @return int number of matching submissions | |
976 | */ | |
977 | public function count_submissions_need_grading() { | |
978 | global $DB; | |
979 | ||
980 | $params = array($this->get_course_module()->instance); | |
981 | ||
982 | return $DB->count_records_sql("SELECT COUNT('x') | |
983 | FROM {assign_submission} s | |
984 | LEFT JOIN {assign_grades} g ON s.assignment = g.assignment AND s.userid = g.userid | |
985 | WHERE s.assignment = ? | |
986 | AND s.timemodified IS NOT NULL | |
987 | AND (s.timemodified > g.timemodified OR g.timemodified IS NULL)", | |
988 | $params); | |
989 | } | |
990 | ||
bbd0e548 DW |
991 | /** |
992 | * Load a count of users enrolled in the current course with the specified permission and group (optional) | |
993 | * | |
994 | * @param string $status The submission status - should match one of the constants | |
995 | * @return int number of matching submissions | |
996 | */ | |
997 | public function count_submissions_with_status($status) { | |
998 | global $DB; | |
999 | return $DB->count_records_sql("SELECT COUNT('x') | |
1000 | FROM {assign_submission} | |
1001 | WHERE assignment = ? AND | |
1002 | status = ?", array($this->get_course_module()->instance, $status)); | |
1003 | } | |
1004 | ||
1005 | /** | |
1006 | * Utility function to get the userid for every row in the grading table | |
1007 | * so the order can be frozen while we iterate it | |
1008 | * | |
1009 | * @return array An array of userids | |
1010 | */ | |
1011 | private function get_grading_userid_list(){ | |
1012 | $filter = get_user_preferences('assign_filter', ''); | |
bf78ebd6 | 1013 | $table = new assign_grading_table($this, 0, $filter, 0, false); |
bbd0e548 DW |
1014 | |
1015 | $useridlist = $table->get_column_data('userid'); | |
1016 | ||
1017 | return $useridlist; | |
1018 | } | |
1019 | ||
1020 | ||
1021 | /** | |
1022 | * Utility function get the userid based on the row number of the grading table. | |
1023 | * This takes into account any active filters on the table. | |
1024 | * | |
1025 | * @param int $num The row number of the user | |
1026 | * @param bool $last This is set to true if this is the last user in the table | |
1027 | * @return mixed The user id of the matching user or false if there was an error | |
1028 | */ | |
1029 | private function get_userid_for_row($num, $last){ | |
1030 | if (!array_key_exists('userid_for_row', $this->cache)) { | |
1031 | $this->cache['userid_for_row'] = array(); | |
1032 | } | |
1033 | if (array_key_exists($num, $this->cache['userid_for_row'])) { | |
1034 | list($userid, $last) = $this->cache['userid_for_row'][$num]; | |
1035 | return $userid; | |
1036 | } | |
1037 | ||
1038 | $filter = get_user_preferences('assign_filter', ''); | |
bf78ebd6 | 1039 | $table = new assign_grading_table($this, 0, $filter, 0, false); |
bbd0e548 DW |
1040 | |
1041 | $userid = $table->get_cell_data($num, 'userid', $last); | |
1042 | ||
1043 | $this->cache['userid_for_row'][$num] = array($userid, $last); | |
1044 | return $userid; | |
1045 | } | |
1046 | ||
1047 | /** | |
1048 | * Return all assignment submissions by ENROLLED students (even empty) | |
1049 | * | |
1050 | * @param string $sort optional field names for the ORDER BY in the sql query | |
1051 | * @param string $dir optional specifying the sort direction, defaults to DESC | |
1052 | * @return array The submission objects indexed by id | |
1053 | */ | |
1054 | private function get_all_submissions( $sort="", $dir="DESC") { | |
1055 | global $CFG, $DB; | |
1056 | ||
1057 | if ($sort == "lastname" or $sort == "firstname") { | |
1058 | $sort = "u.$sort $dir"; | |
1059 | } else if (empty($sort)) { | |
1060 | $sort = "a.timemodified DESC"; | |
1061 | } else { | |
1062 | $sort = "a.$sort $dir"; | |
1063 | } | |
1064 | ||
1065 | return $DB->get_records_sql("SELECT a.* | |
1066 | FROM {assign_submission} a, {user} u | |
1067 | WHERE u.id = a.userid | |
1068 | AND a.assignment = ? | |
1069 | ORDER BY $sort", array($this->get_instance()->id)); | |
1070 | ||
1071 | } | |
1072 | ||
1073 | /** | |
1074 | * Generate zip file from array of given files | |
1075 | * | |
1076 | * @param array $filesforzipping - array of files to pass into archive_to_pathname - this array is indexed by the final file name and each element in the array is an instance of a stored_file object | |
1077 | * @return path of temp file - note this returned file does not have a .zip extension - it is a temp file. | |
1078 | */ | |
1079 | private function pack_files($filesforzipping) { | |
1080 | global $CFG; | |
1081 | //create path for new zip file. | |
1082 | $tempzip = tempnam($CFG->tempdir.'/', 'assignment_'); | |
1083 | //zip files | |
1084 | $zipper = new zip_packer(); | |
1085 | if ($zipper->archive_to_pathname($filesforzipping, $tempzip)) { | |
1086 | return $tempzip; | |
1087 | } | |
1088 | return false; | |
1089 | } | |
1090 | ||
bbd0e548 | 1091 | /** |
3f7b501e SH |
1092 | * Finds all assignment notifications that have yet to be mailed out, and mails them. |
1093 | * | |
1094 | * Cron function to be run periodically according to the moodle cron | |
bbd0e548 DW |
1095 | * |
1096 | * @return bool | |
1097 | */ | |
1098 | static function cron() { | |
3f7b501e | 1099 | global $DB; |
75f87a57 DW |
1100 | |
1101 | // only ever send a max of one days worth of updates | |
1102 | $yesterday = time() - (24 * 3600); | |
1103 | $timenow = time(); | |
1104 | ||
3f7b501e SH |
1105 | // Collect all submissions from the past 24 hours that require mailing. |
1106 | $sql = "SELECT s.*, a.course, a.name, g.*, g.id as gradeid, g.timemodified as lastmodified | |
1107 | FROM {assign} a | |
1108 | JOIN {assign_grades} g ON g.assignment = a.id | |
1109 | LEFT JOIN {assign_submission} s ON s.assignment = a.id AND s.userid = g.userid | |
1110 | WHERE g.timemodified >= :yesterday AND | |
1111 | g.timemodified <= :today AND | |
1112 | g.mailed = 0"; | |
1113 | $params = array('yesterday' => $yesterday, 'today' => $timenow); | |
1114 | $submissions = $DB->get_records_sql($sql, $params); | |
75f87a57 | 1115 | |
c8314005 SH |
1116 | if (empty($submissions)) { |
1117 | mtrace('done.'); | |
1118 | return true; | |
1119 | } | |
1120 | ||
75f87a57 DW |
1121 | mtrace('Processing ' . count($submissions) . ' assignment submissions ...'); |
1122 | ||
3f7b501e SH |
1123 | // Preload courses we are going to need those. |
1124 | $courseids = array(); | |
1125 | foreach ($submissions as $submission) { | |
1126 | $courseids[] = $submission->course; | |
1127 | } | |
1128 | // Filter out duplicates | |
1129 | $courseids = array_unique($courseids); | |
1130 | $ctxselect = context_helper::get_preload_record_columns_sql('ctx'); | |
1131 | list($courseidsql, $params) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED); | |
1132 | $sql = "SELECT c.*, {$ctxselect} | |
1133 | FROM {course} c | |
1134 | LEFT JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel | |
1135 | WHERE c.id {$courseidsql}"; | |
1136 | $params['contextlevel'] = CONTEXT_COURSE; | |
1137 | $courses = $DB->get_records_sql($sql, $params); | |
1138 | // Clean up... this could go on for a while. | |
1139 | unset($courseids); | |
1140 | unset($ctxselect); | |
1141 | unset($courseidsql); | |
1142 | unset($params); | |
1143 | ||
1144 | // Simple array we'll use for caching modules. | |
1145 | $modcache = array(); | |
1146 | ||
c1d09c6f | 1147 | // Message students about new feedback |
75f87a57 DW |
1148 | foreach ($submissions as $submission) { |
1149 | ||
1150 | mtrace("Processing assignment submission $submission->id ..."); | |
1151 | ||
1152 | // do not cache user lookups - could be too many | |
3f7b501e | 1153 | if (!$user = $DB->get_record("user", array("id"=>$submission->userid))) { |
75f87a57 DW |
1154 | mtrace("Could not find user $submission->userid"); |
1155 | continue; | |
1156 | } | |
1157 | ||
1158 | // use a cache to prevent the same DB queries happening over and over | |
3f7b501e SH |
1159 | if (!array_key_exists($submission->course, $courses)) { |
1160 | mtrace("Could not find course $submission->course"); | |
1161 | continue; | |
1162 | } | |
1163 | $course = $courses[$submission->course]; | |
1164 | if (isset($course->ctxid)) { | |
1165 | // Context has not yet been preloaded. Do so now. | |
1166 | context_helper::preload_from_record($course); | |
75f87a57 DW |
1167 | } |
1168 | ||
3f7b501e SH |
1169 | // Override the language and timezone of the "current" user, so that |
1170 | // mail is customised for the receiver. | |
75f87a57 DW |
1171 | cron_setup_user($user, $course); |
1172 | ||
1173 | // context lookups are already cached | |
3f7b501e | 1174 | $coursecontext = context_course::instance($course->id); |
75f87a57 | 1175 | if (!is_enrolled($coursecontext, $user->id)) { |
3f7b501e | 1176 | $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext)); |
75f87a57 DW |
1177 | mtrace(fullname($user)." not an active participant in " . $courseshortname); |
1178 | continue; | |
1179 | } | |
1180 | ||
3f7b501e | 1181 | if (!$grader = $DB->get_record("user", array("id"=>$submission->grader))) { |
75f87a57 DW |
1182 | mtrace("Could not find grader $submission->grader"); |
1183 | continue; | |
1184 | } | |
1185 | ||
3f7b501e | 1186 | if (!array_key_exists($submission->assignment, $modcache)) { |
75f87a57 DW |
1187 | if (! $mod = get_coursemodule_from_instance("assign", $submission->assignment, $course->id)) { |
1188 | mtrace("Could not find course module for assignment id $submission->assignment"); | |
1189 | continue; | |
1190 | } | |
3f7b501e SH |
1191 | $modcache[$submission->assignment] = $mod; |
1192 | } else { | |
1193 | $mod = $modcache[$submission->assignment]; | |
75f87a57 | 1194 | } |
75f87a57 DW |
1195 | // context lookups are already cached |
1196 | $contextmodule = context_module::instance($mod->id); | |
bbd0e548 | 1197 | |
3f7b501e SH |
1198 | if (!$mod->visible) { |
1199 | // Hold mail notification for hidden assignments until later | |
75f87a57 DW |
1200 | continue; |
1201 | } | |
1202 | ||
1203 | // need to send this to the student | |
3f7b501e | 1204 | $messagetype = 'feedbackavailable'; |
f750cf71 | 1205 | $eventtype = 'assign_notification'; |
3f7b501e SH |
1206 | $updatetime = $submission->lastmodified; |
1207 | $modulename = get_string('modulename', 'assign'); | |
1208 | self::send_assignment_notification($grader, $user, $messagetype, $eventtype, $updatetime, $mod, $contextmodule, $course, $modulename, $submission->name); | |
75f87a57 DW |
1209 | |
1210 | $grade = new stdClass(); | |
1211 | $grade->id = $submission->gradeid; | |
1212 | $grade->mailed = 1; | |
1213 | $DB->update_record('assign_grades', $grade); | |
1214 | ||
1215 | mtrace('Done'); | |
1216 | } | |
1217 | mtrace('Done processing ' . count($submissions) . ' assignment submissions'); | |
1218 | ||
1219 | cron_setup_user(); | |
3f7b501e SH |
1220 | |
1221 | // Free up memory just to be sure | |
1222 | unset($courses); | |
1223 | unset($modcache); | |
bbd0e548 DW |
1224 | |
1225 | return true; | |
1226 | } | |
1227 | ||
1228 | /** | |
1229 | * Update a grade in the grade table for the assignment and in the gradebook | |
1230 | * | |
1231 | * @param stdClass $grade a grade record keyed on id | |
1232 | * @return bool true for success | |
1233 | */ | |
1234 | private function update_grade($grade) { | |
1235 | global $DB; | |
1236 | ||
1237 | $grade->timemodified = time(); | |
1238 | ||
1239 | if ($grade->grade && $grade->grade != -1) { | |
1240 | if ($this->get_instance()->grade > 0) { | |
1241 | if (!is_numeric($grade->grade)) { | |
1242 | return false; | |
1243 | } else if ($grade->grade > $this->get_instance()->grade) { | |
1244 | return false; | |
1245 | } else if ($grade->grade < 0) { | |
1246 | return false; | |
1247 | } | |
1248 | } else { | |
1249 | // this is a scale | |
1250 | if ($scale = $DB->get_record('scale', array('id' => -($this->get_instance()->grade)))) { | |
1251 | $scaleoptions = make_menu_from_list($scale->scale); | |
1252 | if (!array_key_exists((int) $grade->grade, $scaleoptions)) { | |
1253 | return false; | |
1254 | } | |
1255 | } | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | $result = $DB->update_record('assign_grades', $grade); | |
1260 | if ($result) { | |
1261 | $this->gradebook_item_update(null, $grade); | |
1262 | } | |
1263 | return $result; | |
1264 | } | |
1265 | ||
1266 | /** | |
1267 | * display the submission that is used by a plugin | |
1268 | * Uses url parameters 'sid', 'gid' and 'plugin' | |
1269 | * @param string $pluginsubtype | |
1270 | * @return string | |
1271 | */ | |
1272 | private function view_plugin_content($pluginsubtype) { | |
1273 | global $USER; | |
1274 | ||
1275 | $o = ''; | |
1276 | ||
1277 | $submissionid = optional_param('sid', 0, PARAM_INT); | |
1278 | $gradeid = optional_param('gid', 0, PARAM_INT); | |
1279 | $plugintype = required_param('plugin', PARAM_TEXT); | |
1280 | $item = null; | |
1281 | if ($pluginsubtype == 'assignsubmission') { | |
1282 | $plugin = $this->get_submission_plugin_by_type($plugintype); | |
1283 | if ($submissionid <= 0) { | |
1284 | throw new coding_exception('Submission id should not be 0'); | |
1285 | } | |
1286 | $item = $this->get_submission($submissionid); | |
1287 | ||
1288 | // permissions | |
1289 | if ($item->userid != $USER->id) { | |
1290 | require_capability('mod/assign:grade', $this->context); | |
1291 | } | |
1292 | $o .= $this->output->render(new assign_header($this->get_instance(), | |
1293 | $this->get_context(), | |
1294 | $this->show_intro(), | |
1295 | $this->get_course_module()->id, | |
1296 | $plugin->get_name())); | |
1297 | $o .= $this->output->render(new assign_submission_plugin_submission($plugin, | |
1298 | $item, | |
1299 | assign_submission_plugin_submission::FULL, | |
1300 | $this->get_course_module()->id, | |
1301 | $this->get_return_action(), | |
1302 | $this->get_return_params())); | |
1303 | ||
1304 | $this->add_to_log('view submission', get_string('viewsubmissionforuser', 'assign', $item->userid)); | |
1305 | } else { | |
1306 | $plugin = $this->get_feedback_plugin_by_type($plugintype); | |
1307 | if ($gradeid <= 0) { | |
1308 | throw new coding_exception('Grade id should not be 0'); | |
1309 | } | |
1310 | $item = $this->get_grade($gradeid); | |
1311 | // permissions | |
1312 | if ($item->userid != $USER->id) { | |
1313 | require_capability('mod/assign:grade', $this->context); | |
1314 | } | |
1315 | $o .= $this->output->render(new assign_header($this->get_instance(), | |
1316 | $this->get_context(), | |
1317 | $this->show_intro(), | |
1318 | $this->get_course_module()->id, | |
1319 | $plugin->get_name())); | |
1320 | $o .= $this->output->render(new assign_feedback_plugin_feedback($plugin, | |
1321 | $item, | |
1322 | assign_feedback_plugin_feedback::FULL, | |
1323 | $this->get_course_module()->id, | |
1324 | $this->get_return_action(), | |
1325 | $this->get_return_params())); | |
1326 | $this->add_to_log('view feedback', get_string('viewfeedbackforuser', 'assign', $item->userid)); | |
1327 | } | |
1328 | ||
1329 | ||
1330 | $o .= $this->view_return_links(); | |
1331 | ||
1332 | $o .= $this->view_footer(); | |
1333 | return $o; | |
1334 | } | |
1335 | ||
1336 | /** | |
1337 | * render the content in editor that is often used by plugin | |
1338 | * | |
1339 | * @param string $filearea | |
1340 | * @param int $submissionid | |
1341 | * @param string $plugintype | |
1342 | * @param string $editor | |
1343 | * @param string $component | |
1344 | * @return string | |
1345 | */ | |
1346 | public function render_editor_content($filearea, $submissionid, $plugintype, $editor, $component) { | |
1347 | global $CFG; | |
1348 | ||
1349 | $result = ''; | |
1350 | ||
1351 | $plugin = $this->get_submission_plugin_by_type($plugintype); | |
1352 | ||
1353 | $text = $plugin->get_editor_text($editor, $submissionid); | |
1354 | $format = $plugin->get_editor_format($editor, $submissionid); | |
1355 | ||
1356 | $finaltext = file_rewrite_pluginfile_urls($text, 'pluginfile.php', $this->get_context()->id, $component, $filearea, $submissionid); | |
1357 | $result .= format_text($finaltext, $format, array('overflowdiv' => true, 'context' => $this->get_context())); | |
1358 | ||
1359 | ||
1360 | ||
1361 | if ($CFG->enableportfolios) { | |
1362 | require_once($CFG->libdir . '/portfoliolib.php'); | |
1363 | ||
1364 | $button = new portfolio_add_button(); | |
1365 | $button->set_callback_options('assign_portfolio_caller', array('cmid' => $this->get_course_module()->id, 'sid' => $submissionid, 'plugin' => $plugintype, 'editor' => $editor, 'area'=>$filearea), '/mod/assign/portfolio_callback.php'); | |
1366 | $fs = get_file_storage(); | |
1367 | ||
1368 | if ($files = $fs->get_area_files($this->context->id, $component,$filearea, $submissionid, "timemodified", false)) { | |
1369 | $button->set_formats(PORTFOLIO_FORMAT_RICHHTML); | |
1370 | } else { | |
1371 | $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML); | |
1372 | } | |
1373 | $result .= $button->to_html(); | |
1374 | } | |
1375 | return $result; | |
1376 | } | |
1377 | ||
bf78ebd6 DW |
1378 | /** |
1379 | * Display a grading error | |
1380 | * | |
1381 | * @param string $message - The description of the result | |
1382 | * @return string | |
1383 | */ | |
1384 | private function view_quickgrading_result($message) { | |
1385 | $o = ''; | |
1386 | $o .= $this->output->render(new assign_header($this->get_instance(), | |
1387 | $this->get_context(), | |
1388 | $this->show_intro(), | |
1389 | $this->get_course_module()->id, | |
1390 | get_string('quickgradingresult', 'assign'))); | |
1391 | $o .= $this->output->render(new assign_quickgrading_result($message, $this->get_course_module()->id)); | |
1392 | $o .= $this->view_footer(); | |
1393 | return $o; | |
1394 | } | |
bbd0e548 DW |
1395 | |
1396 | /** | |
1397 | * Display the page footer | |
1398 | * | |
bf78ebd6 | 1399 | * @return string |
bbd0e548 DW |
1400 | */ |
1401 | private function view_footer() { | |
1402 | return $this->output->render_footer(); | |
1403 | } | |
1404 | ||
1405 | /** | |
1406 | * Does this user have grade permission for this assignment | |
1407 | * | |
1408 | * @return bool | |
1409 | */ | |
1410 | private function can_grade() { | |
1411 | // Permissions check | |
1412 | if (!has_capability('mod/assign:grade', $this->context)) { | |
1413 | return false; | |
1414 | } | |
1415 | ||
1416 | return true; | |
1417 | } | |
1418 | ||
1419 | /** | |
1420 | * Download a zip file of all assignment submissions | |
1421 | * | |
1422 | * @return void | |
1423 | */ | |
1424 | private function download_submissions() { | |
1425 | global $CFG,$DB; | |
1426 | ||
1427 | // more efficient to load this here | |
1428 | require_once($CFG->libdir.'/filelib.php'); | |
1429 | ||
1430 | // load all submissions | |
1431 | $submissions = $this->get_all_submissions('',''); | |
1432 | ||
1433 | if (empty($submissions)) { | |
1434 | print_error('errornosubmissions', 'assign'); | |
1435 | return; | |
1436 | } | |
1437 | ||
1438 | // build a list of files to zip | |
1439 | $filesforzipping = array(); | |
1440 | $fs = get_file_storage(); | |
1441 | ||
1442 | $groupmode = groups_get_activity_groupmode($this->get_course_module()); | |
1443 | $groupid = 0; // All users | |
1444 | $groupname = ''; | |
1445 | if ($groupmode) { | |
1446 | $groupid = groups_get_activity_group($this->get_course_module(), true); | |
1447 | $groupname = groups_get_group_name($groupid).'-'; | |
1448 | } | |
1449 | ||
1450 | // construct the zip file name | |
1451 | $filename = str_replace(' ', '_', clean_filename($this->get_course()->shortname.'-'.$this->get_instance()->name.'-'.$groupname.$this->get_course_module()->id.".zip")); //name of new zip file. | |
1452 | ||
1453 | // get all the files for each submission | |
1454 | foreach ($submissions as $submission) { | |
1455 | $userid = $submission->userid; //get userid | |
1456 | if ((groups_is_member($groupid,$userid) or !$groupmode or !$groupid)) { | |
1457 | // get the plugins to add their own files to the zip | |
1458 | ||
1459 | $user = $DB->get_record("user", array("id"=>$userid),'id,username,firstname,lastname', MUST_EXIST); | |
1460 | ||
1461 | $prefix = clean_filename(fullname($user) . "_" .$userid . "_"); | |
1462 | ||
1463 | foreach ($this->submissionplugins as $plugin) { | |
1464 | if ($plugin->is_enabled() && $plugin->is_visible()) { | |
1465 | $pluginfiles = $plugin->get_files($submission); | |
1466 | ||
1467 | ||
1468 | foreach ($pluginfiles as $zipfilename => $file) { | |
1469 | $filesforzipping[$prefix . $zipfilename] = $file; | |
1470 | } | |
1471 | } | |
1472 | } | |
1473 | ||
1474 | } | |
1475 | } // end of foreach loop | |
1476 | if ($zipfile = $this->pack_files($filesforzipping)) { | |
1477 | $this->add_to_log('download all submissions', get_string('downloadall', 'assign')); | |
1478 | send_temp_file($zipfile, $filename); //send file and delete after sending. | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | /** | |
1483 | * Util function to add a message to the log | |
1484 | * | |
1485 | * @param string $action The current action | |
1486 | * @param string $info A detailed description of the change. But no more than 255 characters. | |
1487 | * @param string $url The url to the assign module instance. | |
1488 | * @return void | |
1489 | */ | |
1490 | public function add_to_log($action = '', $info = '', $url='') { | |
1491 | global $USER; | |
1492 | ||
1493 | $fullurl = 'view.php?id=' . $this->get_course_module()->id; | |
1494 | if ($url != '') { | |
1495 | $fullurl .= '&' . $url; | |
1496 | } | |
1497 | ||
1498 | add_to_log($this->get_course()->id, 'assign', $action, $fullurl, $info, $this->get_course_module()->id, $USER->id); | |
1499 | } | |
1500 | ||
1501 | /** | |
1502 | * Load the submission object for a particular user, optionally creating it if required | |
1503 | * | |
1504 | * @param int $userid The id of the user whose submission we want or 0 in which case USER->id is used | |
1505 | * @param bool $create optional Defaults to false. If set to true a new submission object will be created in the database | |
1506 | * @return stdClass The submission | |
1507 | */ | |
1508 | private function get_user_submission($userid, $create) { | |
1509 | global $DB, $USER; | |
1510 | ||
1511 | if (!$userid) { | |
1512 | $userid = $USER->id; | |
1513 | } | |
1514 | // if the userid is not null then use userid | |
1515 | $submission = $DB->get_record('assign_submission', array('assignment'=>$this->get_instance()->id, 'userid'=>$userid)); | |
1516 | ||
1517 | if ($submission) { | |
1518 | return $submission; | |
1519 | } | |
1520 | if ($create) { | |
1521 | $submission = new stdClass(); | |
1522 | $submission->assignment = $this->get_instance()->id; | |
1523 | $submission->userid = $userid; | |
1524 | $submission->timecreated = time(); | |
1525 | $submission->timemodified = $submission->timecreated; | |
1526 | ||
1527 | if ($this->get_instance()->submissiondrafts) { | |
1528 | $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT; | |
1529 | } else { | |
1530 | $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; | |
1531 | } | |
1532 | $sid = $DB->insert_record('assign_submission', $submission); | |
1533 | $submission->id = $sid; | |
1534 | return $submission; | |
1535 | } | |
1536 | return false; | |
1537 | } | |
1538 | ||
1539 | /** | |
1540 | * Load the submission object from it's id | |
1541 | * | |
1542 | * @param int $submissionid The id of the submission we want | |
1543 | * @return stdClass The submission | |
1544 | */ | |
1545 | private function get_submission($submissionid) { | |
1546 | global $DB; | |
1547 | ||
1548 | return $DB->get_record('assign_submission', array('assignment'=>$this->get_instance()->id, 'id'=>$submissionid), '*', MUST_EXIST); | |
1549 | } | |
1550 | ||
1551 | /** | |
1552 | * This will retrieve a grade object from the db, optionally creating it if required | |
1553 | * | |
1554 | * @param int $userid The user we are grading | |
1555 | * @param bool $create If true the grade will be created if it does not exist | |
1556 | * @return stdClass The grade record | |
1557 | */ | |
1558 | private function get_user_grade($userid, $create) { | |
1559 | global $DB, $USER; | |
1560 | ||
1561 | if (!$userid) { | |
1562 | $userid = $USER->id; | |
1563 | } | |
1564 | ||
1565 | // if the userid is not null then use userid | |
1566 | $grade = $DB->get_record('assign_grades', array('assignment'=>$this->get_instance()->id, 'userid'=>$userid)); | |
1567 | ||
1568 | if ($grade) { | |
1569 | return $grade; | |
1570 | } | |
1571 | if ($create) { | |
1572 | $grade = new stdClass(); | |
1573 | $grade->assignment = $this->get_instance()->id; | |
1574 | $grade->userid = $userid; | |
1575 | $grade->timecreated = time(); | |
1576 | $grade->timemodified = $grade->timecreated; | |
1577 | $grade->locked = 0; | |
1578 | $grade->grade = -1; | |
1579 | $grade->grader = $USER->id; | |
1580 | $gid = $DB->insert_record('assign_grades', $grade); | |
1581 | $grade->id = $gid; | |
1582 | return $grade; | |
1583 | } | |
1584 | return false; | |
1585 | } | |
1586 | ||
1587 | /** | |
1588 | * This will retrieve a grade object from the db | |
1589 | * | |
1590 | * @param int $gradeid The id of the grade | |
1591 | * @return stdClass The grade record | |
1592 | */ | |
1593 | private function get_grade($gradeid) { | |
1594 | global $DB; | |
1595 | ||
1596 | return $DB->get_record('assign_grades', array('assignment'=>$this->get_instance()->id, 'id'=>$gradeid), '*', MUST_EXIST); | |
1597 | } | |
1598 | ||
1599 | /** | |
1600 | * Print the grading page for a single user submission | |
1601 | * | |
1602 | * @param moodleform $mform | |
1603 | * @param int $offset | |
1604 | * @return string | |
1605 | */ | |
1606 | private function view_single_grade_page($mform, $offset=0) { | |
1607 | global $DB, $CFG; | |
1608 | ||
1609 | $o = ''; | |
1610 | ||
1611 | // Include grade form | |
1612 | require_once($CFG->dirroot . '/mod/assign/gradeform.php'); | |
1613 | ||
1614 | // Need submit permission to submit an assignment | |
1615 | require_capability('mod/assign:grade', $this->context); | |
1616 | ||
1617 | $o .= $this->output->render(new assign_header($this->get_instance(), | |
1618 | $this->get_context(), false, $this->get_course_module()->id,get_string('grading', 'assign'))); | |
1619 | ||
1620 | $rownum = required_param('rownum', PARAM_INT) + $offset; | |
1621 | $useridlist = optional_param('useridlist', '', PARAM_TEXT); | |
1622 | if ($useridlist) { | |
1623 | $useridlist = explode(',', $useridlist); | |
1624 | } else { | |
1625 | $useridlist = $this->get_grading_userid_list(); | |
1626 | } | |
1627 | $last = false; | |
1628 | $userid = $useridlist[$rownum]; | |
1629 | if ($rownum == count($useridlist) - 1) { | |
1630 | $last = true; | |
1631 | } | |
1632 | // the placement of this is important so can pass the list of userids above | |
1633 | if ($offset) { | |
1634 | $_POST = array(); | |
1635 | } | |
1636 | if(!$userid){ | |
1637 | throw new coding_exception('Row is out of bounds for the current grading table: ' . $rownum); | |
1638 | } | |
1639 | $user = $DB->get_record('user', array('id' => $userid)); | |
1640 | if ($user) { | |
1641 | $o .= $this->output->render(new assign_user_summary($user, $this->get_course()->id, has_capability('moodle/site:viewfullnames', $this->get_course_context()))); | |
1642 | } | |
1643 | $submission = $this->get_user_submission($userid, false); | |
1644 | // get the current grade | |
1645 | $grade = $this->get_user_grade($userid, false); | |
1646 | if ($this->can_view_submission($userid)) { | |
1647 | $gradelocked = ($grade && $grade->locked) || $this->grading_disabled($userid); | |
1648 | $o .= $this->output->render(new assign_submission_status($this->get_instance()->allowsubmissionsfromdate, | |
1649 | $this->get_instance()->alwaysshowdescription, | |
1650 | $submission, | |
1651 | $this->is_any_submission_plugin_enabled(), | |
1652 | $gradelocked, | |
1653 | $this->is_graded($userid), | |
1654 | $this->get_instance()->duedate, | |
1655 | $this->get_submission_plugins(), | |
1656 | $this->get_return_action(), | |
1657 | $this->get_return_params(), | |
1658 | $this->get_course_module()->id, | |
1659 | assign_submission_status::GRADER_VIEW, | |
1660 | false, | |
1661 | false)); | |
1662 | } | |
1663 | if ($grade) { | |
1664 | $data = new stdClass(); | |
2d8a9ce9 | 1665 | if ($grade->grade !== NULL && $grade->grade >= 0) { |
bbd0e548 DW |
1666 | $data->grade = format_float($grade->grade,2); |
1667 | } | |
1668 | } else { | |
1669 | $data = new stdClass(); | |
1670 | $data->grade = ''; | |
1671 | } | |
1672 | ||
1673 | // now show the grading form | |
1674 | if (!$mform) { | |
1675 | $mform = new mod_assign_grade_form(null, array($this, $data, array('rownum'=>$rownum, 'useridlist'=>$useridlist, 'last'=>$last)), 'post', '', array('class'=>'gradeform')); | |
1676 | } | |
1677 | $o .= $this->output->render(new assign_form('gradingform',$mform)); | |
1678 | ||
1679 | $this->add_to_log('view grading form', get_string('viewgradingformforstudent', 'assign', array('id'=>$user->id, 'fullname'=>fullname($user)))); | |
1680 | ||
1681 | $o .= $this->view_footer(); | |
1682 | return $o; | |
1683 | } | |
1684 | ||
1685 | ||
1686 | ||
1687 | /** | |
1688 | * View a link to go back to the previous page. Uses url parameters returnaction and returnparams. | |
1689 | * | |
1690 | * @return string | |
1691 | */ | |
1692 | private function view_return_links() { | |
1693 | ||
1694 | $returnaction = optional_param('returnaction','', PARAM_ALPHA); | |
1695 | $returnparams = optional_param('returnparams','', PARAM_TEXT); | |
1696 | ||
1697 | $params = array(); | |
1698 | parse_str($returnparams, $params); | |
1699 | $params = array_merge( array('id' => $this->get_course_module()->id, 'action' => $returnaction), $params); | |
1700 | ||
1701 | return $this->output->single_button(new moodle_url('/mod/assign/view.php', $params), get_string('back'), 'get'); | |
1702 | ||
1703 | } | |
1704 | ||
1705 | /** | |
1706 | * View the grading table of all submissions for this assignment | |
1707 | * | |
1708 | * @return string | |
1709 | */ | |
1710 | private function view_grading_table() { | |
1711 | global $USER, $CFG; | |
1712 | // Include grading options form | |
1713 | require_once($CFG->dirroot . '/mod/assign/gradingoptionsform.php'); | |
bf78ebd6 | 1714 | require_once($CFG->dirroot . '/mod/assign/quickgradingform.php'); |
bbd0e548 DW |
1715 | require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php'); |
1716 | $o = ''; | |
1717 | ||
1718 | $links = array(); | |
bbd0e548 DW |
1719 | if (has_capability('gradereport/grader:view', $this->get_course_context()) && |
1720 | has_capability('moodle/grade:viewall', $this->get_course_context())) { | |
a1e54f4d | 1721 | $gradebookurl = '/grade/report/grader/index.php?id=' . $this->get_course()->id; |
bbd0e548 DW |
1722 | $links[$gradebookurl] = get_string('viewgradebook', 'assign'); |
1723 | } | |
1724 | if ($this->is_any_submission_plugin_enabled()) { | |
a1e54f4d | 1725 | $downloadurl = '/mod/assign/view.php?id=' . $this->get_course_module()->id . '&action=downloadall'; |
bbd0e548 DW |
1726 | $links[$downloadurl] = get_string('downloadall', 'assign'); |
1727 | } | |
a1e54f4d DW |
1728 | |
1729 | $gradingactions = new url_select($links); | |
bbd0e548 | 1730 | |
bf78ebd6 DW |
1731 | $gradingmanager = get_grading_manager($this->get_context(), 'mod_assign', 'submissions'); |
1732 | ||
bbd0e548 DW |
1733 | $perpage = get_user_preferences('assign_perpage', 10); |
1734 | $filter = get_user_preferences('assign_filter', ''); | |
bf78ebd6 DW |
1735 | $controller = $gradingmanager->get_active_controller(); |
1736 | $showquickgrading = empty($controller); | |
1737 | if (optional_param('action', '', PARAM_ALPHA) == 'saveoptions') { | |
1738 | $quickgrading = optional_param('quickgrading', false, PARAM_BOOL); | |
1739 | set_user_preference('assign_quickgrading', $quickgrading); | |
1740 | } | |
1741 | $quickgrading = get_user_preferences('assign_quickgrading', false); | |
1742 | ||
bbd0e548 DW |
1743 | // print options for changing the filter and changing the number of results per page |
1744 | $gradingoptionsform = new mod_assign_grading_options_form(null, | |
1745 | array('cm'=>$this->get_course_module()->id, | |
1746 | 'contextid'=>$this->context->id, | |
bf78ebd6 | 1747 | 'userid'=>$USER->id, |
44e2f0fe | 1748 | 'submissionsenabled'=>$this->is_any_submission_plugin_enabled(), |
bf78ebd6 DW |
1749 | 'showquickgrading'=>$showquickgrading, |
1750 | 'quickgrading'=>$quickgrading), | |
bbd0e548 DW |
1751 | 'post', '', |
1752 | array('class'=>'gradingoptionsform')); | |
1753 | ||
1754 | $gradingbatchoperationsform = new mod_assign_grading_batch_operations_form(null, | |
1755 | array('cm'=>$this->get_course_module()->id, | |
1756 | 'submissiondrafts'=>$this->get_instance()->submissiondrafts), | |
1757 | 'post', '', | |
1758 | array('class'=>'gradingbatchoperationsform')); | |
1759 | ||
1760 | $gradingoptionsdata = new stdClass(); | |
1761 | $gradingoptionsdata->perpage = $perpage; | |
1762 | $gradingoptionsdata->filter = $filter; | |
1763 | $gradingoptionsform->set_data($gradingoptionsdata); | |
1764 | ||
1765 | // plagiarism update status apearring in the grading book | |
1766 | if (!empty($CFG->enableplagiarism)) { | |
1767 | /** Include plagiarismlib.php */ | |
1768 | require_once($CFG->libdir . '/plagiarismlib.php'); | |
9650334f | 1769 | $o .= plagiarism_update_status($this->get_course(), $this->get_course_module()); |
bbd0e548 DW |
1770 | } |
1771 | ||
a1e54f4d DW |
1772 | $actionformtext = $this->output->render($gradingactions); |
1773 | $o .= $this->output->render(new assign_header($this->get_instance(), | |
1774 | $this->get_context(), false, $this->get_course_module()->id, get_string('grading', 'assign'), $actionformtext)); | |
1775 | $o .= groups_print_activity_menu($this->get_course_module(), $CFG->wwwroot . '/mod/assign/view.php?id=' . $this->get_course_module()->id.'&action=grading', true); | |
1776 | ||
bbd0e548 DW |
1777 | |
1778 | // load and print the table of submissions | |
bf78ebd6 DW |
1779 | if ($showquickgrading && $quickgrading) { |
1780 | $table = $this->output->render(new assign_grading_table($this, $perpage, $filter, 0, true)); | |
1781 | $quickgradingform = new mod_assign_quick_grading_form(null, | |
1782 | array('cm'=>$this->get_course_module()->id, | |
1783 | 'gradingtable'=>$table)); | |
1784 | $o .= $this->output->render(new assign_form('quickgradingform', $quickgradingform)); | |
1785 | } else { | |
1786 | $o .= $this->output->render(new assign_grading_table($this, $perpage, $filter, 0, false)); | |
1787 | } | |
bbd0e548 DW |
1788 | |
1789 | $currentgroup = groups_get_activity_group($this->get_course_module(), true); | |
1790 | $users = array_keys($this->list_participants($currentgroup, true)); | |
1791 | if (count($users) != 0) { | |
1792 | // if no enrolled user in a course then don't display the batch operations feature | |
1793 | $o .= $this->output->render(new assign_form('gradingbatchoperationsform', $gradingbatchoperationsform)); | |
1794 | } | |
a1e54f4d | 1795 | $o .= $this->output->render(new assign_form('gradingoptionsform', $gradingoptionsform, 'M.mod_assign.init_grading_options')); |
bbd0e548 DW |
1796 | return $o; |
1797 | } | |
1798 | ||
1799 | /** | |
1800 | * View entire grading page. | |
1801 | * | |
1802 | * @return string | |
1803 | */ | |
1804 | private function view_grading_page() { | |
1805 | global $CFG; | |
1806 | ||
1807 | $o = ''; | |
1808 | // Need submit permission to submit an assignment | |
1809 | require_capability('mod/assign:grade', $this->context); | |
1810 | require_once($CFG->dirroot . '/mod/assign/gradeform.php'); | |
1811 | ||
1812 | // only load this if it is | |
1813 | ||
bbd0e548 DW |
1814 | $o .= $this->view_grading_table(); |
1815 | ||
1816 | $o .= $this->view_footer(); | |
1817 | $this->add_to_log('view submission grading table', get_string('viewsubmissiongradingtable', 'assign')); | |
1818 | return $o; | |
1819 | } | |
1820 | ||
1821 | /** | |
1822 | * Capture the output of the plagiarism plugins disclosures and return it as a string | |
1823 | * | |
1824 | * @return void | |
1825 | */ | |
1826 | private function plagiarism_print_disclosure() { | |
1827 | global $CFG; | |
1828 | $o = ''; | |
1829 | ||
1830 | if (!empty($CFG->enableplagiarism)) { | |
1831 | /** Include plagiarismlib.php */ | |
1832 | require_once($CFG->libdir . '/plagiarismlib.php'); | |
bbd0e548 | 1833 | |
9650334f | 1834 | $o .= plagiarism_print_disclosure($this->get_course_module()->id); |
bbd0e548 DW |
1835 | } |
1836 | ||
1837 | return $o; | |
1838 | } | |
1839 | ||
1840 | /** | |
1841 | * message for students when assignment submissions have been closed | |
1842 | * | |
1843 | * @return string | |
1844 | */ | |
1845 | private function view_student_error_message() { | |
1846 | global $CFG; | |
1847 | ||
1848 | $o = ''; | |
1849 | // Need submit permission to submit an assignment | |
1850 | require_capability('mod/assign:submit', $this->context); | |
1851 | ||
1852 | $o .= $this->output->render(new assign_header($this->get_instance(), | |
1853 | $this->get_context(), | |
1854 | $this->show_intro(), | |
1855 | $this->get_course_module()->id, | |
1856 | get_string('editsubmission', 'assign'))); | |
1857 | ||
bf78ebd6 | 1858 | $o .= $this->output->notification(get_string('submissionsclosed', 'assign')); |
bbd0e548 DW |
1859 | |
1860 | $o .= $this->view_footer(); | |
1861 | ||
1862 | return $o; | |
1863 | ||
1864 | } | |
1865 | ||
1866 | /** | |
1867 | * View edit submissions page. | |
1868 | * | |
1869 | * @param moodleform $mform | |
1870 | * @return void | |
1871 | */ | |
1872 | private function view_edit_submission_page($mform) { | |
1873 | global $CFG; | |
1874 | ||
1875 | $o = ''; | |
1876 | // Include submission form | |
1877 | require_once($CFG->dirroot . '/mod/assign/submission_form.php'); | |
1878 | // Need submit permission to submit an assignment | |
1879 | require_capability('mod/assign:submit', $this->context); | |
1880 | ||
1881 | if (!$this->submissions_open()) { | |
bf78ebd6 | 1882 | return $this->view_student_error_message(); |
bbd0e548 DW |
1883 | } |
1884 | $o .= $this->output->render(new assign_header($this->get_instance(), | |
1885 | $this->get_context(), | |
1886 | $this->show_intro(), | |
1887 | $this->get_course_module()->id, | |
1888 | get_string('editsubmission', 'assign'))); | |
1889 | $o .= $this->plagiarism_print_disclosure(); | |
1890 | $data = new stdClass(); | |
1891 | ||
1892 | if (!$mform) { | |
1893 | $mform = new mod_assign_submission_form(null, array($this, $data)); | |
1894 | } | |
1895 | ||
1896 | $o .= $this->output->render(new assign_form('editsubmissionform',$mform)); | |
1897 | ||
1898 | $o .= $this->view_footer(); | |
1899 | $this->add_to_log('view submit assignment form', get_string('viewownsubmissionform', 'assign')); | |
1900 | ||
1901 | return $o; | |
1902 | } | |
1903 | ||
1904 | /** | |
1905 | * See if this assignment has a grade yet | |
1906 | * | |
1907 | * @param int $userid | |
1908 | * @return bool | |
1909 | */ | |
1910 | private function is_graded($userid) { | |
1911 | $grade = $this->get_user_grade($userid, false); | |
1912 | if ($grade) { | |
2d8a9ce9 | 1913 | return ($grade->grade !== NULL && $grade->grade >= 0); |
bbd0e548 DW |
1914 | } |
1915 | return false; | |
1916 | } | |
1917 | ||
1918 | ||
1919 | /** | |
1920 | * Perform an access check to see if the current $USER can view this users submission | |
1921 | * | |
1922 | * @param int $userid | |
1923 | * @return bool | |
1924 | */ | |
1925 | public function can_view_submission($userid) { | |
1926 | global $USER; | |
1927 | ||
1928 | if (!is_enrolled($this->get_course_context(), $userid)) { | |
1929 | return false; | |
1930 | } | |
1931 | if ($userid == $USER->id && !has_capability('mod/assign:submit', $this->context)) { | |
1932 | return false; | |
1933 | } | |
1934 | if ($userid != $USER->id && !has_capability('mod/assign:grade', $this->context)) { | |
1935 | return false; | |
1936 | } | |
1937 | return true; | |
1938 | } | |
1939 | ||
1940 | /** | |
1941 | * Ask the user to confirm they want to perform this batch operation | |
1942 | * @return string | |
1943 | */ | |
1944 | private function process_batch_grading_operation() { | |
1945 | global $CFG; | |
1946 | require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php'); | |
1947 | require_sesskey(); | |
1948 | ||
1949 | $gradingbatchoperationsform = new mod_assign_grading_batch_operations_form(null, | |
1950 | array('cm'=>$this->get_course_module()->id, | |
1951 | 'submissiondrafts'=>$this->get_instance()->submissiondrafts), | |
1952 | 'post', '', | |
1953 | array('class'=>'gradingbatchoperationsform')); | |
1954 | ||
1955 | if ($data = $gradingbatchoperationsform->get_data()) { | |
1956 | // get the list of users | |
1957 | $users = $data->selectedusers; | |
1958 | $userlist = explode(',', $users); | |
1959 | ||
1960 | foreach ($userlist as $userid) { | |
1961 | if ($data->operation == 'lock') { | |
1962 | $this->process_lock($userid); | |
1963 | } else if ($data->operation == 'unlock') { | |
1964 | $this->process_unlock($userid); | |
1965 | } else if ($data->operation == 'reverttodraft') { | |
1966 | $this->process_revert_to_draft($userid); | |
1967 | } | |
1968 | } | |
1969 | } | |
1970 | ||
1971 | return true; | |
1972 | } | |
1973 | ||
1974 | /** | |
1975 | * Ask the user to confirm they want to submit their work for grading | |
1976 | * @return string | |
1977 | */ | |
1978 | private function check_submit_for_grading() { | |
1979 | global $USER; | |
1980 | // Check that all of the submission plugins are ready for this submission | |
1981 | $notifications = array(); | |
1982 | $submission = $this->get_user_submission($USER->id, false); | |
1983 | $plugins = $this->get_submission_plugins(); | |
1984 | foreach ($plugins as $plugin) { | |
1985 | if ($plugin->is_enabled() && $plugin->is_visible()) { | |
1986 | $check = $plugin->precheck_submission($submission); | |
1987 | if ($check !== true) { | |
1988 | $notifications[] = $check; | |
1989 | } | |
1990 | } | |
1991 | } | |
1992 | ||
1993 | $o = ''; | |
1994 | $o .= $this->output->header(); | |
1995 | $o .= $this->output->render(new assign_submit_for_grading_page($notifications, $this->get_course_module()->id)); | |
1996 | $o .= $this->view_footer(); | |
1997 | return $o; | |
1998 | } | |
1999 | ||
2000 | /** | |
2001 | * Print 2 tables of information with no action links - | |
2002 | * the submission summary and the grading summary | |
2003 | * | |
2004 | * @param stdClass $user the user to print the report for | |
2005 | * @param bool $showlinks - Return plain text or links to the profile | |
2006 | * @return string - the html summary | |
2007 | */ | |
2008 | public function view_student_summary($user, $showlinks) { | |
2009 | global $CFG, $DB, $PAGE; | |
2010 | ||
2011 | $grade = $this->get_user_grade($user->id, false); | |
2012 | $submission = $this->get_user_submission($user->id, false); | |
2013 | $o = ''; | |
2014 | ||
2015 | if ($this->can_view_submission($user->id)) { | |
2016 | $showedit = has_capability('mod/assign:submit', $this->context) && | |
2017 | $this->submissions_open() && ($this->is_any_submission_plugin_enabled()) && $showlinks; | |
2018 | $showsubmit = $submission && ($submission->status == ASSIGN_SUBMISSION_STATUS_DRAFT) && $showlinks; | |
2019 | $gradelocked = ($grade && $grade->locked) || $this->grading_disabled($user->id); | |
2020 | ||
2021 | $o .= $this->output->render(new assign_submission_status($this->get_instance()->allowsubmissionsfromdate, | |
2022 | $this->get_instance()->alwaysshowdescription, | |
2023 | $submission, | |
2024 | $this->is_any_submission_plugin_enabled(), | |
2025 | $gradelocked, | |
2026 | $this->is_graded($user->id), | |
2027 | $this->get_instance()->duedate, | |
2028 | $this->get_submission_plugins(), | |
2029 | $this->get_return_action(), | |
2030 | $this->get_return_params(), | |
2031 | $this->get_course_module()->id, | |
2032 | assign_submission_status::STUDENT_VIEW, | |
2033 | $showedit, | |
2034 | $showsubmit)); | |
2035 | require_once($CFG->libdir.'/gradelib.php'); | |
2036 | require_once($CFG->dirroot.'/grade/grading/lib.php'); | |
2037 | ||
2038 | $gradinginfo = grade_get_grades($this->get_course()->id, | |
2039 | 'mod', | |
2040 | 'assign', | |
2041 | $this->get_instance()->id, | |
2042 | $user->id); | |
2043 | ||
2044 | $gradingitem = $gradinginfo->items[0]; | |
2045 | $gradebookgrade = $gradingitem->grades[$user->id]; | |
2046 | ||
2047 | // check to see if all feedback plugins are empty | |
2048 | $emptyplugins = true; | |
2049 | if ($grade) { | |
2050 | foreach ($this->get_feedback_plugins() as $plugin) { | |
2051 | if ($plugin->is_visible() && $plugin->is_enabled()) { | |
2052 | if (!$plugin->is_empty($grade)) { | |
2053 | $emptyplugins = false; | |
2054 | } | |
2055 | } | |
2056 | } | |
2057 | } | |
2058 | ||
2059 | ||
2060 | if (!($gradebookgrade->hidden) && ($gradebookgrade->grade !== null || !$emptyplugins)) { | |
2061 | ||
2062 | $gradefordisplay = ''; | |
2063 | $gradingmanager = get_grading_manager($this->get_context(), 'mod_assign', 'submissions'); | |
2064 | ||
2065 | if ($controller = $gradingmanager->get_active_controller()) { | |
2066 | $controller->set_grade_range(make_grades_menu($this->get_instance()->grade)); | |
2067 | $gradefordisplay = $controller->render_grade($PAGE, | |
2068 | $grade->id, | |
2069 | $gradingitem, | |
2070 | $gradebookgrade->str_long_grade, | |
2071 | has_capability('mod/assign:grade', $this->get_context())); | |
2072 | } else { | |
bf78ebd6 | 2073 | $gradefordisplay = $this->display_grade($gradebookgrade->grade, false); |
bbd0e548 DW |
2074 | } |
2075 | ||
2076 | $gradeddate = $gradebookgrade->dategraded; | |
2077 | $grader = $DB->get_record('user', array('id'=>$gradebookgrade->usermodified)); | |
2078 | ||
2079 | $feedbackstatus = new assign_feedback_status($gradefordisplay, | |
2080 | $gradeddate, | |
2081 | $grader, | |
2082 | $this->get_feedback_plugins(), | |
2083 | $grade, | |
2084 | $this->get_course_module()->id, | |
2085 | $this->get_return_action(), | |
2086 | $this->get_return_params()); | |
2087 | ||
2088 | $o .= $this->output->render($feedbackstatus); | |
2089 | } | |
2090 | ||
2091 | } | |
2092 | return $o; | |
2093 | } | |
2094 | ||
2095 | /** | |
2096 | * View submissions page (contains details of current submission). | |
2097 | * | |
2098 | * @return string | |
2099 | */ | |
2100 | private function view_submission_page() { | |
2101 | global $CFG, $DB, $USER, $PAGE; | |
2102 | ||
2103 | $o = ''; | |
2104 | $o .= $this->output->render(new assign_header($this->get_instance(), | |
2105 | $this->get_context(), | |
2106 | $this->show_intro(), | |
2107 | $this->get_course_module()->id)); | |
2108 | ||
2109 | if ($this->can_grade()) { | |
2110 | $o .= $this->output->render(new assign_grading_summary($this->count_participants(0), | |
2111 | $this->get_instance()->submissiondrafts, | |
2112 | $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_DRAFT), | |
2113 | $this->is_any_submission_plugin_enabled(), | |
2114 | $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED), | |
2115 | $this->get_instance()->duedate, | |
f70079b9 DW |
2116 | $this->get_course_module()->id, |
2117 | $this->count_submissions_need_grading() | |
bbd0e548 DW |
2118 | )); |
2119 | } | |
2120 | $grade = $this->get_user_grade($USER->id, false); | |
2121 | $submission = $this->get_user_submission($USER->id, false); | |
2122 | ||
2123 | if ($this->can_view_submission($USER->id)) { | |
2124 | $o .= $this->view_student_summary($USER, true); | |
2125 | } | |
2126 | ||
2127 | ||
2128 | $o .= $this->view_footer(); | |
2129 | $this->add_to_log('view', get_string('viewownsubmissionstatus', 'assign')); | |
2130 | return $o; | |
2131 | } | |
2132 | ||
2133 | /** | |
2134 | * convert the final raw grade(s) in the grading table for the gradebook | |
2135 | * | |
2136 | * @param stdClass $grade | |
2137 | * @return array | |
2138 | */ | |
2139 | private function convert_grade_for_gradebook(stdClass $grade) { | |
2140 | $gradebookgrade = array(); | |
2141 | // trying to match those array keys in grade update function in gradelib.php | |
2142 | // with keys in th database table assign_grades | |
2143 | // starting around line 262 | |
99467a9f DW |
2144 | if ($grade->grade >= 0) { |
2145 | $gradebookgrade['rawgrade'] = $grade->grade; | |
2146 | } | |
bbd0e548 DW |
2147 | $gradebookgrade['userid'] = $grade->userid; |
2148 | $gradebookgrade['usermodified'] = $grade->grader; | |
2149 | $gradebookgrade['datesubmitted'] = NULL; | |
2150 | $gradebookgrade['dategraded'] = $grade->timemodified; | |
2151 | if (isset($grade->feedbackformat)) { | |
2152 | $gradebookgrade['feedbackformat'] = $grade->feedbackformat; | |
2153 | } | |
2154 | if (isset($grade->feedbacktext)) { | |
2155 | $gradebookgrade['feedback'] = $grade->feedbacktext; | |
2156 | } | |
2157 | ||
2158 | return $gradebookgrade; | |
2159 | } | |
2160 | ||
2161 | /** | |
2162 | * convert submission details for the gradebook | |
2163 | * | |
2164 | * @param stdClass $submission | |
2165 | * @return array | |
2166 | */ | |
2167 | private function convert_submission_for_gradebook(stdClass $submission) { | |
2168 | $gradebookgrade = array(); | |
2169 | ||
2170 | ||
2171 | $gradebookgrade['userid'] = $submission->userid; | |
2172 | $gradebookgrade['usermodified'] = $submission->userid; | |
2173 | $gradebookgrade['datesubmitted'] = $submission->timemodified; | |
2174 | ||
2175 | return $gradebookgrade; | |
2176 | } | |
2177 | ||
2178 | /** | |
2179 | * update grades in the gradebook | |
2180 | * | |
2181 | * @param mixed $submission stdClass|null | |
2182 | * @param mixed $grade stdClass|null | |
2183 | * @return bool | |
2184 | */ | |
2185 | private function gradebook_item_update($submission=NULL, $grade=NULL) { | |
2186 | ||
2187 | if($submission != NULL){ | |
bbd0e548 | 2188 | $gradebookgrade = $this->convert_submission_for_gradebook($submission); |
bbd0e548 | 2189 | }else{ |
bbd0e548 DW |
2190 | $gradebookgrade = $this->convert_grade_for_gradebook($grade); |
2191 | } | |
09b46f0e AA |
2192 | // Grading is disabled, return. |
2193 | if ($this->grading_disabled($gradebookgrade['userid'])) { | |
2194 | return false; | |
2195 | } | |
bbd0e548 DW |
2196 | $assign = clone $this->get_instance(); |
2197 | $assign->cmidnumber = $this->get_course_module()->id; | |
2198 | ||
2199 | return assign_grade_item_update($assign, $gradebookgrade); | |
2200 | } | |
2201 | ||
2202 | /** | |
2203 | * update grades in the gradebook based on submission time | |
2204 | * | |
2205 | * @param stdClass $submission | |
2206 | * @param bool $updatetime | |
2207 | * @return bool | |
2208 | */ | |
2209 | private function update_submission(stdClass $submission, $updatetime=true) { | |
2210 | global $DB; | |
2211 | ||
2212 | if ($updatetime) { | |
2213 | $submission->timemodified = time(); | |
2214 | } | |
2215 | $result= $DB->update_record('assign_submission', $submission); | |
2216 | if ($result) { | |
2217 | $this->gradebook_item_update($submission); | |
2218 | } | |
2219 | return $result; | |
2220 | } | |
2221 | ||
2222 | /** | |
2223 | * Is this assignment open for submissions? | |
2224 | * | |
2225 | * Check the due date, | |
2226 | * prevent late submissions, | |
2227 | * has this person already submitted, | |
2228 | * is the assignment locked? | |
2229 | * | |
2230 | * @return bool | |
2231 | */ | |
2232 | private function submissions_open() { | |
2233 | global $USER; | |
2234 | ||
2235 | $time = time(); | |
2236 | $dateopen = true; | |
2237 | if ($this->get_instance()->preventlatesubmissions && $this->get_instance()->duedate) { | |
2238 | $dateopen = ($this->get_instance()->allowsubmissionsfromdate <= $time && $time <= $this->get_instance()->duedate); | |
2239 | } else { | |
2240 | $dateopen = ($this->get_instance()->allowsubmissionsfromdate <= $time); | |
2241 | } | |
2242 | ||
2243 | if (!$dateopen) { | |
2244 | return false; | |
2245 | } | |
2246 | ||
2247 | // now check if this user has already submitted etc. | |
2248 | if (!is_enrolled($this->get_course_context(), $USER)) { | |
2249 | return false; | |
2250 | } | |
2251 | if ($submission = $this->get_user_submission($USER->id, false)) { | |
2252 | if ($this->get_instance()->submissiondrafts && $submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED) { | |
2253 | // drafts are tracked and the student has submitted the assignment | |
2254 | return false; | |
2255 | } | |
2256 | } | |
2257 | if ($grade = $this->get_user_grade($USER->id, false)) { | |
2258 | if ($grade->locked) { | |
2259 | return false; | |
2260 | } | |
2261 | } | |
2262 | ||
2263 | if ($this->grading_disabled($USER->id)) { | |
2264 | return false; | |
2265 | } | |
2266 | ||
2267 | return true; | |
2268 | } | |
2269 | ||
2270 | /** | |
2271 | * render the files in file area | |
2272 | * @param string $component | |
2273 | * @param string $area | |
2274 | * @param int $submissionid | |
2275 | * @return string | |
2276 | */ | |
2277 | public function render_area_files($component, $area, $submissionid) { | |
2278 | global $USER; | |
2279 | ||
2280 | if (!$submissionid) { | |
2281 | $submission = $this->get_user_submission($USER->id,false); | |
2282 | $submissionid = $submission->id; | |
2283 | } | |
2284 | ||
bbd0e548 DW |
2285 | $fs = get_file_storage(); |
2286 | $browser = get_file_browser(); | |
2287 | $files = $fs->get_area_files($this->get_context()->id, $component, $area , $submissionid , "timemodified", false); | |
2288 | return $this->output->assign_files($this->context, $submissionid, $area, $component); | |
2289 | ||
2290 | } | |
2291 | ||
2292 | /** | |
2293 | * Returns a list of teachers that should be grading given submission | |
2294 | * | |
75f87a57 | 2295 | * @param int $userid |
bbd0e548 DW |
2296 | * @return array |
2297 | */ | |
75f87a57 | 2298 | private function get_graders($userid) { |
bbd0e548 | 2299 | //potential graders |
75f87a57 | 2300 | $potentialgraders = get_enrolled_users($this->context, "mod/assign:grade"); |
bbd0e548 DW |
2301 | |
2302 | $graders = array(); | |
2303 | if (groups_get_activity_groupmode($this->get_course_module()) == SEPARATEGROUPS) { // Separate groups are being used | |
75f87a57 | 2304 | if ($groups = groups_get_all_groups($this->get_course()->id, $userid)) { // Try to find all groups |
bbd0e548 DW |
2305 | foreach ($groups as $group) { |
2306 | foreach ($potentialgraders as $grader) { | |
75f87a57 | 2307 | if ($grader->id == $userid) { |
bbd0e548 DW |
2308 | continue; // do not send self |
2309 | } | |
2310 | if (groups_is_member($group->id, $grader->id)) { | |
2311 | $graders[$grader->id] = $grader; | |
2312 | } | |
2313 | } | |
2314 | } | |
2315 | } else { | |
2316 | // user not in group, try to find graders without group | |
2317 | foreach ($potentialgraders as $grader) { | |
75f87a57 | 2318 | if ($grader->id == $userid) { |
bbd0e548 DW |
2319 | continue; // do not send self |
2320 | } | |
2321 | if (!groups_has_membership($this->get_course_module(), $grader->id)) { | |
2322 | $graders[$grader->id] = $grader; | |
2323 | } | |
2324 | } | |
2325 | } | |
2326 | } else { | |
2327 | foreach ($potentialgraders as $grader) { | |
75f87a57 | 2328 | if ($grader->id == $userid) { |
bbd0e548 DW |
2329 | continue; // do not send self |
2330 | } | |
2331 | // must be enrolled | |
2332 | if (is_enrolled($this->get_course_context(), $grader->id)) { | |
2333 | $graders[$grader->id] = $grader; | |
2334 | } | |
2335 | } | |
2336 | } | |
2337 | return $graders; | |
2338 | } | |
2339 | ||
2340 | /** | |
3f7b501e | 2341 | * Format a notification for plain text |
bbd0e548 | 2342 | * |
75f87a57 DW |
2343 | * @param string $messagetype |
2344 | * @param stdClass $info | |
2345 | * @param stdClass $course | |
2346 | * @param stdClass $context | |
2347 | * @param string $modulename | |
2348 | * @param string $assignmentname | |
bbd0e548 | 2349 | */ |
75f87a57 DW |
2350 | private static function format_notification_message_text($messagetype, $info, $course, $context, $modulename, $assignmentname) { |
2351 | $posttext = format_string($course->shortname, true, array('context' => $context->get_course_context())).' -> '. | |
2352 | $modulename.' -> '. | |
2353 | format_string($assignmentname, true, array('context' => $context))."\n"; | |
bbd0e548 | 2354 | $posttext .= '---------------------------------------------------------------------'."\n"; |
75f87a57 | 2355 | $posttext .= get_string($messagetype . 'text', "assign", $info)."\n"; |
bbd0e548 DW |
2356 | $posttext .= "\n---------------------------------------------------------------------\n"; |
2357 | return $posttext; | |
2358 | } | |
2359 | ||
75f87a57 | 2360 | /** |
3f7b501e | 2361 | * Format a notification for HTML |
bbd0e548 | 2362 | * |
75f87a57 DW |
2363 | * @param string $messagetype |
2364 | * @param stdClass $info | |
3f7b501e SH |
2365 | * @param stdClass $course |
2366 | * @param stdClass $context | |
2367 | * @param string $modulename | |
2368 | * @param stdClass $coursemodule | |
2369 | * @param string $assignmentname | |
2370 | * @param stdClass $info | |
bbd0e548 | 2371 | */ |
75f87a57 | 2372 | private static function format_notification_message_html($messagetype, $info, $course, $context, $modulename, $coursemodule, $assignmentname) { |
bbd0e548 DW |
2373 | global $CFG; |
2374 | $posthtml = '<p><font face="sans-serif">'. | |
75f87a57 | 2375 | '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.format_string($course->shortname, true, array('context' => $context->get_course_context())).'</a> ->'. |
599f7a27 RK |
2376 | '<a href="'.$CFG->wwwroot.'/mod/assign/index.php?id='.$course->id.'">'.$modulename.'</a> ->'. |
2377 | '<a href="'.$CFG->wwwroot.'/mod/assign/view.php?id='.$coursemodule->id.'">'.format_string($assignmentname, true, array('context' => $context)).'</a></font></p>'; | |
bbd0e548 | 2378 | $posthtml .= '<hr /><font face="sans-serif">'; |
75f87a57 | 2379 | $posthtml .= '<p>'.get_string($messagetype . 'html', 'assign', $info).'</p>'; |
bbd0e548 DW |
2380 | $posthtml .= '</font><hr />'; |
2381 | return $posthtml; | |
2382 | } | |
2383 | ||
75f87a57 | 2384 | /** |
c1d09c6f | 2385 | * Message someone about something (static so it can be called from cron) |
75f87a57 | 2386 | * |
75f87a57 DW |
2387 | * @param stdClass $userfrom |
2388 | * @param stdClass $userto | |
2389 | * @param string $messagetype | |
2390 | * @param string $eventtype | |
2391 | * @param int $updatetime | |
2392 | * @param stdClass $coursemodule | |
2393 | * @param stdClass $context | |
2394 | * @param stdClass $course | |
2395 | * @param string $modulename | |
75f87a57 DW |
2396 | * @param string $assignmentname |
2397 | * @return void | |
2398 | */ | |
2399 | public static function send_assignment_notification($userfrom, $userto, $messagetype, $eventtype, | |
2400 | $updatetime, $coursemodule, $context, $course, | |
2401 | $modulename, $assignmentname) { | |
3f7b501e | 2402 | global $CFG; |
75f87a57 DW |
2403 | |
2404 | $info = new stdClass(); | |
2405 | $info->username = fullname($userfrom, true); | |
2406 | $info->assignment = format_string($assignmentname,true, array('context'=>$context)); | |
2407 | $info->url = $CFG->wwwroot.'/mod/assign/view.php?id='.$coursemodule->id; | |
2408 | $info->timeupdated = strftime('%c',$updatetime); | |
2409 | ||
2410 | $postsubject = get_string($messagetype . 'small', 'assign', $info); | |
2411 | $posttext = self::format_notification_message_text($messagetype, $info, $course, $context, $modulename, $assignmentname); | |
2412 | $posthtml = ($userto->mailformat == 1) ? self::format_notification_message_html($messagetype, $info, $course, $context, $modulename, $coursemodule, $assignmentname) : ''; | |
2413 | ||
2414 | $eventdata = new stdClass(); | |
2415 | $eventdata->modulename = 'assign'; | |
2416 | $eventdata->userfrom = $userfrom; | |
2417 | $eventdata->userto = $userto; | |
2418 | $eventdata->subject = $postsubject; | |
2419 | $eventdata->fullmessage = $posttext; | |
2420 | $eventdata->fullmessageformat = FORMAT_PLAIN; | |
2421 | $eventdata->fullmessagehtml = $posthtml; | |
2422 | $eventdata->smallmessage = $postsubject; | |
2423 | ||
2424 | $eventdata->name = $eventtype; | |
2425 | $eventdata->component = 'mod_assign'; | |
2426 | $eventdata->notification = 1; | |
2427 | $eventdata->contexturl = $info->url; | |
2428 | $eventdata->contexturlname = $info->assignment; | |
2429 | ||
2430 | message_send($eventdata); | |
75f87a57 DW |
2431 | } |
2432 | ||
2433 | /** | |
c1d09c6f | 2434 | * Message someone about something |
75f87a57 | 2435 | * |
3f7b501e SH |
2436 | * @param stdClass $userfrom |
2437 | * @param stdClass $userto | |
2438 | * @param string $messagetype | |
2439 | * @param string $eventtype | |
2440 | * @param int $updatetime | |
75f87a57 DW |
2441 | * @return void |
2442 | */ | |
2443 | public function send_notification($userfrom, $userto, $messagetype, $eventtype, $updatetime) { | |
2444 | self::send_assignment_notification($userfrom, $userto, $messagetype, $eventtype, $updatetime, $this->get_course_module(), $this->get_context(), $this->get_course(), $this->get_module_name(), $this->get_instance()->name); | |
2445 | } | |
2446 | ||
2447 | /** | |
c1d09c6f | 2448 | * Notify student upon successful submission |
75f87a57 | 2449 | * |
75f87a57 DW |
2450 | * @global moodle_database $DB |
2451 | * @param stdClass $submission | |
2452 | * @return void | |
2453 | */ | |
c1d09c6f | 2454 | private function notify_student_submission_receipt(stdClass $submission) { |
75f87a57 DW |
2455 | global $DB; |
2456 | ||
2457 | $adminconfig = $this->get_admin_config(); | |
3f7b501e SH |
2458 | if (!$adminconfig->submissionreceipts) { |
2459 | // No need to do anything | |
75f87a57 DW |
2460 | return; |
2461 | } | |
75f87a57 | 2462 | $user = $DB->get_record('user', array('id'=>$submission->userid), '*', MUST_EXIST); |
f750cf71 | 2463 | $this->send_notification($user, $user, 'submissionreceipt', 'assign_notification', $submission->timemodified); |
75f87a57 DW |
2464 | } |
2465 | ||
bbd0e548 | 2466 | /** |
c1d09c6f | 2467 | * Send notifications to graders upon student submissions |
bbd0e548 | 2468 | * |
75f87a57 | 2469 | * @global moodle_database $DB |
bbd0e548 DW |
2470 | * @param stdClass $submission |
2471 | * @return void | |
2472 | */ | |
c1d09c6f | 2473 | private function notify_graders(stdClass $submission) { |
75f87a57 DW |
2474 | global $DB; |
2475 | ||
2476 | $late = $this->get_instance()->duedate && ($this->get_instance()->duedate < time()); | |
bbd0e548 | 2477 | |
75f87a57 | 2478 | if (!$this->get_instance()->sendnotifications && !($late && $this->get_instance()->sendlatenotifications)) { // No need to do anything |
bbd0e548 DW |
2479 | return; |
2480 | } | |
2481 | ||
2482 | $user = $DB->get_record('user', array('id'=>$submission->userid), '*', MUST_EXIST); | |
75f87a57 | 2483 | if ($teachers = $this->get_graders($user->id)) { |
bbd0e548 | 2484 | foreach ($teachers as $teacher) { |
f750cf71 | 2485 | $this->send_notification($user, $teacher, 'gradersubmissionupdated', 'assign_notification', $submission->timemodified); |
bbd0e548 DW |
2486 | } |
2487 | } | |
2488 | } | |
2489 | ||
2490 | /** | |
2491 | * assignment submission is processed before grading | |
2492 | * | |
2493 | * @return void | |
2494 | */ | |
2495 | private function process_submit_for_grading() { | |
2496 | global $USER; | |
2497 | ||
2498 | // Need submit permission to submit an assignment | |
2499 | require_capability('mod/assign:submit', $this->context); | |
2500 | require_sesskey(); | |
2501 | ||
2502 | $submission = $this->get_user_submission($USER->id,true); | |
2503 | if ($submission->status != ASSIGN_SUBMISSION_STATUS_SUBMITTED) { | |
2504 | // Give each submission plugin a chance to process the submission | |
2505 | $plugins = $this->get_submission_plugins(); | |
2506 | foreach ($plugins as $plugin) { | |
2507 | $plugin->submit_for_grading(); | |
2508 | } | |
2509 | ||
2510 | $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; | |
2511 | $this->update_submission($submission); | |
2512 | $this->add_to_log('submit for grading', $this->format_submission_for_log($submission)); | |
c1d09c6f RK |
2513 | $this->notify_graders($submission); |
2514 | $this->notify_student_submission_receipt($submission); | |
bbd0e548 DW |
2515 | } |
2516 | } | |
2517 | ||
bf78ebd6 DW |
2518 | /** |
2519 | * save quick grades | |
2520 | * | |
2a4fbc32 SH |
2521 | * @global moodle_database $DB |
2522 | * @return string The result of the save operation | |
bf78ebd6 DW |
2523 | */ |
2524 | private function process_save_quick_grades() { | |
2525 | global $USER, $DB, $CFG; | |
2526 | ||
2527 | // Need grade permission | |
2528 | require_capability('mod/assign:grade', $this->context); | |
2529 | ||
2530 | // make sure advanced grading is disabled | |
2531 | $gradingmanager = get_grading_manager($this->get_context(), 'mod_assign', 'submissions'); | |
2532 | $controller = $gradingmanager->get_active_controller(); | |
2533 | if (!empty($controller)) { | |
2a4fbc32 | 2534 | return get_string('errorquickgradingvsadvancedgrading', 'assign'); |
bf78ebd6 DW |
2535 | } |
2536 | ||
2537 | $users = array(); | |
2538 | // first check all the last modified values | |
2c6a3dbf DW |
2539 | $currentgroup = groups_get_activity_group($this->get_course_module(), true); |
2540 | $participants = $this->list_participants($currentgroup, true); | |
2541 | ||
2a4fbc32 | 2542 | // gets a list of possible users and look for values based upon that. |
2c6a3dbf DW |
2543 | foreach ($participants as $userid => $unused) { |
2544 | $modified = optional_param('grademodified_' . $userid, -1, PARAM_INT); | |
2545 | if ($modified >= 0) { | |
bf78ebd6 DW |
2546 | // gather the userid, updated grade and last modified value |
2547 | $record = new stdClass(); | |
2c6a3dbf | 2548 | $record->userid = $userid; |
4913af93 | 2549 | $record->grade = unformat_float(required_param('quickgrade_' . $record->userid, PARAM_TEXT)); |
2c6a3dbf DW |
2550 | $record->lastmodified = $modified; |
2551 | $record->gradinginfo = grade_get_grades($this->get_course()->id, 'mod', 'assign', $this->get_instance()->id, array($userid)); | |
2552 | $users[$userid] = $record; | |
bf78ebd6 DW |
2553 | } |
2554 | } | |
2a4fbc32 SH |
2555 | if (empty($users)) { |
2556 | // Quick check to see whether we have any users to update and we don't | |
2557 | return get_string('quickgradingchangessaved', 'assign'); // Technical lie | |
2558 | } | |
bf78ebd6 | 2559 | |
2a4fbc32 SH |
2560 | list($userids, $params) = $DB->get_in_or_equal(array_keys($users), SQL_PARAMS_NAMED); |
2561 | $params['assignment'] = $this->get_instance()->id; | |
bf78ebd6 | 2562 | // check them all for currency |
2a4fbc32 SH |
2563 | $sql = 'SELECT u.id as userid, g.grade as grade, g.timemodified as lastmodified |
2564 | FROM {user} u | |
2565 | LEFT JOIN {assign_grades} g ON u.id = g.userid AND g.assignment = :assignment | |
2566 | WHERE u.id ' . $userids; | |
2567 | $currentgrades = $DB->get_recordset_sql($sql, $params); | |
bf78ebd6 DW |
2568 | |
2569 | $modifiedusers = array(); | |
2570 | foreach ($currentgrades as $current) { | |
2571 | $modified = $users[(int)$current->userid]; | |
2c6a3dbf | 2572 | $grade = $this->get_user_grade($userid, false); |
bf78ebd6 DW |
2573 | |
2574 | // check to see if the outcomes were modified | |
2575 | if ($CFG->enableoutcomes) { | |
2576 | foreach ($modified->gradinginfo->outcomes as $outcomeid => $outcome) { | |
2577 | $oldoutcome = $outcome->grades[$modified->userid]->grade; | |
4913af93 | 2578 | $newoutcome = optional_param('outcome_' . $outcomeid . '_' . $modified->userid, -1, PARAM_FLOAT); |
bf78ebd6 DW |
2579 | if ($oldoutcome != $newoutcome) { |
2580 | // can't check modified time for outcomes because it is not reported | |
2581 | $modifiedusers[$modified->userid] = $modified; | |
2582 | continue; | |
2583 | } | |
2584 | } | |
2585 | } | |
2586 | ||
2c6a3dbf DW |
2587 | // let plugins participate |
2588 | foreach ($this->feedbackplugins as $plugin) { | |
2589 | if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->supports_quickgrading()) { | |
2590 | if ($plugin->is_quickgrading_modified($modified->userid, $grade)) { | |
2591 | if ((int)$current->lastmodified > (int)$modified->lastmodified) { | |
2592 | return get_string('errorrecordmodified', 'assign'); | |
2593 | } else { | |
2594 | $modifiedusers[$modified->userid] = $modified; | |
2595 | continue; | |
2596 | } | |
2597 | } | |
2598 | } | |
2599 | } | |
2600 | ||
bf78ebd6 DW |
2601 | |
2602 | if (($current->grade < 0 || $current->grade === NULL) && | |
2603 | ($modified->grade < 0 || $modified->grade === NULL)) { | |
2604 | // different ways to indicate no grade | |
2605 | continue; | |
2606 | } | |
a13edd49 DW |
2607 | // Treat 0 and null as different values |
2608 | if ($current->grade !== null) { | |
2609 | $current->grade = floatval($current->grade); | |
2610 | } | |
2611 | if ($current->grade !== $modified->grade) { | |
bf78ebd6 | 2612 | // grade changed |
09b46f0e AA |
2613 | if ($this->grading_disabled($modified->userid)) { |
2614 | continue; | |
2615 | } | |
bf78ebd6 DW |
2616 | if ((int)$current->lastmodified > (int)$modified->lastmodified) { |
2617 | // error - record has been modified since viewing the page | |
2618 | return get_string('errorrecordmodified', 'assign'); | |
2619 | } else { | |
2620 | $modifiedusers[$modified->userid] = $modified; | |
2621 | } | |
2622 | } | |
2623 | ||
2624 | } | |
2a4fbc32 | 2625 | $currentgrades->close(); |
bf78ebd6 DW |
2626 | |
2627 | // ok - ready to process the updates | |
2628 | foreach ($modifiedusers as $userid => $modified) { | |
2629 | $grade = $this->get_user_grade($userid, true); | |
4913af93 | 2630 | $grade->grade= grade_floatval(unformat_float($modified->grade)); |
bf78ebd6 DW |
2631 | $grade->grader= $USER->id; |
2632 | ||
2633 | $this->update_grade($grade); | |
2634 | ||
2c6a3dbf DW |
2635 | // save plugins data |
2636 | foreach ($this->feedbackplugins as $plugin) { | |
2637 | if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->supports_quickgrading()) { | |
2638 | $plugin->save_quickgrading_changes($userid, $grade); | |
2639 | } | |
2640 | } | |
2641 | ||
bf78ebd6 DW |
2642 | // save outcomes |
2643 | if ($CFG->enableoutcomes) { | |
2644 | $data = array(); | |
2645 | foreach ($modified->gradinginfo->outcomes as $outcomeid => $outcome) { | |
2646 | $oldoutcome = $outcome->grades[$modified->userid]->grade; | |
2647 | $newoutcome = optional_param('outcome_' . $outcomeid . '_' . $modified->userid, -1, PARAM_INT); | |
2648 | if ($oldoutcome != $newoutcome) { | |
2649 | $data[$outcomeid] = $newoutcome; | |
2650 | } | |
2651 | } | |
2652 | if (count($data) > 0) { | |
2653 | grade_update_outcomes('mod/assign', $this->course->id, 'mod', 'assign', $this->get_instance()->id, $userid, $data); | |
2654 | } | |
2655 | } | |
2656 | ||
bf78ebd6 DW |
2657 | $this->add_to_log('grade submission', $this->format_grade_for_log($grade)); |
2658 | } | |
2659 | ||
2660 | return get_string('quickgradingchangessaved', 'assign'); | |
2661 | } | |
2662 | ||
bbd0e548 DW |
2663 | /** |
2664 | * save grading options | |
2665 | * | |
2666 | * @return void | |
2667 | */ | |
2668 | private function process_save_grading_options() { | |
2669 | global $USER, $CFG; | |
2670 | ||
2671 | // Include grading options form | |
2672 | require_once($CFG->dirroot . '/mod/assign/gradingoptionsform.php'); | |
2673 | ||
2674 | // Need submit permission to submit an assignment | |
2675 | require_capability('mod/assign:grade', $this->context); | |
2676 | ||
44e2f0fe DW |
2677 | $mform = new mod_assign_grading_options_form(null, array('cm'=>$this->get_course_module()->id, |
2678 | 'contextid'=>$this->context->id, | |
2679 | 'userid'=>$USER->id, | |
2680 | 'submissionsenabled'=>$this->is_any_submission_plugin_enabled(), | |
2681 | 'showquickgrading'=>false)); | |
bbd0e548 DW |
2682 | if ($formdata = $mform->get_data()) { |
2683 | set_user_preference('assign_perpage', $formdata->perpage); | |
2684 | set_user_preference('assign_filter', $formdata->filter); | |
2685 | } | |
2686 | } | |
2687 | ||
2688 | /** | |
2689 | * Take a grade object and print a short summary for the log file. | |
2690 | * The size limit for the log file is 255 characters, so be careful not | |
2691 | * to include too much information. | |
2692 | * | |
2693 | * @param stdClass $grade | |
2694 | * @return string | |
2695 | */ | |
2696 | private function format_grade_for_log(stdClass $grade) { | |
2697 | global $DB; | |
2698 | ||
2699 | $user = $DB->get_record('user', array('id' => $grade->userid), '*', MUST_EXIST); | |
2700 | ||
2701 | $info = get_string('gradestudent', 'assign', array('id'=>$user->id, 'fullname'=>fullname($user))); | |
2702 | if ($grade->grade != '') { | |
bf78ebd6 | 2703 | $info .= get_string('grade') . ': ' . $this->display_grade($grade->grade, false) . '. '; |
bbd0e548 DW |
2704 | } else { |
2705 | $info .= get_string('nograde', 'assign'); | |
2706 | } | |
2707 | if ($grade->locked) { | |
2708 | $info .= get_string('submissionslocked', 'assign') . '. '; | |
2709 | } | |
2710 | return $info; | |
2711 | } | |
2712 | ||
2713 | /** | |
2714 | * Take a submission object and print a short summary for the log file. | |
2715 | * The size limit for the log file is 255 characters, so be careful not | |
2716 | * to include too much information. | |
2717 | * | |
2718 | * @param stdClass $submission | |
2719 | * @return string | |
2720 | */ | |
2721 | private function format_submission_for_log(stdClass $submission) { | |
2722 | $info = ''; | |
2723 | $info .= get_string('submissionstatus', 'assign') . ': ' . get_string('submissionstatus_' . $submission->status, 'assign') . '. <br>'; | |
2724 | // format_for_log here iterating every single log INFO from either submission or grade in every assignment plugin | |
2725 | ||
2726 | foreach ($this->submissionplugins as $plugin) { | |
2727 | if ($plugin->is_enabled() && $plugin->is_visible()) { | |
2728 | ||
2729 | ||
2730 | $info .= "<br>" . $plugin->format_for_log($submission); | |
2731 | } | |
2732 | } | |
2733 | ||
2734 | ||
2735 | return $info; | |
2736 | } | |
2737 | ||
2738 | /** | |
2739 | * save assignment submission | |
2740 | * | |
2741 | * @param moodleform $mform | |
2742 | * @return bool | |
2743 | */ | |
2744 | private function process_save_submission(&$mform) { | |
2745 | global $USER, $CFG; | |
2746 | ||
2747 | // Include submission form | |
2748 | require_once($CFG->dirroot . '/mod/assign/submission_form.php'); | |
2749 | ||
2750 | // Need submit permission to submit an assignment | |
2751 | require_capability('mod/assign:submit', $this->context); | |
2752 | require_sesskey(); | |
2753 | ||
2754 | $data = new stdClass(); | |
2755 | $mform = new mod_assign_submission_form(null, array($this, $data)); | |
2756 | if ($mform->is_cancelled()) { | |
2757 | return true; | |
2758 | } | |
2759 | if ($data = $mform->get_data()) { | |
2760 | $submission = $this->get_user_submission($USER->id, true); //create the submission if needed & its id | |
2761 | $grade = $this->get_user_grade($USER->id, false); // get the grade to check if it is locked | |
2762 | if ($grade && $grade->locked) { | |
2763 | print_error('submissionslocked', 'assign'); | |
2764 | return true; | |
2765 | } | |
2766 | ||
2767 | ||
2768 | foreach ($this->submissionplugins as $plugin) { | |
2769 | if ($plugin->is_enabled()) { | |
2770 | if (!$plugin->save($submission, $data)) { | |
2771 | print_error($plugin->get_error()); | |
2772 | } | |
2773 | } | |
2774 | } | |
2775 | ||
2776 | $this->update_submission($submission); | |
2777 | ||
2778 | // Logging | |
2779 | $this->add_to_log('submit', $this->format_submission_for_log($submission)); | |
2780 | ||
2781 | if (!$this->get_instance()->submissiondrafts) { | |
c1d09c6f RK |
2782 | $this->notify_student_submission_receipt($submission); |
2783 | $this->notify_graders($submission); | |
bbd0e548 DW |
2784 | } |
2785 | return true; | |
2786 | } | |
2787 | return false; | |
2788 | } | |
2789 | ||
bbd0e548 DW |
2790 | |
2791 | /** | |
2792 | * Determine if this users grade is locked or overridden | |
2793 | * | |
2794 | * @param int $userid - The student userid | |
2795 | * @return bool $gradingdisabled | |
2796 | */ | |
09b46f0e | 2797 | public function grading_disabled($userid) { |
bbd0e548 DW |
2798 | global $CFG; |
2799 | ||
2800 | $gradinginfo = grade_get_grades($this->get_course()->id, 'mod', 'assign', $this->get_instance()->id, array($userid)); | |
2801 | if (!$gradinginfo) { | |
2802 | return false; | |
2803 | } | |
2804 | ||
2805 | if (!isset($gradinginfo->items[0]->grades[$userid])) { | |
2806 | return false; | |
2807 | } | |
2808 | $gradingdisabled = $gradinginfo->items[0]->grades[$userid]->locked || $gradinginfo->items[0]->grades[$userid]->overridden; | |
2809 | return $gradingdisabled; | |
2810 | } | |
2811 | ||
2812 | ||
2813 | /** | |
2814 | * Get an instance of a grading form if advanced grading is enabled | |
2815 | * This is specific to the assignment, marker and student | |
2816 | * | |
2817 | * @param int $userid - The student userid | |
2818 | * @param bool $gradingdisabled | |
2819 | * @return mixed gradingform_instance|null $gradinginstance | |
2820 | */ | |
2821 | private function get_grading_instance($userid, $gradingdisabled) { | |
2822 | global $CFG, $USER; | |
2823 | ||
2824 | $grade = $this->get_user_grade($userid, false); | |
2825 | $grademenu = make_grades_menu($this->get_instance()->grade); | |
2826 | ||
2827 | $advancedgradingwarning = false; | |
2828 | $gradingmanager = get_grading_manager($this->context, 'mod_assign', 'submissions'); | |
2829 | $gradinginstance = null; | |
2830 | if ($gradingmethod = $gradingmanager->get_active_method()) { | |
2831 | $controller = $gradingmanager->get_controller($gradingmethod); | |
2832 | if ($controller->is_form_available()) { | |
2833 | $itemid = null; | |
2834 | if ($grade) { | |
2835 | $itemid = $grade->id; | |
2836 | } | |
2837 | if ($gradingdisabled && $itemid) { | |
2838 | $gradinginstance = ($controller->get_current_instance($USER->id, $itemid)); | |
2839 | } else if (!$gradingdisabled) { | |
2840 | $instanceid = optional_param('advancedgradinginstanceid', 0, PARAM_INT); | |
2841 | $gradinginstance = ($controller->get_or_create_instance($instanceid, $USER->id, $itemid)); | |
2842 | } | |
2843 | } else { | |
2844 | $advancedgradingwarning = $controller->form_unavailable_notification(); | |
2845 | } | |
2846 | } | |
2847 | if ($gradinginstance) { | |
2848 | $gradinginstance->get_controller()->set_grade_range($grademenu); | |
2849 | } | |
2850 | return $gradinginstance; | |
2851 | } | |
2852 | ||
2853 | /** | |
2854 | * add elements to grade form | |
2855 | * | |
2856 | * @param MoodleQuickForm $mform | |
2857 | * @param stdClass $data | |
2858 | * @param array $params | |
2859 | * @return void | |
2860 | */ | |
2861 | public function add_grade_form_elements(MoodleQuickForm $mform, stdClass $data, $params) { | |
2862 | global $USER, $CFG; | |
2863 | $settings = $this->get_instance(); | |
2864 | ||
2865 | $rownum = $params['rownum']; | |
2866 | $last = $params['last']; | |
2867 | $useridlist = $params['useridlist']; | |
2868 | $userid = $useridlist[$rownum]; | |
2869 | $grade = $this->get_user_grade($userid, false); | |
2870 | ||
2871 | // add advanced grading | |
2872 | $gradingdisabled = $this->grading_disabled($userid); | |
2873 | $gradinginstance = $this->get_grading_instance($userid, $gradingdisabled); | |
2874 | ||
2875 | if ($gradinginstance) { | |
2876 | $gradingelement = $mform->addElement('grading', 'advancedgrading', get_string('grade').':', array('gradinginstance' => $gradinginstance)); | |
2877 | if ($gradingdisabled) { | |
2878 | $gradingelement->freeze(); | |
2879 | } else { | |
2880 | $mform->addElement('hidden', 'advancedgradinginstanceid', $gradinginstance->get_id()); | |
2881 | } | |
2882 | } else { | |
2883 | // use simple direct grading | |
2884 | if ($this->get_instance()->grade > 0) { | |
09b46f0e | 2885 | $gradingelement = $mform->addElement('text', 'grade', get_string('gradeoutof', 'assign',$this->get_instance()->grade)); |
bbd0e548 DW |
2886 | $mform->addHelpButton('grade', 'gradeoutofhelp', 'assign'); |
2887 | $mform->setType('grade', PARAM_TEXT); | |
09b46f0e AA |
2888 | if ($gradingdisabled) { |
2889 | $gradingelement->freeze(); | |
2890 | } | |
bbd0e548 DW |
2891 | } else { |
2892 | $grademenu = make_grades_menu($this->get_instance()->grade); | |
2893 | if (count($grademenu) > 0) { | |
09b46f0e | 2894 | $gradingelement = $mform->addElement('select', 'grade', get_string('grade').':', $grademenu); |
bbd0e548 | 2895 | $mform->setType('grade', PARAM_INT); |
09b46f0e AA |
2896 | if ($gradingdisabled) { |
2897 | $gradingelement->freeze(); | |
2898 | } | |
bbd0e548 DW |
2899 | } |
2900 | } | |
2901 | } | |
bbd0e548 | 2902 | |
c45261df DW |
2903 | $gradinginfo = grade_get_grades($this->get_course()->id, |
2904 | 'mod', | |
2905 | 'assign', | |
2906 | $this->get_instance()->id, | |
2907 | $userid); | |
2908 | if (!empty($CFG->enableoutcomes)) { | |
2909 | foreach($gradinginfo->outcomes as $index=>$outcome) { | |
2910 | $options = make_grades_menu(-$outcome->scaleid); | |
2911 | if ($outcome->grades[$userid]->locked) { | |
2912 | $options[0] = get_string('nooutcome', 'grades'); | |
2913 | $mform->addElement('static', 'outcome_'.$index.'['.$userid.']', $outcome->name.':', | |
2914 | $options[$outcome->grades[$userid]->grade]); | |
2915 | } else { | |
2916 | $options[''] = get_string('nooutcome', 'grades'); | |
2917 | $attributes = array('id' => 'menuoutcome_'.$index ); | |
2918 | $mform->addElement('select', 'outcome_'.$index.'['.$userid.']', $outcome->name.':', $options, $attributes ); | |
2919 | $mform->setType('outcome_'.$index.'['.$userid.']', PARAM_INT); | |
2920 | $mform->setDefault('outcome_'.$index.'['.$userid.']', $outcome->grades[$userid]->grade ); | |
2921 | } | |
2922 | } | |
2923 | } | |
2924 | ||
787ad881 DW |
2925 | if (has_all_capabilities(array('gradereport/grader:view', 'moodle/grade:viewall'), $this->get_course_context())) { |
2926 | $grade = $this->output->action_link(new moodle_url('/grade/report/grader/index.php', | |
2927 | array('id'=>$this->get_course()->id)), | |
2928 | $gradinginfo->items[0]->grades[$userid]->str_grade); | |
2929 | } else { | |
2930 | $grade = $gradinginfo->items[0]->grades[$userid]->str_grade; | |
2931 | } | |
2932 | $mform->addElement('static', 'finalgrade', get_string('currentgrade', 'assign').':' ,$grade); | |
2933 | ||
2934 | ||
c45261df | 2935 | $mform->addElement('static', 'progress', '', get_string('gradingstudentprogress', 'assign', array('index'=>$rownum+1, 'count'=>count($useridlist)))); |
bbd0e548 DW |
2936 | |
2937 | // plugins | |
2938 | $this->add_plugin_grade_elements($grade, $mform, $data); | |
2939 | ||
2940 | // hidden params | |
2941 | $mform->addElement('hidden', 'id', $this->get_course_module()->id); | |
2942 | $mform->setType('id', PARAM_INT); | |
2943 | $mform->addElement('hidden', 'rownum', $rownum); | |
2944 | $mform->setType('rownum', PARAM_INT); | |
2945 | $mform->addElement('hidden', 'useridlist', implode(',', $useridlist)); | |
2946 | $mform->setType('useridlist', PARAM_TEXT); | |
2947 | $mform->addElement('hidden', 'ajax', optional_param('ajax', 0, PARAM_INT)); | |
2948 | $mform->setType('ajax', PARAM_INT); | |
2949 | ||
2950 | $mform->addElement('hidden', 'action', 'submitgrade'); | |
2951 | $mform->setType('action', PARAM_ALPHA); | |
2952 | ||
2953 | ||
2954 | $buttonarray=array(); | |
2955 | $buttonarray[] = $mform->createElement('submit', 'savegrade', get_string('savechanges', 'assign')); | |
2956 | if (!$last){ | |
2957 | $buttonarray[] = $mform->createElement('submit', 'saveandshownext', get_string('savenext','assign')); | |
2958 | } | |
2959 | $buttonarray[] = $mform->createElement('cancel', 'cancelbutton', get_string('cancel')); | |
2960 | $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); | |
2961 | $mform->closeHeaderBefore('buttonar'); | |
2962 | $buttonarray=array(); | |
2963 | ||
2964 | if ($rownum > 0) { | |
2965 | $buttonarray[] = $mform->createElement('submit', 'nosaveandprevious', get_string('previous','assign')); | |
2966 | } | |
2967 | ||
2968 | if (!$last){ | |
2969 | $buttonarray[] = $mform->createElement('submit', 'nosaveandnext', get_string('nosavebutnext', 'assign')); | |
2970 | } | |
2971 | $mform->addGroup($buttonarray, 'navar', '', array(' '), false); | |
2972 | } | |
2973 | ||
2974 | ||
2975 | /** | |
2976 | * add elements in submission plugin form | |
2977 | * | |
2978 | * @param mixed $submission stdClass|null | |
2979 | * @param MoodleQuickForm $mform | |
2980 | * @param stdClass $data | |
2981 | * @return void | |
2982 | */ | |
2983 | private function add_plugin_submission_elements($submission, MoodleQuickForm $mform, stdClass $data) { | |
2984 | foreach ($this->submissionplugins as $plugin) { | |
2985 | if ($plugin->is_enabled() && $plugin->is_visible() && $plugin->allow_submissions()) { | |
2986 | $mform->addElement('header', 'header_' . $plugin->get_type(), $plugin->get_name()); | |
2987 | if (!$plugin->get_form_elements($submission, $mform, $data)) { | |
2988 | $mform->removeElement('header_' . $plugin->get_type()); | |
2989 | } | |
2990 | } | |
2991 | } | |
2992 | } | |
2993 | ||
2994 | /** | |
2995 | * check if feedback plugins installed are enabled | |
2996 | * | |
2997 | * @return bool | |
2998 | */ | |
2999 | public function is_any_feedback_plugin_enabled() { | |
3000 | if (!isset($this->cache['any_feedback_plugin_enabled'])) { | |
3001 | $this->cache['any_feedback_plugin_enabled'] = false; | |
3002 | foreach ($this->feedbackplugins as $plugin) { | |
3003 | if ($plugin->is_enabled() && $plugin->is_visible()) { | |
3004 | $this->cache['any_feedback_plugin_enabled'] = true; | |
3005 | break; | |
3006 | } | |
3007 | } | |
3008 | } | |
3009 | ||
3010 | return $this->cache['any_feedback_plugin_enabled']; | |
3011 | ||
3012 | } | |
3013 | ||
3014 | /** | |
3015 | * check if submission plugins installed are enabled | |
3016 | * | |
3017 | * @return bool | |
3018 | */ | |
3019 | public function is_any_submission_plugin_enabled() { | |
3020 | if (!isset($this->cache['any_submission_plugin_enabled'])) { | |
3021 | $this->cache['any_submission_plugin_enabled'] = false; | |
3022 | foreach ($this->submissionplugins as $plugin) { | |
3023 | if ($plugin->is_enabled() && $plugin->is_visible() && $plugin->allow_submissions()) { | |
3024 | $this->cache['any_submission_plugin_enabled'] = true; | |
3025 | break; | |
3026 | } | |
3027 | } | |
3028 | } | |
3029 | ||
3030 | return $this->cache['any_submission_plugin_enabled']; | |
3031 | ||
3032 | } | |
3033 | ||
3034 | /** | |
3035 | * add elements to submission form | |
3036 | * @param MoodleQuickForm $mform | |
3037 | * @param stdClass $data | |
3038 | * @return void | |
3039 | */ | |
3040 | public function add_submission_form_elements(MoodleQuickForm $mform, stdClass $data) { | |
3041 | global $USER; | |
3042 | ||
3043 | // online text submissions | |
3044 | ||
3045 | $submission = $this->get_user_submission($USER->id, false); | |
3046 | ||
3047 | $this->add_plugin_submission_elements($submission, $mform, $data); | |
3048 | ||
3049 | // hidden params | |
3050 | $mform->addElement('hidden', 'id', $this->get_course_module()->id); | |
3051 | $mform->setType('id', PARAM_INT); | |
3052 | ||
3053 | $mform->addElement('hidden', 'action', 'savesubmission'); | |
3054 | $mform->setType('action', PARAM_TEXT); | |
3055 | // buttons | |
3056 | ||
3057 | } | |
3058 | ||
3059 | /** | |
3060 | * revert to draft | |
3061 | * Uses url parameter userid | |
9682626e | 3062 | * |
bbd0e548 DW |
3063 | * @param int $userid |
3064 | * @return void | |
3065 | */ | |
3066 | private function process_revert_to_draft($userid = 0) { | |
3067 | global $USER, $DB; | |
3068 | ||
3069 | // Need grade permission | |
3070 | require_capability('mod/assign:grade', $this->context); | |
3071 | require_sesskey(); | |
3072 | ||
3073 | if (!$userid) { | |
3074 | $userid = required_param('userid', PARAM_INT); | |
3075 | } | |
3076 | ||
3077 | $submission = $this->get_user_submission($userid, false); | |
3078 | if (!$submission) { | |
3079 | return; | |
3080 | } | |
3081 | $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT; | |
3082 | $this->update_submission($submission, false); | |
3083 | ||
3084 | // update the modified time on the grade (grader modified) | |
3085 | $grade = $this->get_user_grade($userid, true); | |
3086 | $this->update_grade($grade); | |
3087 | ||
3088 | $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); | |
3089 | ||
3090 | $this->add_to_log('revert submission to draft', get_string('reverttodraftforstudent', 'assign', array('id'=>$user->id, 'fullname'=>fullname($user)))); | |
3091 | ||
3092 | } | |
3093 | ||
3094 | /** | |
3095 | * lock the process | |
3096 | * Uses url parameter userid | |
3097 | * @param int $userid | |
3098 | * @return void | |
3099 | */ | |
3100 | private function process_lock($userid = 0) { | |
3101 | global $USER, $DB; | |
3102 | ||
3103 | // Need grade permission | |
3104 | require_capability('mod/assign:grade', $this->context); | |
3105 | require_sesskey(); | |
3106 | ||
3107 | if (!$userid) { | |
3108 | $userid = required_param('userid', PARAM_INT); | |
3109 | } | |
3110 | ||
3111 | $grade = $this->get_user_grade($userid, true); | |
3112 | $grade->locked = 1; | |
3113 | $grade->grader = $USER->id; | |
3114 | $this->update_grade($grade); | |
3115 | ||
3116 | $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); | |
3117 | ||
3118 | $this->add_to_log('lock submission', get_string('locksubmissionforstudent', 'assign', array('id'=>$user->id, 'fullname'=>fullname($user)))); | |
3119 | } | |
3120 | ||
3121 | /** | |
3122 | * unlock the process | |
3123 | * | |
3124 | * @param int $userid | |
3125 | * @return void | |
3126 | */ | |
3127 | private function process_unlock($userid = 0) { | |
3128 | global $USER, $DB; | |
3129 | ||
3130 | // Need grade permission | |
3131 | require_capability('mod/assign:grade', $this->context); | |
3132 | require_sesskey(); | |
3133 | ||
3134 | if (!$userid) { | |
3135 | $userid = required_param('userid', PARAM_INT); | |
3136 | } | |
3137 | ||
3138 | $grade = $this->get_user_grade($userid, true); | |
3139 | $grade->locked = 0; | |
3140 | $grade->grader = $USER->id; | |
3141 | $this->update_grade($grade); | |
3142 | ||
3143 | $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); | |
3144 | ||
3145 | $this->add_to_log('unlock submission', get_string('unlocksubmissionforstudent', 'assign', array('id'=>$user->id, 'fullname'=>fullname($user)))); | |
3146 | } | |
3147 | ||
c45261df DW |
3148 | /** |
3149 | * save outcomes submitted from grading form | |
3150 | * | |
3151 | * @param int $userid | |
3152 | * @param stdClass $formdata | |
3153 | */ | |
3154 | private function process_outcomes($userid, $formdata) { | |
3155 | global $CFG, $USER; | |
3156 | ||
3157 | if (empty($CFG->enableoutcomes)) { | |
3158 | return; | |
3159 | } | |
09b46f0e AA |
3160 | if ($this->grading_disabled($userid)) { |
3161 | return; | |
3162 | } | |
c45261df DW |
3163 | |
3164 | require_once($CFG->libdir.'/gradelib.php'); | |
3165 | ||
3166 | $data = array(); | |
3167 | $gradinginfo = grade_get_grades($this->get_course()->id, | |
3168 | 'mod', | |
3169 | 'assign', | |
3170 | $this->get_instance()->id, | |
3171 | $userid); | |
3172 | ||
3173 | if (!empty($gradinginfo->outcomes)) { | |
3174 | foreach($gradinginfo->outcomes as $index=>$oldoutcome) { | |
3175 | $name = 'outcome_'.$index; | |
3176 | if (isset($formdata->{$name}[$userid]) and $oldoutcome->grades[$userid]->grade != $formdata->{$name}[$userid]) { | |
3177 | $data[$index] = $formdata->{$name}[$userid]; | |
3178 | } | |
3179 | } | |
3180 | } | |
3181 | if (count($data) > 0) { | |
3182 | grade_update_outcomes('mod/assign', $this->course->id, 'mod', 'assign', $this->get_instance()->id, $userid, $data); | |
3183 | } | |
3184 | ||
3185 | } | |
3186 | ||
3187 | ||
bbd0e548 DW |
3188 | /** |
3189 | * save grade | |
3190 | * | |
3191 | * @param moodleform $mform | |
3192 | * @return bool - was the grade saved | |
3193 | */ | |
3194 | private function process_save_grade(&$mform) { | |
3195 | global $USER, $DB, $CFG; | |
3196 | // Include grade form | |
3197 | require_once($CFG->dirroot . '/mod/assign/gradeform.php'); | |
3198 | ||
3199 | // Need submit permission to submit an assignment | |
3200 | require_capability('mod/assign:grade', $this->context); | |
3201 | require_sesskey(); | |
3202 | ||
3203 | $rownum = required_param('rownum', PARAM_INT); | |
3204 | $useridlist = optional_param('useridlist', '', PARAM_TEXT); | |
3205 | if ($useridlist) { | |
3206 | $useridlist = explode(',', $useridlist); | |
3207 | } else { | |
3208 | $useridlist = $this->get_grading_userid_list(); | |
3209 | } | |
3210 | $last = false; | |
3211 | $userid = $useridlist[$rownum]; | |
3212 | if ($rownum == count($useridlist) - 1) { | |
3213 | $last = true; | |
3214 | } | |
3215 | ||
3216 | $data = new stdClass(); | |
3217 | $mform = new mod_assign_grade_form(null, array($this, $data, array('rownum'=>$rownum, 'useridlist'=>$useridlist, 'last'=>false)), 'post', '', array('class'=>'gradeform')); | |
3218 | ||
3219 | if ($formdata = $mform->get_data()) { | |
3220 | $grade = $this->get_user_grade($userid, true); | |
3221 | $gradingdisabled = $this->grading_disabled($userid); | |
3222 | $gradinginstance = $this->get_grading_instance($userid, $gradingdisabled); | |
09b46f0e AA |
3223 | if (!$gradingdisabled) { |
3224 | if ($gradinginstance) { | |
3225 | $grade->grade = $gradinginstance->submit_and_get_grade($formdata->advancedgrading, $grade->id); | |
3226 | } else { | |
3227 | // handle the case when grade is set to No Grade | |
3228 | if (isset($formdata->grade)) { | |
3229 | $grade->grade = grade_floatval(unformat_float($formdata->grade)); | |
3230 | } | |
bbd0e548 DW |
3231 | } |
3232 | } | |
3233 | $grade->grader= $USER->id; | |
3234 | ||
cfc81f03 DW |
3235 | $adminconfig = $this->get_admin_config(); |
3236 | $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook; | |
bbd0e548 DW |
3237 | |
3238 | // call save in plugins | |
3239 | foreach ($this->feedbackplugins as $plugin) { | |
3240 | if ($plugin->is_enabled() && $plugin->is_visible()) { | |
3241 | if (!$plugin->save($grade, $formdata)) { | |
3242 | $result = false; | |
3243 | print_error($plugin->get_error()); | |
3244 | } | |
3245 | if (('assignfeedback_' . $plugin->get_type()) == $gradebookplugin) { | |
3246 | // this is the feedback plugin chose to push comments to the gradebook | |
3247 | $grade->feedbacktext = $plugin->text_for_gradebook($grade); | |
3248 | $grade->feedbackformat = $plugin->format_for_gradebook($grade); | |
3249 | } | |
3250 | } | |
3251 | } | |
c45261df | 3252 | $this->process_outcomes($userid, $formdata); |
75f87a57 DW |
3253 | |
3254 | $grade->mailed = 0; | |
3255 | ||
bbd0e548 DW |
3256 | $this->update_grade($grade); |
3257 | ||
3258 | $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); | |
3259 | ||
3260 | $this->add_to_log('grade submission', $this->format_grade_for_log($grade)); | |
3261 | ||
3262 | ||
3263 | } else { | |
3264 | return false; | |
3265 | } | |
3266 | return true; | |
3267 | } | |
3268 | ||
3269 | /** | |
3270 | * This function is a static wrapper around can_upgrade | |
3271 | * | |
3272 | * @param string $type The plugin type | |
3273 | * @param int $version The plugin version | |
3274 | * @return bool | |
3275 | */ | |
3276 | public static function can_upgrade_assignment($type, $version) { | |
3277 | $assignment = new assign(null, null, null); | |
3278 | return $assignment->can_upgrade($type, $version); | |
3279 | } | |
3280 | ||
3281 | /** | |
3282 | * This function returns true if it can upgrade an assignment from the 2.2 | |
3283 | * module. | |
3284 | * @param string $type The plugin type | |
3285 | * @param int $version The plugin version | |
3286 | * @return bool | |
3287 | */ | |
3288 | public function can_upgrade($type, $version) { | |
3289 | if ($type == 'offline' && $version >= 2011112900) { | |
3290 | return true; | |
3291 | } | |
3292 | foreach ($this->submissionplugins as $plugin) { | |
3293 | if ($plugin->can_upgrade($type, $version)) { | |
3294 | return true; | |
3295 | } | |
3296 | } | |
3297 | foreach ($this->feedbackplugins as $plugin) { | |
3298 | if ($plugin->can_upgrade($type, $version)) { | |
3299 | return true; | |
3300 | } | |
3301 | } | |
3302 | return false; | |
3303 | } | |
3304 | ||
3305 | /** | |
3306 | * Copy all the files from the old assignment files area to the new one. | |
3307 | * This is used by the plugin upgrade code. | |
3308 | * | |
3309 | * @param int $oldcontextid The old assignment context id | |
3310 | * @param int $oldcomponent The old assignment component ('assignment') | |
3311 | * @param int $oldfilearea The old assignment filearea ('submissions') | |
3312 | * @param int $olditemid The old submissionid (can be null e.g. intro) | |
3313 | * @param int $newcontextid The new assignment context id | |
3314 | * @param int $newcomponent The new assignment component ('assignment') | |
3315 | * @param int $newfilearea The new assignment filearea ('submissions') | |
3316 | * @param int $newitemid The new submissionid (can be null e.g. intro) | |
3317 | * @return int The number of files copied | |
3318 | */ | |
3319 | public function copy_area_files_for_upgrade($oldcontextid, $oldcomponent, $oldfilearea, $olditemid, $newcontextid, $newcomponent, $newfilearea, $newitemid) { | |
3320 | // Note, this code is based on some code in filestorage - but that code | |
3321 | // deleted the old files (which we don't want) | |
3322 | $count = 0; | |
3323 | ||
3324 | $fs = get_file_storage(); | |
3325 | ||
3326 | $oldfiles = $fs->get_area_files($oldcontextid, $oldcomponent, $oldfilearea, $olditemid, 'id', false); | |
3327 | foreach ($oldfiles as $oldfile) { | |
3328 | $filerecord = new stdClass(); | |
3329 | $filerecord->contextid = $newcontextid; | |
3330 | $filerecord->component = $newcomponent; | |
3331 | $filerecord->filearea = $newfilearea; | |
3332 | $filerecord->itemid = $newitemid; | |
3333 | $fs->create_file_from_storedfile($filerecord, $oldfile); | |
3334 | $count += 1; | |
3335 | } | |
3336 | ||
3337 | return $count; | |
3338 | } | |
3339 | ||
3340 | /** | |
3341 | * Get an upto date list of user grades and feedback for the gradebook | |
3342 | * | |
3343 | * @param int $userid int or 0 for all users | |
3344 | * @return array of grade data formated for the gradebook api | |
3345 | * The data required by the gradebook api is userid, | |
3346 | * rawgrade, | |
3347 | * feedback, | |
3348 | * feedbackformat, | |
3349 | * usermodified, | |
3350 | * dategraded, | |
3351 | * datesubmitted | |
3352 | */ | |
3353 | public function get_user_grades_for_gradebook($userid) { | |
3354 | global $DB, $CFG; | |
3355 | $grades = array(); | |
3356 | $assignmentid = $this->get_instance()->id; | |
3357 | ||
b11808c7 DW |
3358 | $adminconfig = $this->get_admin_config(); |
3359 | $gradebookpluginname = $adminconfig->feedback_plugin_for_gradebook; | |
bbd0e548 DW |
3360 | $gradebookplugin = null; |
3361 | ||
3362 | // find the gradebook plugin | |
3363 | foreach ($this->feedbackplugins as $plugin) { | |
3364 | if ($plugin->is_enabled() && $plugin->is_visible()) { | |
3365 | if (('assignfeedback_' . $plugin->get_type()) == $gradebookpluginname) { | |
3366 | $gradebookplugin = $plugin; | |
3367 | } | |
3368 | } | |
3369 | } | |
3370 | if ($userid) { | |
3371 | $where = ' WHERE u.id = ? '; | |
3372 | } else { | |
3373 | $where = ' WHERE u.id != ? '; | |
3374 | } | |
3375 | ||
3376 | $graderesults = $DB->get_recordset_sql('SELECT u.id as userid, s.timemodified as datesubmitted, g.grade as rawgrade, g.timemodified as dategraded, g.grader as usermodified | |
3377 | FROM {user} u | |
3378 | LEFT JOIN {assign_submission} s ON u.id = s.userid and s.assignment = ? | |
3379 | LEFT JOIN {assign_grades} g ON u.id = g.userid and g.assignment = ? | |
3380 | ' . $where, array($assignmentid, $assignmentid, $userid)); | |
3381 | ||
3382 | ||
3383 | foreach ($graderesults as $result) { | |
3384 | $gradebookgrade = clone $result; | |
3385 | // now get the feedback | |
3386 | if ($gradebookplugin) { | |
3387 | $grade = $this->get_user_grade($result->userid, false); | |
14c46e24 DW |
3388 | if ($grade) { |
3389 | $gradebookgrade->feedbacktext = $gradebookplugin->text_for_gradebook($grade); | |
3390 | $gradebookgrade->feedbackformat = $gradebookplugin->format_for_gradebook($grade); | |
3391 | } | |
bbd0e548 DW |
3392 | } |
3393 | $grades[$gradebookgrade->userid] = $gradebookgrade; | |
3394 | } | |
3395 | ||
3396 | $graderesults->close(); | |
3397 | return $grades; | |
3398 | } | |
3399 | ||
3400 | } | |
3401 |