navigation MDL-23378 Added additional = sign to find method of nav node collection
[moodle.git] / course / togglecompletion.php
CommitLineData
4e781c7b 1<?php
d9cb06dc 2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
2be4d090
MD
19 * Toggles the manual completion flag for a particular activity or course completion
20 * and the current user.
d9cb06dc 21 *
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package course
24 */
4e781c7b 25
26require_once('../config.php');
27require_once($CFG->libdir.'/completionlib.php');
28
29// Parameters
2be4d090
MD
30$cmid = optional_param('id', 0, PARAM_INT);
31$courseid = optional_param('course', 0, PARAM_INT);
32$confirm = optional_param('confirm', 0, PARAM_BOOL);
33
34if (!$cmid && !$courseid) {
35 print_error('invalidarguments');
36}
37
38// Process self completion
39if ($courseid) {
40
41 // Check user is logged in
42 $course = $DB->get_record('course', array('id' => $courseid));
43 require_login($course);
44
45 $completion = new completion_info($course);
46
47 // Check if we are marking a user complete via the completion report
48 $user = optional_param('user', 0, PARAM_INT);
49 $rolec = optional_param('rolec', 0, PARAM_INT);
50
51 if ($user && $rolec) {
52
53 $criteria = completion_criteria::factory((object) array('id'=>$rolec, 'criteriatype'=>COMPLETION_CRITERIA_TYPE_ROLE));
54 $criteria_completions = $completion->get_completions($user, COMPLETION_CRITERIA_TYPE_ROLE);
55
56 foreach ($criteria_completions as $criteria_completion) {
57 if ($criteria_completion->criteriaid == $rolec) {
58 $criteria->complete($criteria_completion);
59 break;
60 }
61 }
62
63 // Return to previous page
64 if (!empty($_SERVER['HTTP_REFERER'])) {
65 redirect($_SERVER['HTTP_REFERER']);
66 } else {
67 redirect('view.php?id='.$course->id);
68 }
69
70 } else {
71
72 // Confirm with user
73 if ($confirm) {
74 $completion = $completion->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
75
76 if (!$completion) {
77 print_error('noselfcompletioncriteria', 'completion');
78 }
79
80 // Check if the user has already marked themselves as complete
81 if ($completion->is_complete()) {
82 print_error('useralreadymarkedcomplete', 'completion');
83 }
84
85 $completion->mark_complete();
86
87 redirect($CFG->wwwroot.'/course/view.php?id='.$courseid);
88 return;
89 }
90
91 $strconfirm = get_string('confirmselfcompletion', 'completion');
92 print_header_simple($strconfirm, '', build_navigation(array(array('name' => $strconfirm, 'link' => '', 'type' => 'misc'))));
93 notice_yesno($strconfirm, $CFG->wwwroot.'/course/togglecompletion.php?course='.$courseid.'&confirm=1', $CFG->wwwroot.'/course/view.php?id='.$courseid);
94 print_simple_box_end();
95 print_footer($course);
96 exit;
97 }
98}
99
100
8c194133
PS
101$targetstate = required_param('completionstate', PARAM_INT);
102$fromajax = optional_param('fromajax', 0, PARAM_INT);
d9cb06dc 103
8c194133 104$PAGE->set_url('/course/togglecompletion.php', array('id'=>$cmid, 'completionstate'=>$targetstate));
d9cb06dc 105
4e781c7b 106switch($targetstate) {
107 case COMPLETION_COMPLETE:
108 case COMPLETION_INCOMPLETE:
109 break;
110 default:
9b34dc6e 111 print_error('unsupportedstate');
4e781c7b 112}
4e781c7b 113
114// Get course-modules entry
8c194133
PS
115$cm = get_coursemodule_from_id(null, $cmid, null, false, MUST_EXIST);
116$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
4e781c7b 117
8c194133
PS
118// Check user is logged in
119require_login($course, false, $cm);
120
121if (isguestuser() or !confirm_sesskey()) {
122 print_error('error');
4e781c7b 123}
124
8c194133
PS
125// Now change state
126$completion = new completion_info($course);
127if (!$completion->is_enabled()) {
128 die;
129}
4e781c7b 130
131// Check completion state is manual
8c194133
PS
132if($cm->completion != COMPLETION_TRACKING_MANUAL) {
133 error_or_ajax('cannotmanualctrack', $fromajax);
4e781c7b 134}
135
8c194133 136$completion->update_state($cm, $targetstate);
4e781c7b 137
138// And redirect back to course
8c194133 139if ($fromajax) {
4e781c7b 140 print 'OK';
141} else {
e399d47c 142 // In case of use in other areas of code we allow a 'backto' parameter,
143 // otherwise go back to course page
8c194133 144 $backto = optional_param('backto', 'view.php?id='.$course->id, PARAM_URL);
e399d47c 145 redirect($backto);
4e781c7b 146}
d9cb06dc 147
8c194133
PS
148// utility functions
149
150function error_or_ajax($message, $fromajax) {
151 if ($fromajax) {
152 print get_string($message, 'error');
153 exit;
154 } else {
155 print_error($message);
156 }
157}
158