2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Main course enrolment management UI, this is not compatible with frontpage course.
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
);
39 // When users reset the form, redirect back to first page without other params.
40 if (optional_param('resetbutton', '', PARAM_RAW
) !== '') {
41 redirect('users.php?id=' . $id);
44 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST
);
45 $context = context_course
::instance($course->id
, MUST_EXIST
);
47 if ($course->id
== SITEID
) {
48 redirect(new moodle_url('/'));
51 require_login($course);
52 require_capability('moodle/course:enrolreview', $context);
53 $PAGE->set_pagelayout('admin');
55 $manager = new course_enrolment_manager($PAGE, $course, $filter, $role, $search, $fgroup, $status);
56 $table = new course_enrolment_users_table($manager, $PAGE);
57 $PAGE->set_url('/enrol/users.php', $manager->get_url_params()+
$table->get_url_params());
58 navigation_node
::override_active_url(new moodle_url('/enrol/users.php', array('id' => $id)));
60 // Check if there is an action to take
63 // Check if the page is confirmed (and sesskey is correct)
64 $confirm = optional_param('confirm', false, PARAM_BOOL
) && confirm_sesskey();
74 * Removes a role from the user with this course
77 if (has_capability('moodle/role:assign', $manager->get_context())) {
78 $role = required_param('roleid', PARAM_INT
);
79 $user = required_param('user', PARAM_INT
);
80 if ($confirm && $manager->unassign_role_from_user($user, $role)) {
83 $user = $DB->get_record('user', array('id'=>$user), '*', MUST_EXIST
);
84 $allroles = $manager->get_all_roles();
85 $role = $allroles[$role];
86 $yesurl = new moodle_url($PAGE->url
, array('action'=>'unassign', 'roleid'=>$role->id
, 'user'=>$user->id
, 'confirm'=>1, 'sesskey'=>sesskey()));
87 $message = get_string('unassignconfirm', 'role', array('user'=>fullname($user, true), 'role'=>$role->localname
));
88 $pagetitle = get_string('unassignarole', 'role', $role->localname
);
89 $pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url
);
95 * Assigns a new role to a user enrolled within this course.
96 * A user must be enrolled in the course in order for this script to action
99 $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT
)), '*', MUST_EXIST
);
100 if (is_enrolled($context, $user) && has_capability('moodle/role:assign', $manager->get_context())) {
101 $mform = new enrol_users_assign_form(NULL, array('user'=>$user, 'course'=>$course, 'assignable'=>$manager->get_assignable_roles()));
102 $mform->set_data($PAGE->url
->params());
103 $data = $mform->get_data();
104 if ($mform->is_cancelled() ||
($data && array_key_exists($data->roleid
, $manager->get_assignable_roles()) && $manager->assign_role_to_user($data->roleid
, $user->id
))) {
105 redirect($PAGE->url
);
107 $pagetitle = get_string('assignroles', 'role');
113 * Removes the user from the given group
116 if (has_capability('moodle/course:managegroups', $manager->get_context())) {
117 $groupid = required_param('group', PARAM_INT
);
118 $userid = required_param('user', PARAM_INT
);
119 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST
);
120 if ($confirm && $manager->remove_user_from_group($user, $groupid)) {
121 redirect($PAGE->url
);
123 $group = $manager->get_group($groupid);
127 $yesurl = new moodle_url($PAGE->url
, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey()));
128 $message = get_string('removefromgroupconfirm', 'group', array('user'=>fullname($user, true), 'group'=>$group->name
));
129 $pagetitle = get_string('removefromgroup', 'group', $group->name
);
130 $pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url
);
136 * Makes the user a member of a given group
139 if (has_capability('moodle/course:managegroups', $manager->get_context())) {
140 $userid = required_param('user', PARAM_INT
);
141 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST
);
143 $mform = new enrol_users_addmember_form(NULL, array('user'=>$user, 'course'=>$course, 'allgroups'=>$manager->get_all_groups()));
144 $mform->set_data($PAGE->url
->params());
145 $data = $mform->get_data();
146 if ($mform->is_cancelled() ||
($data && $manager->add_user_to_group($user, $data->groupid
))) {
147 redirect($PAGE->url
);
149 $pagetitle = get_string('addgroup', 'group');
156 // If we took an action display we need to display something special.
158 if (empty($pageheading)) {
159 $pageheading = $pagetitle;
161 $PAGE->set_title($pagetitle);
162 $PAGE->set_heading($pageheading);
163 echo $OUTPUT->header();
164 echo $OUTPUT->heading(fullname($user));
165 if (!is_null($mform)) {
170 echo $OUTPUT->footer();
176 $renderer = $PAGE->get_renderer('core_enrol');
177 $userdetails = array('picture' => false, 'userfullnamedisplay' => false);
178 // Get all the user names in a reasonable default order.
179 $allusernames = get_all_user_name_fields(false, null, null, null, true);
180 // Initialise the variable for the user's names in the table header.
181 $usernameheader = null;
182 // Get the alternative full name format for users with the viewfullnames capability.
183 $fullusernames = $CFG->alternativefullnameformat
;
184 // If fullusernames is empty or accidentally set to language then fall back to default of just first and last name.
185 if ($fullusernames == 'language' ||
empty($fullusernames)) {
186 // Set $a variables to return 'firstname' and 'lastname'.
188 $a->firstname
= 'firstname';
189 $a->lastname
= 'lastname';
190 // Getting the fullname display will ensure that the order in the language file is maintained.
191 $usernameheader = explode(' ', get_string('fullnamedisplay', null, $a));
193 // If everything is as expected then put them in the order specified by the alternative full name format setting.
194 $usernameheader = order_in_string($allusernames, $fullusernames);
197 // Loop through each name and return the language string.
198 foreach ($usernameheader as $key => $username) {
199 $userdetails[$username] = get_string($username);
201 $extrafields = get_extra_user_fields($context);
202 foreach ($extrafields as $field) {
203 $userdetails[$field] = get_user_field_name($field);
207 'userdetails' => $userdetails,
208 'lastcourseaccess' => get_string('lastcourseaccess'),
209 'role' => get_string('roles', 'role'),
210 'group' => get_string('groups', 'group'),
211 'enrol' => get_string('enrolmentinstances', 'enrol')
214 // Remove hidden fields if the user has no access
215 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
216 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields
));
217 if (isset($hiddenfields['lastaccess'])) {
218 unset($fields['lastcourseaccess']);
220 if (isset($hiddenfields['groups'])) {
221 unset($fields['group']);
225 $filterform = new enrol_users_filter_form('users.php', array('manager' => $manager, 'id' => $id),
226 'get', '', array('id' => 'filterform'));
227 $filterform->set_data(array('search' => $search, 'ifilter' => $filter, 'role' => $role,
228 'filtergroup' => $fgroup, 'status' => $status));
230 $table->set_fields($fields, $renderer);
232 $canassign = has_capability('moodle/role:assign', $manager->get_context());
233 $users = $manager->get_users_for_display($manager, $table->sort
, $table->sortdirection
, $table->page
, $table->perpage
);
234 foreach ($users as $userid=>&$user) {
235 $user['picture'] = $OUTPUT->render($user['picture']);
236 $user['role'] = $renderer->user_roles_and_actions($userid, $user['roles'], $manager->get_assignable_roles(), $canassign, $PAGE->url
);
237 $user['group'] = $renderer->user_groups_and_actions($userid, $user['groups'], $manager->get_all_groups(), has_capability('moodle/course:managegroups', $manager->get_context()), $PAGE->url
);
238 $user['enrol'] = $renderer->user_enrolments_and_actions($user['enrolments']);
240 $table->set_total_users($manager->get_total_users());
241 $table->set_users($users);
243 $PAGE->set_title($PAGE->course
->fullname
.': '.get_string('totalenrolledusers', 'enrol', $manager->get_total_users()));
244 $PAGE->set_heading($PAGE->title
);
246 echo $OUTPUT->header();
247 echo $OUTPUT->heading(get_string('enrolledusers', 'enrol'));
248 echo $renderer->render_course_enrolment_users_table($table, $filterform);
249 echo $OUTPUT->footer();