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->access
['rsw'][$context->path
])) {
38 print_error('cantunenrollinthisrole', '',
39 $CFG->wwwroot
.'/course/view.php?id='.$course->id
);
42 if ($confirm and confirm_sesskey()) {
44 if (! role_unassign(0, $userid, 0, $context->id
)) {
45 error("An error occurred while trying to unenrol that person.");
48 // force accessinfo refresh for users visiting this context...
49 mark_context_dirty($context->path
);
51 add_to_log($course->id
, 'course', 'unenrol', "view.php?id=$course->id", $userid);
52 redirect($CFG->wwwroot
.'/user/index.php?id='.$course->id
);
55 if (! role_unassign(0, $USER->id
, 0, $context->id
)) {
56 error("An error occurred while trying to unenrol you.");
59 // force accessinfo refresh for users visiting this context...
60 mark_context_dirty($context->path
);
61 // force a refresh of mycourses
62 unset($USER->mycourses
);
63 add_to_log($course->id
, 'course', 'unenrol', "view.php?id=$course->id", $USER->id
);
65 redirect($CFG->wwwroot
);
70 $strunenrol = get_string('unenrol');
72 $navlinks[] = array('name' => $strunenrol, 'link' => null, 'type' => 'misc');
73 $navigation = build_navigation($navlinks);
75 print_header("$course->shortname: $strunenrol", $course->fullname
, $navigation);
78 if (!$user = get_record('user', 'id', $userid)) {
79 error('That user does not exist!');
81 $strunenrolsure = get_string('unenrolsure', '', fullname($user, true));
82 notice_yesno($strunenrolsure, "unenrol.php?id=$id&user=$user->id&confirm=yes&sesskey=".sesskey(),
83 $_SERVER['HTTP_REFERER']);
85 $strunenrolsure = get_string('unenrolsure', '', get_string("yourself"));
86 notice_yesno($strunenrolsure, "unenrol.php?id=$id&confirm=yes&sesskey=".sesskey(),
87 $_SERVER['HTTP_REFERER']);
90 print_footer($course);