backup MDL-22251 fixed issue with delete_old_backup_dirs passing a full path rather...
[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/**
19 * Toggles the manual completion flag for a particular activity and the current user.
20 *
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package course
23 */
4e781c7b 24
25require_once('../config.php');
26require_once($CFG->libdir.'/completionlib.php');
27
28// Parameters
8c194133
PS
29$cmid = required_param('id', PARAM_INT);
30$targetstate = required_param('completionstate', PARAM_INT);
31$fromajax = optional_param('fromajax', 0, PARAM_INT);
d9cb06dc 32
8c194133 33$PAGE->set_url('/course/togglecompletion.php', array('id'=>$cmid, 'completionstate'=>$targetstate));
d9cb06dc 34
4e781c7b 35switch($targetstate) {
36 case COMPLETION_COMPLETE:
37 case COMPLETION_INCOMPLETE:
38 break;
39 default:
9b34dc6e 40 print_error('unsupportedstate');
4e781c7b 41}
4e781c7b 42
43// Get course-modules entry
8c194133
PS
44$cm = get_coursemodule_from_id(null, $cmid, null, false, MUST_EXIST);
45$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
4e781c7b 46
8c194133
PS
47// Check user is logged in
48require_login($course, false, $cm);
49
50if (isguestuser() or !confirm_sesskey()) {
51 print_error('error');
4e781c7b 52}
53
8c194133
PS
54// Now change state
55$completion = new completion_info($course);
56if (!$completion->is_enabled()) {
57 die;
58}
4e781c7b 59
60// Check completion state is manual
8c194133
PS
61if($cm->completion != COMPLETION_TRACKING_MANUAL) {
62 error_or_ajax('cannotmanualctrack', $fromajax);
4e781c7b 63}
64
8c194133 65$completion->update_state($cm, $targetstate);
4e781c7b 66
67// And redirect back to course
8c194133 68if ($fromajax) {
4e781c7b 69 print 'OK';
70} else {
e399d47c 71 // In case of use in other areas of code we allow a 'backto' parameter,
72 // otherwise go back to course page
8c194133 73 $backto = optional_param('backto', 'view.php?id='.$course->id, PARAM_URL);
e399d47c 74 redirect($backto);
4e781c7b 75}
d9cb06dc 76
8c194133
PS
77// utility functions
78
79function error_or_ajax($message, $fromajax) {
80 if ($fromajax) {
81 print get_string($message, 'error');
82 exit;
83 } else {
84 print_error($message);
85 }
86}
87