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($userid == $USER->id
){
18 // the rest of this code assumes $userid=0 means
19 // you are unassigning yourself, so set this for the
20 // correct capabiliy checks & language later
24 if (! $course = get_record('course', 'id', $id) ) {
25 error('Invalid course id');
28 if (! $context = get_context_instance(CONTEXT_COURSE
, $course->id
)) {
29 error('Invalid context');
32 require_login($course->id
);
34 if ($course->metacourse
) {
35 print_error('cantunenrollfrommetacourse', '', $CFG->wwwroot
.'/course/view.php?id='.$course->id
);
38 if ($userid) { // Unenrolling someone else
39 require_capability('moodle/role:assign', $context, NULL, false);
41 $roles = get_user_roles($context, $userid, false);
43 // verify user may unassign all roles at course context
44 foreach($roles as $role) {
45 if (!user_can_assign($context, $role->roleid
)) {
46 error('Can not unassign this user from role id:'.$role->roleid
);
50 } else { // Unenrol yourself
51 require_capability('moodle/role:unassignself', $context, NULL, false);
54 if (!empty($USER->access
['rsw'][$context->path
])) {
55 print_error('cantunenrollinthisrole', '',
56 $CFG->wwwroot
.'/course/view.php?id='.$course->id
);
59 if ($confirm and confirm_sesskey()) {
61 if (! role_unassign(0, $userid, 0, $context->id
)) {
62 error("An error occurred while trying to unenrol that person.");
65 add_to_log($course->id
, 'course', 'unenrol',
66 "view.php?id=$course->id", $course->id
);
67 redirect($CFG->wwwroot
.'/user/index.php?id='.$course->id
);
70 if (! role_unassign(0, $USER->id
, 0, $context->id
)) {
71 error("An error occurred while trying to unenrol you.");
74 // force a refresh of mycourses
75 unset($USER->mycourses
);
76 add_to_log($course->id
, 'course', 'unenrol',
77 "view.php?id=$course->id", $course->id
);
79 redirect($CFG->wwwroot
);
84 $strunenrol = get_string('unenrol');
86 $navlinks[] = array('name' => $strunenrol, 'link' => null, 'type' => 'misc');
87 $navigation = build_navigation($navlinks);
89 print_header("$course->shortname: $strunenrol", $course->fullname
, $navigation);
92 if (!$user = get_record('user', 'id', $userid)) {
93 error('That user does not exist!');
95 $strunenrolsure = get_string('unenrolsure', '', fullname($user, true));
96 notice_yesno($strunenrolsure, "unenrol.php?id=$id&user=$user->id&confirm=yes&sesskey=".sesskey(),
97 $_SERVER['HTTP_REFERER']);
99 $strunenrolsure = get_string('unenrolsure', '', get_string("yourself"));
100 notice_yesno($strunenrolsure, "unenrol.php?id=$id&confirm=yes&sesskey=".sesskey(),
101 $_SERVER['HTTP_REFERER']);
104 print_footer($course);