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