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('role', 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', 'role'=>$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 (
179 'firstname' => get_string('firstname'),
180 'lastname' => get_string('lastname'),
182 $extrafields = get_extra_user_fields($context);
183 foreach ($extrafields as $field) {
184 $userdetails[$field] = get_user_field_name($field);
188 'userdetails' => $userdetails,
189 'lastseen' => get_string('lastaccess'),
190 'role' => get_string('roles', 'role'),
191 'group' => get_string('groups', 'group'),
192 'enrol' => get_string('enrolmentinstances', 'enrol')
195 // Remove hidden fields if the user has no access
196 if (!has_capability('moodle/course:viewhiddenuserfields', $context)) {
197 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields
));
198 if (isset($hiddenfields['lastaccess'])) {
199 unset($fields['lastseen']);
201 if (isset($hiddenfields['groups'])) {
202 unset($fields['group']);
206 $filterform = new enrol_users_filter_form('users.php', array('manager' => $manager, 'id' => $id),
207 'get', '', array('id' => 'filterform'));
208 $filterform->set_data(array('search' => $search, 'ifilter' => $filter, 'role' => $role));
210 $table->set_fields($fields, $renderer);
212 $canassign = has_capability('moodle/role:assign', $manager->get_context());
213 $users = $manager->get_users_for_display($manager, $table->sort
, $table->sortdirection
, $table->page
, $table->perpage
);
214 foreach ($users as $userid=>&$user) {
215 $user['picture'] = $OUTPUT->render($user['picture']);
216 $user['role'] = $renderer->user_roles_and_actions($userid, $user['roles'], $manager->get_assignable_roles(), $canassign, $PAGE->url
);
217 $user['group'] = $renderer->user_groups_and_actions($userid, $user['groups'], $manager->get_all_groups(), has_capability('moodle/course:managegroups', $manager->get_context()), $PAGE->url
);
218 $user['enrol'] = $renderer->user_enrolments_and_actions($user['enrolments']);
220 $table->set_total_users($manager->get_total_users());
221 $table->set_users($users);
223 $PAGE->set_title($PAGE->course
->fullname
.': '.get_string('totalenrolledusers', 'enrol', $manager->get_total_users()));
224 $PAGE->set_heading($PAGE->title
);
226 echo $OUTPUT->header();
227 echo $OUTPUT->heading(get_string('enrolledusers', 'enrol'));
228 echo $renderer->render_course_enrolment_users_table($table, $filterform);
229 echo $OUTPUT->footer();