From: moodler Date: Wed, 13 Sep 2006 06:41:58 +0000 (+0000) Subject: Fixed up this script so that self-unenrolment works as expected X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=761a15574c3e0c1833d4fdd8a6369c2333dab592;p=moodle.git Fixed up this script so that self-unenrolment works as expected --- diff --git a/course/unenrol.php b/course/unenrol.php index 4213ade65e..4d0cd8b5ff 100644 --- a/course/unenrol.php +++ b/course/unenrol.php @@ -1,6 +1,6 @@ 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, "wwwroot/course/view.php?id=$course->id\">$course->shortname -> $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);