Search moodle.org's
Developer Documentation


   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  $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));
  63  
  64  $PAGE->set_pagelayout('admin');
  65  navigation_node::override_active_url($usersurl);
  66  
  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);
  71  }
  72  
  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');
  77  
  78  $PAGE->set_title($title);
  79  $PAGE->set_heading($title);
  80  $PAGE->navbar->add($title);
  81  $PAGE->navbar->add($fullname);
  82  
  83  echo $OUTPUT->header();
  84  echo $OUTPUT->heading($fullname);
  85  echo $OUTPUT->confirm($message, $yesurl, $returnurl);
  86  echo $OUTPUT->footer();

Search This Site: