MDL-30876 filter_mediaplugin Wrapping the style sheets access with try & catch
[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
9682626e 328 * @param int $timestart the time the rendering started
bbd0e548
DW
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
cfc81f03
DW
353 $showrecentsubmissions = get_config('mod_assign', 'showrecentsubmissions');
354
bbd0e548
DW
355 foreach($submissions as $submission) {
356 if (!array_key_exists($submission->cmid, $modinfo->get_cms())) {
357 continue;
358 }
359 $cm = $modinfo->get_cm($submission->cmid);
360 if (!$cm->uservisible) {
361 continue;
362 }
363 if ($submission->userid == $USER->id) {
364 $show[] = $submission;
365 continue;
366 }
367
368 $context = context_module::instance($submission->cmid);
369 // the act of sumbitting of assignment may be considered private - only graders will see it if specified
cfc81f03 370 if (empty($showrecentsubmissions)) {
bbd0e548
DW
371 if (!array_key_exists($cm->id, $grader)) {
372 $grader[$cm->id] = has_capability('moodle/grade:viewall',$context);
373 }
374 if (!$grader[$cm->id]) {
375 continue;
376 }
377 }
378
379 $groupmode = groups_get_activity_groupmode($cm, $course);
380
381 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
382 if (isguestuser()) {
383 // shortcut - guest user does not belong into any group
384 continue;
385 }
386
387 if (is_null($modinfo->get_groups())) {
388 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
389 }
390
391 // this will be slow - show only users that share group with me in this cm
392 if (empty($modinfo->groups[$cm->id])) {
393 continue;
394 }
395 $usersgroups = groups_get_all_groups($course->id, $submission->userid, $cm->groupingid);
396 if (is_array($usersgroups)) {
397 $usersgroups = array_keys($usersgroups);
398 $intersect = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
399 if (empty($intersect)) {
400 continue;
401 }
402 }
403 }
404 $show[] = $submission;
405 }
406
407 if (empty($show)) {
408 return false;
409 }
410
411 echo $OUTPUT->heading(get_string('newsubmissions', 'assign').':', 3);
412
413 foreach ($show as $submission) {
414 $cm = $modinfo->get_cm($submission->cmid);
415 $link = $CFG->wwwroot.'/mod/assign/view.php?id='.$cm->id;
416 print_recent_activity_note($submission->timemodified, $submission, $cm->name, $link, false, $viewfullnames);
417 }
418
419 return true;
420}
421
422/**
423 * Returns all assignments since a given time
c06b2127 424 *
bbd0e548
DW
425 * @param array $activities The activity information is returned in this array
426 * @param int $index The current index in the activities array
427 * @param int $timestart The earliest activity to show
428 * @param int $courseid Limit the search to this course
429 * @param int $cmid The course module id
430 * @param int $userid Optional user id
431 * @param int $groupid Optional group id
432 * @return void
433 */
434function assign_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
435 global $CFG, $COURSE, $USER, $DB;
436
437 if ($COURSE->id == $courseid) {
438 $course = $COURSE;
439 } else {
440 $course = $DB->get_record('course', array('id'=>$courseid));
441 }
442
443 $modinfo = get_fast_modinfo($course); // no need pass this by reference as the return object already being cached
444
445 $cm = $modinfo->get_cm($cmid);
446 $params = array();
447 if ($userid) {
448 $userselect = "AND u.id = :userid";
449 $params['userid'] = $userid;
450 } else {
451 $userselect = "";
452 }
453
454 if ($groupid) {
455 $groupselect = "AND gm.groupid = :groupid";
456 $groupjoin = "JOIN {groups_members} gm ON gm.userid=u.id";
457 $params['groupid'] = $groupid;
458 } else {
459 $groupselect = "";
460 $groupjoin = "";
461 }
462
463 $params['cminstance'] = $cm->instance;
464 $params['timestart'] = $timestart;
465
466 $userfields = user_picture::fields('u', null, 'userid');
467
468 if (!$submissions = $DB->get_records_sql("SELECT asb.id, asb.timemodified,
469 $userfields
470 FROM {assign_submission} asb
471 JOIN {assign} a ON a.id = asb.assignment
472 JOIN {user} u ON u.id = asb.userid
473 $groupjoin
474 WHERE asb.timemodified > :timestart AND a.id = :cminstance
475 $userselect $groupselect
476 ORDER BY asb.timemodified ASC", $params)) {
477 return;
478 }
479
480 $groupmode = groups_get_activity_groupmode($cm, $course);
481 $cm_context = context_module::instance($cm->id);
482 $grader = has_capability('moodle/grade:viewall', $cm_context);
483 $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
484 $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
485
486 if (is_null($modinfo->get_groups())) {
487 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
488 }
489
cfc81f03 490 $showrecentsubmissions = get_config('mod_assign', 'showrecentsubmissions');
bbd0e548
DW
491 $show = array();
492 $usersgroups = groups_get_all_groups($course->id, $USER->id, $cm->groupingid);
493 if (is_array($usersgroups)) {
494 $usersgroups = array_keys($usersgroups);
495 }
496 foreach($submissions as $submission) {
497 if ($submission->userid == $USER->id) {
498 $show[] = $submission;
499 continue;
500 }
501 // the act of submitting of assignment may be considered private - only graders will see it if specified
cfc81f03 502 if (empty($showrecentsubmissions)) {
bbd0e548
DW
503 if (!$grader) {
504 continue;
505 }
506 }
507
508 if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
509 if (isguestuser()) {
510 // shortcut - guest user does not belong into any group
511 continue;
512 }
513
514 // this will be slow - show only users that share group with me in this cm
515 if (empty($modinfo->groups[$cm->id])) {
516 continue;
517 }
518 if (is_array($usersgroups)) {
519 $intersect = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
520 if (empty($intersect)) {
521 continue;
522 }
523 }
524 }
525 $show[] = $submission;
526 }
527
528 if (empty($show)) {
529 return;
530 }
531
532 if ($grader) {
533 require_once($CFG->libdir.'/gradelib.php');
534 $userids = array();
535 foreach ($show as $id=>$submission) {
536 $userids[] = $submission->userid;
537
538 }
539 $grades = grade_get_grades($courseid, 'mod', 'assign', $cm->instance, $userids);
540 }
541
542 $aname = format_string($cm->name,true);
543 foreach ($show as $submission) {
544 $activity = new stdClass();
545
546 $activity->type = 'assign';
547 $activity->cmid = $cm->id;
548 $activity->name = $aname;
549 $activity->sectionnum = $cm->sectionnum;
550 $activity->timestamp = $submission->timemodified;
551 $activity->user = new stdClass();
552 if ($grader) {
553 $activity->grade = $grades->items[0]->grades[$submission->userid]->str_long_grade;
554 }
555
556 $userfields = explode(',', user_picture::fields());
557 foreach ($userfields as $userfield) {
558 if ($userfield == 'id') {
559 $activity->user->{$userfield} = $submission->userid; // aliased in SQL above
560 } else {
561 $activity->user->{$userfield} = $submission->{$userfield};
562 }
563 }
564 $activity->user->fullname = fullname($submission, $viewfullnames);
565
566 $activities[$index++] = $activity;
567 }
568
569 return;
570}
571
572/**
573 * Print recent activity from all assignments in a given course
574 *
575 * This is used by course/recent.php
576 * @param stdClass $activity
577 * @param int $courseid
578 * @param bool $detail
579 * @param array $modnames
580 */
581function assign_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
582 global $CFG, $OUTPUT;
583
584 echo '<table border="0" cellpadding="3" cellspacing="0" class="assignment-recent">';
585
586 echo "<tr><td class=\"userpicture\" valign=\"top\">";
587 echo $OUTPUT->user_picture($activity->user);
588 echo "</td><td>";
589
590 if ($detail) {
591 $modname = $modnames[$activity->type];
592 echo '<div class="title">';
593 echo "<img src=\"" . $OUTPUT->pix_url('icon', 'assign') . "\" ".
594 "class=\"icon\" alt=\"$modname\">";
595 echo "<a href=\"$CFG->wwwroot/mod/assign/view.php?id={$activity->cmid}\">{$activity->name}</a>";
596 echo '</div>';
597 }
598
599 if (isset($activity->grade)) {
600 echo '<div class="grade">';
601 echo get_string('grade').': ';
602 echo $activity->grade;
603 echo '</div>';
604 }
605
606 echo '<div class="user">';
607 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&amp;course=$courseid\">"
608 ."{$activity->user->fullname}</a> - ".userdate($activity->timestamp);
609 echo '</div>';
610
611 echo "</td></tr></table>";
612}
613
614/**
615 * Checks if a scale is being used by an assignment
616 *
617 * This is used by the backup code to decide whether to back up a scale
9682626e 618 * @param int $assignmentid
bbd0e548
DW
619 * @param int $scaleid
620 * @return boolean True if the scale is used by the assignment
621 */
622function assign_scale_used($assignmentid, $scaleid) {
623 global $DB;
624
625 $return = false;
626 $rec = $DB->get_record('assign', array('id'=>$assignmentid,'grade'=>-$scaleid));
627
628 if (!empty($rec) && !empty($scaleid)) {
629 $return = true;
630 }
631
632 return $return;
633}
634
635/**
636 * Checks if scale is being used by any instance of assignment
637 *
638 * This is used to find out if scale used anywhere
9682626e 639 * @param int $scaleid
bbd0e548
DW
640 * @return boolean True if the scale is used by any assignment
641 */
642function assign_scale_used_anywhere($scaleid) {
643 global $DB;
644
645 if ($scaleid and $DB->record_exists('assign', array('grade'=>-$scaleid))) {
646 return true;
647 } else {
648 return false;
649 }
650}
651
652/**
653 * function to list the actions that correspond to a view of this module
654 * This is used by the participation report
655 * @return array
656 */
657function assign_get_view_actions() {
658 return array('view submission', 'view feedback');
659}
660
661/**
662 * function to list the actions that correspond to a post of this module
663 * This is used by the participation report
664 * @return array
665 */
666function assign_get_post_actions() {
667 return array('upload', 'submit', 'submit for grading');
668}
669
670/**
671 * Call cron on the assign module
672 */
673function assign_cron() {
674 global $CFG;
675
676 require_once($CFG->dirroot . '/mod/assign/locallib.php');
75f87a57
DW
677 assign::cron();
678
bbd0e548
DW
679 $plugins = get_plugin_list('assignsubmission');
680
681 foreach ($plugins as $name => $plugin) {
682 $disabled = get_config('assignsubmission_' . $name, 'disabled');
683 if (!$disabled) {
684 $class = 'assign_submission_' . $name;
685 require_once($CFG->dirroot . '/mod/assign/submission/' . $name . '/locallib.php');
686 $class::cron();
687 }
688 }
689 $plugins = get_plugin_list('assignfeedback');
690
691 foreach ($plugins as $name => $plugin) {
692 $disabled = get_config('assignfeedback_' . $name, 'disabled');
693 if (!$disabled) {
694 $class = 'assign_feedback_' . $name;
695 require_once($CFG->dirroot . '/mod/assign/feedback/' . $name . '/locallib.php');
696 $class::cron();
697 }
698 }
699}
700
701/**
702 * Returns all other capabilities used by this module.
703 * @return array Array of capability strings
704 */
705function assign_get_extra_capabilities() {
706 return array('gradereport/grader:view', 'moodle/grade:viewall', 'moodle/site:viewfullnames', 'moodle/site:config');
707}
708
709/**
710 * Create grade item for given assignment
711 *
712 * @param stdClass $assign record with extra cmidnumber
713 * @param array $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
714 * @return int 0 if ok, error code otherwise
715 */
716function assign_grade_item_update($assign, $grades=NULL) {
717 global $CFG;
718 require_once($CFG->libdir.'/gradelib.php');
719
720 if (!isset($assign->courseid)) {
721 $assign->courseid = $assign->course;
722 }
723
724 $params = array('itemname'=>$assign->name, 'idnumber'=>$assign->cmidnumber);
725
726 if ($assign->grade > 0) {
727 $params['gradetype'] = GRADE_TYPE_VALUE;
728 $params['grademax'] = $assign->grade;
729 $params['grademin'] = 0;
730
731 } else if ($assign->grade < 0) {
732 $params['gradetype'] = GRADE_TYPE_SCALE;
733 $params['scaleid'] = -$assign->grade;
734
735 } else {
736 $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
737 }
738
739 if ($grades === 'reset') {
740 $params['reset'] = true;
741 $grades = NULL;
742 }
743
744 return grade_update('mod/assign', $assign->courseid, 'mod', 'assign', $assign->id, 0, $grades, $params);
745}
746
747/**
748 * Return grade for given user or all users.
749 *
750 * @param stdClass $assign record of assign with an additional cmidnumber
751 * @param int $userid optional user id, 0 means all users
752 * @return array array of grades, false if none
753 */
754function assign_get_user_grades($assign, $userid=0) {
755 global $CFG;
756 require_once($CFG->dirroot . '/mod/assign/locallib.php');
757
b11808c7
DW
758 $assignment = new assign(null, null, null);
759 $assignment->set_instance($assign);
bbd0e548
DW
760 return $assignment->get_user_grades_for_gradebook($userid);
761}
762
763/**
764 * Update activity grades
765 *
766 * @param stdClass $assign database record
767 * @param int $userid specific user only, 0 means all
768 * @param bool $nullifnone - not used
769 */
770function assign_update_grades($assign, $userid=0, $nullifnone=true) {
771 global $CFG;
772 require_once($CFG->libdir.'/gradelib.php');
773
774 if ($assign->grade == 0) {
775 assign_grade_item_update($assign);
776
777 } else if ($grades = assign_get_user_grades($assign, $userid)) {
778 foreach($grades as $k=>$v) {
779 if ($v->rawgrade == -1) {
780 $grades[$k]->rawgrade = null;
781 }
782 }
783 assign_grade_item_update($assign, $grades);
784
785 } else {
786 assign_grade_item_update($assign);
787 }
788}
789
790/**
791 * List the file areas that can be browsed
792 *
793 * @param stdClass $course
794 * @param stdClass $cm
795 * @param stdClass $context
796 * @return array
797 */
6b8b0b2e 798function assign_get_file_areas($course, $cm, $context) {
bbd0e548
DW
799 global $CFG;
800 require_once($CFG->dirroot . '/mod/assign/locallib.php');
801 $areas = array();
802
803 $assignment = new assign($context, $cm, $course);
804 foreach ($assignment->get_submission_plugins() as $plugin) {
805 if ($plugin->is_visible()) {
806 $pluginareas = $plugin->get_file_areas();
807
808 if ($pluginareas) {
809 $areas = array_merge($areas, $pluginareas);
810 }
811 }
812 }
813 foreach ($assignment->get_feedback_plugins() as $plugin) {
814 if ($plugin->is_visible()) {
815 $pluginareas = $plugin->get_file_areas();
816
817 if ($pluginareas) {
818 $areas = array_merge($areas, $pluginareas);
819 }
820 }
821 }
822
823 return $areas;
824}
825
826/**
827 * File browsing support for assign module.
828 *
829 * @param file_browser $browser
830 * @param object $areas
831 * @param object $course
832 * @param object $cm
833 * @param object $context
834 * @param string $filearea
835 * @param int $itemid
836 * @param string $filepath
837 * @param string $filename
838 * @return object file_info instance or null if not found
839 */
6b8b0b2e 840function assign_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
bbd0e548
DW
841 global $CFG;
842 require_once($CFG->dirroot . '/mod/assign/locallib.php');
843
844 if ($context->contextlevel != CONTEXT_MODULE) {
845 return null;
846 }
847
848 $fs = get_file_storage();
849 $filepath = is_null($filepath) ? '/' : $filepath;
850 $filename = is_null($filename) ? '.' : $filename;
851
852 // need to find the plugin this belongs to
853 $assignment = new assign($context, $cm, $course);
854 $pluginowner = null;
855 foreach ($assignment->get_submission_plugins() as $plugin) {
856 if ($plugin->is_visible()) {
857 $pluginareas = $plugin->get_file_areas();
858
859 if (array_key_exists($filearea, $pluginareas)) {
860 $pluginowner = $plugin;
861 break;
862 }
863 }
864 }
865 if (!$pluginowner) {
866 foreach ($assignment->get_feedback_plugins() as $plugin) {
867 if ($plugin->is_visible()) {
868 $pluginareas = $plugin->get_file_areas();
869
870 if (array_key_exists($filearea, $pluginareas)) {
871 $pluginowner = $plugin;
872 break;
873 }
874 }
875 }
876 }
877
878 if (!$pluginowner) {
879 return null;
880 }
881
882 $result = $pluginowner->get_file_info($browser, $filearea, $itemid, $filepath, $filename);
883 return $result;
884}
885
886/**
887 * Prints the complete info about a user's interaction with an assignment
888 *
889 * @param stdClass $course
890 * @param stdClass $user
891 * @param stdClass $coursemodule
892 * @param stdClass $assign the database assign record
893 *
894 * This prints the submission summary and feedback summary for this student
895 */
896function assign_user_complete($course, $user, $coursemodule, $assign) {
897 global $CFG;
898 require_once($CFG->dirroot . '/mod/assign/locallib.php');
899 $context = context_module::instance($coursemodule->id);
900
901 $assignment = new assign($context, $coursemodule, $course);
902
903 echo $assignment->view_student_summary($user, false);
904}
905
906/**
907 * Print the grade information for the assignment for this user
9682626e 908 *
bbd0e548
DW
909 * @param stdClass $course
910 * @param stdClass $user
911 * @param stdClass $coursemodule
912 * @param stdClass $assignment
913 */
914function assign_user_outline($course, $user, $coursemodule, $assignment) {
915 global $CFG;
916 require_once($CFG->libdir.'/gradelib.php');
917 require_once($CFG->dirroot.'/grade/grading/lib.php');
918
919 $gradinginfo = grade_get_grades($course->id,
920 'mod',
921 'assign',
922 $assignment->id,
923 $user->id);
924
925 $gradingitem = $gradinginfo->items[0];
926 $gradebookgrade = $gradingitem->grades[$user->id];
927
928 if (!$gradebookgrade) {
929 return null;
930 }
931 $result = new stdClass();
932 $result->info = get_string('outlinegrade', 'assign', $gradebookgrade->grade);
933 $result->time = $gradebookgrade->dategraded;
934
935 return $result;
936}