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