Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Completely unenrol a user from a course.
  19   *
  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...
  22   *
  23   * @package    core_enrol
  24   * @copyright  2011 Petr skoda
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  require('../config.php');
  29  require_once("$CFG->dirroot/enrol/locallib.php");
  30  require_once("$CFG->dirroot/enrol/renderer.php");
  31  
  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);
  35  
  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);
  40  
  41  $context = context_course::instance($course->id);
  42  
  43  // set up PAGE url first!
  44  $PAGE->set_url('/enrol/unenroluser.php', array('ue'=>$ueid, 'ifilter'=>$filter));
  45  
  46  require_login($course);
  47  
  48  if (!enrol_is_enabled($instance->enrol)) {
  49      print_error('erroreditenrolment', 'enrol');
  50  }
  51  
  52  $plugin = enrol_get_plugin($instance->enrol);
  53  
  54  if (!$plugin->allow_unenrol_user($instance, $ue) or !has_capability("enrol/$instance->enrol:unenrol", $context)) {
  55      print_error('erroreditenrolment', 'enrol');
  56  }
  57  
  58  $manager = new course_enrolment_manager($PAGE, $course, $filter);
  59  $table = new course_enrolment_users_table($manager, $PAGE);
  60  
  61  $usersurl = new moodle_url('/user/index.php', array('id' => $course->id));
  62  
  63  $PAGE->set_pagelayout('admin');
  64  navigation_node::override_active_url($usersurl);
  65  
  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);
  70  }
  71  
  72  $yesurl = new moodle_url($PAGE->url, array('confirm'=>1, 'sesskey'=>sesskey()));
  73  $message = get_string('unenrolconfirm', 'core_enrol',
  74      [
  75          'user' => fullname($user, true),
  76          'course' => format_string($course->fullname),
  77          'enrolinstancename' => $plugin->get_instance_name($instance)
  78      ]
  79  );
  80  $fullname = fullname($user);
  81  $title = get_string('unenrol', 'core_enrol');
  82  
  83  $PAGE->set_title($title);
  84  $PAGE->set_heading($title);
  85  $PAGE->navbar->add($title);
  86  $PAGE->navbar->add($fullname);
  87  
  88  echo $OUTPUT->header();
  89  echo $OUTPUT->heading($fullname);
  90  echo $OUTPUT->confirm($message, $yesurl, $usersurl);
  91  echo $OUTPUT->footer();