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