MDL-40059 glossary: event course_module_instance_list_viewed
[moodle.git] / enrol / users.php
blobb82ebe4bad5750081d045f17caeaeb4bcd8e5521
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);
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
61 if ($action) {
63 // Check if the page is confirmed (and sesskey is correct)
64 $confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey();
66 $actiontaken = false;
67 $pagetitle = '';
68 $pageheading = '';
69 $mform = null;
70 $pagecontent = null;
72 switch ($action) {
73 /**
74 * Removes a role from the user with this course
76 case 'unassign':
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)) {
81 redirect($PAGE->url);
82 } else {
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);
91 $actiontaken = true;
93 break;
94 /**
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
98 case 'assign':
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);
106 } else {
107 $pagetitle = get_string('assignroles', 'role');
109 $actiontaken = true;
111 break;
113 * Removes the user from the given group
115 case 'removemember':
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);
122 } else {
123 $group = $manager->get_group($groupid);
124 if (!$group) {
125 break;
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);
132 $actiontaken = true;
134 break;
136 * Makes the user a member of a given group
138 case 'addmember':
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);
148 } else {
149 $pagetitle = get_string('addgroup', 'group');
151 $actiontaken = true;
153 break;
156 // If we took an action display we need to display something special.
157 if ($actiontaken) {
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)) {
166 $mform->display();
167 } else {
168 echo $pagecontent;
170 echo $OUTPUT->footer();
171 exit;
176 $renderer = $PAGE->get_renderer('core_enrol');
177 $userdetails = array (
178 'picture' => false,
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);
187 $fields = array(
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();