Merge branch 'master_MDL-34544' of git://github.com/danmarsden/moodle
[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');
66 /**
67  * Standard base class for mod_assign (assignment types).
68  *
69  * @package   mod_assign
70  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
71  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
72  */
73 class assign {
76     /** @var stdClass the assignment record that contains the global settings for this assign instance */
77     private $instance;
79     /** @var context the context of the course module for this assign instance (or just the course if we are
80         creating a new one) */
81     private $context;
83     /** @var stdClass the course this assign instance belongs to */
84     private $course;
86     /** @var stdClass the admin config for all assign instances  */
87     private $adminconfig;
90     /** @var assign_renderer the custom renderer for this module */
91     private $output;
93     /** @var stdClass the course module for this assign instance */
94     private $coursemodule;
96     /** @var array cache for things like the coursemodule name or the scale menu - only lives for a single
97         request */
98     private $cache;
100     /** @var array list of the installed submission plugins */
101     private $submissionplugins;
103     /** @var array list of the installed feedback plugins */
104     private $feedbackplugins;
106     /** @var string action to be used to return to this page (without repeating any form submissions etc.) */
107     private $returnaction = 'view';
109     /** @var array params to be used to return to this page */
110     private $returnparams = array();
112     /** @var string modulename prevents excessive calls to get_string */
113     private static $modulename = null;
115     /** @var string modulenameplural prevents excessive calls to get_string */
116     private static $modulenameplural = null;
118     /**
119      * Constructor for the base assign class
120      *
121      * @param mixed $coursemodulecontext context|null the course module context (or the course context if the coursemodule has not been created yet)
122      * @param mixed $coursemodule the current course module if it was already loaded - otherwise this class will load one from the context as required
123      * @param mixed $course the current course  if it was already loaded - otherwise this class will load one from the context as required
124      */
125     public function __construct($coursemodulecontext, $coursemodule, $course) {
126         global $PAGE;
128         $this->context = $coursemodulecontext;
129         $this->coursemodule = $coursemodule;
130         $this->course = $course;
131         $this->cache = array(); // temporary cache only lives for a single request - used to reduce db lookups
133         $this->submissionplugins = $this->load_plugins('assignsubmission');
134         $this->feedbackplugins = $this->load_plugins('assignfeedback');
135         $this->output = $PAGE->get_renderer('mod_assign');
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      * Expose the renderer to plugins
313      *
314      * @return assign_renderer
315      */
316     public function get_renderer() {
317         return $this->output;
318     }
320     /**
321      * Display the assignment, used by view.php
322      *
323      * The assignment is displayed differently depending on your role,
324      * the settings for the assignment and the status of the assignment.
325      * @param string $action The current action if any.
326      * @return void
327      */
328     public function view($action='') {
330         $o = '';
331         $mform = null;
333         // handle form submissions first
334         if ($action == 'savesubmission') {
335             $action = 'editsubmission';
336             if ($this->process_save_submission($mform)) {
337                 $action = 'view';
338             }
339         } else if ($action == 'lock') {
340             $this->process_lock();
341             $action = 'grading';
342         } else if ($action == 'reverttodraft') {
343             $this->process_revert_to_draft();
344             $action = 'grading';
345         } else if ($action == 'unlock') {
346             $this->process_unlock();
347             $action = 'grading';
348         } else if ($action == 'confirmsubmit') {
349             $action = 'submit';
350             if ($this->process_submit_for_grading($mform)) {
351                 $action = 'view';
352             }
353         } else if ($action == 'gradingbatchoperation') {
354             $action = $this->process_grading_batch_operation($mform);
355         } else if ($action == 'submitgrade') {
356             if (optional_param('saveandshownext', null, PARAM_ALPHA)) {
357                 //save and show next
358                 $action = 'grade';
359                 if ($this->process_save_grade($mform)) {
360                     $action = 'nextgrade';
361                 }
362             } else if (optional_param('nosaveandprevious', null, PARAM_ALPHA)) {
363                 $action = 'previousgrade';
364             } else if (optional_param('nosaveandnext', null, PARAM_ALPHA)) {
365                 //show next button
366                 $action = 'nextgrade';
367             } else if (optional_param('savegrade', null, PARAM_ALPHA)) {
368                 //save changes button
369                 $action = 'grade';
370                 if ($this->process_save_grade($mform)) {
371                     $action = 'grading';
372                 }
373             } else {
374                 //cancel button
375                 $action = 'grading';
376             }
377         } else if ($action == 'quickgrade') {
378             $message = $this->process_save_quick_grades();
379             $action = 'quickgradingresult';
380         } else if ($action == 'saveoptions') {
381             $this->process_save_grading_options();
382             $action = 'grading';
383         } else if ($action == 'saveextension') {
384             $action = 'grantextension';
385             if ($this->process_save_extension($mform)) {
386                 $action = 'grading';
387             }
388         } else if ($action == 'revealidentitiesconfirm') {
389             $this->process_reveal_identities();
390             $action = 'grading';
391         }
393         $returnparams = array('rownum'=>optional_param('rownum', 0, PARAM_INT));
394         $this->register_return_link($action, $returnparams);
396         // now show the right view page
397         if ($action == 'previousgrade') {
398             $mform = null;
399             $o .= $this->view_single_grade_page($mform, -1);
400         } else if ($action == 'quickgradingresult') {
401             $mform = null;
402             $o .= $this->view_quickgrading_result($message);
403         } else if ($action == 'nextgrade') {
404             $mform = null;
405             $o .= $this->view_single_grade_page($mform, 1);
406         } else if ($action == 'grade') {
407             $o .= $this->view_single_grade_page($mform);
408         } else if ($action == 'viewpluginassignfeedback') {
409             $o .= $this->view_plugin_content('assignfeedback');
410         } else if ($action == 'viewpluginassignsubmission') {
411             $o .= $this->view_plugin_content('assignsubmission');
412         } else if ($action == 'editsubmission') {
413             $o .= $this->view_edit_submission_page($mform);
414         } else if ($action == 'grading') {
415             $o .= $this->view_grading_page();
416         } else if ($action == 'downloadall') {
417             $o .= $this->download_submissions();
418         } else if ($action == 'submit') {
419             $o .= $this->check_submit_for_grading($mform);
420         } else if ($action == 'grantextension') {
421             $o .= $this->view_grant_extension($mform);
422         } else if ($action == 'revealidentities') {
423             $o .= $this->view_reveal_identities_confirm($mform);
424         } else if ($action == 'plugingradingbatchoperation') {
425             $o .= $this->view_plugin_grading_batch_operation($mform);
426         } else if ($action == 'viewpluginpage') {
427              $o .= $this->view_plugin_page();
428         } else {
429             $o .= $this->view_submission_page();
430         }
432         return $o;
433     }
436     /**
437      * Add this instance to the database
438      *
439      * @param stdClass $formdata The data submitted from the form
440      * @param bool $callplugins This is used to skip the plugin code
441      *             when upgrading an old assignment to a new one (the plugins get called manually)
442      * @return mixed false if an error occurs or the int id of the new instance
443      */
444     public function add_instance(stdClass $formdata, $callplugins) {
445         global $DB;
447         $err = '';
449         // add the database record
450         $update = new stdClass();
451         $update->name = $formdata->name;
452         $update->timemodified = time();
453         $update->timecreated = time();
454         $update->course = $formdata->course;
455         $update->courseid = $formdata->course;
456         $update->intro = $formdata->intro;
457         $update->introformat = $formdata->introformat;
458         $update->alwaysshowdescription = $formdata->alwaysshowdescription;
459         $update->submissiondrafts = $formdata->submissiondrafts;
460         $update->requiresubmissionstatement = $formdata->requiresubmissionstatement;
461         $update->sendnotifications = $formdata->sendnotifications;
462         $update->sendlatenotifications = $formdata->sendlatenotifications;
463         $update->duedate = $formdata->duedate;
464         $update->cutoffdate = $formdata->cutoffdate;
465         $update->allowsubmissionsfromdate = $formdata->allowsubmissionsfromdate;
466         $update->grade = $formdata->grade;
467         $update->completionsubmit = !empty($formdata->completionsubmit);
468         $update->teamsubmission = $formdata->teamsubmission;
469         $update->requireallteammemberssubmit = $formdata->requireallteammemberssubmit;
470         $update->teamsubmissiongroupingid = $formdata->teamsubmissiongroupingid;
471         $update->blindmarking = $formdata->blindmarking;
473         $returnid = $DB->insert_record('assign', $update);
474         $this->instance = $DB->get_record('assign', array('id'=>$returnid), '*', MUST_EXIST);
475         // cache the course record
476         $this->course = $DB->get_record('course', array('id'=>$formdata->course), '*', MUST_EXIST);
478         if ($callplugins) {
479             // call save_settings hook for submission plugins
480             foreach ($this->submissionplugins as $plugin) {
481                 if (!$this->update_plugin_instance($plugin, $formdata)) {
482                     print_error($plugin->get_error());
483                     return false;
484                 }
485             }
486             foreach ($this->feedbackplugins as $plugin) {
487                 if (!$this->update_plugin_instance($plugin, $formdata)) {
488                     print_error($plugin->get_error());
489                     return false;
490                 }
491             }
493             // in the case of upgrades the coursemodule has not been set so we need to wait before calling these two
494             // TODO: add event to the calendar
495             $this->update_calendar($formdata->coursemodule);
496             // TODO: add the item in the gradebook
497             $this->update_gradebook(false, $formdata->coursemodule);
499         }
501         $update = new stdClass();
502         $update->id = $this->get_instance()->id;
503         $update->nosubmissions = (!$this->is_any_submission_plugin_enabled()) ? 1: 0;
504         $DB->update_record('assign', $update);
506         return $returnid;
507     }
509     /**
510      * Delete all grades from the gradebook for this assignment
511      *
512      * @return bool
513      */
514     private function delete_grades() {
515         global $CFG;
517         return grade_update('mod/assign', $this->get_course()->id, 'mod', 'assign', $this->get_instance()->id, 0, NULL, array('deleted'=>1)) == GRADE_UPDATE_OK;
518     }
520     /**
521      * Delete this instance from the database
522      *
523      * @return bool false if an error occurs
524      */
525     public function delete_instance() {
526         global $DB;
527         $result = true;
529         foreach ($this->submissionplugins as $plugin) {
530             if (!$plugin->delete_instance()) {
531                 print_error($plugin->get_error());
532                 $result = false;
533             }
534         }
535         foreach ($this->feedbackplugins as $plugin) {
536             if (!$plugin->delete_instance()) {
537                 print_error($plugin->get_error());
538                 $result = false;
539             }
540         }
542         // delete files associated with this assignment
543         $fs = get_file_storage();
544         if (! $fs->delete_area_files($this->context->id) ) {
545             $result = false;
546         }
548         // delete_records will throw an exception if it fails - so no need for error checking here
550         $DB->delete_records('assign_submission', array('assignment'=>$this->get_instance()->id));
551         $DB->delete_records('assign_grades', array('assignment'=>$this->get_instance()->id));
552         $DB->delete_records('assign_plugin_config', array('assignment'=>$this->get_instance()->id));
554         // delete items from the gradebook
555         if (! $this->delete_grades()) {
556             $result = false;
557         }
559         // delete the instance
560         $DB->delete_records('assign', array('id'=>$this->get_instance()->id));
562         return $result;
563     }
565     /**
566      * Actual implementation of the reset course functionality, delete all the
567      * assignment submissions for course $data->courseid.
568      *
569      * @param stdClass $data the data submitted from the reset course.
570      * @return array status array
571      */
572     public function reset_userdata($data) {
573         global $CFG,$DB;
575         $componentstr = get_string('modulenameplural', 'assign');
576         $status = array();
578         $fs = get_file_storage();
579         if (!empty($data->reset_assign_submissions)) {
580             // Delete files associated with this assignment.
581             foreach ($this->submissionplugins as $plugin) {
582                 $fileareas = $plugin->get_file_areas();
583                 foreach ($fileareas as $filearea) {
584                     $fs->delete_area_files($this->context->id, 'mod_assign', $filearea);
585                 }
587                 if (!$plugin->delete_instance()) {
588                     $status[] = array('component' => $componentstr,
589                                       'item' => get_string('deleteallsubmissions', 'assign'),
590                                       'error' => $plugin->get_error());
591                 }
592             }
594             foreach ($this->feedbackplugins as $plugin) {
595                 $fileareas = $plugin->get_file_areas();
596                 foreach ($fileareas as $filearea) {
597                     $fs->delete_area_files($this->context->id, 'mod_assign', $filearea);
598                 }
600                 if (!$plugin->delete_instance()) {
601                     $status[] = array('component' => $componentstr,
602                                       'item' => get_string('deleteallsubmissions', 'assign'),
603                                       'error' => $plugin->get_error());
604                 }
605             }
607             $assignssql = "SELECT a.id
608                              FROM {assign} a
609                            WHERE a.course = :course";
610             $params = array ("course" => $data->courseid);
612             $DB->delete_records_select('assign_submission', "assignment IN ($assignssql)", $params);
613             $status[] = array('component' => $componentstr,
614                               'item' => get_string('deleteallsubmissions', 'assign'),
615                               'error' => false);
617             if (empty($data->reset_gradebook_grades)) {
618                 // Remove all grades from gradebook.
619                 require_once($CFG->dirroot.'/mod/assign/lib.php');
620                 assign_reset_gradebook($data->courseid);
621             }
622         }
623         // Updating dates - shift may be negative too.
624         if ($data->timeshift) {
625             shift_course_mod_dates('assign', array('duedate', 'allowsubmissionsfromdate', 'cutoffdate'), $data->timeshift, $data->courseid);
626             $status[] = array('component' => $componentstr,
627                               'item' => get_string('datechanged'),
628                               'error' => false);
629         }
631         return $status;
632     }
634     /**
635      * Update the settings for a single plugin
636      *
637      * @param assign_plugin $plugin The plugin to update
638      * @param stdClass $formdata The form data
639      * @return bool false if an error occurs
640      */
641     private function update_plugin_instance(assign_plugin $plugin, stdClass $formdata) {
642         if ($plugin->is_visible()) {
643             $enabledname = $plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled';
644             if ($formdata->$enabledname) {
645                 $plugin->enable();
646                 if (!$plugin->save_settings($formdata)) {
647                     print_error($plugin->get_error());
648                     return false;
649                 }
650             } else {
651                 $plugin->disable();
652             }
653         }
654         return true;
655     }
657     /**
658      * Update the gradebook information for this assignment
659      *
660      * @param bool $reset If true, will reset all grades in the gradbook for this assignment
661      * @param int $coursemoduleid This is required because it might not exist in the database yet
662      * @return bool
663      */
664     public function update_gradebook($reset, $coursemoduleid) {
665          global $CFG;
666         /** Include lib.php */
667         require_once($CFG->dirroot.'/mod/assign/lib.php');
668         $assign = clone $this->get_instance();
669         $assign->cmidnumber = $coursemoduleid;
670         $param = null;
671         if ($reset) {
672             $param = 'reset';
673         }
675         return assign_grade_item_update($assign, $param);
676     }
678     /** Load and cache the admin config for this module
679      *
680      * @return stdClass the plugin config
681      */
682     public function get_admin_config() {
683         if ($this->adminconfig) {
684             return $this->adminconfig;
685         }
686         $this->adminconfig = get_config('assign');
687         return $this->adminconfig;
688     }
691     /**
692      * Update the calendar entries for this assignment
693      *
694      * @param int $coursemoduleid - Required to pass this in because it might not exist in the database yet
695      * @return bool
696      */
697     public function update_calendar($coursemoduleid) {
698         global $DB, $CFG;
699         require_once($CFG->dirroot.'/calendar/lib.php');
701         // special case for add_instance as the coursemodule has not been set yet.
703         if ($this->get_instance()->duedate) {
704             $event = new stdClass();
706             if ($event->id = $DB->get_field('event', 'id', array('modulename'=>'assign', 'instance'=>$this->get_instance()->id))) {
708                 $event->name        = $this->get_instance()->name;
710                 $event->description = format_module_intro('assign', $this->get_instance(), $coursemoduleid);
711                 $event->timestart   = $this->get_instance()->duedate;
713                 $calendarevent = calendar_event::load($event->id);
714                 $calendarevent->update($event);
715             } else {
716                 $event = new stdClass();
717                 $event->name        = $this->get_instance()->name;
718                 $event->description = format_module_intro('assign', $this->get_instance(), $coursemoduleid);
719                 $event->courseid    = $this->get_instance()->course;
720                 $event->groupid     = 0;
721                 $event->userid      = 0;
722                 $event->modulename  = 'assign';
723                 $event->instance    = $this->get_instance()->id;
724                 $event->eventtype   = 'due';
725                 $event->timestart   = $this->get_instance()->duedate;
726                 $event->timeduration = 0;
728                 calendar_event::create($event);
729             }
730         } else {
731             $DB->delete_records('event', array('modulename'=>'assign', 'instance'=>$this->get_instance()->id));
732         }
733     }
736     /**
737      * Update this instance in the database
738      *
739      * @param stdClass $formdata - the data submitted from the form
740      * @return bool false if an error occurs
741      */
742     public function update_instance($formdata) {
743         global $DB;
745         $update = new stdClass();
746         $update->id = $formdata->instance;
747         $update->name = $formdata->name;
748         $update->timemodified = time();
749         $update->course = $formdata->course;
750         $update->intro = $formdata->intro;
751         $update->introformat = $formdata->introformat;
752         $update->alwaysshowdescription = $formdata->alwaysshowdescription;
753         $update->submissiondrafts = $formdata->submissiondrafts;
754         $update->requiresubmissionstatement = $formdata->requiresubmissionstatement;
755         $update->sendnotifications = $formdata->sendnotifications;
756         $update->sendlatenotifications = $formdata->sendlatenotifications;
757         $update->duedate = $formdata->duedate;
758         $update->cutoffdate = $formdata->cutoffdate;
759         $update->allowsubmissionsfromdate = $formdata->allowsubmissionsfromdate;
760         $update->grade = $formdata->grade;
761         $update->completionsubmit = !empty($formdata->completionsubmit);
762         $update->teamsubmission = $formdata->teamsubmission;
763         $update->requireallteammemberssubmit = $formdata->requireallteammemberssubmit;
764         $update->teamsubmissiongroupingid = $formdata->teamsubmissiongroupingid;
765         $update->blindmarking = $formdata->blindmarking;
768         $result = $DB->update_record('assign', $update);
769         $this->instance = $DB->get_record('assign', array('id'=>$update->id), '*', MUST_EXIST);
771         // load the assignment so the plugins have access to it
773         // call save_settings hook for submission plugins
774         foreach ($this->submissionplugins as $plugin) {
775             if (!$this->update_plugin_instance($plugin, $formdata)) {
776                 print_error($plugin->get_error());
777                 return false;
778             }
779         }
780         foreach ($this->feedbackplugins as $plugin) {
781             if (!$this->update_plugin_instance($plugin, $formdata)) {
782                 print_error($plugin->get_error());
783                 return false;
784             }
785         }
788         // update the database record
791         // update all the calendar events
792         $this->update_calendar($this->get_course_module()->id);
794         $this->update_gradebook(false, $this->get_course_module()->id);
796         $update = new stdClass();
797         $update->id = $this->get_instance()->id;
798         $update->nosubmissions = (!$this->is_any_submission_plugin_enabled()) ? 1: 0;
799         $DB->update_record('assign', $update);
805         return $result;
806     }
808     /**
809      * add elements in grading plugin form
810      *
811      * @param mixed $grade stdClass|null
812      * @param MoodleQuickForm $mform
813      * @param stdClass $data
814      * @return void
815      */
816     private function add_plugin_grade_elements($grade, MoodleQuickForm $mform, stdClass $data) {
817         foreach ($this->feedbackplugins as $plugin) {
818             if ($plugin->is_enabled() && $plugin->is_visible()) {
819                 $mform->addElement('header', 'header_' . $plugin->get_type(), $plugin->get_name());
820                 if (!$plugin->get_form_elements($grade, $mform, $data)) {
821                     $mform->removeElement('header_' . $plugin->get_type());
822                 }
823             }
824         }
825     }
829     /**
830      * Add one plugins settings to edit plugin form
831      *
832      * @param assign_plugin $plugin The plugin to add the settings from
833      * @param MoodleQuickForm $mform The form to add the configuration settings to. This form is modified directly (not returned)
834      * @return void
835      */
836     private function add_plugin_settings(assign_plugin $plugin, MoodleQuickForm $mform) {
837         global $CFG;
838         if ($plugin->is_visible()) {
839             // enabled
840             //tied disableIf rule to this select element
841             $mform->addElement('selectyesno', $plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', $plugin->get_name());
842             $mform->addHelpButton($plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', 'enabled', $plugin->get_subtype() . '_' . $plugin->get_type());
845             $default = get_config($plugin->get_subtype() . '_' . $plugin->get_type(), 'default');
846             if ($plugin->get_config('enabled') !== false) {
847                 $default = $plugin->is_enabled();
848             }
849             $mform->setDefault($plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', $default);
851             $plugin->get_settings($mform);
853         }
855     }
858     /**
859      * Add settings to edit plugin form
860      *
861      * @param MoodleQuickForm $mform The form to add the configuration settings to. This form is modified directly (not returned)
862      * @return void
863      */
864     public function add_all_plugin_settings(MoodleQuickForm $mform) {
865         $mform->addElement('header', 'general', get_string('submissionsettings', 'assign'));
867         foreach ($this->submissionplugins as $plugin) {
868             $this->add_plugin_settings($plugin, $mform);
870         }
871         $mform->addElement('header', 'general', get_string('feedbacksettings', 'assign'));
872         foreach ($this->feedbackplugins as $plugin) {
873             $this->add_plugin_settings($plugin, $mform);
874         }
875     }
877     /**
878      * Allow each plugin an opportunity to update the defaultvalues
879      * passed in to the settings form (needed to set up draft areas for
880      * editor and filemanager elements)
881      * @param array $defaultvalues
882      */
883     public function plugin_data_preprocessing(&$defaultvalues) {
884         foreach ($this->submissionplugins as $plugin) {
885             if ($plugin->is_visible()) {
886                 $plugin->data_preprocessing($defaultvalues);
887             }
888         }
889         foreach ($this->feedbackplugins as $plugin) {
890             if ($plugin->is_visible()) {
891                 $plugin->data_preprocessing($defaultvalues);
892             }
893         }
894     }
896     /**
897      * Get the name of the current module.
898      *
899      * @return string the module name (Assignment)
900      */
901     protected function get_module_name() {
902         if (isset(self::$modulename)) {
903             return self::$modulename;
904         }
905         self::$modulename = get_string('modulename', 'assign');
906         return self::$modulename;
907     }
909     /**
910      * Get the plural name of the current module.
911      *
912      * @return string the module name plural (Assignments)
913      */
914     protected function get_module_name_plural() {
915         if (isset(self::$modulenameplural)) {
916             return self::$modulenameplural;
917         }
918         self::$modulenameplural = get_string('modulenameplural', 'assign');
919         return self::$modulenameplural;
920     }
922     /**
923      * Has this assignment been constructed from an instance?
924      *
925      * @return bool
926      */
927     public function has_instance() {
928         return $this->instance || $this->get_course_module();
929     }
931     /**
932      * Get the settings for the current instance of this assignment
933      *
934      * @return stdClass The settings
935      */
936     public function get_instance() {
937         global $DB;
938         if ($this->instance) {
939             return $this->instance;
940         }
941         if ($this->get_course_module()) {
942             $this->instance = $DB->get_record('assign', array('id' => $this->get_course_module()->instance), '*', MUST_EXIST);
943         }
944         if (!$this->instance) {
945             throw new coding_exception('Improper use of the assignment class. Cannot load the assignment record.');
946         }
947         return $this->instance;
948     }
950     /**
951      * Get the context of the current course
952      * @return mixed context|null The course context
953      */
954     public function get_course_context() {
955         if (!$this->context && !$this->course) {
956             throw new coding_exception('Improper use of the assignment class. Cannot load the course context.');
957         }
958         if ($this->context) {
959             return $this->context->get_course_context();
960         } else {
961             return context_course::instance($this->course->id);
962         }
963     }
966     /**
967      * Get the current course module
968      *
969      * @return mixed stdClass|null The course module
970      */
971     public function get_course_module() {
972         if ($this->coursemodule) {
973             return $this->coursemodule;
974         }
975         if (!$this->context) {
976             return null;
977         }
979         if ($this->context->contextlevel == CONTEXT_MODULE) {
980             $this->coursemodule = get_coursemodule_from_id('assign', $this->context->instanceid, 0, false, MUST_EXIST);
981             return $this->coursemodule;
982         }
983         return null;
984     }
986     /**
987      * Get context module
988      *
989      * @return context
990      */
991     public function get_context() {
992         return $this->context;
993     }
995     /**
996      * Get the current course
997      * @return mixed stdClass|null The course
998      */
999     public function get_course() {
1000         global $DB;
1001         if ($this->course) {
1002             return $this->course;
1003         }
1005         if (!$this->context) {
1006             return null;
1007         }
1008         $this->course = $DB->get_record('course', array('id' => $this->get_course_context()->instanceid), '*', MUST_EXIST);
1009         return $this->course;
1010     }
1012     /**
1013      * Return a grade in user-friendly form, whether it's a scale or not
1014      *
1015      * @param mixed $grade int|null
1016      * @param boolean $editing Are we allowing changes to this grade?
1017      * @param int $userid The user id the grade belongs to
1018      * @param int $modified Timestamp from when the grade was last modified
1019      * @return string User-friendly representation of grade
1020      */
1021     public function display_grade($grade, $editing, $userid=0, $modified=0) {
1022         global $DB;
1024         static $scalegrades = array();
1026         if ($this->get_instance()->grade >= 0) {
1027             // Normal number
1028             if ($editing && $this->get_instance()->grade > 0) {
1029                 if ($grade < 0) {
1030                     $displaygrade = '';
1031                 } else {
1032                     $displaygrade = format_float($grade);
1033                 }
1034                 $o = '<input type="text" name="quickgrade_' . $userid . '" value="' . $displaygrade . '" 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 = '<select name="quickgrade_' . $userid . '" class="quickgrade">';
1057                 $o .= '<option value="-1">' . get_string('nograde') . '</option>';
1058                 foreach ($this->cache['scale'] as $optionid => $option) {
1059                     $selected = '';
1060                     if ($grade == $optionid) {
1061                         $selected = 'selected="selected"';
1062                     }
1063                     $o .= '<option value="' . $optionid . '" ' . $selected . '>' . $option . '</option>';
1064                 }
1065                 $o .= '</select>';
1066                 $o .= '<input type="hidden" name="grademodified_' . $userid . '" value="' . $modified . '"/>';
1067                 return $o;
1068             } else {
1069                 $scaleid = (int)$grade;
1070                 if (isset($this->cache['scale'][$scaleid])) {
1071                     return $this->cache['scale'][$scaleid];
1072                 }
1073                 return '-';
1074             }
1075         }
1076     }
1078     /**
1079      * Load a list of users enrolled in the current course with the specified permission and group (0 for no group)
1080      *
1081      * @param int $currentgroup
1082      * @param bool $idsonly
1083      * @return array List of user records
1084      */
1085     public function list_participants($currentgroup, $idsonly) {
1086         if ($idsonly) {
1087             return get_enrolled_users($this->context, "mod/assign:submit", $currentgroup, 'u.id');
1088         } else {
1089             return get_enrolled_users($this->context, "mod/assign:submit", $currentgroup);
1090         }
1091     }
1093     /**
1094      * Load a count of valid teams for this assignment
1095      *
1096      * @return int number of valid teams
1097      */
1098     public function count_teams() {
1100         $groups = groups_get_all_groups($this->get_course()->id, 0, $this->get_instance()->teamsubmissiongroupingid, 'g.id');
1101         $count = count($groups);
1103         // See if there are any users in the default group.
1104         $defaultusers = $this->get_submission_group_members(0, true);
1105         if (count($defaultusers) > 0) {
1106             $count += 1;
1107         }
1108         return $count;
1109     }
1111     /**
1112      * Load a count of users enrolled in the current course with the specified permission and group (0 for no group)
1113      *
1114      * @param int $currentgroup
1115      * @return int number of matching users
1116      */
1117     public function count_participants($currentgroup) {
1118         return count_enrolled_users($this->context, "mod/assign:submit", $currentgroup);
1119     }
1121     /**
1122      * Load a count of users submissions in the current module that require grading
1123      * This means the submission modification time is more recent than the
1124      * grading modification time.
1125      *
1126      * @return int number of matching submissions
1127      */
1128     public function count_submissions_need_grading() {
1129         global $DB;
1131         $params = array($this->get_course_module()->instance);
1133         return $DB->count_records_sql("SELECT COUNT('x')
1134                                        FROM {assign_submission} s
1135                                        LEFT JOIN {assign_grades} g ON s.assignment = g.assignment AND s.userid = g.userid
1136                                        WHERE s.assignment = ?
1137                                            AND s.timemodified IS NOT NULL
1138                                            AND (s.timemodified > g.timemodified OR g.timemodified IS NULL)",
1139                                        $params);
1140     }
1142     /**
1143      * Load a count of grades
1144      *
1145      * @return int number of grades
1146      */
1147     public function count_grades() {
1148         global $DB;
1150         if (!$this->has_instance()) {
1151             return 0;
1152         }
1154         $sql = 'SELECT COUNT(id) FROM {assign_grades} WHERE assignment = ?';
1155         $params = array($this->get_course_module()->instance);
1157         return $DB->count_records_sql($sql, $params);
1158     }
1160     /**
1161      * Load a count of submissions
1162      *
1163      * @return int number of submissions
1164      */
1165     public function count_submissions() {
1166         global $DB;
1168         if (!$this->has_instance()) {
1169             return 0;
1170         }
1172         $sql = 'SELECT COUNT(id) FROM {assign_submission} WHERE assignment = ?';
1173         $params = array($this->get_course_module()->instance);
1175         if ($this->get_instance()->teamsubmission) {
1176             // only look at team submissions
1177             $sql .= ' AND userid = ?';
1178             $params[] = 0;
1179         }
1180         return $DB->count_records_sql($sql, $params);
1181     }
1183     /**
1184      * Load a count of submissions with a specified status
1185      *
1186      * @param string $status The submission status - should match one of the constants
1187      * @return int number of matching submissions
1188      */
1189     public function count_submissions_with_status($status) {
1190         global $DB;
1191         $sql = 'SELECT COUNT(id) FROM {assign_submission} WHERE assignment = ? AND status = ?';
1192         $params = array($this->get_course_module()->instance, $status);
1194         if ($this->get_instance()->teamsubmission) {
1195             // only look at team submissions
1196             $sql .= ' AND userid = ?';
1197             $params[] = 0;
1198         }
1199         return $DB->count_records_sql($sql, $params);
1200     }
1202     /**
1203      * Utility function to get the userid for every row in the grading table
1204      * so the order can be frozen while we iterate it
1205      *
1206      * @return array An array of userids
1207      */
1208     private function get_grading_userid_list() {
1209         $filter = get_user_preferences('assign_filter', '');
1210         $table = new assign_grading_table($this, 0, $filter, 0, false);
1212         $useridlist = $table->get_column_data('userid');
1214         return $useridlist;
1215     }
1218     /**
1219      * Utility function get the userid based on the row number of the grading table.
1220      * This takes into account any active filters on the table.
1221      *
1222      * @param int $num The row number of the user
1223      * @param bool $last This is set to true if this is the last user in the table
1224      * @return mixed The user id of the matching user or false if there was an error
1225      */
1226     private function get_userid_for_row($num, $last) {
1227         if (!array_key_exists('userid_for_row', $this->cache)) {
1228             $this->cache['userid_for_row'] = array();
1229         }
1230         if (array_key_exists($num, $this->cache['userid_for_row'])) {
1231             list($userid, $last) = $this->cache['userid_for_row'][$num];
1232             return $userid;
1233         }
1235         $filter = get_user_preferences('assign_filter', '');
1236         $table = new assign_grading_table($this, 0, $filter, 0, false);
1238         $userid = $table->get_cell_data($num, 'userid', $last);
1240         $this->cache['userid_for_row'][$num] = array($userid, $last);
1241         return $userid;
1242     }
1244     /**
1245      * Return all assignment submissions by ENROLLED students (even empty)
1246      *
1247      * @param string $sort optional field names for the ORDER BY in the sql query
1248      * @param string $dir optional specifying the sort direction, defaults to DESC
1249      * @return array The submission objects indexed by id
1250      */
1251     private function get_all_submissions( $sort="", $dir="DESC") {
1252         global $CFG, $DB;
1254         if ($sort == "lastname" or $sort == "firstname") {
1255             $sort = "u.$sort $dir";
1256         } else if (empty($sort)) {
1257             $sort = "a.timemodified DESC";
1258         } else {
1259             $sort = "a.$sort $dir";
1260         }
1262         return $DB->get_records_sql("SELECT a.*
1263                                        FROM {assign_submission} a, {user} u
1264                                       WHERE u.id = a.userid
1265                                             AND a.assignment = ?
1266                                    ORDER BY $sort", array($this->get_instance()->id));
1268     }
1270     /**
1271      * Generate zip file from array of given files
1272      *
1273      * @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
1274      * @return path of temp file - note this returned file does not have a .zip extension - it is a temp file.
1275      */
1276      private function pack_files($filesforzipping) {
1277          global $CFG;
1278          //create path for new zip file.
1279          $tempzip = tempnam($CFG->tempdir.'/', 'assignment_');
1280          //zip files
1281          $zipper = new zip_packer();
1282          if ($zipper->archive_to_pathname($filesforzipping, $tempzip)) {
1283              return $tempzip;
1284          }
1285          return false;
1286     }
1288     /**
1289      * Finds all assignment notifications that have yet to be mailed out, and mails them.
1290      *
1291      * Cron function to be run periodically according to the moodle cron
1292      *
1293      * @return bool
1294      */
1295     static function cron() {
1296         global $DB;
1298         // only ever send a max of one days worth of updates
1299         $yesterday = time() - (24 * 3600);
1300         $timenow   = time();
1302         // Collect all submissions from the past 24 hours that require mailing.
1303         $sql = "SELECT s.*, a.course, a.name, a.blindmarking, a.revealidentities,
1304                        g.*, g.id as gradeid, g.timemodified as lastmodified
1305                  FROM {assign} a
1306                  JOIN {assign_grades} g ON g.assignment = a.id
1307             LEFT JOIN {assign_submission} s ON s.assignment = a.id AND s.userid = g.userid
1308                 WHERE g.timemodified >= :yesterday AND
1309                       g.timemodified <= :today AND
1310                       g.mailed = 0";
1311         $params = array('yesterday' => $yesterday, 'today' => $timenow);
1312         $submissions = $DB->get_records_sql($sql, $params);
1314         if (empty($submissions)) {
1315             mtrace('done.');
1316             return true;
1317         }
1319         mtrace('Processing ' . count($submissions) . ' assignment submissions ...');
1321         // Preload courses we are going to need those.
1322         $courseids = array();
1323         foreach ($submissions as $submission) {
1324             $courseids[] = $submission->course;
1325         }
1326         // Filter out duplicates
1327         $courseids = array_unique($courseids);
1328         $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
1329         list($courseidsql, $params) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
1330         $sql = "SELECT c.*, {$ctxselect}
1331                   FROM {course} c
1332              LEFT JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
1333                  WHERE c.id {$courseidsql}";
1334         $params['contextlevel'] = CONTEXT_COURSE;
1335         $courses = $DB->get_records_sql($sql, $params);
1336         // Clean up... this could go on for a while.
1337         unset($courseids);
1338         unset($ctxselect);
1339         unset($courseidsql);
1340         unset($params);
1342         // Simple array we'll use for caching modules.
1343         $modcache = array();
1345         // Message students about new feedback
1346         foreach ($submissions as $submission) {
1348             mtrace("Processing assignment submission $submission->id ...");
1350             // do not cache user lookups - could be too many
1351             if (!$user = $DB->get_record("user", array("id"=>$submission->userid))) {
1352                 mtrace("Could not find user $submission->userid");
1353                 continue;
1354             }
1356             // use a cache to prevent the same DB queries happening over and over
1357             if (!array_key_exists($submission->course, $courses)) {
1358                 mtrace("Could not find course $submission->course");
1359                 continue;
1360             }
1361             $course = $courses[$submission->course];
1362             if (isset($course->ctxid)) {
1363                 // Context has not yet been preloaded. Do so now.
1364                 context_helper::preload_from_record($course);
1365             }
1367             // Override the language and timezone of the "current" user, so that
1368             // mail is customised for the receiver.
1369             cron_setup_user($user, $course);
1371             // context lookups are already cached
1372             $coursecontext = context_course::instance($course->id);
1373             if (!is_enrolled($coursecontext, $user->id)) {
1374                 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
1375                 mtrace(fullname($user)." not an active participant in " . $courseshortname);
1376                 continue;
1377             }
1379             if (!$grader = $DB->get_record("user", array("id"=>$submission->grader))) {
1380                 mtrace("Could not find grader $submission->grader");
1381                 continue;
1382             }
1384             if (!array_key_exists($submission->assignment, $modcache)) {
1385                 if (! $mod = get_coursemodule_from_instance("assign", $submission->assignment, $course->id)) {
1386                     mtrace("Could not find course module for assignment id $submission->assignment");
1387                     continue;
1388                 }
1389                 $modcache[$submission->assignment] = $mod;
1390             } else {
1391                 $mod = $modcache[$submission->assignment];
1392             }
1393             // context lookups are already cached
1394             $contextmodule = context_module::instance($mod->id);
1396             if (!$mod->visible) {
1397                 // Hold mail notification for hidden assignments until later
1398                 continue;
1399             }
1401             // need to send this to the student
1402             $messagetype = 'feedbackavailable';
1403             $eventtype = 'assign_notification';
1404             $updatetime = $submission->lastmodified;
1405             $modulename = get_string('modulename', 'assign');
1407             $uniqueid = 0;
1408             if ($submission->blindmarking && !$submission->revealidentities) {
1409                 $uniqueid = self::get_uniqueid_for_user_static($submission->assignment, $user->id);
1410             }
1411             self::send_assignment_notification($grader, $user, $messagetype, $eventtype, $updatetime,
1412                                                $mod, $contextmodule, $course, $modulename, $submission->name,
1413                                                $submission->blindmarking && !$submission->revealidentities,
1414                                                $uniqueid);
1416             $grade = new stdClass();
1417             $grade->id = $submission->gradeid;
1418             $grade->mailed = 1;
1419             $DB->update_record('assign_grades', $grade);
1421             mtrace('Done');
1422         }
1423         mtrace('Done processing ' . count($submissions) . ' assignment submissions');
1425         cron_setup_user();
1427         // Free up memory just to be sure
1428         unset($courses);
1429         unset($modcache);
1431         return true;
1432     }
1434     /**
1435      * Update a grade in the grade table for the assignment and in the gradebook
1436      *
1437      * @param stdClass $grade a grade record keyed on id
1438      * @return bool true for success
1439      */
1440     public function update_grade($grade) {
1441         global $DB;
1443         $grade->timemodified = time();
1445         if ($grade->grade && $grade->grade != -1) {
1446             if ($this->get_instance()->grade > 0) {
1447                 if (!is_numeric($grade->grade)) {
1448                     return false;
1449                 } else if ($grade->grade > $this->get_instance()->grade) {
1450                     return false;
1451                 } else if ($grade->grade < 0) {
1452                     return false;
1453                 }
1454             } else {
1455                 // this is a scale
1456                 if ($scale = $DB->get_record('scale', array('id' => -($this->get_instance()->grade)))) {
1457                     $scaleoptions = make_menu_from_list($scale->scale);
1458                     if (!array_key_exists((int) $grade->grade, $scaleoptions)) {
1459                         return false;
1460                     }
1461                 }
1462             }
1463         }
1465         $result = $DB->update_record('assign_grades', $grade);
1466         if ($result) {
1467             $this->gradebook_item_update(null, $grade);
1468         }
1469         return $result;
1470     }
1472     /**
1473      * View the grant extension date page
1474      *
1475      * Uses url parameters 'userid'
1476      * or from parameter 'selectedusers'
1477      * @param moodleform $mform - Used for validation of the submitted data
1478      * @return string
1479      */
1480     private function view_grant_extension($mform) {
1481         global $DB, $CFG;
1482         require_once($CFG->dirroot . '/mod/assign/extensionform.php');
1484         $o = '';
1485         $batchusers = optional_param('selectedusers', '', PARAM_TEXT);
1486         $data = new stdClass();
1487         $data->extensionduedate = null;
1488         $userid = 0;
1489         if (!$batchusers) {
1490             $userid = required_param('userid', PARAM_INT);
1492             $grade = $this->get_user_grade($userid, false);
1494             $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
1496             if ($grade) {
1497                 $data->extensionduedate = $grade->extensionduedate;
1498             }
1499             $data->userid = $userid;
1500         } else {
1501             $data->batchusers = $batchusers;
1502         }
1503         $o .= $this->output->render(new assign_header($this->get_instance(),
1504                                                       $this->get_context(),
1505                                                       $this->show_intro(),
1506                                                       $this->get_course_module()->id,
1507                                                       get_string('grantextension', 'assign')));
1509         if (!$mform) {
1510             $mform = new mod_assign_extension_form(null, array($this->get_course_module()->id,
1511                                                                $userid,
1512                                                                $batchusers,
1513                                                                $this->get_instance(),
1514                                                                $data));
1515         }
1516         $o .= $this->output->render(new assign_form('extensionform', $mform));
1517         $o .= $this->view_footer();
1518         return $o;
1519     }
1521     /**
1522      * Get a list of the users in the same group as this user
1523      *
1524      * @param int $groupid The id of the group whose members we want or 0 for the default group
1525      * @param bool $onlyids Whether to retrieve only the user id's
1526      * @return array The users (possibly id's only)
1527      */
1528     public function get_submission_group_members($groupid, $onlyids) {
1529         $members = array();
1530         if ($groupid != 0) {
1531             if ($onlyids) {
1532                 $allusers = groups_get_members($groupid, 'u.id');
1533             } else {
1534                 $allusers = groups_get_members($groupid);
1535             }
1536             foreach ($allusers as $user) {
1537                 if ($this->get_submission_group($user->id)) {
1538                     $members[] = $user;
1539                 }
1540             }
1541         } else {
1542             $allusers = $this->list_participants(null, $onlyids);
1543             foreach ($allusers as $user) {
1544                 if ($this->get_submission_group($user->id) == null) {
1545                     $members[] = $user;
1546                 }
1547             }
1548         }
1549         return $members;
1550     }
1552     /**
1553      * Get a list of the users in the same group as this user that have not submitted the assignment
1554      *
1555      * @param int $groupid The id of the group whose members we want or 0 for the default group
1556      * @param bool $onlyids Whether to retrieve only the user id's
1557      * @return array The users (possibly id's only)
1558      */
1559     public function get_submission_group_members_who_have_not_submitted($groupid, $onlyids) {
1560         if (!$this->get_instance()->teamsubmission || !$this->get_instance()->requireallteammemberssubmit) {
1561             return array();
1562         }
1563         $members = $this->get_submission_group_members($groupid, $onlyids);
1565         foreach ($members as $id => $member) {
1566             $submission = $this->get_user_submission($member->id, false);
1567             if ($submission && $submission->status != ASSIGN_SUBMISSION_STATUS_DRAFT) {
1568                 unset($members[$id]);
1569             } else {
1570                 if ($this->is_blind_marking()) {
1571                     $members[$id]->alias = get_string('hiddenuser', 'assign') . $this->get_uniqueid_for_user($id);
1572                 }
1573             }
1574         }
1575         return $members;
1576     }
1578     /**
1579      * Load the group submission object for a particular user, optionally creating it if required
1580      *
1581      * This will create the user submission and the group submission if required
1582      *
1583      * @param int $userid The id of the user whose submission we want
1584      * @param int $groupid The id of the group for this user - may be 0 in which case it is determined from the userid
1585      * @param bool $create If set to true a new submission object will be created in the database
1586      * @return stdClass The submission
1587      */
1588     public function get_group_submission($userid, $groupid, $create) {
1589         global $DB;
1591         if ($groupid == 0) {
1592             $group = $this->get_submission_group($userid);
1593             if ($group) {
1594                 $groupid = $group->id;
1595             }
1596         }
1598         if ($create) {
1599             // Make sure there is a submission for this user.
1600             $params = array('assignment'=>$this->get_instance()->id, 'groupid'=>0, 'userid'=>$userid);
1601             $submission = $DB->get_record('assign_submission', $params);
1603             if (!$submission) {
1604                 $submission = new stdClass();
1605                 $submission->assignment   = $this->get_instance()->id;
1606                 $submission->userid       = $userid;
1607                 $submission->groupid      = 0;
1608                 $submission->timecreated  = time();
1609                 $submission->timemodified = $submission->timecreated;
1611                 if ($this->get_instance()->submissiondrafts) {
1612                     $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
1613                 } else {
1614                     $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1615                 }
1616                 $DB->insert_record('assign_submission', $submission);
1617             }
1618         }
1619         // Now get the group submission.
1620         $params = array('assignment'=>$this->get_instance()->id, 'groupid'=>$groupid, 'userid'=>0);
1621         $submission = $DB->get_record('assign_submission', $params);
1623         if ($submission) {
1624             return $submission;
1625         }
1626         if ($create) {
1627             $submission = new stdClass();
1628             $submission->assignment   = $this->get_instance()->id;
1629             $submission->userid       = 0;
1630             $submission->groupid       = $groupid;
1631             $submission->timecreated = time();
1632             $submission->timemodified = $submission->timecreated;
1634             if ($this->get_instance()->submissiondrafts) {
1635                 $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
1636             } else {
1637                 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1638             }
1639             $sid = $DB->insert_record('assign_submission', $submission);
1640             $submission->id = $sid;
1641             return $submission;
1642         }
1643         return false;
1644     }
1646     /**
1647      * View a page rendered by a plugin
1648      *
1649      * Uses url parameters 'pluginaction', 'pluginsubtype', 'plugin', and 'id'
1650      *
1651      * @return string
1652      */
1653     private function view_plugin_page() {
1654         global $USER;
1656         $o = '';
1658         $pluginsubtype = required_param('pluginsubtype', PARAM_ALPHA);
1659         $plugintype = required_param('plugin', PARAM_TEXT);
1660         $pluginaction = required_param('pluginaction', PARAM_ALPHA);
1662         $plugin = $this->get_plugin_by_type($pluginsubtype, $plugintype);
1663         if (!$plugin) {
1664             print_error('invalidformdata', '');
1665             return;
1666         }
1668         $o .= $plugin->view_page($pluginaction);
1670         return $o;
1671     }
1674     /**
1675      * This is used for team assignments to get the group for the specified user.
1676      * If the user is a member of multiple or no groups this will return false
1677      *
1678      * @param int $userid The id of the user whose submission we want
1679      * @return mixed The group or false
1680      */
1681     public function get_submission_group($userid) {
1682         $groups = groups_get_all_groups($this->get_course()->id, $userid, $this->get_instance()->teamsubmissiongroupingid);
1683         if (count($groups) != 1) {
1684             return false;
1685         }
1686         return array_pop($groups);
1687     }
1690     /**
1691      * display the submission that is used by a plugin
1692      * Uses url parameters 'sid', 'gid' and 'plugin'
1693      * @param string $pluginsubtype
1694      * @return string
1695      */
1696     private function view_plugin_content($pluginsubtype) {
1697         global $USER;
1699         $o = '';
1701         $submissionid = optional_param('sid', 0, PARAM_INT);
1702         $gradeid = optional_param('gid', 0, PARAM_INT);
1703         $plugintype = required_param('plugin', PARAM_TEXT);
1704         $item = null;
1705         if ($pluginsubtype == 'assignsubmission') {
1706             $plugin = $this->get_submission_plugin_by_type($plugintype);
1707             if ($submissionid <= 0) {
1708                 throw new coding_exception('Submission id should not be 0');
1709             }
1710             $item = $this->get_submission($submissionid);
1712             // permissions
1713             if ($item->userid != $USER->id) {
1714                 require_capability('mod/assign:grade', $this->context);
1715             }
1716             $o .= $this->output->render(new assign_header($this->get_instance(),
1717                                                               $this->get_context(),
1718                                                               $this->show_intro(),
1719                                                               $this->get_course_module()->id,
1720                                                               $plugin->get_name()));
1721             $o .= $this->output->render(new assign_submission_plugin_submission($plugin,
1722                                                               $item,
1723                                                               assign_submission_plugin_submission::FULL,
1724                                                               $this->get_course_module()->id,
1725                                                               $this->get_return_action(),
1726                                                               $this->get_return_params()));
1728             $this->add_to_log('view submission', get_string('viewsubmissionforuser', 'assign', $item->userid));
1729         } else {
1730             $plugin = $this->get_feedback_plugin_by_type($plugintype);
1731             if ($gradeid <= 0) {
1732                 throw new coding_exception('Grade id should not be 0');
1733             }
1734             $item = $this->get_grade($gradeid);
1735             // permissions
1736             if ($item->userid != $USER->id) {
1737                 require_capability('mod/assign:grade', $this->context);
1738             }
1739             $o .= $this->output->render(new assign_header($this->get_instance(),
1740                                                               $this->get_context(),
1741                                                               $this->show_intro(),
1742                                                               $this->get_course_module()->id,
1743                                                               $plugin->get_name()));
1744             $o .= $this->output->render(new assign_feedback_plugin_feedback($plugin,
1745                                                               $item,
1746                                                               assign_feedback_plugin_feedback::FULL,
1747                                                               $this->get_course_module()->id,
1748                                                               $this->get_return_action(),
1749                                                               $this->get_return_params()));
1750             $this->add_to_log('view feedback', get_string('viewfeedbackforuser', 'assign', $item->userid));
1751         }
1754         $o .= $this->view_return_links();
1756         $o .= $this->view_footer();
1757         return $o;
1758     }
1760     /**
1761      * render the content in editor that is often used by plugin
1762      *
1763      * @param string $filearea
1764      * @param int  $submissionid
1765      * @param string $plugintype
1766      * @param string $editor
1767      * @param string $component
1768      * @return string
1769      */
1770     public function render_editor_content($filearea, $submissionid, $plugintype, $editor, $component) {
1771         global $CFG;
1773         $result = '';
1775         $plugin = $this->get_submission_plugin_by_type($plugintype);
1777         $text = $plugin->get_editor_text($editor, $submissionid);
1778         $format = $plugin->get_editor_format($editor, $submissionid);
1780         $finaltext = file_rewrite_pluginfile_urls($text, 'pluginfile.php', $this->get_context()->id, $component, $filearea, $submissionid);
1781         $result .= format_text($finaltext, $format, array('overflowdiv' => true, 'context' => $this->get_context()));
1785         if ($CFG->enableportfolios) {
1786             require_once($CFG->libdir . '/portfoliolib.php');
1788             $button = new portfolio_add_button();
1789             $button->set_callback_options('assign_portfolio_caller', array('cmid' => $this->get_course_module()->id, 'sid' => $submissionid, 'plugin' => $plugintype, 'editor' => $editor, 'area'=>$filearea), '/mod/assign/portfolio_callback.php');
1790             $fs = get_file_storage();
1792             if ($files = $fs->get_area_files($this->context->id, $component,$filearea, $submissionid, "timemodified", false)) {
1793                 $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
1794             } else {
1795                 $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
1796             }
1797             $result .= $button->to_html();
1798         }
1799         return $result;
1800     }
1802     /**
1803      * Display a grading error
1804      *
1805      * @param string $message - The description of the result
1806      * @return string
1807      */
1808     private function view_quickgrading_result($message) {
1809         $o = '';
1810         $o .= $this->output->render(new assign_header($this->get_instance(),
1811                                                       $this->get_context(),
1812                                                       $this->show_intro(),
1813                                                       $this->get_course_module()->id,
1814                                                       get_string('quickgradingresult', 'assign')));
1815         $o .= $this->output->render(new assign_quickgrading_result($message, $this->get_course_module()->id));
1816         $o .= $this->view_footer();
1817         return $o;
1818     }
1820     /**
1821      * Display the page footer
1822      *
1823      * @return string
1824      */
1825     private function view_footer() {
1826         return $this->output->render_footer();
1827     }
1829     /**
1830      * Does this user have grade permission for this assignment
1831      *
1832      * @return bool
1833      */
1834     private function can_grade() {
1835         // Permissions check
1836         if (!has_capability('mod/assign:grade', $this->context)) {
1837             return false;
1838         }
1840         return true;
1841     }
1843     /**
1844      * Download a zip file of all assignment submissions
1845      *
1846      * @return void
1847      */
1848     private function download_submissions() {
1849         global $CFG,$DB;
1851         // More efficient to load this here.
1852         require_once($CFG->libdir.'/filelib.php');
1854         // Load all users with submit.
1855         $students = get_enrolled_users($this->context, "mod/assign:submit");
1857         // Build a list of files to zip.
1858         $filesforzipping = array();
1859         $fs = get_file_storage();
1861         $groupmode = groups_get_activity_groupmode($this->get_course_module());
1862         // All users.
1863         $groupid = 0;
1864         $groupname = '';
1865         if ($groupmode) {
1866             $groupid = groups_get_activity_group($this->get_course_module(), true);
1867             $groupname = groups_get_group_name($groupid).'-';
1868         }
1870         // Construct the zip file name.
1871         $filename = clean_filename($this->get_course()->shortname.'-'.
1872                                    $this->get_instance()->name.'-'.
1873                                    $groupname.$this->get_course_module()->id.".zip");
1875         // Get all the files for each student.
1876         foreach ($students as $student) {
1877             $userid = $student->id;
1879             if ((groups_is_member($groupid, $userid) or !$groupmode or !$groupid)) {
1880                 // Get the plugins to add their own files to the zip.
1882                 $submissiongroup = false;
1883                 $groupname = '';
1884                 if ($this->get_instance()->teamsubmission) {
1885                     $submission = $this->get_group_submission($userid, 0, false);
1886                     $submissiongroup = $this->get_submission_group($userid);
1887                     $groupname = '-' . $submissiongroup->name;
1888                 } else {
1889                     $submission = $this->get_user_submission($userid, false);
1890                 }
1892                 if ($this->is_blind_marking()) {
1893                     $prefix = clean_filename(str_replace('_', ' ', get_string('participant', 'assign') . $groupname) .
1894                                              "_" . $this->get_uniqueid_for_user($userid) . "_");
1895                 } else {
1896                     $prefix = clean_filename(str_replace('_', ' ', fullname($student) . $groupname) .
1897                                              "_" . $this->get_uniqueid_for_user($userid) . "_");
1898                 }
1900                 if ($submission) {
1901                     foreach ($this->submissionplugins as $plugin) {
1902                         if ($plugin->is_enabled() && $plugin->is_visible()) {
1903                             $pluginfiles = $plugin->get_files($submission);
1904                             foreach ($pluginfiles as $zipfilename => $file) {
1905                                 $subtype = $plugin->get_subtype();
1906                                 $type = $plugin->get_type();
1907                                 $prefixedfilename = $prefix . $subtype . '_' . $type . '_' . $zipfilename;
1908                                 $filesforzipping[$prefixedfilename] = $file;
1909                             }
1910                         }
1911                     }
1912                 }
1913             }
1914         }
1915         if ($zipfile = $this->pack_files($filesforzipping)) {
1916             $this->add_to_log('download all submissions', get_string('downloadall', 'assign'));
1917             // Send file and delete after sending.
1918             send_temp_file($zipfile, $filename);
1919         }
1920     }
1922     /**
1923      * Util function to add a message to the log
1924      *
1925      * @param string $action The current action
1926      * @param string $info A detailed description of the change. But no more than 255 characters.
1927      * @param string $url The url to the assign module instance.
1928      * @return void
1929      */
1930     public function add_to_log($action = '', $info = '', $url='') {
1931         global $USER;
1933         $fullurl = 'view.php?id=' . $this->get_course_module()->id;
1934         if ($url != '') {
1935             $fullurl .= '&' . $url;
1936         }
1938         add_to_log($this->get_course()->id, 'assign', $action, $fullurl, $info, $this->get_course_module()->id, $USER->id);
1939     }
1941     /**
1942      * Load the submission object for a particular user, optionally creating it if required
1943      *
1944      * For team assignments there are 2 submissions - the student submission and the team submission
1945      * All files are associated with the team submission but the status of the students contribution is
1946      * recorded separately.
1947      *
1948      * @param int $userid The id of the user whose submission we want or 0 in which case USER->id is used
1949      * @param bool $create optional Defaults to false. If set to true a new submission object will be created in the database
1950      * @return stdClass The submission
1951      */
1952     public function get_user_submission($userid, $create) {
1953         global $DB, $USER;
1955         if (!$userid) {
1956             $userid = $USER->id;
1957         }
1958         // If the userid is not null then use userid.
1959         $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid, 'groupid'=>0);
1960         $submission = $DB->get_record('assign_submission', $params);
1962         if ($submission) {
1963             return $submission;
1964         }
1965         if ($create) {
1966             $submission = new stdClass();
1967             $submission->assignment   = $this->get_instance()->id;
1968             $submission->userid       = $userid;
1969             $submission->timecreated = time();
1970             $submission->timemodified = $submission->timecreated;
1972             if ($this->get_instance()->submissiondrafts) {
1973                 $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
1974             } else {
1975                 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1976             }
1977             $sid = $DB->insert_record('assign_submission', $submission);
1978             $submission->id = $sid;
1979             return $submission;
1980         }
1981         return false;
1982     }
1984     /**
1985      * Load the submission object from it's id
1986      *
1987      * @param int $submissionid The id of the submission we want
1988      * @return stdClass The submission
1989      */
1990     private function get_submission($submissionid) {
1991         global $DB;
1993         return $DB->get_record('assign_submission', array('assignment'=>$this->get_instance()->id, 'id'=>$submissionid), '*', MUST_EXIST);
1994     }
1996     /**
1997      * This will retrieve a grade object from the db, optionally creating it if required
1998      *
1999      * @param int $userid The user we are grading
2000      * @param bool $create If true the grade will be created if it does not exist
2001      * @return stdClass The grade record
2002      */
2003     public function get_user_grade($userid, $create) {
2004         global $DB, $USER;
2006         if (!$userid) {
2007             $userid = $USER->id;
2008         }
2010         // if the userid is not null then use userid
2011         $grade = $DB->get_record('assign_grades', array('assignment'=>$this->get_instance()->id, 'userid'=>$userid));
2013         if ($grade) {
2014             return $grade;
2015         }
2016         if ($create) {
2017             $grade = new stdClass();
2018             $grade->assignment   = $this->get_instance()->id;
2019             $grade->userid       = $userid;
2020             $grade->timecreated = time();
2021             $grade->timemodified = $grade->timecreated;
2022             $grade->locked = 0;
2023             $grade->grade = -1;
2024             $grade->grader = $USER->id;
2025             $gid = $DB->insert_record('assign_grades', $grade);
2026             $grade->id = $gid;
2027             return $grade;
2028         }
2029         return false;
2030     }
2032     /**
2033      * This will retrieve a grade object from the db
2034      *
2035      * @param int $gradeid The id of the grade
2036      * @return stdClass The grade record
2037      */
2038     private function get_grade($gradeid) {
2039         global $DB;
2041         return $DB->get_record('assign_grades', array('assignment'=>$this->get_instance()->id, 'id'=>$gradeid), '*', MUST_EXIST);
2042     }
2044     /**
2045      * Print the grading page for a single user submission
2046      *
2047      * @param moodleform $mform
2048      * @param int $offset
2049      * @return string
2050      */
2051     private function view_single_grade_page($mform, $offset=0) {
2052         global $DB, $CFG;
2054         $o = '';
2056         // Include grade form
2057         require_once($CFG->dirroot . '/mod/assign/gradeform.php');
2059         // Need submit permission to submit an assignment
2060         require_capability('mod/assign:grade', $this->context);
2062         $o .= $this->output->render(new assign_header($this->get_instance(),
2063                                                       $this->get_context(), false, $this->get_course_module()->id,get_string('grading', 'assign')));
2065         $rownum = required_param('rownum', PARAM_INT) + $offset;
2066         $useridlist = optional_param('useridlist', '', PARAM_TEXT);
2067         if ($useridlist) {
2068             $useridlist = explode(',', $useridlist);
2069         } else {
2070             $useridlist = $this->get_grading_userid_list();
2071         }
2072         $last = false;
2073         $userid = $useridlist[$rownum];
2074         if ($rownum == count($useridlist) - 1) {
2075             $last = true;
2076         }
2077         // the placement of this is important so can pass the list of userids above
2078         if ($offset) {
2079             $_POST = array();
2080         }
2081         if (!$userid) {
2082             throw new coding_exception('Row is out of bounds for the current grading table: ' . $rownum);
2083         }
2084         $user = $DB->get_record('user', array('id' => $userid));
2085         if ($user) {
2086             $o .= $this->output->render(new assign_user_summary($user,
2087                                                                 $this->get_course()->id,
2088                                                                 has_capability('moodle/site:viewfullnames',
2089                                                                 $this->get_course_context()),
2090                                                                 $this->is_blind_marking(),
2091                                                                 $this->get_uniqueid_for_user($user->id)));
2092         }
2093         $submission = $this->get_user_submission($userid, false);
2094         $submissiongroup = null;
2095         $submissiongroupmemberswhohavenotsubmitted = array();
2096         $teamsubmission = null;
2097         $notsubmitted = array();
2098         if ($this->get_instance()->teamsubmission) {
2099             $teamsubmission = $this->get_group_submission($userid, 0, false);
2100             $submissiongroup = $this->get_submission_group($userid);
2101             $groupid = 0;
2102             if ($submissiongroup) {
2103                 $groupid = $submissiongroup->id;
2104             }
2105             $notsubmitted = $this->get_submission_group_members_who_have_not_submitted($groupid, false);
2107         }
2109         // get the current grade
2110         $grade = $this->get_user_grade($userid, false);
2111         if ($this->can_view_submission($userid)) {
2112             $gradelocked = ($grade && $grade->locked) || $this->grading_disabled($userid);
2113             $extensionduedate = null;
2114             if ($grade) {
2115                 $extensionduedate = $grade->extensionduedate;
2116             }
2117             $showedit = $this->submissions_open($userid) && ($this->is_any_submission_plugin_enabled());
2119             if ($teamsubmission) {
2120                 $showsubmit = $showedit && $teamsubmission && ($teamsubmission->status == ASSIGN_SUBMISSION_STATUS_DRAFT);
2121             } else {
2122                 $showsubmit = $showedit && $submission && ($submission->status == ASSIGN_SUBMISSION_STATUS_DRAFT);
2123             }
2124             $viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_course_context());
2126             $o .= $this->output->render(new assign_submission_status($this->get_instance()->allowsubmissionsfromdate,
2127                                                               $this->get_instance()->alwaysshowdescription,
2128                                                               $submission,
2129                                                               $this->get_instance()->teamsubmission,
2130                                                               $teamsubmission,
2131                                                               $submissiongroup,
2132                                                               $notsubmitted,
2133                                                               $this->is_any_submission_plugin_enabled(),
2134                                                               $gradelocked,
2135                                                               $this->is_graded($userid),
2136                                                               $this->get_instance()->duedate,
2137                                                               $this->get_instance()->cutoffdate,
2138                                                               $this->get_submission_plugins(),
2139                                                               $this->get_return_action(),
2140                                                               $this->get_return_params(),
2141                                                               $this->get_course_module()->id,
2142                                                               $this->get_course()->id,
2143                                                               assign_submission_status::GRADER_VIEW,
2144                                                               $showedit,
2145                                                               $showsubmit,
2146                                                               $viewfullnames,
2147                                                               $extensionduedate,
2148                                                               $this->get_context(),
2149                                                               $this->is_blind_marking()));
2150         }
2151         if ($grade) {
2152             $data = new stdClass();
2153             if ($grade->grade !== NULL && $grade->grade >= 0) {
2154                 $data->grade = format_float($grade->grade,2);
2155             }
2156         } else {
2157             $data = new stdClass();
2158             $data->grade = '';
2159         }
2161         // now show the grading form
2162         if (!$mform) {
2163             $pagination = array( 'rownum'=>$rownum, 'useridlist'=>$useridlist, 'last'=>$last);
2164             $formparams = array($this, $data, $pagination);
2165             $mform = new mod_assign_grade_form(null,
2166                                                $formparams,
2167                                                'post',
2168                                                '',
2169                                                array('class'=>'gradeform'));
2170         }
2171         $o .= $this->output->render(new assign_form('gradingform',$mform));
2173         $msg = get_string('viewgradingformforstudent', 'assign', array('id'=>$user->id, 'fullname'=>fullname($user)));
2174         $this->add_to_log('view grading form', $msg);
2176         $o .= $this->view_footer();
2177         return $o;
2178     }
2180     /**
2181      * Show a confirmation page to make sure they want to release student identities
2182      *
2183      * @return string
2184      */
2185     private function view_reveal_identities_confirm() {
2186         global $CFG, $USER;
2188         require_capability('mod/assign:revealidentities', $this->get_context());
2190         $o = '';
2191         $o .= $this->output->render(new assign_header($this->get_instance(),
2192                                                       $this->get_context(), false, $this->get_course_module()->id));
2194         $confirmurl = new moodle_url('/mod/assign/view.php', array('id'=>$this->get_course_module()->id,
2195                                                                     'action'=>'revealidentitiesconfirm',
2196                                                                     'sesskey'=>sesskey()));
2198         $cancelurl = new moodle_url('/mod/assign/view.php', array('id'=>$this->get_course_module()->id,
2199                                                                     'action'=>'grading'));
2201         $o .= $this->output->confirm(get_string('revealidentitiesconfirm', 'assign'), $confirmurl, $cancelurl);
2202         $o .= $this->view_footer();
2203         $this->add_to_log('view', get_string('viewrevealidentitiesconfirm', 'assign'));
2204         return $o;
2205     }
2210     /**
2211      * View a link to go back to the previous page. Uses url parameters returnaction and returnparams.
2212      *
2213      * @return string
2214      */
2215     private function view_return_links() {
2217         $returnaction = optional_param('returnaction','', PARAM_ALPHA);
2218         $returnparams = optional_param('returnparams','', PARAM_TEXT);
2220         $params = array();
2221         parse_str($returnparams, $params);
2222         $params = array_merge( array('id' => $this->get_course_module()->id, 'action' => $returnaction), $params);
2224         return $this->output->single_button(new moodle_url('/mod/assign/view.php', $params), get_string('back'), 'get');
2226     }
2228     /**
2229      * View the grading table of all submissions for this assignment
2230      *
2231      * @return string
2232      */
2233     private function view_grading_table() {
2234         global $USER, $CFG;
2235         // Include grading options form
2236         require_once($CFG->dirroot . '/mod/assign/gradingoptionsform.php');
2237         require_once($CFG->dirroot . '/mod/assign/quickgradingform.php');
2238         require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php');
2239         $o = '';
2241         $links = array();
2242         if (has_capability('gradereport/grader:view', $this->get_course_context()) &&
2243                 has_capability('moodle/grade:viewall', $this->get_course_context())) {
2244             $gradebookurl = '/grade/report/grader/index.php?id=' . $this->get_course()->id;
2245             $links[$gradebookurl] = get_string('viewgradebook', 'assign');
2246         }
2247         if ($this->is_any_submission_plugin_enabled()) {
2248             $downloadurl = '/mod/assign/view.php?id=' . $this->get_course_module()->id . '&action=downloadall';
2249             $links[$downloadurl] = get_string('downloadall', 'assign');
2250         }
2251         if ($this->is_blind_marking() && has_capability('mod/assign:revealidentities', $this->get_context())) {
2252             $revealidentitiesurl = '/mod/assign/view.php?id=' . $this->get_course_module()->id . '&action=revealidentities';
2253             $links[$revealidentitiesurl] = get_string('revealidentities', 'assign');
2254         }
2255         foreach ($this->get_feedback_plugins() as $plugin) {
2256             if ($plugin->is_enabled() && $plugin->is_visible()) {
2257                 foreach ($plugin->get_grading_actions() as $action => $description) {
2258                     $url = '/mod/assign/view.php' .
2259                            '?id=' .  $this->get_course_module()->id .
2260                            '&plugin=' . $plugin->get_type() .
2261                            '&pluginsubtype=assignfeedback' .
2262                            '&action=viewpluginpage&pluginaction=' . $action;
2263                     $links[$url] = $description;
2264                 }
2265             }
2266         }
2268         $gradingactions = new url_select($links);
2270         $gradingmanager = get_grading_manager($this->get_context(), 'mod_assign', 'submissions');
2272         $perpage = get_user_preferences('assign_perpage', 10);
2273         $filter = get_user_preferences('assign_filter', '');
2274         $controller = $gradingmanager->get_active_controller();
2275         $showquickgrading = empty($controller);
2276         if (optional_param('action', '', PARAM_ALPHA) == 'saveoptions') {
2277             $quickgrading = optional_param('quickgrading', false, PARAM_BOOL);
2278             set_user_preference('assign_quickgrading', $quickgrading);
2279         }
2280         $quickgrading = get_user_preferences('assign_quickgrading', false);
2282         // print options  for changing the filter and changing the number of results per page
2283         $gradingoptionsform = new mod_assign_grading_options_form(null,
2284                                                                   array('cm'=>$this->get_course_module()->id,
2285                                                                         'contextid'=>$this->context->id,
2286                                                                         'userid'=>$USER->id,
2287                                                                         'submissionsenabled'=>$this->is_any_submission_plugin_enabled(),
2288                                                                         'showquickgrading'=>$showquickgrading,
2289                                                                         'quickgrading'=>$quickgrading),
2290                                                                   'post', '',
2291                                                                   array('class'=>'gradingoptionsform'));
2293         $gradingbatchoperationsform = new mod_assign_grading_batch_operations_form(null,
2294                                                                   array('cm'=>$this->get_course_module()->id,
2295                                                                         'submissiondrafts'=>$this->get_instance()->submissiondrafts,
2296                                                                         'duedate'=>$this->get_instance()->duedate,
2297                                                                         'feedbackplugins'=>$this->get_feedback_plugins()),
2298                                                                   'post', '',
2299                                                                   array('class'=>'gradingbatchoperationsform'));
2301         $gradingoptionsdata = new stdClass();
2302         $gradingoptionsdata->perpage = $perpage;
2303         $gradingoptionsdata->filter = $filter;
2304         $gradingoptionsform->set_data($gradingoptionsdata);
2306         $actionformtext = $this->output->render($gradingactions);
2307         $o .= $this->output->render(new assign_header($this->get_instance(),
2308                                                       $this->get_context(), false, $this->get_course_module()->id, get_string('grading', 'assign'), $actionformtext));
2309         $o .= groups_print_activity_menu($this->get_course_module(), $CFG->wwwroot . '/mod/assign/view.php?id=' . $this->get_course_module()->id.'&action=grading', true);
2311         // plagiarism update status apearring in the grading book
2312         if (!empty($CFG->enableplagiarism)) {
2313             /** Include plagiarismlib.php */
2314             require_once($CFG->libdir . '/plagiarismlib.php');
2315             $o .= plagiarism_update_status($this->get_course(), $this->get_course_module());
2316         }
2318         // load and print the table of submissions
2319         if ($showquickgrading && $quickgrading) {
2320             $table = $this->output->render(new assign_grading_table($this, $perpage, $filter, 0, true));
2321             $quickgradingform = new mod_assign_quick_grading_form(null,
2322                                                                   array('cm'=>$this->get_course_module()->id,
2323                                                                         'gradingtable'=>$table));
2324             $o .= $this->output->render(new assign_form('quickgradingform', $quickgradingform));
2325         } else {
2326             $o .= $this->output->render(new assign_grading_table($this, $perpage, $filter, 0, false));
2327         }
2329         $currentgroup = groups_get_activity_group($this->get_course_module(), true);
2330         $users = array_keys($this->list_participants($currentgroup, true));
2331         if (count($users) != 0) {
2332             // if no enrolled user in a course then don't display the batch operations feature
2333             $o .= $this->output->render(new assign_form('gradingbatchoperationsform', $gradingbatchoperationsform));
2334         }
2335         $o .= $this->output->render(new assign_form('gradingoptionsform', $gradingoptionsform, 'M.mod_assign.init_grading_options'));
2336         return $o;
2337     }
2339     /**
2340      * View entire grading page.
2341      *
2342      * @return string
2343      */
2344     private function view_grading_page() {
2345         global $CFG;
2347         $o = '';
2348         // Need submit permission to submit an assignment
2349         require_capability('mod/assign:grade', $this->context);
2350         require_once($CFG->dirroot . '/mod/assign/gradeform.php');
2352         // only load this if it is
2354         $o .= $this->view_grading_table();
2356         $o .= $this->view_footer();
2357         $this->add_to_log('view submission grading table', get_string('viewsubmissiongradingtable', 'assign'));
2358         return $o;
2359     }
2361     /**
2362      * Capture the output of the plagiarism plugins disclosures and return it as a string
2363      *
2364      * @return void
2365      */
2366     private function plagiarism_print_disclosure() {
2367         global $CFG;
2368         $o = '';
2370         if (!empty($CFG->enableplagiarism)) {
2371             /** Include plagiarismlib.php */
2372             require_once($CFG->libdir . '/plagiarismlib.php');
2374             $o .= plagiarism_print_disclosure($this->get_course_module()->id);
2375         }
2377         return $o;
2378     }
2380     /**
2381      * message for students when assignment submissions have been closed
2382      *
2383      * @return string
2384      */
2385     private function view_student_error_message() {
2386         global $CFG;
2388         $o = '';
2389         // Need submit permission to submit an assignment
2390         require_capability('mod/assign:submit', $this->context);
2392         $o .= $this->output->render(new assign_header($this->get_instance(),
2393                                                       $this->get_context(),
2394                                                       $this->show_intro(),
2395                                                       $this->get_course_module()->id,
2396                                                       get_string('editsubmission', 'assign')));
2398         $o .= $this->output->notification(get_string('submissionsclosed', 'assign'));
2400         $o .= $this->view_footer();
2402         return $o;
2404     }
2406     /**
2407      * View edit submissions page.
2408      *
2409      * @param moodleform $mform
2410      * @return void
2411      */
2412     private function view_edit_submission_page($mform) {
2413         global $CFG;
2415         $o = '';
2416         // Include submission form
2417         require_once($CFG->dirroot . '/mod/assign/submission_form.php');
2418         // Need submit permission to submit an assignment
2419         require_capability('mod/assign:submit', $this->context);
2421         if (!$this->submissions_open()) {
2422             return $this->view_student_error_message();
2423         }
2424         $o .= $this->output->render(new assign_header($this->get_instance(),
2425                                                       $this->get_context(),
2426                                                       $this->show_intro(),
2427                                                       $this->get_course_module()->id,
2428                                                       get_string('editsubmission', 'assign')));
2429         $o .= $this->plagiarism_print_disclosure();
2430         $data = new stdClass();
2432         if (!$mform) {
2433             $mform = new mod_assign_submission_form(null, array($this, $data));
2434         }
2436         $o .= $this->output->render(new assign_form('editsubmissionform',$mform));
2438         $o .= $this->view_footer();
2439         $this->add_to_log('view submit assignment form', get_string('viewownsubmissionform', 'assign'));
2441         return $o;
2442     }
2444     /**
2445      * See if this assignment has a grade yet
2446      *
2447      * @param int $userid
2448      * @return bool
2449      */
2450     private function is_graded($userid) {
2451         $grade = $this->get_user_grade($userid, false);
2452         if ($grade) {
2453             return ($grade->grade !== NULL && $grade->grade >= 0);
2454         }
2455         return false;
2456     }
2459     /**
2460      * Perform an access check to see if the current $USER can view this users submission
2461      *
2462      * @param int $userid
2463      * @return bool
2464      */
2465     public function can_view_submission($userid) {
2466         global $USER;
2468         if (!is_enrolled($this->get_course_context(), $userid)) {
2469             return false;
2470         }
2471         if ($userid == $USER->id && !has_capability('mod/assign:submit', $this->context)) {
2472             return false;
2473         }
2474         if ($userid != $USER->id && !has_capability('mod/assign:grade', $this->context)) {
2475             return false;
2476         }
2477         return true;
2478     }
2480     /**
2481      * Allows the plugin to show a batch grading operation page.
2482      *
2483      * @return none
2484      */
2485     private function view_plugin_grading_batch_operation($mform) {
2486         require_capability('mod/assign:grade', $this->context);
2487         $prefix = 'plugingradingbatchoperation_';
2489         if ($data = $mform->get_data()) {
2490             $tail = substr($data->operation, strlen($prefix));
2491             list($plugintype, $action) = explode('_', $tail, 2);
2493             $plugin = $this->get_feedback_plugin_by_type($plugintype);
2494             if ($plugin) {
2495                 $users = $data->selectedusers;
2496                 $userlist = explode(',', $users);
2497                 echo $plugin->grading_batch_operation($action, $userlist);
2498                 return;
2499             }
2500         }
2501         print_error('invalidformdata', '');
2502     }
2504     /**
2505      * Ask the user to confirm they want to perform this batch operation
2506      * @param moodleform $mform Set to a grading batch operations form
2507      * @return string - the page to view after processing these actions
2508      */
2509     private function process_grading_batch_operation(& $mform) {
2510         global $CFG;
2511         require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php');
2512         require_sesskey();
2514         $mform = new mod_assign_grading_batch_operations_form(null,
2515                                                               array('cm'=>$this->get_course_module()->id,
2516                                                                     'submissiondrafts'=>$this->get_instance()->submissiondrafts,
2517                                                                     'duedate'=>$this->get_instance()->duedate,
2518                                                                     'feedbackplugins'=>$this->get_feedback_plugins()),
2519                                                               'post',
2520                                                               '',
2521                                                               array('class'=>'gradingbatchoperationsform'));
2523         if ($data = $mform->get_data()) {
2524             // get the list of users
2525             $users = $data->selectedusers;
2526             $userlist = explode(',', $users);
2528             $prefix = 'plugingradingbatchoperation_';
2530             if ($data->operation == 'grantextension') {
2531                 return 'grantextension';
2532             } else if (strpos($data->operation, $prefix) === 0) {
2533                 $tail = substr($data->operation, strlen($prefix));
2534                 list($plugintype, $action) = explode('_', $tail, 2);
2536                 $plugin = $this->get_feedback_plugin_by_type($plugintype);
2537                 if ($plugin) {
2538                     return 'plugingradingbatchoperation';
2539                 }
2540             }
2542             foreach ($userlist as $userid) {
2543                 if ($data->operation == 'lock') {
2544                     $this->process_lock($userid);
2545                 } else if ($data->operation == 'unlock') {
2546                     $this->process_unlock($userid);
2547                 } else if ($data->operation == 'reverttodraft') {
2548                     $this->process_revert_to_draft($userid);
2549                 }
2550             }
2551         }
2553         return 'grading';
2554     }
2556     /**
2557      * Ask the user to confirm they want to submit their work for grading
2558      * @param $mform moodleform - null unless form validation has failed
2559      * @return string
2560      */
2561     private function check_submit_for_grading($mform) {
2562         global $USER, $CFG;
2564         require_once($CFG->dirroot . '/mod/assign/submissionconfirmform.php');
2566         // Check that all of the submission plugins are ready for this submission
2567         $notifications = array();
2568         $submission = $this->get_user_submission($USER->id, false);
2569         $plugins = $this->get_submission_plugins();
2570         foreach ($plugins as $plugin) {
2571             if ($plugin->is_enabled() && $plugin->is_visible()) {
2572                 $check = $plugin->precheck_submission($submission);
2573                 if ($check !== true) {
2574                     $notifications[] = $check;
2575                 }
2576             }
2577         }
2579         $data = new stdClass();
2580         $adminconfig = $this->get_admin_config();
2581         $requiresubmissionstatement = !empty($adminconfig->requiresubmissionstatement) ||
2582                                           $this->get_instance()->requiresubmissionstatement;
2584         $submissionstatement = '';
2585         if (!empty($adminconfig->submissionstatement)) {
2586             $submissionstatement = $adminconfig->submissionstatement;
2587         }
2589         if ($mform == null) {
2590             $mform = new mod_assign_confirm_submission_form(null, array($requiresubmissionstatement,
2591                                                                         $submissionstatement,
2592                                                                         $this->get_course_module()->id,
2593                                                                         $data));
2594         }
2595         $o = '';
2596         $o .= $this->output->header();
2597         $o .= $this->output->render(new assign_submit_for_grading_page($notifications, $this->get_course_module()->id, $mform));
2598         $o .= $this->view_footer();
2600         $this->add_to_log('view confirm submit assignment form', get_string('viewownsubmissionform', 'assign'));
2602         return $o;
2603     }
2605     /**
2606      * Print 2 tables of information with no action links -
2607      * the submission summary and the grading summary
2608      *
2609      * @param stdClass $user the user to print the report for
2610      * @param bool $showlinks - Return plain text or links to the profile
2611      * @return string - the html summary
2612      */
2613     public function view_student_summary($user, $showlinks) {
2614         global $CFG, $DB, $PAGE;
2616         $grade = $this->get_user_grade($user->id, false);
2617         $submission = $this->get_user_submission($user->id, false);
2618         $o = '';
2620         $teamsubmission = null;
2621         $submissiongroup = null;
2622         $notsubmitted = array();
2623         if ($this->get_instance()->teamsubmission) {
2624             $teamsubmission = $this->get_group_submission($user->id, 0, false);
2625             $submissiongroup = $this->get_submission_group($user->id);
2626             $groupid = 0;
2627             if ($submissiongroup) {
2628                 $groupid = $submissiongroup->id;
2629             }
2630             $notsubmitted = $this->get_submission_group_members_who_have_not_submitted($groupid, false);
2631         }
2633         if ($this->can_view_submission($user->id)) {
2634             $showedit = has_capability('mod/assign:submit', $this->context) &&
2635                          $this->submissions_open($user->id) && ($this->is_any_submission_plugin_enabled()) && $showlinks;
2636             $gradelocked = ($grade && $grade->locked) || $this->grading_disabled($user->id);
2638             $showsubmit = ($submission || $teamsubmission) && $showlinks;
2639             if ($teamsubmission && ($teamsubmission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED)) {
2640                 $showsubmit = false;
2641             }
2642             if ($submission && ($submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED)) {
2643                 $showsubmit = false;
2644             }
2645             $extensionduedate = null;
2646             if ($grade) {
2647                 $extensionduedate = $grade->extensionduedate;
2648             }
2649             $viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_course_context());
2650             $o .= $this->output->render(new assign_submission_status($this->get_instance()->allowsubmissionsfromdate,
2651                                                               $this->get_instance()->alwaysshowdescription,
2652                                                               $submission,
2653                                                               $this->get_instance()->teamsubmission,
2654                                                               $teamsubmission,
2655                                                               $submissiongroup,
2656                                                               $notsubmitted,
2657                                                               $this->is_any_submission_plugin_enabled(),
2658                                                               $gradelocked,
2659                                                               $this->is_graded($user->id),
2660                                                               $this->get_instance()->duedate,
2661                                                               $this->get_instance()->cutoffdate,
2662                                                               $this->get_submission_plugins(),
2663                                                               $this->get_return_action(),
2664                                                               $this->get_return_params(),
2665                                                               $this->get_course_module()->id,
2666                                                               $this->get_course()->id,
2667                                                               assign_submission_status::STUDENT_VIEW,
2668                                                               $showedit,
2669                                                               $showsubmit,
2670                                                               $viewfullnames,
2671                                                               $extensionduedate,
2672                                                               $this->get_context(),
2673                                                               $this->is_blind_marking()));
2674             require_once($CFG->libdir.'/gradelib.php');
2675             require_once($CFG->dirroot.'/grade/grading/lib.php');
2677             $gradinginfo = grade_get_grades($this->get_course()->id,
2678                                         'mod',
2679                                         'assign',
2680                                         $this->get_instance()->id,
2681                                         $user->id);
2683             $gradingitem = $gradinginfo->items[0];
2684             $gradebookgrade = $gradingitem->grades[$user->id];
2686             // check to see if all feedback plugins are empty
2687             $emptyplugins = true;
2688             if ($grade) {
2689                 foreach ($this->get_feedback_plugins() as $plugin) {
2690                     if ($plugin->is_visible() && $plugin->is_enabled()) {
2691                         if (!$plugin->is_empty($grade)) {
2692                             $emptyplugins = false;
2693                         }
2694                     }
2695                 }
2696             }
2699             if (!($gradebookgrade->hidden) && ($gradebookgrade->grade !== null || !$emptyplugins)) {
2701                 $gradefordisplay = '';
2702                 $gradingmanager = get_grading_manager($this->get_context(), 'mod_assign', 'submissions');
2704                 if ($controller = $gradingmanager->get_active_controller()) {
2705                     $controller->set_grade_range(make_grades_menu($this->get_instance()->grade));
2706                     $gradefordisplay = $controller->render_grade($PAGE,
2707                                                                  $grade->id,
2708                                                                  $gradingitem,
2709                                                                  $gradebookgrade->str_long_grade,
2710                                                                  has_capability('mod/assign:grade', $this->get_context()));
2711                 } else {
2712                     $gradefordisplay = $this->display_grade($gradebookgrade->grade, false);
2713                 }
2715                 $gradeddate = $gradebookgrade->dategraded;
2716                 $grader = $DB->get_record('user', array('id'=>$gradebookgrade->usermodified));
2718                 $feedbackstatus = new assign_feedback_status($gradefordisplay,
2719                                                       $gradeddate,
2720                                                       $grader,
2721                                                       $this->get_feedback_plugins(),
2722                                                       $grade,
2723                                                       $this->get_course_module()->id,
2724                                                       $this->get_return_action(),
2725                                                       $this->get_return_params());
2727                 $o .= $this->output->render($feedbackstatus);
2728             }
2730         }
2731         return $o;
2732     }
2734     /**
2735      * View submissions page (contains details of current submission).
2736      *
2737      * @return string
2738      */
2739     private function view_submission_page() {
2740         global $CFG, $DB, $USER, $PAGE;
2742         $o = '';
2743         $o .= $this->output->render(new assign_header($this->get_instance(),
2744                                                       $this->get_context(),
2745                                                       $this->show_intro(),
2746                                                       $this->get_course_module()->id));
2748         if ($this->can_grade()) {
2749             if ($this->get_instance()->teamsubmission) {
2750                 $summary = new assign_grading_summary($this->count_teams(),
2751                                                       $this->get_instance()->submissiondrafts,
2752                                                       $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_DRAFT),
2753                                                       $this->is_any_submission_plugin_enabled(),
2754                                                       $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED),
2755                                                       $this->get_instance()->cutoffdate,
2756                                                       $this->get_instance()->duedate,
2757                                                       $this->get_course_module()->id,
2758                                                       $this->count_submissions_need_grading(),
2759                                                       $this->get_instance()->teamsubmission);
2760                 $o .= $this->output->render($summary);
2761             } else {
2762                 $summary = new assign_grading_summary($this->count_participants(0),
2763                                                       $this->get_instance()->submissiondrafts,
2764                                                       $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_DRAFT),
2765                                                       $this->is_any_submission_plugin_enabled(),
2766                                                       $this->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED),
2767                                                       $this->get_instance()->cutoffdate,
2768                                                       $this->get_instance()->duedate,
2769                                                       $this->get_course_module()->id,
2770                                                       $this->count_submissions_need_grading(),
2771                                                       $this->get_instance()->teamsubmission);
2772                 $o .= $this->output->render($summary);
2773             }
2774         }
2775         $grade = $this->get_user_grade($USER->id, false);
2776         $submission = $this->get_user_submission($USER->id, false);
2778         if ($this->can_view_submission($USER->id)) {
2779             $o .= $this->view_student_summary($USER, true);
2780         }
2783         $o .= $this->view_footer();
2784         $this->add_to_log('view', get_string('viewownsubmissionstatus', 'assign'));
2785         return $o;
2786     }
2788     /**
2789      * convert the final raw grade(s) in the  grading table for the gradebook
2790      *
2791      * @param stdClass $grade
2792      * @return array
2793      */
2794     private function convert_grade_for_gradebook(stdClass $grade) {
2795         $gradebookgrade = array();
2796         // trying to match those array keys in grade update function in gradelib.php
2797         // with keys in th database table assign_grades
2798         // starting around line 262
2799         if ($grade->grade >= 0) {
2800             $gradebookgrade['rawgrade'] = $grade->grade;
2801         }
2802         $gradebookgrade['userid'] = $grade->userid;
2803         $gradebookgrade['usermodified'] = $grade->grader;
2804         $gradebookgrade['datesubmitted'] = NULL;
2805         $gradebookgrade['dategraded'] = $grade->timemodified;
2806         if (isset($grade->feedbackformat)) {
2807             $gradebookgrade['feedbackformat'] = $grade->feedbackformat;
2808         }
2809         if (isset($grade->feedbacktext)) {
2810             $gradebookgrade['feedback'] = $grade->feedbacktext;
2811         }
2813         return $gradebookgrade;
2814     }
2816     /**
2817      * convert submission details for the gradebook
2818      *
2819      * @param stdClass $submission
2820      * @return array
2821      */
2822     private function convert_submission_for_gradebook(stdClass $submission) {
2823         $gradebookgrade = array();
2826         $gradebookgrade['userid'] = $submission->userid;
2827         $gradebookgrade['usermodified'] = $submission->userid;
2828         $gradebookgrade['datesubmitted'] = $submission->timemodified;
2830         return $gradebookgrade;
2831     }
2833     /**
2834      * update grades in the gradebook
2835      *
2836      * @param mixed $submission stdClass|null
2837      * @param mixed $grade stdClass|null
2838      * @return bool
2839      */
2840     private function gradebook_item_update($submission=NULL, $grade=NULL) {
2842         // Do not push grade to gradebook if blind marking is active as the gradebook would reveal the students.
2843         if ($this->is_blind_marking()) {
2844             return false;
2845         }
2846         if ($submission != NULL) {
2847             if ($submission->userid == 0) {
2848                 // This is a group submission update.
2849                 $team = groups_get_members($submission->groupid, 'u.id');
2851                 foreach ($team as $member) {
2852                     $submission->groupid = 0;
2853                     $submission->userid = $member->id;
2854                     $this->gradebook_item_update($submission, null);
2855                 }
2856                 return;
2857             }
2859             $gradebookgrade = $this->convert_submission_for_gradebook($submission);
2861         } else {
2862             $gradebookgrade = $this->convert_grade_for_gradebook($grade);
2863         }
2864         // Grading is disabled, return.
2865         if ($this->grading_disabled($gradebookgrade['userid'])) {
2866             return false;
2867         }
2868         $assign = clone $this->get_instance();
2869         $assign->cmidnumber = $this->get_course_module()->id;
2871         return assign_grade_item_update($assign, $gradebookgrade);
2872     }
2874     /**
2875      * update team submission
2876      *
2877      * @param stdClass $submission
2878      * @param int $userid
2879      * @param bool $updatetime
2880      * @return bool
2881      */
2882     private function update_team_submission(stdClass $submission, $userid, $updatetime) {
2883         global $DB;
2885         if ($updatetime) {
2886             $submission->timemodified = time();
2887         }
2889         // First update the submission for the current user.
2890         $mysubmission = $this->get_user_submission($userid, true);
2891         $mysubmission->status = $submission->status;
2893         $this->update_submission($mysubmission, 0, $updatetime, false);
2895         // Now check the team settings to see if this assignment qualifies as submitted or draft.
2896         $team = $this->get_submission_group_members($submission->groupid, true);
2898         $allsubmitted = true;
2899         $anysubmitted = false;
2900         foreach ($team as $member) {
2901             $membersubmission = $this->get_user_submission($member->id, false);
2903             if (!$membersubmission || $membersubmission->status != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
2904                 $allsubmitted = false;
2905                 if ($anysubmitted) {
2906                     break;
2907                 }
2908             } else {
2909                 $anysubmitted = true;
2910             }
2911         }
2912         if ($this->get_instance()->requireallteammemberssubmit) {
2913             if ($allsubmitted) {
2914                 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
2915             } else {
2916                 $submission->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
2917             }
2918             $result= $DB->update_record('assign_submission', $submission);
2919         } else {
2920             if ($anysubmitted) {
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         }
2928         $this->gradebook_item_update($submission);
2929         return $result;
2930     }
2933     /**
2934      * update grades in the gradebook based on submission time
2935      *
2936      * @param stdClass $submission
2937      * @param int $userid
2938      * @param bool $updatetime
2939      * @param bool $teamsubmission
2940      * @return bool
2941      */
2942     private function update_submission(stdClass $submission, $userid, $updatetime, $teamsubmission) {
2943         global $DB;
2945         if ($teamsubmission) {
2946             return $this->update_team_submission($submission, $userid, $updatetime);
2947         }
2949         if ($updatetime) {
2950             $submission->timemodified = time();
2951         }
2952         $result= $DB->update_record('assign_submission', $submission);
2953         if ($result) {
2954             $this->gradebook_item_update($submission);
2955         }
2956         return $result;
2957     }
2959     /**
2960      * Is this assignment open for submissions?
2961      *
2962      * Check the due date,
2963      * prevent late submissions,
2964      * has this person already submitted,
2965      * is the assignment locked?
2966      *
2967      * @param int $userid - Optional userid so we can see if a different user can submit
2968      * @return bool
2969      */
2970     private function submissions_open($userid = 0) {
2971         global $USER;
2973         if (!$userid) {
2974             $userid = $USER->id;
2975         }
2977         $time = time();
2978         $dateopen = true;
2979         $finaldate = false;
2980         if ($this->get_instance()->cutoffdate) {
2981             $finaldate = $this->get_instance()->cutoffdate;
2982         }
2983         // User extensions.
2984         if ($finaldate) {
2985             $grade = $this->get_user_grade($userid, false);
2986             if ($grade && $grade->extensionduedate) {
2987                 // Extension can be before cut off date.
2988                 if ($grade->extensionduedate > $finaldate) {
2989                     $finaldate = $grade->extensionduedate;
2990                 }
2991             }
2992         }
2994         if ($finaldate) {
2995             $dateopen = ($this->get_instance()->allowsubmissionsfromdate <= $time && $time <= $finaldate);
2996         } else {
2997             $dateopen = ($this->get_instance()->allowsubmissionsfromdate <= $time);
2998         }
3000         if (!$dateopen) {
3001             return false;
3002         }
3004         // Now check if this user has already submitted etc.
3005         if (!is_enrolled($this->get_course_context(), $userid)) {
3006             return false;
3007         }
3008         $submission = false;
3009         if ($this->get_instance()->teamsubmission) {
3010             $submission = $this->get_group_submission($USER->id, 0, false);
3011         } else {
3012             $submission = $this->get_user_submission($USER->id, false);
3013         }
3014         if ($submission) {
3016             if ($this->get_instance()->submissiondrafts && $submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
3017                 // drafts are tracked and the student has submitted the assignment
3018                 return false;
3019             }
3020         }
3021         if ($grade = $this->get_user_grade($userid, false)) {
3022             if ($grade->locked) {
3023                 return false;
3024             }
3025         }
3027         if ($this->grading_disabled($userid)) {
3028             return false;
3029         }
3031         return true;
3032     }
3034     /**
3035      * render the files in file area
3036      * @param string $component
3037      * @param string $area
3038      * @param int $submissionid
3039      * @return string
3040      */
3041     public function render_area_files($component, $area, $submissionid) {
3042         global $USER;
3044         $fs = get_file_storage();
3045         $browser = get_file_browser();
3046         $files = $fs->get_area_files($this->get_context()->id, $component, $area , $submissionid , "timemodified", false);
3047         return $this->output->assign_files($this->context, $submissionid, $area, $component);
3049     }
3051     /**
3052      * Returns a list of teachers that should be grading given submission
3053      *
3054      * @param int $userid
3055      * @return array
3056      */
3057     private function get_graders($userid) {
3058         //potential graders
3059         $potentialgraders = get_enrolled_users($this->context, "mod/assign:grade");
3061         $graders = array();
3062         if (groups_get_activity_groupmode($this->get_course_module()) == SEPARATEGROUPS) {   // Separate groups are being used
3063             if ($groups = groups_get_all_groups($this->get_course()->id, $userid)) {  // Try to find all groups
3064                 foreach ($groups as $group) {
3065                     foreach ($potentialgraders as $grader) {
3066                         if ($grader->id == $userid) {
3067                             continue; // do not send self
3068                         }
3069                         if (groups_is_member($group->id, $grader->id)) {
3070                             $graders[$grader->id] = $grader;
3071                         }
3072                     }
3073                 }
3074             } else {
3075                 // user not in group, try to find graders without group
3076                 foreach ($potentialgraders as $grader) {
3077                     if ($grader->id == $userid) {
3078                         continue; // do not send self
3079                     }
3080                     if (!groups_has_membership($this->get_course_module(), $grader->id)) {
3081                         $graders[$grader->id] = $grader;
3082                     }
3083                 }
3084             }
3085         } else {
3086             foreach ($potentialgraders as $grader) {
3087                 if ($grader->id == $userid) {
3088                     continue; // do not send self
3089                 }
3090                 // must be enrolled
3091                 if (is_enrolled($this->get_course_context(), $grader->id)) {
3092                     $graders[$grader->id] = $grader;
3093                 }
3094             }
3095         }
3096         return $graders;
3097     }
3099     /**
3100      * Format a notification for plain text
3101      *
3102      * @param string $messagetype
3103      * @param stdClass $info
3104      * @param stdClass $course
3105      * @param stdClass $context
3106      * @param string $modulename
3107      * @param string $assignmentname
3108      */
3109     private static function format_notification_message_text($messagetype, $info, $course, $context, $modulename, $assignmentname) {
3110         $posttext  = format_string($course->shortname, true, array('context' => $context->get_course_context())).' -> '.
3111                      $modulename.' -> '.
3112                      format_string($assignmentname, true, array('context' => $context))."\n";
3113         $posttext .= '---------------------------------------------------------------------'."\n";
3114         $posttext .= get_string($messagetype . 'text', "assign", $info)."\n";
3115         $posttext .= "\n---------------------------------------------------------------------\n";
3116         return $posttext;
3117     }
3119     /**
3120      * Format a notification for HTML
3121      *
3122      * @param string $messagetype
3123      * @param stdClass $info
3124      * @param stdClass $course
3125      * @param stdClass $context
3126      * @param string $modulename
3127      * @param stdClass $coursemodule
3128      * @param string $assignmentname
3129      * @param stdClass $info
3130      */
3131     private static function format_notification_message_html($messagetype, $info, $course, $context, $modulename, $coursemodule, $assignmentname) {
3132         global $CFG;
3133         $posthtml  = '<p><font face="sans-serif">'.
3134                      '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.format_string($course->shortname, true, array('context' => $context->get_course_context())).'</a> ->'.
3135                      '<a href="'.$CFG->wwwroot.'/mod/assign/index.php?id='.$course->id.'">'.$modulename.'</a> ->'.
3136                      '<a href="'.$CFG->wwwroot.'/mod/assign/view.php?id='.$coursemodule->id.'">'.format_string($assignmentname, true, array('context' => $context)).'</a></font></p>';
3137         $posthtml .= '<hr /><font face="sans-serif">';
3138         $posthtml .= '<p>'.get_string($messagetype . 'html', 'assign', $info).'</p>';
3139         $posthtml .= '</font><hr />';
3140         return $posthtml;
3141     }
3143     /**
3144      * Message someone about something (static so it can be called from cron)
3145      *
3146      * @param stdClass $userfrom
3147      * @param stdClass $userto
3148      * @param string $messagetype
3149      * @param string $eventtype
3150      * @param int $updatetime
3151      * @param stdClass $coursemodule
3152      * @param stdClass $context
3153      * @param stdClass $course
3154      * @param string $modulename
3155      * @param string $assignmentname
3156      * @return void
3157      */
3158     public static function send_assignment_notification($userfrom, $userto, $messagetype, $eventtype,
3159                                                         $updatetime, $coursemodule, $context, $course,
3160                                                         $modulename, $assignmentname, $blindmarking,
3161                                                         $uniqueidforuser) {
3162         global $CFG;
3164         $info = new stdClass();
3165         if ($blindmarking) {
3166             $info->username = get_string('participant', 'assign') . ' ' . $uniqueidforuser;
3167         } else {
3168             $info->username = fullname($userfrom, true);
3169         }
3170         $info->assignment = format_string($assignmentname,true, array('context'=>$context));
3171         $info->url = $CFG->wwwroot.'/mod/assign/view.php?id='.$coursemodule->id;
3172         $info->timeupdated = strftime('%c',$updatetime);
3174         $postsubject = get_string($messagetype . 'small', 'assign', $info);
3175         $posttext = self::format_notification_message_text($messagetype, $info, $course, $context, $modulename, $assignmentname);
3176         $posthtml = ($userto->mailformat == 1) ? self::format_notification_message_html($messagetype, $info, $course, $context, $modulename, $coursemodule, $assignmentname) : '';
3178         $eventdata = new stdClass();
3179         $eventdata->modulename       = 'assign';
3180         $eventdata->userfrom         = $userfrom;
3181         $eventdata->userto           = $userto;
3182         $eventdata->subject          = $postsubject;
3183         $eventdata->fullmessage      = $posttext;
3184         $eventdata->fullmessageformat = FORMAT_PLAIN;
3185         $eventdata->fullmessagehtml  = $posthtml;
3186         $eventdata->smallmessage     = $postsubject;
3188         $eventdata->name            = $eventtype;
3189         $eventdata->component       = 'mod_assign';
3190         $eventdata->notification    = 1;
3191         $eventdata->contexturl      = $info->url;
3192         $eventdata->contexturlname  = $info->assignment;
3194         message_send($eventdata);
3195     }
3197     /**
3198      * Message someone about something
3199      *
3200      * @param stdClass $userfrom
3201      * @param stdClass $userto
3202      * @param string $messagetype
3203      * @param string $eventtype
3204      * @param int $updatetime
3205      * @return void
3206      */
3207     public function send_notification($userfrom, $userto, $messagetype, $eventtype, $updatetime) {
3208         self::send_assignment_notification($userfrom, $userto, $messagetype, $eventtype,
3209                                            $updatetime, $this->get_course_module(), $this->get_context(),
3210                                            $this->get_course(), $this->get_module_name(),
3211                                            $this->get_instance()->name, $this->is_blind_marking(),
3212                                            $this->get_uniqueid_for_user($userfrom->id));
3213     }
3215     /**
3216      * Notify student upon successful submission
3217      *
3218      * @param stdClass $submission
3219      * @return void
3220      */
3221     private function notify_student_submission_receipt(stdClass $submission) {
3222         global $DB, $USER;
3224         $adminconfig = $this->get_admin_config();
3225         if (empty($adminconfig->submissionreceipts)) {
3226             // No need to do anything
3227             return;
3228         }
3229         if ($submission->userid) {
3230             $user = $DB->get_record('user', array('id'=>$submission->userid), '*', MUST_EXIST);
3231         } else {
3232             $user = $USER;
3233         }
3234         $this->send_notification($user, $user, 'submissionreceipt', 'assign_notification', $submission->timemodified);
3235     }
3237     /**
3238      * Send notifications to graders upon student submissions
3239      *
3240      * @param stdClass $submission
3241      * @return void
3242      */
3243     private function notify_graders(stdClass $submission) {
3244         global $DB, $USER;
3246         $late = $this->get_instance()->duedate && ($this->get_instance()->duedate < time());
3248         if (!$this->get_instance()->sendnotifications && !($late && $this->get_instance()->sendlatenotifications)) {          // No need to do anything
3249             return;
3250         }
3252         if ($submission->userid) {
3253             $user = $DB->get_record('user', array('id'=>$submission->userid), '*', MUST_EXIST);
3254         } else {
3255             $user = $USER;
3256         }
3257         if ($teachers = $this->get_graders($user->id)) {
3258             foreach ($teachers as $teacher) {
3259                 $this->send_notification($user, $teacher, 'gradersubmissionupdated', 'assign_notification', $submission->timemodified);
3260             }
3261         }
3262     }
3264     /**
3265      * assignment submission is processed before grading
3266      *
3267      * @param $mform If validation failed when submitting this form - this is the moodleform - it can be null
3268      * @return bool Return false if the validation fails. This affects which page is displayed next.
3269      */
3270     private function process_submit_for_grading($mform) {
3271         global $USER, $CFG;
3273         // Need submit permission to submit an assignment
3274         require_capability('mod/assign:submit', $this->context);
3275         require_once($CFG->dirroot . '/mod/assign/submissionconfirmform.php');
3276         require_sesskey();
3278         $data = new stdClass();
3279         $adminconfig = $this->get_admin_config();
3280         $requiresubmissionstatement = !empty($adminconfig->requiresubmissionstatement) ||