2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Completely unenrol a user from a course.
20 * Please note when unenrolling a user all of their grades are removed as well,
21 * most ppl actually expect enrolments to be suspended only...
25 * @copyright 2011 Petr skoda
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require('../config.php');
30 require_once("$CFG->dirroot/enrol/locallib.php");
31 require_once("$CFG->dirroot/enrol/renderer.php");
33 $ueid = required_param('ue', PARAM_INT
); // user enrolment id
34 $confirm = optional_param('confirm', false, PARAM_BOOL
);
35 $filter = optional_param('ifilter', 0, PARAM_INT
);
37 $ue = $DB->get_record('user_enrolments', array('id' => $ueid), '*', MUST_EXIST
);
38 $user = $DB->get_record('user', array('id'=>$ue->userid
), '*', MUST_EXIST
);
39 $instance = $DB->get_record('enrol', array('id'=>$ue->enrolid
), '*', MUST_EXIST
);
40 $course = $DB->get_record('course', array('id'=>$instance->courseid
), '*', MUST_EXIST
);
42 $context = context_course
::instance($course->id
);
44 // set up PAGE url first!
45 $PAGE->set_url('/enrol/unenroluser.php', array('ue'=>$ueid, 'ifilter'=>$filter));
47 require_login($course);
49 if (!enrol_is_enabled($instance->enrol
)) {
50 print_error('erroreditenrolment', 'enrol');
53 $plugin = enrol_get_plugin($instance->enrol
);
55 if (!$plugin->allow_unenrol_user($instance, $ue) or !has_capability("enrol/$instance->enrol:unenrol", $context)) {
56 print_error('erroreditenrolment', 'enrol');
59 $manager = new course_enrolment_manager($PAGE, $course, $filter);
60 $table = new course_enrolment_users_table($manager, $PAGE);
62 $returnurl = new moodle_url('/enrol/users.php', array('id' => $course->id
)+
$manager->get_url_params()+
$table->get_url_params());
63 $usersurl = new moodle_url('/enrol/users.php', array('id' => $course->id
));
65 $PAGE->set_pagelayout('admin');
66 navigation_node
::override_active_url($usersurl);
68 // If the unenrolment has been confirmed and the sesskey is valid unenrol the user.
69 if ($confirm && confirm_sesskey()) {
70 $plugin->unenrol_user($instance, $ue->userid
);
74 $yesurl = new moodle_url($PAGE->url
, array('confirm'=>1, 'sesskey'=>sesskey()));
75 $message = get_string('unenrolconfirm', 'core_enrol', array('user'=>fullname($user, true), 'course'=>format_string($course->fullname
)));
76 $fullname = fullname($user);
77 $title = get_string('unenrol', 'core_enrol');
79 $PAGE->set_title($title);
80 $PAGE->set_heading($title);
81 $PAGE->navbar
->add($title);
82 $PAGE->navbar
->add($fullname);
84 echo $OUTPUT->header();
85 echo $OUTPUT->heading($fullname);
86 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
87 echo $OUTPUT->footer();