Merge branch 'MDL-26445' of git://github.com/timhunt/moodle
[moodle.git] / course / togglecompletion.php
blobd7201443c89bc365a7b0f9cb67c153c886d19936
1 <?php
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.
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/>.
18 /**
19 * Toggles the manual completion flag for a particular activity or course completion
20 * and the current user.
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package course
26 require_once('../config.php');
27 require_once($CFG->libdir.'/completionlib.php');
29 // Parameters
30 $cmid = optional_param('id', 0, PARAM_INT);
31 $courseid = optional_param('course', 0, PARAM_INT);
32 $confirm = optional_param('confirm', 0, PARAM_BOOL);
34 if (!$cmid && !$courseid) {
35 print_error('invalidarguments');
38 // Process self completion
39 if ($courseid) {
40 $PAGE->set_url(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid)));
42 // Check user is logged in
43 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
44 $context = get_context_instance(CONTEXT_COURSE, $course->id);
45 require_login($course);
47 $completion = new completion_info($course);
49 // Check if we are marking a user complete via the completion report
50 $user = optional_param('user', 0, PARAM_INT);
51 $rolec = optional_param('rolec', 0, PARAM_INT);
53 if ($user && $rolec) {
54 require_sesskey();
56 completion_criteria::factory((object) array('id'=>$rolec, 'criteriatype'=>COMPLETION_CRITERIA_TYPE_ROLE)); //TODO: this is dumb, because it does not fetch the data?!?!
57 $criteria = completion_criteria_role::fetch(array('id'=>$rolec));
59 if ($criteria and user_has_role_assignment($USER->id, $criteria->role, $context->id)) {
60 $criteria_completions = $completion->get_completions($user, COMPLETION_CRITERIA_TYPE_ROLE);
62 foreach ($criteria_completions as $criteria_completion) {
63 if ($criteria_completion->criteriaid == $rolec) {
64 $criteria->complete($criteria_completion);
65 break;
70 // Return to previous page
71 if (!empty($_SERVER['HTTP_REFERER'])) {
72 redirect($_SERVER['HTTP_REFERER']);
73 } else {
74 redirect('view.php?id='.$course->id);
77 } else {
79 // Confirm with user
80 if ($confirm and confirm_sesskey()) {
81 $completion = $completion->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
83 if (!$completion) {
84 print_error('noselfcompletioncriteria', 'completion');
87 // Check if the user has already marked themselves as complete
88 if ($completion->is_complete()) {
89 print_error('useralreadymarkedcomplete', 'completion');
92 $completion->mark_complete();
94 redirect($CFG->wwwroot.'/course/view.php?id='.$courseid);
95 return;
98 $strconfirm = get_string('confirmselfcompletion', 'completion');
99 $PAGE->set_title($strconfirm);
100 $PAGE->set_heading($course->fullname);
101 $PAGE->navbar->add($strconfirm);
102 echo $OUTPUT->header();
103 $buttoncontinue = new single_button(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid, 'confirm'=>1, 'sesskey'=>sesskey())), get_string('yes'), 'post');
104 $buttoncancel = new single_button(new moodle_url('/course/view.php', array('id'=>$courseid)), get_string('no'), 'get');
105 echo $OUTPUT->confirm($strconfirm, $buttoncontinue, $buttoncancel);
106 echo $OUTPUT->footer();
107 exit;
112 $targetstate = required_param('completionstate', PARAM_INT);
113 $fromajax = optional_param('fromajax', 0, PARAM_INT);
115 $PAGE->set_url('/course/togglecompletion.php', array('id'=>$cmid, 'completionstate'=>$targetstate));
117 switch($targetstate) {
118 case COMPLETION_COMPLETE:
119 case COMPLETION_INCOMPLETE:
120 break;
121 default:
122 print_error('unsupportedstate');
125 // Get course-modules entry
126 $cm = get_coursemodule_from_id(null, $cmid, null, false, MUST_EXIST);
127 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
129 // Check user is logged in
130 require_login($course, false, $cm);
132 if (isguestuser() or !confirm_sesskey()) {
133 print_error('error');
136 // Now change state
137 $completion = new completion_info($course);
138 if (!$completion->is_enabled()) {
139 die;
142 // Check completion state is manual
143 if($cm->completion != COMPLETION_TRACKING_MANUAL) {
144 error_or_ajax('cannotmanualctrack', $fromajax);
147 $completion->update_state($cm, $targetstate);
149 // And redirect back to course
150 if ($fromajax) {
151 print 'OK';
152 } else {
153 // In case of use in other areas of code we allow a 'backto' parameter,
154 // otherwise go back to course page
155 $backto = optional_param('backto', 'view.php?id='.$course->id, PARAM_URL);
156 redirect($backto);
159 // utility functions
161 function error_or_ajax($message, $fromajax) {
162 if ($fromajax) {
163 print get_string($message, 'error');
164 exit;
165 } else {
166 print_error($message);