3 // Remove oneself or someone else from a course, unassigning all
4 // roles one might have
6 // This will not delete any of their data from the course,
7 // but will remove them from the participant list and prevent
8 // any course email being sent to them.
10 require_once("../config.php");
11 require_once("lib.php");
13 $id = required_param('id', PARAM_INT
); //course
14 $userid = optional_param('user', 0, PARAM_INT
); //course
15 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
17 if (! $course = get_record('course', 'id', $id) ) {
18 error('Invalid course id');
21 if (! $context = get_context_instance(CONTEXT_COURSE
, $course->id
)) {
22 error('Invalid context');
25 require_login($course->id
);
27 if ($course->metacourse
) {
28 print_error('cantunenrollfrommetacourse', '', $CFG->wwwroot
.'/course/view.php?id='.$course->id
);
31 if ($userid) { // Unenrolling someone else
32 require_capability('moodle/role:assign', $context, NULL, false);
33 } else { // Unenrol yourself
34 require_capability('moodle/role:unassignself', $context, NULL, false);
37 if (!empty($USER->switchrole
[$context->id
])) {
38 print_error('cantunenrollinthisrole', '', $CFG->wwwroot
.'/course/view.php?id='.$course->id
);
41 if ($confirm and confirm_sesskey()) {
43 if (! role_unassign(0, $userid, 0, $context->id
)) {
44 error("An error occurred while trying to unenrol that person.");
47 add_to_log($course->id
, 'course', 'unenrol', "view.php?id=$course->id", $userid);
48 redirect($CFG->wwwroot
.'/user/index.php?id='.$course->id
);
51 if (! role_unassign(0, $USER->id
, 0, $context->id
)) {
52 error("An error occurred while trying to unenrol you.");
54 add_to_log($course->id
, 'course', 'unenrol', "view.php?id=$course->id", $USER->id
);
56 redirect($CFG->wwwroot
);
61 $strunenrol = get_string('unenrol');
63 $navlinks[] = array('name' => $strunenrol, 'link' => null, 'type' => 'misc');
64 $navigation = build_navigation($navlinks);
66 print_header("$course->shortname: $strunenrol", $course->fullname
, $navigation);
69 if (!$user = get_record('user', 'id', $userid)) {
70 error('That user does not exist!');
72 $strunenrolsure = get_string('unenrolsure', '', fullname($user, true));
73 notice_yesno($strunenrolsure, "unenrol.php?id=$id&user=$user->id&confirm=yes&sesskey=".sesskey(),
74 $_SERVER['HTTP_REFERER']);
76 $strunenrolsure = get_string('unenrolsure', '', get_string("yourself"));
77 notice_yesno($strunenrolsure, "unenrol.php?id=$id&confirm=yes&sesskey=".sesskey(),
78 $_SERVER['HTTP_REFERER']);
81 print_footer($course);