MDL-26198 make user_has_role_assignment() check parent contexts too
[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) {
49a33d8f
SH
40 $PAGE->set_url(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid)));
41
2be4d090 42 // Check user is logged in
49a33d8f 43 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
2be4d090
MD
44 require_login($course);
45
46 $completion = new completion_info($course);
47
48 // Check if we are marking a user complete via the completion report
49 $user = optional_param('user', 0, PARAM_INT);
50 $rolec = optional_param('rolec', 0, PARAM_INT);
51
52 if ($user && $rolec) {
53
54 $criteria = completion_criteria::factory((object) array('id'=>$rolec, 'criteriatype'=>COMPLETION_CRITERIA_TYPE_ROLE));
55 $criteria_completions = $completion->get_completions($user, COMPLETION_CRITERIA_TYPE_ROLE);
56
57 foreach ($criteria_completions as $criteria_completion) {
58 if ($criteria_completion->criteriaid == $rolec) {
59 $criteria->complete($criteria_completion);
60 break;
61 }
62 }
63
64 // Return to previous page
65 if (!empty($_SERVER['HTTP_REFERER'])) {
66 redirect($_SERVER['HTTP_REFERER']);
67 } else {
68 redirect('view.php?id='.$course->id);
69 }
70
71 } else {
72
73 // Confirm with user
74 if ($confirm) {
75 $completion = $completion->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
76
77 if (!$completion) {
78 print_error('noselfcompletioncriteria', 'completion');
79 }
80
81 // Check if the user has already marked themselves as complete
82 if ($completion->is_complete()) {
83 print_error('useralreadymarkedcomplete', 'completion');
84 }
85
86 $completion->mark_complete();
87
88 redirect($CFG->wwwroot.'/course/view.php?id='.$courseid);
89 return;
90 }
91
92 $strconfirm = get_string('confirmselfcompletion', 'completion');
49a33d8f
SH
93 $PAGE->set_title($strconfirm);
94 $PAGE->set_heading($course->fullname);
95 $PAGE->navbar->add($strconfirm);
96 echo $OUTPUT->header();
97 $buttoncontinue = new single_button(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid, 'confirm'=>1)), get_string('yes'), 'post');
98 $buttoncancel = new single_button(new moodle_url('/course/view.php', array('id'=>$courseid)), get_string('no'), 'get');
99 echo $OUTPUT->confirm($strconfirm, $buttoncontinue, $buttoncancel);
100 echo $OUTPUT->footer();
2be4d090
MD
101 exit;
102 }
103}
104
105
8c194133
PS
106$targetstate = required_param('completionstate', PARAM_INT);
107$fromajax = optional_param('fromajax', 0, PARAM_INT);
d9cb06dc 108
8c194133 109$PAGE->set_url('/course/togglecompletion.php', array('id'=>$cmid, 'completionstate'=>$targetstate));
d9cb06dc 110
4e781c7b 111switch($targetstate) {
112 case COMPLETION_COMPLETE:
113 case COMPLETION_INCOMPLETE:
114 break;
115 default:
9b34dc6e 116 print_error('unsupportedstate');
4e781c7b 117}
4e781c7b 118
119// Get course-modules entry
8c194133
PS
120$cm = get_coursemodule_from_id(null, $cmid, null, false, MUST_EXIST);
121$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
4e781c7b 122
8c194133
PS
123// Check user is logged in
124require_login($course, false, $cm);
125
126if (isguestuser() or !confirm_sesskey()) {
127 print_error('error');
4e781c7b 128}
129
8c194133
PS
130// Now change state
131$completion = new completion_info($course);
132if (!$completion->is_enabled()) {
133 die;
134}
4e781c7b 135
136// Check completion state is manual
8c194133
PS
137if($cm->completion != COMPLETION_TRACKING_MANUAL) {
138 error_or_ajax('cannotmanualctrack', $fromajax);
4e781c7b 139}
140
8c194133 141$completion->update_state($cm, $targetstate);
4e781c7b 142
143// And redirect back to course
8c194133 144if ($fromajax) {
4e781c7b 145 print 'OK';
146} else {
e399d47c 147 // In case of use in other areas of code we allow a 'backto' parameter,
148 // otherwise go back to course page
8c194133 149 $backto = optional_param('backto', 'view.php?id='.$course->id, PARAM_URL);
e399d47c 150 redirect($backto);
4e781c7b 151}
d9cb06dc 152
8c194133
PS
153// utility functions
154
155function error_or_ajax($message, $fromajax) {
156 if ($fromajax) {
157 print get_string($message, 'error');
158 exit;
159 } else {
160 print_error($message);
161 }
162}
163