Take out debugging & merge branch 'w05_MDL-26198_20_completion' of git://github.com...
[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/**
2be4d090
MD
19 * Toggles the manual completion flag for a particular activity or course completion
20 * and the current user.
d9cb06dc 21 *
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package course
24 */
4e781c7b 25
26require_once('../config.php');
27require_once($CFG->libdir.'/completionlib.php');
28
29// Parameters
2be4d090
MD
30$cmid = optional_param('id', 0, PARAM_INT);
31$courseid = optional_param('course', 0, PARAM_INT);
32$confirm = optional_param('confirm', 0, PARAM_BOOL);
33
34if (!$cmid && !$courseid) {
35 print_error('invalidarguments');
36}
37
38// Process self completion
39if ($courseid) {
49a33d8f 40 $PAGE->set_url(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid)));
9cedb80c 41
2be4d090 42 // Check user is logged in
49a33d8f 43 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
9cedb80c 44 $context = get_context_instance(CONTEXT_COURSE, $course->id);
2be4d090
MD
45 require_login($course);
46
47 $completion = new completion_info($course);
48
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);
52
53 if ($user && $rolec) {
9cedb80c
PS
54 require_sesskey();
55
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));
2be4d090 58
9cedb80c
PS
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);
2be4d090 61
9cedb80c
PS
62 foreach ($criteria_completions as $criteria_completion) {
63 if ($criteria_completion->criteriaid == $rolec) {
64 $criteria->complete($criteria_completion);
65 break;
66 }
2be4d090
MD
67 }
68 }
69
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);
75 }
76
77 } else {
78
79 // Confirm with user
9cedb80c 80 if ($confirm and confirm_sesskey()) {
2be4d090
MD
81 $completion = $completion->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
82
83 if (!$completion) {
84 print_error('noselfcompletioncriteria', 'completion');
85 }
86
87 // Check if the user has already marked themselves as complete
88 if ($completion->is_complete()) {
89 print_error('useralreadymarkedcomplete', 'completion');
90 }
91
92 $completion->mark_complete();
93
94 redirect($CFG->wwwroot.'/course/view.php?id='.$courseid);
95 return;
96 }
97
98 $strconfirm = get_string('confirmselfcompletion', 'completion');
49a33d8f
SH
99 $PAGE->set_title($strconfirm);
100 $PAGE->set_heading($course->fullname);
101 $PAGE->navbar->add($strconfirm);
102 echo $OUTPUT->header();
9cedb80c 103 $buttoncontinue = new single_button(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid, 'confirm'=>1, 'sesskey'=>sesskey())), get_string('yes'), 'post');
49a33d8f
SH
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();
2be4d090
MD
107 exit;
108 }
109}
110
111
8c194133
PS
112$targetstate = required_param('completionstate', PARAM_INT);
113$fromajax = optional_param('fromajax', 0, PARAM_INT);
d9cb06dc 114
8c194133 115$PAGE->set_url('/course/togglecompletion.php', array('id'=>$cmid, 'completionstate'=>$targetstate));
d9cb06dc 116
4e781c7b 117switch($targetstate) {
118 case COMPLETION_COMPLETE:
119 case COMPLETION_INCOMPLETE:
120 break;
121 default:
9b34dc6e 122 print_error('unsupportedstate');
4e781c7b 123}
4e781c7b 124
125// Get course-modules entry
8c194133
PS
126$cm = get_coursemodule_from_id(null, $cmid, null, false, MUST_EXIST);
127$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
4e781c7b 128
8c194133
PS
129// Check user is logged in
130require_login($course, false, $cm);
131
132if (isguestuser() or !confirm_sesskey()) {
133 print_error('error');
4e781c7b 134}
135
8c194133
PS
136// Now change state
137$completion = new completion_info($course);
138if (!$completion->is_enabled()) {
139 die;
140}
4e781c7b 141
142// Check completion state is manual
8c194133
PS
143if($cm->completion != COMPLETION_TRACKING_MANUAL) {
144 error_or_ajax('cannotmanualctrack', $fromajax);
4e781c7b 145}
146
8c194133 147$completion->update_state($cm, $targetstate);
4e781c7b 148
149// And redirect back to course
8c194133 150if ($fromajax) {
4e781c7b 151 print 'OK';
152} else {
e399d47c 153 // In case of use in other areas of code we allow a 'backto' parameter,
154 // otherwise go back to course page
8c194133 155 $backto = optional_param('backto', 'view.php?id='.$course->id, PARAM_URL);
e399d47c 156 redirect($backto);
4e781c7b 157}
d9cb06dc 158
8c194133
PS
159// utility functions
160
161function error_or_ajax($message, $fromajax) {
162 if ($fromajax) {
163 print get_string($message, 'error');
164 exit;
165 } else {
166 print_error($message);
167 }
168}
169