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