MDL-20204 fixed HTML_ATTR_EMPTY regression
[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
29$cmid=required_param('id',PARAM_INT);
30$targetstate=required_param('completionstate',PARAM_INT);
d9cb06dc 31
a6855934 32$PAGE->set_url('/course/togglecompletion.php', array('id'=>$id,'completionstate'=>$targetstate));
d9cb06dc 33
4e781c7b 34switch($targetstate) {
35 case COMPLETION_COMPLETE:
36 case COMPLETION_INCOMPLETE:
37 break;
38 default:
9b34dc6e 39 print_error('unsupportedstate');
4e781c7b 40}
41$fromajax=optional_param('fromajax',0,PARAM_INT);
42
43function error_or_ajax($message) {
44 global $fromajax;
45 if($fromajax) {
9b34dc6e 46 print get_string($message, 'error');
4e781c7b 47 exit;
48 } else {
9b34dc6e 49 print_error($message);
4e781c7b 50 }
51}
52
53// Get course-modules entry
54if(!($cm=$DB->get_record('course_modules',array('id'=>$cmid)))) {
9b34dc6e 55 error_or_ajax('invalidactivityid');
4e781c7b 56}
57
58if(!($course=$DB->get_record('course',array('id'=>$cm->course)))) {
9b34dc6e 59 error_or_ajax('invalidcourseid');
4e781c7b 60}
61
62// Check user is logged in
63require_login($course);
64
65// Check completion state is manual
66if($cm->completion!=COMPLETION_TRACKING_MANUAL) {
9b34dc6e 67 error_or_ajax('cannotmanualctrack');
4e781c7b 68}
69
70// Now change state
71$completion=new completion_info($course);
72$completion->update_state($cm,$targetstate);
73
74// And redirect back to course
75if($fromajax) {
76 print 'OK';
77} else {
e399d47c 78 // In case of use in other areas of code we allow a 'backto' parameter,
79 // otherwise go back to course page
80 $backto=optional_param('backto','view.php?id='.$course->id,PARAM_URL);
81 redirect($backto);
4e781c7b 82}
d9cb06dc 83