forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.php
217 lines (196 loc) · 9.16 KB
/
users.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Main course enrolment management UI, this is not compatible with frontpage course.
*
* @package core
* @subpackage enrol
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../config.php');
require_once("$CFG->dirroot/enrol/locallib.php");
require_once("$CFG->dirroot/enrol/users_forms.php");
require_once("$CFG->dirroot/enrol/renderer.php");
require_once("$CFG->dirroot/group/lib.php");
$id = required_param('id', PARAM_INT); // course id
$action = optional_param('action', '', PARAM_ACTION);
$filter = optional_param('ifilter', 0, PARAM_INT);
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
if ($course->id == SITEID) {
redirect(new moodle_url('/'));
}
require_login($course);
require_capability('moodle/course:enrolreview', $context);
$PAGE->set_pagelayout('admin');
$manager = new course_enrolment_manager($PAGE, $course, $filter);
$table = new course_enrolment_users_table($manager, $PAGE);
$PAGE->set_url('/enrol/users.php', $manager->get_url_params()+$table->get_url_params());
navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id' => $id)));
// Check if there is an action to take
if ($action) {
// Check if the page is confirmed (and sesskey is correct)
$confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey();
$actiontaken = false;
$pagetitle = '';
$pageheading = '';
$mform = null;
$pagecontent = null;
switch ($action) {
/**
* Removes a role from the user with this course
*/
case 'unassign':
if (has_capability('moodle/role:assign', $manager->get_context())) {
$role = required_param('role', PARAM_INT);
$user = required_param('user', PARAM_INT);
if ($confirm && $manager->unassign_role_from_user($user, $role)) {
redirect($PAGE->url);
} else {
$user = $DB->get_record('user', array('id'=>$user), '*', MUST_EXIST);
$allroles = $manager->get_all_roles();
$role = $allroles[$role];
$yesurl = new moodle_url($PAGE->url, array('action'=>'unassign', 'role'=>$role->id, 'user'=>$user->id, 'confirm'=>1, 'sesskey'=>sesskey()));
$message = get_string('unassignconfirm', 'role', array('user'=>fullname($user, true), 'role'=>$role->localname));
$pagetitle = get_string('unassignarole', 'role', $role->localname);
$pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url);
}
$actiontaken = true;
}
break;
/**
* Assigns a new role to a user enrolled within this course.
* A user must be enrolled in the course in order for this script to action
*/
case 'assign':
$user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
if (is_enrolled($context, $user) && has_capability('moodle/role:assign', $manager->get_context())) {
$mform = new enrol_users_assign_form(NULL, array('user'=>$user, 'course'=>$course, 'assignable'=>$manager->get_assignable_roles()));
$mform->set_data($PAGE->url->params());
$data = $mform->get_data();
if ($mform->is_cancelled() || ($data && array_key_exists($data->roleid, $manager->get_assignable_roles()) && $manager->assign_role_to_user($data->roleid, $user->id))) {
redirect($PAGE->url);
} else {
$pagetitle = get_string('assignroles', 'role');
}
$actiontaken = true;
}
break;
/**
* Removes the user from the given group
*/
case 'removemember':
if (has_capability('moodle/course:managegroups', $manager->get_context())) {
$groupid = required_param('group', PARAM_INT);
$userid = required_param('user', PARAM_INT);
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
if ($confirm && $manager->remove_user_from_group($user, $groupid)) {
redirect($PAGE->url);
} else {
$group = $manager->get_group($groupid);
if (!$group) {
break;
}
$yesurl = new moodle_url($PAGE->url, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey()));
$message = get_string('removefromgroupconfirm', 'group', array('user'=>fullname($user, true), 'group'=>$group->name));
$pagetitle = get_string('removefromgroup', 'group', $group->name);
$pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url);
}
$actiontaken = true;
}
break;
/**
* Makes the user a member of a given group
*/
case 'addmember':
if (has_capability('moodle/course:managegroups', $manager->get_context())) {
$userid = required_param('user', PARAM_INT);
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
$mform = new enrol_users_addmember_form(NULL, array('user'=>$user, 'course'=>$course, 'allgroups'=>$manager->get_all_groups()));
$mform->set_data($PAGE->url->params());
$data = $mform->get_data();
if ($mform->is_cancelled() || ($data && $manager->add_user_to_group($user, $data->groupid))) {
redirect($PAGE->url);
} else {
$pagetitle = get_string('addgroup', 'group');
}
$actiontaken = true;
}
break;
}
// If we took an action display we need to display something special.
if ($actiontaken) {
if (empty($pageheading)) {
$pageheading = $pagetitle;
}
$PAGE->set_title($pagetitle);
$PAGE->set_heading($pageheading);
echo $OUTPUT->header();
echo $OUTPUT->heading(fullname($user));
if (!is_null($mform)) {
$mform->display();
} else {
echo $pagecontent;
}
echo $OUTPUT->footer();
exit;
}
}
$renderer = $PAGE->get_renderer('core_enrol');
$userdetails = array (
'picture' => false,
'firstname' => get_string('firstname'),
'lastname' => get_string('lastname'),
);
$extrafields = get_extra_user_fields($context);
foreach ($extrafields as $field) {
$userdetails[$field] = get_user_field_name($field);
}
$fields = array(
'userdetails' => $userdetails,
'lastseen' => get_string('lastaccess'),
'role' => get_string('roles', 'role'),
'group' => get_string('groups', 'group'),
'enrol' => get_string('enrolmentinstances', 'enrol')
);
// Remove hidden fields if the user has no access
if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
$hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
if (isset($hiddenfields['lastaccess'])) {
unset($fields['lastseen']);
}
if (isset($hiddenfields['groups'])) {
unset($fields['group']);
}
}
$table->set_fields($fields, $renderer);
$canassign = has_capability('moodle/role:assign', $manager->get_context());
$users = $manager->get_users_for_display($manager, $table->sort, $table->sortdirection, $table->page, $table->perpage);
foreach ($users as $userid=>&$user) {
$user['picture'] = $OUTPUT->render($user['picture']);
$user['role'] = $renderer->user_roles_and_actions($userid, $user['roles'], $manager->get_assignable_roles(), $canassign, $PAGE->url);
$user['group'] = $renderer->user_groups_and_actions($userid, $user['groups'], $manager->get_all_groups(), has_capability('moodle/course:managegroups', $manager->get_context()), $PAGE->url);
$user['enrol'] = $renderer->user_enrolments_and_actions($user['enrolments']);;
}
$table->set_total_users($manager->get_total_users());
$table->set_users($users);
$PAGE->set_title($PAGE->course->fullname.': '.get_string('totalenrolledusers', 'enrol', $manager->get_total_users()));
$PAGE->set_heading($PAGE->title);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('enrolledusers', 'enrol'));
echo $renderer->render($table);
echo $OUTPUT->footer();