MDL-53077 assign: Set specific pagetype for pages with actions.
[moodle.git] / mod / assign / locallib.php
CommitLineData
bbd0e548
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file contains the definition for the class assignment
19 *
20 * This class provides all the functionality for the new assign module.
21 *
22 * @package mod_assign
23 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
e5403f8c 29// Assignment submission statuses.
9e3eee67 30define('ASSIGN_SUBMISSION_STATUS_NEW', 'new');
df211804 31define('ASSIGN_SUBMISSION_STATUS_REOPENED', 'reopened');
e5403f8c
DW
32define('ASSIGN_SUBMISSION_STATUS_DRAFT', 'draft');
33define('ASSIGN_SUBMISSION_STATUS_SUBMITTED', 'submitted');
bbd0e548 34
e5403f8c 35// Search filters for grading page.
bbd0e548 36define('ASSIGN_FILTER_SUBMITTED', 'submitted');
c80d59f2 37define('ASSIGN_FILTER_NOT_SUBMITTED', 'notsubmitted');
bbd0e548
DW
38define('ASSIGN_FILTER_SINGLE_USER', 'singleuser');
39define('ASSIGN_FILTER_REQUIRE_GRADING', 'require_grading');
40
c80d59f2
JF
41// Marker filter for grading page.
42define('ASSIGN_MARKER_FILTER_NO_MARKER', -1);
43
df211804
DW
44// Reopen attempt methods.
45define('ASSIGN_ATTEMPT_REOPEN_METHOD_NONE', 'none');
46define('ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL', 'manual');
47define('ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS', 'untilpass');
48
49// Special value means allow unlimited attempts.
50define('ASSIGN_UNLIMITED_ATTEMPTS', -1);
51
bd3ee807
MN
52// Grading states.
53define('ASSIGN_GRADING_STATUS_GRADED', 'graded');
54define('ASSIGN_GRADING_STATUS_NOT_GRADED', 'notgraded');
55
f8d107b3
DM
56// Marking workflow states.
57define('ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED', 'notmarked');
58define('ASSIGN_MARKING_WORKFLOW_STATE_INMARKING', 'inmarking');
59define('ASSIGN_MARKING_WORKFLOW_STATE_READYFORREVIEW', 'readyforreview');
60define('ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW', 'inreview');
61define('ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE', 'readyforrelease');
62define('ASSIGN_MARKING_WORKFLOW_STATE_RELEASED', 'released');
63
7faf78cb
HB
64// Name of file area for intro attachments.
65define('ASSIGN_INTROATTACHMENT_FILEAREA', 'introattachment');
66
e5403f8c
DW
67require_once($CFG->libdir . '/accesslib.php');
68require_once($CFG->libdir . '/formslib.php');
bbd0e548 69require_once($CFG->dirroot . '/repository/lib.php');
e5403f8c
DW
70require_once($CFG->dirroot . '/mod/assign/mod_form.php');
71require_once($CFG->libdir . '/gradelib.php');
72require_once($CFG->dirroot . '/grade/grading/lib.php');
73require_once($CFG->dirroot . '/mod/assign/feedbackplugin.php');
74require_once($CFG->dirroot . '/mod/assign/submissionplugin.php');
75require_once($CFG->dirroot . '/mod/assign/renderable.php');
76require_once($CFG->dirroot . '/mod/assign/gradingtable.php');
77require_once($CFG->libdir . '/eventslib.php');
37743241 78require_once($CFG->libdir . '/portfolio/caller.php');
bbd0e548
DW
79
80/**
81 * Standard base class for mod_assign (assignment types).
82 *
83 * @package mod_assign
84 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
85 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
86 */
87class assign {
88
bbd0e548
DW
89 /** @var stdClass the assignment record that contains the global settings for this assign instance */
90 private $instance;
91
0d64c75f
PN
92 /** @var stdClass the grade_item record for this assign instance's primary grade item. */
93 private $gradeitem;
94
e5403f8c
DW
95 /** @var context the context of the course module for this assign instance
96 * (or just the course if we are creating a new one)
97 */
bbd0e548
DW
98 private $context;
99
100 /** @var stdClass the course this assign instance belongs to */
101 private $course;
bc5a657b 102
cfc81f03
DW
103 /** @var stdClass the admin config for all assign instances */
104 private $adminconfig;
105
bbd0e548
DW
106 /** @var assign_renderer the custom renderer for this module */
107 private $output;
108
c13ac85d 109 /** @var cm_info the course module for this assign instance */
bbd0e548
DW
110 private $coursemodule;
111
e5403f8c
DW
112 /** @var array cache for things like the coursemodule name or the scale menu -
113 * only lives for a single request.
114 */
bbd0e548
DW
115 private $cache;
116
117 /** @var array list of the installed submission plugins */
118 private $submissionplugins;
119
120 /** @var array list of the installed feedback plugins */
121 private $feedbackplugins;
122
e5403f8c
DW
123 /** @var string action to be used to return to this page
124 * (without repeating any form submissions etc).
125 */
bbd0e548
DW
126 private $returnaction = 'view';
127
128 /** @var array params to be used to return to this page */
129 private $returnparams = array();
130
131 /** @var string modulename prevents excessive calls to get_string */
f5b32abe 132 private static $modulename = null;
bbd0e548
DW
133
134 /** @var string modulenameplural prevents excessive calls to get_string */
f5b32abe 135 private static $modulenameplural = null;
bbd0e548 136
f8d107b3
DM
137 /** @var array of marking workflow states for the current user */
138 private $markingworkflowstates = null;
139
4c4c7b3f 140 /** @var bool whether to exclude users with inactive enrolment */
1ecb8044 141 private $showonlyactiveenrol = null;
4c4c7b3f 142
1a51661b 143 /** @var string A key used to identify userlists created by this object. */
3fca6937
NM
144 private $useridlistid = null;
145
c46db93c
DW
146 /** @var array cached list of participants for this assignment. The cache key will be group, showactive and the context id */
147 private $participants = array();
148
bc006a66
DM
149 /** @var array cached list of user groups when team submissions are enabled. The cache key will be the user. */
150 private $usersubmissiongroups = array();
151
ee5d4eef
AH
152 /** @var array cached list of user groups. The cache key will be the user. */
153 private $usergroups = array();
154
6d6e297b
DM
155 /** @var array cached list of IDs of users who share group membership with the user. The cache key will be the user. */
156 private $sharedgroupmembers = array();
157
bbd0e548 158 /**
e5403f8c 159 * Constructor for the base assign class.
bbd0e548 160 *
c13ac85d 161 * Note: For $coursemodule you can supply a stdclass if you like, but it
162 * will be more efficient to supply a cm_info object.
163 *
e5403f8c
DW
164 * @param mixed $coursemodulecontext context|null the course module context
165 * (or the course context if the coursemodule has not been
166 * created yet).
167 * @param mixed $coursemodule the current course module if it was already loaded,
168 * otherwise this class will load one from the context as required.
169 * @param mixed $course the current course if it was already loaded,
170 * otherwise this class will load one from the context as required.
bbd0e548
DW
171 */
172 public function __construct($coursemodulecontext, $coursemodule, $course) {
1a51661b
DM
173 global $SESSION;
174
bbd0e548 175 $this->context = $coursemodulecontext;
bbd0e548 176 $this->course = $course;
e5403f8c 177
c13ac85d 178 // Ensure that $this->coursemodule is a cm_info object (or null).
179 $this->coursemodule = cm_info::create($coursemodule);
180
e5403f8c
DW
181 // Temporary cache only lives for a single request - used to reduce db lookups.
182 $this->cache = array();
bbd0e548
DW
183
184 $this->submissionplugins = $this->load_plugins('assignsubmission');
185 $this->feedbackplugins = $this->load_plugins('assignfeedback');
3fca6937
NM
186
187 // Extra entropy is required for uniqid() to work on cygwin.
188 $this->useridlistid = clean_param(uniqid('', true), PARAM_ALPHANUM);
1a51661b
DM
189
190 if (!isset($SESSION->mod_assign_useridlist)) {
191 $SESSION->mod_assign_useridlist = [];
192 }
bbd0e548
DW
193 }
194
195 /**
e5403f8c 196 * Set the action and parameters that can be used to return to the current page.
bbd0e548
DW
197 *
198 * @param string $action The action for the current page
e5403f8c
DW
199 * @param array $params An array of name value pairs which form the parameters
200 * to return to the current page.
bbd0e548
DW
201 * @return void
202 */
203 public function register_return_link($action, $params) {
d04557b3
DW
204 global $PAGE;
205 $params['action'] = $action;
206 $currenturl = $PAGE->url;
207
208 $currenturl->params($params);
209 $PAGE->set_url($currenturl);
bbd0e548
DW
210 }
211
212 /**
e5403f8c
DW
213 * Return an action that can be used to get back to the current page.
214 *
bbd0e548
DW
215 * @return string action
216 */
217 public function get_return_action() {
d04557b3
DW
218 global $PAGE;
219
220 $params = $PAGE->url->params();
221
c2114099
DW
222 if (!empty($params['action'])) {
223 return $params['action'];
224 }
225 return '';
bbd0e548
DW
226 }
227
228 /**
e5403f8c
DW
229 * Based on the current assignment settings should we display the intro.
230 *
bbd0e548
DW
231 * @return bool showintro
232 */
7faf78cb 233 public function show_intro() {
bbd0e548
DW
234 if ($this->get_instance()->alwaysshowdescription ||
235 time() > $this->get_instance()->allowsubmissionsfromdate) {
236 return true;
237 }
238 return false;
239 }
240
241 /**
e5403f8c
DW
242 * Return a list of parameters that can be used to get back to the current page.
243 *
bbd0e548
DW
244 * @return array params
245 */
246 public function get_return_params() {
d04557b3
DW
247 global $PAGE;
248
c2114099
DW
249 $params = $PAGE->url->params();
250 unset($params['id']);
251 unset($params['action']);
252 return $params;
bbd0e548
DW
253 }
254
255 /**
e5403f8c
DW
256 * Set the submitted form data.
257 *
bbd0e548
DW
258 * @param stdClass $data The form data (instance)
259 */
260 public function set_instance(stdClass $data) {
261 $this->instance = $data;
262 }
263
264 /**
e5403f8c
DW
265 * Set the context.
266 *
bbd0e548
DW
267 * @param context $context The new context
268 */
269 public function set_context(context $context) {
270 $this->context = $context;
271 }
272
273 /**
e5403f8c
DW
274 * Set the course data.
275 *
bbd0e548
DW
276 * @param stdClass $course The course data
277 */
278 public function set_course(stdClass $course) {
279 $this->course = $course;
280 }
281
282 /**
e5403f8c
DW
283 * Get list of feedback plugins installed.
284 *
bbd0e548
DW
285 * @return array
286 */
287 public function get_feedback_plugins() {
288 return $this->feedbackplugins;
289 }
290
291 /**
e5403f8c
DW
292 * Get list of submission plugins installed.
293 *
bbd0e548
DW
294 * @return array
295 */
296 public function get_submission_plugins() {
297 return $this->submissionplugins;
298 }
299
b473171a
DW
300 /**
301 * Is blind marking enabled and reveal identities not set yet?
302 *
303 * @return bool
304 */
305 public function is_blind_marking() {
306 return $this->get_instance()->blindmarking && !$this->get_instance()->revealidentities;
307 }
308
309 /**
310 * Does an assignment have submission(s) or grade(s) already?
311 *
312 * @return bool
313 */
314 public function has_submissions_or_grades() {
315 $allgrades = $this->count_grades();
316 $allsubmissions = $this->count_submissions();
317 if (($allgrades == 0) && ($allsubmissions == 0)) {
318 return false;
319 }
320 return true;
321 }
bbd0e548
DW
322
323 /**
e5403f8c
DW
324 * Get a specific submission plugin by its type.
325 *
bbd0e548
DW
326 * @param string $subtype assignsubmission | assignfeedback
327 * @param string $type
328 * @return mixed assign_plugin|null
329 */
df47b77f 330 public function get_plugin_by_type($subtype, $type) {
bbd0e548
DW
331 $shortsubtype = substr($subtype, strlen('assign'));
332 $name = $shortsubtype . 'plugins';
d0d4796b
DW
333 if ($name != 'feedbackplugins' && $name != 'submissionplugins') {
334 return null;
335 }
bbd0e548
DW
336 $pluginlist = $this->$name;
337 foreach ($pluginlist as $plugin) {
338 if ($plugin->get_type() == $type) {
339 return $plugin;
340 }
341 }
342 return null;
343 }
344
345 /**
e5403f8c
DW
346 * Get a feedback plugin by type.
347 *
bbd0e548
DW
348 * @param string $type - The type of plugin e.g comments
349 * @return mixed assign_feedback_plugin|null
350 */
351 public function get_feedback_plugin_by_type($type) {
352 return $this->get_plugin_by_type('assignfeedback', $type);
353 }
354
355 /**
e5403f8c
DW
356 * Get a submission plugin by type.
357 *
bbd0e548
DW
358 * @param string $type - The type of plugin e.g comments
359 * @return mixed assign_submission_plugin|null
360 */
361 public function get_submission_plugin_by_type($type) {
362 return $this->get_plugin_by_type('assignsubmission', $type);
363 }
364
365 /**
e5403f8c
DW
366 * Load the plugins from the sub folders under subtype.
367 *
bbd0e548
DW
368 * @param string $subtype - either submission or feedback
369 * @return array - The sorted list of plugins
370 */
47f48152 371 protected function load_plugins($subtype) {
bbd0e548
DW
372 global $CFG;
373 $result = array();
374
bd3b3bba 375 $names = core_component::get_plugin_list($subtype);
bbd0e548
DW
376
377 foreach ($names as $name => $path) {
378 if (file_exists($path . '/locallib.php')) {
379 require_once($path . '/locallib.php');
380
381 $shortsubtype = substr($subtype, strlen('assign'));
382 $pluginclass = 'assign_' . $shortsubtype . '_' . $name;
383
384 $plugin = new $pluginclass($this, $name);
385
386 if ($plugin instanceof assign_plugin) {
387 $idx = $plugin->get_sort_order();
e5403f8c
DW
388 while (array_key_exists($idx, $result)) {
389 $idx +=1;
390 }
bbd0e548
DW
391 $result[$idx] = $plugin;
392 }
393 }
394 }
395 ksort($result);
396 return $result;
397 }
398
bbd0e548
DW
399 /**
400 * Display the assignment, used by view.php
401 *
402 * The assignment is displayed differently depending on your role,
403 * the settings for the assignment and the status of the assignment.
e5403f8c 404 *
bbd0e548 405 * @param string $action The current action if any.
df211804 406 * @return string - The page output.
bbd0e548
DW
407 */
408 public function view($action='') {
e61036f4 409 global $PAGE;
bbd0e548
DW
410
411 $o = '';
412 $mform = null;
34b8f3a8 413 $notices = array();
34e338a4 414 $nextpageparams = array();
bbd0e548 415
34e338a4
MN
416 if (!empty($this->get_course_module()->id)) {
417 $nextpageparams['id'] = $this->get_course_module()->id;
418 }
d04557b3 419
34b8f3a8 420 // Handle form submissions first.
bbd0e548
DW
421 if ($action == 'savesubmission') {
422 $action = 'editsubmission';
34b8f3a8 423 if ($this->process_save_submission($mform, $notices)) {
d04557b3
DW
424 $action = 'redirect';
425 $nextpageparams['action'] = 'view';
bbd0e548 426 }
df211804
DW
427 } else if ($action == 'editprevioussubmission') {
428 $action = 'editsubmission';
429 if ($this->process_copy_previous_attempt($notices)) {
430 $action = 'redirect';
431 $nextpageparams['action'] = 'editsubmission';
432 }
9e795179 433 } else if ($action == 'lock') {
05a6445a 434 $this->process_lock_submission();
d04557b3
DW
435 $action = 'redirect';
436 $nextpageparams['action'] = 'grading';
df211804
DW
437 } else if ($action == 'addattempt') {
438 $this->process_add_attempt(required_param('userid', PARAM_INT));
439 $action = 'redirect';
440 $nextpageparams['action'] = 'grading';
9e795179 441 } else if ($action == 'reverttodraft') {
bbd0e548 442 $this->process_revert_to_draft();
d04557b3
DW
443 $action = 'redirect';
444 $nextpageparams['action'] = 'grading';
9e795179 445 } else if ($action == 'unlock') {
05a6445a 446 $this->process_unlock_submission();
d04557b3
DW
447 $action = 'redirect';
448 $nextpageparams['action'] = 'grading';
f8d107b3
DM
449 } else if ($action == 'setbatchmarkingworkflowstate') {
450 $this->process_set_batch_marking_workflow_state();
451 $action = 'redirect';
452 $nextpageparams['action'] = 'grading';
453 } else if ($action == 'setbatchmarkingallocation') {
454 $this->process_set_batch_marking_allocation();
455 $action = 'redirect';
456 $nextpageparams['action'] = 'grading';
9e795179 457 } else if ($action == 'confirmsubmit') {
94f26900 458 $action = 'submit';
57fbd5f9 459 if ($this->process_submit_for_grading($mform, $notices)) {
d04557b3
DW
460 $action = 'redirect';
461 $nextpageparams['action'] = 'view';
57fbd5f9
DW
462 } else if ($notices) {
463 $action = 'viewsubmitforgradingerror';
464 }
465 } else if ($action == 'submitotherforgrading') {
466 if ($this->process_submit_other_for_grading($mform, $notices)) {
467 $action = 'redirect';
468 $nextpageparams['action'] = 'grading';
469 } else {
470 $action = 'viewsubmitforgradingerror';
94f26900 471 }
df47b77f
DW
472 } else if ($action == 'gradingbatchoperation') {
473 $action = $this->process_grading_batch_operation($mform);
d04557b3
DW
474 if ($action == 'grading') {
475 $action = 'redirect';
476 $nextpageparams['action'] = 'grading';
477 }
9e795179 478 } else if ($action == 'submitgrade') {
ba30fe35 479 if (optional_param('saveandshownext', null, PARAM_RAW)) {
e5403f8c 480 // Save and show next.
bbd0e548
DW
481 $action = 'grade';
482 if ($this->process_save_grade($mform)) {
d04557b3
DW
483 $action = 'redirect';
484 $nextpageparams['action'] = 'grade';
485 $nextpageparams['rownum'] = optional_param('rownum', 0, PARAM_INT) + 1;
3fca6937 486 $nextpageparams['useridlistid'] = optional_param('useridlistid', $this->get_useridlist_key_id(), PARAM_ALPHANUM);
bbd0e548 487 }
ba30fe35 488 } else if (optional_param('nosaveandprevious', null, PARAM_RAW)) {
d04557b3
DW
489 $action = 'redirect';
490 $nextpageparams['action'] = 'grade';
491 $nextpageparams['rownum'] = optional_param('rownum', 0, PARAM_INT) - 1;
3fca6937 492 $nextpageparams['useridlistid'] = optional_param('useridlistid', $this->get_useridlist_key_id(), PARAM_ALPHANUM);
ba30fe35 493 } else if (optional_param('nosaveandnext', null, PARAM_RAW)) {
d04557b3
DW
494 $action = 'redirect';
495 $nextpageparams['action'] = 'grade';
496 $nextpageparams['rownum'] = optional_param('rownum', 0, PARAM_INT) + 1;
3fca6937 497 $nextpageparams['useridlistid'] = optional_param('useridlistid', $this->get_useridlist_key_id(), PARAM_ALPHANUM);
ba30fe35 498 } else if (optional_param('savegrade', null, PARAM_RAW)) {
e5403f8c 499 // Save changes button.
bbd0e548
DW
500 $action = 'grade';
501 if ($this->process_save_grade($mform)) {
082ad257
JF
502 $action = 'redirect';
503 $nextpageparams['action'] = 'savegradingresult';
bbd0e548
DW
504 }
505 } else {
e5403f8c 506 // Cancel button.
d04557b3
DW
507 $action = 'redirect';
508 $nextpageparams['action'] = 'grading';
bbd0e548 509 }
9e795179 510 } else if ($action == 'quickgrade') {
bf78ebd6
DW
511 $message = $this->process_save_quick_grades();
512 $action = 'quickgradingresult';
9e795179 513 } else if ($action == 'saveoptions') {
bbd0e548 514 $this->process_save_grading_options();
d04557b3
DW
515 $action = 'redirect';
516 $nextpageparams['action'] = 'grading';
9e795179
DW
517 } else if ($action == 'saveextension') {
518 $action = 'grantextension';
519 if ($this->process_save_extension($mform)) {
d04557b3
DW
520 $action = 'redirect';
521 $nextpageparams['action'] = 'grading';
9e795179 522 }
b473171a
DW
523 } else if ($action == 'revealidentitiesconfirm') {
524 $this->process_reveal_identities();
d04557b3
DW
525 $action = 'redirect';
526 $nextpageparams['action'] = 'grading';
bbd0e548
DW
527 }
528
d04557b3 529 $returnparams = array('rownum'=>optional_param('rownum', 0, PARAM_INT),
3fca6937 530 'useridlistid' => optional_param('useridlistid', $this->get_useridlist_key_id(), PARAM_ALPHANUM));
bbd0e548
DW
531 $this->register_return_link($action, $returnparams);
532
e61036f4
DB
533 // Include any page action as part of the body tag CSS id.
534 if (!empty($action)) {
535 $PAGE->set_pagetype('mod-assign-' . $action);
536 }
34b8f3a8 537 // Now show the right view page.
d04557b3
DW
538 if ($action == 'redirect') {
539 $nextpageurl = new moodle_url('/mod/assign/view.php', $nextpageparams);
540 redirect($nextpageurl);
541 return;
df211804 542 } else if ($action == 'savegradingresult') {
082ad257 543 $message = get_string('gradingchangessaved', 'assign');
df211804 544 $o .= $this->view_savegrading_result($message);
bf78ebd6
DW
545 } else if ($action == 'quickgradingresult') {
546 $mform = null;
547 $o .= $this->view_quickgrading_result($message);
bbd0e548
DW
548 } else if ($action == 'grade') {
549 $o .= $this->view_single_grade_page($mform);
550 } else if ($action == 'viewpluginassignfeedback') {
551 $o .= $this->view_plugin_content('assignfeedback');
552 } else if ($action == 'viewpluginassignsubmission') {
553 $o .= $this->view_plugin_content('assignsubmission');
554 } else if ($action == 'editsubmission') {
34b8f3a8 555 $o .= $this->view_edit_submission_page($mform, $notices);
bbd0e548
DW
556 } else if ($action == 'grading') {
557 $o .= $this->view_grading_page();
558 } else if ($action == 'downloadall') {
559 $o .= $this->download_submissions();
560 } else if ($action == 'submit') {
94f26900 561 $o .= $this->check_submit_for_grading($mform);
9e795179
DW
562 } else if ($action == 'grantextension') {
563 $o .= $this->view_grant_extension($mform);
b473171a
DW
564 } else if ($action == 'revealidentities') {
565 $o .= $this->view_reveal_identities_confirm($mform);
df47b77f
DW
566 } else if ($action == 'plugingradingbatchoperation') {
567 $o .= $this->view_plugin_grading_batch_operation($mform);
7a2b911c 568 } else if ($action == 'viewpluginpage') {
df47b77f 569 $o .= $this->view_plugin_page();
64220210
DW
570 } else if ($action == 'viewcourseindex') {
571 $o .= $this->view_course_index();
f8d107b3
DM
572 } else if ($action == 'viewbatchsetmarkingworkflowstate') {
573 $o .= $this->view_batch_set_workflow_state($mform);
574 } else if ($action == 'viewbatchmarkingallocation') {
575 $o .= $this->view_batch_markingallocation($mform);
57fbd5f9
DW
576 } else if ($action == 'viewsubmitforgradingerror') {
577 $o .= $this->view_error_page(get_string('submitforgrading', 'assign'), $notices);
bbd0e548
DW
578 } else {
579 $o .= $this->view_submission_page();
580 }
581
582 return $o;
583 }
584
bbd0e548 585 /**
e5403f8c 586 * Add this instance to the database.
bbd0e548
DW
587 *
588 * @param stdClass $formdata The data submitted from the form
589 * @param bool $callplugins This is used to skip the plugin code
590 * when upgrading an old assignment to a new one (the plugins get called manually)
591 * @return mixed false if an error occurs or the int id of the new instance
592 */
593 public function add_instance(stdClass $formdata, $callplugins) {
594 global $DB;
8e1266bf 595 $adminconfig = $this->get_admin_config();
bbd0e548
DW
596
597 $err = '';
598
e5403f8c 599 // Add the database record.
bbd0e548
DW
600 $update = new stdClass();
601 $update->name = $formdata->name;
602 $update->timemodified = time();
603 $update->timecreated = time();
604 $update->course = $formdata->course;
605 $update->courseid = $formdata->course;
606 $update->intro = $formdata->intro;
607 $update->introformat = $formdata->introformat;
18de52db 608 $update->alwaysshowdescription = !empty($formdata->alwaysshowdescription);
bbd0e548 609 $update->submissiondrafts = $formdata->submissiondrafts;
94f26900 610 $update->requiresubmissionstatement = $formdata->requiresubmissionstatement;
bbd0e548 611 $update->sendnotifications = $formdata->sendnotifications;
75f87a57 612 $update->sendlatenotifications = $formdata->sendlatenotifications;
8e1266bf
DW
613 $update->sendstudentnotifications = $adminconfig->sendstudentnotifications;
614 if (isset($formdata->sendstudentnotifications)) {
615 $update->sendstudentnotifications = $formdata->sendstudentnotifications;
616 }
b618f2d9 617 $update->duedate = $formdata->duedate;
9e795179 618 $update->cutoffdate = $formdata->cutoffdate;
bbd0e548
DW
619 $update->allowsubmissionsfromdate = $formdata->allowsubmissionsfromdate;
620 $update->grade = $formdata->grade;
694b11ab 621 $update->completionsubmit = !empty($formdata->completionsubmit);
12a1a0da
DW
622 $update->teamsubmission = $formdata->teamsubmission;
623 $update->requireallteammemberssubmit = $formdata->requireallteammemberssubmit;
1fba9cf6
DW
624 if (isset($formdata->teamsubmissiongroupingid)) {
625 $update->teamsubmissiongroupingid = $formdata->teamsubmissiongroupingid;
626 }
b473171a 627 $update->blindmarking = $formdata->blindmarking;
e8490064
DW
628 $update->attemptreopenmethod = ASSIGN_ATTEMPT_REOPEN_METHOD_NONE;
629 if (!empty($formdata->attemptreopenmethod)) {
630 $update->attemptreopenmethod = $formdata->attemptreopenmethod;
631 }
df211804
DW
632 if (!empty($formdata->maxattempts)) {
633 $update->maxattempts = $formdata->maxattempts;
634 }
e528997a
AH
635 if (isset($formdata->preventsubmissionnotingroup)) {
636 $update->preventsubmissionnotingroup = $formdata->preventsubmissionnotingroup;
637 }
f8d107b3
DM
638 $update->markingworkflow = $formdata->markingworkflow;
639 $update->markingallocation = $formdata->markingallocation;
5a96486b
DM
640 if (empty($update->markingworkflow)) { // If marking workflow is disabled, make sure allocation is disabled.
641 $update->markingallocation = 0;
642 }
12a1a0da 643
bbd0e548
DW
644 $returnid = $DB->insert_record('assign', $update);
645 $this->instance = $DB->get_record('assign', array('id'=>$returnid), '*', MUST_EXIST);
e5403f8c 646 // Cache the course record.
bbd0e548
DW
647 $this->course = $DB->get_record('course', array('id'=>$formdata->course), '*', MUST_EXIST);
648
7faf78cb
HB
649 $this->save_intro_draft_files($formdata);
650
bbd0e548 651 if ($callplugins) {
e5403f8c 652 // Call save_settings hook for submission plugins.
bbd0e548
DW
653 foreach ($this->submissionplugins as $plugin) {
654 if (!$this->update_plugin_instance($plugin, $formdata)) {
655 print_error($plugin->get_error());
656 return false;
657 }
658 }
659 foreach ($this->feedbackplugins as $plugin) {
660 if (!$this->update_plugin_instance($plugin, $formdata)) {
661 print_error($plugin->get_error());
662 return false;
663 }
664 }
665
e5403f8c
DW
666 // In the case of upgrades the coursemodule has not been set,
667 // so we need to wait before calling these two.
bbd0e548 668 $this->update_calendar($formdata->coursemodule);
bbd0e548
DW
669 $this->update_gradebook(false, $formdata->coursemodule);
670
671 }
672
673 $update = new stdClass();
674 $update->id = $this->get_instance()->id;
675 $update->nosubmissions = (!$this->is_any_submission_plugin_enabled()) ? 1: 0;
676 $DB->update_record('assign', $update);
677
678 return $returnid;
679 }
680
681 /**
e5403f8c 682 * Delete all grades from the gradebook for this assignment.
bbd0e548
DW
683 *
684 * @return bool
685 */
47f48152 686 protected function delete_grades() {
bbd0e548
DW
687 global $CFG;
688
e5403f8c
DW
689 $result = grade_update('mod/assign',
690 $this->get_course()->id,
691 'mod',
692 'assign',
693 $this->get_instance()->id,
694 0,
695 null,
696 array('deleted'=>1));
697 return $result == GRADE_UPDATE_OK;
bbd0e548
DW
698 }
699
700 /**
e5403f8c 701 * Delete this instance from the database.
bbd0e548
DW
702 *
703 * @return bool false if an error occurs
704 */
705 public function delete_instance() {
706 global $DB;
707 $result = true;
708
709 foreach ($this->submissionplugins as $plugin) {
710 if (!$plugin->delete_instance()) {
711 print_error($plugin->get_error());
712 $result = false;
713 }
714 }
715 foreach ($this->feedbackplugins as $plugin) {
716 if (!$plugin->delete_instance()) {
717 print_error($plugin->get_error());
718 $result = false;
719 }
720 }
721
e5403f8c 722 // Delete files associated with this assignment.
bbd0e548
DW
723 $fs = get_file_storage();
724 if (! $fs->delete_area_files($this->context->id) ) {
725 $result = false;
726 }
727
e5403f8c 728 // Delete_records will throw an exception if it fails - so no need for error checking here.
26ac0abc
MN
729 $DB->delete_records('assign_submission', array('assignment' => $this->get_instance()->id));
730 $DB->delete_records('assign_grades', array('assignment' => $this->get_instance()->id));
731 $DB->delete_records('assign_plugin_config', array('assignment' => $this->get_instance()->id));
732 $DB->delete_records('assign_user_flags', array('assignment' => $this->get_instance()->id));
733 $DB->delete_records('assign_user_mapping', array('assignment' => $this->get_instance()->id));
bbd0e548 734
e5403f8c 735 // Delete items from the gradebook.
bbd0e548
DW
736 if (! $this->delete_grades()) {
737 $result = false;
738 }
739
e5403f8c 740 // Delete the instance.
bbd0e548
DW
741 $DB->delete_records('assign', array('id'=>$this->get_instance()->id));
742
743 return $result;
744 }
745
d38dc52f 746 /**
e5403f8c
DW
747 * Actual implementation of the reset course functionality, delete all the
748 * assignment submissions for course $data->courseid.
749 *
1561a37c 750 * @param stdClass $data the data submitted from the reset course.
e5403f8c
DW
751 * @return array status array
752 */
d38dc52f 753 public function reset_userdata($data) {
e5403f8c 754 global $CFG, $DB;
d38dc52f
RW
755
756 $componentstr = get_string('modulenameplural', 'assign');
757 $status = array();
758
759 $fs = get_file_storage();
760 if (!empty($data->reset_assign_submissions)) {
761 // Delete files associated with this assignment.
762 foreach ($this->submissionplugins as $plugin) {
763 $fileareas = array();
764 $plugincomponent = $plugin->get_subtype() . '_' . $plugin->get_type();
765 $fileareas = $plugin->get_file_areas();
43fb9ced 766 foreach ($fileareas as $filearea => $notused) {
d38dc52f
RW
767 $fs->delete_area_files($this->context->id, $plugincomponent, $filearea);
768 }
769
770 if (!$plugin->delete_instance()) {
771 $status[] = array('component'=>$componentstr,
e5403f8c 772 'item'=>get_string('deleteallsubmissions', 'assign'),
d38dc52f
RW
773 'error'=>$plugin->get_error());
774 }
775 }
776
777 foreach ($this->feedbackplugins as $plugin) {
778 $fileareas = array();
779 $plugincomponent = $plugin->get_subtype() . '_' . $plugin->get_type();
780 $fileareas = $plugin->get_file_areas();
43fb9ced 781 foreach ($fileareas as $filearea => $notused) {
d38dc52f
RW
782 $fs->delete_area_files($this->context->id, $plugincomponent, $filearea);
783 }
784
785 if (!$plugin->delete_instance()) {
786 $status[] = array('component'=>$componentstr,
e5403f8c 787 'item'=>get_string('deleteallsubmissions', 'assign'),
d38dc52f
RW
788 'error'=>$plugin->get_error());
789 }
790 }
791
26ac0abc
MN
792 $assignids = $DB->get_records('assign', array('course' => $data->courseid), '', 'id');
793 list($sql, $params) = $DB->get_in_or_equal(array_keys($assignids));
d38dc52f 794
26ac0abc
MN
795 $DB->delete_records_select('assign_submission', "assignment $sql", $params);
796 $DB->delete_records_select('assign_user_flags', "assignment $sql", $params);
e5403f8c 797
d38dc52f 798 $status[] = array('component'=>$componentstr,
e5403f8c 799 'item'=>get_string('deleteallsubmissions', 'assign'),
d38dc52f
RW
800 'error'=>false);
801
e5403f8c 802 if (!empty($data->reset_gradebook_grades)) {
26ac0abc 803 $DB->delete_records_select('assign_grades', "assignment $sql", $params);
d38dc52f
RW
804 // Remove all grades from gradebook.
805 require_once($CFG->dirroot.'/mod/assign/lib.php');
806 assign_reset_gradebook($data->courseid);
48242bf3
MA
807
808 // Reset revealidentities if both submissions and grades have been reset.
48242bf3 809 if ($this->get_instance()->blindmarking && $this->get_instance()->revealidentities) {
225fa27f 810 $DB->set_field('assign', 'revealidentities', 0, array('id' => $this->get_instance()->id));
48242bf3 811 }
d38dc52f
RW
812 }
813 }
814 // Updating dates - shift may be negative too.
815 if ($data->timeshift) {
816 shift_course_mod_dates('assign',
e5403f8c 817 array('duedate', 'allowsubmissionsfromdate', 'cutoffdate'),
d38dc52f 818 $data->timeshift,
e63515ba 819 $data->courseid, $this->get_instance()->id);
d38dc52f
RW
820 $status[] = array('component'=>$componentstr,
821 'item'=>get_string('datechanged'),
822 'error'=>false);
823 }
824
825 return $status;
826 }
827
bbd0e548 828 /**
e5403f8c 829 * Update the settings for a single plugin.
bbd0e548
DW
830 *
831 * @param assign_plugin $plugin The plugin to update
832 * @param stdClass $formdata The form data
833 * @return bool false if an error occurs
834 */
47f48152 835 protected function update_plugin_instance(assign_plugin $plugin, stdClass $formdata) {
bbd0e548
DW
836 if ($plugin->is_visible()) {
837 $enabledname = $plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled';
b0da618b 838 if (!empty($formdata->$enabledname)) {
bbd0e548
DW
839 $plugin->enable();
840 if (!$plugin->save_settings($formdata)) {
841 print_error($plugin->get_error());
842 return false;
843 }
844 } else {
845 $plugin->disable();
846 }
847 }
848 return true;
849 }
850
851 /**
e5403f8c 852 * Update the gradebook information for this assignment.
bbd0e548
DW
853 *
854 * @param bool $reset If true, will reset all grades in the gradbook for this assignment
855 * @param int $coursemoduleid This is required because it might not exist in the database yet
856 * @return bool
857 */
858 public function update_gradebook($reset, $coursemoduleid) {
e5403f8c
DW
859 global $CFG;
860
bbd0e548
DW
861 require_once($CFG->dirroot.'/mod/assign/lib.php');
862 $assign = clone $this->get_instance();
863 $assign->cmidnumber = $coursemoduleid;
456d7bc7
RT
864
865 // Set assign gradebook feedback plugin status (enabled and visible).
866 $assign->gradefeedbackenabled = $this->is_gradebook_feedback_enabled();
867
bbd0e548
DW
868 $param = null;
869 if ($reset) {
870 $param = 'reset';
871 }
872
873 return assign_grade_item_update($assign, $param);
874 }
875
e5403f8c
DW
876 /**
877 * Load and cache the admin config for this module.
bc5a657b 878 *
cfc81f03
DW
879 * @return stdClass the plugin config
880 */
881 public function get_admin_config() {
882 if ($this->adminconfig) {
883 return $this->adminconfig;
884 }
b11808c7 885 $this->adminconfig = get_config('assign');
cfc81f03
DW
886 return $this->adminconfig;
887 }
888
bbd0e548 889 /**
e5403f8c 890 * Update the calendar entries for this assignment.
bbd0e548 891 *
e5403f8c
DW
892 * @param int $coursemoduleid - Required to pass this in because it might
893 * not exist in the database yet.
bbd0e548
DW
894 * @return bool
895 */
896 public function update_calendar($coursemoduleid) {
897 global $DB, $CFG;
898 require_once($CFG->dirroot.'/calendar/lib.php');
899
e5403f8c
DW
900 // Special case for add_instance as the coursemodule has not been set yet.
901 $instance = $this->get_instance();
bbd0e548 902
72797e68
MA
903 $eventtype = 'due';
904
e5403f8c 905 if ($instance->duedate) {
bbd0e548
DW
906 $event = new stdClass();
907
72797e68 908 $params = array('modulename' => 'assign', 'instance' => $instance->id, 'eventtype' => $eventtype);
0cfc7d13
FM
909 $event->id = $DB->get_field('event', 'id', $params);
910 $event->name = $instance->name;
911 $event->timestart = $instance->duedate;
912
913 // Convert the links to pluginfile. It is a bit hacky but at this stage the files
914 // might not have been saved in the module area yet.
915 $intro = $instance->intro;
916 if ($draftid = file_get_submitted_draft_itemid('introeditor')) {
917 $intro = file_rewrite_urls_to_pluginfile($intro, $draftid);
918 }
bbd0e548 919
0cfc7d13
FM
920 // We need to remove the links to files as the calendar is not ready
921 // to support module events with file areas.
922 $intro = strip_pluginfile_content($intro);
ae7638f7
DW
923 if ($this->show_intro()) {
924 $event->description = array(
925 'text' => $intro,
926 'format' => $instance->introformat
927 );
928 } else {
929 $event->description = array(
930 'text' => '',
931 'format' => $instance->introformat
932 );
933 }
bbd0e548 934
0cfc7d13 935 if ($event->id) {
bbd0e548
DW
936 $calendarevent = calendar_event::load($event->id);
937 $calendarevent->update($event);
938 } else {
0cfc7d13 939 unset($event->id);
e5403f8c 940 $event->courseid = $instance->course;
bbd0e548
DW
941 $event->groupid = 0;
942 $event->userid = 0;
943 $event->modulename = 'assign';
e5403f8c 944 $event->instance = $instance->id;
72797e68 945 $event->eventtype = $eventtype;
bbd0e548 946 $event->timeduration = 0;
bbd0e548
DW
947 calendar_event::create($event);
948 }
949 } else {
72797e68 950 $DB->delete_records('event', array('modulename' => 'assign', 'instance' => $instance->id, 'eventtype' => $eventtype));
bbd0e548
DW
951 }
952 }
953
954
955 /**
e5403f8c 956 * Update this instance in the database.
bbd0e548
DW
957 *
958 * @param stdClass $formdata - the data submitted from the form
959 * @return bool false if an error occurs
960 */
961 public function update_instance($formdata) {
962 global $DB;
8e1266bf 963 $adminconfig = $this->get_admin_config();
bbd0e548
DW
964
965 $update = new stdClass();
966 $update->id = $formdata->instance;
967 $update->name = $formdata->name;
968 $update->timemodified = time();
969 $update->course = $formdata->course;
970 $update->intro = $formdata->intro;
971 $update->introformat = $formdata->introformat;
18de52db 972 $update->alwaysshowdescription = !empty($formdata->alwaysshowdescription);
bbd0e548 973 $update->submissiondrafts = $formdata->submissiondrafts;
94f26900 974 $update->requiresubmissionstatement = $formdata->requiresubmissionstatement;
bbd0e548 975 $update->sendnotifications = $formdata->sendnotifications;
75f87a57 976 $update->sendlatenotifications = $formdata->sendlatenotifications;
8e1266bf
DW
977 $update->sendstudentnotifications = $adminconfig->sendstudentnotifications;
978 if (isset($formdata->sendstudentnotifications)) {
979 $update->sendstudentnotifications = $formdata->sendstudentnotifications;
980 }
b618f2d9 981 $update->duedate = $formdata->duedate;
9e795179 982 $update->cutoffdate = $formdata->cutoffdate;
bbd0e548
DW
983 $update->allowsubmissionsfromdate = $formdata->allowsubmissionsfromdate;
984 $update->grade = $formdata->grade;
a9f5fc15 985 if (!empty($formdata->completionunlocked)) {
986 $update->completionsubmit = !empty($formdata->completionsubmit);
987 }
12a1a0da
DW
988 $update->teamsubmission = $formdata->teamsubmission;
989 $update->requireallteammemberssubmit = $formdata->requireallteammemberssubmit;
1fba9cf6
DW
990 if (isset($formdata->teamsubmissiongroupingid)) {
991 $update->teamsubmissiongroupingid = $formdata->teamsubmissiongroupingid;
992 }
b473171a 993 $update->blindmarking = $formdata->blindmarking;
e8490064
DW
994 $update->attemptreopenmethod = ASSIGN_ATTEMPT_REOPEN_METHOD_NONE;
995 if (!empty($formdata->attemptreopenmethod)) {
996 $update->attemptreopenmethod = $formdata->attemptreopenmethod;
997 }
df211804
DW
998 if (!empty($formdata->maxattempts)) {
999 $update->maxattempts = $formdata->maxattempts;
1000 }
e528997a
AH
1001 if (isset($formdata->preventsubmissionnotingroup)) {
1002 $update->preventsubmissionnotingroup = $formdata->preventsubmissionnotingroup;
1003 }
f8d107b3
DM
1004 $update->markingworkflow = $formdata->markingworkflow;
1005 $update->markingallocation = $formdata->markingallocation;
5a96486b
DM
1006 if (empty($update->markingworkflow)) { // If marking workflow is disabled, make sure allocation is disabled.
1007 $update->markingallocation = 0;
1008 }
12a1a0da 1009
bbd0e548
DW
1010 $result = $DB->update_record('assign', $update);
1011 $this->instance = $DB->get_record('assign', array('id'=>$update->id), '*', MUST_EXIST);
1012
7faf78cb
HB
1013 $this->save_intro_draft_files($formdata);
1014
e5403f8c 1015 // Load the assignment so the plugins have access to it.
bbd0e548 1016
e5403f8c 1017 // Call save_settings hook for submission plugins.
bbd0e548
DW
1018 foreach ($this->submissionplugins as $plugin) {
1019 if (!$this->update_plugin_instance($plugin, $formdata)) {
1020 print_error($plugin->get_error());
1021 return false;
1022 }
1023 }
1024 foreach ($this->feedbackplugins as $plugin) {
1025 if (!$this->update_plugin_instance($plugin, $formdata)) {
1026 print_error($plugin->get_error());
1027 return false;
1028 }
1029 }
1030
bbd0e548 1031 $this->update_calendar($this->get_course_module()->id);
bbd0e548
DW
1032 $this->update_gradebook(false, $this->get_course_module()->id);
1033
1034 $update = new stdClass();
1035 $update->id = $this->get_instance()->id;
1036 $update->nosubmissions = (!$this->is_any_submission_plugin_enabled()) ? 1: 0;
1037 $DB->update_record('assign', $update);
1038
bbd0e548
DW
1039 return $result;
1040 }
1041
7faf78cb
HB
1042 /**
1043 * Save the attachments in the draft areas.
1044 *
1045 * @param stdClass $formdata
1046 */
1047 protected function save_intro_draft_files($formdata) {
1048 if (isset($formdata->introattachments)) {
1049 file_save_draft_area_files($formdata->introattachments, $this->get_context()->id,
1050 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA, 0);
1051 }
1052 }
1053
bbd0e548 1054 /**
e5403f8c 1055 * Add elements in grading plugin form.
bbd0e548
DW
1056 *
1057 * @param mixed $grade stdClass|null
1058 * @param MoodleQuickForm $mform
1059 * @param stdClass $data
fc7b7d52 1060 * @param int $userid - The userid we are grading
bbd0e548
DW
1061 * @return void
1062 */
47f48152 1063 protected function add_plugin_grade_elements($grade, MoodleQuickForm $mform, stdClass $data, $userid) {
bbd0e548
DW
1064 foreach ($this->feedbackplugins as $plugin) {
1065 if ($plugin->is_enabled() && $plugin->is_visible()) {
3a5c3b3c 1066 $plugin->get_form_elements_for_user($grade, $mform, $data, $userid);
bbd0e548
DW
1067 }
1068 }
1069 }
1070
1071
1072
1073 /**
e5403f8c 1074 * Add one plugins settings to edit plugin form.
bbd0e548
DW
1075 *
1076 * @param assign_plugin $plugin The plugin to add the settings from
e5403f8c
DW
1077 * @param MoodleQuickForm $mform The form to add the configuration settings to.
1078 * This form is modified directly (not returned).
b0da618b
DW
1079 * @param array $pluginsenabled A list of form elements to be added to a group.
1080 * The new element is added to this array by this function.
bbd0e548
DW
1081 * @return void
1082 */
b0da618b 1083 protected function add_plugin_settings(assign_plugin $plugin, MoodleQuickForm $mform, & $pluginsenabled) {
bbd0e548 1084 global $CFG;
f159ad73 1085 if ($plugin->is_visible() && !$plugin->is_configurable() && $plugin->is_enabled()) {
1086 $name = $plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled';
1087 $pluginsenabled[] = $mform->createElement('hidden', $name, 1);
1088 $mform->setType($name, PARAM_BOOL);
1089 $plugin->get_settings($mform);
1090 } else if ($plugin->is_visible() && $plugin->is_configurable()) {
b0da618b
DW
1091 $name = $plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled';
1092 $label = $plugin->get_name();
1093 $label .= ' ' . $this->get_renderer()->help_icon('enabled', $plugin->get_subtype() . '_' . $plugin->get_type());
1094 $pluginsenabled[] = $mform->createElement('checkbox', $name, '', $label);
bbd0e548 1095
cfc81f03 1096 $default = get_config($plugin->get_subtype() . '_' . $plugin->get_type(), 'default');
bbd0e548
DW
1097 if ($plugin->get_config('enabled') !== false) {
1098 $default = $plugin->is_enabled();
1099 }
1100 $mform->setDefault($plugin->get_subtype() . '_' . $plugin->get_type() . '_enabled', $default);
1101
1102 $plugin->get_settings($mform);
1103
1104 }
bbd0e548
DW
1105 }
1106
bbd0e548 1107 /**
e5403f8c 1108 * Add settings to edit plugin form.
bbd0e548 1109 *
e5403f8c
DW
1110 * @param MoodleQuickForm $mform The form to add the configuration settings to.
1111 * This form is modified directly (not returned).
bbd0e548
DW
1112 * @return void
1113 */
1114 public function add_all_plugin_settings(MoodleQuickForm $mform) {
df211804 1115 $mform->addElement('header', 'submissiontypes', get_string('submissiontypes', 'assign'));
bbd0e548 1116
b0da618b
DW
1117 $submissionpluginsenabled = array();
1118 $group = $mform->addGroup(array(), 'submissionplugins', get_string('submissiontypes', 'assign'), array(' '), false);
bbd0e548 1119 foreach ($this->submissionplugins as $plugin) {
b0da618b 1120 $this->add_plugin_settings($plugin, $mform, $submissionpluginsenabled);
bbd0e548 1121 }
b0da618b
DW
1122 $group->setElements($submissionpluginsenabled);
1123
df211804 1124 $mform->addElement('header', 'feedbacktypes', get_string('feedbacktypes', 'assign'));
b0da618b
DW
1125 $feedbackpluginsenabled = array();
1126 $group = $mform->addGroup(array(), 'feedbackplugins', get_string('feedbacktypes', 'assign'), array(' '), false);
bbd0e548 1127 foreach ($this->feedbackplugins as $plugin) {
b0da618b 1128 $this->add_plugin_settings($plugin, $mform, $feedbackpluginsenabled);
bbd0e548 1129 }
b0da618b
DW
1130 $group->setElements($feedbackpluginsenabled);
1131 $mform->setExpanded('submissiontypes');
bbd0e548
DW
1132 }
1133
1134 /**
1135 * Allow each plugin an opportunity to update the defaultvalues
1136 * passed in to the settings form (needed to set up draft areas for
1137 * editor and filemanager elements)
e5403f8c 1138 *
bbd0e548
DW
1139 * @param array $defaultvalues
1140 */
1141 public function plugin_data_preprocessing(&$defaultvalues) {
1142 foreach ($this->submissionplugins as $plugin) {
1143 if ($plugin->is_visible()) {
1144 $plugin->data_preprocessing($defaultvalues);
1145 }
1146 }
1147 foreach ($this->feedbackplugins as $plugin) {
1148 if ($plugin->is_visible()) {
1149 $plugin->data_preprocessing($defaultvalues);
1150 }
1151 }
1152 }
1153
1154 /**
1155 * Get the name of the current module.
1156 *
1157 * @return string the module name (Assignment)
1158 */
1159 protected function get_module_name() {
1160 if (isset(self::$modulename)) {
1161 return self::$modulename;
1162 }
1163 self::$modulename = get_string('modulename', 'assign');
1164 return self::$modulename;
1165 }
1166
1167 /**
1168 * Get the plural name of the current module.
1169 *
1170 * @return string the module name plural (Assignments)
1171 */
1172 protected function get_module_name_plural() {
1173 if (isset(self::$modulenameplural)) {
1174 return self::$modulenameplural;
1175 }
1176 self::$modulenameplural = get_string('modulenameplural', 'assign');
1177 return self::$modulenameplural;
1178 }
1179
1180 /**
1181 * Has this assignment been constructed from an instance?
1182 *
1183 * @return bool
1184 */
1185 public function has_instance() {
1186 return $this->instance || $this->get_course_module();
1187 }
1188
1189 /**
1190 * Get the settings for the current instance of this assignment
1191 *
1192 * @return stdClass The settings
1193 */
1194 public function get_instance() {
1195 global $DB;
1196 if ($this->instance) {
1197 return $this->instance;
1198 }
1199 if ($this->get_course_module()) {
e5403f8c
DW
1200 $params = array('id' => $this->get_course_module()->instance);
1201 $this->instance = $DB->get_record('assign', $params, '*', MUST_EXIST);
bbd0e548
DW
1202 }
1203 if (!$this->instance) {
e5403f8c
DW
1204 throw new coding_exception('Improper use of the assignment class. ' .
1205 'Cannot load the assignment record.');
bbd0e548
DW
1206 }
1207 return $this->instance;
1208 }
1209
0d64c75f
PN
1210 /**
1211 * Get the primary grade item for this assign instance.
1212 *
1213 * @return stdClass The grade_item record
1214 */
1215 public function get_grade_item() {
1216 if ($this->gradeitem) {
1217 return $this->gradeitem;
1218 }
1219 $instance = $this->get_instance();
1220 $params = array('itemtype' => 'mod',
1221 'itemmodule' => 'assign',
1222 'iteminstance' => $instance->id,
1223 'courseid' => $instance->course,
1224 'itemnumber' => 0);
1225 $this->gradeitem = grade_item::fetch($params);
1226 if (!$this->gradeitem) {
1227 throw new coding_exception('Improper use of the assignment class. ' .
1228 'Cannot load the grade item.');
1229 }
1230 return $this->gradeitem;
1231 }
1232
bbd0e548 1233 /**
e5403f8c
DW
1234 * Get the context of the current course.
1235 *
bbd0e548
DW
1236 * @return mixed context|null The course context
1237 */
1238 public function get_course_context() {
1239 if (!$this->context && !$this->course) {
e5403f8c
DW
1240 throw new coding_exception('Improper use of the assignment class. ' .
1241 'Cannot load the course context.');
bbd0e548
DW
1242 }
1243 if ($this->context) {
1244 return $this->context->get_course_context();
1245 } else {
1246 return context_course::instance($this->course->id);
1247 }
1248 }
1249
1250
1251 /**
e5403f8c 1252 * Get the current course module.
bbd0e548 1253 *
c13ac85d 1254 * @return cm_info|null The course module or null if not known
bbd0e548
DW
1255 */
1256 public function get_course_module() {
1257 if ($this->coursemodule) {
1258 return $this->coursemodule;
1259 }
1260 if (!$this->context) {
1261 return null;
1262 }
1263
1264 if ($this->context->contextlevel == CONTEXT_MODULE) {
c13ac85d 1265 $modinfo = get_fast_modinfo($this->get_course());
1266 $this->coursemodule = $modinfo->get_cm($this->context->instanceid);
bbd0e548
DW
1267 return $this->coursemodule;
1268 }
1269 return null;
1270 }
1271
1272 /**
e5403f8c 1273 * Get context module.
bbd0e548
DW
1274 *
1275 * @return context
1276 */
1277 public function get_context() {
1278 return $this->context;
1279 }
1280
1281 /**
e5403f8c
DW
1282 * Get the current course.
1283 *
bbd0e548
DW
1284 * @return mixed stdClass|null The course
1285 */
1286 public function get_course() {
1287 global $DB;
e5403f8c 1288
bbd0e548
DW
1289 if ($this->course) {
1290 return $this->course;
1291 }
1292
1293 if (!$this->context) {
1294 return null;
1295 }
e5403f8c
DW
1296 $params = array('id' => $this->get_course_context()->instanceid);
1297 $this->course = $DB->get_record('course', $params, '*', MUST_EXIST);
1298
bbd0e548
DW
1299 return $this->course;
1300 }
1301
7faf78cb
HB
1302 /**
1303 * Count the number of intro attachments.
1304 *
1305 * @return int
1306 */
1307 protected function count_attachments() {
1308
1309 $fs = get_file_storage();
1310 $files = $fs->get_area_files($this->get_context()->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA,
1311 0, 'id', false);
1312
1313 return count($files);
1314 }
1315
1316 /**
1317 * Are there any intro attachments to display?
1318 *
1319 * @return boolean
1320 */
1321 protected function has_visible_attachments() {
1322 return ($this->count_attachments() > 0);
1323 }
1324
bbd0e548 1325 /**
e5403f8c 1326 * Return a grade in user-friendly form, whether it's a scale or not.
bbd0e548 1327 *
9682626e 1328 * @param mixed $grade int|null
bf78ebd6 1329 * @param boolean $editing Are we allowing changes to this grade?
2a4fbc32
SH
1330 * @param int $userid The user id the grade belongs to
1331 * @param int $modified Timestamp from when the grade was last modified
bbd0e548
DW
1332 * @return string User-friendly representation of grade
1333 */
bf78ebd6 1334 public function display_grade($grade, $editing, $userid=0, $modified=0) {
bbd0e548
DW
1335 global $DB;
1336
1337 static $scalegrades = array();
1338
be79d93f
DW
1339 $o = '';
1340
2a4fbc32 1341 if ($this->get_instance()->grade >= 0) {
e5403f8c 1342 // Normal number.
e7ade405 1343 if ($editing && $this->get_instance()->grade > 0) {
2d8a9ce9
DW
1344 if ($grade < 0) {
1345 $displaygrade = '';
1346 } else {
dd8ba800 1347 $displaygrade = format_float($grade, 2);
2d8a9ce9 1348 }
e5403f8c
DW
1349 $o .= '<label class="accesshide" for="quickgrade_' . $userid . '">' .
1350 get_string('usergrade', 'assign') .
1351 '</label>';
1352 $o .= '<input type="text"
1353 id="quickgrade_' . $userid . '"
1354 name="quickgrade_' . $userid . '"
1355 value="' . $displaygrade . '"
1356 size="6"
1357 maxlength="10"
1358 class="quickgrade"/>';
1359 $o .= '&nbsp;/&nbsp;' . format_float($this->get_instance()->grade, 2);
bf78ebd6 1360 return $o;
bbd0e548 1361 } else {
a1e54f4d 1362 if ($grade == -1 || $grade === null) {
be79d93f 1363 $o .= '-';
a1e54f4d 1364 } else {
0d64c75f
PN
1365 $item = $this->get_grade_item();
1366 $o .= grade_format_gradevalue($grade, $item);
1367 if ($item->get_displaytype() == GRADE_DISPLAY_TYPE_REAL) {
1368 // If displaying the raw grade, also display the total value.
1369 $o .= '&nbsp;/&nbsp;' . format_float($this->get_instance()->grade, 2);
1370 }
a1e54f4d 1371 }
0d64c75f 1372 return $o;
bbd0e548
DW
1373 }
1374
2a4fbc32 1375 } else {
e5403f8c 1376 // Scale.
bbd0e548
DW
1377 if (empty($this->cache['scale'])) {
1378 if ($scale = $DB->get_record('scale', array('id'=>-($this->get_instance()->grade)))) {
1379 $this->cache['scale'] = make_menu_from_list($scale->scale);
1380 } else {
be79d93f
DW
1381 $o .= '-';
1382 return $o;
bbd0e548
DW
1383 }
1384 }
bf78ebd6 1385 if ($editing) {
e5403f8c
DW
1386 $o .= '<label class="accesshide"
1387 for="quickgrade_' . $userid . '">' .
1388 get_string('usergrade', 'assign') .
1389 '</label>';
7400be1b 1390 $o .= '<select name="quickgrade_' . $userid . '" class="quickgrade">';
bf78ebd6
DW
1391 $o .= '<option value="-1">' . get_string('nograde') . '</option>';
1392 foreach ($this->cache['scale'] as $optionid => $option) {
1393 $selected = '';
1394 if ($grade == $optionid) {
1395 $selected = 'selected="selected"';
1396 }
1397 $o .= '<option value="' . $optionid . '" ' . $selected . '>' . $option . '</option>';
1398 }
1399 $o .= '</select>';
bf78ebd6
DW
1400 return $o;
1401 } else {
1402 $scaleid = (int)$grade;
1403 if (isset($this->cache['scale'][$scaleid])) {
be79d93f
DW
1404 $o .= $this->cache['scale'][$scaleid];
1405 return $o;
bf78ebd6 1406 }
be79d93f
DW
1407 $o .= '-';
1408 return $o;
bbd0e548 1409 }
bbd0e548
DW
1410 }
1411 }
1412
1413 /**
e5403f8c
DW
1414 * Load a list of users enrolled in the current course with the specified permission and group.
1415 * 0 for no group.
bbd0e548
DW
1416 *
1417 * @param int $currentgroup
1418 * @param bool $idsonly
1419 * @return array List of user records
1420 */
1421 public function list_participants($currentgroup, $idsonly) {
bc006a66
DM
1422
1423 if (empty($currentgroup)) {
1424 $currentgroup = 0;
1425 }
1426
c46db93c
DW
1427 $key = $this->context->id . '-' . $currentgroup . '-' . $this->show_only_active_users();
1428 if (!isset($this->participants[$key])) {
a0e59f04 1429 $users = get_enrolled_users($this->context, 'mod/assign:submit', $currentgroup, 'u.*', null, null, null,
1ecb8044 1430 $this->show_only_active_users());
c46db93c
DW
1431
1432 $cm = $this->get_course_module();
c13ac85d 1433 $info = new \core_availability\info_module($cm);
1434 $users = $info->filter_user_list($users);
c46db93c
DW
1435
1436 $this->participants[$key] = $users;
bbd0e548 1437 }
a0e59f04 1438
c46db93c
DW
1439 if ($idsonly) {
1440 $idslist = array();
1441 foreach ($this->participants[$key] as $id => $user) {
1442 $idslist[$id] = new stdClass();
1443 $idslist[$id]->id = $id;
a0e59f04 1444 }
c46db93c 1445 return $idslist;
a0e59f04 1446 }
c46db93c 1447 return $this->participants[$key];
bbd0e548
DW
1448 }
1449
12a1a0da 1450 /**
e5403f8c 1451 * Load a count of valid teams for this assignment.
12a1a0da 1452 *
bc006a66 1453 * @param int $activitygroup Activity active group
12a1a0da
DW
1454 * @return int number of valid teams
1455 */
bc006a66
DM
1456 public function count_teams($activitygroup = 0) {
1457
1458 $count = 0;
1459
1460 $participants = $this->list_participants($activitygroup, true);
1461
1462 // If a team submission grouping id is provided all good as all returned groups
1463 // are the submission teams, but if no team submission grouping was specified
1464 // $groups will contain all participants groups.
1465 if ($this->get_instance()->teamsubmissiongroupingid) {
12a1a0da 1466
bc006a66
DM
1467 // We restrict the users to the selected group ones.
1468 $groups = groups_get_all_groups($this->get_course()->id,
1469 array_keys($participants),
1470 $this->get_instance()->teamsubmissiongroupingid,
1471 'DISTINCT g.id, g.name');
12a1a0da 1472
bc006a66
DM
1473 $count = count($groups);
1474
1475 // When a specific group is selected we don't count the default group users.
1476 if ($activitygroup == 0) {
e528997a
AH
1477 if (empty($this->get_instance()->preventsubmissionnotingroup)) {
1478 // See if there are any users in the default group.
1479 $defaultusers = $this->get_submission_group_members(0, true);
1480 if (count($defaultusers) > 0) {
1481 $count += 1;
1482 }
bc006a66
DM
1483 }
1484 }
1485 } else {
1486 // It is faster to loop around participants if no grouping was specified.
1487 $groups = array();
1488 foreach ($participants as $participant) {
1489 if ($group = $this->get_submission_group($participant->id)) {
1490 $groups[$group->id] = true;
e528997a 1491 } else if (empty($this->get_instance()->preventsubmissionnotingroup)) {
bc006a66
DM
1492 $groups[0] = true;
1493 }
1494 }
12a1a0da 1495
bc006a66 1496 $count = count($groups);
12a1a0da 1497 }
bc006a66 1498
12a1a0da
DW
1499 return $count;
1500 }
1501
bbd0e548 1502 /**
c494e18c 1503 * Load a count of active users enrolled in the current course with the specified permission and group.
e5403f8c 1504 * 0 for no group.
bbd0e548
DW
1505 *
1506 * @param int $currentgroup
1507 * @return int number of matching users
1508 */
1509 public function count_participants($currentgroup) {
a0e59f04 1510 return count($this->list_participants($currentgroup, true));
bbd0e548
DW
1511 }
1512
f70079b9 1513 /**
c494e18c 1514 * Load a count of active users submissions in the current module that require grading
f70079b9 1515 * This means the submission modification time is more recent than the
7a9fd6da 1516 * grading modification time and the status is SUBMITTED.
f70079b9
DW
1517 *
1518 * @return int number of matching submissions
1519 */
1520 public function count_submissions_need_grading() {
1521 global $DB;
1522
8f7e1c05
DW
1523 if ($this->get_instance()->teamsubmission) {
1524 // This does not make sense for group assignment because the submission is shared.
1525 return 0;
1526 }
1527
1528 $currentgroup = groups_get_activity_group($this->get_course_module(), true);
c494e18c 1529 list($esql, $params) = get_enrolled_sql($this->get_context(), 'mod/assign:submit', $currentgroup, true);
8f7e1c05
DW
1530
1531 $params['assignid'] = $this->get_instance()->id;
1532 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1533
1534 $sql = 'SELECT COUNT(s.userid)
1535 FROM {assign_submission} s
1536 LEFT JOIN {assign_grades} g ON
1537 s.assignment = g.assignment AND
df211804 1538 s.userid = g.userid AND
ad030ab4 1539 g.attemptnumber = s.attemptnumber
0c7b6910 1540 JOIN(' . $esql . ') e ON e.id = s.userid
8f7e1c05 1541 WHERE
ad030ab4 1542 s.latest = 1 AND
8f7e1c05
DW
1543 s.assignment = :assignid AND
1544 s.timemodified IS NOT NULL AND
1545 s.status = :submitted AND
ab848fea 1546 (s.timemodified >= g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL)';
f70079b9 1547
8f7e1c05 1548 return $DB->count_records_sql($sql, $params);
f70079b9
DW
1549 }
1550
bbd0e548 1551 /**
e5403f8c 1552 * Load a count of grades.
b473171a
DW
1553 *
1554 * @return int number of grades
1555 */
1556 public function count_grades() {
1557 global $DB;
1558
1559 if (!$this->has_instance()) {
1560 return 0;
1561 }
1562
8f7e1c05 1563 $currentgroup = groups_get_activity_group($this->get_course_module(), true);
c494e18c 1564 list($esql, $params) = get_enrolled_sql($this->get_context(), 'mod/assign:submit', $currentgroup, true);
8f7e1c05
DW
1565
1566 $params['assignid'] = $this->get_instance()->id;
1567
1568 $sql = 'SELECT COUNT(g.userid)
1569 FROM {assign_grades} g
0c7b6910 1570 JOIN(' . $esql . ') e ON e.id = g.userid
8f7e1c05 1571 WHERE g.assignment = :assignid';
b473171a
DW
1572
1573 return $DB->count_records_sql($sql, $params);
1574 }
1575
1576 /**
e5403f8c 1577 * Load a count of submissions.
b473171a 1578 *
ab14ab74 1579 * @param bool $includenew When true, also counts the submissions with status 'new'.
b473171a
DW
1580 * @return int number of submissions
1581 */
ab14ab74 1582 public function count_submissions($includenew = false) {
b473171a
DW
1583 global $DB;
1584
1585 if (!$this->has_instance()) {
1586 return 0;
1587 }
1588
8f7e1c05 1589 $params = array();
ab14ab74
FM
1590 $sqlnew = '';
1591
1592 if (!$includenew) {
1593 $sqlnew = ' AND s.status <> :status ';
1594 $params['status'] = ASSIGN_SUBMISSION_STATUS_NEW;
1595 }
b473171a
DW
1596
1597 if ($this->get_instance()->teamsubmission) {
8f7e1c05 1598 // We cannot join on the enrolment tables for group submissions (no userid).
df211804 1599 $sql = 'SELECT COUNT(DISTINCT s.groupid)
8f7e1c05
DW
1600 FROM {assign_submission} s
1601 WHERE
1602 s.assignment = :assignid AND
1603 s.timemodified IS NOT NULL AND
ab14ab74
FM
1604 s.userid = :groupuserid' .
1605 $sqlnew;
8f7e1c05
DW
1606
1607 $params['assignid'] = $this->get_instance()->id;
1608 $params['groupuserid'] = 0;
1609 } else {
1610 $currentgroup = groups_get_activity_group($this->get_course_module(), true);
ab14ab74 1611 list($esql, $enrolparams) = get_enrolled_sql($this->get_context(), 'mod/assign:submit', $currentgroup, true);
8f7e1c05 1612
ab14ab74 1613 $params = array_merge($params, $enrolparams);
8f7e1c05
DW
1614 $params['assignid'] = $this->get_instance()->id;
1615
df211804 1616 $sql = 'SELECT COUNT(DISTINCT s.userid)
8f7e1c05 1617 FROM {assign_submission} s
0c7b6910 1618 JOIN(' . $esql . ') e ON e.id = s.userid
8f7e1c05
DW
1619 WHERE
1620 s.assignment = :assignid AND
ab14ab74
FM
1621 s.timemodified IS NOT NULL ' .
1622 $sqlnew;
4c4c7b3f 1623
b473171a 1624 }
8f7e1c05 1625
b473171a
DW
1626 return $DB->count_records_sql($sql, $params);
1627 }
1628
1629 /**
e5403f8c 1630 * Load a count of submissions with a specified status.
bbd0e548
DW
1631 *
1632 * @param string $status The submission status - should match one of the constants
1633 * @return int number of matching submissions
1634 */
1635 public function count_submissions_with_status($status) {
1636 global $DB;
8f7e1c05
DW
1637
1638 $currentgroup = groups_get_activity_group($this->get_course_module(), true);
c494e18c 1639 list($esql, $params) = get_enrolled_sql($this->get_context(), 'mod/assign:submit', $currentgroup, true);
8f7e1c05
DW
1640
1641 $params['assignid'] = $this->get_instance()->id;
df211804 1642 $params['assignid2'] = $this->get_instance()->id;
8f7e1c05 1643 $params['submissionstatus'] = $status;
12a1a0da
DW
1644
1645 if ($this->get_instance()->teamsubmission) {
bc006a66
DM
1646
1647 $groupsstr = '';
1648 if ($currentgroup != 0) {
1649 // If there is an active group we should only display the current group users groups.
1650 $participants = $this->list_participants($currentgroup, true);
1651 $groups = groups_get_all_groups($this->get_course()->id,
1652 array_keys($participants),
1653 $this->get_instance()->teamsubmissiongroupingid,
1654 'DISTINCT g.id, g.name');
1655 list($groupssql, $groupsparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
1656 $groupsstr = 's.groupid ' . $groupssql . ' AND';
1657 $params = $params + $groupsparams;
1658 }
8f7e1c05
DW
1659 $sql = 'SELECT COUNT(s.groupid)
1660 FROM {assign_submission} s
1661 WHERE
ad030ab4 1662 s.latest = 1 AND
8f7e1c05
DW
1663 s.assignment = :assignid AND
1664 s.timemodified IS NOT NULL AND
bc006a66
DM
1665 s.userid = :groupuserid AND '
1666 . $groupsstr . '
8f7e1c05
DW
1667 s.status = :submissionstatus';
1668 $params['groupuserid'] = 0;
1669 } else {
1670 $sql = 'SELECT COUNT(s.userid)
1671 FROM {assign_submission} s
0c7b6910 1672 JOIN(' . $esql . ') e ON e.id = s.userid
8f7e1c05 1673 WHERE
ad030ab4 1674 s.latest = 1 AND
8f7e1c05
DW
1675 s.assignment = :assignid AND
1676 s.timemodified IS NOT NULL AND
1677 s.status = :submissionstatus';
4c4c7b3f 1678
12a1a0da 1679 }
8f7e1c05 1680
12a1a0da 1681 return $DB->count_records_sql($sql, $params);
bbd0e548
DW
1682 }
1683
1684 /**
1685 * Utility function to get the userid for every row in the grading table
e5403f8c 1686 * so the order can be frozen while we iterate it.
bbd0e548
DW
1687 *
1688 * @return array An array of userids
1689 */
47f48152 1690 protected function get_grading_userid_list() {
bbd0e548 1691 $filter = get_user_preferences('assign_filter', '');
bf78ebd6 1692 $table = new assign_grading_table($this, 0, $filter, 0, false);
bbd0e548
DW
1693
1694 $useridlist = $table->get_column_data('userid');
1695
1696 return $useridlist;
1697 }
1698
bbd0e548 1699 /**
e5403f8c 1700 * Generate zip file from array of given files.
bbd0e548 1701 *
e5403f8c
DW
1702 * @param array $filesforzipping - array of files to pass into archive_to_pathname.
1703 * This array is indexed by the final file name and each
1704 * element in the array is an instance of a stored_file object.
1705 * @return path of temp file - note this returned file does
1706 * not have a .zip extension - it is a temp file.
bbd0e548 1707 */
47f48152 1708 protected function pack_files($filesforzipping) {
e5403f8c
DW
1709 global $CFG;
1710 // Create path for new zip file.
1711 $tempzip = tempnam($CFG->tempdir . '/', 'assignment_');
1712 // Zip files.
1713 $zipper = new zip_packer();
1714 if ($zipper->archive_to_pathname($filesforzipping, $tempzip)) {
1715 return $tempzip;
bbd0e548 1716 }
e5403f8c 1717 return false;
bbd0e548
DW
1718 }
1719
bbd0e548 1720 /**
3f7b501e
SH
1721 * Finds all assignment notifications that have yet to be mailed out, and mails them.
1722 *
e5403f8c 1723 * Cron function to be run periodically according to the moodle cron.
bbd0e548
DW
1724 *
1725 * @return bool
1726 */
e5403f8c 1727 public static function cron() {
3f7b501e 1728 global $DB;
75f87a57 1729
e5403f8c 1730 // Only ever send a max of one days worth of updates.
75f87a57
DW
1731 $yesterday = time() - (24 * 3600);
1732 $timenow = time();
abd0f0ae 1733 $lastcron = $DB->get_field('modules', 'lastcron', array('name' => 'assign'));
75f87a57 1734
70cfc878
JF
1735 // Collect all submissions that require mailing.
1736 // Submissions are included if all are true:
1737 // - The assignment is visible in the gradebook.
1738 // - No previous notification has been sent.
1739 // - If marking workflow is not enabled, the grade was updated in the past 24 hours, or
1740 // if marking workflow is enabled, the workflow state is at 'released'.
86dced43
TB
1741 $sql = "SELECT g.id as gradeid, a.course, a.name, a.blindmarking, a.revealidentities,
1742 g.*, g.timemodified as lastmodified, cm.id as cmid
3f7b501e
SH
1743 FROM {assign} a
1744 JOIN {assign_grades} g ON g.assignment = a.id
fdd3ebc5 1745 LEFT JOIN {assign_user_flags} uf ON uf.assignment = a.id AND uf.userid = g.userid
86dced43
TB
1746 JOIN {course_modules} cm ON cm.course = a.course AND cm.instance = a.id
1747 JOIN {modules} md ON md.id = cm.module AND md.name = 'assign'
fdd3ebc5 1748 JOIN {grade_items} gri ON gri.iteminstance = a.id AND gri.courseid = a.course AND gri.itemmodule = md.name
70cfc878
JF
1749 WHERE ((a.markingworkflow = 0 AND g.timemodified >= :yesterday AND g.timemodified <= :today) OR
1750 (a.markingworkflow = 1 AND uf.workflowstate = :wfreleased)) AND
f33be9eb
TB
1751 uf.mailed = 0 AND gri.hidden = 0
1752 ORDER BY a.course, cm.id";
e5403f8c 1753
70cfc878
JF
1754 $params = array(
1755 'yesterday' => $yesterday,
1756 'today' => $timenow,
1757 'wfreleased' => ASSIGN_MARKING_WORKFLOW_STATE_RELEASED,
1758 );
3f7b501e 1759 $submissions = $DB->get_records_sql($sql, $params);
75f87a57 1760
ae7638f7 1761 if (!empty($submissions)) {
c8314005 1762
ae7638f7 1763 mtrace('Processing ' . count($submissions) . ' assignment submissions ...');
75f87a57 1764
ae7638f7
DW
1765 // Preload courses we are going to need those.
1766 $courseids = array();
1767 foreach ($submissions as $submission) {
1768 $courseids[] = $submission->course;
1769 }
e5403f8c 1770
ae7638f7
DW
1771 // Filter out duplicates.
1772 $courseids = array_unique($courseids);
1773 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
1774 list($courseidsql, $params) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
1775 $sql = 'SELECT c.*, ' . $ctxselect .
1776 ' FROM {course} c
1777 LEFT JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
1778 WHERE c.id ' . $courseidsql;
e5403f8c 1779
ae7638f7
DW
1780 $params['contextlevel'] = CONTEXT_COURSE;
1781 $courses = $DB->get_records_sql($sql, $params);
e5403f8c 1782
ae7638f7
DW
1783 // Clean up... this could go on for a while.
1784 unset($courseids);
1785 unset($ctxselect);
1786 unset($courseidsql);
1787 unset($params);
3f7b501e 1788
ae7638f7
DW
1789 // Message students about new feedback.
1790 foreach ($submissions as $submission) {
75f87a57 1791
ae7638f7 1792 mtrace("Processing assignment submission $submission->id ...");
75f87a57 1793
ae7638f7
DW
1794 // Do not cache user lookups - could be too many.
1795 if (!$user = $DB->get_record('user', array('id'=>$submission->userid))) {
1796 mtrace('Could not find user ' . $submission->userid);
1797 continue;
1798 }
75f87a57 1799
ae7638f7
DW
1800 // Use a cache to prevent the same DB queries happening over and over.
1801 if (!array_key_exists($submission->course, $courses)) {
1802 mtrace('Could not find course ' . $submission->course);
1803 continue;
1804 }
1805 $course = $courses[$submission->course];
1806 if (isset($course->ctxid)) {
1807 // Context has not yet been preloaded. Do so now.
1808 context_helper::preload_from_record($course);
1809 }
75f87a57 1810
ae7638f7
DW
1811 // Override the language and timezone of the "current" user, so that
1812 // mail is customised for the receiver.
1813 cron_setup_user($user, $course);
1814
1815 // Context lookups are already cached.
1816 $coursecontext = context_course::instance($course->id);
1817 if (!is_enrolled($coursecontext, $user->id)) {
1818 $courseshortname = format_string($course->shortname,
1819 true,
1820 array('context' => $coursecontext));
1821 mtrace(fullname($user) . ' not an active participant in ' . $courseshortname);
1822 continue;
1823 }
75f87a57 1824
ae7638f7
DW
1825 if (!$grader = $DB->get_record('user', array('id'=>$submission->grader))) {
1826 mtrace('Could not find grader ' . $submission->grader);
1827 continue;
1828 }
75f87a57 1829
ccd6839a 1830 $modinfo = get_fast_modinfo($course, $user->id);
86dced43 1831 $cm = $modinfo->get_cm($submission->cmid);
ae7638f7 1832 // Context lookups are already cached.
86dced43 1833 $contextmodule = context_module::instance($cm->id);
ae7638f7 1834
ccd6839a
TB
1835 if (!$cm->uservisible) {
1836 // Hold mail notification for assignments the user cannot access until later.
75f87a57
DW
1837 continue;
1838 }
bbd0e548 1839
ae7638f7
DW
1840 // Need to send this to the student.
1841 $messagetype = 'feedbackavailable';
1842 $eventtype = 'assign_notification';
1843 $updatetime = $submission->lastmodified;
1844 $modulename = get_string('modulename', 'assign');
75f87a57 1845
ae7638f7
DW
1846 $uniqueid = 0;
1847 if ($submission->blindmarking && !$submission->revealidentities) {
1848 $uniqueid = self::get_uniqueid_for_user_static($submission->assignment, $user->id);
1849 }
1850 $showusers = $submission->blindmarking && !$submission->revealidentities;
1851 self::send_assignment_notification($grader,
1852 $user,
1853 $messagetype,
1854 $eventtype,
1855 $updatetime,
86dced43 1856 $cm,
ae7638f7
DW
1857 $contextmodule,
1858 $course,
1859 $modulename,
1860 $submission->name,
1861 $showusers,
1862 $uniqueid);
1863
1864 $flags = $DB->get_record('assign_user_flags', array('userid'=>$user->id, 'assignment'=>$submission->assignment));
1865 if ($flags) {
1866 $flags->mailed = 1;
1867 $DB->update_record('assign_user_flags', $flags);
1868 } else {
1869 $flags = new stdClass();
1870 $flags->userid = $user->id;
1871 $flags->assignment = $submission->assignment;
1872 $flags->mailed = 1;
1873 $DB->insert_record('assign_user_flags', $flags);
1874 }
b473171a 1875
ae7638f7 1876 mtrace('Done');
df211804 1877 }
ae7638f7 1878 mtrace('Done processing ' . count($submissions) . ' assignment submissions');
75f87a57 1879
ae7638f7
DW
1880 cron_setup_user();
1881
1882 // Free up memory just to be sure.
1883 unset($courses);
75f87a57 1884 }
75f87a57 1885
ae7638f7
DW
1886 // Update calendar events to provide a description.
1887 $sql = 'SELECT id
1888 FROM {assign}
1889 WHERE
1890 allowsubmissionsfromdate >= :lastcron AND
1891 allowsubmissionsfromdate <= :timenow AND
1892 alwaysshowdescription = 0';
1893 $params = array('lastcron' => $lastcron, 'timenow' => $timenow);
1894 $newlyavailable = $DB->get_records_sql($sql, $params);
1895 foreach ($newlyavailable as $record) {
1896 $cm = get_coursemodule_from_instance('assign', $record->id, 0, false, MUST_EXIST);
1897 $context = context_module::instance($cm->id);
3f7b501e 1898
ae7638f7
DW
1899 $assignment = new assign($context, null, null);
1900 $assignment->update_calendar($cm->id);
1901 }
bbd0e548
DW
1902
1903 return true;
1904 }
1905
d6c673ed
DW
1906 /**
1907 * Mark in the database that this grade record should have an update notification sent by cron.
1908 *
1909 * @param stdClass $grade a grade record keyed on id
a4b10a52 1910 * @param bool $mailedoverride when true, flag notification to be sent again.
d6c673ed
DW
1911 * @return bool true for success
1912 */
a4b10a52 1913 public function notify_grade_modified($grade, $mailedoverride = false) {
d6c673ed
DW
1914 global $DB;
1915
df211804 1916 $flags = $this->get_user_flags($grade->userid, true);
a4b10a52 1917 if ($flags->mailed != 1 || $mailedoverride) {
df211804 1918 $flags->mailed = 0;
d6c673ed
DW
1919 }
1920
df211804
DW
1921 return $this->update_user_flags($flags);
1922 }
1923
1924 /**
1925 * Update user flags for this user in this assignment.
1926 *
1927 * @param stdClass $flags a flags record keyed on id
1928 * @return bool true for success
1929 */
1930 public function update_user_flags($flags) {
1931 global $DB;
1932 if ($flags->userid <= 0 || $flags->assignment <= 0 || $flags->id <= 0) {
1933 return false;
1934 }
1935
1936 $result = $DB->update_record('assign_user_flags', $flags);
1937 return $result;
d6c673ed
DW
1938 }
1939
bbd0e548 1940 /**
e5403f8c 1941 * Update a grade in the grade table for the assignment and in the gradebook.
bbd0e548
DW
1942 *
1943 * @param stdClass $grade a grade record keyed on id
ab38cb28 1944 * @param bool $reopenattempt If the attempt reopen method is manual, allow another attempt at this assignment.
bbd0e548
DW
1945 * @return bool true for success
1946 */
ab38cb28 1947 public function update_grade($grade, $reopenattempt = false) {
bbd0e548
DW
1948 global $DB;
1949
1950 $grade->timemodified = time();
1951
f8d107b3
DM
1952 if (!empty($grade->workflowstate)) {
1953 $validstates = $this->get_marking_workflow_states_for_current_user();
1954 if (!array_key_exists($grade->workflowstate, $validstates)) {
1955 return false;
1956 }
1957 }
1958
bbd0e548
DW
1959 if ($grade->grade && $grade->grade != -1) {
1960 if ($this->get_instance()->grade > 0) {
1961 if (!is_numeric($grade->grade)) {
1962 return false;
1963 } else if ($grade->grade > $this->get_instance()->grade) {
1964 return false;
1965 } else if ($grade->grade < 0) {
1966 return false;
1967 }
1968 } else {
e5403f8c 1969 // This is a scale.
bbd0e548
DW
1970 if ($scale = $DB->get_record('scale', array('id' => -($this->get_instance()->grade)))) {
1971 $scaleoptions = make_menu_from_list($scale->scale);
1972 if (!array_key_exists((int) $grade->grade, $scaleoptions)) {
1973 return false;
1974 }
1975 }
1976 }
1977 }
1978
a8fdb36c
DW
1979 if (empty($grade->attemptnumber)) {
1980 // Set it to the default.
1981 $grade->attemptnumber = 0;
1982 }
e77bacab 1983 $DB->update_record('assign_grades', $grade);
df211804 1984
df211804
DW
1985 $submission = null;
1986 if ($this->get_instance()->teamsubmission) {
1987 $submission = $this->get_group_submission($grade->userid, 0, false);
1988 } else {
1989 $submission = $this->get_user_submission($grade->userid, false);
1990 }
767fda9b
GF
1991
1992 // Only push to gradebook if the update is for the latest attempt.
df211804
DW
1993 // Not the latest attempt.
1994 if ($submission && $submission->attemptnumber != $grade->attemptnumber) {
1995 return true;
1996 }
1997
bd3ee807
MN
1998 if ($this->gradebook_item_update(null, $grade)) {
1999 \mod_assign\event\submission_graded::create_from_grade($this, $grade)->trigger();
2000 }
e77bacab
MP
2001
2002 // If the conditions are met, allow another attempt.
2003 if ($submission) {
2004 $this->reopen_submission_if_required($grade->userid,
2005 $submission,
2006 $reopenattempt);
bbd0e548 2007 }
bd3ee807 2008
e77bacab 2009 return true;
bbd0e548
DW
2010 }
2011
9e795179 2012 /**
e5403f8c 2013 * View the grant extension date page.
9e795179
DW
2014 *
2015 * Uses url parameters 'userid'
2016 * or from parameter 'selectedusers'
e5403f8c 2017 *
9e795179
DW
2018 * @param moodleform $mform - Used for validation of the submitted data
2019 * @return string
2020 */
47f48152 2021 protected function view_grant_extension($mform) {
9e795179
DW
2022 global $DB, $CFG;
2023 require_once($CFG->dirroot . '/mod/assign/extensionform.php');
2024
2025 $o = '';
26670f5e 2026
9e795179 2027 $data = new stdClass();
26670f5e 2028 $data->id = $this->get_course_module()->id;
9e795179 2029
26670f5e
JF
2030 $formparams = array(
2031 'instance' => $this->get_instance()
2032 );
2033
2034 $extrauserfields = get_extra_user_fields($this->get_context());
2035
2036 if ($mform) {
2037 $submitteddata = $mform->get_data();
2038 $users = $submitteddata->selectedusers;
2039 $userlist = explode(',', $users);
2040
2041 $data->selectedusers = $users;
2042 $data->userid = 0;
9e795179 2043
26670f5e
JF
2044 $usershtml = '';
2045 $usercount = 0;
2046 foreach ($userlist as $userid) {
2047 if ($usercount >= 5) {
2048 $usershtml .= get_string('moreusers', 'assign', count($userlist) - 5);
2049 break;
2050 }
2051 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
2052
2053 $usershtml .= $this->get_renderer()->render(new assign_user_summary($user,
2054 $this->get_course()->id,
2055 has_capability('moodle/site:viewfullnames',
2056 $this->get_course_context()),
2057 $this->is_blind_marking(),
2058 $this->get_uniqueid_for_user($user->id),
2059 $extrauserfields,
2060 !$this->is_active_user($userid)));
2061 $usercount += 1;
2062 }
2063
2064 $formparams['userscount'] = count($userlist);
2065 $formparams['usershtml'] = $usershtml;
2066
2067 } else {
2068 $userid = required_param('userid', PARAM_INT);
9e795179 2069 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
26670f5e 2070 $flags = $this->get_user_flags($userid, false);
9e795179 2071
26670f5e 2072 $data->userid = $user->id;
9f65f9c3
DW
2073 if ($flags) {
2074 $data->extensionduedate = $flags->extensionduedate;
9e795179 2075 }
26670f5e
JF
2076
2077 $usershtml = $this->get_renderer()->render(new assign_user_summary($user,
2078 $this->get_course()->id,
2079 has_capability('moodle/site:viewfullnames',
2080 $this->get_course_context()),
2081 $this->is_blind_marking(),
2082 $this->get_uniqueid_for_user($user->id),
2083 $extrauserfields,
2084 !$this->is_active_user($userid)));
2085 $formparams['usershtml'] = $usershtml;
9e795179 2086 }
26670f5e
JF
2087
2088 $mform = new mod_assign_extension_form(null, $formparams);
2089 $mform->set_data($data);
e5403f8c
DW
2090 $header = new assign_header($this->get_instance(),
2091 $this->get_context(),
2092 $this->show_intro(),
2093 $this->get_course_module()->id,
2094 get_string('grantextension', 'assign'));
2095 $o .= $this->get_renderer()->render($header);
49d83b9d 2096 $o .= $this->get_renderer()->render(new assign_form('extensionform', $mform));
9e795179
DW
2097 $o .= $this->view_footer();
2098 return $o;
2099 }
2100
12a1a0da 2101 /**
e5403f8c 2102 * Get a list of the users in the same group as this user.
12a1a0da
DW
2103 *
2104 * @param int $groupid The id of the group whose members we want or 0 for the default group
2105 * @param bool $onlyids Whether to retrieve only the user id's
b2885704 2106 * @param bool $excludesuspended Whether to exclude suspended users
12a1a0da
DW
2107 * @return array The users (possibly id's only)
2108 */
b2885704 2109 public function get_submission_group_members($groupid, $onlyids, $excludesuspended = false) {
12a1a0da
DW
2110 $members = array();
2111 if ($groupid != 0) {
2112 if ($onlyids) {
2113 $allusers = groups_get_members($groupid, 'u.id');
2114 } else {
2115 $allusers = groups_get_members($groupid);
2116 }
2117 foreach ($allusers as $user) {
2118 if ($this->get_submission_group($user->id)) {
2119 $members[] = $user;
2120 }
2121 }
2122 } else {
2123 $allusers = $this->list_participants(null, $onlyids);
2124 foreach ($allusers as $user) {
2125 if ($this->get_submission_group($user->id) == null) {
2126 $members[] = $user;
2127 }
2128 }
2129 }
4c4c7b3f 2130 // Exclude suspended users, if user can't see them.
b2885704 2131 if ($excludesuspended || !has_capability('moodle/course:viewsuspendedusers', $this->context)) {
4c4c7b3f 2132 foreach ($members as $key => $member) {
1ecb8044 2133 if (!$this->is_active_user($member->id)) {
4c4c7b3f
RT
2134 unset($members[$key]);
2135 }
2136 }
2137 }
bc006a66 2138
12a1a0da
DW
2139 return $members;
2140 }
2141
2142 /**
e5403f8c 2143 * Get a list of the users in the same group as this user that have not submitted the assignment.
12a1a0da
DW
2144 *
2145 * @param int $groupid The id of the group whose members we want or 0 for the default group
2146 * @param bool $onlyids Whether to retrieve only the user id's
2147 * @return array The users (possibly id's only)
2148 */
2149 public function get_submission_group_members_who_have_not_submitted($groupid, $onlyids) {
e5403f8c
DW
2150 $instance = $this->get_instance();
2151 if (!$instance->teamsubmission || !$instance->requireallteammemberssubmit) {
12a1a0da
DW
2152 return array();
2153 }
2154 $members = $this->get_submission_group_members($groupid, $onlyids);
2155
2156 foreach ($members as $id => $member) {
2157 $submission = $this->get_user_submission($member->id, false);
df211804 2158 if ($submission && $submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
12a1a0da 2159 unset($members[$id]);
88cfe469
DW
2160 } else {
2161 if ($this->is_blind_marking()) {
e5403f8c
DW
2162 $members[$id]->alias = get_string('hiddenuser', 'assign') .
2163 $this->get_uniqueid_for_user($id);
88cfe469 2164 }
12a1a0da
DW
2165 }
2166 }
2167 return $members;
2168 }
2169
2170 /**
e5403f8c 2171 * Load the group submission object for a particular user, optionally creating it if required.
12a1a0da
DW
2172 *
2173 * @param int $userid The id of the user whose submission we want
e5403f8c
DW
2174 * @param int $groupid The id of the group for this user - may be 0 in which
2175 * case it is determined from the userid.
12a1a0da 2176 * @param bool $create If set to true a new submission object will be created in the database
9e3eee67 2177 * with the status set to "new".
df211804 2178 * @param int $attemptnumber - -1 means the latest attempt
12a1a0da
DW
2179 * @return stdClass The submission
2180 */
df211804 2181 public function get_group_submission($userid, $groupid, $create, $attemptnumber=-1) {
12a1a0da
DW
2182 global $DB;
2183
2184 if ($groupid == 0) {
2185 $group = $this->get_submission_group($userid);
2186 if ($group) {
2187 $groupid = $group->id;
2188 }
2189 }
2190
12a1a0da
DW
2191 // Now get the group submission.
2192 $params = array('assignment'=>$this->get_instance()->id, 'groupid'=>$groupid, 'userid'=>0);
df211804
DW
2193 if ($attemptnumber >= 0) {
2194 $params['attemptnumber'] = $attemptnumber;
2195 }
2196
2197 // Only return the row with the highest attemptnumber.
2198 $submission = null;
2199 $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
2200 if ($submissions) {
2201 $submission = reset($submissions);
2202 }
12a1a0da
DW
2203
2204 if ($submission) {
2205 return $submission;
2206 }
2207 if ($create) {
2208 $submission = new stdClass();
e5403f8c
DW
2209 $submission->assignment = $this->get_instance()->id;
2210 $submission->userid = 0;
2211 $submission->groupid = $groupid;
12a1a0da
DW
2212 $submission->timecreated = time();
2213 $submission->timemodified = $submission->timecreated;
df211804
DW
2214 if ($attemptnumber >= 0) {
2215 $submission->attemptnumber = $attemptnumber;
12a1a0da 2216 } else {
df211804 2217 $submission->attemptnumber = 0;
12a1a0da 2218 }
1523f9e0
DW
2219 // Work out if this is the latest submission.
2220 $submission->latest = 0;
2221 $params = array('assignment'=>$this->get_instance()->id, 'groupid'=>$groupid, 'userid'=>0);
2222 if ($attemptnumber == -1) {
2223 // This is a new submission so it must be the latest.
2224 $submission->latest = 1;
2225 } else {
2226 // We need to work this out.
2227 $result = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', 'attemptnumber', 0, 1);
2228 if ($result) {
2229 $latestsubmission = reset($result);
2230 }
2231 if (!$latestsubmission || ($attemptnumber == $latestsubmission->attemptnumber)) {
2232 $submission->latest = 1;
2233 }
2234 }
2235 if ($submission->latest) {
2236 // This is the case when we need to set latest to 0 for all the other attempts.
2237 $DB->set_field('assign_submission', 'latest', 0, $params);
2238 }
9e3eee67 2239 $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
12a1a0da 2240 $sid = $DB->insert_record('assign_submission', $submission);
4781ff2e 2241 return $DB->get_record('assign_submission', array('id' => $sid));
12a1a0da
DW
2242 }
2243 return false;
2244 }
2245
df47b77f 2246 /**
64220210
DW
2247 * View a summary listing of all assignments in the current course.
2248 *
2249 * @return string
2250 */
2251 private function view_course_index() {
2252 global $USER;
2253
2254 $o = '';
2255
2256 $course = $this->get_course();
2257 $strplural = get_string('modulenameplural', 'assign');
2258
2259 if (!$cms = get_coursemodules_in_course('assign', $course->id, 'm.duedate')) {
2260 $o .= $this->get_renderer()->notification(get_string('thereareno', 'moodle', $strplural));
2261 $o .= $this->get_renderer()->continue_button(new moodle_url('/course/view.php', array('id' => $course->id)));
2262 return $o;
2263 }
2264
09af1e28 2265 $strsectionname = '';
64220210
DW
2266 $usesections = course_format_uses_sections($course->format);
2267 $modinfo = get_fast_modinfo($course);
2268
2269 if ($usesections) {
09af1e28 2270 $strsectionname = get_string('sectionname', 'format_'.$course->format);
64220210
DW
2271 $sections = $modinfo->get_section_info_all();
2272 }
2273 $courseindexsummary = new assign_course_index_summary($usesections, $strsectionname);
2274
2275 $timenow = time();
2276
2277 $currentsection = '';
2278 foreach ($modinfo->instances['assign'] as $cm) {
2279 if (!$cm->uservisible) {
2280 continue;
2281 }
2282
e5403f8c 2283 $timedue = $cms[$cm->id]->duedate;
64220210
DW
2284
2285 $sectionname = '';
2286 if ($usesections && $cm->sectionnum) {
2287 $sectionname = get_section_name($course, $sections[$cm->sectionnum]);
2288 }
2289
2290 $submitted = '';
2291 $context = context_module::instance($cm->id);
2292
2293 $assignment = new assign($context, $cm, $course);
2294
2295 if (has_capability('mod/assign:grade', $context)) {
2296 $submitted = $assignment->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED);
2297
2298 } else if (has_capability('mod/assign:submit', $context)) {
2299 $usersubmission = $assignment->get_user_submission($USER->id, false);
2300
2301 if (!empty($usersubmission->status)) {
2302 $submitted = get_string('submissionstatus_' . $usersubmission->status, 'assign');
2303 } else {
2304 $submitted = get_string('submissionstatus_', 'assign');
2305 }
2306 }
539af602
DW
2307 $gradinginfo = grade_get_grades($course->id, 'mod', 'assign', $cm->instance, $USER->id);
2308 if (isset($gradinginfo->items[0]->grades[$USER->id]) &&
2309 !$gradinginfo->items[0]->grades[$USER->id]->hidden ) {
2310 $grade = $gradinginfo->items[0]->grades[$USER->id]->str_grade;
64220210
DW
2311 } else {
2312 $grade = '-';
2313 }
2314
2315 $courseindexsummary->add_assign_info($cm->id, $cm->name, $sectionname, $timedue, $submitted, $grade);
2316
2317 }
2318
2319 $o .= $this->get_renderer()->render($courseindexsummary);
2320 $o .= $this->view_footer();
2321
2322 return $o;
2323 }
2324
2325 /**
2326 * View a page rendered by a plugin.
df47b77f 2327 *
e5403f8c 2328 * Uses url parameters 'pluginaction', 'pluginsubtype', 'plugin', and 'id'.
df47b77f
DW
2329 *
2330 * @return string
2331 */
47f48152 2332 protected function view_plugin_page() {
df47b77f
DW
2333 global $USER;
2334
2335 $o = '';
2336
2337 $pluginsubtype = required_param('pluginsubtype', PARAM_ALPHA);
2338 $plugintype = required_param('plugin', PARAM_TEXT);
2339 $pluginaction = required_param('pluginaction', PARAM_ALPHA);
2340
2341 $plugin = $this->get_plugin_by_type($pluginsubtype, $plugintype);
2342 if (!$plugin) {
2343 print_error('invalidformdata', '');
2344 return;
2345 }
2346
2347 $o .= $plugin->view_page($pluginaction);
2348
2349 return $o;
2350 }
2351
2352
12a1a0da
DW
2353 /**
2354 * This is used for team assignments to get the group for the specified user.
2355 * If the user is a member of multiple or no groups this will return false
2356 *
2357 * @param int $userid The id of the user whose submission we want
2358 * @return mixed The group or false
2359 */
2360 public function get_submission_group($userid) {
bc006a66
DM
2361
2362 if (isset($this->usersubmissiongroups[$userid])) {
2363 return $this->usersubmissiongroups[$userid];
2364 }
2365
ee5d4eef 2366 $groups = $this->get_all_groups($userid);
12a1a0da 2367 if (count($groups) != 1) {
bc006a66
DM
2368 $return = false;
2369 } else {
2370 $return = array_pop($groups);
12a1a0da 2371 }
bc006a66
DM
2372
2373 // Cache the user submission group.
2374 $this->usersubmissiongroups[$userid] = $return;
2375
2376 return $return;
12a1a0da
DW
2377 }
2378
a059ef2e
DP
2379 /**
2380 * Gets all groups the user is a member of.
2381 *
2382 * @param int $userid Teh id of the user who's groups we are checking
2383 * @return array The group objects
2384 */
ce449f63 2385 public function get_all_groups($userid) {
ee5d4eef
AH
2386 if (isset($this->usergroups[$userid])) {
2387 return $this->usergroups[$userid];
2388 }
2389
2390 $grouping = $this->get_instance()->teamsubmissiongroupingid;
2391 $return = groups_get_all_groups($this->get_course()->id, $userid, $grouping);
2392
2393 $this->usergroups[$userid] = $return;
2394
2395 return $return;
2396 }
2397
9e795179 2398
bbd0e548 2399 /**
e5403f8c
DW
2400 * Display the submission that is used by a plugin.
2401 *
2402 * Uses url parameters 'sid', 'gid' and 'plugin'.
2403 *
bbd0e548
DW
2404 * @param string $pluginsubtype
2405 * @return string
2406 */
47f48152 2407 protected function view_plugin_content($pluginsubtype) {
bbd0e548
DW
2408 $o = '';
2409
2410 $submissionid = optional_param('sid', 0, PARAM_INT);
2411 $gradeid = optional_param('gid', 0, PARAM_INT);
2412 $plugintype = required_param('plugin', PARAM_TEXT);
2413 $item = null;
2414 if ($pluginsubtype == 'assignsubmission') {
2415 $plugin = $this->get_submission_plugin_by_type($plugintype);
2416 if ($submissionid <= 0) {
2417 throw new coding_exception('Submission id should not be 0');
2418 }
2419 $item = $this->get_submission($submissionid);
2420
e5403f8c 2421 // Check permissions.
4a47008c 2422 $this->require_view_submission($item->userid);
49d83b9d 2423 $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
bbd0e548
DW
2424 $this->get_context(),
2425 $this->show_intro(),
2426 $this->get_course_module()->id,
2427 $plugin->get_name()));
49d83b9d 2428 $o .= $this->get_renderer()->render(new assign_submission_plugin_submission($plugin,
bbd0e548
DW
2429 $item,
2430 assign_submission_plugin_submission::FULL,
2431 $this->get_course_module()->id,
2432 $this->get_return_action(),
2433 $this->get_return_params()));
2434
1be7aef2 2435 // Trigger event for viewing a submission.
1b90858f
PS
2436 \mod_assign\event\submission_viewed::create_from_submission($this, $item)->trigger();
2437
bbd0e548
DW
2438 } else {
2439 $plugin = $this->get_feedback_plugin_by_type($plugintype);
2440 if ($gradeid <= 0) {
2441 throw new coding_exception('Grade id should not be 0');
2442 }
2443 $item = $this->get_grade($gradeid);
e5403f8c 2444 // Check permissions.
4a47008c 2445 $this->require_view_submission($item->userid);
49d83b9d 2446 $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
bbd0e548
DW
2447 $this->get_context(),
2448 $this->show_intro(),
2449 $this->get_course_module()->id,
2450 $plugin->get_name()));
49d83b9d 2451 $o .= $this->get_renderer()->render(new assign_feedback_plugin_feedback($plugin,
bbd0e548
DW
2452 $item,
2453 assign_feedback_plugin_feedback::FULL,
2454 $this->get_course_module()->id,
2455 $this->get_return_action(),
2456 $this->get_return_params()));
3290c01d
MN
2457
2458 // Trigger event for viewing feedback.
1b90858f 2459 \mod_assign\event\feedback_viewed::create_from_grade($this, $item)->trigger();
bbd0e548
DW
2460 }
2461
bbd0e548
DW
2462 $o .= $this->view_return_links();
2463
2464 $o .= $this->view_footer();
1be7aef2 2465
bbd0e548
DW
2466 return $o;
2467 }
2468
2406815b
DW
2469 /**
2470 * Rewrite plugin file urls so they resolve correctly in an exported zip.
2471 *
1561a37c 2472 * @param string $text - The replacement text
2406815b
DW
2473 * @param stdClass $user - The user record
2474 * @param assign_plugin $plugin - The assignment plugin
2475 */
2476 public function download_rewrite_pluginfile_urls($text, $user, $plugin) {
2477 $groupmode = groups_get_activity_groupmode($this->get_course_module());
2478 $groupname = '';
2479 if ($groupmode) {
2480 $groupid = groups_get_activity_group($this->get_course_module(), true);
2481 $groupname = groups_get_group_name($groupid).'-';
2482 }
2483
2484 if ($this->is_blind_marking()) {
2485 $prefix = $groupname . get_string('participant', 'assign');
2486 $prefix = str_replace('_', ' ', $prefix);
2487 $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($user->id) . '_');
2488 } else {
2489 $prefix = $groupname . fullname($user);
2490 $prefix = str_replace('_', ' ', $prefix);
2491 $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($user->id) . '_');
2492 }
2493
2494 $subtype = $plugin->get_subtype();
2495 $type = $plugin->get_type();
2496 $prefix = $prefix . $subtype . '_' . $type . '_';
2497
2498 $result = str_replace('@@PLUGINFILE@@/', $prefix, $text);
2499
2500 return $result;
2501 }
2502
bbd0e548 2503 /**
e5403f8c 2504 * Render the content in editor that is often used by plugin.
bbd0e548
DW
2505 *
2506 * @param string $filearea
2507 * @param int $submissionid
2508 * @param string $plugintype
2509 * @param string $editor
2510 * @param string $component
2511 * @return string
2512 */
2513 public function render_editor_content($filearea, $submissionid, $plugintype, $editor, $component) {
2514 global $CFG;
2515
2516 $result = '';
2517
2518 $plugin = $this->get_submission_plugin_by_type($plugintype);
2519
2520 $text = $plugin->get_editor_text($editor, $submissionid);
2521 $format = $plugin->get_editor_format($editor, $submissionid);
2522
e5403f8c
DW
2523 $finaltext = file_rewrite_pluginfile_urls($text,
2524 'pluginfile.php',
2525 $this->get_context()->id,
2526 $component,
2527 $filearea,
2528 $submissionid);
2529 $params = array('overflowdiv' => true, 'context' => $this->get_context());
2530 $result .= format_text($finaltext, $format, $params);
bbd0e548 2531
418bd23f 2532 if ($CFG->enableportfolios && has_capability('mod/assign:exportownsubmission', $this->context)) {
bbd0e548
DW
2533 require_once($CFG->libdir . '/portfoliolib.php');
2534
2535 $button = new portfolio_add_button();
e5403f8c
DW
2536 $portfolioparams = array('cmid' => $this->get_course_module()->id,
2537 'sid' => $submissionid,
2538 'plugin' => $plugintype,
2539 'editor' => $editor,
2540 'area'=>$filearea);
2541 $button->set_callback_options('assign_portfolio_caller', $portfolioparams, 'mod_assign');
bbd0e548
DW
2542 $fs = get_file_storage();
2543
e5403f8c
DW
2544 if ($files = $fs->get_area_files($this->context->id,
2545 $component,
2546 $filearea,
2547 $submissionid,
2548 'timemodified',
2549 false)) {
bbd0e548
DW
2550 $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
2551 } else {
2552 $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
2553 }
2554 $result .= $button->to_html();
2555 }
2556 return $result;
2557 }
2558
df211804 2559 /**
d9bfe3c5 2560 * Display a continue page after grading.
df211804 2561 *
d9bfe3c5 2562 * @param string $message - The message to display.
df211804
DW
2563 * @return string
2564 */
2565 protected function view_savegrading_result($message) {
2566 $o = '';
2567 $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
2568 $this->get_context(),
2569 $this->show_intro(),
2570 $this->get_course_module()->id,
2571 get_string('savegradingresult', 'assign')));
2572 $gradingresult = new assign_gradingmessage(get_string('savegradingresult', 'assign'),
2573 $message,
2574 $this->get_course_module()->id);
2575 $o .= $this->get_renderer()->render($gradingresult);
2576 $o .= $this->view_footer();
2577 return $o;
2578 }
bf78ebd6 2579 /**
d9bfe3c5 2580 * Display a continue page after quickgrading.
bf78ebd6 2581 *
d9bfe3c5 2582 * @param string $message - The message to display.
bf78ebd6
DW
2583 * @return string
2584 */
47f48152 2585 protected function view_quickgrading_result($message) {
bf78ebd6 2586 $o = '';
49d83b9d 2587 $o .= $this->get_renderer()->render(new assign_header($this->get_instance(),
bf78ebd6
DW
2588 $this->get_context(),
2589 $this->show_intro(),
2590 $this->get_course_module()->id,
2591 get_string('quickgradingresult', 'assign')));
cfdd3e5c 2592 $lastpage = optional_param('lastpage', null, PARAM_INT);
df211804
DW
2593 $gradingresult = new assign_gradingmessage(get_string('quickgradingresult', 'assign'),
2594 $message,
cfdd3e5c
GF
2595 $this->get_course_module()->id,
2596 false,
2597 $lastpage);
e5403f8c 2598 $o .= $this->get_renderer()->render($gradingresult);
bf78ebd6
DW
2599 $o .= $this->view_footer();
2600 return $o;
2601 }
bbd0e548
DW
2602
2603 /**
e5403f8c 2604 * Display the page footer.
bbd0e548 2605 *
bf78ebd6 2606 * @return string
bbd0e548 2607 */
47f48152 2608 protected function view_footer() {
1be7aef2
MN
2609 // When viewing the footer during PHPUNIT tests a set_state error is thrown.
2610 if (!PHPUNIT_TEST) {
2611 return $this->get_renderer()->render_footer();
2612 }
2613
2614 return '';
bbd0e548
DW
2615 }
2616
4a47008c
DW
2617 /**
2618 * Throw an error if the permissions to view this users submission are missing.
2619 *
2620 * @throws required_capability_exception
2621 * @return none
2622 */
2623 public function require_view_submission($userid) {
2624 if (!$this->can_view_submission($userid)) {
2625 throw new required_capability_exception($this->context, 'mod/assign:viewgrades', 'nopermission', '');
2626 }
2627 }
2628
2629 /**
2630 * Throw an error if the permissions to view grades in this assignment are missing.
2631 *
2632 * @throws required_capability_exception
2633 * @return none
2634 */
2635 public function require_view_grades() {
2636 if (!$this->can_view_grades()) {
2637 throw new required_capability_exception($this->context, 'mod/assign:viewgrades', 'nopermission', '');
2638 }
2639 }
2640
2641 /**
2642 * Does this user have view grade or grade permission for this assignment?
2643 *
2644 * @return bool
2645 */
2646 public function can_view_grades() {
2647 // Permissions check.
2648 if (!has_any_capability(array('mod/assign:viewgrades', 'mod/assign:grade'), $this->context)) {
2649 return false;
2650 }
2651
2652 return true;
2653 }
2654
bbd0e548 2655 /**
e5403f8c 2656 * Does this user have grade permission for this assignment?
bbd0e548
DW
2657 *
2658 * @return bool
2659 */
5c386472 2660 public function can_grade() {
e5403f8c 2661 // Permissions check.
bbd0e548
DW
2662 if (!has_capability('mod/assign:grade', $this->context)) {
2663 return false;
2664 }
2665
2666 return true;
2667 }
2668
2669 /**
e5403f8c 2670 * Download a zip file of all assignment submissions.
bbd0e548 2671 *
df211804 2672 * @return string - If an error occurs, this will contain the error page.
bbd0e548 2673 */
47f48152 2674 protected function download_submissions() {
e5403f8c 2675 global $CFG, $DB;
bbd0e548 2676
d0d4796b 2677 // More efficient to load this here.
bbd0e548
DW
2678 require_once($CFG->libdir.'/filelib.php');
2679
08334e06
MS
2680 // Increase the server timeout to handle the creation and sending of large zip files.
2681 core_php_time_limit::raise();
2682
4a47008c 2683 $this->require_view_grades();
76640b27 2684
d0d4796b 2685 // Load all users with submit.
4c4c7b3f 2686 $students = get_enrolled_users($this->context, "mod/assign:submit", null, 'u.*', null, null, null,
1ecb8044 2687 $this->show_only_active_users());
bbd0e548 2688
d0d4796b 2689 // Build a list of files to zip.
bbd0e548
DW
2690 $filesforzipping = array();
2691 $fs = get_file_storage();
2692
2693 $groupmode = groups_get_activity_groupmode($this->get_course_module());
d0d4796b
DW
2694 // All users.
2695 $groupid = 0;
bbd0e548
DW
2696 $groupname = '';
2697 if ($groupmode) {
2698 $groupid = groups_get_activity_group($this->get_course_module(), true);
2699 $groupname = groups_get_group_name($groupid).'-';
2700 }
2701
d0d4796b 2702 // Construct the zip file name.
e5403f8c
DW
2703 $filename = clean_filename($this->get_course()->shortname . '-' .
2704 $this->get_instance()->name . '-' .
2705 $groupname.$this->get_course_module()->id . '.zip');
bbd0e548 2706
d0d4796b
DW
2707 // Get all the files for each student.
2708 foreach ($students as $student) {
2709 $userid = $student->id;
bbd0e548 2710
7a2b911c 2711 if ((groups_is_member($groupid, $userid) or !$groupmode or !$groupid)) {
d0d4796b 2712 // Get the plugins to add their own files to the zip.
bbd0e548 2713
d0d4796b
DW
2714 $submissiongroup = false;
2715 $groupname = '';
2716 if ($this->get_instance()->teamsubmission) {
2717 $submission = $this->get_group_submission($userid, 0, false);
2718 $submissiongroup = $this->get_submission_group($userid);
21f77397
DW
2719 if ($submissiongroup) {
2720 $groupname = $submissiongroup->name . '-';
2721 } else {
2722 $groupname = get_string('defaultteam', 'assign') . '-';
2723 }
b473171a 2724 } else {
d0d4796b 2725 $submission = $this->get_user_submission($userid, false);
b473171a 2726 }
bbd0e548 2727
b473171a 2728 if ($this->is_blind_marking()) {
e5403f8c
DW
2729 $prefix = str_replace('_', ' ', $groupname . get_string('participant', 'assign'));
2730 $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($userid) . '_');
b473171a 2731 } else {
e5403f8c
DW
2732 $prefix = str_replace('_', ' ', $groupname . fullname($student));
2733 $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($userid) . '_');
b473171a 2734 }
bbd0e548 2735
d0d4796b
DW
2736 if ($submission) {
2737 foreach ($this->submissionplugins as $plugin) {
2738 if ($plugin->is_enabled() && $plugin->is_visible()) {
2406815b 2739 $pluginfiles = $plugin->get_files($submission, $student);
d0d4796b 2740 foreach ($pluginfiles as $zipfilename => $file) {
7a2b911c
DW
2741 $subtype = $plugin->get_subtype();
2742 $type = $plugin->get_type();
e5403f8c
DW
2743 $prefixedfilename = clean_filename($prefix .
2744 $subtype .
2745 '_' .
2746 $type .
2747 '_' .
2748 $zipfilename);
d0d4796b
DW
2749 $filesforzipping[$prefixedfilename] = $file;
2750 }
bbd0e548
DW
2751 }
2752 }
2753 }
bbd0e548 2754 }
d0d4796b 2755 }
afa3e637 2756 $result = '';
5c778358 2757 if (count($filesforzipping) == 0) {
afa3e637
DW
2758 $header = new assign_header($this->get_instance(),
2759 $this->get_context(),
2760 '',
2761 $this->get_course_module()->id,
2762 get_string('downloadall', 'assign'));
2763 $result .= $this->get_renderer()->render($header);
5c778358 2764 $result .= $this->get_renderer()->notification(get_string('nosubmission', 'assign'));
afa3e637
DW
2765 $url = new moodle_url('/mod/assign/view.php', array('id'=>$this->get_course_module()->id,
2766 'action'=>'grading'));
2767 $result .= $this->get_renderer()->continue_button($url);
5c778358 2768 $result .= $this->view_footer();
5c778358 2769 } else if ($zipfile = $this->pack_files($filesforzipping)) {
1b90858f 2770 \mod_assign\event\all_submissions_downloaded::create_from_assign($this)->trigger();
d0d4796b
DW
2771 // Send file and delete after sending.
2772 send_temp_file($zipfile, $filename);
afa3e637 2773 // We will not get here - send_temp_file calls exit.
bbd0e548 2774 }
afa3e637 2775 return $result;
bbd0e548
DW
2776 }
2777
2778 /**
e5403f8c 2779 * Util function to add a message to the log.
bbd0e548 2780 *
c17e70e5
MN
2781 * @deprecated since 2.7 - Use new events system instead.
2782 * (see http://docs.moodle.org/dev/Migrating_logging_calls_in_plugins).
2783 *
bbd0e548
DW
2784 * @param string $action The current action
2785 * @param string $info A detailed description of the change. But no more than 255 characters.
2786 * @param string $url The url to the assign module instance.
caa06f4b
FM
2787 * @param bool $return If true, returns the arguments, else adds to log. The purpose of this is to
2788 * retrieve the arguments to use them with the new event system (Event 2).
2789 * @return void|array
bbd0e548 2790 */
caa06f4b 2791 public function add_to_log($action = '', $info = '', $url='', $return = false) {
bbd0e548
DW
2792 global $USER;
2793
2794 $fullurl = 'view.php?id=' . $this->get_course_module()->id;
2795 if ($url != '') {
2796 $fullurl .= '&' . $url;
2797 }
2798
caa06f4b
FM
2799 $args = array(
2800 $this->get_course()->id,
2801 'assign',
2802 $action,
2803 $fullurl,
2804 $info,
666abe6e 2805 $this->get_course_module()->id
caa06f4b
FM
2806 );
2807
2808 if ($return) {
c17e70e5
MN
2809 // We only need to call debugging when returning a value. This is because the call to
2810 // call_user_func_array('add_to_log', $args) will trigger a debugging message of it's own.
2811 debugging('The mod_assign add_to_log() function is now deprecated.', DEBUG_DEVELOPER);
caa06f4b
FM
2812 return $args;
2813 }
2814 call_user_func_array('add_to_log', $args);
bbd0e548
DW
2815 }
2816
2cffef9f 2817 /**
e5403f8c 2818 * Lazy load the page renderer and expose the renderer to plugins.
49d83b9d 2819 *
2cffef9f
PC
2820 * @return assign_renderer
2821 */
23fffa2b 2822 public function get_renderer() {
2cffef9f
PC
2823 global $PAGE;
2824 if ($this->output) {
2825 return $this->output;
2826 }
2827 $this->output = $PAGE->get_renderer('mod_assign');
2828 return $this->output;
2829 }
bbd0e548
DW
2830
2831 /**
e5403f8c 2832 * Load the submission object for a particular user, optionally creating it if required.
bbd0e548 2833 *
12a1a0da
DW
2834 * For team assignments there are 2 submissions - the student submission and the team submission
2835 * All files are associated with the team submission but the status of the students contribution is
2836 * recorded separately.
2837 *
bbd0e548 2838 * @param int $userid The id of the user whose submission we want or 0 in which case USER->id is used
f6b4fe38 2839 * @param bool $create If set to true a new submission object will be created in the database with the status set to "new".
df211804 2840 * @param int $attemptnumber - -1 means the latest attempt
bbd0e548
DW
2841 * @return stdClass The submission
2842 */
df211804 2843 public function get_user_submission($userid, $create, $attemptnumber=-1) {
bbd0e548
DW
2844 global $DB, $USER;
2845
2846 if (!$userid) {
2847 $userid = $USER->id;
2848 }
12a1a0da
DW
2849 // If the userid is not null then use userid.
2850 $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid, 'groupid'=>0);
df211804
DW
2851 if ($attemptnumber >= 0) {
2852 $params['attemptnumber'] = $attemptnumber;
2853 }
2854
2855 // Only return the row with the highest attemptnumber.
2856 $submission = null;
2857 $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
2858 if ($submissions) {
2859 $submission = reset($submissions);
2860 }
bbd0e548
DW
2861
2862 if ($submission) {
2863 return $submission;
2864 }
2865 if ($create) {
2866 $submission = new stdClass();
2867 $submission->assignment = $this->get_instance()->id;
2868 $submission->userid = $userid;
2869 $submission->timecreated = time();
2870 $submission->timemodified = $submission->timecreated;
9e3eee67 2871 $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
df211804
DW
2872 if ($attemptnumber >= 0) {
2873 $submission->attemptnumber = $attemptnumber;
2874 } else {
2875 $submission->attemptnumber = 0;
2876 }
1523f9e0
DW
2877 // Work out if this is the latest submission.
2878 $submission->latest = 0;
2879 $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid, 'groupid'=>0);
2880 if ($attemptnumber == -1) {
2881 // This is a new submission so it must be the latest.
2882 $submission->latest = 1;
2883 } else {
2884 // We need to work this out.
2885 $result = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', 'attemptnumber', 0, 1);
9e3eee67 2886 $latestsubmission = null;
1523f9e0
DW
2887 if ($result) {
2888 $latestsubmission = reset($result);
2889 }
9e3eee67 2890 if (empty($latestsubmission) || ($attemptnumber > $latestsubmission->attemptnumber)) {
1523f9e0
DW
2891 $submission->latest = 1;
2892 }
2893 }
2894 if ($submission->latest) {
2895 // This is the case when we need to set latest to 0 for all the other attempts.
2896 $DB->set_field('assign_submission', 'latest', 0, $params);
2897 }
bbd0e548 2898 $sid = $DB->insert_record('assign_submission', $submission);
4781ff2e 2899 return $DB->get_record('assign_submission', array('id' => $sid));
bbd0e548
DW
2900 }
2901 return false;
2902 }
2903
2904 /**
e5403f8c 2905 * Load the submission object from it's id.
bbd0e548
DW
2906 *
2907 * @param int $submissionid The id of the submission we want
2908 * @return stdClass The submission
2909 */
47f48152 2910 protected function get_submission($submissionid) {
bbd0e548
DW
2911 global $DB;
2912
e5403f8c
DW
2913 $params = array('assignment'=>$this->get_instance()->id, 'id'=>$submissionid);
2914 return $DB->get_record('assign_submission', $params, '*', MUST_EXIST);
bbd0e548
DW
2915 }
2916
df211804
DW
2917 /**
2918 * This will retrieve a user flags object from the db optionally creating it if required.
2919 * The user flags was split from the user_grades table in 2.5.
2920 *
2921 * @param int $userid The user we are getting the flags for.
2922 * @param bool $create If true the flags record will be created if it does not exist
2923 * @return stdClass The flags record
2924 */
2925 public function get_user_flags($userid, $create) {
2926 global $DB, $USER;
2927
2928 // If the userid is not null then use userid.
2929 if (!$userid) {
2930 $userid = $USER->id;
2931 }
2932
2933 $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid);
2934
2935 $flags = $DB->get_record('assign_user_flags', $params);
2936
2937 if ($flags) {
2938 return $flags;
2939 }
2940 if ($create) {
2941 $flags = new stdClass();
2942 $flags->assignment = $this->get_instance()->id;
2943 $flags->userid = $userid;
2944 $flags->locked = 0;
2945 $flags->extensionduedate = 0;
f8d107b3
DM
2946 $flags->workflowstate = '';
2947 $flags->allocatedmarker = 0;
df211804
DW
2948
2949 // The mailed flag can be one of 3 values: 0 is unsent, 1 is sent and 2 is do not send yet.
2950 // This is because students only want to be notified about certain types of update (grades and feedback).
2951 $flags->mailed = 2;
2952
2953 $fid = $DB->insert_record('assign_user_flags', $flags);
2954 $flags->id = $fid;
2955 return $flags;
2956 }
2957 return false;
2958 }
2959
bbd0e548 2960 /**
e5403f8c 2961 * This will retrieve a grade object from the db, optionally creating it if required.
bbd0e548
DW
2962 *
2963 * @param int $userid The user we are grading
2964 * @param bool $create If true the grade will be created if it does not exist
df211804 2965 * @param int $attemptnumber The attempt number to retrieve the grade for. -1 means the latest submission.
bbd0e548
DW
2966 * @return stdClass The grade record
2967 */
df211804 2968 public function get_user_grade($userid, $create, $attemptnumber=-1) {
bbd0e548
DW
2969 global $DB, $USER;
2970
df211804 2971 // If the userid is not null then use userid.
bbd0e548
DW
2972 if (!$userid) {
2973 $userid = $USER->id;
2974 }
9d3c6b95 2975 $submission = null;
bbd0e548 2976
df211804 2977 $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid);
9d3c6b95 2978 if ($attemptnumber < 0 || $create) {
e7af1926
DW
2979 // Make sure this grade matches the latest submission attempt.
2980 if ($this->get_instance()->teamsubmission) {
9e3eee67 2981 $submission = $this->get_group_submission($userid, 0, true);
e7af1926 2982 } else {
9e3eee67 2983 $submission = $this->get_user_submission($userid, true);
e7af1926
DW
2984 }
2985 if ($submission) {
2986 $attemptnumber = $submission->attemptnumber;
2987 }
2988 }
2989
df211804
DW
2990 if ($attemptnumber >= 0) {
2991 $params['attemptnumber'] = $attemptnumber;
2992 }
bbd0e548 2993
df211804
DW
2994 $grades = $DB->get_records('assign_grades', $params, 'attemptnumber DESC', '*', 0, 1);
2995
2996 if ($grades) {
2997 return reset($grades);
bbd0e548
DW
2998 }
2999 if ($create) {
3000 $grade = new stdClass();
3001 $grade->assignment = $this->get_instance()->id;
3002 $grade->userid = $userid;
3003 $grade->timecreated = time();
9d3c6b95
DW
3004 // If we are "auto-creating" a grade - and there is a submission
3005 // the new grade should not have a more recent timemodified value
3006 // than the submission.
3007 if ($submission) {
3008 $grade->timemodified = $submission->timemodified;
3009 } else {
3010 $grade->timemodified = $grade->timecreated;
3011 }
bbd0e548
DW
3012 $grade->grade = -1;
3013 $grade->grader = $USER->id;
df211804
DW
3014 if ($attemptnumber >= 0) {
3015 $grade->attemptnumber = $attemptnumber;
3016 }
d6c673ed 3017
bbd0e548
DW
3018 $gid = $DB->insert_record('assign_grades', $grade);
3019 $grade->id = $gid;
3020 return $grade;
3021 }
3022 return false;
3023 }
3024
3025 /**
e5403f8c 3026 * This will retrieve a grade object from the db.
bbd0e548
DW
3027 *
3028 * @param int $gradeid The id of the grade
3029 * @return stdClass The grade record
3030 */
47f48152 3031 protected function get_grade($gradeid) {
bbd0e548
DW
3032 global $DB;
3033
e5403f8c
DW
3034 $params = array('assignment'=>$this->get_instance()->id, 'id'=>$gradeid);
3035 return $DB->get_record('assign_grades', $params, '*', MUST_EXIST);
bbd0e548
DW
3036 }
3037
3038 /**
e5403f8c 3039 * Print the grading page for a single user submission.
bbd0e548
DW
3040 *
3041 * @param moodleform $mform
bbd0e548
DW
3042 * @return string
3043 */
d04557b3 3044 protected function view_single_grade_page($mform) {
1a51661b 3045 global $DB, $CFG, $SESSION;
bbd0e548
DW
3046
3047 $o = '';
e5403f8c 3048 $instance = $this->get_instance();
bbd0e548 3049
bbd0e548
DW
3050 require_once($CFG->dirroot . '/mod/assign/gradeform.php');
3051
e5403f8c 3052 // Need submit permission to submit an assignment.
bbd0e548
DW
3053 require_capability('mod/assign:grade', $this->context);
3054
e5403f8c
DW
3055 $header = new assign_header($instance,
3056 $this->get_context(),
3057 false,
3058 $this->get_course_module()->id,
3059 get_string('grading', 'assign'));
3060 $o .= $this->get_renderer()->render($header);
bbd0e548 3061
df211804 3062 // If userid is passed - we are only grading a single student.
d04557b3 3063 $rownum = required_param('rownum', PARAM_INT);
3fca6937 3064 $useridlistid = optional_param('useridlistid', $this->get_useridlist_key_id(), PARAM_ALPHANUM);
df211804
DW
3065 $userid = optional_param('userid', 0, PARAM_INT);
3066 $attemptnumber = optional_param('attemptnumber', -1, PARAM_INT);
3067
df211804 3068 if (!$userid) {
1a51661b
DM
3069 $useridlistkey = $this->get_useridlist_key($useridlistid);
3070 if (empty($SESSION->mod_assign_useridlist[$useridlistkey])) {
3071 $SESSION->mod_assign_useridlist[$useridlistkey] = $this->get_grading_userid_list();
df211804 3072 }
1a51661b 3073 $useridlist = $SESSION->mod_assign_useridlist[$useridlistkey];
df211804
DW
3074 } else {
3075 $rownum = 0;
3076 $useridlist = array($userid);
bbd0e548 3077 }
d04557b3
DW
3078
3079 if ($rownum < 0 || $rownum > count($useridlist)) {
3080 throw new coding_exception('Row is out of bounds for the current grading table: ' . $rownum);
bbd0e548 3081 }
d04557b3 3082
bbd0e548
DW
3083 $last = false;
3084 $userid = $useridlist[$rownum];
3085 if ($rownum == count($useridlist) - 1) {
3086 $last = true;
3087 }
cfca338d
DW
3088 // This variation on the url will link direct to this student, with no next/previous links.
3089 // The benefit is the url will be the same every time for this student, so Atto autosave drafts can match up.
3090 $returnparams = array('userid' => $userid, 'rownum' => 0, 'useridlistid' => 0);
3091 $this->register_return_link('grade', $returnparams);
3092
bbd0e548
DW
3093 $user = $DB->get_record('user', array('id' => $userid));
3094 if ($user) {
e5403f8c
DW
3095 $viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_course_context());
3096 $usersummary = new assign_user_summary($user,
3097 $this->get_course()->id,
3098 $viewfullnames,
3099 $this->is_blind_marking(),
d08e6c31 3100 $this->get_uniqueid_for_user($user->id),
a69944eb 3101 get_extra_user_fields($this->get_context()),
1ecb8044 3102 !$this->is_active_user($userid));
e5403f8c 3103 $o .= $this->get_renderer()->render($usersummary);
bbd0e548 3104 }
df211804 3105 $submission = $this->get_user_submission($userid, false, $attemptnumber);
12a1a0da 3106 $submissiongroup = null;
12a1a0da
DW
3107 $teamsubmission = null;
3108 $notsubmitted = array();
e5403f8c 3109 if ($instance->teamsubmission) {
df211804 3110 $teamsubmission = $this->get_group_submission($userid, 0, false, $attemptnumber);
12a1a0da
DW
3111 $submissiongroup = $this->get_submission_group($userid);
3112 $groupid = 0;
3113 if ($submissiongroup) {
3114 $groupid = $submissiongroup->id;
3115 }
3116 $notsubmitted = $this->get_submission_group_members_who_have_not_submitted($groupid, false);
3117
3118 }
3119
df211804
DW
3120 // Get the requested grade.
3121 $grade = $this->get_user_grade($userid, false, $attemptnumber);
3122 $flags = $this->get_user_flags($userid, false);
bbd0e548 3123 if ($this->can_view_submission($userid)) {
df211804 3124 $gradelocked = ($flags && $flags->locked) || $this->grading_disabled($userid);
9e795179 3125 $extensionduedate = null;
df211804
DW
3126 if ($flags) {
3127 $extensionduedate = $flags->extensionduedate;
9e795179 3128 }
88cfe469 3129 $showedit = $this->submissions_open($userid) && ($this->is_any_submission_plugin_enabled());
12a1a0da 3130 $viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_course_context());
ee5d4eef 3131 $usergroups = $this->get_all_groups($user->id);
9e795179 3132
e5403f8c
DW
3133 $submissionstatus = new assign_submission_status($instance->allowsubmissionsfromdate,
3134 $instance->alwaysshowdescription,
3135 $submission,
3136 $instance->teamsubmission,
3137 $teamsubmission,
3138 $submissiongroup,
3139 $notsubmitted,
3140 $this->is_any_submission_plugin_enabled(),
3141 $gradelocked,
3142 $this->is_graded($userid),
3143 $instance->duedate,
3144 $instance->cutoffdate,
3145 $this->get_submission_plugins(),
3146 $this->get_return_action(),
3147 $this->get_return_params(),
3148 $this->get_course_module()->id,
3149 $this->get_course()->id,
3150 assign_submission_status::GRADER_VIEW,
3151 $showedit,
19195b60 3152 false,
e5403f8c
DW
3153 $viewfullnames,
3154 $extensionduedate,
3155 $this->get_context(),
3156 $this->is_blind_marking(),
df211804
DW
3157 '',
3158 $instance->attemptreopenmethod,
bd3ee807 3159 $instance->maxattempts,
e528997a 3160 $this->get_grading_status($userid),
ee5d4eef
AH
3161 $instance->preventsubmissionnotingroup,
3162 $usergroups);
e5403f8c 3163 $o .= $this->get_renderer()->render($submissionstatus);
bbd0e548 3164 }