MDL-67264 core_course: Begin set up for Activity chooser
[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 *
37ee40f3
AB
22 * If by student params: course=2
23 * If by manager params: course=2&user=4&rolec=3&sesskey=ghfgsdf
24 *
d9cb06dc 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * @package course
27 */
4e781c7b 28
29require_once('../config.php');
30require_once($CFG->libdir.'/completionlib.php');
31
32// Parameters
2be4d090
MD
33$cmid = optional_param('id', 0, PARAM_INT);
34$courseid = optional_param('course', 0, PARAM_INT);
35$confirm = optional_param('confirm', 0, PARAM_BOOL);
36
95d1a366 37// Check if we are marking a user complete via the completion report.
37ee40f3
AB
38$user = optional_param('user', 0, PARAM_INT);
39$rolec = optional_param('rolec', 0, PARAM_INT);
40
2be4d090
MD
41if (!$cmid && !$courseid) {
42 print_error('invalidarguments');
43}
44
45// Process self completion
46if ($courseid) {
49a33d8f 47 $PAGE->set_url(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid)));
9cedb80c 48
2be4d090 49 // Check user is logged in
74df2951 50 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
9a5e297b 51 $context = context_course::instance($course->id);
2be4d090
MD
52 require_login($course);
53
54 $completion = new completion_info($course);
37ee40f3
AB
55 $trackeduser = ($user ? $user : $USER->id);
56
10a8581f
AB
57 if (!$completion->is_enabled()) {
58 throw new moodle_exception('completionnotenabled', 'completion');
95d1a366 59 } else if (!$completion->is_tracked_user($trackeduser)) {
10a8581f
AB
60 throw new moodle_exception('nottracked', 'completion');
61 }
2be4d090 62
2be4d090 63 if ($user && $rolec) {
9cedb80c
PS
64 require_sesskey();
65
9df81506 66 completion_criteria::factory(array('id'=>$rolec, 'criteriatype'=>COMPLETION_CRITERIA_TYPE_ROLE)); //TODO: this is dumb, because it does not fetch the data?!?!
9cedb80c 67 $criteria = completion_criteria_role::fetch(array('id'=>$rolec));
2be4d090 68
9cedb80c
PS
69 if ($criteria and user_has_role_assignment($USER->id, $criteria->role, $context->id)) {
70 $criteria_completions = $completion->get_completions($user, COMPLETION_CRITERIA_TYPE_ROLE);
2be4d090 71
9cedb80c
PS
72 foreach ($criteria_completions as $criteria_completion) {
73 if ($criteria_completion->criteriaid == $rolec) {
74 $criteria->complete($criteria_completion);
75 break;
76 }
2be4d090
MD
77 }
78 }
79
80 // Return to previous page
dcee0b94 81 $referer = get_local_referer(false);
b2687a05
FM
82 if (!empty($referer)) {
83 redirect($referer);
2be4d090
MD
84 } else {
85 redirect('view.php?id='.$course->id);
86 }
87
88 } else {
89
90 // Confirm with user
9cedb80c 91 if ($confirm and confirm_sesskey()) {
2be4d090
MD
92 $completion = $completion->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
93
94 if (!$completion) {
95 print_error('noselfcompletioncriteria', 'completion');
96 }
97
98 // Check if the user has already marked themselves as complete
99 if ($completion->is_complete()) {
100 print_error('useralreadymarkedcomplete', 'completion');
101 }
102
103 $completion->mark_complete();
104
105 redirect($CFG->wwwroot.'/course/view.php?id='.$courseid);
106 return;
107 }
108
109 $strconfirm = get_string('confirmselfcompletion', 'completion');
49a33d8f
SH
110 $PAGE->set_title($strconfirm);
111 $PAGE->set_heading($course->fullname);
112 $PAGE->navbar->add($strconfirm);
113 echo $OUTPUT->header();
9cedb80c 114 $buttoncontinue = new single_button(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid, 'confirm'=>1, 'sesskey'=>sesskey())), get_string('yes'), 'post');
49a33d8f
SH
115 $buttoncancel = new single_button(new moodle_url('/course/view.php', array('id'=>$courseid)), get_string('no'), 'get');
116 echo $OUTPUT->confirm($strconfirm, $buttoncontinue, $buttoncancel);
117 echo $OUTPUT->footer();
2be4d090
MD
118 exit;
119 }
120}
121
122
8c194133
PS
123$targetstate = required_param('completionstate', PARAM_INT);
124$fromajax = optional_param('fromajax', 0, PARAM_INT);
d9cb06dc 125
8c194133 126$PAGE->set_url('/course/togglecompletion.php', array('id'=>$cmid, 'completionstate'=>$targetstate));
d9cb06dc 127
4e781c7b 128switch($targetstate) {
129 case COMPLETION_COMPLETE:
130 case COMPLETION_INCOMPLETE:
131 break;
132 default:
9b34dc6e 133 print_error('unsupportedstate');
4e781c7b 134}
4e781c7b 135
136// Get course-modules entry
7e29340f 137$cm = get_coursemodule_from_id(null, $cmid, null, true, MUST_EXIST);
74df2951 138$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
4e781c7b 139
8c194133
PS
140// Check user is logged in
141require_login($course, false, $cm);
7868aab1 142require_capability('moodle/course:togglecompletion', context_module::instance($cmid));
8c194133
PS
143
144if (isguestuser() or !confirm_sesskey()) {
145 print_error('error');
4e781c7b 146}
147
7ae7c3cf 148// Set up completion object and check it is enabled.
8c194133
PS
149$completion = new completion_info($course);
150if (!$completion->is_enabled()) {
10a8581f 151 throw new moodle_exception('completionnotenabled', 'completion');
8c194133 152}
4e781c7b 153
7ae7c3cf 154// NOTE: All users are allowed to toggle their completion state, including
155// users for whom completion information is not directly tracked. (I.e. even
156// if you are a teacher, or admin who is not enrolled, you can still toggle
157// your own completion state. You just don't appear on the reports.)
158
4e781c7b 159// Check completion state is manual
8c194133
PS
160if($cm->completion != COMPLETION_TRACKING_MANUAL) {
161 error_or_ajax('cannotmanualctrack', $fromajax);
4e781c7b 162}
163
8c194133 164$completion->update_state($cm, $targetstate);
4e781c7b 165
166// And redirect back to course
8c194133 167if ($fromajax) {
4e781c7b 168 print 'OK';
169} else {
e399d47c 170 // In case of use in other areas of code we allow a 'backto' parameter,
171 // otherwise go back to course page
7e29340f
MG
172
173 if ($backto = optional_param('backto', null, PARAM_URL)) {
174 redirect($backto);
175 } else {
176 redirect(course_get_url($course, $cm->sectionnum));
177 }
4e781c7b 178}
d9cb06dc 179
8c194133
PS
180// utility functions
181
182function error_or_ajax($message, $fromajax) {
183 if ($fromajax) {
184 print get_string($message, 'error');
185 exit;
186 } else {
187 print_error($message);
188 }
189}
190