MDL-49695 tool_behat: fixed URL
[moodle.git] / enrol / users.php
blobac2b68d1aab7485805e3e8242cb46f6faaa5d391
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() || ($data && $manager->add_user_to_group($user, $data->groupid))) {
148 redirect($PAGE->url);
149 } else {
150 $pagetitle = get_string('addgroup', 'group');
152 $actiontaken = true;
154 break;
157 // If we took an action display we need to display something special.
158 if ($actiontaken) {
159 if (empty($pageheading)) {
160 $pageheading = $pagetitle;
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();
168 } else {
169 echo $pagecontent;
171 echo $OUTPUT->footer();
172 exit;
177 $renderer = $PAGE->get_renderer('core_enrol');
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);
193 // Loop through each name and return the language string.
194 foreach ($usernameheader as $key => $username) {
195 $userdetails[$username] = get_string($username);
197 $extrafields = get_extra_user_fields($context);
198 foreach ($extrafields as $field) {
199 $userdetails[$field] = get_user_field_name($field);
202 $fields = array(
203 'userdetails' => $userdetails,
204 'lastcourseaccess' => get_string('lastcourseaccess'),
205 'role' => get_string('roles', 'role'),
206 'group' => get_string('groups', 'group'),
207 'enrol' => get_string('enrolmentinstances', 'enrol')
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'])) {
214 unset($fields['lastcourseaccess']);
216 if (isset($hiddenfields['groups'])) {
217 unset($fields['group']);
221 $filterform = new enrol_users_filter_form('users.php', array('manager' => $manager, 'id' => $id, 'newcourse' => $newcourse),
222 'get', '', array('id' => 'filterform'));
223 $filterform->set_data(array('search' => $search, 'ifilter' => $filter, 'role' => $role, 'filtergroup' => $fgroup));
225 $table->set_fields($fields, $renderer);
227 $canassign = has_capability('moodle/role:assign', $manager->get_context());
228 $users = $manager->get_users_for_display($manager, $table->sort, $table->sortdirection, $table->page, $table->perpage);
229 foreach ($users as $userid=>&$user) {
230 $user['picture'] = $OUTPUT->render($user['picture']);
231 $user['role'] = $renderer->user_roles_and_actions($userid, $user['roles'], $manager->get_assignable_roles(), $canassign, $PAGE->url);
232 $user['group'] = $renderer->user_groups_and_actions($userid, $user['groups'], $manager->get_all_groups(), has_capability('moodle/course:managegroups', $manager->get_context()), $PAGE->url);
233 $user['enrol'] = $renderer->user_enrolments_and_actions($user['enrolments']);
235 $table->set_total_users($manager->get_total_users());
236 $table->set_users($users);
238 $PAGE->set_title($PAGE->course->fullname.': '.get_string('totalenrolledusers', 'enrol', $manager->get_total_users()));
239 $PAGE->set_heading($PAGE->title);
241 echo $OUTPUT->header();
242 echo $OUTPUT->heading(get_string('enrolledusers', 'enrol'));
243 echo $renderer->render_course_enrolment_users_table($table, $filterform);
244 if ($newcourse == 1) {
245 echo $OUTPUT->single_button(new moodle_url('/course/view.php', array('id' => $id)),
246 get_string('proceedtocourse', 'enrol'), 'GET', array('class' => 'enrol-users-page-action'));
248 echo $OUTPUT->footer();