MDL-29148 MNet - improved handling of a verification function return code
[moodle.git] / course / unenrol.php
blob7121f3c6778fa4179d4c815382f699a093b0ad6d
1 <?php // $Id$
3 // Remove oneself or someone else from a course, unassigning all
4 // roles one might have
5 //
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
21 $userid = 0;
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()) {
60 if ($userid) {
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);
69 } else {
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');
85 $navlinks = array();
86 $navlinks[] = array('name' => $strunenrol, 'link' => null, 'type' => 'misc');
87 $navigation = build_navigation($navlinks);
89 print_header("$course->shortname: $strunenrol", $course->fullname, $navigation);
91 if ($userid) {
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&amp;user=$user->id&amp;confirm=yes&amp;sesskey=".sesskey(),
97 $_SERVER['HTTP_REFERER']);
98 } else {
99 $strunenrolsure = get_string('unenrolsure', '', get_string("yourself"));
100 notice_yesno($strunenrolsure, "unenrol.php?id=$id&amp;confirm=yes&amp;sesskey=".sesskey(),
101 $_SERVER['HTTP_REFERER']);
104 print_footer($course);