Commit | Line | Data |
---|---|---|
df997f84 PS |
1 | <?php |
2 | // This file is part of Moodle - http://moodle.org/ | |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
18 | * Main course enrolment management UI, this is not compatible with frontpage course. | |
19 | * | |
31ac2aef | 20 | * @package core_enrol |
df997f84 PS |
21 | * @copyright 2010 Petr Skoda {@link http://skodak.org} |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | require('../config.php'); | |
a70eb30f | 26 | require_once("$CFG->dirroot/enrol/locallib.php"); |
df997f84 | 27 | require_once("$CFG->dirroot/enrol/users_forms.php"); |
a70eb30f | 28 | require_once("$CFG->dirroot/enrol/renderer.php"); |
df997f84 PS |
29 | require_once("$CFG->dirroot/group/lib.php"); |
30 | ||
31 | $id = required_param('id', PARAM_INT); // course id | |
405aca35 | 32 | $action = optional_param('action', '', PARAM_ALPHANUMEXT); |
a70eb30f | 33 | $filter = optional_param('ifilter', 0, PARAM_INT); |
71638033 | 34 | $search = optional_param('search', '', PARAM_RAW); |
35 | $role = optional_param('role', 0, PARAM_INT); | |
d9734c91 | 36 | $fgroup = optional_param('filtergroup', 0, PARAM_INT); |
aa513214 | 37 | $status = optional_param('status', -1, PARAM_INT); |
bbc3b079 | 38 | $newcourse = optional_param('newcourse', false, PARAM_BOOL); |
71638033 | 39 | |
40 | // When users reset the form, redirect back to first page without other params. | |
41 | if (optional_param('resetbutton', '', PARAM_RAW) !== '') { | |
52a5d7a2 | 42 | redirect('users.php?id=' . $id . '&newcourse=' . $newcourse); |
71638033 | 43 | } |
df997f84 | 44 | |
74df2951 | 45 | $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); |
55bcef29 | 46 | $context = context_course::instance($course->id, MUST_EXIST); |
df997f84 | 47 | |
df997f84 | 48 | if ($course->id == SITEID) { |
a70eb30f | 49 | redirect(new moodle_url('/')); |
df997f84 PS |
50 | } |
51 | ||
a70eb30f SH |
52 | require_login($course); |
53 | require_capability('moodle/course:enrolreview', $context); | |
df997f84 PS |
54 | $PAGE->set_pagelayout('admin'); |
55 | ||
aa513214 | 56 | $manager = new course_enrolment_manager($PAGE, $course, $filter, $role, $search, $fgroup, $status); |
61c32b7b | 57 | $table = new course_enrolment_users_table($manager, $PAGE); |
bbc3b079 | 58 | $PAGE->set_url('/enrol/users.php', $manager->get_url_params()+$table->get_url_params()+array('newcourse' => $newcourse)); |
12a52d7c | 59 | navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id' => $id))); |
df997f84 | 60 | |
a70eb30f SH |
61 | // Check if there is an action to take |
62 | if ($action) { | |
df997f84 | 63 | |
a70eb30f SH |
64 | // Check if the page is confirmed (and sesskey is correct) |
65 | $confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey(); | |
66 | ||
67 | $actiontaken = false; | |
68 | $pagetitle = ''; | |
69 | $pageheading = ''; | |
70 | $mform = null; | |
71 | $pagecontent = null; | |
df997f84 | 72 | |
df997f84 | 73 | switch ($action) { |
a3c71984 SH |
74 | /** |
75 | * Removes a role from the user with this course | |
76 | */ | |
df997f84 | 77 | case 'unassign': |
a3c71984 | 78 | if (has_capability('moodle/role:assign', $manager->get_context())) { |
cc6a0dd0 | 79 | $role = required_param('roleid', PARAM_INT); |
a3c71984 SH |
80 | $user = required_param('user', PARAM_INT); |
81 | if ($confirm && $manager->unassign_role_from_user($user, $role)) { | |
61c32b7b | 82 | redirect($PAGE->url); |
a3c71984 SH |
83 | } else { |
84 | $user = $DB->get_record('user', array('id'=>$user), '*', MUST_EXIST); | |
85 | $allroles = $manager->get_all_roles(); | |
86 | $role = $allroles[$role]; | |
cc6a0dd0 | 87 | $yesurl = new moodle_url($PAGE->url, array('action'=>'unassign', 'roleid'=>$role->id, 'user'=>$user->id, 'confirm'=>1, 'sesskey'=>sesskey())); |
a3c71984 SH |
88 | $message = get_string('unassignconfirm', 'role', array('user'=>fullname($user, true), 'role'=>$role->localname)); |
89 | $pagetitle = get_string('unassignarole', 'role', $role->localname); | |
61c32b7b | 90 | $pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url); |
a3c71984 SH |
91 | } |
92 | $actiontaken = true; | |
df997f84 PS |
93 | } |
94 | break; | |
a3c71984 SH |
95 | /** |
96 | * Assigns a new role to a user enrolled within this course. | |
97 | * A user must be enrolled in the course in order for this script to action | |
98 | */ | |
df997f84 | 99 | case 'assign': |
a3c71984 SH |
100 | $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST); |
101 | if (is_enrolled($context, $user) && has_capability('moodle/role:assign', $manager->get_context())) { | |
102 | $mform = new enrol_users_assign_form(NULL, array('user'=>$user, 'course'=>$course, 'assignable'=>$manager->get_assignable_roles())); | |
61c32b7b | 103 | $mform->set_data($PAGE->url->params()); |
a3c71984 | 104 | $data = $mform->get_data(); |
823a807e | 105 | if ($mform->is_cancelled() || ($data && array_key_exists($data->roleid, $manager->get_assignable_roles()) && $manager->assign_role_to_user($data->roleid, $user->id))) { |
61c32b7b | 106 | redirect($PAGE->url); |
a3c71984 SH |
107 | } else { |
108 | $pagetitle = get_string('assignroles', 'role'); | |
109 | } | |
110 | $actiontaken = true; | |
df997f84 | 111 | } |
a70eb30f | 112 | break; |
a3c71984 SH |
113 | /** |
114 | * Removes the user from the given group | |
115 | */ | |
df997f84 | 116 | case 'removemember': |
a3c71984 SH |
117 | if (has_capability('moodle/course:managegroups', $manager->get_context())) { |
118 | $groupid = required_param('group', PARAM_INT); | |
119 | $userid = required_param('user', PARAM_INT); | |
120 | $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST); | |
121 | if ($confirm && $manager->remove_user_from_group($user, $groupid)) { | |
61c32b7b | 122 | redirect($PAGE->url); |
a3c71984 SH |
123 | } else { |
124 | $group = $manager->get_group($groupid); | |
125 | if (!$group) { | |
126 | break; | |
127 | } | |
61c32b7b | 128 | $yesurl = new moodle_url($PAGE->url, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey())); |
a3c71984 SH |
129 | $message = get_string('removefromgroupconfirm', 'group', array('user'=>fullname($user, true), 'group'=>$group->name)); |
130 | $pagetitle = get_string('removefromgroup', 'group', $group->name); | |
61c32b7b | 131 | $pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url); |
a70eb30f | 132 | } |
a3c71984 | 133 | $actiontaken = true; |
df997f84 PS |
134 | } |
135 | break; | |
a3c71984 SH |
136 | /** |
137 | * Makes the user a member of a given group | |
138 | */ | |
df997f84 | 139 | case 'addmember': |
a3c71984 SH |
140 | if (has_capability('moodle/course:managegroups', $manager->get_context())) { |
141 | $userid = required_param('user', PARAM_INT); | |
142 | $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST); | |
143 | ||
144 | $mform = new enrol_users_addmember_form(NULL, array('user'=>$user, 'course'=>$course, 'allgroups'=>$manager->get_all_groups())); | |
61c32b7b | 145 | $mform->set_data($PAGE->url->params()); |
a3c71984 SH |
146 | $data = $mform->get_data(); |
147 | if ($mform->is_cancelled() || ($data && $manager->add_user_to_group($user, $data->groupid))) { | |
61c32b7b | 148 | redirect($PAGE->url); |
a3c71984 SH |
149 | } else { |
150 | $pagetitle = get_string('addgroup', 'group'); | |
151 | } | |
152 | $actiontaken = true; | |
df997f84 | 153 | } |
a70eb30f | 154 | break; |
df997f84 | 155 | } |
df997f84 | 156 | |
a3c71984 | 157 | // If we took an action display we need to display something special. |
a70eb30f SH |
158 | if ($actiontaken) { |
159 | if (empty($pageheading)) { | |
160 | $pageheading = $pagetitle; | |
df997f84 | 161 | } |
a70eb30f SH |
162 | $PAGE->set_title($pagetitle); |
163 | $PAGE->set_heading($pageheading); | |
164 | echo $OUTPUT->header(); | |
165 | echo $OUTPUT->heading(fullname($user)); | |
166 | if (!is_null($mform)) { | |
167 | $mform->display(); | |
df997f84 | 168 | } else { |
a70eb30f | 169 | echo $pagecontent; |
df997f84 | 170 | } |
a70eb30f SH |
171 | echo $OUTPUT->footer(); |
172 | exit; | |
df997f84 | 173 | } |
df997f84 PS |
174 | } |
175 | ||
df997f84 | 176 | |
9fe92ae8 | 177 | $renderer = $PAGE->get_renderer('core_enrol'); |
95065006 AG |
178 | $userdetails = array('picture' => false); |
179 | // Get all the user names in a reasonable default order. | |
180 | $allusernames = get_all_user_name_fields(false, null, null, null, true); | |
181 | // Initialise the variable for the user's names in the table header. | |
182 | $usernameheader = null; | |
183 | // Get the alternative full name format for users with the viewfullnames capability. | |
184 | $fullusernames = $CFG->alternativefullnameformat; | |
185 | // If fullusernames is empty or accidentally set to language then fall back on the $allusernames set up. | |
186 | if ($fullusernames == 'language' || empty($fullusernames)) { | |
187 | $usernameheader = $allusernames; | |
188 | } else { | |
189 | // If everything is as expected then put them in the order specified by the alternative full name format setting. | |
190 | $usernameheader = order_in_string($allusernames, $fullusernames); | |
191 | } | |
192 | ||
193 | // Loop through each name and return the language string. | |
194 | foreach ($usernameheader as $key => $username) { | |
195 | $userdetails[$username] = get_string($username); | |
196 | } | |
b849c212 | 197 | $extrafields = get_extra_user_fields($context); |
198 | foreach ($extrafields as $field) { | |
199 | $userdetails[$field] = get_user_field_name($field); | |
200 | } | |
201 | ||
a70eb30f | 202 | $fields = array( |
b849c212 | 203 | 'userdetails' => $userdetails, |
e89682d8 | 204 | 'lastcourseaccess' => get_string('lastcourseaccess'), |
a70eb30f SH |
205 | 'role' => get_string('roles', 'role'), |
206 | 'group' => get_string('groups', 'group'), | |
207 | 'enrol' => get_string('enrolmentinstances', 'enrol') | |
208 | ); | |
ce13ea6c ARN |
209 | |
210 | // Remove hidden fields if the user has no access | |
211 | if (!has_capability('moodle/course:viewhiddenuserfields', $context)) { | |
212 | $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); | |
213 | if (isset($hiddenfields['lastaccess'])) { | |
e89682d8 | 214 | unset($fields['lastcourseaccess']); |
ce13ea6c ARN |
215 | } |
216 | if (isset($hiddenfields['groups'])) { | |
217 | unset($fields['group']); | |
218 | } | |
219 | } | |
220 | ||
52a5d7a2 | 221 | $filterform = new enrol_users_filter_form('users.php', array('manager' => $manager, 'id' => $id, 'newcourse' => $newcourse), |
71638033 | 222 | 'get', '', array('id' => 'filterform')); |
af4e80ae AM |
223 | $filterform->set_data(array('search' => $search, 'ifilter' => $filter, 'role' => $role, |
224 | 'filtergroup' => $fgroup, 'status' => $status)); | |
71638033 | 225 | |
9fe92ae8 | 226 | $table->set_fields($fields, $renderer); |
a70eb30f | 227 | |
a70eb30f | 228 | $canassign = has_capability('moodle/role:assign', $manager->get_context()); |
291215f4 | 229 | $users = $manager->get_users_for_display($manager, $table->sort, $table->sortdirection, $table->page, $table->perpage); |
a70eb30f SH |
230 | foreach ($users as $userid=>&$user) { |
231 | $user['picture'] = $OUTPUT->render($user['picture']); | |
61c32b7b PS |
232 | $user['role'] = $renderer->user_roles_and_actions($userid, $user['roles'], $manager->get_assignable_roles(), $canassign, $PAGE->url); |
233 | $user['group'] = $renderer->user_groups_and_actions($userid, $user['groups'], $manager->get_all_groups(), has_capability('moodle/course:managegroups', $manager->get_context()), $PAGE->url); | |
0e35ba6f | 234 | $user['enrol'] = $renderer->user_enrolments_and_actions($user['enrolments']); |
a70eb30f | 235 | } |
6db3eee0 | 236 | $table->set_total_users($manager->get_total_users()); |
a70eb30f | 237 | $table->set_users($users); |
df997f84 | 238 | |
2c770b7a SH |
239 | $PAGE->set_title($PAGE->course->fullname.': '.get_string('totalenrolledusers', 'enrol', $manager->get_total_users())); |
240 | $PAGE->set_heading($PAGE->title); | |
df997f84 | 241 | |
a70eb30f SH |
242 | echo $OUTPUT->header(); |
243 | echo $OUTPUT->heading(get_string('enrolledusers', 'enrol')); | |
71638033 | 244 | echo $renderer->render_course_enrolment_users_table($table, $filterform); |
bbc3b079 | 245 | if ($newcourse == 1) { |
246 | echo $OUTPUT->single_button(new moodle_url('/course/view.php', array('id' => $id)), | |
247 | get_string('proceedtocourse', 'enrol'), 'GET', array('class' => 'enrol-users-page-action')); | |
248 | } | |
ce13ea6c | 249 | echo $OUTPUT->footer(); |