MDL-51167 tool_monitor: fix too strict condition & minor cleanup
[moodle.git] / enrol / users.php
blob403121d876b2e513b2b90e47fdafb3287b79180b
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.
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/>.
17 /**
18 * Main course enrolment management UI, this is not compatible with frontpage course.
20 * @package core_enrol
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require('../config.php');
26 require_once("$CFG->dirroot/enrol/locallib.php");
27 require_once("$CFG->dirroot/enrol/users_forms.php");
28 require_once("$CFG->dirroot/enrol/renderer.php");
29 require_once("$CFG->dirroot/group/lib.php");
31 $id = required_param('id', PARAM_INT); // course id
32 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
33 $filter = optional_param('ifilter', 0, PARAM_INT);
34 $search = optional_param('search', '', PARAM_RAW);
35 $role = optional_param('role', 0, PARAM_INT);
36 $fgroup = optional_param('filtergroup', 0, PARAM_INT);
37 $status = optional_param('status', -1, PARAM_INT);
38 $newcourse = optional_param('newcourse', false, PARAM_BOOL);
40 // When users reset the form, redirect back to first page without other params.
41 if (optional_param('resetbutton', '', PARAM_RAW) !== '') {
42 redirect('users.php?id=' . $id . '&newcourse=' . $newcourse);
45 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
46 $context = context_course::instance($course->id, MUST_EXIST);
48 if ($course->id == SITEID) {
49 redirect(new moodle_url('/'));
52 require_login($course);
53 require_capability('moodle/course:enrolreview', $context);
54 $PAGE->set_pagelayout('admin');
56 $manager = new course_enrolment_manager($PAGE, $course, $filter, $role, $search, $fgroup, $status);
57 $table = new course_enrolment_users_table($manager, $PAGE);
58 $PAGE->set_url('/enrol/users.php', $manager->get_url_params()+$table->get_url_params()+array('newcourse' => $newcourse));
59 navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id' => $id)));
61 // Check if there is an action to take
62 if ($action) {
64 // Check if the page is confirmed (and sesskey is correct)
65 $confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey();
67 $actiontaken = false;
68 $pagetitle = '';
69 $pageheading = '';
70 $mform = null;
71 $pagecontent = null;
73 switch ($action) {
74 /**
75 * Removes a role from the user with this course
77 case 'unassign':
78 if (has_capability('moodle/role:assign', $manager->get_context())) {
79 $role = required_param('roleid', PARAM_INT);
80 $user = required_param('user', PARAM_INT);
81 if ($confirm && $manager->unassign_role_from_user($user, $role)) {
82 redirect($PAGE->url);
83 } else {
84 $user = $DB->get_record('user', array('id'=>$user), '*', MUST_EXIST);
85 $allroles = $manager->get_all_roles();
86 $role = $allroles[$role];
87 $yesurl = new moodle_url($PAGE->url, array('action'=>'unassign', 'roleid'=>$role->id, 'user'=>$user->id, 'confirm'=>1, 'sesskey'=>sesskey()));
88 $message = get_string('unassignconfirm', 'role', array('user'=>fullname($user, true), 'role'=>$role->localname));
89 $pagetitle = get_string('unassignarole', 'role', $role->localname);
90 $pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url);
92 $actiontaken = true;
94 break;
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
99 case 'assign':
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()));
103 $mform->set_data($PAGE->url->params());
104 $data = $mform->get_data();
105 if ($mform->is_cancelled() || ($data && array_key_exists($data->roleid, $manager->get_assignable_roles()) && $manager->assign_role_to_user($data->roleid, $user->id))) {
106 redirect($PAGE->url);
107 } else {
108 $pagetitle = get_string('assignroles', 'role');
110 $actiontaken = true;
112 break;
114 * Removes the user from the given group
116 case 'removemember':
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)) {
122 redirect($PAGE->url);
123 } else {
124 $group = $manager->get_group($groupid);
125 if (!$group) {
126 break;
128 $yesurl = new moodle_url($PAGE->url, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey()));
129 $message = get_string('removefromgroupconfirm', 'group', array('user'=>fullname($user, true), 'group'=>$group->name));
130 $pagetitle = get_string('removefromgroup', 'group', $group->name);
131 $pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url);
133 $actiontaken = true;
135 break;
137 * Makes the user a member of a given group
139 case 'addmember':
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);
144 $mform = new enrol_users_addmember_form(NULL, array('user'=>$user, 'course'=>$course, 'allgroups'=>$manager->get_all_groups()));
145 $mform->set_data($PAGE->url->params());
146 $data = $mform->get_data();
147 if ($mform->is_cancelled()) {
148 redirect($PAGE->url);
149 } if (!empty($data->groupids)) {
150 foreach ($data->groupids as $groupid) {
151 $manager->add_user_to_group($user, $groupid);
153 redirect($PAGE->url);
154 } else {
155 $pagetitle = get_string('addgroup', 'group');
157 $actiontaken = true;
159 break;
162 // If we took an action display we need to display something special.
163 if ($actiontaken) {
164 if (empty($pageheading)) {
165 $pageheading = $pagetitle;
167 $PAGE->set_title($pagetitle);
168 $PAGE->set_heading($pageheading);
169 echo $OUTPUT->header();
170 echo $OUTPUT->heading(fullname($user));
171 if (!is_null($mform)) {
172 $mform->display();
173 } else {
174 echo $pagecontent;
176 echo $OUTPUT->footer();
177 exit;
182 $renderer = $PAGE->get_renderer('core_enrol');
183 $userdetails = array('picture' => false, 'userfullnamedisplay' => false);
184 // Get all the user names in a reasonable default order.
185 $allusernames = get_all_user_name_fields(false, null, null, null, true);
186 // Initialise the variable for the user's names in the table header.
187 $usernameheader = null;
188 // Get the alternative full name format for users with the viewfullnames capability.
189 $fullusernames = $CFG->alternativefullnameformat;
190 // If fullusernames is empty or accidentally set to language then fall back to default of just first and last name.
191 if ($fullusernames == 'language' || empty($fullusernames)) {
192 // Set $a variables to return 'firstname' and 'lastname'.
193 $a = new stdClass();
194 $a->firstname = 'firstname';
195 $a->lastname = 'lastname';
196 // Getting the fullname display will ensure that the order in the language file is maintained.
197 $usernameheader = explode(' ', get_string('fullnamedisplay', null, $a));
198 } else {
199 // If everything is as expected then put them in the order specified by the alternative full name format setting.
200 $usernameheader = order_in_string($allusernames, $fullusernames);
203 // Loop through each name and return the language string.
204 foreach ($usernameheader as $key => $username) {
205 $userdetails[$username] = get_string($username);
207 $extrafields = get_extra_user_fields($context);
208 foreach ($extrafields as $field) {
209 $userdetails[$field] = get_user_field_name($field);
212 $fields = array(
213 'userdetails' => $userdetails,
214 'lastcourseaccess' => get_string('lastcourseaccess'),
215 'role' => get_string('roles', 'role'),
216 'group' => get_string('groups', 'group'),
217 'enrol' => get_string('enrolmentinstances', 'enrol')
220 // Remove hidden fields if the user has no access
221 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
222 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
223 if (isset($hiddenfields['lastaccess'])) {
224 unset($fields['lastcourseaccess']);
226 if (isset($hiddenfields['groups'])) {
227 unset($fields['group']);
231 $filterform = new enrol_users_filter_form('users.php', array('manager' => $manager, 'id' => $id, 'newcourse' => $newcourse),
232 'get', '', array('id' => 'filterform'));
233 $filterform->set_data(array('search' => $search, 'ifilter' => $filter, 'role' => $role,
234 'filtergroup' => $fgroup, 'status' => $status));
236 $table->set_fields($fields, $renderer);
238 $canassign = has_capability('moodle/role:assign', $manager->get_context());
239 $users = $manager->get_users_for_display($manager, $table->sort, $table->sortdirection, $table->page, $table->perpage);
240 foreach ($users as $userid=>&$user) {
241 $user['picture'] = $OUTPUT->render($user['picture']);
242 $user['role'] = $renderer->user_roles_and_actions($userid, $user['roles'], $manager->get_assignable_roles(), $canassign, $PAGE->url);
243 $user['group'] = $renderer->user_groups_and_actions($userid, $user['groups'], $manager->get_all_groups(), has_capability('moodle/course:managegroups', $manager->get_context()), $PAGE->url);
244 $user['enrol'] = $renderer->user_enrolments_and_actions($user['enrolments']);
246 $table->set_total_users($manager->get_total_users());
247 $table->set_users($users);
249 $PAGE->set_title($PAGE->course->fullname.': '.get_string('totalenrolledusers', 'enrol', $manager->get_total_users()));
250 $PAGE->set_heading($PAGE->title);
252 echo $OUTPUT->header();
253 echo $OUTPUT->heading(get_string('enrolledusers', 'enrol'));
254 echo $renderer->render_course_enrolment_users_table($table, $filterform);
255 if ($newcourse == 1) {
256 echo $OUTPUT->single_button(new moodle_url('/course/view.php', array('id' => $id)),
257 get_string('proceedtocourse', 'enrol'), 'GET', array('class' => 'enrol-users-page-action'));
259 echo $OUTPUT->footer();