MDL-31270 mod_assign: introducing the assignment module
[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();
215 if ($assignment->duedate) {
216 if ($assignment->preventlatesubmissions) {
217 $isopen = ($assignment->allowsubmissionsfromdate <= $time && $time <= $assignment->duedate);
218 } else {
219 $isopen = ($assignment->allowsubmissionsfromdate <= $time);
220 }
221 }
222 if (empty($isopen) || empty($assignment->duedate)) {
223 $assignmentids[] = $assignment->id;
224 } else {
225 $assignmentids[] = $assignment->id;
226 }
227 }
228
229 if (empty($assignmentids)){
230 // no assignments to look at - we're done
231 return true;
232 }
233
234 $strduedate = get_string('duedate', 'assign');
235 $strduedateno = get_string('duedateno', 'assign');
236 $strgraded = get_string('graded', 'assign');
237 $strnotgradedyet = get_string('notgradedyet', 'assign');
238 $strnotsubmittedyet = get_string('notsubmittedyet', 'assign');
239 $strsubmitted = get_string('submitted', 'assign');
240 $strassignment = get_string('modulename', 'assign');
241 $strreviewed = get_string('reviewed','assign');
242
243
244 // NOTE: we do all possible database work here *outside* of the loop to ensure this scales
245 //
246 list($sqlassignmentids, $assignmentidparams) = $DB->get_in_or_equal($assignmentids);
247
248 // build up and array of unmarked submissions indexed by assignment id/ userid
249 // for use where the user has grading rights on assignment
250 $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
251 FROM {assign_submission} s LEFT JOIN {assign_grades} g ON s.userid = g.userid and s.assignment = g.assignment
252 WHERE g.timemodified = 0 OR s.timemodified > g.timemodified
253 AND s.assignment $sqlassignmentids", $assignmentidparams);
254
255 $unmarkedsubmissions = array();
256 foreach ($rs as $rd) {
257 $unmarkedsubmissions[$rd->assignment][$rd->userid] = $rd->id;
258 }
259 $rs->close();
260
261
262 // get all user submissions, indexed by assignment id
263 $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
264 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 = ?
265 AND a.id $sqlassignmentids", array_merge(array($USER->id, $USER->id), $assignmentidparams));
266
267 foreach ($assignments as $assignment) {
268 $str = '<div class="assign overview"><div class="name">'.$strassignment. ': '.
269 '<a '.($assignment->visible ? '':' class="dimmed"').
270 'title="'.$strassignment.'" href="'.$CFG->wwwroot.
271 '/mod/assign/view.php?id='.$assignment->coursemodule.'">'.
272 format_string($assignment->name).'</a></div>';
273 if ($assignment->duedate) {
274 $str .= '<div class="info">'.$strduedate.': '.userdate($assignment->duedate).'</div>';
275 } else {
276 $str .= '<div class="info">'.$strduedateno.'</div>';
277 }
278 $context = context_module::instance($assignment->coursemodule);
279 if (has_capability('mod/assign:grade', $context)) {
280
281 // count how many people can submit
282 $submissions = 0; // init
283 if ($students = get_enrolled_users($context, 'mod/assign:view', 0, 'u.id')) {
284 foreach ($students as $student) {
285 if (isset($unmarkedsubmissions[$assignment->id][$student->id])) {
286 $submissions++;
287 }
288 }
289 }
290
291 if ($submissions) {
292 $link = new moodle_url('/mod/assign/view.php', array('id'=>$assignment->coursemodule, 'action'=>'grading'));
293 $str .= '<div class="details"><a href="'.$link.'">'.get_string('submissionsnotgraded', 'assign', $submissions).'</a></div>';
294 }
295 } if (has_capability('mod/assign:submit', $context)) {
296 $str .= '<div class="details">';
297 $str .= get_string('mysubmission', 'assign');
298 $submission = $mysubmissions[$assignment->id];
299 if ($submission->offline) {
300 $str .= get_string('offline', 'assign');
301 } else if(!$submission->status || $submission->status == 'draft'){
302 $str .= $strnotsubmittedyet;
303 }else {
304 $str .= get_string('submissionstatus_' . $submission->status, 'assign');
305 }
306 if (!$submission->grade || $submission->grade < 0) {
307 $str .= ', ' . get_string('notgraded', 'assign');
308 } else {
309 $str .= ', ' . get_string('graded', 'assign');
310 }
311 $str .= '</div>';
312 }
313 $str .= '</div>';
314 if (empty($htmlarray[$assignment->course]['assign'])) {
315 $htmlarray[$assignment->course]['assign'] = $str;
316 } else {
317 $htmlarray[$assignment->course]['assign'] .= $str;
318 }
319 }
320}
321
322/**
323 * Print recent activity from all assignments in a given course
324 *
325 * This is used by the recent activity block
326 * @param mixed $course the course to print activity for
327 * @param bool $viewfullnames boolean to determine whether to show full names or not
328 * @param int $timestart the time the rendering started
329 */
330function assign_print_recent_activity($course, $viewfullnames, $timestart) {
331 global $CFG, $USER, $DB, $OUTPUT;
332
333 // do not use log table if possible, it may be huge
334
335 if (!$submissions = $DB->get_records_sql("SELECT asb.id, asb.timemodified, cm.id AS cmid, asb.userid,
336 u.firstname, u.lastname, u.email, u.picture
337 FROM {assign_submission} asb
338 JOIN {assign} a ON a.id = asb.assignment
339 JOIN {course_modules} cm ON cm.instance = a.id
340 JOIN {modules} md ON md.id = cm.module
341 JOIN {user} u ON u.id = asb.userid
342 WHERE asb.timemodified > ? AND
343 a.course = ? AND
344 md.name = 'assign'
345 ORDER BY asb.timemodified ASC", array($timestart, $course->id))) {
346 return false;
347 }
348
349 $modinfo = get_fast_modinfo($course); // no need pass this by reference as the return object already being cached
350 $show = array();
351 $grader = array();
352
353 foreach($submissions as $submission) {
354 if (!array_key_exists($submission->cmid, $modinfo->get_cms())) {
355 continue;
356 }
357 $cm = $modinfo->get_cm($submission->cmid);
358 if (!$cm->uservisible) {
359 continue;
360 }
361 if ($submission->userid == $USER->id) {
362 $show[] = $submission;
363 continue;
364 }
365
366 $context = context_module::instance($submission->cmid);
367 // the act of sumbitting of assignment may be considered private - only graders will see it if specified
368 if (empty($CFG->assign_showrecentsubmissions)) {
369 if (!array_key_exists($cm->id, $grader)) {
370 $grader[$cm->id] = has_capability('moodle/grade:viewall',$context);
371 }
372 if (!$grader[$cm->id]) {
373 continue;
374 }
375 }
376
377 $groupmode = groups_get_activity_groupmode($cm, $course);
378
379 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
380 if (isguestuser()) {
381 // shortcut - guest user does not belong into any group
382 continue;
383 }
384
385 if (is_null($modinfo->get_groups())) {
386 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
387 }
388
389 // this will be slow - show only users that share group with me in this cm
390 if (empty($modinfo->groups[$cm->id])) {
391 continue;
392 }
393 $usersgroups = groups_get_all_groups($course->id, $submission->userid, $cm->groupingid);
394 if (is_array($usersgroups)) {
395 $usersgroups = array_keys($usersgroups);
396 $intersect = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
397 if (empty($intersect)) {
398 continue;
399 }
400 }
401 }
402 $show[] = $submission;
403 }
404
405 if (empty($show)) {
406 return false;
407 }
408
409 echo $OUTPUT->heading(get_string('newsubmissions', 'assign').':', 3);
410
411 foreach ($show as $submission) {
412 $cm = $modinfo->get_cm($submission->cmid);
413 $link = $CFG->wwwroot.'/mod/assign/view.php?id='.$cm->id;
414 print_recent_activity_note($submission->timemodified, $submission, $cm->name, $link, false, $viewfullnames);
415 }
416
417 return true;
418}
419
420/**
421 * Returns all assignments since a given time
422 *
423 * @param array $activities The activity information is returned in this array
424 * @param int $index The current index in the activities array
425 * @param int $timestart The earliest activity to show
426 * @param int $courseid Limit the search to this course
427 * @param int $cmid The course module id
428 * @param int $userid Optional user id
429 * @param int $groupid Optional group id
430 * @return void
431 */
432function assign_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
433 global $CFG, $COURSE, $USER, $DB;
434
435 if ($COURSE->id == $courseid) {
436 $course = $COURSE;
437 } else {
438 $course = $DB->get_record('course', array('id'=>$courseid));
439 }
440
441 $modinfo = get_fast_modinfo($course); // no need pass this by reference as the return object already being cached
442
443 $cm = $modinfo->get_cm($cmid);
444 $params = array();
445 if ($userid) {
446 $userselect = "AND u.id = :userid";
447 $params['userid'] = $userid;
448 } else {
449 $userselect = "";
450 }
451
452 if ($groupid) {
453 $groupselect = "AND gm.groupid = :groupid";
454 $groupjoin = "JOIN {groups_members} gm ON gm.userid=u.id";
455 $params['groupid'] = $groupid;
456 } else {
457 $groupselect = "";
458 $groupjoin = "";
459 }
460
461 $params['cminstance'] = $cm->instance;
462 $params['timestart'] = $timestart;
463
464 $userfields = user_picture::fields('u', null, 'userid');
465
466 if (!$submissions = $DB->get_records_sql("SELECT asb.id, asb.timemodified,
467 $userfields
468 FROM {assign_submission} asb
469 JOIN {assign} a ON a.id = asb.assignment
470 JOIN {user} u ON u.id = asb.userid
471 $groupjoin
472 WHERE asb.timemodified > :timestart AND a.id = :cminstance
473 $userselect $groupselect
474 ORDER BY asb.timemodified ASC", $params)) {
475 return;
476 }
477
478 $groupmode = groups_get_activity_groupmode($cm, $course);
479 $cm_context = context_module::instance($cm->id);
480 $grader = has_capability('moodle/grade:viewall', $cm_context);
481 $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
482 $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
483
484 if (is_null($modinfo->get_groups())) {
485 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
486 }
487
488 $show = array();
489 $usersgroups = groups_get_all_groups($course->id, $USER->id, $cm->groupingid);
490 if (is_array($usersgroups)) {
491 $usersgroups = array_keys($usersgroups);
492 }
493 foreach($submissions as $submission) {
494 if ($submission->userid == $USER->id) {
495 $show[] = $submission;
496 continue;
497 }
498 // the act of submitting of assignment may be considered private - only graders will see it if specified
499 if (empty($CFG->assign_showrecentsubmissions)) {
500 if (!$grader) {
501 continue;
502 }
503 }
504
505 if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
506 if (isguestuser()) {
507 // shortcut - guest user does not belong into any group
508 continue;
509 }
510
511 // this will be slow - show only users that share group with me in this cm
512 if (empty($modinfo->groups[$cm->id])) {
513 continue;
514 }
515 if (is_array($usersgroups)) {
516 $intersect = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
517 if (empty($intersect)) {
518 continue;
519 }
520 }
521 }
522 $show[] = $submission;
523 }
524
525 if (empty($show)) {
526 return;
527 }
528
529 if ($grader) {
530 require_once($CFG->libdir.'/gradelib.php');
531 $userids = array();
532 foreach ($show as $id=>$submission) {
533 $userids[] = $submission->userid;
534
535 }
536 $grades = grade_get_grades($courseid, 'mod', 'assign', $cm->instance, $userids);
537 }
538
539 $aname = format_string($cm->name,true);
540 foreach ($show as $submission) {
541 $activity = new stdClass();
542
543 $activity->type = 'assign';
544 $activity->cmid = $cm->id;
545 $activity->name = $aname;
546 $activity->sectionnum = $cm->sectionnum;
547 $activity->timestamp = $submission->timemodified;
548 $activity->user = new stdClass();
549 if ($grader) {
550 $activity->grade = $grades->items[0]->grades[$submission->userid]->str_long_grade;
551 }
552
553 $userfields = explode(',', user_picture::fields());
554 foreach ($userfields as $userfield) {
555 if ($userfield == 'id') {
556 $activity->user->{$userfield} = $submission->userid; // aliased in SQL above
557 } else {
558 $activity->user->{$userfield} = $submission->{$userfield};
559 }
560 }
561 $activity->user->fullname = fullname($submission, $viewfullnames);
562
563 $activities[$index++] = $activity;
564 }
565
566 return;
567}
568
569/**
570 * Print recent activity from all assignments in a given course
571 *
572 * This is used by course/recent.php
573 * @param stdClass $activity
574 * @param int $courseid
575 * @param bool $detail
576 * @param array $modnames
577 */
578function assign_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
579 global $CFG, $OUTPUT;
580
581 echo '<table border="0" cellpadding="3" cellspacing="0" class="assignment-recent">';
582
583 echo "<tr><td class=\"userpicture\" valign=\"top\">";
584 echo $OUTPUT->user_picture($activity->user);
585 echo "</td><td>";
586
587 if ($detail) {
588 $modname = $modnames[$activity->type];
589 echo '<div class="title">';
590 echo "<img src=\"" . $OUTPUT->pix_url('icon', 'assign') . "\" ".
591 "class=\"icon\" alt=\"$modname\">";
592 echo "<a href=\"$CFG->wwwroot/mod/assign/view.php?id={$activity->cmid}\">{$activity->name}</a>";
593 echo '</div>';
594 }
595
596 if (isset($activity->grade)) {
597 echo '<div class="grade">';
598 echo get_string('grade').': ';
599 echo $activity->grade;
600 echo '</div>';
601 }
602
603 echo '<div class="user">';
604 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&amp;course=$courseid\">"
605 ."{$activity->user->fullname}</a> - ".userdate($activity->timestamp);
606 echo '</div>';
607
608 echo "</td></tr></table>";
609}
610
611/**
612 * Checks if a scale is being used by an assignment
613 *
614 * This is used by the backup code to decide whether to back up a scale
615 * @param int $assignmentid
616 * @param int $scaleid
617 * @return boolean True if the scale is used by the assignment
618 */
619function assign_scale_used($assignmentid, $scaleid) {
620 global $DB;
621
622 $return = false;
623 $rec = $DB->get_record('assign', array('id'=>$assignmentid,'grade'=>-$scaleid));
624
625 if (!empty($rec) && !empty($scaleid)) {
626 $return = true;
627 }
628
629 return $return;
630}
631
632/**
633 * Checks if scale is being used by any instance of assignment
634 *
635 * This is used to find out if scale used anywhere
636 * @param int $scaleid
637 * @return boolean True if the scale is used by any assignment
638 */
639function assign_scale_used_anywhere($scaleid) {
640 global $DB;
641
642 if ($scaleid and $DB->record_exists('assign', array('grade'=>-$scaleid))) {
643 return true;
644 } else {
645 return false;
646 }
647}
648
649/**
650 * function to list the actions that correspond to a view of this module
651 * This is used by the participation report
652 * @return array
653 */
654function assign_get_view_actions() {
655 return array('view submission', 'view feedback');
656}
657
658/**
659 * function to list the actions that correspond to a post of this module
660 * This is used by the participation report
661 * @return array
662 */
663function assign_get_post_actions() {
664 return array('upload', 'submit', 'submit for grading');
665}
666
667/**
668 * Call cron on the assign module
669 */
670function assign_cron() {
671 global $CFG;
672
673 require_once($CFG->dirroot . '/mod/assign/locallib.php');
674 //assignment::cron();
675 $plugins = get_plugin_list('assignsubmission');
676
677 foreach ($plugins as $name => $plugin) {
678 $disabled = get_config('assignsubmission_' . $name, 'disabled');
679 if (!$disabled) {
680 $class = 'assign_submission_' . $name;
681 require_once($CFG->dirroot . '/mod/assign/submission/' . $name . '/locallib.php');
682 $class::cron();
683 }
684 }
685 $plugins = get_plugin_list('assignfeedback');
686
687 foreach ($plugins as $name => $plugin) {
688 $disabled = get_config('assignfeedback_' . $name, 'disabled');
689 if (!$disabled) {
690 $class = 'assign_feedback_' . $name;
691 require_once($CFG->dirroot . '/mod/assign/feedback/' . $name . '/locallib.php');
692 $class::cron();
693 }
694 }
695}
696
697/**
698 * Returns all other capabilities used by this module.
699 * @return array Array of capability strings
700 */
701function assign_get_extra_capabilities() {
702 return array('gradereport/grader:view', 'moodle/grade:viewall', 'moodle/site:viewfullnames', 'moodle/site:config');
703}
704
705/**
706 * Create grade item for given assignment
707 *
708 * @param stdClass $assign record with extra cmidnumber
709 * @param array $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
710 * @return int 0 if ok, error code otherwise
711 */
712function assign_grade_item_update($assign, $grades=NULL) {
713 global $CFG;
714 require_once($CFG->libdir.'/gradelib.php');
715
716 if (!isset($assign->courseid)) {
717 $assign->courseid = $assign->course;
718 }
719
720 $params = array('itemname'=>$assign->name, 'idnumber'=>$assign->cmidnumber);
721
722 if ($assign->grade > 0) {
723 $params['gradetype'] = GRADE_TYPE_VALUE;
724 $params['grademax'] = $assign->grade;
725 $params['grademin'] = 0;
726
727 } else if ($assign->grade < 0) {
728 $params['gradetype'] = GRADE_TYPE_SCALE;
729 $params['scaleid'] = -$assign->grade;
730
731 } else {
732 $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
733 }
734
735 if ($grades === 'reset') {
736 $params['reset'] = true;
737 $grades = NULL;
738 }
739
740 return grade_update('mod/assign', $assign->courseid, 'mod', 'assign', $assign->id, 0, $grades, $params);
741}
742
743/**
744 * Return grade for given user or all users.
745 *
746 * @param stdClass $assign record of assign with an additional cmidnumber
747 * @param int $userid optional user id, 0 means all users
748 * @return array array of grades, false if none
749 */
750function assign_get_user_grades($assign, $userid=0) {
751 global $CFG;
752 require_once($CFG->dirroot . '/mod/assign/locallib.php');
753
754 $context = context_module::instance($assign->cmidnumber);
755 $assignment = new assign($context, null, null);
756 return $assignment->get_user_grades_for_gradebook($userid);
757}
758
759/**
760 * Update activity grades
761 *
762 * @param stdClass $assign database record
763 * @param int $userid specific user only, 0 means all
764 * @param bool $nullifnone - not used
765 */
766function assign_update_grades($assign, $userid=0, $nullifnone=true) {
767 global $CFG;
768 require_once($CFG->libdir.'/gradelib.php');
769
770 if ($assign->grade == 0) {
771 assign_grade_item_update($assign);
772
773 } else if ($grades = assign_get_user_grades($assign, $userid)) {
774 foreach($grades as $k=>$v) {
775 if ($v->rawgrade == -1) {
776 $grades[$k]->rawgrade = null;
777 }
778 }
779 assign_grade_item_update($assign, $grades);
780
781 } else {
782 assign_grade_item_update($assign);
783 }
784}
785
786/**
787 * List the file areas that can be browsed
788 *
789 * @param stdClass $course
790 * @param stdClass $cm
791 * @param stdClass $context
792 * @return array
793 */
794function mod_assign_get_file_areas($course, $cm, $context) {
795 global $CFG;
796 require_once($CFG->dirroot . '/mod/assign/locallib.php');
797 $areas = array();
798
799 $assignment = new assign($context, $cm, $course);
800 foreach ($assignment->get_submission_plugins() as $plugin) {
801 if ($plugin->is_visible()) {
802 $pluginareas = $plugin->get_file_areas();
803
804 if ($pluginareas) {
805 $areas = array_merge($areas, $pluginareas);
806 }
807 }
808 }
809 foreach ($assignment->get_feedback_plugins() as $plugin) {
810 if ($plugin->is_visible()) {
811 $pluginareas = $plugin->get_file_areas();
812
813 if ($pluginareas) {
814 $areas = array_merge($areas, $pluginareas);
815 }
816 }
817 }
818
819 return $areas;
820}
821
822/**
823 * File browsing support for assign module.
824 *
825 * @param file_browser $browser
826 * @param object $areas
827 * @param object $course
828 * @param object $cm
829 * @param object $context
830 * @param string $filearea
831 * @param int $itemid
832 * @param string $filepath
833 * @param string $filename
834 * @return object file_info instance or null if not found
835 */
836function mod_assign_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
837 global $CFG;
838 require_once($CFG->dirroot . '/mod/assign/locallib.php');
839
840 if ($context->contextlevel != CONTEXT_MODULE) {
841 return null;
842 }
843
844 $fs = get_file_storage();
845 $filepath = is_null($filepath) ? '/' : $filepath;
846 $filename = is_null($filename) ? '.' : $filename;
847
848 // need to find the plugin this belongs to
849 $assignment = new assign($context, $cm, $course);
850 $pluginowner = null;
851 foreach ($assignment->get_submission_plugins() as $plugin) {
852 if ($plugin->is_visible()) {
853 $pluginareas = $plugin->get_file_areas();
854
855 if (array_key_exists($filearea, $pluginareas)) {
856 $pluginowner = $plugin;
857 break;
858 }
859 }
860 }
861 if (!$pluginowner) {
862 foreach ($assignment->get_feedback_plugins() as $plugin) {
863 if ($plugin->is_visible()) {
864 $pluginareas = $plugin->get_file_areas();
865
866 if (array_key_exists($filearea, $pluginareas)) {
867 $pluginowner = $plugin;
868 break;
869 }
870 }
871 }
872 }
873
874 if (!$pluginowner) {
875 return null;
876 }
877
878 $result = $pluginowner->get_file_info($browser, $filearea, $itemid, $filepath, $filename);
879 return $result;
880}
881
882/**
883 * Prints the complete info about a user's interaction with an assignment
884 *
885 * @param stdClass $course
886 * @param stdClass $user
887 * @param stdClass $coursemodule
888 * @param stdClass $assign the database assign record
889 *
890 * This prints the submission summary and feedback summary for this student
891 */
892function assign_user_complete($course, $user, $coursemodule, $assign) {
893 global $CFG;
894 require_once($CFG->dirroot . '/mod/assign/locallib.php');
895 $context = context_module::instance($coursemodule->id);
896
897 $assignment = new assign($context, $coursemodule, $course);
898
899 echo $assignment->view_student_summary($user, false);
900}
901
902/**
903 * Print the grade information for the assignment for this user
904 *
905 * @param stdClass $course
906 * @param stdClass $user
907 * @param stdClass $coursemodule
908 * @param stdClass $assignment
909 */
910function assign_user_outline($course, $user, $coursemodule, $assignment) {
911 global $CFG;
912 require_once($CFG->libdir.'/gradelib.php');
913 require_once($CFG->dirroot.'/grade/grading/lib.php');
914
915 $gradinginfo = grade_get_grades($course->id,
916 'mod',
917 'assign',
918 $assignment->id,
919 $user->id);
920
921 $gradingitem = $gradinginfo->items[0];
922 $gradebookgrade = $gradingitem->grades[$user->id];
923
924 if (!$gradebookgrade) {
925 return null;
926 }
927 $result = new stdClass();
928 $result->info = get_string('outlinegrade', 'assign', $gradebookgrade->grade);
929 $result->time = $gradebookgrade->dategraded;
930
931 return $result;
932}