Skip to content

Commit

Permalink
Fixed up this script so that self-unenrolment works as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
moodler committed Sep 13, 2006
1 parent 8d630fd commit 761a155
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions course/unenrol.php
@@ -1,6 +1,6 @@
<?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.
Expand All @@ -9,55 +9,45 @@
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&amp;user=$user->id&amp;confirm=yes&amp;sesskey=$USER->sesskey", $_SERVER['HTTP_REFERER']);
notice_yesno($strunenrolsure, "unenrol.php?id=$id&amp;confirm=yes&amp;sesskey=$USER->sesskey", $_SERVER['HTTP_REFERER']);

print_footer($course);

Expand Down

0 comments on commit 761a155

Please sign in to comment.