Merge branch 'MDL-62415-master' of git://github.com/peterRd/moodle
[moodle.git] / enrol / unenroluser.php
blob4690c57516cc284cc56fea230793733edebb1267
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 $usersurl = new moodle_url('/user/index.php', array('id' => $course->id));
63 $PAGE->set_pagelayout('admin');
64 navigation_node::override_active_url($usersurl);
66 // If the unenrolment has been confirmed and the sesskey is valid unenrol the user.
67 if ($confirm && confirm_sesskey()) {
68 $plugin->unenrol_user($instance, $ue->userid);
69 redirect($usersurl);
72 $yesurl = new moodle_url($PAGE->url, array('confirm'=>1, 'sesskey'=>sesskey()));
73 $message = get_string('unenrolconfirm', 'core_enrol',
75 'user' => fullname($user, true),
76 'course' => format_string($course->fullname),
77 'enrolinstancename' => $plugin->get_instance_name($instance)
80 $fullname = fullname($user);
81 $title = get_string('unenrol', 'core_enrol');
83 $PAGE->set_title($title);
84 $PAGE->set_heading($title);
85 $PAGE->navbar->add($title);
86 $PAGE->navbar->add($fullname);
88 echo $OUTPUT->header();
89 echo $OUTPUT->heading($fullname);
90 echo $OUTPUT->confirm($message, $yesurl, $usersurl);
91 echo $OUTPUT->footer();