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', "view.php?id=$course->id", $userid);
66 redirect($CFG->wwwroot
.'/user/index.php?id='.$course->id
);
69 if (! role_unassign(0, $USER->id
, 0, $context->id
)) {
70 error("An error occurred while trying to unenrol you.");
73 // force a refresh of mycourses
74 unset($USER->mycourses
);
75 add_to_log($course->id
, 'course', 'unenrol', "view.php?id=$course->id", $USER->id
);
77 redirect($CFG->wwwroot
);
82 $strunenrol = get_string('unenrol');
84 $navlinks[] = array('name' => $strunenrol, 'link' => null, 'type' => 'misc');
85 $navigation = build_navigation($navlinks);
87 print_header("$course->shortname: $strunenrol", $course->fullname
, $navigation);
90 if (!$user = get_record('user', 'id', $userid)) {
91 error('That user does not exist!');
93 $strunenrolsure = get_string('unenrolsure', '', fullname($user, true));
94 notice_yesno($strunenrolsure, "unenrol.php?id=$id&user=$user->id&confirm=yes&sesskey=".sesskey(),
95 $_SERVER['HTTP_REFERER']);
97 $strunenrolsure = get_string('unenrolsure', '', get_string("yourself"));
98 notice_yesno($strunenrolsure, "unenrol.php?id=$id&confirm=yes&sesskey=".sesskey(),
99 $_SERVER['HTTP_REFERER']);
102 print_footer($course);