weekly release 3.2dev
[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
418a0d04
JP
90/**
91 * This standard function will check all instances of this module
92 * and make sure there are up-to-date events created for each of them.
93 * If courseid = 0, then every assignment event in the site is checked, else
94 * only assignment events belonging to the course specified are checked.
95 *
96 * @param int $courseid
97 * @return bool
98 */
99function assign_refresh_events($courseid = 0) {
100 global $CFG, $DB;
101 require_once($CFG->dirroot . '/mod/assign/locallib.php');
102
103 if ($courseid) {
104 // Make sure that the course id is numeric.
105 if (!is_numeric($courseid)) {
106 return false;
107 }
108 if (!$assigns = $DB->get_records('assign', array('course' => $courseid))) {
109 return false;
110 }
111 // Get course from courseid parameter.
112 if (!$course = $DB->get_record('course', array('id' => $courseid), '*')) {
113 return false;
114 }
115 } else {
116 if (!$assigns = $DB->get_records('assign')) {
117 return false;
118 }
119 }
120 foreach ($assigns as $assign) {
121 // Use assignment's course column if courseid parameter is not given.
122 if (!$courseid) {
123 $courseid = $assign->course;
124 if (!$course = $DB->get_record('course', array('id' => $courseid), '*')) {
125 continue;
126 }
127 }
128 if (!$cm = get_coursemodule_from_instance('assign', $assign->id, $courseid, false)) {
129 continue;
130 }
131 $context = context_module::instance($cm->id);
132 $assignment = new assign($context, $cm, $course);
133 $assignment->update_calendar($cm->id);
134 }
135
136 return true;
137}
138
d38dc52f
RW
139/**
140 * Removes all grades from gradebook
141 *
142 * @param int $courseid The ID of the course to reset
143 * @param string $type Optional type of assignment to limit the reset to a particular assignment type
144 */
145function assign_reset_gradebook($courseid, $type='') {
146 global $CFG, $DB;
147
e5403f8c 148 $params = array('moduletype'=>'assign', 'courseid'=>$courseid);
d38dc52f
RW
149 $sql = 'SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid
150 FROM {assign} a, {course_modules} cm, {modules} m
151 WHERE m.name=:moduletype AND m.id=cm.module AND cm.instance=a.id AND a.course=:courseid';
152
e5403f8c 153 if ($assignments = $DB->get_records_sql($sql, $params)) {
d38dc52f
RW
154 foreach ($assignments as $assignment) {
155 assign_grade_item_update($assignment, 'reset');
156 }
157 }
158}
159
160/**
161 * Implementation of the function for printing the form elements that control
162 * whether the course reset functionality affects the assignment.
1561a37c 163 * @param moodleform $mform form passed by reference
d38dc52f
RW
164 */
165function assign_reset_course_form_definition(&$mform) {
166 $mform->addElement('header', 'assignheader', get_string('modulenameplural', 'assign'));
e5403f8c
DW
167 $name = get_string('deleteallsubmissions', 'assign');
168 $mform->addElement('advcheckbox', 'reset_assign_submissions', $name);
d38dc52f
RW
169}
170
171/**
172 * Course reset form defaults.
173 * @param object $course
174 * @return array
175 */
176function assign_reset_course_form_defaults($course) {
177 return array('reset_assign_submissions'=>1);
178}
179
bbd0e548
DW
180/**
181 * Update an assignment instance
182 *
183 * This is done by calling the update_instance() method of the assignment type class
184 * @param stdClass $data
1561a37c 185 * @param stdClass $form - unused
bbd0e548
DW
186 * @return object
187 */
dd5d933f 188function assign_update_instance(stdClass $data, $form) {
bbd0e548
DW
189 global $CFG;
190 require_once($CFG->dirroot . '/mod/assign/locallib.php');
191 $context = context_module::instance($data->coursemodule);
192 $assignment = new assign($context, null, null);
193 return $assignment->update_instance($data);
194}
195
196/**
197 * Return the list if Moodle features this module supports
198 *
199 * @param string $feature FEATURE_xx constant for requested feature
200 * @return mixed True if module supports feature, null if doesn't know
201 */
202function assign_supports($feature) {
203 switch($feature) {
df211804
DW
204 case FEATURE_GROUPS:
205 return true;
206 case FEATURE_GROUPINGS:
207 return true;
df211804
DW
208 case FEATURE_MOD_INTRO:
209 return true;
210 case FEATURE_COMPLETION_TRACKS_VIEWS:
211 return true;
212 case FEATURE_COMPLETION_HAS_RULES:
213 return true;
214 case FEATURE_GRADE_HAS_GRADE:
215 return true;
216 case FEATURE_GRADE_OUTCOMES:
217 return true;
218 case FEATURE_BACKUP_MOODLE2:
219 return true;
220 case FEATURE_SHOW_DESCRIPTION:
221 return true;
222 case FEATURE_ADVANCED_GRADING:
223 return true;
224 case FEATURE_PLAGIARISM:
225 return true;
226
227 default:
228 return null;
bbd0e548
DW
229 }
230}
231
232/**
233 * Lists all gradable areas for the advanced grading methods gramework
234 *
235 * @return array('string'=>'string') An array with area names as keys and descriptions as values
236 */
237function assign_grading_areas_list() {
238 return array('submissions'=>get_string('submissions', 'assign'));
239}
240
241
242/**
243 * extend an assigment navigation settings
244 *
245 * @param settings_navigation $settings
246 * @param navigation_node $navref
247 * @return void
248 */
249function assign_extend_settings_navigation(settings_navigation $settings, navigation_node $navref) {
b473171a 250 global $PAGE, $DB;
bbd0e548
DW
251
252 $cm = $PAGE->cm;
253 if (!$cm) {
254 return;
255 }
256
257 $context = $cm->context;
258 $course = $PAGE->course;
259
bbd0e548
DW
260 if (!$course) {
261 return;
262 }
263
e5403f8c
DW
264 // Link to gradebook.
265 if (has_capability('gradereport/grader:view', $cm->context) &&
266 has_capability('moodle/grade:viewall', $cm->context)) {
267 $link = new moodle_url('/grade/report/grader/index.php', array('id' => $course->id));
268 $linkname = get_string('viewgradebook', 'assign');
269 $node = $navref->add($linkname, $link, navigation_node::TYPE_SETTING);
270 }
bbd0e548 271
e5403f8c 272 // Link to download all submissions.
4a47008c 273 if (has_any_capability(array('mod/assign:grade', 'mod/assign:viewgrades'), $context)) {
e5403f8c
DW
274 $link = new moodle_url('/mod/assign/view.php', array('id' => $cm->id, 'action'=>'grading'));
275 $node = $navref->add(get_string('viewgrading', 'assign'), $link, navigation_node::TYPE_SETTING);
bbd0e548 276
e5403f8c
DW
277 $link = new moodle_url('/mod/assign/view.php', array('id' => $cm->id, 'action'=>'downloadall'));
278 $node = $navref->add(get_string('downloadall', 'assign'), $link, navigation_node::TYPE_SETTING);
279 }
bbd0e548 280
e5403f8c
DW
281 if (has_capability('mod/assign:revealidentities', $context)) {
282 $dbparams = array('id'=>$cm->instance);
283 $assignment = $DB->get_record('assign', $dbparams, 'blindmarking, revealidentities');
b473171a 284
e5403f8c
DW
285 if ($assignment && $assignment->blindmarking && !$assignment->revealidentities) {
286 $urlparams = array('id' => $cm->id, 'action'=>'revealidentities');
287 $url = new moodle_url('/mod/assign/view.php', $urlparams);
288 $linkname = get_string('revealidentities', 'assign');
289 $node = $navref->add($linkname, $url, navigation_node::TYPE_SETTING);
290 }
291 }
bbd0e548
DW
292}
293
bbd0e548
DW
294/**
295 * Add a get_coursemodule_info function in case any assignment type wants to add 'extra' information
296 * for the course (see resource).
297 *
298 * Given a course_module object, this function returns any "extra" information that may be needed
299 * when printing this activity in a course listing. See get_array_of_activities() in course/lib.php.
300 *
301 * @param stdClass $coursemodule The coursemodule object (record).
e5403f8c
DW
302 * @return cached_cm_info An object on information that the courses
303 * will know about (most noticeably, an icon).
bbd0e548
DW
304 */
305function assign_get_coursemodule_info($coursemodule) {
306 global $CFG, $DB;
307
e5403f8c 308 $dbparams = array('id'=>$coursemodule->instance);
b618f2d9 309 $fields = 'id, name, alwaysshowdescription, allowsubmissionsfromdate, intro, introformat';
e5403f8c 310 if (! $assignment = $DB->get_record('assign', $dbparams, $fields)) {
bbd0e548
DW
311 return false;
312 }
313
314 $result = new cached_cm_info();
315 $result->name = $assignment->name;
316 if ($coursemodule->showdescription) {
317 if ($assignment->alwaysshowdescription || time() > $assignment->allowsubmissionsfromdate) {
318 // Convert intro to html. Do not filter cached version, filters run at display time.
319 $result->content = format_module_intro('assign', $assignment, $coursemodule->id, false);
320 }
321 }
322 return $result;
323}
324
bbd0e548
DW
325/**
326 * Return a list of page types
327 * @param string $pagetype current page type
328 * @param stdClass $parentcontext Block's parent context
329 * @param stdClass $currentcontext Current context of block
330 */
331function assign_page_type_list($pagetype, $parentcontext, $currentcontext) {
539af602 332 $modulepagetype = array(
bbd0e548
DW
333 'mod-assign-*' => get_string('page-mod-assign-x', 'assign'),
334 'mod-assign-view' => get_string('page-mod-assign-view', 'assign'),
335 );
539af602 336 return $modulepagetype;
bbd0e548
DW
337}
338
339/**
340 * Print an overview of all assignments
341 * for the courses.
342 *
343 * @param mixed $courses The list of courses to print the overview for
344 * @param array $htmlarray The array of html to return
a7366bc8
AA
345 *
346 * @return true
bbd0e548
DW
347 */
348function assign_print_overview($courses, &$htmlarray) {
a7366bc8 349 global $CFG, $DB;
bbd0e548
DW
350
351 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
a7366bc8 352 return true;
bbd0e548
DW
353 }
354
e5403f8c 355 if (!$assignments = get_all_instances_in_courses('assign', $courses)) {
a7366bc8 356 return true;
bbd0e548
DW
357 }
358
359 $assignmentids = array();
360
e5403f8c 361 // Do assignment_base::isopen() here without loading the whole thing for speed.
bbd0e548
DW
362 foreach ($assignments as $key => $assignment) {
363 $time = time();
710f1a34 364 $isopen = false;
bbd0e548 365 if ($assignment->duedate) {
9e795179
DW
366 $duedate = false;
367 if ($assignment->cutoffdate) {
368 $duedate = $assignment->cutoffdate;
369 }
370 if ($duedate) {
371 $isopen = ($assignment->allowsubmissionsfromdate <= $time && $time <= $duedate);
372 } else {
373 $isopen = ($assignment->allowsubmissionsfromdate <= $time);
bbd0e548
DW
374 }
375 }
da099b12 376 if ($isopen) {
bbd0e548
DW
377 $assignmentids[] = $assignment->id;
378 }
379 }
380
e5403f8c
DW
381 if (empty($assignmentids)) {
382 // No assignments to look at - we're done.
bbd0e548
DW
383 return true;
384 }
385
d43b3109
DW
386 // Definitely something to print, now include the constants we need.
387 require_once($CFG->dirroot . '/mod/assign/locallib.php');
388
bbd0e548 389 $strduedate = get_string('duedate', 'assign');
9e795179
DW
390 $strcutoffdate = get_string('nosubmissionsacceptedafter', 'assign');
391 $strnolatesubmissions = get_string('nolatesubmissions', 'assign');
392 $strduedateno = get_string('duedateno', 'assign');
bbd0e548 393 $strassignment = get_string('modulename', 'assign');
bbd0e548 394
e5403f8c 395 // We do all possible database work here *outside* of the loop to ensure this scales.
bbd0e548
DW
396 list($sqlassignmentids, $assignmentidparams) = $DB->get_in_or_equal($assignmentids);
397
b8d3aa8f
DW
398 $mysubmissions = null;
399 $unmarkedsubmissions = null;
400
bbd0e548 401 foreach ($assignments as $assignment) {
a7366bc8 402
e5403f8c 403 // Do not show assignments that are not open.
da099b12
DW
404 if (!in_array($assignment->id, $assignmentids)) {
405 continue;
406 }
a7366bc8
AA
407
408 $context = context_module::instance($assignment->coursemodule);
409
410 // Does the submission status of the assignment require notification?
411 if (has_capability('mod/assign:submit', $context)) {
412 // Does the submission status of the assignment require notification?
413 $submitdetails = assign_get_mysubmission_details_for_print_overview($mysubmissions, $sqlassignmentids,
414 $assignmentidparams, $assignment);
415 } else {
416 $submitdetails = false;
417 }
418
419 if (has_capability('mod/assign:grade', $context)) {
420 // Does the grading status of the assignment require notification ?
421 $gradedetails = assign_get_grade_details_for_print_overview($unmarkedsubmissions, $sqlassignmentids,
422 $assignmentidparams, $assignment, $context);
423 } else {
424 $gradedetails = false;
425 }
426
427 if (empty($submitdetails) && empty($gradedetails)) {
428 // There is no need to display this assignment as there is nothing to notify.
429 continue;
430 }
431
e5403f8c
DW
432 $dimmedclass = '';
433 if (!$assignment->visible) {
434 $dimmedclass = ' class="dimmed"';
435 }
436 $href = $CFG->wwwroot . '/mod/assign/view.php?id=' . $assignment->coursemodule;
a7366bc8 437 $basestr = '<div class="assign overview">' .
e5403f8c
DW
438 '<div class="name">' .
439 $strassignment . ': '.
440 '<a ' . $dimmedclass .
441 'title="' . $strassignment . '" ' .
442 'href="' . $href . '">' .
443 format_string($assignment->name) .
444 '</a></div>';
bbd0e548 445 if ($assignment->duedate) {
e5403f8c 446 $userdate = userdate($assignment->duedate);
a7366bc8 447 $basestr .= '<div class="info">' . $strduedate . ': ' . $userdate . '</div>';
bbd0e548 448 } else {
a7366bc8 449 $basestr .= '<div class="info">' . $strduedateno . '</div>';
bbd0e548 450 }
9e795179
DW
451 if ($assignment->cutoffdate) {
452 if ($assignment->cutoffdate == $assignment->duedate) {
a7366bc8 453 $basestr .= '<div class="info">' . $strnolatesubmissions . '</div>';
9e795179 454 } else {
e5403f8c 455 $userdate = userdate($assignment->cutoffdate);
a7366bc8 456 $basestr .= '<div class="info">' . $strcutoffdate . ': ' . $userdate . '</div>';
9e795179
DW
457 }
458 }
d77731d6 459
a7366bc8
AA
460 // Show only relevant information.
461 if (!empty($submitdetails)) {
462 $basestr .= $submitdetails;
e5403f8c 463 }
d77731d6 464
a7366bc8
AA
465 if (!empty($gradedetails)) {
466 $basestr .= $gradedetails;
bbd0e548 467 }
a7366bc8
AA
468 $basestr .= '</div>';
469
bbd0e548 470 if (empty($htmlarray[$assignment->course]['assign'])) {
a7366bc8 471 $htmlarray[$assignment->course]['assign'] = $basestr;
bbd0e548 472 } else {
a7366bc8 473 $htmlarray[$assignment->course]['assign'] .= $basestr;
bbd0e548
DW
474 }
475 }
a7366bc8
AA
476 return true;
477}
478
479/**
480 * This api generates html to be displayed to students in print overview section, related to their submission status of the given
481 * assignment.
482 *
483 * @param array $mysubmissions list of submissions of current user indexed by assignment id.
484 * @param string $sqlassignmentids sql clause used to filter open assignments.
485 * @param array $assignmentidparams sql params used to filter open assignments.
486 * @param stdClass $assignment current assignment
487 *
488 * @return bool|string html to display , false if nothing needs to be displayed.
489 * @throws coding_exception
490 */
491function assign_get_mysubmission_details_for_print_overview(&$mysubmissions, $sqlassignmentids, $assignmentidparams,
492 $assignment) {
493 global $USER, $DB;
494
495 if ($assignment->nosubmissions) {
496 // Offline assignment. No need to display alerts for offline assignments.
497 return false;
498 }
499
500 $strnotsubmittedyet = get_string('notsubmittedyet', 'assign');
501
502 if (!isset($mysubmissions)) {
503
504 // Get all user submissions, indexed by assignment id.
505 $dbparams = array_merge(array($USER->id), $assignmentidparams, array($USER->id));
506 $mysubmissions = $DB->get_records_sql('SELECT a.id AS assignment,
507 a.nosubmissions AS nosubmissions,
508 g.timemodified AS timemarked,
509 g.grader AS grader,
510 g.grade AS grade,
511 s.status AS status
512 FROM {assign} a, {assign_submission} s
513 LEFT JOIN {assign_grades} g ON
514 g.assignment = s.assignment AND
515 g.userid = ? AND
516 g.attemptnumber = s.attemptnumber
517 WHERE a.id ' . $sqlassignmentids . ' AND
518 s.latest = 1 AND
519 s.assignment = a.id AND
520 s.userid = ?', $dbparams);
608132d7 521 }
a7366bc8
AA
522
523 $submitdetails = '';
524 $submitdetails .= '<div class="details">';
525 $submitdetails .= get_string('mysubmission', 'assign');
526 $submission = false;
527
528 if (isset($mysubmissions[$assignment->id])) {
529 $submission = $mysubmissions[$assignment->id];
530 }
531
532 if ($submission && $submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
533 // A valid submission already exists, no need to notify students about this.
534 return false;
535 }
536
537 // We need to show details only if a valid submission doesn't exist.
538 if (!$submission ||
539 !$submission->status ||
540 $submission->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
541 $submission->status == ASSIGN_SUBMISSION_STATUS_NEW
542 ) {
543 $submitdetails .= $strnotsubmittedyet;
544 } else {
545 $submitdetails .= get_string('submissionstatus_' . $submission->status, 'assign');
546 }
547 if ($assignment->markingworkflow) {
548 $workflowstate = $DB->get_field('assign_user_flags', 'workflowstate', array('assignment' =>
549 $assignment->id, 'userid' => $USER->id));
550 if ($workflowstate) {
551 $gradingstatus = 'markingworkflowstate' . $workflowstate;
552 } else {
553 $gradingstatus = 'markingworkflowstate' . ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
554 }
555 } else if (!empty($submission->grade) && $submission->grade !== null && $submission->grade >= 0) {
556 $gradingstatus = ASSIGN_GRADING_STATUS_GRADED;
557 } else {
558 $gradingstatus = ASSIGN_GRADING_STATUS_NOT_GRADED;
559 }
560 $submitdetails .= ', ' . get_string($gradingstatus, 'assign');
561 $submitdetails .= '</div>';
562 return $submitdetails;
563}
564
565/**
566 * This api generates html to be displayed to teachers in print overview section, related to the grading status of the given
567 * assignment's submissions.
568 *
569 * @param array $unmarkedsubmissions list of submissions of that are currently unmarked indexed by assignment id.
570 * @param string $sqlassignmentids sql clause used to filter open assignments.
571 * @param array $assignmentidparams sql params used to filter open assignments.
572 * @param stdClass $assignment current assignment
573 * @param context $context context of the assignment.
574 *
575 * @return bool|string html to display , false if nothing needs to be displayed.
576 * @throws coding_exception
577 */
578function assign_get_grade_details_for_print_overview(&$unmarkedsubmissions, $sqlassignmentids, $assignmentidparams,
579 $assignment, $context) {
580 global $DB;
581 if (!isset($unmarkedsubmissions)) {
582 // Build up and array of unmarked submissions indexed by assignment id/ userid
583 // for use where the user has grading rights on assignment.
584 $dbparams = array_merge(array(ASSIGN_SUBMISSION_STATUS_SUBMITTED), $assignmentidparams);
585 $rs = $DB->get_recordset_sql('SELECT s.assignment as assignment,
586 s.userid as userid,
587 s.id as id,
588 s.status as status,
589 g.timemodified as timegraded
590 FROM {assign_submission} s
591 LEFT JOIN {assign_grades} g ON
592 s.userid = g.userid AND
593 s.assignment = g.assignment AND
594 g.attemptnumber = s.attemptnumber
595 WHERE
596 ( g.timemodified is NULL OR
597 s.timemodified > g.timemodified OR
598 g.grade IS NULL ) AND
599 s.timemodified IS NOT NULL AND
600 s.status = ? AND
601 s.latest = 1 AND
602 s.assignment ' . $sqlassignmentids, $dbparams);
603
604 $unmarkedsubmissions = array();
605 foreach ($rs as $rd) {
606 $unmarkedsubmissions[$rd->assignment][$rd->userid] = $rd->id;
607 }
608 $rs->close();
609 }
610
611 // Count how many people can submit.
612 $submissions = 0;
613 if ($students = get_enrolled_users($context, 'mod/assign:view', 0, 'u.id')) {
614 foreach ($students as $student) {
615 if (isset($unmarkedsubmissions[$assignment->id][$student->id])) {
616 $submissions++;
617 }
618 }
619 }
620
621 if ($submissions) {
622 $urlparams = array('id' => $assignment->coursemodule, 'action' => 'grading');
623 $url = new moodle_url('/mod/assign/view.php', $urlparams);
624 $gradedetails = '<div class="details">' .
625 '<a href="' . $url . '">' .
626 get_string('submissionsnotgraded', 'assign', $submissions) .
627 '</a></div>';
628 return $gradedetails;
629 } else {
630 return false;
631 }
632
bbd0e548
DW
633}
634
635/**
636 * Print recent activity from all assignments in a given course
637 *
638 * This is used by the recent activity block
639 * @param mixed $course the course to print activity for
640 * @param bool $viewfullnames boolean to determine whether to show full names or not
9682626e 641 * @param int $timestart the time the rendering started
5c7ec9f6 642 * @return bool true if activity was printed, false otherwise.
bbd0e548
DW
643 */
644function assign_print_recent_activity($course, $viewfullnames, $timestart) {
645 global $CFG, $USER, $DB, $OUTPUT;
9e035dae 646 require_once($CFG->dirroot . '/mod/assign/locallib.php');
bbd0e548 647
e5403f8c 648 // Do not use log table if possible, it may be huge.
bbd0e548 649
9e035dae 650 $dbparams = array($timestart, $course->id, 'assign', ASSIGN_SUBMISSION_STATUS_SUBMITTED);
5c7ec9f6 651 $namefields = user_picture::fields('u', null, 'userid');
5e171c69 652 if (!$submissions = $DB->get_records_sql("SELECT asb.id, asb.timemodified, cm.id AS cmid, um.id as recordid,
5c7ec9f6 653 $namefields
bbd0e548
DW
654 FROM {assign_submission} asb
655 JOIN {assign} a ON a.id = asb.assignment
656 JOIN {course_modules} cm ON cm.instance = a.id
657 JOIN {modules} md ON md.id = cm.module
658 JOIN {user} u ON u.id = asb.userid
5e171c69 659 LEFT JOIN {assign_user_mapping} um ON um.userid = u.id AND um.assignment = a.id
bbd0e548 660 WHERE asb.timemodified > ? AND
ad030ab4 661 asb.latest = 1 AND
bbd0e548 662 a.course = ? AND
9e035dae
DW
663 md.name = ? AND
664 asb.status = ?
5c7ec9f6 665 ORDER BY asb.timemodified ASC", $dbparams)) {
bbd0e548
DW
666 return false;
667 }
668
e5403f8c 669 $modinfo = get_fast_modinfo($course);
bbd0e548
DW
670 $show = array();
671 $grader = array();
672
d6f418cb 673 $showrecentsubmissions = get_config('assign', 'showrecentsubmissions');
cfc81f03 674
e5403f8c 675 foreach ($submissions as $submission) {
bbd0e548
DW
676 if (!array_key_exists($submission->cmid, $modinfo->get_cms())) {
677 continue;
678 }
679 $cm = $modinfo->get_cm($submission->cmid);
680 if (!$cm->uservisible) {
681 continue;
682 }
683 if ($submission->userid == $USER->id) {
684 $show[] = $submission;
685 continue;
686 }
687
688 $context = context_module::instance($submission->cmid);
e5403f8c
DW
689 // The act of submitting of assignment may be considered private -
690 // only graders will see it if specified.
cfc81f03 691 if (empty($showrecentsubmissions)) {
bbd0e548 692 if (!array_key_exists($cm->id, $grader)) {
e5403f8c 693 $grader[$cm->id] = has_capability('moodle/grade:viewall', $context);
bbd0e548
DW
694 }
695 if (!$grader[$cm->id]) {
696 continue;
697 }
698 }
699
700 $groupmode = groups_get_activity_groupmode($cm, $course);
701
e5403f8c
DW
702 if ($groupmode == SEPARATEGROUPS &&
703 !has_capability('moodle/site:accessallgroups', $context)) {
bbd0e548 704 if (isguestuser()) {
e5403f8c 705 // Shortcut - guest user does not belong into any group.
bbd0e548
DW
706 continue;
707 }
708
e5403f8c 709 // This will be slow - show only users that share group with me in this cm.
e19c086c 710 if (!$modinfo->get_groups($cm->groupingid)) {
bbd0e548
DW
711 continue;
712 }
713 $usersgroups = groups_get_all_groups($course->id, $submission->userid, $cm->groupingid);
714 if (is_array($usersgroups)) {
715 $usersgroups = array_keys($usersgroups);
e19c086c 716 $intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid));
bbd0e548
DW
717 if (empty($intersect)) {
718 continue;
719 }
720 }
721 }
722 $show[] = $submission;
723 }
724
725 if (empty($show)) {
726 return false;
727 }
728
729 echo $OUTPUT->heading(get_string('newsubmissions', 'assign').':', 3);
730
731 foreach ($show as $submission) {
732 $cm = $modinfo->get_cm($submission->cmid);
d89dc6da
TL
733 $context = context_module::instance($submission->cmid);
734 $assign = new assign($context, $cm, $cm->course);
bbd0e548 735 $link = $CFG->wwwroot.'/mod/assign/view.php?id='.$cm->id;
d89dc6da
TL
736 // Obscure first and last name if blind marking enabled.
737 if ($assign->is_blind_marking()) {
738 $submission->firstname = get_string('participant', 'mod_assign');
5e171c69
AN
739 if (empty($submission->recordid)) {
740 $submission->recordid = $assign->get_uniqueid_for_user($submission->userid);
741 }
742 $submission->lastname = $submission->recordid;
d89dc6da 743 }
e5403f8c
DW
744 print_recent_activity_note($submission->timemodified,
745 $submission,
746 $cm->name,
747 $link,
748 false,
749 $viewfullnames);
bbd0e548
DW
750 }
751
752 return true;
753}
754
755/**
e5403f8c 756 * Returns all assignments since a given time.
c06b2127 757 *
bbd0e548
DW
758 * @param array $activities The activity information is returned in this array
759 * @param int $index The current index in the activities array
760 * @param int $timestart The earliest activity to show
761 * @param int $courseid Limit the search to this course
762 * @param int $cmid The course module id
763 * @param int $userid Optional user id
764 * @param int $groupid Optional group id
765 * @return void
766 */
e5403f8c
DW
767function assign_get_recent_mod_activity(&$activities,
768 &$index,
769 $timestart,
770 $courseid,
771 $cmid,
772 $userid=0,
773 $groupid=0) {
bbd0e548
DW
774 global $CFG, $COURSE, $USER, $DB;
775
9e035dae
DW
776 require_once($CFG->dirroot . '/mod/assign/locallib.php');
777
bbd0e548
DW
778 if ($COURSE->id == $courseid) {
779 $course = $COURSE;
780 } else {
781 $course = $DB->get_record('course', array('id'=>$courseid));
782 }
783
e5403f8c 784 $modinfo = get_fast_modinfo($course);
bbd0e548
DW
785
786 $cm = $modinfo->get_cm($cmid);
787 $params = array();
788 if ($userid) {
e5403f8c 789 $userselect = 'AND u.id = :userid';
bbd0e548
DW
790 $params['userid'] = $userid;
791 } else {
e5403f8c 792 $userselect = '';
bbd0e548
DW
793 }
794
795 if ($groupid) {
e5403f8c
DW
796 $groupselect = 'AND gm.groupid = :groupid';
797 $groupjoin = 'JOIN {groups_members} gm ON gm.userid=u.id';
bbd0e548
DW
798 $params['groupid'] = $groupid;
799 } else {
e5403f8c
DW
800 $groupselect = '';
801 $groupjoin = '';
bbd0e548
DW
802 }
803
804 $params['cminstance'] = $cm->instance;
805 $params['timestart'] = $timestart;
9e035dae 806 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
bbd0e548
DW
807
808 $userfields = user_picture::fields('u', null, 'userid');
809
f93fdcef
DW
810 if (!$submissions = $DB->get_records_sql('SELECT asb.id, asb.timemodified, ' .
811 $userfields .
812 ' FROM {assign_submission} asb
813 JOIN {assign} a ON a.id = asb.assignment
814 JOIN {user} u ON u.id = asb.userid ' .
815 $groupjoin .
816 ' WHERE asb.timemodified > :timestart AND
9e035dae 817 asb.status = :submitted AND
e5403f8c
DW
818 a.id = :cminstance
819 ' . $userselect . ' ' . $groupselect .
820 ' ORDER BY asb.timemodified ASC', $params)) {
bbd0e548
DW
821 return;
822 }
823
824 $groupmode = groups_get_activity_groupmode($cm, $course);
539af602
DW
825 $cmcontext = context_module::instance($cm->id);
826 $grader = has_capability('moodle/grade:viewall', $cmcontext);
827 $accessallgroups = has_capability('moodle/site:accessallgroups', $cmcontext);
828 $viewfullnames = has_capability('moodle/site:viewfullnames', $cmcontext);
bbd0e548 829
bbd0e548 830
d6f418cb 831 $showrecentsubmissions = get_config('assign', 'showrecentsubmissions');
bbd0e548 832 $show = array();
e5403f8c 833 foreach ($submissions as $submission) {
bbd0e548
DW
834 if ($submission->userid == $USER->id) {
835 $show[] = $submission;
836 continue;
837 }
e5403f8c
DW
838 // The act of submitting of assignment may be considered private -
839 // only graders will see it if specified.
cfc81f03 840 if (empty($showrecentsubmissions)) {
bbd0e548
DW
841 if (!$grader) {
842 continue;
843 }
844 }
845
846 if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
847 if (isguestuser()) {
e5403f8c 848 // Shortcut - guest user does not belong into any group.
bbd0e548
DW
849 continue;
850 }
851
e5403f8c 852 // This will be slow - show only users that share group with me in this cm.
e19c086c 853 if (!$modinfo->get_groups($cm->groupingid)) {
bbd0e548
DW
854 continue;
855 }
e19c086c 856 $usersgroups = groups_get_all_groups($course->id, $submission->userid, $cm->groupingid);
bbd0e548 857 if (is_array($usersgroups)) {
e19c086c
MG
858 $usersgroups = array_keys($usersgroups);
859 $intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid));
bbd0e548
DW
860 if (empty($intersect)) {
861 continue;
862 }
863 }
864 }
865 $show[] = $submission;
866 }
867
868 if (empty($show)) {
869 return;
870 }
871
872 if ($grader) {
873 require_once($CFG->libdir.'/gradelib.php');
874 $userids = array();
e5403f8c 875 foreach ($show as $id => $submission) {
bbd0e548 876 $userids[] = $submission->userid;
bbd0e548
DW
877 }
878 $grades = grade_get_grades($courseid, 'mod', 'assign', $cm->instance, $userids);
879 }
880
e5403f8c 881 $aname = format_string($cm->name, true);
bbd0e548
DW
882 foreach ($show as $submission) {
883 $activity = new stdClass();
884
885 $activity->type = 'assign';
886 $activity->cmid = $cm->id;
887 $activity->name = $aname;
888 $activity->sectionnum = $cm->sectionnum;
889 $activity->timestamp = $submission->timemodified;
890 $activity->user = new stdClass();
891 if ($grader) {
892 $activity->grade = $grades->items[0]->grades[$submission->userid]->str_long_grade;
893 }
894
895 $userfields = explode(',', user_picture::fields());
896 foreach ($userfields as $userfield) {
897 if ($userfield == 'id') {
e5403f8c
DW
898 // Aliased in SQL above.
899 $activity->user->{$userfield} = $submission->userid;
bbd0e548
DW
900 } else {
901 $activity->user->{$userfield} = $submission->{$userfield};
902 }
903 }
904 $activity->user->fullname = fullname($submission, $viewfullnames);
905
906 $activities[$index++] = $activity;
907 }
908
909 return;
910}
911
912/**
913 * Print recent activity from all assignments in a given course
914 *
915 * This is used by course/recent.php
916 * @param stdClass $activity
917 * @param int $courseid
918 * @param bool $detail
919 * @param array $modnames
920 */
e5403f8c 921function assign_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
bbd0e548
DW
922 global $CFG, $OUTPUT;
923
924 echo '<table border="0" cellpadding="3" cellspacing="0" class="assignment-recent">';
925
e5403f8c 926 echo '<tr><td class="userpicture" valign="top">';
bbd0e548 927 echo $OUTPUT->user_picture($activity->user);
e5403f8c 928 echo '</td><td>';
bbd0e548
DW
929
930 if ($detail) {
931 $modname = $modnames[$activity->type];
932 echo '<div class="title">';
e5403f8c
DW
933 echo '<img src="' . $OUTPUT->pix_url('icon', 'assign') . '" '.
934 'class="icon" alt="' . $modname . '">';
935 echo '<a href="' . $CFG->wwwroot . '/mod/assign/view.php?id=' . $activity->cmid . '">';
936 echo $activity->name;
937 echo '</a>';
bbd0e548
DW
938 echo '</div>';
939 }
940
941 if (isset($activity->grade)) {
942 echo '<div class="grade">';
943 echo get_string('grade').': ';
944 echo $activity->grade;
945 echo '</div>';
946 }
947
948 echo '<div class="user">';
e5403f8c
DW
949 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&amp;course=$courseid\">";
950 echo "{$activity->user->fullname}</a> - " . userdate($activity->timestamp);
bbd0e548
DW
951 echo '</div>';
952
e5403f8c 953 echo '</td></tr></table>';
bbd0e548
DW
954}
955
956/**
e5403f8c 957 * Checks if a scale is being used by an assignment.
bbd0e548
DW
958 *
959 * This is used by the backup code to decide whether to back up a scale
9682626e 960 * @param int $assignmentid
bbd0e548
DW
961 * @param int $scaleid
962 * @return boolean True if the scale is used by the assignment
963 */
964function assign_scale_used($assignmentid, $scaleid) {
965 global $DB;
966
967 $return = false;
e5403f8c 968 $rec = $DB->get_record('assign', array('id'=>$assignmentid, 'grade'=>-$scaleid));
bbd0e548
DW
969
970 if (!empty($rec) && !empty($scaleid)) {
971 $return = true;
972 }
973
974 return $return;
975}
976
977/**
978 * Checks if scale is being used by any instance of assignment
979 *
980 * This is used to find out if scale used anywhere
9682626e 981 * @param int $scaleid
bbd0e548
DW
982 * @return boolean True if the scale is used by any assignment
983 */
984function assign_scale_used_anywhere($scaleid) {
985 global $DB;
986
987 if ($scaleid and $DB->record_exists('assign', array('grade'=>-$scaleid))) {
988 return true;
989 } else {
990 return false;
991 }
992}
993
994/**
e5403f8c
DW
995 * List the actions that correspond to a view of this module.
996 * This is used by the participation report.
b2b4ec30
RT
997 *
998 * Note: This is not used by new logging system. Event with
999 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
1000 * be considered as view action.
1001 *
bbd0e548
DW
1002 * @return array
1003 */
1004function assign_get_view_actions() {
1005 return array('view submission', 'view feedback');
1006}
1007
1008/**
e5403f8c
DW
1009 * List the actions that correspond to a post of this module.
1010 * This is used by the participation report.
b2b4ec30
RT
1011 *
1012 * Note: This is not used by new logging system. Event with
1013 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
1014 * will be considered as post action.
1015 *
bbd0e548
DW
1016 * @return array
1017 */
1018function assign_get_post_actions() {
1019 return array('upload', 'submit', 'submit for grading');
1020}
1021
1022/**
e5403f8c 1023 * Call cron on the assign module.
bbd0e548
DW
1024 */
1025function assign_cron() {
1026 global $CFG;
1027
1028 require_once($CFG->dirroot . '/mod/assign/locallib.php');
75f87a57
DW
1029 assign::cron();
1030
bd3b3bba 1031 $plugins = core_component::get_plugin_list('assignsubmission');
bbd0e548
DW
1032
1033 foreach ($plugins as $name => $plugin) {
1034 $disabled = get_config('assignsubmission_' . $name, 'disabled');
1035 if (!$disabled) {
1036 $class = 'assign_submission_' . $name;
1037 require_once($CFG->dirroot . '/mod/assign/submission/' . $name . '/locallib.php');
1038 $class::cron();
1039 }
1040 }
bd3b3bba 1041 $plugins = core_component::get_plugin_list('assignfeedback');
bbd0e548
DW
1042
1043 foreach ($plugins as $name => $plugin) {
1044 $disabled = get_config('assignfeedback_' . $name, 'disabled');
1045 if (!$disabled) {
1046 $class = 'assign_feedback_' . $name;
1047 require_once($CFG->dirroot . '/mod/assign/feedback/' . $name . '/locallib.php');
1048 $class::cron();
1049 }
1050 }
abd0f0ae
JM
1051
1052 return true;
bbd0e548
DW
1053}
1054
1055/**
1056 * Returns all other capabilities used by this module.
1057 * @return array Array of capability strings
1058 */
1059function assign_get_extra_capabilities() {
e5403f8c
DW
1060 return array('gradereport/grader:view',
1061 'moodle/grade:viewall',
1062 'moodle/site:viewfullnames',
1063 'moodle/site:config');
bbd0e548
DW
1064}
1065
1066/**
e5403f8c 1067 * Create grade item for given assignment.
bbd0e548
DW
1068 *
1069 * @param stdClass $assign record with extra cmidnumber
1070 * @param array $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
1071 * @return int 0 if ok, error code otherwise
1072 */
e5403f8c 1073function assign_grade_item_update($assign, $grades=null) {
bbd0e548
DW
1074 global $CFG;
1075 require_once($CFG->libdir.'/gradelib.php');
1076
1077 if (!isset($assign->courseid)) {
1078 $assign->courseid = $assign->course;
1079 }
1080
1081 $params = array('itemname'=>$assign->name, 'idnumber'=>$assign->cmidnumber);
1082
456d7bc7
RT
1083 // Check if feedback plugin for gradebook is enabled, if yes then
1084 // gradetype = GRADE_TYPE_TEXT else GRADE_TYPE_NONE.
1085 $gradefeedbackenabled = false;
1086
1087 if (isset($assign->gradefeedbackenabled)) {
1088 $gradefeedbackenabled = $assign->gradefeedbackenabled;
1089 } else if ($assign->grade == 0) { // Grade feedback is needed only when grade == 0.
2507b7e0 1090 require_once($CFG->dirroot . '/mod/assign/locallib.php');
456d7bc7
RT
1091 $mod = get_coursemodule_from_instance('assign', $assign->id, $assign->courseid);
1092 $cm = context_module::instance($mod->id);
1093 $assignment = new assign($cm, null, null);
1094 $gradefeedbackenabled = $assignment->is_gradebook_feedback_enabled();
1095 }
1096
bbd0e548
DW
1097 if ($assign->grade > 0) {
1098 $params['gradetype'] = GRADE_TYPE_VALUE;
1099 $params['grademax'] = $assign->grade;
1100 $params['grademin'] = 0;
1101
1102 } else if ($assign->grade < 0) {
1103 $params['gradetype'] = GRADE_TYPE_SCALE;
1104 $params['scaleid'] = -$assign->grade;
1105
456d7bc7
RT
1106 } else if ($gradefeedbackenabled) {
1107 // $assign->grade == 0 and feedback enabled.
e5403f8c 1108 $params['gradetype'] = GRADE_TYPE_TEXT;
456d7bc7
RT
1109 } else {
1110 // $assign->grade == 0 and no feedback enabled.
1111 $params['gradetype'] = GRADE_TYPE_NONE;
bbd0e548
DW
1112 }
1113
1114 if ($grades === 'reset') {
1115 $params['reset'] = true;
e5403f8c 1116 $grades = null;
bbd0e548
DW
1117 }
1118
e5403f8c
DW
1119 return grade_update('mod/assign',
1120 $assign->courseid,
1121 'mod',
1122 'assign',
1123 $assign->id,
1124 0,
1125 $grades,
1126 $params);
bbd0e548
DW
1127}
1128
1129/**
1130 * Return grade for given user or all users.
1131 *
1132 * @param stdClass $assign record of assign with an additional cmidnumber
1133 * @param int $userid optional user id, 0 means all users
1134 * @return array array of grades, false if none
1135 */
1136function assign_get_user_grades($assign, $userid=0) {
1137 global $CFG;
e5403f8c 1138
bbd0e548
DW
1139 require_once($CFG->dirroot . '/mod/assign/locallib.php');
1140
79e7dfcc
RW
1141 $cm = get_coursemodule_from_instance('assign', $assign->id, 0, false, MUST_EXIST);
1142 $context = context_module::instance($cm->id);
1143 $assignment = new assign($context, null, null);
b11808c7 1144 $assignment->set_instance($assign);
bbd0e548
DW
1145 return $assignment->get_user_grades_for_gradebook($userid);
1146}
1147
1148/**
e5403f8c 1149 * Update activity grades.
bbd0e548
DW
1150 *
1151 * @param stdClass $assign database record
1152 * @param int $userid specific user only, 0 means all
1153 * @param bool $nullifnone - not used
1154 */
1155function assign_update_grades($assign, $userid=0, $nullifnone=true) {
1156 global $CFG;
1157 require_once($CFG->libdir.'/gradelib.php');
1158
1159 if ($assign->grade == 0) {
1160 assign_grade_item_update($assign);
1161
1162 } else if ($grades = assign_get_user_grades($assign, $userid)) {
e5403f8c 1163 foreach ($grades as $k => $v) {
bbd0e548
DW
1164 if ($v->rawgrade == -1) {
1165 $grades[$k]->rawgrade = null;
1166 }
1167 }
1168 assign_grade_item_update($assign, $grades);
1169
1170 } else {
1171 assign_grade_item_update($assign);
1172 }
1173}
1174
1175/**
e5403f8c 1176 * List the file areas that can be browsed.
bbd0e548
DW
1177 *
1178 * @param stdClass $course
1179 * @param stdClass $cm
1180 * @param stdClass $context
1181 * @return array
1182 */
6b8b0b2e 1183function assign_get_file_areas($course, $cm, $context) {
bbd0e548
DW
1184 global $CFG;
1185 require_once($CFG->dirroot . '/mod/assign/locallib.php');
7faf78cb
HB
1186
1187 $areas = array(ASSIGN_INTROATTACHMENT_FILEAREA => get_string('introattachments', 'mod_assign'));
bbd0e548
DW
1188
1189 $assignment = new assign($context, $cm, $course);
1190 foreach ($assignment->get_submission_plugins() as $plugin) {
1191 if ($plugin->is_visible()) {
1192 $pluginareas = $plugin->get_file_areas();
1193
1194 if ($pluginareas) {
1195 $areas = array_merge($areas, $pluginareas);
1196 }
1197 }
1198 }
1199 foreach ($assignment->get_feedback_plugins() as $plugin) {
1200 if ($plugin->is_visible()) {
1201 $pluginareas = $plugin->get_file_areas();
1202
1203 if ($pluginareas) {
1204 $areas = array_merge($areas, $pluginareas);
1205 }
1206 }
1207 }
1208
1209 return $areas;
1210}
1211
1212/**
1213 * File browsing support for assign module.
1214 *
1215 * @param file_browser $browser
1216 * @param object $areas
1217 * @param object $course
1218 * @param object $cm
1219 * @param object $context
1220 * @param string $filearea
1221 * @param int $itemid
1222 * @param string $filepath
1223 * @param string $filename
1224 * @return object file_info instance or null if not found
1225 */
e5403f8c
DW
1226function assign_get_file_info($browser,
1227 $areas,
1228 $course,
1229 $cm,
1230 $context,
1231 $filearea,
1232 $itemid,
1233 $filepath,
1234 $filename) {
bbd0e548
DW
1235 global $CFG;
1236 require_once($CFG->dirroot . '/mod/assign/locallib.php');
1237
1238 if ($context->contextlevel != CONTEXT_MODULE) {
1239 return null;
1240 }
1241
7faf78cb 1242 $urlbase = $CFG->wwwroot.'/pluginfile.php';
bbd0e548
DW
1243 $fs = get_file_storage();
1244 $filepath = is_null($filepath) ? '/' : $filepath;
1245 $filename = is_null($filename) ? '.' : $filename;
1246
7faf78cb 1247 // Need to find where this belongs to.
bbd0e548 1248 $assignment = new assign($context, $cm, $course);
7faf78cb
HB
1249 if ($filearea === ASSIGN_INTROATTACHMENT_FILEAREA) {
1250 if (!has_capability('moodle/course:managefiles', $context)) {
1251 // Students can not peak here!
1252 return null;
1253 }
1254 if (!($storedfile = $fs->get_file($assignment->get_context()->id,
1255 'mod_assign', $filearea, 0, $filepath, $filename))) {
1256 return null;
1257 }
1258 return new file_info_stored($browser,
1259 $assignment->get_context(),
1260 $storedfile,
1261 $urlbase,
1262 $filearea,
1263 $itemid,
1264 true,
1265 true,
1266 false);
1267 }
1268
bbd0e548
DW
1269 $pluginowner = null;
1270 foreach ($assignment->get_submission_plugins() as $plugin) {
1271 if ($plugin->is_visible()) {
1272 $pluginareas = $plugin->get_file_areas();
1273
1274 if (array_key_exists($filearea, $pluginareas)) {
1275 $pluginowner = $plugin;
1276 break;
1277 }
1278 }
1279 }
1280 if (!$pluginowner) {
1281 foreach ($assignment->get_feedback_plugins() as $plugin) {
1282 if ($plugin->is_visible()) {
1283 $pluginareas = $plugin->get_file_areas();
1284
1285 if (array_key_exists($filearea, $pluginareas)) {
1286 $pluginowner = $plugin;
1287 break;
1288 }
1289 }
1290 }
1291 }
1292
1293 if (!$pluginowner) {
1294 return null;
1295 }
1296
1297 $result = $pluginowner->get_file_info($browser, $filearea, $itemid, $filepath, $filename);
1298 return $result;
1299}
1300
1301/**
e5403f8c 1302 * Prints the complete info about a user's interaction with an assignment.
bbd0e548
DW
1303 *
1304 * @param stdClass $course
1305 * @param stdClass $user
1306 * @param stdClass $coursemodule
1307 * @param stdClass $assign the database assign record
1308 *
e5403f8c 1309 * This prints the submission summary and feedback summary for this student.
bbd0e548
DW
1310 */
1311function assign_user_complete($course, $user, $coursemodule, $assign) {
1312 global $CFG;
1313 require_once($CFG->dirroot . '/mod/assign/locallib.php');
e5403f8c 1314
bbd0e548
DW
1315 $context = context_module::instance($coursemodule->id);
1316
1317 $assignment = new assign($context, $coursemodule, $course);
1318
1319 echo $assignment->view_student_summary($user, false);
1320}
1321
d629c601
DW
1322/**
1323 * Rescale all grades for this activity and push the new grades to the gradebook.
1324 *
1325 * @param stdClass $course Course db record
1326 * @param stdClass $cm Course module db record
1327 * @param float $oldmin
1328 * @param float $oldmax
1329 * @param float $newmin
1330 * @param float $newmax
1331 */
1332function assign_rescale_activity_grades($course, $cm, $oldmin, $oldmax, $newmin, $newmax) {
1333 global $DB;
1334
1335 if ($oldmax <= $oldmin) {
1336 // Grades cannot be scaled.
1337 return false;
1338 }
1339 $scale = ($newmax - $newmin) / ($oldmax - $oldmin);
1340 if (($newmax - $newmin) <= 1) {
1341 // We would lose too much precision, lets bail.
1342 return false;
1343 }
1344
1345 $params = array(
1346 'p1' => $oldmin,
1347 'p2' => $scale,
1348 'p3' => $newmin,
1349 'a' => $cm->instance
1350 );
1351
1352 $sql = 'UPDATE {assign_grades} set grade = (((grade - :p1) * :p2) + :p3) where assignment = :a';
1353 $dbupdate = $DB->execute($sql, $params);
1354 if (!$dbupdate) {
1355 return false;
1356 }
1357
1358 // Now re-push all grades to the gradebook.
1359 $dbparams = array('id' => $cm->instance);
1360 $assign = $DB->get_record('assign', $dbparams);
1361 $assign->cmidnumber = $cm->idnumber;
1362
1363 assign_update_grades($assign);
1364
1365 return true;
1366}
1367
bbd0e548 1368/**
e5403f8c 1369 * Print the grade information for the assignment for this user.
9682626e 1370 *
bbd0e548
DW
1371 * @param stdClass $course
1372 * @param stdClass $user
1373 * @param stdClass $coursemodule
1374 * @param stdClass $assignment
1375 */
1376function assign_user_outline($course, $user, $coursemodule, $assignment) {
1377 global $CFG;
1378 require_once($CFG->libdir.'/gradelib.php');
1379 require_once($CFG->dirroot.'/grade/grading/lib.php');
1380
1381 $gradinginfo = grade_get_grades($course->id,
1382 'mod',
1383 'assign',
1384 $assignment->id,
1385 $user->id);
1386
1387 $gradingitem = $gradinginfo->items[0];
1388 $gradebookgrade = $gradingitem->grades[$user->id];
1389
f6aa750e 1390 if (empty($gradebookgrade->str_long_grade)) {
bbd0e548
DW
1391 return null;
1392 }
1393 $result = new stdClass();
f6aa750e 1394 $result->info = get_string('outlinegrade', 'assign', $gradebookgrade->str_long_grade);
bbd0e548
DW
1395 $result->time = $gradebookgrade->dategraded;
1396
1397 return $result;
1398}
79ed4d84
DW
1399
1400/**
1401 * Obtains the automatic completion state for this module based on any conditions
1402 * in assign settings.
1403 *
1404 * @param object $course Course
1405 * @param object $cm Course-module
1406 * @param int $userid User ID
1407 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
1408 * @return bool True if completed, false if not, $type if conditions not set.
1409 */
3a66d425 1410function assign_get_completion_state($course, $cm, $userid, $type) {
e5403f8c 1411 global $CFG, $DB;
79ed4d84
DW
1412 require_once($CFG->dirroot . '/mod/assign/locallib.php');
1413
1414 $assign = new assign(null, $cm, $course);
1415
3a66d425
DP
1416 // If completion option is enabled, evaluate it and return true/false.
1417 if ($assign->get_instance()->completionsubmit) {
9569b01c
SB
1418 if ($assign->get_instance()->teamsubmission) {
1419 $submission = $assign->get_group_submission($userid, 0, false);
1420 } else {
1421 $submission = $assign->get_user_submission($userid, false);
1422 }
79ed4d84
DW
1423 return $submission && $submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED;
1424 } else {
3a66d425 1425 // Completion option is not enabled so just return $type.
79ed4d84
DW
1426 return $type;
1427 }
1428}
7faf78cb
HB
1429
1430/**
1431 * Serves intro attachment files.
1432 *
1433 * @param mixed $course course or id of the course
1434 * @param mixed $cm course module or id of the course module
1435 * @param context $context
1436 * @param string $filearea
1437 * @param array $args
1438 * @param bool $forcedownload
1439 * @param array $options additional options affecting the file serving
1440 * @return bool false if file not found, does not return if found - just send the file
1441 */
1442function assign_pluginfile($course,
1443 $cm,
1444 context $context,
1445 $filearea,
1446 $args,
1447 $forcedownload,
1448 array $options=array()) {
1449 global $CFG;
1450
1451 if ($context->contextlevel != CONTEXT_MODULE) {
1452 return false;
1453 }
1454
1455 require_login($course, false, $cm);
1456 if (!has_capability('mod/assign:view', $context)) {
1457 return false;
1458 }
1459
1460 require_once($CFG->dirroot . '/mod/assign/locallib.php');
1461 $assign = new assign($context, $cm, $course);
1462
1463 if ($filearea !== ASSIGN_INTROATTACHMENT_FILEAREA) {
1464 return false;
1465 }
1466 if (!$assign->show_intro()) {
1467 return false;
1468 }
1469
1470 $itemid = (int)array_shift($args);
1471 if ($itemid != 0) {
1472 return false;
1473 }
1474
1475 $relativepath = implode('/', $args);
1476
1477 $fullpath = "/{$context->id}/mod_assign/$filearea/$itemid/$relativepath";
1478
1479 $fs = get_file_storage();
1480 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
1481 return false;
1482 }
1483 send_stored_file($file, 0, 0, $forcedownload, $options);
1484}
bb690849
DW
1485
1486/**
1487 * Serve the grading panel as a fragment.
1488 *
1489 * @param array $args List of named arguments for the fragment loader.
1490 * @return string
1491 */
1492function mod_assign_output_fragment_gradingpanel($args) {
1493 global $CFG;
1494
1495 $context = $args['context'];
1496
1497 if ($context->contextlevel != CONTEXT_MODULE) {
1498 return null;
1499 }
1500 require_once($CFG->dirroot . '/mod/assign/locallib.php');
1501 $assign = new assign($context, null, null);
1502
1503 $userid = clean_param($args['userid'], PARAM_INT);
1504 $attemptnumber = clean_param($args['attemptnumber'], PARAM_INT);
1505 $formdata = array();
1506 if (!empty($args['jsonformdata'])) {
1507 $serialiseddata = json_decode($args['jsonformdata']);
1508 parse_str($serialiseddata, $formdata);
1509 }
1510 $viewargs = array(
1511 'userid' => $userid,
1512 'attemptnumber' => $attemptnumber,
1513 'formdata' => $formdata
1514 );
1515
1516 return $assign->view('gradingpanel', $viewargs);
1517}