2004-09-12 12:21:27 +00:00
|
|
|
<?php // $Id$
|
2002-06-04 06:30:51 +00:00
|
|
|
|
2007-08-17 19:09:11 +00:00
|
|
|
// Remove oneself or someone else from a course, unassigning all
|
2006-09-28 08:55:04 +00:00
|
|
|
// roles one might have
|
|
|
|
//
|
2007-08-17 19:09:11 +00:00
|
|
|
// This will not delete any of their data from the course,
|
|
|
|
// but will remove them from the participant list and prevent
|
2002-06-04 06:30:51 +00:00
|
|
|
// any course email being sent to them.
|
|
|
|
|
2003-01-05 14:19:20 +00:00
|
|
|
require_once("../config.php");
|
|
|
|
require_once("lib.php");
|
2002-06-04 06:30:51 +00:00
|
|
|
|
2006-04-12 16:59:51 +00:00
|
|
|
$id = required_param('id', PARAM_INT); //course
|
2006-09-28 08:55:04 +00:00
|
|
|
$userid = optional_param('user', 0, PARAM_INT); //course
|
2006-04-12 16:59:51 +00:00
|
|
|
$confirm = optional_param('confirm', 0, PARAM_BOOL);
|
2002-06-04 06:30:51 +00:00
|
|
|
|
2007-12-23 16:18:25 +00:00
|
|
|
if($userid == $USER->id){
|
|
|
|
// the rest of this code assumes $userid=0 means
|
|
|
|
// you are unassigning yourself, so set this for the
|
|
|
|
// correct capabiliy checks & language later
|
|
|
|
$userid = 0;
|
|
|
|
}
|
|
|
|
|
2008-06-01 18:12:24 +00:00
|
|
|
if (!$course = $DB->get_record('course', array('id'=>$id))) {
|
2008-05-01 05:44:41 +00:00
|
|
|
print_error('invalidcourseid');
|
2002-06-04 06:30:51 +00:00
|
|
|
}
|
2006-09-13 06:41:58 +00:00
|
|
|
|
|
|
|
if (! $context = get_context_instance(CONTEXT_COURSE, $course->id)) {
|
2008-05-01 05:44:41 +00:00
|
|
|
print_error('invalidcontext');
|
2002-06-19 15:42:49 +00:00
|
|
|
}
|
2002-06-04 06:30:51 +00:00
|
|
|
|
|
|
|
require_login($course->id);
|
|
|
|
|
2006-09-13 06:41:58 +00:00
|
|
|
if ($course->metacourse) {
|
2006-09-21 16:11:31 +00:00
|
|
|
print_error('cantunenrollfrommetacourse', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
|
2006-09-28 08:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($userid) { // Unenrolling someone else
|
|
|
|
require_capability('moodle/role:assign', $context, NULL, false);
|
2008-02-05 12:59:28 +00:00
|
|
|
|
|
|
|
$roles = get_user_roles($context, $userid, false);
|
|
|
|
|
|
|
|
// verify user may unassign all roles at course context
|
|
|
|
foreach($roles as $role) {
|
|
|
|
if (!user_can_assign($context, $role->roleid)) {
|
2008-05-01 05:44:41 +00:00
|
|
|
print_error('cannotunassignrolefrom', '', '',
|
|
|
|
$role->roleid);
|
2008-02-05 12:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-28 08:55:04 +00:00
|
|
|
} else { // Unenrol yourself
|
2006-09-13 06:41:58 +00:00
|
|
|
require_capability('moodle/role:unassignself', $context, NULL, false);
|
2003-10-12 18:03:50 +00:00
|
|
|
}
|
|
|
|
|
2007-09-19 07:15:38 +00:00
|
|
|
if (!empty($USER->access['rsw'][$context->path])) {
|
|
|
|
print_error('cantunenrollinthisrole', '',
|
|
|
|
$CFG->wwwroot.'/course/view.php?id='.$course->id);
|
2006-09-21 16:11:31 +00:00
|
|
|
}
|
|
|
|
|
2006-04-12 16:59:51 +00:00
|
|
|
if ($confirm and confirm_sesskey()) {
|
2006-09-28 08:55:04 +00:00
|
|
|
if ($userid) {
|
|
|
|
if (! role_unassign(0, $userid, 0, $context->id)) {
|
2008-05-01 05:44:41 +00:00
|
|
|
print_error("unenrolerror");
|
2006-09-28 08:55:04 +00:00
|
|
|
}
|
2006-10-23 03:12:37 +00:00
|
|
|
|
2008-05-16 02:15:23 +00:00
|
|
|
add_to_log($course->id, 'course', 'unenrol',
|
|
|
|
"view.php?id=$course->id", $course->id);
|
2006-10-23 03:12:37 +00:00
|
|
|
redirect($CFG->wwwroot.'/user/index.php?id='.$course->id);
|
|
|
|
|
2006-09-28 08:55:04 +00:00
|
|
|
} else {
|
|
|
|
if (! role_unassign(0, $USER->id, 0, $context->id)) {
|
2008-05-01 05:44:41 +00:00
|
|
|
print_error("unenrolerror");
|
2006-09-28 08:55:04 +00:00
|
|
|
}
|
2007-09-19 07:12:43 +00:00
|
|
|
|
2007-09-19 07:17:23 +00:00
|
|
|
// force a refresh of mycourses
|
|
|
|
unset($USER->mycourses);
|
2008-05-16 02:15:23 +00:00
|
|
|
add_to_log($course->id, 'course', 'unenrol',
|
|
|
|
"view.php?id=$course->id", $course->id);
|
2002-06-04 06:30:51 +00:00
|
|
|
|
2006-10-23 03:12:37 +00:00
|
|
|
redirect($CFG->wwwroot);
|
|
|
|
}
|
2002-06-04 06:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-13 06:41:58 +00:00
|
|
|
$strunenrol = get_string('unenrol');
|
2007-08-17 19:09:11 +00:00
|
|
|
$navlinks = array();
|
|
|
|
$navlinks[] = array('name' => $strunenrol, 'link' => null, 'type' => 'misc');
|
|
|
|
$navigation = build_navigation($navlinks);
|
2002-08-11 15:41:54 +00:00
|
|
|
|
2007-08-17 19:09:11 +00:00
|
|
|
print_header("$course->shortname: $strunenrol", $course->fullname, $navigation);
|
2002-06-04 06:30:51 +00:00
|
|
|
|
2006-09-28 08:55:04 +00:00
|
|
|
if ($userid) {
|
2008-06-02 08:13:24 +00:00
|
|
|
if (!$user = $DB->get_record('user', array('id'=>$userid))) {
|
2008-05-01 05:44:41 +00:00
|
|
|
print_error('nousers');
|
2006-09-28 08:55:04 +00:00
|
|
|
}
|
|
|
|
$strunenrolsure = get_string('unenrolsure', '', fullname($user, true));
|
2007-08-17 19:09:11 +00:00
|
|
|
notice_yesno($strunenrolsure, "unenrol.php?id=$id&user=$user->id&confirm=yes&sesskey=".sesskey(),
|
2006-09-28 08:55:04 +00:00
|
|
|
$_SERVER['HTTP_REFERER']);
|
|
|
|
} else {
|
|
|
|
$strunenrolsure = get_string('unenrolsure', '', get_string("yourself"));
|
2007-08-17 19:09:11 +00:00
|
|
|
notice_yesno($strunenrolsure, "unenrol.php?id=$id&confirm=yes&sesskey=".sesskey(),
|
2006-09-28 08:55:04 +00:00
|
|
|
$_SERVER['HTTP_REFERER']);
|
|
|
|
}
|
2002-06-04 06:30:51 +00:00
|
|
|
|
2005-03-17 15:12:07 +00:00
|
|
|
print_footer($course);
|
2002-06-04 06:30:51 +00:00
|
|
|
|
|
|
|
?>
|