7a8dcae2c43ff5973e18b39383aee2dee229427d
[moodle.git] / mod / assign / locallib.php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
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
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');
42 /** Include accesslib.php */
43 require_once($CFG->libdir.'/accesslib.php');
44 /** Include formslib.php */
45 require_once($CFG->libdir.'/formslib.php');
46 /** Include repository/lib.php */
47 require_once($CFG->dirroot . '/repository/lib.php');
48 /** Include local mod_form.php */
49 require_once($CFG->dirroot.'/mod/assign/mod_form.php');
50 /** gradelib.php */
51 require_once($CFG->libdir.'/gradelib.php');
52 /** grading lib.php */
53 require_once($CFG->dirroot.'/grade/grading/lib.php');
54 /** Include feedbackplugin.php */
55 require_once($CFG->dirroot.'/mod/assign/feedbackplugin.php');
56 /** Include submissionplugin.php */
57 require_once($CFG->dirroot.'/mod/assign/submissionplugin.php');
58 /** Include renderable.php */
59 require_once($CFG->dirroot.'/mod/assign/renderable.php');
60 /** Include gradingtable.php */
61 require_once($CFG->dirroot.'/mod/assign/gradingtable.php');
62 /** Include eventslib.php */
63 require_once($CFG->libdir.'/eventslib.php');
64 /** Include portfolio caller.php */
65 require_once($CFG->libdir . '/portfolio/caller.php');
67 /**
68  * Standard base class for mod_assign (assignment types).
69  *
70  * @package   mod_assign
71  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
72  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
73  */
74 class assign {
77     /** @var stdClass the assignment record that contains the global settings for this assign instance */
78     private $instance;
80     /** @var context the context of the course module for this assign instance (or just the course if we are
81         creating a new one) */
82     private $context;
84     /** @var stdClass the course this assign instance belongs to */
85     private $course;
87     /** @var stdClass the admin config for all assign instances  */
88     private $adminconfig;
91     /** @var assign_renderer the custom renderer for this module */
92     private $output;
94     /** @var stdClass the course module for this assign instance */
95     private $coursemodule;
97     /** @var array cache for things like the coursemodule name or the scale menu - only lives for a single
98         request */
99     private $cache;
101     /** @var array list of the installed submission plugins */
102     private $submissionplugins;
104     /** @var array list of the installed feedback plugins */
105     private $feedbackplugins;
107     /** @var string action to be used to return to this page (without repeating any form submissions etc.) */
108     private $returnaction = 'view';
110     /** @var array params to be used to return to this page */
111     private $returnparams = array();
113     /** @var string modulename prevents excessive calls to get_string */
114     private static $modulename = null;
116     /** @var string modulenameplural prevents excessive calls to get_string */
117     private static $modulenameplural = null;
119     /**
120      * Constructor for the base assign class
121      *
122      * @param mixed $coursemodulecontext context|null the course module context (or the course context if the coursemodule has not been created yet)
123      * @param mixed $coursemodule the current course module if it was already loaded - otherwise this class will load one from the context as required
124      * @param mixed $course the current course  if it was already loaded - otherwise this class will load one from the context as required
125      */
126     public function __construct($coursemodulecontext, $coursemodule, $course) {
127         global $PAGE;
129         $this->context = $coursemodulecontext;
130         $this->coursemodule = $coursemodule;
131         $this->course = $course;
132         $this->cache = array(); // temporary cache only lives for a single request - used to reduce db lookups
134         $this->submissionplugins = $this->load_plugins('assignsubmission');
135         $this->feedbackplugins = $this->load_plugins('assignfeedback');
136     }
138     /**
139      * Set the action and parameters that can be used to return to the current page
140      *
141      * @param string $action The action for the current page
142      * @param array $params An array of name value pairs which form the parameters to return to the current page
143      * @return void
144      */
145     public function register_return_link($action, $params) {
146         $this->returnaction = $action;
147         $this->returnparams = $params;
148     }
150     /**
151      * Return an action that can be used to get back to the current page
152      * @return string action
153      */
154     public function get_return_action() {
155         return $this->returnaction;
156     }
158     /**
159      * Based on the current assignment settings should we display the intro
160      * @return bool showintro
161      */
162     private function show_intro() {
163         if ($this->get_instance()->alwaysshowdescription ||
164                 time() > $this->get_instance()->allowsubmissionsfromdate) {
165             return true;
166         }
167         return false;
168     }
170     /**
171      * Return a list of parameters that can be used to get back to the current page
172      * @return array params
173      */
174     public function get_return_params() {
175         return $this->returnparams;
176     }
178     /**
179      * Set the submitted form data
180      * @param stdClass $data The form data (instance)
181      */
182     public function set_instance(stdClass $data) {
183         $this->instance = $data;
184     }
186     /**
187      * Set the context
188      * @param context $context The new context
189      */
190     public function set_context(context $context) {
191         $this->context = $context;
192     }
194     /**
195      * Set the course data
196      * @param stdClass $course The course data
197      */
198     public function set_course(stdClass $course) {
199         $this->course = $course;
200     }
202     /**
203      * get list of feedback plugins installed
204      * @return array
205      */
206     public function get_feedback_plugins() {
207         return $this->feedbackplugins;
208     }
210     /**
211      * get list of submission plugins installed
212      * @return array
213      */
214     public function get_submission_plugins() {
215         return $this->submissionplugins;
216     }
218     /**
219      * Is blind marking enabled and reveal identities not set yet?
220      *
221      * @return bool
222      */
223     public function is_blind_marking() {
224         return $this->get_instance()->blindmarking && !$this->get_instance()->revealidentities;
225     }
227     /**
228      * Does an assignment have submission(s) or grade(s) already?
229      *
230      * @return bool
231      */
232     public function has_submissions_or_grades() {
233         $allgrades = $this->count_grades();
234         $allsubmissions = $this->count_submissions();
235         if (($allgrades == 0) && ($allsubmissions == 0)) {
236             return false;
237         }
238         return true;
239     }
241     /**
242      * get a specific submission plugin by its type
243      * @param string $subtype assignsubmission | assignfeedback
244      * @param string $type
245      * @return mixed assign_plugin|null
246      */
247     public function get_plugin_by_type($subtype, $type) {
248         $shortsubtype = substr($subtype, strlen('assign'));
249         $name = $shortsubtype . 'plugins';
250         if ($name != 'feedbackplugins' && $name != 'submissionplugins') {
251             return null;
252         }
253         $pluginlist = $this->$name;
254         foreach ($pluginlist as $plugin) {
255             if ($plugin->get_type() == $type) {
256                 return $plugin;
257             }
258         }
259         return null;
260     }
262     /**
263      * Get a feedback plugin by type
264      * @param string $type - The type of plugin e.g comments
265      * @return mixed assign_feedback_plugin|null
266      */
267     public function get_feedback_plugin_by_type($type) {
268         return $this->get_plugin_by_type('assignfeedback', $type);
269     }
271     /**
272      * Get a submission plugin by type
273      * @param string $type - The type of plugin e.g comments
274      * @return mixed assign_submission_plugin|null
275      */
276     public function get_submission_plugin_by_type($type) {
277         return $this->get_plugin_by_type('assignsubmission', $type);
278     }
280     /**
281      * Load the plugins from the sub folders under subtype
282      * @param string $subtype - either submission or feedback
283      * @return array - The sorted list of plugins
284      */
285     private function load_plugins($subtype) {
286         global $CFG;
287         $result = array();
289         $names = get_plugin_list($subtype);
291         foreach ($names as $name => $path) {
292             if (file_exists($path . '/locallib.php')) {
293                 require_once($path . '/locallib.php');
295                 $shortsubtype = substr($subtype, strlen('assign'));
296                 $pluginclass = 'assign_' . $shortsubtype . '_' . $name;
298                 $plugin = new $pluginclass($this, $name);
300                 if ($plugin instanceof assign_plugin) {
301                     $idx = $plugin->get_sort_order();
302                     while (array_key_exists($idx, $result)) $idx +=1;
303                     $result[$idx] = $plugin;
304                 }
305             }
306         }
307         ksort($result);
308         return $result;
309     }
311     /**
312      * Display the assignment, used by view.php
313      *
314      * The assignment is displayed differently depending on your role,
315      * the settings for the assignment and the status of the assignment.
316      * @param string $action The current action if any.
317      * @return void
318      */
319     public function view($action='') {
321         $o = '';
322         $mform = null;
324         // handle form submissions first
325         if ($action == 'savesubmission') {
326             $action = 'editsubmission';
327             if ($this->process_save_submission($mform)) {
328                 $action = 'view';
329             }
330         } else if ($action == 'lock') {
331             $this->process_lock();
332             $action = 'grading';
333         } else if ($action == 'reverttodraft') {
334             $this->process_revert_to_draft();
335             $action = 'grading';
336         } else if ($action == 'unlock') {
337             $this->process_unlock();
338             $action = 'grading';
339         } else if ($action == 'confirmsubmit') {
340             $action = 'submit';
341             if ($this->process_submit_for_grading($mform)) {
342                 $action = 'view';
343             }
344         } else if ($action == 'gradingbatchoperation') {
345             $action = $this->process_grading_batch_operation($mform);
346         } else if ($action == 'submitgrade') {
347             if (optional_param('saveandshownext', null, PARAM_RAW)) {
348                 //save and show next
349                 $action = 'grade';
350                 if ($this->process_save_grade($mform)) {
351                     $action = 'nextgrade';
352                 }
353             } else if (optional_param('nosaveandprevious', null, PARAM_RAW)) {
354                 $action = 'previousgrade';
355             } else if (optional_param('nosaveandnext', null, PARAM_RAW)) {
356                 //show next button
357                 $action = 'nextgrade';
358             } else if (optional_param('savegrade', null, PARAM_RAW)) {
359                 //save changes button
360                 $action = 'grade';
361                 if ($this->process_save_grade($mform)) {
362                     $action = 'grading';
363                 }
364             } else {
365                 //cancel button
366                 $action = 'grading';
367             }
368         } else if ($action == 'quickgrade') {
369             $message = $this->process_save_quick_grades();
370             $action = 'quickgradingresult';
371         } else if ($action == 'saveoptions') {
372             $this->process_save_grading_options();
373             $action = 'grading';
374         } else if ($action == 'saveextension') {
375             $action = 'grantextension';
376             if ($this->process_save_extension($mform)) {
377                 $action = 'grading';
378             }
379         } else if ($action == 'revealidentitiesconfirm') {
380             $this->process_reveal_identities();
381             $action = 'grading';
382         }
384         $returnparams = array('rownum'=>optional_param('rownum', 0, PARAM_INT));
385         $this->register_return_link($action, $returnparams);
387         // now show the right view page
388         if ($action == 'previousgrade') {
389             $mform = null;
390             $o .= $this->view_single_grade_page($mform, -1);
391         } else if ($action == 'quickgradingresult') {
392             $mform = null;
393             $o .= $this->view_quickgrading_result($message);
394         } else if ($action == 'nextgrade') {
395             $mform = null;
396             $o .= $this->view_single_grade_page($mform, 1);
397         } else if ($action == 'grade') {
398             $o .= $this->view_single_grade_page($mform);
399         } else if ($action == 'viewpluginassignfeedback') {
400             $o .= $this->view_plugin_content('assignfeedback');
401         } else if ($action == 'viewpluginassignsubmission') {
402             $o .= $this->view_plugin_content('assignsubmission');
403         } else if ($action == 'editsubmission') {
404             $o .= $this->view_edit_submission_page($mform);
405         } else if ($action == 'grading') {
406             $o .= $this->view_grading_page();
407         } else if ($action == 'downloadall') {
408             $o .= $this->download_submissions();
409         } else if ($action == 'submit') {
410             $o .= $this->check_submit_for_grading($mform);
411         } else if ($action == 'grantextension') {
412             $o .= $this->view_grant_extension($mform);
413         } else if ($action == 'revealidentities') {
414             $o .= $this->view_reveal_identities_confirm($mform);
415         } else if ($action == 'plugingradingbatchoperation') {
416             $o .= $this->view_plugin_grading_batch_operation($mform);
417         } else if ($action == 'viewpluginpage') {
418              $o .= $this->view_plugin_page();
419         } else {
420             $o .= $this->view_submission_page();
421         }
423         return $o;
424     }
427     /**
428      * Add this instance to the database
429      *
430      * @param stdClass $formdata The data submitted from the form
431      * @param bool $callplugins This is used to skip the plugin code
432      *             when upgrading an old assignment to a new one (the plugins get called manually)
433      * @return mixed false if an error occurs or the int id of the new instance
434      */
435     public function add_instance(stdClass $formdata, $callplugins) {
436         global $DB;
438         $err = '';
440         // add the database record
441         $update = new stdClass();
442         $update->name = $formdata->name;
443         $update->timemodified = time();
444         $update->timecreated = time();
445         $update->course = $formdata->course;
446         $update->courseid = $formdata->course;
447         $update->intro = $formdata->intro;
448         $update->introformat = $formdata->introformat;
449         $update->alwaysshowdescription = $formdata->alwaysshowdescription;
450         $update->submissiondrafts = $formdata->submissiondrafts;
451         $update->requiresubmissionstatement = $formdata->requiresubmissionstatement;
452         $update->sendnotifications = $formdata->sendnotifications;
453         $update->sendlatenotifications = $formdata->sendlatenotifications;
454         $update->duedate = $formdata->duedate;
455         $update->cutoffdate = $formdata->cutoffdate;
456         $update->allowsubmissionsfromdate = $formdata->allowsubmissionsfromdate;
457         $update->grade = $formdata->grade;
458         $update->completionsubmit = !empty($formdata->completionsubmit);
459         $update->teamsubmission = $formdata->teamsubmission;
460         $update->requireallteammemberssubmit = $formdata->requireallteammemberssubmit;
461         $update->teamsubmissiongroupingid = $formdata->teamsubmissiongroupingid;
462         $update->blindmarking = $formdata->blindmarking;
464         $returnid = $DB->insert_record('assign', $update);
465         $this->instance = $DB->get_record('assign', array('id'=>$returnid), '*', MUST_EXIST);
466         // cache the course record
467         $this->course = $DB->get_record('course', array('id'=>$formdata->course), '*', MUST_EXIST);
469         if ($callplugins) {
470             // call save_settings hook for submission plugins
471             foreach ($this->submissionplugins as $plugin) {
472                 if (!$this->update_plugin_instance($plugin, $formdata)) {
473                     print_error($plugin->get_error());
474                     return false;
475                 }
476             }
477             foreach ($this->feedbackplugins as $plugin) {
478                 if (!$this->update_plugin_instance($plugin, $formdata)) {
479                     print_error($plugin->get_error());
480                     return false;
481                 }
482             }
484             // in the case of upgrades the coursemodule has not been set so we need to wait before calling these two
485             // TODO: add event to the calendar
486             $this->update_calendar($formdata->coursemodule);
487             // TODO: add the item in the gradebook
488             $this->update_gradebook(false, $formdata->coursemodule);
490         }
492         $update = new stdClass();
493         $update->id = $this->get_instance()->id;
494         $update->nosubmissions = (!$this->is_any_submission_plugin_enabled()) ? 1: 0;
495         $DB->update_record('assign', $update);
497         return $returnid;
498     }
500     /**
501      * Delete all grades from the gradebook for this assignment
502      *
503      * @return bool
504      */
505     private function delete_grades() {
506         global $CFG;
508         return grade_update('mod/assign', $this->get_course()->id, 'mod', 'assign', $this->get_instance()->id, 0, NULL, array('deleted'=>1)) == GRADE_UPDATE_OK;
509     }
511     /**
512      * Delete this instance from the database
513      *
514      * @return bool false if an error occurs
515      */
516     public function delete_instance() {
517         global $DB;
518         $result = true;
520         foreach ($this->submissionplugins as $plugin) {
521             if (!$plugin->delete_instance()) {
522                 print_error($plugin->get_error());
523                 $result = false;
524             }
525         }
526         foreach ($this->feedbackplugins as $plugin) {
527             if (!$plugin->delete_instance()) {
528                 print_error($plugin->get_error());
529                 $result = false;
530             }
531         }
533         // delete files associated with this assignment
534         $fs = get_file_storage();
535         if (! $fs->delete_area_files($this->context->id) ) {
536             $result = false;
537         }
539         // delete_records will throw an exception if it fails - so no need for error checking here
541         $DB->delete_records('assign_submission', array('assignment'=>$this->get_instance()->id));
542         $DB->delete_records('assign_grades', array('assignment'=>$this->get_instance()->id));
543         $DB->delete_records('assign_plugin_config', array('assignment'=>$this->get_instance()->id));
545         // delete items from the gradebook
546         if (! $this->delete_grades()) {
547             $result = false;
548         }
550         // delete the instance
551         $DB->delete_records('assign', array('id'=>$this->get_instance()->id));
553         return $result;
554     }
556     /**
557     * Actual implementation of the reset course functionality, delete all the
558     * assignment submissions for course $data->courseid.
559     *
560     * @param $data the data submitted from the reset course.
561     * @return array status array
562     */
563     public function reset_userdata($data) {
564         global $CFG,$DB;
566         $componentstr = get_string('modulenameplural', 'assign');
567         $status = array();
569         $fs = get_file_storage();
570         if (!empty($data->reset_assign_submissions)) {
571             // Delete files associated with this assignment.
572             foreach ($this->submissionplugins as $plugin) {
573                 $fileareas = array();
574                 $plugincomponent = $plugin->get_subtype() . '_' . $plugin->get_type();
575                 $fileareas = $plugin->get_file_areas();
576                 foreach ($fileareas as $filearea) {
577                     $fs->delete_area_files($this->context->id, $plugincomponent, $filearea);
578                 }
580                 if (!$plugin->delete_instance()) {
581                     $status[] = array('component'=>$componentstr,
582                                       'item'=>get_string('deleteallsubmissions','assign'),
583                                       'error'=>$plugin->get_error());
584                 }
585             }
587             foreach ($this->feedbackplugins as $plugin) {
588                 $fileareas = array();
589                 $plugincomponent = $plugin->get_subtype() . '_' . $plugin->get_type();
590                 $fileareas = $plugin->get_file_areas();
591                 foreach ($fileareas as $filearea) {
592                     $fs->delete_area_files($this->context->id, $plugincomponent, $filearea);
593                 }
595                 if (!$plugin->delete_instance()) {
596                     $status[] = array('component'=>$componentstr,
597                                       'item'=>get_string('deleteallsubmissions','assign'),
598                                       'error'=>$plugin->get_error());
599                 }
600             }
602             $assignssql = "SELECT a.id
603                              FROM {assign} a
604                            WHERE a.course=:course";
605             $params = array ("course" => $data->courseid);
607             $DB->delete_records_select('assign_submission', "assignment IN ($assignssql)", $params);
608             $status[] = array('component'=>$componentstr,
609                               'item'=>get_string('deleteallsubmissions','assign'),
610                               'error'=>false);
612             if (empty($data->reset_gradebook_grades)) {
613                 // Remove all grades from gradebook.
614                 require_once($CFG->dirroot.'/mod/assign/lib.php');
615                 assign_reset_gradebook($data->courseid);
616             }
617         }
618         // Updating dates - shift may be negative too.
619         if ($data->timeshift) {
620             shift_course_mod_dates('assign',
621                                     array('duedate', 'allowsubmissionsfromdate','cutoffdate'),
622                                     $data->timeshift,
623                                     $data->courseid);
624             $status[] = array('component'=>$componentstr,
625                               'item'=>get_string('datechanged'),
626                               'error'=>false);
627         }
629         return $status;
630     }
632     /**
633      * Update the settings for a single plugin
634      *
635      * @param assign_plugin $plugin The plugin to update
636      * @param stdClass $formdata The form data
637      * @return bool false if an error occurs
638      */
639     private function update_plugin_instance(assign_plugin $plugin, stdClass $formdata) {
640         if ($plugin->is_visible()) {
641             $enabledname = $plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled';
642             if ($formdata->$enabledname) {
643                 $plugin->enable();
644                 if (!$plugin->save_settings($formdata)) {
645                     print_error($plugin->get_error());
646                     return false;
647                 }
648             } else {
649                 $plugin->disable();
650             }
651         }
652         return true;
653     }
655     /**
656      * Update the gradebook information for this assignment
657      *
658      * @param bool $reset If true, will reset all grades in the gradbook for this assignment
659      * @param int $coursemoduleid This is required because it might not exist in the database yet
660      * @return bool
661      */
662     public function update_gradebook($reset, $coursemoduleid) {
663          global $CFG;
664         /** Include lib.php */
665         require_once($CFG->dirroot.'/mod/assign/lib.php');
666         $assign = clone $this->get_instance();
667         $assign->cmidnumber = $coursemoduleid;
668         $param = null;
669         if ($reset) {
670             $param = 'reset';
671         }
673         return assign_grade_item_update($assign, $param);
674     }
676     /** Load and cache the admin config for this module
677      *
678      * @return stdClass the plugin config
679      */
680     public function get_admin_config() {
681         if ($this->adminconfig) {
682             return $this->adminconfig;
683         }
684         $this->adminconfig = get_config('assign');
685         return $this->adminconfig;
686     }
689     /**
690      * Update the calendar entries for this assignment
691      *
692      * @param int $coursemoduleid - Required to pass this in because it might not exist in the database yet
693      * @return bool
694      */
695     public function update_calendar($coursemoduleid) {
696         global $DB, $CFG;
697         require_once($CFG->dirroot.'/calendar/lib.php');
699         // special case for add_instance as the coursemodule has not been set yet.
701         if ($this->get_instance()->duedate) {
702             $event = new stdClass();
704             if ($event->id = $DB->get_field('event', 'id', array('modulename'=>'assign', 'instance'=>$this->get_instance()->id))) {
706                 $event->name        = $this->get_instance()->name;
708                 $event->description = format_module_intro('assign', $this->get_instance(), $coursemoduleid);
709                 $event->timestart   = $this->get_instance()->duedate;
711                 $calendarevent = calendar_event::load($event->id);
712                 $calendarevent->update($event);
713             } else {
714                 $event = new stdClass();
715                 $event->name        = $this->get_instance()->name;
716                 $event->description = format_module_intro('assign', $this->get_instance(), $coursemoduleid);
717                 $event->courseid    = $this->get_instance()->course;
718                 $event->groupid     = 0;
719                 $event->userid      = 0;
720                 $event->modulename  = 'assign';
721                 $event->instance    = $this->get_instance()->id;
722                 $event->eventtype   = 'due';
723                 $event->timestart   = $this->get_instance()->duedate;
724                 $event->timeduration = 0;
726                 calendar_event::create($event);
727             }
728         } else {
729             $DB->delete_records('event', array('modulename'=>'assign', 'instance'=>$this->get_instance()->id));
730         }
731     }
734     /**
735      * Update this instance in the database
736      *
737      * @param stdClass $formdata - the data submitted from the form
738      * @return bool false if an error occurs
739      */
740     public function update_instance($formdata) {
741         global $DB;
743         $update = new stdClass();
744         $update->id = $formdata->instance;
745         $update->name = $formdata->name;
746         $update->timemodified = time();
747         $update->course = $formdata->course;
748         $update->intro = $formdata->intro;
749         $update->introformat = $formdata->introformat;
750         $update->alwaysshowdescription = $formdata->alwaysshowdescription;
751         $update->submissiondrafts = $formdata->submissiondrafts;
752         $update->requiresubmissionstatement = $formdata->requiresubmissionstatement;
753         $update->sendnotifications = $formdata->sendnotifications;
754         $update->sendlatenotifications = $formdata->sendlatenotifications;
755         $update->duedate = $formdata->duedate;
756         $update->cutoffdate = $formdata->cutoffdate;
757         $update->allowsubmissionsfromdate = $formdata->allowsubmissionsfromdate;
758         $update->grade = $formdata->grade;
759         $update->completionsubmit = !empty($formdata->completionsubmit);
760         $update->teamsubmission = $formdata->teamsubmission;
761         $update->requireallteammemberssubmit = $formdata->requireallteammemberssubmit;
762         $update->teamsubmissiongroupingid = $formdata->teamsubmissiongroupingid;
763         $update->blindmarking = $formdata->blindmarking;
765         $result = $DB->update_record('assign', $update);
766         $this->instance = $DB->get_record('assign', array('id'=>$update->id), '*', MUST_EXIST);
768         // load the assignment so the plugins have access to it
770         // call save_settings hook for submission plugins
771         foreach ($this->submissionplugins as $plugin) {
772             if (!$this->update_plugin_instance($plugin, $formdata)) {
773                 print_error($plugin->get_error());
774                 return false;
775             }
776         }
777         foreach ($this->feedbackplugins as $plugin) {
778             if (!$this->update_plugin_instance($plugin, $formdata)) {
779                 print_error($plugin->get_error());
780                 return false;
781             }
782         }
785         // update the database record
788         // update all the calendar events
789         $this->update_calendar($this->get_course_module()->id);
791         $this->update_gradebook(false, $this->get_course_module()->id);
793         $update = new stdClass();
794         $update->id = $this->get_instance()->id;
795         $update->nosubmissions = (!$this->is_any_submission_plugin_enabled()) ? 1: 0;
796         $DB->update_record('assign', $update);
802         return $result;
803     }
805     /**
806      * add elements in grading plugin form
807      *
808      * @param mixed $grade stdClass|null
809      * @param MoodleQuickForm $mform
810      * @param stdClass $data
811      * @param int $userid - The userid we are grading
812      * @return void
813      */
814     private function add_plugin_grade_elements($grade, MoodleQuickForm $mform, stdClass $data, $userid) {
815         foreach ($this->feedbackplugins as $plugin) {
816             if ($plugin->is_enabled() && $plugin->is_visible()) {
817                 $mform->addElement('header', 'header_' . $plugin->get_type(), $plugin->get_name());
818                 if (!$plugin->get_form_elements_for_user($grade, $mform, $data, $userid)) {
819                     $mform->removeElement('header_' . $plugin->get_type());
820                 }
821             }
822         }
823     }
827     /**
828      * Add one plugins settings to edit plugin form
829      *
830      * @param assign_plugin $plugin The plugin to add the settings from
831      * @param MoodleQuickForm $mform The form to add the configuration settings to. This form is modified directly (not returned)
832      * @return void
833      */
834     private function add_plugin_settings(assign_plugin $plugin, MoodleQuickForm $mform) {
835         global $CFG;
836         if ($plugin->is_visible()) {
837             // enabled
838             //tied disableIf rule to this select element
839             $mform->addElement('selectyesno', $plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', $plugin->get_name());
840             $mform->addHelpButton($plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', 'enabled', $plugin->get_subtype() . '_' . $plugin->get_type());
843             $default = get_config($plugin->get_subtype() . '_' . $plugin->get_type(), 'default');
844             if ($plugin->get_config('enabled') !== false) {
845                 $default = $plugin->is_enabled();
846             }
847             $mform->setDefault($plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', $default);
849             $plugin->get_settings($mform);
851         }
853     }
856     /**
857      * Add settings to edit plugin form
858      *
859      * @param MoodleQuickForm $mform The form to add the configuration settings to. This form is modified directly (not returned)
860      * @return void
861      */
862     public function add_all_plugin_settings(MoodleQuickForm $mform) {
863         $mform->addElement('header', 'general', get_string('submissionsettings', 'assign'));
865         foreach ($this->submissionplugins as $plugin) {
866             $this->add_plugin_settings($plugin, $mform);
868         }
869         $mform->addElement('header', 'general', get_string('feedbacksettings', 'assign'));
870         foreach ($this->feedbackplugins as $plugin) {
871             $this->add_plugin_settings($plugin, $mform);
872         }
873     }
875     /**
876      * Allow each plugin an opportunity to update the defaultvalues
877      * passed in to the settings form (needed to set up draft areas for
878      * editor and filemanager elements)
879      * @param array $defaultvalues
880      */
881     public function plugin_data_preprocessing(&$defaultvalues) {
882         foreach ($this->submissionplugins as $plugin) {
883             if ($plugin->is_visible()) {
884                 $plugin->data_preprocessing($defaultvalues);
885             }
886         }
887         foreach ($this->feedbackplugins as $plugin) {
888             if ($plugin->is_visible()) {
889                 $plugin->data_preprocessing($defaultvalues);
890             }
891         }
892     }
894     /**
895      * Get the name of the current module.
896      *
897      * @return string the module name (Assignment)
898      */
899     protected function get_module_name() {
900         if (isset(self::$modulename)) {
901             return self::$modulename;
902         }
903         self::$modulename = get_string('modulename', 'assign');
904         return self::$modulename;
905     }
907     /**
908      * Get the plural name of the current module.
909      *
910      * @return string the module name plural (Assignments)
911      */
912     protected function get_module_name_plural() {
913         if (isset(self::$modulenameplural)) {
914             return self::$modulenameplural;
915         }
916         self::$modulenameplural = get_string('modulenameplural', 'assign');
917         return self::$modulenameplural;
918     }
920     /**
921      * Has this assignment been constructed from an instance?
922      *
923      * @return bool
924      */
925     public function has_instance() {
926         return $this->instance || $this->get_course_module();
927     }
929     /**
930      * Get the settings for the current instance of this assignment
931      *
932      * @return stdClass The settings
933      */
934     public function get_instance() {
935         global $DB;
936         if ($this->instance) {
937             return $this->instance;
938         }
939         if ($this->get_course_module()) {
940             $this->instance = $DB->get_record('assign', array('id' => $this->get_course_module()->instance), '*', MUST_EXIST);
941         }
942         if (!$this->instance) {
943             throw new coding_exception('Improper use of the assignment class. Cannot load the assignment record.');
944         }
945         return $this->instance;
946     }
948     /**
949      * Get the context of the current course
950      * @return mixed context|null The course context
951      */
952     public function get_course_context() {
953         if (!$this->context && !$this->course) {
954             throw new coding_exception('Improper use of the assignment class. Cannot load the course context.');
955         }
956         if ($this->context) {
957             return $this->context->get_course_context();
958         } else {
959             return context_course::instance($this->course->id);
960         }
961     }
964     /**
965      * Get the current course module
966      *
967      * @return mixed stdClass|null The course module
968      */
969     public function get_course_module() {
970         if ($this->coursemodule) {
971             return $this->coursemodule;
972         }
973         if (!$this->context) {
974             return null;
975         }
977         if ($this->context->contextlevel == CONTEXT_MODULE) {
978             $this->coursemodule = get_coursemodule_from_id('assign', $this->context->instanceid, 0, false, MUST_EXIST);
979             return $this->coursemodule;
980         }
981         return null;
982     }
984     /**
985      * Get context module
986      *
987      * @return context
988      */
989     public function get_context() {
990         return $this->context;
991     }
993     /**
994      * Get the current course
995      * @return mixed stdClass|null The course
996      */
997     public function get_course() {
998         global $DB;
999         if ($this->course) {
1000             return $this->course;
1001         }
1003         if (!$this->context) {
1004             return null;
1005         }
1006         $this->course = $DB->get_record('course', array('id' => $this->get_course_context()->instanceid), '*', MUST_EXIST);
1007         return $this->course;
1008     }
1010     /**
1011      * Return a grade in user-friendly form, whether it's a scale or not
1012      *
1013      * @param mixed $grade int|null
1014      * @param boolean $editing Are we allowing changes to this grade?
1015      * @param int $userid The user id the grade belongs to
1016      * @param int $modified Timestamp from when the grade was last modified
1017      * @return string User-friendly representation of grade
1018      */
1019     public function display_grade($grade, $editing, $userid=0, $modified=0) {
1020         global $DB;
1022         static $scalegrades = array();
1024         if ($this->get_instance()->grade >= 0) {
1025             // Normal number
1026             if ($editing && $this->get_instance()->grade > 0) {
1027                 if ($grade < 0) {
1028                     $displaygrade = '';
1029                 } else {
1030                     $displaygrade = format_float($grade);
1031                 }
1032                 $o = '<label class="accesshide" for="quickgrade_' . $userid . '">' . get_string('usergrade', 'assign') . '</label>';
1033                 $o .= '<input type="text" id="quickgrade_' . $userid . '" name="quickgrade_' . $userid . '" value="' . $displaygrade
1034                         . '" size="6" maxlength="10" class="quickgrade"/>';
1035                 $o .= '&nbsp;/&nbsp;' . format_float($this->get_instance()->grade,2);
1036                 $o .= '<input type="hidden" name="grademodified_' . $userid . '" value="' . $modified . '"/>';
1037                 return $o;
1038             } else {
1039                 if ($grade == -1 || $grade === null) {
1040                     return '-';
1041                 } else {
1042                     return format_float(($grade),2) .'&nbsp;/&nbsp;'. format_float($this->get_instance()->grade,2);
1043                 }
1044             }
1046         } else {
1047             // Scale
1048             if (empty($this->cache['scale'])) {
1049                 if ($scale = $DB->get_record('scale', array('id'=>-($this->get_instance()->grade)))) {
1050                     $this->cache['scale'] = make_menu_from_list($scale->scale);
1051                 } else {
1052                     return '-';
1053                 }
1054             }
1055             if ($editing) {
1056                 $o = '<label class="accesshide" for="quickgrade_' . $userid . '">' . get_string('usergrade', 'assign') . '</label>';
1057                 $o .= '<select name="quickgrade_' . $userid . '" class="quickgrade">';
1058                 $o .= '<option value="-1">' . get_string('nograde') . '</option>';
1059                 foreach ($this->cache['scale'] as $optionid => $option) {
1060                     $selected = '';
1061                     if ($grade == $optionid) {
1062                         $selected = 'selected="selected"';
1063                     }
1064                     $o .= '<option value="' . $optionid . '" ' . $selected . '>' . $option . '</option>';
1065                 }
1066                 $o .= '</select>';
1067                 $o .= '<input type="hidden" name="grademodified_' . $userid . '" value="' . $modified . '"/>';
1068                 return $o;
1069             } else {
1070                 $scaleid = (int)$grade;
1071                 if (isset($this->cache['scale'][$scaleid])) {
1072                     return $this->cache['scale'][$scaleid];
1073                 }
1074                 return '-';
1075             }
1076         }
1077     }
1079     /**
1080      * Load a list of users enrolled in the current course with the specified permission and group (0 for no group)
1081      *
1082      * @param int $currentgroup
1083      * @param bool $idsonly
1084      * @return array List of user records
1085      */
1086     public function list_participants($currentgroup, $idsonly) {
1087         if ($idsonly) {
1088             return get_enrolled_users($this->context, "mod/assign:submit", $currentgroup, 'u.id');
1089         } else {
1090             return get_enrolled_users($this->context, "mod/assign:submit", $currentgroup);
1091         }
1092     }
1094     /**
1095      * Load a count of valid teams for this assignment
1096      *
1097      * @return int number of valid teams
1098      */
1099     public function count_teams() {
1101         $groups = groups_get_all_groups($this->get_course()->id, 0, $this->get_instance()->teamsubmissiongroupingid, 'g.id');
1102         $count = count($groups);
1104         // See if there are any users in the default group.
1105         $defaultusers = $this->get_submission_group_members(0, true);
1106         if (count($defaultusers) > 0) {
1107             $count += 1;
1108         }
1109         return $count;
1110     }
1112     /**
1113      * Load a count of users enrolled in the current course with the specified permission and group (0 for no group)
1114      *
1115      * @param int $currentgroup
1116      * @return int number of matching users
1117      */
1118     public function count_participants($currentgroup) {
1119         return count_enrolled_users($this->context, "mod/assign:submit", $currentgroup);
1120     }
1122     /**
1123      * Load a count of users submissions in the current module that require grading
1124      * This means the submission modification time is more recent than the
1125      * grading modification time.
1126      *
1127      * @return int number of matching submissions
1128      */
1129     public function count_submissions_need_grading() {
1130         global $DB;
1132         $params = array($this->get_course_module()->instance);
1134         return $DB->count_records_sql("SELECT COUNT('x')
1135                                        FROM {assign_submission} s
1136                                        LEFT JOIN {assign_grades} g ON s.assignment = g.assignment AND s.userid = g.userid
1137                                        WHERE s.assignment = ?
1138                                            AND s.timemodified IS NOT NULL
1139                                            AND (s.timemodified > g.timemodified OR g.timemodified IS NULL)",
1140                                        $params);
1141     }
1143     /**
1144      * Load a count of grades
1145      *
1146      * @return int number of grades
1147      */
1148     public function count_grades() {
1149         global $DB;
1151         if (!$this->has_instance()) {
1152             return 0;
1153         }
1155         $sql = 'SELECT COUNT(id) FROM {assign_grades} WHERE assignment = ?';
1156         $params = array($this->get_course_module()->instance);
1158         return $DB->count_records_sql($sql, $params);
1159     }
1161     /**
1162      * Load a count of submissions
1163      *
1164      * @return int number of submissions
1165      */
1166     public function count_submissions() {
1167         global $DB;
1169         if (!$this->has_instance()) {
1170             return 0;
1171         }
1173         $sql = 'SELECT COUNT(id) FROM {assign_submission} WHERE assignment = ?';
1174         $params = array($this->get_course_module()->instance);
1176         if ($this->get_instance()->teamsubmission) {
1177             // only look at team submissions
1178             $sql .= ' AND userid = ?';
1179             $params[] = 0;
1180         }
1181         return $DB->count_records_sql($sql, $params);
1182     }
1184     /**
1185      * Load a count of submissions with a specified status
1186      *
1187      * @param string $status The submission status - should match one of the constants
1188      * @return int number of matching submissions
1189      */
1190     public function count_submissions_with_status($status) {
1191         global $DB;
1192         $sql = 'SELECT COUNT(id) FROM {assign_submission} WHERE assignment = ? AND status = ?';
1193         $params = array($this->get_course_module()->instance, $status);
1195         if ($this->get_instance()->teamsubmission) {
1196             // only look at team submissions
1197             $sql .= ' AND userid = ?';
1198             $params[] = 0;
1199         }
1200         return $DB->count_records_sql($sql, $params);
1201     }
1203     /**
1204      * Utility function to get the userid for every row in the grading table
1205      * so the order can be frozen while we iterate it
1206      *
1207      * @return array An array of userids
1208      */
1209     private function get_grading_userid_list() {
1210         $filter = get_user_preferences('assign_filter', '');
1211         $table = new assign_grading_table($this, 0, $filter, 0, false);
1213         $useridlist = $table->get_column_data('userid');
1215         return $useridlist;
1216     }
1219     /**
1220      * Utility function get the userid based on the row number of the grading table.
1221      * This takes into account any active filters on the table.
1222      *
1223      * @param int $num The row number of the user
1224      * @param bool $last This is set to true if this is the last user in the table
1225      * @return mixed The user id of the matching user or false if there was an error
1226      */
1227     private function get_userid_for_row($num, $last) {
1228         if (!array_key_exists('userid_for_row', $this->cache)) {
1229             $this->cache['userid_for_row'] = array();
1230         }
1231         if (array_key_exists($num, $this->cache['userid_for_row'])) {
1232             list($userid, $last) = $this->cache['userid_for_row'][$num];
1233             return $userid;
1234         }
1236         $filter = get_user_preferences('assign_filter', '');
1237         $table = new assign_grading_table($this, 0, $filter, 0, false);
1239         $userid = $table->get_cell_data($num, 'userid', $last);
1241         $this->cache['userid_for_row'][$num] = array($userid, $last);
1242         return $userid;
1243     }
1245     /**
1246      * Generate zip file from array of given files
1247      *
1248      * @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
1249      * @return path of temp file - note this returned file does not have a .zip extension - it is a temp file.
1250      */
1251      private function pack_files($filesforzipping) {
1252          global $CFG;
1253          //create path for new zip file.
1254          $tempzip = tempnam($CFG->tempdir.'/', 'assignment_');
1255          //zip files
1256          $zipper = new zip_packer();
1257          if ($zipper->archive_to_pathname($filesforzipping, $tempzip)) {
1258              return $tempzip;
1259          }
1260          return false;
1261     }
1263     /**
1264      * Finds all assignment notifications that have yet to be mailed out, and mails them.
1265      *
1266      * Cron function to be run periodically according to the moodle cron
1267      *
1268      * @return bool
1269      */
1270     static function cron() {
1271         global $DB;
1273         // only ever send a max of one days worth of updates
1274         $yesterday = time() - (24 * 3600);
1275         $timenow   = time();
1277         // Collect all submissions from the past 24 hours that require mailing.
1278         $sql = "SELECT s.*, a.course, a.name, a.blindmarking, a.revealidentities,
1279                        g.*, g.id as gradeid, g.timemodified as lastmodified
1280                  FROM {assign} a
1281                  JOIN {assign_grades} g ON g.assignment = a.id
1282             LEFT JOIN {assign_submission} s ON s.assignment = a.id AND s.userid = g.userid
1283                 WHERE g.timemodified >= :yesterday AND
1284                       g.timemodified <= :today AND
1285                       g.mailed = 0";
1286         $params = array('yesterday' => $yesterday, 'today' => $timenow);
1287         $submissions = $DB->get_records_sql($sql, $params);
1289         if (empty($submissions)) {
1290             mtrace('done.');
1291             return true;
1292         }
1294         mtrace('Processing ' . count($submissions) . ' assignment submissions ...');
1296         // Preload courses we are going to need those.
1297         $courseids = array();
1298         foreach ($submissions as $submission) {
1299             $courseids[] = $submission->course;
1300         }
1301         // Filter out duplicates
1302         $courseids = array_unique($courseids);
1303         $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
1304         list($courseidsql, $params) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
1305         $sql = "SELECT c.*, {$ctxselect}
1306                   FROM {course} c
1307              LEFT JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
1308                  WHERE c.id {$courseidsql}";
1309         $params['contextlevel'] = CONTEXT_COURSE;
1310         $courses = $DB->get_records_sql($sql, $params);
1311         // Clean up... this could go on for a while.
1312         unset($courseids);
1313         unset($ctxselect);
1314         unset($courseidsql);
1315         unset($params);
1317         // Simple array we'll use for caching modules.
1318         $modcache = array();
1320         // Message students about new feedback
1321         foreach ($submissions as $submission) {
1323             mtrace("Processing assignment submission $submission->id ...");
1325             // do not cache user lookups - could be too many
1326             if (!$user = $DB->get_record("user", array("id"=>$submission->userid))) {
1327                 mtrace("Could not find user $submission->userid");
1328                 continue;
1329             }
1331             // use a cache to prevent the same DB queries happening over and over
1332             if (!array_key_exists($submission->course, $courses)) {
1333                 mtrace("Could not find course $submission->course");
1334                 continue;
1335             }
1336             $course = $courses[$submission->course];
1337             if (isset($course->ctxid)) {
1338                 // Context has not yet been preloaded. Do so now.
1339                 context_helper::preload_from_record($course);
1340             }
1342             // Override the language and timezone of the "current" user, so that
1343             // mail is customised for the receiver.
1344             cron_setup_user($user, $course);
1346             // context lookups are already cached
1347             $coursecontext = context_course::instance($course->id);
1348             if (!is_enrolled($coursecontext, $user->id)) {
1349                 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
1350                 mtrace(fullname($user)." not an active participant in " . $courseshortname);
1351                 continue;
1352             }
1354             if (!$grader = $DB->get_record("user", array("id"=>$submission->grader))) {
1355                 mtrace("Could not find grader $submission->grader");
1356                 continue;
1357             }
1359             if (!array_key_exists($submission->assignment, $modcache)) {
1360                 if (! $mod = get_coursemodule_from_instance("assign", $submission->assignment, $course->id)) {
1361                     mtrace("Could not find course module for assignment id $submission->assignment");
1362                     continue;
1363                 }
1364                 $modcache[$submission->assignment] = $mod;
1365             } else {
1366                 $mod = $modcache[$submission->assignment];
1367             }
1368             // context lookups are already cached
1369             $contextmodule = context_module::instance($mod->id);
1371             if (!$mod->visible) {
1372                 // Hold mail notification for hidden assignments until later
1373                 continue;
1374             }
1376             // need to send this to the student
1377             $messagetype = 'feedbackavailable';
1378             $eventtype = 'assign_notification';
1379             $updatetime = $submission->lastmodified;
1380             $modulename = get_string('modulename', 'assign');
1382             $uniqueid = 0;
1383             if ($submission->blindmarking && !$submission->revealidentities) {
1384                 $uniqueid = self::get_uniqueid_for_user_static($submission->assignment, $user->id);
1385             }
1386             self::send_assignment_notification($grader, $user, $messagetype, $eventtype, $updatetime,
1387                                                $mod, $contextmodule, $course, $modulename, $submission->name,
1388                                                $submission->blindmarking && !$submission->revealidentities,
1389                                                $uniqueid);
1391             $grade = new stdClass();
1392             $grade->id = $submission->gradeid;
1393             $grade->mailed = 1;
1394             $DB->update_record('assign_grades', $grade);
1396             mtrace('Done');
1397         }
1398         mtrace('Done processing ' . count($submissions) . ' assignment submissions');
1400         cron_setup_user();
1402         // Free up memory just to be sure
1403         unset($courses);
1404         unset($modcache);
1406         return true;
1407     }
1409     /**
1410      * Update a grade in the grade table for the assignment and in the gradebook
1411      *
1412      * @param stdClass $grade a grade record keyed on id
1413      * @return bool true for success
1414      */
1415     public function update_grade($grade) {
1416         global $DB;
1418         $grade->timemodified = time();
1420         if ($grade->grade && $grade->grade != -1) {
1421             if ($this->get_instance()->grade > 0) {
1422                 if (!is_numeric($grade->grade)) {
1423                     return false;
1424                 } else if ($grade->grade > $this->get_instance()->grade) {
1425                     return false;
1426                 } else if ($grade->grade < 0) {
1427                     return false;
1428                 }
1429             } else {
1430                 // this is a scale
1431                 if ($scale = $DB->get_record('scale', array('id' => -($this->get_instance()->grade)))) {
1432                     $scaleoptions = make_menu_from_list($scale->scale);
1433                     if (!array_key_exists((int) $grade->grade, $scaleoptions)) {
1434                         return false;
1435                     }
1436                 }
1437             }
1438         }
1440         $result = $DB->update_record('assign_grades', $grade);
1441         if ($result) {
1442             $this->gradebook_item_update(null, $grade);
1443         }
1444         return $result;
1445     }
1447     /**
1448      * View the grant extension date page
1449      *
1450      * Uses url parameters 'userid'
1451      * or from parameter 'selectedusers'
1452      * @param moodleform $mform - Used for validation of the submitted data
1453      * @return string
1454      */
1455     private function view_grant_extension($mform) {
1456         global $DB, $CFG;
1457         require_once($CFG->dirroot . '/mod/assign/extensionform.php');
1459         $o = '';
1460         $batchusers = optional_param('selectedusers', '', PARAM_TEXT);
1461         $data = new stdClass();
1462         $data->extensionduedate = null;
1463         $userid = 0;
1464         if (!$batchusers) {
1465             $userid = required_param('userid', PARAM_INT);
1467             $grade = $this->get_user_grade($userid, false);
1469             $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
1471             if ($grade) {
1472                 $data->extensionduedate = $grade->extensionduedate;
1473             }
1474             $data->userid = $userid;
1475         } else {
1476             $data->batchusers = $batchusers;
1477         }
1478         $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
1479                                                       $this->get_context(),
1480                                                       $this->show_intro(),
1481                                                       $this->get_course_module()->id,
1482                                                       get_string('grantextension', 'assign')));
1484         if (!$mform) {
1485             $mform = new mod_assign_extension_form(null, array($this->get_course_module()->id,
1486                                                                $userid,
1487                                                                $batchusers,
1488                                                                $this->get_instance(),
1489                                                                $data));
1490         }
1491         $o .= $this->get_renderer()->render(new assign_form('extensionform', $mform));
1492         $o .= $this->view_footer();
1493         return $o;
1494     }
1496     /**
1497      * Get a list of the users in the same group as this user
1498      *
1499      * @param int $groupid The id of the group whose members we want or 0 for the default group
1500      * @param bool $onlyids Whether to retrieve only the user id's
1501      * @return array The users (possibly id's only)
1502      */
1503     public function get_submission_group_members($groupid, $onlyids) {
1504         $members = array();
1505         if ($groupid != 0) {
1506             if ($onlyids) {
1507                 $allusers = groups_get_members($groupid, 'u.id');
1508             } else {
1509                 $allusers = groups_get_members($groupid);
1510             }
1511             foreach ($allusers as $user) {
1512                 if ($this->get_submission_group($user->id)) {
1513                     $members[] = $user;
1514                 }
1515             }
1516         } else {
1517             $allusers = $this->list_participants(null, $onlyids);
1518             foreach ($allusers as $user) {
1519                 if ($this->get_submission_group($user->id) == null) {
1520                     $members[] = $user;
1521                 }
1522             }
1523         }
1524         return $members;
1525     }
1527     /**
1528      * Get a list of the users in the same group as this user that have not submitted the assignment
1529      *
1530      * @param int $groupid The id of the group whose members we want or 0 for the default group
1531      * @param bool $onlyids Whether to retrieve only the user id's
1532      * @return array The users (possibly id's only)
1533      */
1534     public function get_submission_group_members_who_have_not_submitted($groupid, $onlyids) {
1535         if (!$this->get_instance()->teamsubmission || !$this->get_instance()->requireallteammemberssubmit) {
1536             return array();
1537         }
1538         $members = $this->get_submission_group_members($groupid, $onlyids);
1540         foreach ($members as $id => $member) {
1541             $submission = $this->get_user_submission($member->id, false);
1542             if ($submission && $submission->status != ASSIGN_SUBMISSION_STATUS_DRAFT) {
1543                 unset($members[$id]);
1544             } else {
1545                 if ($this->is_blind_marking()) {
1546                     $members[$id]->alias = get_string('hiddenuser', 'assign') . $this->get_uniqueid_for_user($id);
1547                 }
1548             }
1549         }
1550         return $members;
1551     }
1553     /**
1554      * Load the group submission object for a particular user, optionally creating it if required
1555      *
1556      * This will create the user submission and the group submission if required
1557      *
1558      * @param int $userid The id of the user whose submission we want
1559      * @param int $groupid The id of the group for this user - may be 0 in which case it is determined from the userid
1560      * @param bool $create If set to true a new submission object will be created in the database
1561      * @return stdClass The submission
1562      */
1563     public function get_group_submission($userid, $groupid, $create) {
1564         global $DB;
1566         if ($groupid == 0) {
1567             $group = $this->get_submission_group($userid);
1568             if ($group) {
1569                 $groupid = $group->id;
1570             }
1571         }
1573         if ($create) {
1574             // Make sure there is a submission for this user.
1575             $params = array('assignment'=>$this->get_instance()->id, 'groupid'=>0, 'userid'=>$userid);
1576             $submission = $DB->get_record('assign_submission', $params);
1578             if (!$submission) {
1579                 $submission = new stdClass();
1580                 $submission->assignment   = $this->get_instance()->id;
1581                 $submission->userid       = $userid;
1582                 $submission->groupid      = 0;
1583                 $submission->timecreated  = time();
1584                 $submission->timemodified = $submission->timecreated;
1586                 if ($this->get_instance()->submissiondrafts) {
1587                     $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
1588                 } else {
1589                     $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1590                 }
1591                 $DB->insert_record('assign_submission', $submission);
1592             }
1593         }
1594         // Now get the group submission.
1595         $params = array('assignment'=>$this->get_instance()->id, 'groupid'=>$groupid, 'userid'=>0);
1596         $submission = $DB->get_record('assign_submission', $params);
1598         if ($submission) {
1599             return $submission;
1600         }
1601         if ($create) {
1602             $submission = new stdClass();
1603             $submission->assignment   = $this->get_instance()->id;
1604             $submission->userid       = 0;
1605             $submission->groupid       = $groupid;
1606             $submission->timecreated = time();
1607             $submission->timemodified = $submission->timecreated;
1609             if ($this->get_instance()->submissiondrafts) {
1610                 $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
1611             } else {
1612                 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1613             }
1614             $sid = $DB->insert_record('assign_submission', $submission);
1615             $submission->id = $sid;
1616             return $submission;
1617         }
1618         return false;
1619     }
1621     /**
1622      * View a page rendered by a plugin
1623      *
1624      * Uses url parameters 'pluginaction', 'pluginsubtype', 'plugin', and 'id'
1625      *
1626      * @return string
1627      */
1628     private function view_plugin_page() {
1629         global $USER;
1631         $o = '';
1633         $pluginsubtype = required_param('pluginsubtype', PARAM_ALPHA);
1634         $plugintype = required_param('plugin', PARAM_TEXT);
1635         $pluginaction = required_param('pluginaction', PARAM_ALPHA);
1637         $plugin = $this->get_plugin_by_type($pluginsubtype, $plugintype);
1638         if (!$plugin) {
1639             print_error('invalidformdata', '');
1640             return;
1641         }
1643         $o .= $plugin->view_page($pluginaction);
1645         return $o;
1646     }
1649     /**
1650      * This is used for team assignments to get the group for the specified user.
1651      * If the user is a member of multiple or no groups this will return false
1652      *
1653      * @param int $userid The id of the user whose submission we want
1654      * @return mixed The group or false
1655      */
1656     public function get_submission_group($userid) {
1657         $groups = groups_get_all_groups($this->get_course()->id, $userid, $this->get_instance()->teamsubmissiongroupingid);
1658         if (count($groups) != 1) {
1659             return false;
1660         }
1661         return array_pop($groups);
1662     }
1665     /**
1666      * display the submission that is used by a plugin
1667      * Uses url parameters 'sid', 'gid' and 'plugin'
1668      * @param string $pluginsubtype
1669      * @return string
1670      */
1671     private function view_plugin_content($pluginsubtype) {
1672         global $USER;
1674         $o = '';
1676         $submissionid = optional_param('sid', 0, PARAM_INT);
1677         $gradeid = optional_param('gid', 0, PARAM_INT);
1678         $plugintype = required_param('plugin', PARAM_TEXT);
1679         $item = null;
1680         if ($pluginsubtype == 'assignsubmission') {
1681             $plugin = $this->get_submission_plugin_by_type($plugintype);
1682             if ($submissionid <= 0) {
1683                 throw new coding_exception('Submission id should not be 0');
1684             }
1685             $item = $this->get_submission($submissionid);
1687             // permissions
1688             if ($item->userid != $USER->id) {
1689                 require_capability('mod/assign:grade', $this->context);
1690             }
1691             $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
1692                                                               $this->get_context(),
1693                                                               $this->show_intro(),
1694                                                               $this->get_course_module()->id,
1695                                                               $plugin->get_name()));
1696             $o .= $this->get_renderer()->render(new assign_submission_plugin_submission($plugin,
1697                                                               $item,
1698                                                               assign_submission_plugin_submission::FULL,
1699                                                               $this->get_course_module()->id,
1700                                                               $this->get_return_action(),
1701                                                               $this->get_return_params()));
1703             $this->add_to_log('view submission', get_string('viewsubmissionforuser', 'assign', $item->userid));
1704         } else {
1705             $plugin = $this->get_feedback_plugin_by_type($plugintype);
1706             if ($gradeid <= 0) {
1707                 throw new coding_exception('Grade id should not be 0');
1708             }
1709             $item = $this->get_grade($gradeid);
1710             // permissions
1711             if ($item->userid != $USER->id) {
1712                 require_capability('mod/assign:grade', $this->context);
1713             }
1714             $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
1715                                                               $this->get_context(),
1716                                                               $this->show_intro(),
1717                                                               $this->get_course_module()->id,
1718                                                               $plugin->get_name()));
1719             $o .= $this->get_renderer()->render(new assign_feedback_plugin_feedback($plugin,
1720                                                               $item,
1721                                                               assign_feedback_plugin_feedback::FULL,
1722                                                               $this->get_course_module()->id,
1723                                                               $this->get_return_action(),
1724                                                               $this->get_return_params()));
1725             $this->add_to_log('view feedback', get_string('viewfeedbackforuser', 'assign', $item->userid));
1726         }
1729         $o .= $this->view_return_links();
1731         $o .= $this->view_footer();
1732         return $o;
1733     }
1735     /**
1736      * render the content in editor that is often used by plugin
1737      *
1738      * @param string $filearea
1739      * @param int  $submissionid
1740      * @param string $plugintype
1741      * @param string $editor
1742      * @param string $component
1743      * @return string
1744      */
1745     public function render_editor_content($filearea, $submissionid, $plugintype, $editor, $component) {
1746         global $CFG;
1748         $result = '';
1750         $plugin = $this->get_submission_plugin_by_type($plugintype);
1752         $text = $plugin->get_editor_text($editor, $submissionid);
1753         $format = $plugin->get_editor_format($editor, $submissionid);
1755         $finaltext = file_rewrite_pluginfile_urls($text, 'pluginfile.php', $this->get_context()->id, $component, $filearea, $submissionid);
1756         $result .= format_text($finaltext, $format, array('overflowdiv' => true, 'context' => $this->get_context()));
1760         if ($CFG->enableportfolios) {
1761             require_once($CFG->libdir . '/portfoliolib.php');
1763             $button = new portfolio_add_button();
1764             $button->set_callback_options('assign_portfolio_caller', array('cmid' => $this->get_course_module()->id,
1765                                           'sid' => $submissionid, 'plugin' => $plugintype, 'editor' => $editor, 'area'=>$filearea),
1766                                           'mod_assign');
1767             $fs = get_file_storage();
1769             if ($files = $fs->get_area_files($this->context->id, $component,$filearea, $submissionid, "timemodified", false)) {
1770                 $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
1771             } else {
1772                 $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
1773             }
1774             $result .= $button->to_html();
1775         }
1776         return $result;
1777     }
1779     /**
1780      * Display a grading error
1781      *
1782      * @param string $message - The description of the result
1783      * @return string
1784      */
1785     private function view_quickgrading_result($message) {
1786         $o = '';
1787         $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
1788                                                       $this->get_context(),
1789                                                       $this->show_intro(),
1790                                                       $this->get_course_module()->id,
1791                                                       get_string('quickgradingresult', 'assign')));
1792         $o .= $this->get_renderer()->render(new assign_quickgrading_result($message, $this->get_course_module()->id));
1793         $o .= $this->view_footer();
1794         return $o;
1795     }
1797     /**
1798      * Display the page footer
1799      *
1800      * @return string
1801      */
1802     private function view_footer() {
1803         return $this->get_renderer()->render_footer();
1804     }
1806     /**
1807      * Does this user have grade permission for this assignment
1808      *
1809      * @return bool
1810      */
1811     private function can_grade() {
1812         // Permissions check
1813         if (!has_capability('mod/assign:grade', $this->context)) {
1814             return false;
1815         }
1817         return true;
1818     }
1820     /**
1821      * Download a zip file of all assignment submissions
1822      *
1823      * @return void
1824      */
1825     private function download_submissions() {
1826         global $CFG,$DB;
1828         // More efficient to load this here.
1829         require_once($CFG->libdir.'/filelib.php');
1831         // Load all users with submit.
1832         $students = get_enrolled_users($this->context, "mod/assign:submit");
1834         // Build a list of files to zip.
1835         $filesforzipping = array();
1836         $fs = get_file_storage();
1838         $groupmode = groups_get_activity_groupmode($this->get_course_module());
1839         // All users.
1840         $groupid = 0;
1841         $groupname = '';
1842         if ($groupmode) {
1843             $groupid = groups_get_activity_group($this->get_course_module(), true);
1844             $groupname = groups_get_group_name($groupid).'-';
1845         }
1847         // Construct the zip file name.
1848         $filename = clean_filename($this->get_course()->shortname.'-'.
1849                                    $this->get_instance()->name.'-'.
1850                                    $groupname.$this->get_course_module()->id.".zip");
1852         // Get all the files for each student.
1853         foreach ($students as $student) {
1854             $userid = $student->id;
1856             if ((groups_is_member($groupid, $userid) or !$groupmode or !$groupid)) {
1857                 // Get the plugins to add their own files to the zip.
1859                 $submissiongroup = false;
1860                 $groupname = '';
1861                 if ($this->get_instance()->teamsubmission) {
1862                     $submission = $this->get_group_submission($userid, 0, false);
1863                     $submissiongroup = $this->get_submission_group($userid);
1864                     if ($submissiongroup) {
1865                         $groupname = $submissiongroup->name . '-';
1866                     } else {
1867                         $groupname = get_string('defaultteam', 'assign') . '-';
1868                     }
1869                 } else {
1870                     $submission = $this->get_user_submission($userid, false);
1871                 }
1873                 if ($this->is_blind_marking()) {
1874                     $prefix = clean_filename(str_replace('_', ' ', $groupname . get_string('participant', 'assign')) .
1875                                              "_" . $this->get_uniqueid_for_user($userid) . "_");
1876                 } else {
1877                     $prefix = clean_filename(str_replace('_', ' ', $groupname . fullname($student)) .
1878                                              "_" . $this->get_uniqueid_for_user($userid) . "_");
1879                 }
1881                 if ($submission) {
1882                     foreach ($this->submissionplugins as $plugin) {
1883                         if ($plugin->is_enabled() && $plugin->is_visible()) {
1884                             $pluginfiles = $plugin->get_files($submission);
1885                             foreach ($pluginfiles as $zipfilename => $file) {
1886                                 $subtype = $plugin->get_subtype();
1887                                 $type = $plugin->get_type();
1888                                 $prefixedfilename = $prefix . $subtype . '_' . $type . '_' . $zipfilename;
1889                                 $filesforzipping[$prefixedfilename] = $file;
1890                             }
1891                         }
1892                     }
1893                 }
1894             }
1895         }
1896         if ($zipfile = $this->pack_files($filesforzipping)) {
1897             $this->add_to_log('download all submissions', get_string('downloadall', 'assign'));
1898             // Send file and delete after sending.
1899             send_temp_file($zipfile, $filename);
1900         }
1901     }
1903     /**
1904      * Util function to add a message to the log
1905      *
1906      * @param string $action The current action
1907      * @param string $info A detailed description of the change. But no more than 255 characters.
1908      * @param string $url The url to the assign module instance.
1909      * @return void
1910      */
1911     public function add_to_log($action = '', $info = '', $url='') {
1912         global $USER;
1914         $fullurl = 'view.php?id=' . $this->get_course_module()->id;
1915         if ($url != '') {
1916             $fullurl .= '&' . $url;
1917         }
1919         add_to_log($this->get_course()->id, 'assign', $action, $fullurl, $info, $this->get_course_module()->id, $USER->id);
1920     }
1922     /**
1923      * Lazy load the page renderer and expose the renderer to plugins
1924      *
1925      * @return assign_renderer
1926      */
1927     public function get_renderer() {
1928         global $PAGE;
1929         if ($this->output) {
1930             return $this->output;
1931         }
1932         $this->output = $PAGE->get_renderer('mod_assign');
1933         return $this->output;
1934     }
1936     /**
1937      * Load the submission object for a particular user, optionally creating it if required
1938      *
1939      * For team assignments there are 2 submissions - the student submission and the team submission
1940      * All files are associated with the team submission but the status of the students contribution is
1941      * recorded separately.
1942      *
1943      * @param int $userid The id of the user whose submission we want or 0 in which case USER->id is used
1944      * @param bool $create optional Defaults to false. If set to true a new submission object will be created in the database
1945      * @return stdClass The submission
1946      */
1947     public function get_user_submission($userid, $create) {
1948         global $DB, $USER;
1950         if (!$userid) {
1951             $userid = $USER->id;
1952         }
1953         // If the userid is not null then use userid.
1954         $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid, 'groupid'=>0);
1955         $submission = $DB->get_record('assign_submission', $params);
1957         if ($submission) {
1958             return $submission;
1959         }
1960         if ($create) {
1961             $submission = new stdClass();
1962             $submission->assignment   = $this->get_instance()->id;
1963             $submission->userid       = $userid;
1964             $submission->timecreated = time();
1965             $submission->timemodified = $submission->timecreated;
1967             if ($this->get_instance()->submissiondrafts) {
1968                 $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
1969             } else {
1970                 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1971             }
1972             $sid = $DB->insert_record('assign_submission', $submission);
1973             $submission->id = $sid;
1974             return $submission;
1975         }
1976         return false;
1977     }
1979     /**
1980      * Load the submission object from it's id
1981      *
1982      * @param int $submissionid The id of the submission we want
1983      * @return stdClass The submission
1984      */
1985     private function get_submission($submissionid) {
1986         global $DB;
1988         return $DB->get_record('assign_submission', array('assignment'=>$this->get_instance()->id, 'id'=>$submissionid), '*', MUST_EXIST);
1989     }
1991     /**
1992      * This will retrieve a grade object from the db, optionally creating it if required
1993      *
1994      * @param int $userid The user we are grading
1995      * @param bool $create If true the grade will be created if it does not exist
1996      * @return stdClass The grade record
1997      */
1998     public function get_user_grade($userid, $create) {
1999         global $DB, $USER;
2001         if (!$userid) {
2002             $userid = $USER->id;
2003         }
2005         // if the userid is not null then use userid
2006         $grade = $DB->get_record('assign_grades', array('assignment'=>$this->get_instance()->id, 'userid'=>$userid));
2008         if ($grade) {
2009             return $grade;
2010         }
2011         if ($create) {
2012             $grade = new stdClass();
2013             $grade->assignment   = $this->get_instance()->id;
2014             $grade->userid       = $userid;
2015             $grade->timecreated = time();
2016             $grade->timemodified = $grade->timecreated;
2017             $grade->locked = 0;
2018             $grade->grade = -1;
2019             $grade->grader = $USER->id;
2020             $grade->extensionduedate = 0;
2021             $gid = $DB->insert_record('assign_grades', $grade);
2022             $grade->id = $gid;
2023             return $grade;
2024         }
2025         return false;
2026     }
2028     /**
2029      * This will retrieve a grade object from the db
2030      *
2031      * @param int $gradeid The id of the grade
2032      * @return stdClass The grade record
2033      */
2034     private function get_grade($gradeid) {
2035         global $DB;
2037         return $DB->get_record('assign_grades', array('assignment'=>$this->get_instance()->id, 'id'=>$gradeid), '*', MUST_EXIST);
2038     }
2040     /**
2041      * Print the grading page for a single user submission
2042      *
2043      * @param moodleform $mform
2044      * @param int $offset
2045      * @return string
2046      */
2047     private function view_single_grade_page($mform, $offset=0) {
2048         global $DB, $CFG;
2050         $o = '';
2052         // Include grade form
2053         require_once($CFG->dirroot . '/mod/assign/gradeform.php');
2055         // Need submit permission to submit an assignment
2056         require_capability('mod/assign:grade', $this->context);
2058         $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
2059                                                       $this->get_context(), false, $this->get_course_module()->id,get_string('grading', 'assign')));
2061         $rownum = required_param('rownum', PARAM_INT) + $offset;
2062         $useridlist = optional_param('useridlist', '', PARAM_TEXT);
2063         if ($useridlist) {
2064             $useridlist = explode(',', $useridlist);
2065         } else {
2066             $useridlist = $this->get_grading_userid_list();
2067         }
2068         $last = false;
2069         $userid = $useridlist[$rownum];
2070         if ($rownum == count($useridlist) - 1) {
2071             $last = true;
2072         }
2073         if (!$userid) {
2074             throw new coding_exception('Row is out of bounds for the current grading table: ' . $rownum);
2075         }
2076         $user = $DB->get_record('user', array('id' => $userid));
2077         if ($user) {
2078             $o .= $this->get_renderer()->render(new assign_user_summary($user,
2079                                                                 $this->get_course()->id,
2080                                                                 has_capability('moodle/site:viewfullnames',
2081                                                                 $this->get_course_context()),
2082                                                                 $this->is_blind_marking(),
2083                                                                 $this->get_uniqueid_for_user($user->id)));
2084         }
2085         $submission = $this->get_user_submission($userid, false);
2086         $submissiongroup = null;
2087         $submissiongroupmemberswhohavenotsubmitted = array();
2088         $teamsubmission = null;
2089         $notsubmitted = array();
2090         if ($this->get_instance()->teamsubmission) {
2091             $teamsubmission = $this->get_group_submission($userid, 0, false);
2092             $submissiongroup = $this->get_submission_group($userid);
2093             $groupid = 0;
2094             if ($submissiongroup) {
2095                 $groupid = $submissiongroup->id;
2096             }
2097             $notsubmitted = $this->get_submission_group_members_who_have_not_submitted($groupid, false);
2099         }
2101         // get the current grade
2102         $grade = $this->get_user_grade($userid, false);
2103         if ($this->can_view_submission($userid)) {
2104             $gradelocked = ($grade && $grade->locked) || $this->grading_disabled($userid);
2105             $extensionduedate = null;
2106             if ($grade) {
2107                 $extensionduedate = $grade->extensionduedate;
2108             }
2109             $showedit = $this->submissions_open($userid) && ($this->is_any_submission_plugin_enabled());
2111             if ($teamsubmission) {
2112                 $showsubmit = $showedit && $teamsubmission && ($teamsubmission->status == ASSIGN_SUBMISSION_STATUS_DRAFT);
2113             } else {
2114                 $showsubmit = $showedit && $submission && ($submission->status == ASSIGN_SUBMISSION_STATUS_DRAFT);
2115             }
2116             $viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_course_context());
2118             $o .= $this->get_renderer()->render(new assign_submission_status($this->get_instance()->allowsubmissionsfromdate,
2119                                                               $this->get_instance()->alwaysshowdescription,
2120                                                               $submission,
2121                                                               $this->get_instance()->teamsubmission,
2122                                                               $teamsubmission,
2123                                                               $submissiongroup,
2124                                                               $notsubmitted,
2125                                                               $this->is_any_submission_plugin_enabled(),
2126                                                               $gradelocked,
2127                                                               $this->is_graded($userid),
2128                                                               $this->get_instance()->duedate,
2129                                                               $this->get_instance()->cutoffdate,
2130                                                               $this->get_submission_plugins(),
2131                                                               $this->get_return_action(),
2132                                                               $this->get_return_params(),
2133                                                               $this->get_course_module()->id,
2134                                                               $this->get_course()->id,
2135                                                               assign_submission_status::GRADER_VIEW,
2136                                                               $showedit,
2137                                                               $showsubmit,
2138                                                               $viewfullnames,
2139                                                               $extensionduedate,
2140                                                               $this->get_context(),
2141                                                               $this->is_blind_marking(),
2142                                                               ''));
2143         }
2144         if ($grade) {
2145             $data = new stdClass();
2146             if ($grade->grade !== NULL && $grade->grade >= 0) {
2147                 $data->grade = format_float($grade->grade,2);
2148             }
2149         } else {
2150             $data = new stdClass();
2151             $data->grade = '';
2152         }
2154         // now show the grading form
2155         if (!$mform) {
2156             $pagination = array( 'rownum'=>$rownum, 'useridlist'=>$useridlist, 'last'=>$last);
2157             $formparams = array($this, $data, $pagination);
2158             $mform = new mod_assign_grade_form(null,
2159                                                $formparams,
2160                                                'post',
2161                                                '',
2162                                                array('class'=>'gradeform'));
2163         }
2164         $o .= $this->get_renderer()->render(new assign_form('gradingform',$mform));
2166         $msg = get_string('viewgradingformforstudent', 'assign', array('id'=>$user->id, 'fullname'=>fullname($user)));
2167         $this->add_to_log('view grading form', $msg);
2169         $o .= $this->view_footer();
2170         return $o;
2171     }
2173     /**
2174      * Show a confirmation page to make sure they want to release student identities
2175      *
2176      * @return string
2177      */
2178     private function view_reveal_identities_confirm() {
2179         global $CFG, $USER;
2181         require_capability('mod/assign:revealidentities', $this->get_context());
2183         $o = '';
2184         $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
2185                                                       $this->get_context(), false, $this->get_course_module()->id));
2187         $confirmurl = new moodle_url('/mod/assign/view.php', array('id'=>$this->get_course_module()->id,
2188                                                                     'action'=>'revealidentitiesconfirm',
2189                                                                     'sesskey'=>sesskey()));
2191         $cancelurl = new moodle_url('/mod/assign/view.php', array('id'=>$this->get_course_module()->id,
2192                                                                     'action'=>'grading'));
2194         $o .= $this->get_renderer()->confirm(get_string('revealidentitiesconfirm', 'assign'), $confirmurl, $cancelurl);
2195         $o .= $this->view_footer();
2196         $this->add_to_log('view', get_string('viewrevealidentitiesconfirm', 'assign'));
2197         return $o;
2198     }
2203     /**
2204      * View a link to go back to the previous page. Uses url parameters returnaction and returnparams.
2205      *
2206      * @return string
2207      */
2208     private function view_return_links() {
2210         $returnaction = optional_param('returnaction','', PARAM_ALPHA);
2211         $returnparams = optional_param('returnparams','', PARAM_TEXT);
2213         $params = array();
2214         parse_str($returnparams, $params);
2215         $params = array_merge( array('id' => $this->get_course_module()->id, 'action' => $returnaction), $params);
2217         return $this->get_renderer()->single_button(new moodle_url('/mod/assign/view.php', $params), get_string('back'), 'get');
2219     }
2221     /**
2222      * View the grading table of all submissions for this assignment
2223      *
2224      * @return string
2225      */
2226     private function view_grading_table() {
2227         global $USER, $CFG;
2228         // Include grading options form
2229         require_once($CFG->dirroot . '/mod/assign/gradingoptionsform.php');
2230         require_once($CFG->dirroot . '/mod/assign/quickgradingform.php');
2231         require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php');
2232         $o = '';
2234         $links = array();
2235         if (has_capability('gradereport/grader:view', $this->get_course_context()) &&
2236                 has_capability('moodle/grade:viewall', $this->get_course_context())) {
2237             $gradebookurl = '/grade/report/grader/index.php?id=' . $this->get_course()->id;
2238             $links[$gradebookurl] = get_string('viewgradebook', 'assign');
2239         }
2240         if ($this->is_any_submission_plugin_enabled()) {
2241             $downloadurl = '/mod/assign/view.php?id=' . $this->get_course_module()->id . '&action=downloadall';
2242             $links[$downloadurl] = get_string('downloadall', 'assign');
2243         }
2244         if ($this->is_blind_marking() && has_capability('mod/assign:revealidentities', $this->get_context())) {
2245             $revealidentitiesurl = '/mod/assign/view.php?id=' . $this->get_course_module()->id . '&action=revealidentities';
2246             $links[$revealidentitiesurl] = get_string('revealidentities', 'assign');
2247         }
2248         foreach ($this->get_feedback_plugins() as $plugin) {
2249             if ($plugin->is_enabled() && $plugin->is_visible()) {
2250                 foreach ($plugin->get_grading_actions() as $action => $description) {
2251                     $url = '/mod/assign/view.php' .
2252                            '?id=' .  $this->get_course_module()->id .
2253                            '&plugin=' . $plugin->get_type() .
2254                            '&pluginsubtype=assignfeedback' .
2255                            '&action=viewpluginpage&pluginaction=' . $action;
2256                     $links[$url] = $description;
2257                 }
2258             }
2259         }
2261         $gradingactions = new url_select($links);
2263         $gradingmanager = get_grading_manager($this->get_context(), 'mod_assign', 'submissions');
2265         $perpage = get_user_preferences('assign_perpage', 10);
2266         $filter = get_user_preferences('assign_filter', '');
2267         $controller = $gradingmanager->get_active_controller();
2268         $showquickgrading = empty($controller);
2269         if (optional_param('action', '', PARAM_ALPHA) == 'saveoptions') {
2270             $quickgrading = optional_param('quickgrading', false, PARAM_BOOL);
2271             set_user_preference('assign_quickgrading', $quickgrading);
2272         }
2273         $quickgrading = get_user_preferences('assign_quickgrading', false);
2275         // print options  for changing the filter and changing the number of results per page
2276         $gradingoptionsform = new mod_assign_grading_options_form(null,
2277                                                                   array('cm'=>$this->get_course_module()->id,
2278                                                                         'contextid'=>$this->context->id,
2279                                                                         'userid'=>$USER->id,
2280                                                                         'submissionsenabled'=>$this->is_any_submission_plugin_enabled(),
2281                                                                         'showquickgrading'=>$showquickgrading,
2282                                                                         'quickgrading'=>$quickgrading),
2283                                                                   'post', '',
2284                                                                   array('class'=>'gradingoptionsform'));
2286         $gradingbatchoperationsform = new mod_assign_grading_batch_operations_form(null,
2287                                                                   array('cm'=>$this->get_course_module()->id,
2288                                                                         'submissiondrafts'=>$this->get_instance()->submissiondrafts,
2289                                                                         'duedate'=>$this->get_instance()->duedate,
2290                                                                         'feedbackplugins'=>$this->get_feedback_plugins()),
2291                                                                   'post', '',
2292                                                                   array('class'=>'gradingbatchoperationsform'));
2294         $gradingoptionsdata = new stdClass();
2295         $gradingoptionsdata->perpage = $perpage;
2296         $gradingoptionsdata->filter = $filter;
2297         $gradingoptionsform->set_data($gradingoptionsdata);
2299         $actionformtext = $this->get_renderer()->render($gradingactions);
2300         $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
2301                                                       $this->get_context(), false, $this->get_course_module()->id, get_string('grading', 'assign'), $actionformtext));
2302         $o .= groups_print_activity_menu($this->get_course_module(), $CFG->wwwroot . '/mod/assign/view.php?id=' . $this->get_course_module()->id.'&action=grading', true);
2304         // plagiarism update status apearring in the grading book
2305         if (!empty($CFG->enableplagiarism)) {
2306             /** Include plagiarismlib.php */
2307             require_once($CFG->libdir . '/plagiarismlib.php');
2308             $o .= plagiarism_update_status($this->get_course(), $this->get_course_module());
2309         }
2311         // load and print the table of submissions
2312         if ($showquickgrading && $quickgrading) {
2313             $table = $this->get_renderer()->render(new assign_grading_table($this, $perpage, $filter, 0, true));
2314             $quickgradingform = new mod_assign_quick_grading_form(null,
2315                                                                   array('cm'=>$this->get_course_module()->id,
2316                                                                         'gradingtable'=>$table));
2317             $o .= $this->get_renderer()->render(new assign_form('quickgradingform', $quickgradingform));
2318         } else {
2319             $o .= $this->get_renderer()->render(new assign_grading_table($this, $perpage, $filter, 0, false));
2320         }
2322         $currentgroup = groups_get_activity_group($this->get_course_module(), true);
2323         $users = array_keys($this->list_participants($currentgroup, true));
2324         if (count($users) != 0) {
2325             // if no enrolled user in a course then don't display the batch operations feature
2326             $o .= $this->get_renderer()->render(new assign_form('gradingbatchoperationsform', $gradingbatchoperationsform));
2327         }
2328         $o .= $this->get_renderer()->render(new assign_form('gradingoptionsform', $gradingoptionsform, 'M.mod_assign.init_grading_options'));
2329         return $o;
2330     }
2332     /**
2333      * View entire grading page.
2334      *
2335      * @return string
2336      */
2337     private function view_grading_page() {
2338         global $CFG;
2340         $o = '';
2341         // Need submit permission to submit an assignment
2342         require_capability('mod/assign:grade', $this->context);
2343         require_once($CFG->dirroot . '/mod/assign/gradeform.php');
2345         // only load this if it is
2347         $o .= $this->view_grading_table();
2349         $o .= $this->view_footer();
2350         $this->add_to_log('view submission grading table', get_string('viewsubmissiongradingtable', 'assign'));
2351         return $o;
2352     }
2354     /**
2355      * Capture the output of the plagiarism plugins disclosures and return it as a string
2356      *
2357      * @return void
2358      */
2359     private function plagiarism_print_disclosure() {
2360         global $CFG;
2361         $o = '';
2363         if (!empty($CFG->enableplagiarism)) {
2364             /** Include plagiarismlib.php */
2365             require_once($CFG->libdir . '/plagiarismlib.php');
2367             $o .= plagiarism_print_disclosure($this->get_course_module()->id);
2368         }
2370         return $o;
2371     }
2373     /**
2374      * message for students when assignment submissions have been closed
2375      *
2376      * @return string
2377      */
2378     private function view_student_error_message() {
2379         global $CFG;
2381         $o = '';
2382         // Need submit permission to submit an assignment
2383         require_capability('mod/assign:submit', $this->context);
2385         $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
2386                                                       $this->get_context(),
2387                                                       $this->show_intro(),
2388                                                       $this->get_course_module()->id,
2389                                                       get_string('editsubmission', 'assign')));
2391         $o .= $this->get_renderer()->notification(get_string('submissionsclosed', 'assign'));
2393         $o .= $this->view_footer();
2395         return $o;
2397     }
2399     /**
2400      * View edit submissions page.
2401      *
2402      * @param moodleform $mform
2403      * @return void
2404      */
2405     private function view_edit_submission_page($mform) {
2406         global $CFG;
2408         $o = '';
2409         // Include submission form
2410         require_once($CFG->dirroot . '/mod/assign/submission_form.php');
2411         // Need submit permission to submit an assignment
2412         require_capability('mod/assign:submit', $this->context);
2414         if (!$this->submissions_open()) {
2415             return $this->view_student_error_message();
2416         }
2417         $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
2418                                                       $this->get_context(),
2419                                                       $this->show_intro(),
2420                                                       $this->get_course_module()->id,
2421                                                       get_string('editsubmission', 'assign')));
2422         $o .= $this->plagiarism_print_disclosure();
2423         $data = new stdClass();
2425         if (!$mform) {
2426             $mform = new mod_assign_submission_form(null, array($this, $data));
2427         }
2429         $o .= $this->get_renderer()->render(new assign_form('editsubmissionform',$mform));
2431         $o .= $this->view_footer();
2432         $this->add_to_log('view submit assignment form', get_string('viewownsubmissionform', 'assign'));
2434         return $o;
2435     }
2437     /**
2438      * See if this assignment has a grade yet
2439      *
2440      * @param int $userid
2441      * @return bool
2442      */
2443     private function is_graded($userid) {
2444         $grade = $this->get_user_grade($userid, false);
2445         if ($grade) {
2446             return ($grade->grade !== NULL && $grade->grade >= 0);
2447         }
2448         return false;
2449     }
2452     /**
2453      * Perform an access check to see if the current $USER can view this users submission
2454      *
2455      * @param int $userid
2456      * @return bool
2457      */
2458     public function can_view_submission($userid) {
2459         global $USER;
2461         if (!is_enrolled($this->get_course_context(), $userid)) {
2462             return false;
2463         }
2464         if ($userid == $USER->id && !has_capability('mod/assign:submit', $this->context)) {
2465             return false;
2466         }
2467         if ($userid != $USER->id && !has_capability('mod/assign:grade', $this->context)) {
2468             return false;
2469         }
2470         return true;
2471     }
2473     /**
2474      * Allows the plugin to show a batch grading operation page.
2475      *
2476      * @return none
2477      */
2478     private function view_plugin_grading_batch_operation($mform) {
2479         require_capability('mod/assign:grade', $this->context);
2480         $prefix = 'plugingradingbatchoperation_';
2482         if ($data = $mform->get_data()) {
2483             $tail = substr($data->operation, strlen($prefix));
2484             list($plugintype, $action) = explode('_', $tail, 2);
2486             $plugin = $this->get_feedback_plugin_by_type($plugintype);
2487             if ($plugin) {
2488                 $users = $data->selectedusers;
2489                 $userlist = explode(',', $users);
2490                 echo $plugin->grading_batch_operation($action, $userlist);
2491                 return;
2492             }
2493         }
2494         print_error('invalidformdata', '');
2495     }
2497     /**
2498      * Ask the user to confirm they want to perform this batch operation
2499      * @param moodleform $mform Set to a grading batch operations form
2500      * @return string - the page to view after processing these actions
2501      */
2502     private function process_grading_batch_operation(& $mform) {
2503         global $CFG;
2504         require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php');
2505         require_sesskey();
2507         $mform = new mod_assign_grading_batch_operations_form(null,
2508                                                               array('cm'=>$this->get_course_module()->id,
2509                                                                     'submissiondrafts'=>$this->get_instance()->submissiondrafts,
2510                                                                     'duedate'=>$this->get_instance()->duedate,
2511                                                                     'feedbackplugins'=>$this->get_feedback_plugins()),
2512                                                               'post',
2513                                                               '',
2514                                                               array('class'=>'gradingbatchoperationsform'));
2516         if ($data = $mform->get_data()) {
2517             // get the list of users
2518             $users = $data->selectedusers;
2519             $userlist = explode(',', $users);
2521             $prefix = 'plugingradingbatchoperation_';
2523             if ($data->operation == 'grantextension') {
2524                 // Reset the form so the grant extension page will create the extension form.
2525                 $mform = null;
2526                 return 'grantextension';
2527             } else if (strpos($data->operation, $prefix) === 0) {
2528                 $tail = substr($data->operation, strlen($prefix));
2529                 list($plugintype, $action) = explode('_', $tail, 2);
2531                 $plugin = $this->get_feedback_plugin_by_type($plugintype);
2532                 if ($plugin) {
2533                     return 'plugingradingbatchoperation';
2534                 }
2535             }
2537             foreach ($userlist as $userid) {
2538                 if ($data->operation == 'lock') {
2539                     $this->process_lock($userid);
2540                 } else if ($data->operation == 'unlock') {
2541                     $this->process_unlock($userid);
2542                 } else if ($data->operation == 'reverttodraft') {
2543                     $this->process_revert_to_draft($userid);
2544                 }
2545             }
2546         }
2548         return 'grading';
2549     }
2551     /**
2552      * Ask the user to confirm they want to submit their work for grading
2553      * @param $mform moodleform - null unless form validation has failed
2554      * @return string
2555      */
2556     private function check_submit_for_grading($mform) {
2557         global $USER, $CFG;
2559         require_once($CFG->dirroot . '/mod/assign/submissionconfirmform.php');
2561         // Check that all of the submission plugins are ready for this submission
2562         $notifications = array();
2563         $submission = $this->get_user_submission($USER->id, false);
2564         $plugins = $this->get_submission_plugins();
2565         foreach ($plugins as $plugin) {
2566             if ($plugin->is_enabled() && $plugin->is_visible()) {
2567                 $check = $plugin->precheck_submission($submission);
2568                 if ($check !== true) {
2569                     $notifications[] = $check;
2570                 }
2571             }
2572         }
2574         $data = new stdClass();
2575         $adminconfig = $this->get_admin_config();
2576         $requiresubmissionstatement = (!empty($adminconfig->requiresubmissionstatement) ||
2577                                        $this->get_instance()->requiresubmissionstatement) &&
2578                                        !empty($adminconfig->submissionstatement);
2580         $submissionstatement = '';
2581         if (!empty($adminconfig->submissionstatement)) {
2582             $submissionstatement = $adminconfig->submissionstatement;
2583         }
2585         if ($mform == null) {
2586             $mform = new mod_assign_confirm_submission_form(null, array($requiresubmissionstatement,
2587                                                                         $submissionstatement,
2588                                                                         $this->get_course_module()->id,
2589                                                                         $data));
2590         }
2591         $o = '';
2592         $o .= $this->get_renderer()->header();
2593         $o .= $this->get_renderer()->render(new assign_submit_for_grading_page($notifications, $this->get_course_module()->id, $mform));
2594         $o .= $this->view_footer();
2596         $this->add_to_log('view confirm submit assignment form', get_string('viewownsubmissionform', 'assign'));
2598         return $o;
2599     }
2601     /**
2602      * Print 2 tables of information with no action links -
2603      * the submission summary and the grading summary
2604      *
2605      * @param stdClass $user the user to print the report for
2606      * @param bool $showlinks - Return plain text or links to the profile
2607      * @return string - the html summary
2608      */
2609     public function view_student_summary($user, $showlinks) {
2610         global $CFG, $DB, $PAGE;
2612         $grade = $this->get_user_grade($user->id, false);
2613         $submission = $this->get_user_submission($user->id, false);
2614         $o = '';
2616         $teamsubmission = null;
2617         $submissiongroup = null;
2618         $notsubmitted = array();
2619         if ($this->get_instance()->teamsubmission) {
2620             $teamsubmission = $this->get_group_submission($user->id, 0, false);
2621             $submissiongroup = $this->get_submission_group($user->id);
2622             $groupid = 0;
2623             if ($submissiongroup) {
2624                 $groupid = $submissiongroup->id;
2625             }
2626             $notsubmitted = $this->get_submission_group_members_who_have_not_submitted($groupid, false);
2627         }
2629         if ($this->can_view_submission($user->id)) {
2630             $showedit = has_capability('mod/assign:submit', $this->context) &&
2631                          $this->submissions_open($user->id) && ($this->is_any_submission_plugin_enabled()) && $showlinks;
2632             $gradelocked = ($grade && $grade->locked) || $this->grading_disabled($user->id);
2633             // Grading criteria preview.
2634             $gradingmanager = get_grading_manager($this->context, 'mod_assign', 'submissions');
2635             $gradingcontrollerpreview = '';
2636             if ($gradingmethod = $gradingmanager->get_active_method()) {
2637                 $controller = $gradingmanager->get_controller($gradingmethod);
2638                 if ($controller->is_form_defined()) {
2639                     $gradingcontrollerpreview = $controller->render_preview($PAGE);
2640                 }
2641             }
2643             $showsubmit = ($submission || $teamsubmission) && $showlinks;
2644             if ($teamsubmission && ($teamsubmission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED)) {
2645                 $showsubmit = false;
2646             }
2647             if ($submission && ($submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED)) {
2648                 $showsubmit = false;
2649             }
2650             $extensionduedate = null;
2651             if ($grade) {
2652                 $extensionduedate = $grade->extensionduedate;
2653             }
2654             $viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_course_context());
2655             $o .= $this->get_renderer()->render(new assign_submission_status($this->get_instance()->allowsubmissionsfromdate,
2656                                                               $this->get_instance()->alwaysshowdescription,
2657                                                               $submission,
2658                                                               $this->get_instance()->teamsubmission,
2659                                                               $teamsubmission,
2660                                                               $submissiongroup,
2661                                                               $notsubmitted,
2662                                                               $this->is_any_submission_plugin_enabled(),
2663                                                               $gradelocked,
2664                                                               $this->is_graded($user->id),
2665                                                               $this->get_instance()->duedate,
2666                                                               $this->get_instance()->cutoffdate,
2667                                                               $this->get_submission_plugins(),
2668                                                               $this->get_return_action(),
2669                                                               $this->get_return_params(),
2670                                                               $this->get_course_module()->id,
2671                                                               $this->get_course()->id,
2672                                                               assign_submission_status::STUDENT_VIEW,
2673                                                               $showedit,
2674                                                               $showsubmit,
2675                                                               $viewfullnames,
2676                                                               $extensionduedate,
2677                                                               $this->get_context(),
2678                                                               $this->is_blind_marking(),
2679                                                               $gradingcontrollerpreview));
2681             require_once($CFG->libdir.'/gradelib.php');
2682             require_once($CFG->dirroot.'/grade/grading/lib.php');
2684             $gradinginfo = grade_get_grades($this->get_course()->id,
2685                                         'mod',
2686                                         'assign',
2687                                         $this->get_instance()->id,
2688                                         $user->id);
2690             $gradingitem = $gradinginfo->items[0];
2691             $gradebookgrade = $gradingitem->grades[$user->id];
2693             // check to see if all feedback plugins are empty
2694             $emptyplugins = true;
2695             if ($grade) {
2696                 foreach ($this->get_feedback_plugins() as $plugin) {
2697                     if ($plugin->is_visible() && $plugin->is_enabled()) {
2698                         if (!$plugin->is_empty($grade)) {
2699                             $emptyplugins = false;
2700                         }
2701                     }
2702                 }
2703             }
2706             if (!($gradebookgrade->hidden) && ($gradebookgrade->grade !== null || !$emptyplugins)) {
2708                 $gradefordisplay = '';
2709                 $gradingmanager = get_grading_manager($this->get_context(), 'mod_assign', 'submissions');
2711                 if ($controller = $gradingmanager->get_active_controller()) {
2712                     $controller->set_grade_range(make_grades_menu($this->get_instance()->grade));
2713                     $gradefordisplay = $controller->render_grade($PAGE,
2714                                                                  $grade->id,
2715                                                                  $gradingitem,
2716                                                                  $gradebookgrade->str_long_grade,
2717                                                                  has_capability('mod/assign:grade', $this->get_context()));
2718                 } else {
2719                     $gradefordisplay = $this->display_grade($gradebookgrade->grade, false);
2720                 }
2722                 $gradeddate = $gradebookgrade->dategraded;
2723                 $grader = $DB->get_record('user', array('id'=>$gradebookgrade->usermodified));
2725                 $feedbackstatus = new assign_feedback_status($gradefordisplay,
2726                                                       $gradeddate,
2727                                                       $grader,
2728                                                       $this->get_feedback_plugins(),
2729                                                       $grade,
2730                                                       $this->get_course_module()->id,
2731                                                       $this->get_return_action(),
2732                                                       $this->get_return_params());
2734                 $o .= $this->get_renderer()->render($feedbackstatus);
2735             }
2737         }
2738         return $o;
2739     }
2741     /**
2742      * View submissions page (contains details of current submission).
2743      *
2744      * @return string
2745      */
2746     private function view_submission_page() {
2747         global $CFG, $DB, $USER, $PAGE;
2749         $o = '';
2750         $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
2751                                                       $this->get_context(),
2752                                                       $this->show_intro(),
2753                                                       $this->get_course_module()->id));
2755         if ($this->can_grade()) {
2756             if ($this->get_instance()->teamsubmission) {
2757                 $summary = new assign_grading_summary($this->count_teams(),
2758                                                       $this->get_instance()->submissiondrafts,
2759                                                       $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_DRAFT),
2760                                                       $this->is_any_submission_plugin_enabled(),
2761                                                       $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED),
2762                                                       $this->get_instance()->cutoffdate,
2763                                                       $this->get_instance()->duedate,
2764                                                       $this->get_course_module()->id,
2765                                                       $this->count_submissions_need_grading(),
2766                                                       $this->get_instance()->teamsubmission);
2767                 $o .= $this->get_renderer()->render($summary);
2768             } else {
2769                 $summary = new assign_grading_summary($this->count_participants(0),
2770                                                       $this->get_instance()->submissiondrafts,
2771                                                       $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_DRAFT),
2772                                                       $this->is_any_submission_plugin_enabled(),
2773                                                       $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED),
2774                                                       $this->get_instance()->cutoffdate,
2775                                                       $this->get_instance()->duedate,
2776                                                       $this->get_course_module()->id,
2777                                                       $this->count_submissions_need_grading(),
2778                                                       $this->get_instance()->teamsubmission);
2779                 $o .= $this->get_renderer()->render($summary);
2780             }
2781         }
2782         $grade = $this->get_user_grade($USER->id, false);
2783         $submission = $this->get_user_submission($USER->id, false);
2785         if ($this->can_view_submission($USER->id)) {
2786             $o .= $this->view_student_summary($USER, true);
2787         }
2790         $o .= $this->view_footer();
2791         $this->add_to_log('view', get_string('viewownsubmissionstatus', 'assign'));
2792         return $o;
2793     }
2795     /**
2796      * convert the final raw grade(s) in the  grading table for the gradebook
2797      *
2798      * @param stdClass $grade
2799      * @return array
2800      */
2801     private function convert_grade_for_gradebook(stdClass $grade) {
2802         $gradebookgrade = array();
2803         // trying to match those array keys in grade update function in gradelib.php
2804         // with keys in th database table assign_grades
2805         // starting around line 262
2806         if ($grade->grade >= 0) {
2807             $gradebookgrade['rawgrade'] = $grade->grade;
2808         }
2809         $gradebookgrade['userid'] = $grade->userid;
2810         $gradebookgrade['usermodified'] = $grade->grader;
2811         $gradebookgrade['datesubmitted'] = NULL;
2812         $gradebookgrade['dategraded'] = $grade->timemodified;
2813         if (isset($grade->feedbackformat)) {
2814             $gradebookgrade['feedbackformat'] = $grade->feedbackformat;
2815         }
2816         if (isset($grade->feedbacktext)) {
2817             $gradebookgrade['feedback'] = $grade->feedbacktext;
2818         }
2820         return $gradebookgrade;
2821     }
2823     /**
2824      * convert submission details for the gradebook
2825      *
2826      * @param stdClass $submission
2827      * @return array
2828      */
2829     private function convert_submission_for_gradebook(stdClass $submission) {
2830         $gradebookgrade = array();
2833         $gradebookgrade['userid'] = $submission->userid;
2834         $gradebookgrade['usermodified'] = $submission->userid;
2835         $gradebookgrade['datesubmitted'] = $submission->timemodified;
2837         return $gradebookgrade;
2838     }
2840     /**
2841      * update grades in the gradebook
2842      *
2843      * @param mixed $submission stdClass|null
2844      * @param mixed $grade stdClass|null
2845      * @return bool
2846      */
2847     private function gradebook_item_update($submission=NULL, $grade=NULL) {
2849         // Do not push grade to gradebook if blind marking is active as the gradebook would reveal the students.
2850         if ($this->is_blind_marking()) {
2851             return false;
2852         }
2853         if ($submission != NULL) {
2854             if ($submission->userid == 0) {
2855                 // This is a group submission update.
2856                 $team = groups_get_members($submission->groupid, 'u.id');
2858                 foreach ($team as $member) {
2859                     $submission->groupid = 0;
2860                     $submission->userid = $member->id;
2861                     $this->gradebook_item_update($submission, null);
2862                 }
2863                 return;
2864             }
2866             $gradebookgrade = $this->convert_submission_for_gradebook($submission);
2868         } else {
2869             $gradebookgrade = $this->convert_grade_for_gradebook($grade);
2870         }
2871         // Grading is disabled, return.
2872         if ($this->grading_disabled($gradebookgrade['userid'])) {
2873             return false;
2874         }
2875         $assign = clone $this->get_instance();
2876         $assign->cmidnumber = $this->get_course_module()->id;
2878         return assign_grade_item_update($assign, $gradebookgrade);
2879     }
2881     /**
2882      * update team submission
2883      *
2884      * @param stdClass $submission
2885      * @param int $userid
2886      * @param bool $updatetime
2887      * @return bool
2888      */
2889     private function update_team_submission(stdClass $submission, $userid, $updatetime) {
2890         global $DB;
2892         if ($updatetime) {
2893             $submission->timemodified = time();
2894         }
2896         // First update the submission for the current user.
2897         $mysubmission = $this->get_user_submission($userid, true);
2898         $mysubmission->status = $submission->status;
2900         $this->update_submission($mysubmission, 0, $updatetime, false);
2902         // Now check the team settings to see if this assignment qualifies as submitted or draft.
2903         $team = $this->get_submission_group_members($submission->groupid, true);
2905         $allsubmitted = true;
2906         $anysubmitted = false;
2907         foreach ($team as $member) {
2908             $membersubmission = $this->get_user_submission($member->id, false);
2910             if (!$membersubmission || $membersubmission->status != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
2911                 $allsubmitted = false;
2912                 if ($anysubmitted) {
2913                     break;
2914                 }
2915             } else {
2916                 $anysubmitted = true;
2917             }
2918         }
2919         if ($this->get_instance()->requireallteammemberssubmit) {
2920             if ($allsubmitted) {
2921                 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
2922             } else {
2923                 $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
2924             }
2925             $result= $DB->update_record('assign_submission', $submission);
2926         } else {
2927             if ($anysubmitted) {
2928                 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
2929             } else {
2930                 $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
2931             }
2932             $result= $DB->update_record('assign_submission', $submission);
2933         }
2935         $this->gradebook_item_update($submission);
2936         return $result;
2937     }
2940     /**
2941      * update grades in the gradebook based on submission time
2942      *
2943      * @param stdClass $submission
2944      * @param int $userid
2945      * @param bool $updatetime
2946      * @param bool $teamsubmission
2947      * @return bool
2948      */
2949     private function update_submission(stdClass $submission, $userid, $updatetime, $teamsubmission) {
2950         global $DB;
2952         if ($teamsubmission) {
2953             return $this->update_team_submission($submission, $userid, $updatetime);
2954         }
2956         if ($updatetime) {
2957             $submission->timemodified = time();
2958         }
2959         $result= $DB->update_record('assign_submission', $submission);
2960         if ($result) {
2961             $this->gradebook_item_update($submission);
2962         }
2963         return $result;
2964     }
2966     /**
2967      * Is this assignment open for submissions?
2968      *
2969      * Check the due date,
2970      * prevent late submissions,
2971      * has this person already submitted,
2972      * is the assignment locked?
2973      *
2974      * @param int $userid - Optional userid so we can see if a different user can submit
2975      * @return bool
2976      */
2977     private function submissions_open($userid = 0) {
2978         global $USER;
2980         if (!$userid) {
2981             $userid = $USER->id;
2982         }
2984         $time = time();
2985         $dateopen = true;
2986         $finaldate = false;
2987         if ($this->get_instance()->cutoffdate) {
2988             $finaldate = $this->get_instance()->cutoffdate;
2989         }
2990         // User extensions.
2991         if ($finaldate) {
2992             $grade = $this->get_user_grade($userid, false);
2993             if ($grade && $grade->extensionduedate) {
2994                 // Extension can be before cut off date.
2995                 if ($grade->extensionduedate > $finaldate) {
2996                     $finaldate = $grade->extensionduedate;
2997                 }
2998             }
2999         }
3001         if ($finaldate) {
3002             $dateopen = ($this->get_instance()->allowsubmissionsfromdate <= $time && $time <= $finaldate);
3003         } else {
3004             $dateopen = ($this->get_instance()->allowsubmissionsfromdate <= $time);
3005         }
3007         if (!$dateopen) {
3008             return false;
3009         }
3011         // Now check if this user has already submitted etc.
3012         if (!is_enrolled($this->get_course_context(), $userid)) {
3013             return false;
3014         }
3015         $submission = false;
3016         if ($this->get_instance()->teamsubmission) {
3017             $submission = $this->get_group_submission($userid, 0, false);
3018         } else {
3019             $submission = $this->get_user_submission($userid, false);
3020         }
3021         if ($submission) {
3023             if ($this->get_instance()->submissiondrafts && $submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
3024                 // drafts are tracked and the student has submitted the assignment
3025                 return false;
3026             }
3027         }
3028         if ($grade = $this->get_user_grade($userid, false)) {
3029             if ($grade->locked) {
3030                 return false;
3031             }
3032         }
3034         if ($this->grading_disabled($userid)) {
3035             return false;
3036         }
3038         return true;
3039     }
3041     /**
3042      * render the files in file area
3043      * @param string $component
3044      * @param string $area
3045      * @param int $submissionid
3046      * @return string
3047      */
3048     public function render_area_files($component, $area, $submissionid) {
3049         global $USER;
3051         $fs = get_file_storage();
3052         $browser = get_file_browser();
3053         $files = $fs->get_area_files($this->get_context()->id, $component, $area , $submissionid , "timemodified", false);
3054         return $this->get_renderer()->assign_files($this->context, $submissionid, $area, $component);
3056     }
3058     /**
3059      * Returns a list of teachers that should be grading given submission
3060      *
3061      * @param int $userid
3062      * @return array
3063      */
3064     private function get_graders($userid) {
3065         //potential graders
3066         $potentialgraders = get_enrolled_users($this->context, "mod/assign:grade");
3068         $graders = array();
3069         if (groups_get_activity_groupmode($this->get_course_module()) == SEPARATEGROUPS) {   // Separate groups are being used
3070             if ($groups = groups_get_all_groups($this->get_course()->id, $userid)) {  // Try to find all groups
3071                 foreach ($groups as $group) {
3072                     foreach ($potentialgraders as $grader) {
3073                         if ($grader->id == $userid) {
3074                             continue; // do not send self
3075                         }
3076                         if (groups_is_member($group->id, $grader->id)) {
3077                             $graders[$grader->id] = $grader;
3078                         }
3079                     }
3080                 }
3081             } else {
3082                 // user not in group, try to find graders without group
3083                 foreach ($potentialgraders as $grader) {
3084                     if ($grader->id == $userid) {
3085                         continue; // do not send self
3086                     }
3087                     if (!groups_has_membership($this->get_course_module(), $grader->id)) {
3088                         $graders[$grader->id] = $grader;
3089                     }
3090                 }
3091             }
3092         } else {
3093             foreach ($potentialgraders as $grader) {
3094                 if ($grader->id == $userid) {
3095                     continue; // do not send self
3096                 }
3097                 // must be enrolled
3098                 if (is_enrolled($this->get_course_context(), $grader->id)) {
3099                     $graders[$grader->id] = $grader;
3100                 }
3101             }
3102         }
3103         return $graders;
3104     }
3106     /**
3107      * Format a notification for plain text
3108      *
3109      * @param string $messagetype
3110      * @param stdClass $info
3111      * @param stdClass $course
3112      * @param stdClass $context
3113      * @param string $modulename
3114      * @param string $assignmentname
3115      */
3116     private static function format_notification_message_text($messagetype, $info, $course, $context, $modulename, $assignmentname) {
3117         $posttext  = format_string($course->shortname, true, array('context' => $context->get_course_context())).' -> '.
3118                      $modulename.' -> '.
3119                      format_string($assignmentname, true, array('context' => $context))."\n";
3120         $posttext .= '---------------------------------------------------------------------'."\n";
3121         $posttext .= get_string($messagetype . 'text', "assign", $info)."\n";
3122         $posttext .= "\n---------------------------------------------------------------------\n";
3123         return $posttext;
3124     }
3126     /**
3127      * Format a notification for HTML
3128      *
3129      * @param string $messagetype
3130      * @param stdClass $info
3131      * @param stdClass $course
3132      * @param stdClass $context
3133      * @param string $modulename
3134      * @param stdClass $coursemodule
3135      * @param string $assignmentname
3136      * @param stdClass $info
3137      */
3138     private static function format_notification_message_html($messagetype, $info, $course, $context, $modulename, $coursemodule, $assignmentname) {
3139         global $CFG;
3140         $posthtml  = '<p><font face="sans-serif">'.
3141                      '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.format_string($course->shortname, true, array('context' => $context->get_course_context())).'</a> ->'.
3142                      '<a href="'.$CFG->wwwroot.'/mod/assign/index.php?id='.$course->id.'">'.$modulename.'</a> ->'.
3143                      '<a href="'.$CFG->wwwroot.'/mod/assign/view.php?id='.$coursemodule->id.'">'.format_string($assignmentname, true, array('context' => $context)).'</a></font></p>';
3144         $posthtml .= '<hr /><font face="sans-serif">';
3145         $posthtml .= '<p>'.get_string($messagetype . 'html', 'assign', $info).'</p>';
3146         $posthtml .= '</font><hr />';
3147         return $posthtml;
3148     }
3150     /**
3151      * Message someone about something (static so it can be called from cron)
3152      *
3153      * @param stdClass $userfrom
3154      * @param stdClass $userto
3155      * @param string $messagetype
3156      * @param string $eventtype
3157      * @param int $updatetime
3158      * @param stdClass $coursemodule
3159      * @param stdClass $context
3160      * @param stdClass $course
3161      * @param string $modulename
3162      * @param string $assignmentname
3163      * @return void
3164      */
3165     public static function send_assignment_notification($userfrom, $userto, $messagetype, $eventtype,
3166                                                         $updatetime, $coursemodule, $context, $course,
3167                                                         $modulename, $assignmentname, $blindmarking,
3168                                                         $uniqueidforuser) {
3169         global $CFG;
3171         $info = new stdClass();
3172         if ($blindmarking) {
3173             $info->username = get_string('participant', 'assign') . ' ' . $uniqueidforuser;
3174         } else {
3175             $info->username = fullname($userfrom, true);
3176         }
3177         $info->assignment = format_string($assignmentname,true, array('context'=>$context));
3178         $info->url = $CFG->wwwroot.'/mod/assign/view.php?id='.$coursemodule->id;
3179         $info->timeupdated = strftime('%c',$updatetime);
3181         $postsubject = get_string($messagetype . 'small', 'assign', $info);
3182         $posttext = self::format_notification_message_text($messagetype, $info, $course, $context, $modulename, $assignmentname);
3183         $posthtml = ($userto->mailformat == 1) ? self::format_notification_message_html($messagetype, $info, $course, $context, $modulename, $coursemodule, $assignmentname) : '';
3185         $eventdata = new stdClass();
3186         $eventdata->modulename       = 'assign';
3187         $eventdata->userfrom         = $userfrom;
3188         $eventdata->userto           = $userto;
3189         $eventdata->subject          = $postsubject;
3190         $eventdata->fullmessage      = $posttext;
3191         $eventdata->fullmessageformat = FORMAT_PLAIN;
3192         $eventdata->fullmessagehtml  = $posthtml;
3193         $eventdata->smallmessage     = $postsubject;
3195         $eventdata->name            = $eventtype;
3196         $eventdata->component       = 'mod_assign';
3197         $eventdata->notification    = 1;
3198         $eventdata->contexturl      = $info->url;
3199         $eventdata->contexturlname  = $info->assignment;
3201         message_send($eventdata);
3202     }
3204     /**
3205      * Message someone about something
3206      *
3207      * @param stdClass $userfrom
3208      * @param stdClass $userto
3209      * @param string $messagetype
3210      * @param string $eventtype
3211      * @param int $updatetime
3212      * @return void
3213      */
3214     public function send_notification($userfrom, $userto, $messagetype, $eventtype, $updatetime) {
3215         self::send_assignment_notification($userfrom, $userto, $messagetype, $eventtype,
3216                                            $updatetime, $this->get_course_module(), $this->get_context(),
3217                                            $this->get_course(), $this->get_module_name(),
3218                                            $this->get_instance()->name, $this->is_blind_marking(),
3219                                            $this->get_uniqueid_for_user($userfrom->id));
3220     }
3222     /**
3223      * Notify student upon successful submission
3224      *
3225      * @param stdClass $submission
3226      * @return void
3227      */
3228     private function notify_student_submission_receipt(stdClass $submission) {
3229         global $DB, $USER;
3231         $adminconfig = $this->get_admin_config();
3232         if (empty($adminconfig->submissionreceipts)) {
3233             // No need to do anything
3234             return;
3235         }
3236         if ($submission->userid) {
3237             $user = $DB->get_record('user', array('id'=>$submission->userid), '*', MUST_EXIST);
3238         } else {
3239             $user = $USER;
3240         }
3241         $this->send_notification($user, $user, 'submissionreceipt', 'assign_notification', $submission->timemodified);
3242     }
3244     /**
3245      * Send notifications to graders upon student submissions
3246      *
3247      * @param stdClass $submission
3248      * @return void
3249      */
3250     private function notify_graders(stdClass $submission) {
3251         global $DB, $USER;
3253         $late = $this->get_instance()->duedate && ($this->get_instance()->duedate < time());
3255         if (!$this->get_instance()->sendnotifications && !($late && $this->get_instance()->sendlatenotifications)) {          // No need to do anything
3256             return;
3257         }
3259         if ($submission->userid) {
3260             $user = $DB->get_record('user', array('id'=>$submission->userid), '*', MUST_EXIST);
3261         } else {
3262             $user = $USER;
3263         }
3264         if ($teachers = $this->get_graders($user->id)) {
3265             foreach ($teachers as $teacher) {
3266                 $this->send_notification($user, $teacher, 'gradersubmissionupdated', 'assign_notification', $submission->timemodified);
3267             }
3268         }
3269     }
3271     /**
3272      * assignment submission is processed before grading
3273      *
3274      * @param $mform If validation failed when submitting this form - this is the moodleform - it can be null
3275      * @return bool Return false if the validation fails. This affects which page is displayed next.
3276      */
3277     private function process_submit_for_grading($mform) {
3278         global $USER, $CFG;
3280         // Need submit permission to submit an assignment
3281         require_capability('mod/assign:submit', $this->context);
3282         require_once($CFG->dirroot . '/mod/assign/submissionconfirmform.php');
3283         require_sesskey();
3285         $data = new stdClass();
3286         $adminconfig = $this->get_admin_config();
3287         $requiresubmissionstatement = (!empty($adminconfig->requiresubmissionstatement) ||
3288                                        $this->get_instance()->requiresubmissionstatement) &&
3289                                        !empty($adminconfig->submissionstatement);
3291         $submissionstatement = '';
3292         if (!empty($adminconfig->submissionstatement)) {
3293             $submissionstatement = $adminconfig->submissionstatement;
3294         }
3296         if ($mform == null) {
3297             $mform = new mod_assign_confirm_submission_form(null, array($requiresubmissionstatement,
3298                                                                     $submissionstatement,
3299                                                                     $this->get_course_module()->id,
3300                                                                     $data));
3301         }
3303         $data = $mform->get_data();
3304         if (!$mform->is_cancelled()) {
3305             if ($mform->get_data() == false) {
3306                 return false;
3307             }
3308             if ($this->get_instance()->teamsubmission) {
3309                 $submission = $this->get_group_submission($USER->id, 0, true);
3310             } else {
3311                 $submission = $this->get_user_submission($USER->id, true);
3312             }
3314             if ($submission->status != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
3315                 // Give each submission plugin a chance to process the submission
3316                 $plugins = $this->get_submission_plugins();
3317                 foreach ($plugins as $plugin) {
3318                     $plugin->submit_for_grading();
3319                 }
3321                 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
3322                 $this->update_submission($submission, $USER->id, true, $this->get_instance()->teamsubmission);
3323                 $completion = new completion_info($this->get_course());
3324                 if ($completion->is_enabled($this->get_course_module()) && $this->get_instance()->completionsubmit) {
3325                     $completion->update_state($this->get_course_module(), COMPLETION_COMPLETE, $USER->id);
3326                 }
3328                 if (isset($data->submissionstatement)) {
3329                     $this->add_to_log('submission statement accepted', get_string('submissionstatementacceptedlog', 'mod_assign', fullname($USER)));
3330                 }
3331                 $this->add_to_log('submit for grading', $this->format_submission_for_log($submission));
3332                 $this->notify_graders($submission);
3333                 $this->notify_student_submission_receipt($submission);
3334                 // Trigger assessable_submitted event on submission.
3335                 $eventdata = new stdClass();
3336                 $eventdata->modulename   = 'assign';
3337                 $eventdata->cmid         = $this->get_course_module()->id;
3338                 $eventdata->itemid       = $submission->id;
3339                 $eventdata->courseid     = $this->get_course()->id;
3340                 $eventdata->userid       = $USER->id;
3341                 $eventdata->params       = array(
3342                     'submission_editable' => false,
3343                 );
3344                 events_trigger('assessable_submitted', $eventdata);
3345             }
3346         }
3347         return true;
3348     }
3350     /**
3351      * save the extension date for a single user
3352      *
3353      * @param int $userid The user id
3354      * @param mixed $extensionduedate Either an integer date or null
3355      * @return boolean
3356      */
3357     private function save_user_extension($userid, $extensionduedate) {
3358         global $DB;
3360         $grade = $this->get_user_grade($userid, true);
3361         $grade->extensionduedate = $extensionduedate;
3362         $grade->timemodified = time();
3364         $result = $DB->update_record('assign_grades', $grade);
3366         if ($result) {
3367             $this->add_to_log('grant extension', $this->format_grade_for_log($grade));
3368         }
3369         return $result;
3370     }
3372     /**
3373      * save extension date
3374      *
3375      * @param moodleform $mform The submitted form
3376      * @return boolean
3377      */
3378     private function process_save_extension(& $mform) {
3379         global $DB, $CFG;
3381         // Include extension form.
3382         require_once($CFG->dirroot . '/mod/assign/extensionform.php');
3384         // Need submit permission to submit an assignment.
3385         require_capability('mod/assign:grantextension', $this->context);
3387         $batchusers = optional_param('selectedusers', '', PARAM_TEXT);
3388         $userid = 0;
3389         if (!$batchusers) {
3390             $userid = required_param('userid', PARAM_INT);
3391             $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
3392         }
3393         $mform = new mod_assign_extension_form(null, array($this->get_course_module()->id,
3394                                                            $userid,
3395                                                            $batchusers,
3396                                                            $this->get_instance(),
3397                                                            null));
3399         if ($mform->is_cancelled()) {
3400             return true;
3401         }
3403         if ($formdata = $mform->get_data()) {
3404             if ($batchusers) {
3405                 $users = explode(',', $batchusers);
3406                 $result = true;
3407                 foreach ($users as $userid) {
3408                     $result = $this->save_user_extension($userid, $formdata->extensionduedate) && $result;
3409                 }
3410                 return $result;
3411             } else {
3412                 return $this->save_user_extension($userid, $formdata->extensionduedate);
3413             }
3414         }
3415         return false;
3416     }
3419     /**
3420      * save quick grades
3421      *
3422      * @return string The result of the save operation
3423      */
3424     private function process_save_quick_grades() {
3425         global $USER, $DB, $CFG;
3427         // Need grade permission
3428         require_capability('mod/assign:grade', $this->context);
3430         // make sure advanced grading is disabled
3431         $gradingmanager = get_grading_manager($this->get_context(), 'mod_assign', 'submissions');
3432         $controller = $gradingmanager->get_active_controller();
3433         if (!empty($controller)) {
3434             return get_string('errorquickgradingvsadvancedgrading', 'assign');
3435         }
3437         $users = array();
3438         // first check all the last modified values
3439         $currentgroup = groups_get_activity_group($this->get_course_module(), true);
3440         $participants = $this->list_participants($currentgroup, true);
3442         // gets a list of possible users and look for values based upon that.
3443         foreach ($participants as $userid => $unused) {
3444             $modified = optional_param('grademodified_' . $userid, -1, PARAM_INT);
3445             if ($modified >= 0) {
3446                 // gather the userid, updated grade and last modified value
3447                 $record = new stdClass();
3448                 $record->userid = $userid;
3449                 $record->grade = unformat_float(required_param('quickgrade_' . $record->userid, PARAM_TEXT));
3450                 $record->lastmodified = $modified;
3451                 $record->gradinginfo = grade_get_grades($this->get_course()->id, 'mod', 'assign', $this->get_instance()->id, array($userid));
3452                 $users[$userid] = $record;
3453             }
3454         }
3455         if (empty($users)) {
3456             // Quick check to see whether we have any users to update and we don't
3457             return get_string('quickgradingchangessaved', 'assign'); // Technical lie
3458         }
3460         list($userids, $params) = $DB->get_in_or_equal(array_keys($users), SQL_PARAMS_NAMED);
3461         $params['assignment'] = $this->get_instance()->id;
3462         // check them all for currency
3463         $sql = 'SELECT u.id as userid, g.grade as grade, g.timemodified as lastmodified
3464                   FROM {user} u
3465              LEFT JOIN {assign_grades} g ON u.id = g.userid AND g.assignment = :assignment
3466                  WHERE u.id ' . $userids;
3467         $currentgrades = $DB->get_recordset_sql($sql, $params);
3469         $modifiedusers = array();
3470         foreach ($currentgrades as $current) {
3471             $modified = $users[(int)$current->userid];
3472             $grade = $this->get_user_grade($userid, false);
3474             // check to see if the outcomes were modified
3475             if ($CFG->enableoutcomes) {
3476                 foreach ($modified->gradinginfo->outcomes as $outcomeid => $outcome) {
3477                     $oldoutcome = $outcome->grades[$modified->userid]->grade;
3478                     $newoutcome = optional_param('outcome_' . $outcomeid . '_' . $modified->userid, -1, PARAM_FLOAT);
3479                     if ($oldoutcome != $newoutcome) {
3480                         // can't check modified time for outcomes because it is not reported
3481                         $modifiedusers[$modified->userid] = $modified;
3482                         continue;
3483                     }
3484                 }
3485             }
3487             // let plugins participate
3488             foreach ($this->feedbackplugins as $plugin) {
3489                 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->supports_quickgrading()) {
3490                     if ($plugin->is_quickgrading_modified($modified->userid, $grade)) {
3491                         if ((int)$current->lastmodified > (int)$modified->lastmodified) {
3492                             return get_string('errorrecordmodified', 'assign');
3493                         } else {
3494                             $modifiedusers[$modified->userid] = $modified;
3495                             continue;
3496                         }
3497                     }
3498                 }
3499             }
3502             if (($current->grade < 0 || $current->grade === NULL) &&
3503                 ($modified->grade < 0 || $modified->grade === NULL)) {
3504                 // different ways to indicate no grade
3505                 continue;
3506             }
3507             // Treat 0 and null as different values
3508             if ($current->grade !== null) {
3509                 $current->grade = floatval($current->grade);
3510             }
3511             if ($current->grade !== $modified->grade) {
3512                 // grade changed
3513                 if ($this->grading_disabled($modified->userid)) {
3514                     continue;
3515                 }
3516                 if ((int)$current->lastmodified > (int)$modified->lastmodified) {
3517                     // error - record has been modified since viewing the page
3518                     return get_string('errorrecordmodified', 'assign');
3519                 } else {
3520                     $modifiedusers[$modified->userid] = $modified;
3521                 }
3522             }
3524         }
3525         $currentgrades->close();
3527         $adminconfig = $this->get_admin_config();
3528         $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook;
3530         // ok - ready to process the updates
3531         foreach ($modifiedusers as $userid => $modified) {
3532             $grade = $this->get_user_grade($userid, true);
3533             $grade->grade= grade_floatval(unformat_float($modified->grade));
3534             $grade->grader= $USER->id;
3536             // save plugins data
3537             foreach ($this->feedbackplugins as $plugin) {
3538                 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->supports_quickgrading()) {
3539                     $plugin->save_quickgrading_changes($userid, $grade);
3540                     if (('assignfeedback_' . $plugin->get_type()) == $gradebookplugin) {
3541                         // This is the feedback plugin chose to push comments to the gradebook.
3542                         $grade->feedbacktext = $plugin->text_for_gradebook($grade);
3543                         $grade->feedbackformat = $plugin->format_for_gradebook($grade);
3544                     }
3545                 }
3546             }
3548             $this->update_grade($grade);
3550             // save outcomes
3551             if ($CFG->enableoutcomes) {
3552                 $data = array();
3553                 foreach ($modified->gradinginfo->outcomes as $outcomeid => $outcome) {
3554                     $oldoutcome = $outcome->grades[$modified->userid]->grade;
3555                     $newoutcome = optional_param('outcome_' . $outcomeid . '_' . $modified->userid, -1, PARAM_INT);
3556                     if ($oldoutcome != $newoutcome) {
3557                         $data[$outcomeid] = $newoutcome;
3558                     }
3559                 }
3560                 if (count($data) > 0) {
3561                     grade_update_outcomes('mod/assign', $this->course->id, 'mod', 'assign', $this->get_instance()->id, $userid, $data);
3562                 }
3563             }
3565             $this->add_to_log('grade submission', $this->format_grade_for_log($grade));
3566         }
3568         return get_string('quickgradingchangessaved', 'assign');
3569     }
3571     /**
3572      * Reveal student identities to markers (and the gradebook)
3573      *
3574      * @return void
3575      */
3576     private function process_reveal_identities() {
3577         global $DB, $CFG;
3579         require_capability('mod/assign:revealidentities', $this->context);
3580         if (!confirm_sesskey()) {
3581             return false;
3582         }
3584         // Update the assignment record.
3585         $update = new stdClass();
3586         $update->id = $this->get_instance()->id;
3587         $update->revealidentities = 1;
3588         $DB->update_record('assign', $update);
3590         // Refresh the instance data.
3591         $this->instance = null;
3593         // Release the grades to the gradebook.
3594         // First create the column in the gradebook.
3595         $this->update_gradebook(false, $this->get_course_module()->id);
3597         // Now release all grades.
3599         $adminconfig = $this->get_admin_config();
3600         $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook;
3601         $grades = $DB->get_records('assign_grades', array('assignment'=>$this->get_instance()->id));
3603         $plugin = $this->get_feedback_plugin_by_type($gradebookplugin);
3605         foreach ($grades as $grade) {
3606             // Fetch any comments for this student.
3607             if ($plugin && $plugin->is_enabled() && $plugin->is_visible()) {
3608                 $grade->feedbacktext = $plugin->text_for_gradebook($grade);
3609                 $grade->feedbackformat = $plugin->format_for_gradebook($grade);
3610             }
3611             $this->gradebook_item_update(NULL, $grade);
3612         }
3614         $this->add_to_log('reveal identities', get_string('revealidentities', 'assign'));
3615     }
3618     /**
3619      * save grading options
3620      *
3621      * @return void
3622      */
3623     private function process_save_grading_options() {
3624         global $USER, $CFG;
3626         // Include grading options form
3627         require_once($CFG->dirroot . '/mod/assign/gradingoptionsform.php');
3629         // Need submit permission to submit an assignment
3630         require_capability('mod/assign:grade', $this->context);
3632         $mform = new mod_assign_grading_options_form(null, array('cm'=>$this->get_course_module()->id,
3633                                                                  'contextid'=>$this->context->id,
3634                                                                  'userid'=>$USER->id,
3635                                                                  'submissionsenabled'=>$this->is_any_submission_plugin_enabled(),
3636                                                                  'showquickgrading'=>false));
3637         if ($formdata = $mform->get_data()) {
3638             set_user_preference('assign_perpage', $formdata->perpage);
3639             if (isset($formdata->filter)) {
3640                 set_user_preference('assign_filter', $formdata->filter);
3641             }
3642         }
3643     }
3645    /**
3646     * Take a grade object and print a short summary for the log file.
3647     * The size limit for the log file is 255 characters, so be careful not
3648     * to include too much information.
3649     *
3650     * @param stdClass $grade
3651     * @return string
3652     */
3653     public function format_grade_for_log(stdClass $grade) {
3654         global $DB;
3656         $user = $DB->get_record('user', array('id' => $grade->userid), '*', MUST_EXIST);
3658         $info = get_string('gradestudent', 'assign', array('id'=>$user->id, 'fullname'=>fullname($user)));
3659         if ($grade->grade != '') {
3660             $info .= get_string('grade') . ': ' . $this->display_grade($grade->grade, false) . '. ';
3661         } else {
3662             $info .= get_string('nograde', 'assign');
3663         }
3664         if ($grade->locked) {
3665             $info .= get_string('submissionslocked', 'assign') . '. ';
3666         }
3667         if (!empty($grade->extensionduedate)) {
3668             $info .= get_string('userextensiondate', 'assign', userdate($grade->extensionduedate));
3669         }
3670         return $info;
3671     }
3673     /**
3674      * Take a submission object and print a short summary for the log file.
3675      * The size limit for the log file is 255 characters, so be careful not
3676      * to include too much information.
3677      *
3678      * @param stdClass $submission
3679      * @return string
3680      */
3681     private function format_submission_for_log(stdClass $submission) {
3682         $info = '';
3683         $info .= get_string('submissionstatus', 'assign') . ': ' . get_string('submissionstatus_' . $submission->status, 'assign') . '. <br>';
3684         // format_for_log here iterating every single log INFO  from either submission or grade in every assignment plugin
3686         foreach ($this->submissionplugins as $plugin) {
3687             if ($plugin->is_enabled() && $plugin->is_visible()) {
3690                 $info .= "<br>" . $plugin->format_for_log($submission);
3691             }
3692         }
3695         return $info;
3696     }
3698     /**
3699      * save assignment submission
3700      *
3701      * @param  moodleform $mform
3702      * @return bool
3703      */
3704     private function process_save_submission(&$mform) {
3705         global $USER, $CFG;
3707         // Include submission form
3708         require_once($CFG->dirroot . '/mod/assign/submission_form.php');
3710         // Need submit permission to submit an assignment
3711         require_capability('mod/assign:submit', $this->context);
3712         require_sesskey();
3714         $data = new stdClass();
3715         $mform = new mod_assign_submission_form(null, array($this, $data));
3716         if ($mform->is_cancelled()) {
3717             return true;
3718         }
3719         if ($data = $mform->get_data()) {
3720             if ($this->get_instance()->teamsubmission) {
3721                 $submission = $this->get_group_submission($USER->id, 0, true);
3722             } else {
3723                 $submission = $this->get_user_submission($USER->id, true);
3724             }
3725             if ($this->get_instance()->submissiondrafts) {
3726                 $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
3727             } else {
3728                 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
3729             }
3731             $grade = $this->get_user_grade($USER->id, false); // get the grade to check if it is locked
3732             if ($grade && $grade->locked) {
3733                 print_error('submissionslocked', 'assign');
3734                 return true;
3735             }
3738             foreach ($this->submissionplugins as $plugin) {
3739                 if ($plugin->is_enabled()) {
3740                     if (!$plugin->save($submission, $data)) {
3741                         print_error($plugin->get_error());
3742                     }
3743                 }
3744             }
3746             $this->update_submission($submission, $USER->id, true, $this->get_instance()->teamsubmission);
3748             // Logging
3749             if (isset($data->submissionstatement)) {
3750                 $this->add_to_log('submission statement accepted', get_string('submissionstatementacceptedlog', 'mod_assign', fullname($USER)));
3751             }
3752             $this->add_to_log('submit', $this->format_submission_for_log($submission));
3754             $complete = COMPLETION_INCOMPLETE;
3755             if ($submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
3756                 $complete = COMPLETION_COMPLETE;
3757             }
3758             $completion = new completion_info($this->get_course());
3759             if ($completion->is_enabled($this->get_course_module()) && $this->get_instance()->completionsubmit) {
3760                 $completion->update_state($this->get_course_module(), $complete, $USER->id);
3761             }
3763             if (!$this->get_instance()->submissiondrafts) {
3764                 $this->notify_student_submission_receipt($submission);
3765                 $this->notify_graders($submission);
3766                 // Trigger assessable_submitted event on submission.
3767                 $eventdata = new stdClass();
3768                 $eventdata->modulename   = 'assign';
3769                 $eventdata->cmid         = $this->get_course_module()->id;
3770                 $eventdata->itemid       = $submission->id;
3771                 $eventdata->courseid     = $this->get_course()->id;
3772                 $eventdata->userid       = $USER->id;
3773                 $eventdata->params       = array(
3774                     'submission_editable' => true,
3775                 );
3776                 events_trigger('assessable_submitted', $eventdata);
3777             }
3778             return true;
3779         }
3780         return false;
3781     }
3784     /**
3785      * Determine if this users grade is locked or overridden
3786      *
3787      * @param int $userid - The student userid
3788      * @return bool $gradingdisabled
3789      */
3790     public function grading_disabled($userid) {
3791         global $CFG;
3793         $gradinginfo = grade_get_grades($this->get_course()->id, 'mod', 'assign', $this->get_instance()->id, array($userid));
3794         if (!$grad