MDL-80880 quiz: change display of previous attempts summary
[moodle.git] / course / togglecompletion.php
blob82ae6b625e5b629dfe9b3e78b235c01f0b13e78c
1 <?php
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.
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/>.
18 /**
19 * Toggles the manual completion flag for a particular activity or course completion
20 * and the current user.
22 * If by student params: course=2
23 * If by manager params: course=2&user=4&rolec=3&sesskey=ghfgsdf
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * @package course
29 require_once('../config.php');
30 require_once($CFG->libdir.'/completionlib.php');
32 // Parameters
33 $cmid = optional_param('id', 0, PARAM_INT);
34 $courseid = optional_param('course', 0, PARAM_INT);
35 $confirm = optional_param('confirm', 0, PARAM_BOOL);
37 // Check if we are marking a user complete via the completion report.
38 $user = optional_param('user', 0, PARAM_INT);
39 $rolec = optional_param('rolec', 0, PARAM_INT);
41 if (!$cmid && !$courseid) {
42 throw new \moodle_exception('invalidarguments');
45 // Process self completion
46 if ($courseid) {
47 $PAGE->set_url(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid)));
49 // Check user is logged in
50 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
51 $context = context_course::instance($course->id);
52 require_login($course);
54 $completion = new completion_info($course);
55 $trackeduser = ($user ? $user : $USER->id);
57 if (!$completion->is_enabled()) {
58 throw new moodle_exception('completionnotenabled', 'completion');
59 } else if (!$completion->is_tracked_user($trackeduser)) {
60 throw new moodle_exception('nottracked', 'completion');
63 if ($user && $rolec) {
64 require_sesskey();
66 completion_criteria::factory(array('id'=>$rolec, 'criteriatype'=>COMPLETION_CRITERIA_TYPE_ROLE)); //TODO: this is dumb, because it does not fetch the data?!?!
67 /** @var completion_criteria_role $criteria */
68 $criteria = completion_criteria_role::fetch(array('id'=>$rolec));
70 if ($criteria and user_has_role_assignment($USER->id, $criteria->role, $context->id)) {
71 $criteria_completions = $completion->get_completions($user, COMPLETION_CRITERIA_TYPE_ROLE);
73 foreach ($criteria_completions as $criteria_completion) {
74 if ($criteria_completion->criteriaid == $rolec) {
75 $criteria->complete($criteria_completion);
76 break;
81 // Return to previous page
82 $referer = get_local_referer(false);
83 if (!empty($referer)) {
84 redirect($referer);
85 } else {
86 redirect('view.php?id='.$course->id);
89 } else {
91 // Confirm with user
92 if ($confirm and confirm_sesskey()) {
93 $completion = $completion->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
95 if (!$completion) {
96 throw new \moodle_exception('noselfcompletioncriteria', 'completion');
99 // Check if the user has already marked themselves as complete
100 if ($completion->is_complete()) {
101 throw new \moodle_exception('useralreadymarkedcomplete', 'completion');
104 $completion->mark_complete();
106 redirect($CFG->wwwroot.'/course/view.php?id='.$courseid);
107 return;
110 $strconfirm = get_string('confirmselfcompletion', 'completion');
111 $PAGE->set_title($strconfirm);
112 $PAGE->set_heading($course->fullname);
113 $PAGE->navbar->add($strconfirm);
114 echo $OUTPUT->header();
115 $buttoncontinue = new single_button(new moodle_url('/course/togglecompletion.php', array('course'=>$courseid, 'confirm'=>1, 'sesskey'=>sesskey())), get_string('yes'), 'post');
116 $buttoncancel = new single_button(new moodle_url('/course/view.php', array('id'=>$courseid)), get_string('no'), 'get');
117 echo $OUTPUT->confirm($strconfirm, $buttoncontinue, $buttoncancel);
118 echo $OUTPUT->footer();
119 exit;
123 $targetstate = required_param('completionstate', PARAM_INT);
125 $PAGE->set_url('/course/togglecompletion.php', array('id'=>$cmid, 'completionstate'=>$targetstate));
127 switch($targetstate) {
128 case COMPLETION_COMPLETE:
129 case COMPLETION_INCOMPLETE:
130 break;
131 default:
132 throw new \moodle_exception('unsupportedstate');
135 // Get course-modules entry
136 $cm = get_coursemodule_from_id(null, $cmid, null, true, MUST_EXIST);
137 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
139 // Check user is logged in
140 require_login($course, false, $cm);
141 require_capability('moodle/course:togglecompletion', context_module::instance($cmid));
143 if (isguestuser() or !confirm_sesskey()) {
144 throw new \moodle_exception('error');
147 // Set up completion object and check it is enabled.
148 $completion = new completion_info($course);
149 if (!$completion->is_enabled()) {
150 throw new moodle_exception('completionnotenabled', 'completion');
153 // NOTE: All users are allowed to toggle their completion state, including
154 // users for whom completion information is not directly tracked. (I.e. even
155 // if you are a teacher, or admin who is not enrolled, you can still toggle
156 // your own completion state. You just don't appear on the reports.)
158 // Check completion state is manual
159 if($cm->completion != COMPLETION_TRACKING_MANUAL) {
160 throw new moodle_exception('cannotmanualctrack');
163 $completion->update_state($cm, $targetstate);
165 // In case of use in other areas of code we allow a 'backto' parameter, otherwise go back to course page.
166 if ($backto = optional_param('backto', null, PARAM_URL)) {
167 redirect($backto);
168 } else {
169 redirect(course_get_url($course, $cm->sectionnum));