MDL-37533 Assign: Use the constant for the submitted status in the query (and paramet...
[moodle.git] / mod / assign / lib.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/**
e5403f8c
DW
18 * This file contains the moodle hooks for the assign module.
19 * It delegates most functions to the assignment class.
bbd0e548
DW
20 *
21 * @package mod_assign
22 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Adds an assignment instance
29 *
30 * This is done by calling the add_instance() method of the assignment type class
31 * @param stdClass $data
32 * @param mod_assign_mod_form $form
33 * @return int The instance id of the new assignment
34 */
2cffef9f 35function assign_add_instance(stdClass $data, mod_assign_mod_form $form = null) {
bbd0e548
DW
36 global $CFG;
37 require_once($CFG->dirroot . '/mod/assign/locallib.php');
38
39 $assignment = new assign(context_module::instance($data->coursemodule), null, null);
40 return $assignment->add_instance($data, true);
41}
42
43/**
44 * delete an assignment instance
45 * @param int $id
46 * @return bool
47 */
48function assign_delete_instance($id) {
49 global $CFG;
50 require_once($CFG->dirroot . '/mod/assign/locallib.php');
51 $cm = get_coursemodule_from_instance('assign', $id, 0, false, MUST_EXIST);
52 $context = context_module::instance($cm->id);
53
54 $assignment = new assign($context, null, null);
55 return $assignment->delete_instance();
56}
57
d38dc52f
RW
58/**
59 * This function is used by the reset_course_userdata function in moodlelib.
60 * This function will remove all assignment submissions and feedbacks in the database
61 * and clean up any related data.
62 * @param $data the data submitted from the reset course.
63 * @return array status array
64 */
65function assign_reset_userdata($data) {
66 global $CFG, $DB;
67 require_once($CFG->dirroot . '/mod/assign/locallib.php');
68
69 $status = array();
70 $params = array('courseid'=>$data->courseid);
71 $sql = "SELECT a.id FROM {assign} a WHERE a.course=:courseid";
e5403f8c
DW
72 $course = $DB->get_record('course', array('id'=>$data->courseid), '*', MUST_EXIST);
73 if ($assigns = $DB->get_records_sql($sql, $params)) {
d38dc52f 74 foreach ($assigns as $assign) {
e5403f8c
DW
75 $cm = get_coursemodule_from_instance('assign',
76 $assign->id,
77 $data->courseid,
78 false,
79 MUST_EXIST);
d38dc52f
RW
80 $context = context_module::instance($cm->id);
81 $assignment = new assign($context, $cm, $course);
82 $status = array_merge($status, $assignment->reset_userdata($data));
83 }
84 }
85 return $status;
86}
87
88/**
89 * Removes all grades from gradebook
90 *
91 * @param int $courseid The ID of the course to reset
92 * @param string $type Optional type of assignment to limit the reset to a particular assignment type
93 */
94function assign_reset_gradebook($courseid, $type='') {
95 global $CFG, $DB;
96
e5403f8c 97 $params = array('moduletype'=>'assign', 'courseid'=>$courseid);
d38dc52f
RW
98 $sql = 'SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid
99 FROM {assign} a, {course_modules} cm, {modules} m
100 WHERE m.name=:moduletype AND m.id=cm.module AND cm.instance=a.id AND a.course=:courseid';
101
e5403f8c 102 if ($assignments = $DB->get_records_sql($sql, $params)) {
d38dc52f
RW
103 foreach ($assignments as $assignment) {
104 assign_grade_item_update($assignment, 'reset');
105 }
106 }
107}
108
109/**
110 * Implementation of the function for printing the form elements that control
111 * whether the course reset functionality affects the assignment.
112 * @param $mform form passed by reference
113 */
114function assign_reset_course_form_definition(&$mform) {
115 $mform->addElement('header', 'assignheader', get_string('modulenameplural', 'assign'));
e5403f8c
DW
116 $name = get_string('deleteallsubmissions', 'assign');
117 $mform->addElement('advcheckbox', 'reset_assign_submissions', $name);
d38dc52f
RW
118}
119
120/**
121 * Course reset form defaults.
122 * @param object $course
123 * @return array
124 */
125function assign_reset_course_form_defaults($course) {
126 return array('reset_assign_submissions'=>1);
127}
128
bbd0e548
DW
129/**
130 * Update an assignment instance
131 *
132 * This is done by calling the update_instance() method of the assignment type class
133 * @param stdClass $data
134 * @param mod_assign_mod_form $form
135 * @return object
136 */
137function assign_update_instance(stdClass $data, mod_assign_mod_form $form) {
138 global $CFG;
139 require_once($CFG->dirroot . '/mod/assign/locallib.php');
140 $context = context_module::instance($data->coursemodule);
141 $assignment = new assign($context, null, null);
142 return $assignment->update_instance($data);
143}
144
145/**
146 * Return the list if Moodle features this module supports
147 *
148 * @param string $feature FEATURE_xx constant for requested feature
149 * @return mixed True if module supports feature, null if doesn't know
150 */
151function assign_supports($feature) {
152 switch($feature) {
153 case FEATURE_GROUPS: return true;
154 case FEATURE_GROUPINGS: return true;
155 case FEATURE_GROUPMEMBERSONLY: return true;
156 case FEATURE_MOD_INTRO: return true;
157 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
79ed4d84 158 case FEATURE_COMPLETION_HAS_RULES: return true;
bbd0e548
DW
159 case FEATURE_GRADE_HAS_GRADE: return true;
160 case FEATURE_GRADE_OUTCOMES: return true;
161 case FEATURE_BACKUP_MOODLE2: return true;
162 case FEATURE_SHOW_DESCRIPTION: return true;
163 case FEATURE_ADVANCED_GRADING: return true;
50da4ddd 164 case FEATURE_PLAGIARISM: return true;
bbd0e548
DW
165
166 default: return null;
167 }
168}
169
170/**
171 * Lists all gradable areas for the advanced grading methods gramework
172 *
173 * @return array('string'=>'string') An array with area names as keys and descriptions as values
174 */
175function assign_grading_areas_list() {
176 return array('submissions'=>get_string('submissions', 'assign'));
177}
178
179
180/**
181 * extend an assigment navigation settings
182 *
183 * @param settings_navigation $settings
184 * @param navigation_node $navref
185 * @return void
186 */
187function assign_extend_settings_navigation(settings_navigation $settings, navigation_node $navref) {
b473171a 188 global $PAGE, $DB;
bbd0e548
DW
189
190 $cm = $PAGE->cm;
191 if (!$cm) {
192 return;
193 }
194
195 $context = $cm->context;
196 $course = $PAGE->course;
197
bbd0e548
DW
198 if (!$course) {
199 return;
200 }
201
e5403f8c
DW
202 // Link to gradebook.
203 if (has_capability('gradereport/grader:view', $cm->context) &&
204 has_capability('moodle/grade:viewall', $cm->context)) {
205 $link = new moodle_url('/grade/report/grader/index.php', array('id' => $course->id));
206 $linkname = get_string('viewgradebook', 'assign');
207 $node = $navref->add($linkname, $link, navigation_node::TYPE_SETTING);
208 }
bbd0e548 209
e5403f8c
DW
210 // Link to download all submissions.
211 if (has_capability('mod/assign:grade', $context)) {
212 $link = new moodle_url('/mod/assign/view.php', array('id' => $cm->id, 'action'=>'grading'));
213 $node = $navref->add(get_string('viewgrading', 'assign'), $link, navigation_node::TYPE_SETTING);
bbd0e548 214
e5403f8c
DW
215 $link = new moodle_url('/mod/assign/view.php', array('id' => $cm->id, 'action'=>'downloadall'));
216 $node = $navref->add(get_string('downloadall', 'assign'), $link, navigation_node::TYPE_SETTING);
217 }
bbd0e548 218
e5403f8c
DW
219 if (has_capability('mod/assign:revealidentities', $context)) {
220 $dbparams = array('id'=>$cm->instance);
221 $assignment = $DB->get_record('assign', $dbparams, 'blindmarking, revealidentities');
b473171a 222
e5403f8c
DW
223 if ($assignment && $assignment->blindmarking && !$assignment->revealidentities) {
224 $urlparams = array('id' => $cm->id, 'action'=>'revealidentities');
225 $url = new moodle_url('/mod/assign/view.php', $urlparams);
226 $linkname = get_string('revealidentities', 'assign');
227 $node = $navref->add($linkname, $url, navigation_node::TYPE_SETTING);
228 }
229 }
bbd0e548
DW
230}
231
bbd0e548
DW
232/**
233 * Add a get_coursemodule_info function in case any assignment type wants to add 'extra' information
234 * for the course (see resource).
235 *
236 * Given a course_module object, this function returns any "extra" information that may be needed
237 * when printing this activity in a course listing. See get_array_of_activities() in course/lib.php.
238 *
239 * @param stdClass $coursemodule The coursemodule object (record).
e5403f8c
DW
240 * @return cached_cm_info An object on information that the courses
241 * will know about (most noticeably, an icon).
bbd0e548
DW
242 */
243function assign_get_coursemodule_info($coursemodule) {
244 global $CFG, $DB;
245
e5403f8c
DW
246 $dbparams = array('id'=>$coursemodule->instance);
247 $fields = 'id, name, alwaysshowdescription, allowsubmissionsfromdate, intro, introformat';
248 if (! $assignment = $DB->get_record('assign', $dbparams, $fields)) {
bbd0e548
DW
249 return false;
250 }
251
252 $result = new cached_cm_info();
253 $result->name = $assignment->name;
254 if ($coursemodule->showdescription) {
255 if ($assignment->alwaysshowdescription || time() > $assignment->allowsubmissionsfromdate) {
256 // Convert intro to html. Do not filter cached version, filters run at display time.
257 $result->content = format_module_intro('assign', $assignment, $coursemodule->id, false);
258 }
259 }
260 return $result;
261}
262
bbd0e548
DW
263/**
264 * Return a list of page types
265 * @param string $pagetype current page type
266 * @param stdClass $parentcontext Block's parent context
267 * @param stdClass $currentcontext Current context of block
268 */
269function assign_page_type_list($pagetype, $parentcontext, $currentcontext) {
270 $module_pagetype = array(
271 'mod-assign-*' => get_string('page-mod-assign-x', 'assign'),
272 'mod-assign-view' => get_string('page-mod-assign-view', 'assign'),
273 );
274 return $module_pagetype;
275}
276
277/**
278 * Print an overview of all assignments
279 * for the courses.
280 *
281 * @param mixed $courses The list of courses to print the overview for
282 * @param array $htmlarray The array of html to return
283 */
284function assign_print_overview($courses, &$htmlarray) {
285 global $USER, $CFG, $DB;
286
287 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
288 return array();
289 }
290
e5403f8c 291 if (!$assignments = get_all_instances_in_courses('assign', $courses)) {
bbd0e548
DW
292 return;
293 }
294
295 $assignmentids = array();
296
e5403f8c 297 // Do assignment_base::isopen() here without loading the whole thing for speed.
bbd0e548
DW
298 foreach ($assignments as $key => $assignment) {
299 $time = time();
710f1a34 300 $isopen = false;
bbd0e548 301 if ($assignment->duedate) {
9e795179
DW
302 $duedate = false;
303 if ($assignment->cutoffdate) {
304 $duedate = $assignment->cutoffdate;
305 }
306 if ($duedate) {
307 $isopen = ($assignment->allowsubmissionsfromdate <= $time && $time <= $duedate);
308 } else {
309 $isopen = ($assignment->allowsubmissionsfromdate <= $time);
bbd0e548
DW
310 }
311 }
da099b12 312 if ($isopen) {
bbd0e548
DW
313 $assignmentids[] = $assignment->id;
314 }
315 }
316
e5403f8c
DW
317 if (empty($assignmentids)) {
318 // No assignments to look at - we're done.
bbd0e548
DW
319 return true;
320 }
321
d43b3109
DW
322 // Definitely something to print, now include the constants we need.
323 require_once($CFG->dirroot . '/mod/assign/locallib.php');
324
bbd0e548 325 $strduedate = get_string('duedate', 'assign');
9e795179
DW
326 $strcutoffdate = get_string('nosubmissionsacceptedafter', 'assign');
327 $strnolatesubmissions = get_string('nolatesubmissions', 'assign');
328 $strduedateno = get_string('duedateno', 'assign');
bbd0e548
DW
329 $strduedateno = get_string('duedateno', 'assign');
330 $strgraded = get_string('graded', 'assign');
331 $strnotgradedyet = get_string('notgradedyet', 'assign');
332 $strnotsubmittedyet = get_string('notsubmittedyet', 'assign');
333 $strsubmitted = get_string('submitted', 'assign');
334 $strassignment = get_string('modulename', 'assign');
e5403f8c 335 $strreviewed = get_string('reviewed', 'assign');
bbd0e548 336
e5403f8c 337 // We do all possible database work here *outside* of the loop to ensure this scales.
bbd0e548
DW
338 list($sqlassignmentids, $assignmentidparams) = $DB->get_in_or_equal($assignmentids);
339
e5403f8c
DW
340 // Build up and array of unmarked submissions indexed by assignment id/ userid
341 // for use where the user has grading rights on assignment.
d43b3109 342 $dbparams = array_merge(array(ASSIGN_SUBMISSION_STATUS_SUBMITTED), $assignmentidparams);
e5403f8c
DW
343 $rs = $DB->get_recordset_sql('SELECT
344 s.assignment as assignment,
345 s.userid as userid,
346 s.id as id,
347 s.status as status,
348 g.timemodified as timegraded
349 FROM {assign_submission} s
350 LEFT JOIN {assign_grades} g ON
351 s.userid = g.userid AND
352 s.assignment = g.assignment
353 WHERE
b7f46141
SB
354 ( g.timemodified is NULL OR
355 s.timemodified > g.timemodified ) AND
356 s.timemodified IS NOT NULL AND
d43b3109
DW
357 s.status = ? AND
358 s.assignment ' . $sqlassignmentids, $dbparams);
bbd0e548
DW
359
360 $unmarkedsubmissions = array();
361 foreach ($rs as $rd) {
362 $unmarkedsubmissions[$rd->assignment][$rd->userid] = $rd->id;
363 }
364 $rs->close();
365
e5403f8c 366 // Get all user submissions, indexed by assignment id.
c1432fad 367 $dbparams = array_merge(array($USER->id, $USER->id), $assignmentidparams);
e5403f8c
DW
368 $mysubmissions = $DB->get_records_sql('SELECT
369 a.id AS assignment,
370 a.nosubmissions AS nosubmissions,
371 g.timemodified AS timemarked,
372 g.grader AS grader,
373 g.grade AS grade,
374 s.status AS status
375 FROM {assign} a
376 LEFT JOIN {assign_grades} g ON
377 g.assignment = a.id AND
378 g.userid = ?
379 LEFT JOIN {assign_submission} s ON
380 s.assignment = a.id AND
425a634b
DW
381 s.userid = ?
382 WHERE a.id ' . $sqlassignmentids, $dbparams);
bbd0e548
DW
383
384 foreach ($assignments as $assignment) {
e5403f8c 385 // Do not show assignments that are not open.
da099b12
DW
386 if (!in_array($assignment->id, $assignmentids)) {
387 continue;
388 }
e5403f8c
DW
389 $dimmedclass = '';
390 if (!$assignment->visible) {
391 $dimmedclass = ' class="dimmed"';
392 }
393 $href = $CFG->wwwroot . '/mod/assign/view.php?id=' . $assignment->coursemodule;
394 $str = '<div class="assign overview">' .
395 '<div class="name">' .
396 $strassignment . ': '.
397 '<a ' . $dimmedclass .
398 'title="' . $strassignment . '" ' .
399 'href="' . $href . '">' .
400 format_string($assignment->name) .
401 '</a></div>';
bbd0e548 402 if ($assignment->duedate) {
e5403f8c
DW
403 $userdate = userdate($assignment->duedate);
404 $str .= '<div class="info">' . $strduedate . ': ' . $userdate . '</div>';
bbd0e548 405 } else {
e5403f8c 406 $str .= '<div class="info">' . $strduedateno . '</div>';
bbd0e548 407 }
9e795179
DW
408 if ($assignment->cutoffdate) {
409 if ($assignment->cutoffdate == $assignment->duedate) {
e5403f8c 410 $str .= '<div class="info">' . $strnolatesubmissions . '</div>';
9e795179 411 } else {
e5403f8c
DW
412 $userdate = userdate($assignment->cutoffdate);
413 $str .= '<div class="info">' . $strcutoffdate . ': ' . $userdate . '</div>';
9e795179
DW
414 }
415 }
bbd0e548
DW
416 $context = context_module::instance($assignment->coursemodule);
417 if (has_capability('mod/assign:grade', $context)) {
e5403f8c
DW
418 // Count how many people can submit.
419 $submissions = 0;
bbd0e548
DW
420 if ($students = get_enrolled_users($context, 'mod/assign:view', 0, 'u.id')) {
421 foreach ($students as $student) {
422 if (isset($unmarkedsubmissions[$assignment->id][$student->id])) {
423 $submissions++;
424 }
425 }
426 }
427
428 if ($submissions) {
e5403f8c
DW
429 $urlparams = array('id'=>$assignment->coursemodule, 'action'=>'grading');
430 $url = new moodle_url('/mod/assign/view.php', $urlparams);
431 $str .= '<div class="details">' .
432 '<a href="' . $url . '">' .
433 get_string('submissionsnotgraded', 'assign', $submissions) .
434 '</a></div>';
bbd0e548 435 }
e5403f8c
DW
436 }
437 if (has_capability('mod/assign:submit', $context)) {
bbd0e548
DW
438 $str .= '<div class="details">';
439 $str .= get_string('mysubmission', 'assign');
440 $submission = $mysubmissions[$assignment->id];
93c18e73 441 if ($submission->nosubmissions) {
e5403f8c
DW
442 $str .= get_string('offline', 'assign');
443 } else if (!$submission->status || $submission->status == 'draft') {
444 $str .= $strnotsubmittedyet;
445 } else {
bbd0e548
DW
446 $str .= get_string('submissionstatus_' . $submission->status, 'assign');
447 }
448 if (!$submission->grade || $submission->grade < 0) {
449 $str .= ', ' . get_string('notgraded', 'assign');
450 } else {
451 $str .= ', ' . get_string('graded', 'assign');
452 }
453 $str .= '</div>';
454 }
e5403f8c 455 $str .= '</div>';
bbd0e548
DW
456 if (empty($htmlarray[$assignment->course]['assign'])) {
457 $htmlarray[$assignment->course]['assign'] = $str;
458 } else {
459 $htmlarray[$assignment->course]['assign'] .= $str;
460 }
461 }
462}
463
464/**
465 * Print recent activity from all assignments in a given course
466 *
467 * This is used by the recent activity block
468 * @param mixed $course the course to print activity for
469 * @param bool $viewfullnames boolean to determine whether to show full names or not
9682626e 470 * @param int $timestart the time the rendering started
bbd0e548
DW
471 */
472function assign_print_recent_activity($course, $viewfullnames, $timestart) {
473 global $CFG, $USER, $DB, $OUTPUT;
474
e5403f8c 475 // Do not use log table if possible, it may be huge.
bbd0e548 476
e5403f8c
DW
477 $dbparams = array($timestart, $course->id, 'assign');
478 if (!$submissions = $DB->get_records_sql('SELECT asb.id, asb.timemodified, cm.id AS cmid, asb.userid,
bbd0e548
DW
479 u.firstname, u.lastname, u.email, u.picture
480 FROM {assign_submission} asb
481 JOIN {assign} a ON a.id = asb.assignment
482 JOIN {course_modules} cm ON cm.instance = a.id
483 JOIN {modules} md ON md.id = cm.module
484 JOIN {user} u ON u.id = asb.userid
485 WHERE asb.timemodified > ? AND
486 a.course = ? AND
e5403f8c
DW
487 md.name = ?
488 ORDER BY asb.timemodified ASC', $dbparams)) {
bbd0e548
DW
489 return false;
490 }
491
e5403f8c 492 $modinfo = get_fast_modinfo($course);
bbd0e548
DW
493 $show = array();
494 $grader = array();
495
cfc81f03
DW
496 $showrecentsubmissions = get_config('mod_assign', 'showrecentsubmissions');
497
e5403f8c 498 foreach ($submissions as $submission) {
bbd0e548
DW
499 if (!array_key_exists($submission->cmid, $modinfo->get_cms())) {
500 continue;
501 }
502 $cm = $modinfo->get_cm($submission->cmid);
503 if (!$cm->uservisible) {
504 continue;
505 }
506 if ($submission->userid == $USER->id) {
507 $show[] = $submission;
508 continue;
509 }
510
511 $context = context_module::instance($submission->cmid);
e5403f8c
DW
512 // The act of submitting of assignment may be considered private -
513 // only graders will see it if specified.
cfc81f03 514 if (empty($showrecentsubmissions)) {
bbd0e548 515 if (!array_key_exists($cm->id, $grader)) {
e5403f8c 516 $grader[$cm->id] = has_capability('moodle/grade:viewall', $context);
bbd0e548
DW
517 }
518 if (!$grader[$cm->id]) {
519 continue;
520 }
521 }
522
523 $groupmode = groups_get_activity_groupmode($cm, $course);
524
e5403f8c
DW
525 if ($groupmode == SEPARATEGROUPS &&
526 !has_capability('moodle/site:accessallgroups', $context)) {
bbd0e548 527 if (isguestuser()) {
e5403f8c 528 // Shortcut - guest user does not belong into any group.
bbd0e548
DW
529 continue;
530 }
531
532 if (is_null($modinfo->get_groups())) {
e5403f8c
DW
533 // Load all my groups and cache it in modinfo.
534 $modinfo->groups = groups_get_user_groups($course->id);
bbd0e548
DW
535 }
536
e5403f8c 537 // This will be slow - show only users that share group with me in this cm.
bbd0e548
DW
538 if (empty($modinfo->groups[$cm->id])) {
539 continue;
540 }
541 $usersgroups = groups_get_all_groups($course->id, $submission->userid, $cm->groupingid);
542 if (is_array($usersgroups)) {
543 $usersgroups = array_keys($usersgroups);
544 $intersect = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
545 if (empty($intersect)) {
546 continue;
547 }
548 }
549 }
550 $show[] = $submission;
551 }
552
553 if (empty($show)) {
554 return false;
555 }
556
557 echo $OUTPUT->heading(get_string('newsubmissions', 'assign').':', 3);
558
559 foreach ($show as $submission) {
560 $cm = $modinfo->get_cm($submission->cmid);
561 $link = $CFG->wwwroot.'/mod/assign/view.php?id='.$cm->id;
e5403f8c
DW
562 print_recent_activity_note($submission->timemodified,
563 $submission,
564 $cm->name,
565 $link,
566 false,
567 $viewfullnames);
bbd0e548
DW
568 }
569
570 return true;
571}
572
573/**
e5403f8c 574 * Returns all assignments since a given time.
c06b2127 575 *
bbd0e548
DW
576 * @param array $activities The activity information is returned in this array
577 * @param int $index The current index in the activities array
578 * @param int $timestart The earliest activity to show
579 * @param int $courseid Limit the search to this course
580 * @param int $cmid The course module id
581 * @param int $userid Optional user id
582 * @param int $groupid Optional group id
583 * @return void
584 */
e5403f8c
DW
585function assign_get_recent_mod_activity(&$activities,
586 &$index,
587 $timestart,
588 $courseid,
589 $cmid,
590 $userid=0,
591 $groupid=0) {
bbd0e548
DW
592 global $CFG, $COURSE, $USER, $DB;
593
594 if ($COURSE->id == $courseid) {
595 $course = $COURSE;
596 } else {
597 $course = $DB->get_record('course', array('id'=>$courseid));
598 }
599
e5403f8c 600 $modinfo = get_fast_modinfo($course);
bbd0e548
DW
601
602 $cm = $modinfo->get_cm($cmid);
603 $params = array();
604 if ($userid) {
e5403f8c 605 $userselect = 'AND u.id = :userid';
bbd0e548
DW
606 $params['userid'] = $userid;
607 } else {
e5403f8c 608 $userselect = '';
bbd0e548
DW
609 }
610
611 if ($groupid) {
e5403f8c
DW
612 $groupselect = 'AND gm.groupid = :groupid';
613 $groupjoin = 'JOIN {groups_members} gm ON gm.userid=u.id';
bbd0e548
DW
614 $params['groupid'] = $groupid;
615 } else {
e5403f8c
DW
616 $groupselect = '';
617 $groupjoin = '';
bbd0e548
DW
618 }
619
620 $params['cminstance'] = $cm->instance;
621 $params['timestart'] = $timestart;
622
623 $userfields = user_picture::fields('u', null, 'userid');
624
f93fdcef
DW
625 if (!$submissions = $DB->get_records_sql('SELECT asb.id, asb.timemodified, ' .
626 $userfields .
627 ' FROM {assign_submission} asb
628 JOIN {assign} a ON a.id = asb.assignment
629 JOIN {user} u ON u.id = asb.userid ' .
630 $groupjoin .
631 ' WHERE asb.timemodified > :timestart AND
e5403f8c
DW
632 a.id = :cminstance
633 ' . $userselect . ' ' . $groupselect .
634 ' ORDER BY asb.timemodified ASC', $params)) {
bbd0e548
DW
635 return;
636 }
637
638 $groupmode = groups_get_activity_groupmode($cm, $course);
639 $cm_context = context_module::instance($cm->id);
640 $grader = has_capability('moodle/grade:viewall', $cm_context);
641 $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
642 $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
643
644 if (is_null($modinfo->get_groups())) {
e5403f8c
DW
645 // Load all my groups and cache it in modinfo.
646 $modinfo->groups = groups_get_user_groups($course->id);
bbd0e548
DW
647 }
648
cfc81f03 649 $showrecentsubmissions = get_config('mod_assign', 'showrecentsubmissions');
bbd0e548
DW
650 $show = array();
651 $usersgroups = groups_get_all_groups($course->id, $USER->id, $cm->groupingid);
652 if (is_array($usersgroups)) {
653 $usersgroups = array_keys($usersgroups);
654 }
e5403f8c 655 foreach ($submissions as $submission) {
bbd0e548
DW
656 if ($submission->userid == $USER->id) {
657 $show[] = $submission;
658 continue;
659 }
e5403f8c
DW
660 // The act of submitting of assignment may be considered private -
661 // only graders will see it if specified.
cfc81f03 662 if (empty($showrecentsubmissions)) {
bbd0e548
DW
663 if (!$grader) {
664 continue;
665 }
666 }
667
668 if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
669 if (isguestuser()) {
e5403f8c 670 // Shortcut - guest user does not belong into any group.
bbd0e548
DW
671 continue;
672 }
673
e5403f8c 674 // This will be slow - show only users that share group with me in this cm.
bbd0e548
DW
675 if (empty($modinfo->groups[$cm->id])) {
676 continue;
677 }
678 if (is_array($usersgroups)) {
679 $intersect = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
680 if (empty($intersect)) {
681 continue;
682 }
683 }
684 }
685 $show[] = $submission;
686 }
687
688 if (empty($show)) {
689 return;
690 }
691
692 if ($grader) {
693 require_once($CFG->libdir.'/gradelib.php');
694 $userids = array();
e5403f8c 695 foreach ($show as $id => $submission) {
bbd0e548 696 $userids[] = $submission->userid;
bbd0e548
DW
697 }
698 $grades = grade_get_grades($courseid, 'mod', 'assign', $cm->instance, $userids);
699 }
700
e5403f8c 701 $aname = format_string($cm->name, true);
bbd0e548
DW
702 foreach ($show as $submission) {
703 $activity = new stdClass();
704
705 $activity->type = 'assign';
706 $activity->cmid = $cm->id;
707 $activity->name = $aname;
708 $activity->sectionnum = $cm->sectionnum;
709 $activity->timestamp = $submission->timemodified;
710 $activity->user = new stdClass();
711 if ($grader) {
712 $activity->grade = $grades->items[0]->grades[$submission->userid]->str_long_grade;
713 }
714
715 $userfields = explode(',', user_picture::fields());
716 foreach ($userfields as $userfield) {
717 if ($userfield == 'id') {
e5403f8c
DW
718 // Aliased in SQL above.
719 $activity->user->{$userfield} = $submission->userid;
bbd0e548
DW
720 } else {
721 $activity->user->{$userfield} = $submission->{$userfield};
722 }
723 }
724 $activity->user->fullname = fullname($submission, $viewfullnames);
725
726 $activities[$index++] = $activity;
727 }
728
729 return;
730}
731
732/**
733 * Print recent activity from all assignments in a given course
734 *
735 * This is used by course/recent.php
736 * @param stdClass $activity
737 * @param int $courseid
738 * @param bool $detail
739 * @param array $modnames
740 */
e5403f8c 741function assign_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
bbd0e548
DW
742 global $CFG, $OUTPUT;
743
744 echo '<table border="0" cellpadding="3" cellspacing="0" class="assignment-recent">';
745
e5403f8c 746 echo '<tr><td class="userpicture" valign="top">';
bbd0e548 747 echo $OUTPUT->user_picture($activity->user);
e5403f8c 748 echo '</td><td>';
bbd0e548
DW
749
750 if ($detail) {
751 $modname = $modnames[$activity->type];
752 echo '<div class="title">';
e5403f8c
DW
753 echo '<img src="' . $OUTPUT->pix_url('icon', 'assign') . '" '.
754 'class="icon" alt="' . $modname . '">';
755 echo '<a href="' . $CFG->wwwroot . '/mod/assign/view.php?id=' . $activity->cmid . '">';
756 echo $activity->name;
757 echo '</a>';
bbd0e548
DW
758 echo '</div>';
759 }
760
761 if (isset($activity->grade)) {
762 echo '<div class="grade">';
763 echo get_string('grade').': ';
764 echo $activity->grade;
765 echo '</div>';
766 }
767
768 echo '<div class="user">';
e5403f8c
DW
769 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&amp;course=$courseid\">";
770 echo "{$activity->user->fullname}</a> - " . userdate($activity->timestamp);
bbd0e548
DW
771 echo '</div>';
772
e5403f8c 773 echo '</td></tr></table>';
bbd0e548
DW
774}
775
776/**
e5403f8c 777 * Checks if a scale is being used by an assignment.
bbd0e548
DW
778 *
779 * This is used by the backup code to decide whether to back up a scale
9682626e 780 * @param int $assignmentid
bbd0e548
DW
781 * @param int $scaleid
782 * @return boolean True if the scale is used by the assignment
783 */
784function assign_scale_used($assignmentid, $scaleid) {
785 global $DB;
786
787 $return = false;
e5403f8c 788 $rec = $DB->get_record('assign', array('id'=>$assignmentid, 'grade'=>-$scaleid));
bbd0e548
DW
789
790 if (!empty($rec) && !empty($scaleid)) {
791 $return = true;
792 }
793
794 return $return;
795}
796
797/**
798 * Checks if scale is being used by any instance of assignment
799 *
800 * This is used to find out if scale used anywhere
9682626e 801 * @param int $scaleid
bbd0e548
DW
802 * @return boolean True if the scale is used by any assignment
803 */
804function assign_scale_used_anywhere($scaleid) {
805 global $DB;
806
807 if ($scaleid and $DB->record_exists('assign', array('grade'=>-$scaleid))) {
808 return true;
809 } else {
810 return false;
811 }
812}
813
814/**
e5403f8c
DW
815 * List the actions that correspond to a view of this module.
816 * This is used by the participation report.
bbd0e548
DW
817 * @return array
818 */
819function assign_get_view_actions() {
820 return array('view submission', 'view feedback');
821}
822
823/**
e5403f8c
DW
824 * List the actions that correspond to a post of this module.
825 * This is used by the participation report.
bbd0e548
DW
826 * @return array
827 */
828function assign_get_post_actions() {
829 return array('upload', 'submit', 'submit for grading');
830}
831
832/**
e5403f8c 833 * Call cron on the assign module.
bbd0e548
DW
834 */
835function assign_cron() {
836 global $CFG;
837
838 require_once($CFG->dirroot . '/mod/assign/locallib.php');
75f87a57
DW
839 assign::cron();
840
bbd0e548
DW
841 $plugins = get_plugin_list('assignsubmission');
842
843 foreach ($plugins as $name => $plugin) {
844 $disabled = get_config('assignsubmission_' . $name, 'disabled');
845 if (!$disabled) {
846 $class = 'assign_submission_' . $name;
847 require_once($CFG->dirroot . '/mod/assign/submission/' . $name . '/locallib.php');
848 $class::cron();
849 }
850 }
851 $plugins = get_plugin_list('assignfeedback');
852
853 foreach ($plugins as $name => $plugin) {
854 $disabled = get_config('assignfeedback_' . $name, 'disabled');
855 if (!$disabled) {
856 $class = 'assign_feedback_' . $name;
857 require_once($CFG->dirroot . '/mod/assign/feedback/' . $name . '/locallib.php');
858 $class::cron();
859 }
860 }
861}
862
863/**
864 * Returns all other capabilities used by this module.
865 * @return array Array of capability strings
866 */
867function assign_get_extra_capabilities() {
e5403f8c
DW
868 return array('gradereport/grader:view',
869 'moodle/grade:viewall',
870 'moodle/site:viewfullnames',
871 'moodle/site:config');
bbd0e548
DW
872}
873
874/**
e5403f8c 875 * Create grade item for given assignment.
bbd0e548
DW
876 *
877 * @param stdClass $assign record with extra cmidnumber
878 * @param array $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
879 * @return int 0 if ok, error code otherwise
880 */
e5403f8c 881function assign_grade_item_update($assign, $grades=null) {
bbd0e548
DW
882 global $CFG;
883 require_once($CFG->libdir.'/gradelib.php');
884
885 if (!isset($assign->courseid)) {
886 $assign->courseid = $assign->course;
887 }
888
889 $params = array('itemname'=>$assign->name, 'idnumber'=>$assign->cmidnumber);
890
891 if ($assign->grade > 0) {
892 $params['gradetype'] = GRADE_TYPE_VALUE;
893 $params['grademax'] = $assign->grade;
894 $params['grademin'] = 0;
895
896 } else if ($assign->grade < 0) {
897 $params['gradetype'] = GRADE_TYPE_SCALE;
898 $params['scaleid'] = -$assign->grade;
899
900 } else {
e5403f8c
DW
901 // Allow text comments only.
902 $params['gradetype'] = GRADE_TYPE_TEXT;
bbd0e548
DW
903 }
904
905 if ($grades === 'reset') {
906 $params['reset'] = true;
e5403f8c 907 $grades = null;
bbd0e548
DW
908 }
909
e5403f8c
DW
910 return grade_update('mod/assign',
911 $assign->courseid,
912 'mod',
913 'assign',
914 $assign->id,
915 0,
916 $grades,
917 $params);
bbd0e548
DW
918}
919
920/**
921 * Return grade for given user or all users.
922 *
923 * @param stdClass $assign record of assign with an additional cmidnumber
924 * @param int $userid optional user id, 0 means all users
925 * @return array array of grades, false if none
926 */
927function assign_get_user_grades($assign, $userid=0) {
928 global $CFG;
e5403f8c 929
bbd0e548
DW
930 require_once($CFG->dirroot . '/mod/assign/locallib.php');
931
79e7dfcc
RW
932 $cm = get_coursemodule_from_instance('assign', $assign->id, 0, false, MUST_EXIST);
933 $context = context_module::instance($cm->id);
934 $assignment = new assign($context, null, null);
b11808c7 935 $assignment->set_instance($assign);
bbd0e548
DW
936 return $assignment->get_user_grades_for_gradebook($userid);
937}
938
939/**
e5403f8c 940 * Update activity grades.
bbd0e548
DW
941 *
942 * @param stdClass $assign database record
943 * @param int $userid specific user only, 0 means all
944 * @param bool $nullifnone - not used
945 */
946function assign_update_grades($assign, $userid=0, $nullifnone=true) {
947 global $CFG;
948 require_once($CFG->libdir.'/gradelib.php');
949
950 if ($assign->grade == 0) {
951 assign_grade_item_update($assign);
952
953 } else if ($grades = assign_get_user_grades($assign, $userid)) {
e5403f8c 954 foreach ($grades as $k => $v) {
bbd0e548
DW
955 if ($v->rawgrade == -1) {
956 $grades[$k]->rawgrade = null;
957 }
958 }
959 assign_grade_item_update($assign, $grades);
960
961 } else {
962 assign_grade_item_update($assign);
963 }
964}
965
966/**
e5403f8c 967 * List the file areas that can be browsed.
bbd0e548
DW
968 *
969 * @param stdClass $course
970 * @param stdClass $cm
971 * @param stdClass $context
972 * @return array
973 */
6b8b0b2e 974function assign_get_file_areas($course, $cm, $context) {
bbd0e548
DW
975 global $CFG;
976 require_once($CFG->dirroot . '/mod/assign/locallib.php');
977 $areas = array();
978
979 $assignment = new assign($context, $cm, $course);
980 foreach ($assignment->get_submission_plugins() as $plugin) {
981 if ($plugin->is_visible()) {
982 $pluginareas = $plugin->get_file_areas();
983
984 if ($pluginareas) {
985 $areas = array_merge($areas, $pluginareas);
986 }
987 }
988 }
989 foreach ($assignment->get_feedback_plugins() as $plugin) {
990 if ($plugin->is_visible()) {
991 $pluginareas = $plugin->get_file_areas();
992
993 if ($pluginareas) {
994 $areas = array_merge($areas, $pluginareas);
995 }
996 }
997 }
998
999 return $areas;
1000}
1001
1002/**
1003 * File browsing support for assign module.
1004 *
1005 * @param file_browser $browser
1006 * @param object $areas
1007 * @param object $course
1008 * @param object $cm
1009 * @param object $context
1010 * @param string $filearea
1011 * @param int $itemid
1012 * @param string $filepath
1013 * @param string $filename
1014 * @return object file_info instance or null if not found
1015 */
e5403f8c
DW
1016function assign_get_file_info($browser,
1017 $areas,
1018 $course,
1019 $cm,
1020 $context,
1021 $filearea,
1022 $itemid,
1023 $filepath,
1024 $filename) {
bbd0e548
DW
1025 global $CFG;
1026 require_once($CFG->dirroot . '/mod/assign/locallib.php');
1027
1028 if ($context->contextlevel != CONTEXT_MODULE) {
1029 return null;
1030 }
1031
1032 $fs = get_file_storage();
1033 $filepath = is_null($filepath) ? '/' : $filepath;
1034 $filename = is_null($filename) ? '.' : $filename;
1035
e5403f8c 1036 // Need to find the plugin this belongs to.
bbd0e548
DW
1037 $assignment = new assign($context, $cm, $course);
1038 $pluginowner = null;
1039 foreach ($assignment->get_submission_plugins() as $plugin) {
1040 if ($plugin->is_visible()) {
1041 $pluginareas = $plugin->get_file_areas();
1042
1043 if (array_key_exists($filearea, $pluginareas)) {
1044 $pluginowner = $plugin;
1045 break;
1046 }
1047 }
1048 }
1049 if (!$pluginowner) {
1050 foreach ($assignment->get_feedback_plugins() as $plugin) {
1051 if ($plugin->is_visible()) {
1052 $pluginareas = $plugin->get_file_areas();
1053
1054 if (array_key_exists($filearea, $pluginareas)) {
1055 $pluginowner = $plugin;
1056 break;
1057 }
1058 }
1059 }
1060 }
1061
1062 if (!$pluginowner) {
1063 return null;
1064 }
1065
1066 $result = $pluginowner->get_file_info($browser, $filearea, $itemid, $filepath, $filename);
1067 return $result;
1068}
1069
1070/**
e5403f8c 1071 * Prints the complete info about a user's interaction with an assignment.
bbd0e548
DW
1072 *
1073 * @param stdClass $course
1074 * @param stdClass $user
1075 * @param stdClass $coursemodule
1076 * @param stdClass $assign the database assign record
1077 *
e5403f8c 1078 * This prints the submission summary and feedback summary for this student.
bbd0e548
DW
1079 */
1080function assign_user_complete($course, $user, $coursemodule, $assign) {
1081 global $CFG;
1082 require_once($CFG->dirroot . '/mod/assign/locallib.php');
e5403f8c 1083
bbd0e548
DW
1084 $context = context_module::instance($coursemodule->id);
1085
1086 $assignment = new assign($context, $coursemodule, $course);
1087
1088 echo $assignment->view_student_summary($user, false);
1089}
1090
1091/**
e5403f8c 1092 * Print the grade information for the assignment for this user.
9682626e 1093 *
bbd0e548
DW
1094 * @param stdClass $course
1095 * @param stdClass $user
1096 * @param stdClass $coursemodule
1097 * @param stdClass $assignment
1098 */
1099function assign_user_outline($course, $user, $coursemodule, $assignment) {
1100 global $CFG;
1101 require_once($CFG->libdir.'/gradelib.php');
1102 require_once($CFG->dirroot.'/grade/grading/lib.php');
1103
1104 $gradinginfo = grade_get_grades($course->id,
1105 'mod',
1106 'assign',
1107 $assignment->id,
1108 $user->id);
1109
1110 $gradingitem = $gradinginfo->items[0];
1111 $gradebookgrade = $gradingitem->grades[$user->id];
1112
1113 if (!$gradebookgrade) {
1114 return null;
1115 }
1116 $result = new stdClass();
1117 $result->info = get_string('outlinegrade', 'assign', $gradebookgrade->grade);
1118 $result->time = $gradebookgrade->dategraded;
1119
1120 return $result;
1121}
79ed4d84
DW
1122
1123/**
1124 * Obtains the automatic completion state for this module based on any conditions
1125 * in assign settings.
1126 *
1127 * @param object $course Course
1128 * @param object $cm Course-module
1129 * @param int $userid User ID
1130 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
1131 * @return bool True if completed, false if not, $type if conditions not set.
1132 */
3a66d425 1133function assign_get_completion_state($course, $cm, $userid, $type) {
e5403f8c 1134 global $CFG, $DB;
79ed4d84
DW
1135 require_once($CFG->dirroot . '/mod/assign/locallib.php');
1136
1137 $assign = new assign(null, $cm, $course);
1138
3a66d425
DP
1139 // If completion option is enabled, evaluate it and return true/false.
1140 if ($assign->get_instance()->completionsubmit) {
e5403f8c
DW
1141 $dbparams = array('assignment'=>$assign->get_instance()->id, 'userid'=>$userid);
1142 $submission = $DB->get_record('assign_submission', $dbparams, '*', IGNORE_MISSING);
79ed4d84
DW
1143 return $submission && $submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1144 } else {
3a66d425 1145 // Completion option is not enabled so just return $type.
79ed4d84
DW
1146 return $type;
1147 }
1148}