moodle/course/unenrol.php
skodak 128f098418 MDL-11640
* improved handling of dirty contexts in general - caching, switching in cron, marking, loading, etc.
* role_assing() and role_unassign() now marks dirty contexts because we use has_capability() in this function - we can not do it later outside to speedup bulk operations
* fixed some inline docs
* fixed notice from rs closing
* removed cached $CONTEXT from has_capability() - $context is now required parameter; this was hiding serious bugs when context did not exist or ppl passed false as parameter
* removed some ===, we can not use these on function parameters - we must support ints, strings, '', nulls, etc. - this could be a source of really dangerous bugs in future
* some other improvements and fixes - documented inline
2007-10-09 12:49:54 +00:00

88 lines
3.1 KiB
PHP

<?php // $Id$
// Remove oneself or someone else 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 participant list and prevent
// any course email being sent to them.
require_once("../config.php");
require_once("lib.php");
$id = required_param('id', PARAM_INT); //course
$userid = optional_param('user', 0, PARAM_INT); //course
$confirm = optional_param('confirm', 0, PARAM_BOOL);
if (! $course = get_record('course', 'id', $id) ) {
error('Invalid course id');
}
if (! $context = get_context_instance(CONTEXT_COURSE, $course->id)) {
error('Invalid context');
}
require_login($course->id);
if ($course->metacourse) {
print_error('cantunenrollfrommetacourse', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
}
if ($userid) { // Unenrolling someone else
require_capability('moodle/role:assign', $context, NULL, false);
} else { // Unenrol yourself
require_capability('moodle/role:unassignself', $context, NULL, false);
}
if (!empty($USER->access['rsw'][$context->path])) {
print_error('cantunenrollinthisrole', '',
$CFG->wwwroot.'/course/view.php?id='.$course->id);
}
if ($confirm and confirm_sesskey()) {
if ($userid) {
if (! role_unassign(0, $userid, 0, $context->id)) {
error("An error occurred while trying to unenrol that person.");
}
add_to_log($course->id, 'course', 'unenrol', "view.php?id=$course->id", $userid);
redirect($CFG->wwwroot.'/user/index.php?id='.$course->id);
} else {
if (! role_unassign(0, $USER->id, 0, $context->id)) {
error("An error occurred while trying to unenrol you.");
}
// force a refresh of mycourses
unset($USER->mycourses);
add_to_log($course->id, 'course', 'unenrol', "view.php?id=$course->id", $USER->id);
redirect($CFG->wwwroot);
}
}
$strunenrol = get_string('unenrol');
$navlinks = array();
$navlinks[] = array('name' => $strunenrol, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header("$course->shortname: $strunenrol", $course->fullname, $navigation);
if ($userid) {
if (!$user = get_record('user', 'id', $userid)) {
error('That user does not exist!');
}
$strunenrolsure = get_string('unenrolsure', '', fullname($user, true));
notice_yesno($strunenrolsure, "unenrol.php?id=$id&amp;user=$user->id&amp;confirm=yes&amp;sesskey=".sesskey(),
$_SERVER['HTTP_REFERER']);
} else {
$strunenrolsure = get_string('unenrolsure', '', get_string("yourself"));
notice_yesno($strunenrolsure, "unenrol.php?id=$id&amp;confirm=yes&amp;sesskey=".sesskey(),
$_SERVER['HTTP_REFERER']);
}
print_footer($course);
?>