forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
unenrol.php
107 lines (83 loc) · 3.63 KB
/
unenrol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php // $Id$
// Remove oneself or someone else from a course, unassigning all
// roles one might have
//
// This will not delete any of their data from the course,
// but will remove them from the participant list and prevent
// any course email being sent to them.
require_once("../config.php");
require_once("lib.php");
$id = required_param('id', PARAM_INT); //course
$userid = optional_param('user', 0, PARAM_INT); //course
$confirm = optional_param('confirm', 0, PARAM_BOOL);
if($userid == $USER->id){
// the rest of this code assumes $userid=0 means
// you are unassigning yourself, so set this for the
// correct capabiliy checks & language later
$userid = 0;
}
if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('invalidcourseid');
}
if (! $context = get_context_instance(CONTEXT_COURSE, $course->id)) {
print_error('invalidcontext');
}
require_login($course->id);
if ($course->metacourse) {
print_error('cantunenrollfrommetacourse', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
}
if ($userid) { // Unenrolling someone else
require_capability('moodle/role:assign', $context, NULL, false);
$roles = get_user_roles($context, $userid, false);
// verify user may unassign all roles at course context
foreach($roles as $role) {
if (!user_can_assign($context, $role->roleid)) {
print_error('cannotunassignrolefrom', '', '',
$role->roleid);
}
}
} else { // Unenrol yourself
require_capability('moodle/role:unassignself', $context, NULL, false);
}
if (!empty($USER->access['rsw'][$context->path])) {
print_error('cantunenrollinthisrole', '',
$CFG->wwwroot.'/course/view.php?id='.$course->id);
}
if ($confirm and confirm_sesskey()) {
if ($userid) {
if (! role_unassign(0, $userid, 0, $context->id)) {
print_error("unenrolerror");
}
add_to_log($course->id, 'course', 'unenrol',
"view.php?id=$course->id", $course->id);
redirect($CFG->wwwroot.'/user/index.php?id='.$course->id);
} else {
if (! role_unassign(0, $USER->id, 0, $context->id)) {
print_error("unenrolerror");
}
// force a refresh of mycourses
unset($USER->mycourses);
add_to_log($course->id, 'course', 'unenrol',
"view.php?id=$course->id", $course->id);
redirect($CFG->wwwroot);
}
}
$strunenrol = get_string('unenrol');
$navlinks = array();
$navlinks[] = array('name' => $strunenrol, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header("$course->shortname: $strunenrol", $course->fullname, $navigation);
if ($userid) {
if (!$user = $DB->get_record('user', array('id'=>$userid))) {
print_error('nousers');
}
$strunenrolsure = get_string('unenrolsure', '', fullname($user, true));
notice_yesno($strunenrolsure, "unenrol.php?id=$id&user=$user->id&confirm=yes&sesskey=".sesskey(),
$_SERVER['HTTP_REFERER']);
} else {
$strunenrolsure = get_string('unenrolsure', '', get_string("yourself"));
notice_yesno($strunenrolsure, "unenrol.php?id=$id&confirm=yes&sesskey=".sesskey(),
$_SERVER['HTTP_REFERER']);
}
print_footer($course);
?>