MDL-15498: Completion system
[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:
16 error('Unsupported completion state');
17}
18$fromajax=optional_param('fromajax',0,PARAM_INT);
19
20function error_or_ajax($message) {
21 global $fromajax;
22 if($fromajax) {
23 print $message;
24 exit;
25 } else {
26 error($message);
27 }
28}
29
30// Get course-modules entry
31if(!($cm=$DB->get_record('course_modules',array('id'=>$cmid)))) {
32 error_or_ajax('Activity ID unknown');
33}
34
35if(!($course=$DB->get_record('course',array('id'=>$cm->course)))) {
36 error_or_ajax('Missing course (database corrupt?)');
37}
38
39// Check user is logged in
40require_login($course);
41
42// Check completion state is manual
43if($cm->completion!=COMPLETION_TRACKING_MANUAL) {
44 error_or_ajax('Activity does not provide manual completion tracking');
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 {
55 redirect('view.php?id='.$course->id);
56}
57?>