question MDL-19820 Replaced deprecated functions
[moodle.git] / course / togglecompletion.php
CommitLineData
4e781c7b 1<?php
2// Toggles the manual completion flag for a particular activity and the current
3// user.
4
5require_once('../config.php');
6require_once($CFG->libdir.'/completionlib.php');
7
8// Parameters
9$cmid=required_param('id',PARAM_INT);
10$targetstate=required_param('completionstate',PARAM_INT);
11switch($targetstate) {
12 case COMPLETION_COMPLETE:
13 case COMPLETION_INCOMPLETE:
14 break;
15 default:
9b34dc6e 16 print_error('unsupportedstate');
4e781c7b 17}
18$fromajax=optional_param('fromajax',0,PARAM_INT);
19
20function error_or_ajax($message) {
21 global $fromajax;
22 if($fromajax) {
9b34dc6e 23 print get_string($message, 'error');
4e781c7b 24 exit;
25 } else {
9b34dc6e 26 print_error($message);
4e781c7b 27 }
28}
29
30// Get course-modules entry
31if(!($cm=$DB->get_record('course_modules',array('id'=>$cmid)))) {
9b34dc6e 32 error_or_ajax('invalidactivityid');
4e781c7b 33}
34
35if(!($course=$DB->get_record('course',array('id'=>$cm->course)))) {
9b34dc6e 36 error_or_ajax('invalidcourseid');
4e781c7b 37}
38
39// Check user is logged in
40require_login($course);
41
42// Check completion state is manual
43if($cm->completion!=COMPLETION_TRACKING_MANUAL) {
9b34dc6e 44 error_or_ajax('cannotmanualctrack');
4e781c7b 45}
46
47// Now change state
48$completion=new completion_info($course);
49$completion->update_state($cm,$targetstate);
50
51// And redirect back to course
52if($fromajax) {
53 print 'OK';
54} else {
e399d47c 55 // In case of use in other areas of code we allow a 'backto' parameter,
56 // otherwise go back to course page
57 $backto=optional_param('backto','view.php?id='.$course->id,PARAM_URL);
58 redirect($backto);
4e781c7b 59}
60?>