Moodle release 2.5.4
[moodle.git] / enrol / unenroluser.php
blob99033919001d03eee12004c5511a378d87623200
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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...
23 * @package core_enrol
24 * @copyright 2011 Petr skoda
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require('../config.php');
29 require_once("$CFG->dirroot/enrol/locallib.php");
30 require_once("$CFG->dirroot/enrol/renderer.php");
32 $ueid = required_param('ue', PARAM_INT); // user enrolment id
33 $confirm = optional_param('confirm', false, PARAM_BOOL);
34 $filter = optional_param('ifilter', 0, PARAM_INT);
36 $ue = $DB->get_record('user_enrolments', array('id' => $ueid), '*', MUST_EXIST);
37 $user = $DB->get_record('user', array('id'=>$ue->userid), '*', MUST_EXIST);
38 $instance = $DB->get_record('enrol', array('id'=>$ue->enrolid), '*', MUST_EXIST);
39 $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
41 $context = context_course::instance($course->id);
43 // set up PAGE url first!
44 $PAGE->set_url('/enrol/unenroluser.php', array('ue'=>$ueid, 'ifilter'=>$filter));
46 require_login($course);
48 if (!enrol_is_enabled($instance->enrol)) {
49 print_error('erroreditenrolment', 'enrol');
52 $plugin = enrol_get_plugin($instance->enrol);
54 if (!$plugin->allow_unenrol_user($instance, $ue) or !has_capability("enrol/$instance->enrol:unenrol", $context)) {
55 print_error('erroreditenrolment', 'enrol');
58 $manager = new course_enrolment_manager($PAGE, $course, $filter);
59 $table = new course_enrolment_users_table($manager, $PAGE);
61 $returnurl = new moodle_url('/enrol/users.php', array('id' => $course->id)+$manager->get_url_params()+$table->get_url_params());
62 $usersurl = new moodle_url('/enrol/users.php', array('id' => $course->id));
64 $PAGE->set_pagelayout('admin');
65 navigation_node::override_active_url($usersurl);
67 // If the unenrolment has been confirmed and the sesskey is valid unenrol the user.
68 if ($confirm && confirm_sesskey()) {
69 $plugin->unenrol_user($instance, $ue->userid);
70 redirect($returnurl);
73 $yesurl = new moodle_url($PAGE->url, array('confirm'=>1, 'sesskey'=>sesskey()));
74 $message = get_string('unenrolconfirm', 'core_enrol', array('user'=>fullname($user, true), 'course'=>format_string($course->fullname)));
75 $fullname = fullname($user);
76 $title = get_string('unenrol', 'core_enrol');
78 $PAGE->set_title($title);
79 $PAGE->set_heading($title);
80 $PAGE->navbar->add($title);
81 $PAGE->navbar->add($fullname);
83 echo $OUTPUT->header();
84 echo $OUTPUT->heading($fullname);
85 echo $OUTPUT->confirm($message, $yesurl, $returnurl);
86 echo $OUTPUT->footer();