<?php // $Id$
-// Removes a student from a class
+// Remove oneself from a course, unassigning all roles one might have
// This will not delete any of their data from the course,
// but will remove them from the student list and prevent
// any course email being sent to them.
require_once("lib.php");
$id = required_param('id', PARAM_INT); //course
- $user = optional_param('user', $USER->id, PARAM_INT); //user
$confirm = optional_param('confirm', 0, PARAM_BOOL);
- if (! $course = get_record("course", "id", $id) ) {
- error("That's an invalid course id");
+ if (! $course = get_record('course', 'id', $id) ) {
+ error('Invalid course id');
}
- if (! $user = get_record("user", "id", $user) ) {
- error("That's an invalid user id");
+
+ if (! $context = get_context_instance(CONTEXT_COURSE, $course->id)) {
+ error('Invalid context');
}
require_login($course->id);
- if ($user->id != $USER->id and !isteacheredit($course->id)) {
- error("You must be a teacher with editing rights to do this");
- }
-
- if ($user->id == $USER->id and !$CFG->allowunenroll or $course->metacourse) {
- error("You are not allowed to unenroll");
+ if ($course->metacourse) {
+ print_error('cantunenrollfrommetacourse');
+ } else {
+ require_capability('moodle/role:unassignself', $context, NULL, false);
+function has_capability($capability, $context=NULL, $userid=NULL, $doanything='true') {
}
if ($confirm and confirm_sesskey()) {
- if (! unenrol_student($user->id, $course->id)) {
+ if (! role_unassign(0, $USER->id, 0, $context->id)) {
error("An error occurred while trying to unenrol you.");
}
- add_to_log($course->id, "course", "unenrol", "view.php?id=$course->id", "$user->id");
+ add_to_log($course->id, 'course', 'unenrol', "view.php?id=$course->id", $USER->id);
- if ($user->id == $USER->id) {
- unset($USER->student["$id"]);
- redirect("$CFG->wwwroot/");
- }
-
- redirect("$CFG->wwwroot/user/index.php?id=$course->id");
+ redirect($CFG->wwwroot.'/');
}
- $strunenrol = get_string("unenrol");
+ $strunenrol = get_string('unenrol');
- print_header("$course->shortname: $strunenrol", "$course->fullname",
+ print_header("$course->shortname: $strunenrol", $course->fullname,
"<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> $strunenrol");
- if ($user->id == $USER->id) {
- $strunenrolsure = get_string("unenrolsure", "", get_string("yourself"));
- } else {
- $strunenrolsure = get_string("unenrolsure", "", fullname($user, true));
- }
+ $strunenrolsure = get_string('unenrolsure', '', get_string("yourself"));
- notice_yesno ($strunenrolsure, "unenrol.php?id=$id&user=$user->id&confirm=yes&sesskey=$USER->sesskey", $_SERVER['HTTP_REFERER']);
+ notice_yesno($strunenrolsure, "unenrol.php?id=$id&confirm=yes&sesskey=$USER->sesskey", $_SERVER['HTTP_REFERER']);
print_footer($course);