MDL-21437 data - checkbox and multimenu fields search working + require all too
[moodle.git] / enrol / users.php
CommitLineData
df997f84
PS
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.
13//
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/>.
16
17/**
18 * Main course enrolment management UI, this is not compatible with frontpage course.
19 *
5379726a
PS
20 * @package core
21 * @subpackage enrol
df997f84
PS
22 * @copyright 2010 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require('../config.php');
a70eb30f 27require_once("$CFG->dirroot/enrol/locallib.php");
df997f84 28require_once("$CFG->dirroot/enrol/users_forms.php");
a70eb30f 29require_once("$CFG->dirroot/enrol/renderer.php");
df997f84
PS
30require_once("$CFG->dirroot/group/lib.php");
31
32$id = required_param('id', PARAM_INT); // course id
33$action = optional_param('action', '', PARAM_ACTION);
a70eb30f 34$filter = optional_param('ifilter', 0, PARAM_INT);
df997f84 35
a70eb30f 36$PAGE->set_url(new moodle_url('/enrol/users.php', array('id'=>$id)));
df997f84
PS
37
38$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
39$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
40
df997f84 41if ($course->id == SITEID) {
a70eb30f 42 redirect(new moodle_url('/'));
df997f84
PS
43}
44
a70eb30f
SH
45require_login($course);
46require_capability('moodle/course:enrolreview', $context);
df997f84
PS
47$PAGE->set_pagelayout('admin');
48
a70eb30f 49$manager = new course_enrolment_manager($course, $filter);
6db3eee0 50$table = new course_enrolment_users_table($manager, $PAGE->url);
a70eb30f 51$pageurl = new moodle_url($PAGE->url, $manager->get_url_params()+$table->get_url_params());
df997f84 52
a70eb30f
SH
53// Check if there is an action to take
54if ($action) {
df997f84 55
a70eb30f
SH
56 // Check if the page is confirmed (and sesskey is correct)
57 $confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey();
58
59 $actiontaken = false;
60 $pagetitle = '';
61 $pageheading = '';
62 $mform = null;
63 $pagecontent = null;
df997f84 64
df997f84 65 switch ($action) {
a3c71984
SH
66 /**
67 * Unenrols a user from this course
68 */
df997f84 69 case 'unenrol':
a70eb30f 70 $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
a3c71984
SH
71 list ($instance, $plugin) = $manager->get_user_enrolment_components($ue);
72 if ($instance && $plugin && $plugin->allow_unenrol($instance) && has_capability("enrol/$instance->enrol:unenrol", $manager->get_context())) {
73 if ($confirm && $manager->unenrol_user($ue)) {
74 redirect($pageurl);
75 } else {
76 $user = $DB->get_record('user', array('id'=>$ue->userid), '*', MUST_EXIST);
77 $yesurl = new moodle_url($pageurl, array('action'=>'unenrol', 'ue'=>$ue->id, 'confirm'=>1, 'sesskey'=>sesskey()));
78 $message = get_string('unenrolconfirm', 'enrol', array('user'=>fullname($user, true), 'course'=>format_string($course->fullname)));
79 $pagetitle = get_string('unenrol', 'enrol');
80 $pagecontent = $OUTPUT->confirm($message, $yesurl, $pageurl);
81 }
82 $actiontaken = true;
df997f84
PS
83 }
84 break;
a3c71984
SH
85 /**
86 * Removes a role from the user with this course
87 */
df997f84 88 case 'unassign':
a3c71984
SH
89 if (has_capability('moodle/role:assign', $manager->get_context())) {
90 $role = required_param('role', PARAM_INT);
91 $user = required_param('user', PARAM_INT);
92 if ($confirm && $manager->unassign_role_from_user($user, $role)) {
93 redirect($pageurl);
94 } else {
95 $user = $DB->get_record('user', array('id'=>$user), '*', MUST_EXIST);
96 $allroles = $manager->get_all_roles();
97 $role = $allroles[$role];
98 $yesurl = new moodle_url($pageurl, array('action'=>'unassign', 'role'=>$role->id, 'user'=>$user->id, 'confirm'=>1, 'sesskey'=>sesskey()));
99 $message = get_string('unassignconfirm', 'role', array('user'=>fullname($user, true), 'role'=>$role->localname));
100 $pagetitle = get_string('unassignarole', 'role', $role->localname);
101 $pagecontent = $OUTPUT->confirm($message, $yesurl, $pageurl);
102 }
103 $actiontaken = true;
df997f84
PS
104 }
105 break;
a3c71984
SH
106 /**
107 * Assigns a new role to a user enrolled within this course.
108 * A user must be enrolled in the course in order for this script to action
109 */
df997f84 110 case 'assign':
a3c71984
SH
111 $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
112 if (is_enrolled($context, $user) && has_capability('moodle/role:assign', $manager->get_context())) {
113 $mform = new enrol_users_assign_form(NULL, array('user'=>$user, 'course'=>$course, 'assignable'=>$manager->get_assignable_roles()));
114 $mform->set_data($pageurl->params());
115 $data = $mform->get_data();
823a807e 116 if ($mform->is_cancelled() || ($data && array_key_exists($data->roleid, $manager->get_assignable_roles()) && $manager->assign_role_to_user($data->roleid, $user->id))) {
a3c71984
SH
117 redirect($pageurl);
118 } else {
119 $pagetitle = get_string('assignroles', 'role');
120 }
121 $actiontaken = true;
df997f84 122 }
a70eb30f 123 break;
a3c71984
SH
124 /**
125 * Removes the user from the given group
126 */
df997f84 127 case 'removemember':
a3c71984
SH
128 if (has_capability('moodle/course:managegroups', $manager->get_context())) {
129 $groupid = required_param('group', PARAM_INT);
130 $userid = required_param('user', PARAM_INT);
131 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
132 if ($confirm && $manager->remove_user_from_group($user, $groupid)) {
133 redirect($pageurl);
134 } else {
135 $group = $manager->get_group($groupid);
136 if (!$group) {
137 break;
138 }
139 $yesurl = new moodle_url($pageurl, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey()));
140 $message = get_string('removefromgroupconfirm', 'group', array('user'=>fullname($user, true), 'group'=>$group->name));
141 $pagetitle = get_string('removefromgroup', 'group', $group->name);
142 $pagecontent = $OUTPUT->confirm($message, $yesurl, $pageurl);
a70eb30f 143 }
a3c71984 144 $actiontaken = true;
df997f84
PS
145 }
146 break;
a3c71984
SH
147 /**
148 * Makes the user a member of a given group
149 */
df997f84 150 case 'addmember':
a3c71984
SH
151 if (has_capability('moodle/course:managegroups', $manager->get_context())) {
152 $userid = required_param('user', PARAM_INT);
153 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
154
155 $mform = new enrol_users_addmember_form(NULL, array('user'=>$user, 'course'=>$course, 'allgroups'=>$manager->get_all_groups()));
156 $mform->set_data($pageurl->params());
157 $data = $mform->get_data();
158 if ($mform->is_cancelled() || ($data && $manager->add_user_to_group($user, $data->groupid))) {
159 redirect($pageurl);
160 } else {
161 $pagetitle = get_string('addgroup', 'group');
162 }
163 $actiontaken = true;
df997f84 164 }
a70eb30f 165 break;
a3c71984
SH
166 /**
167 * Edits the details of a users enrolment in the course
168 */
df997f84 169 case 'edit':
a3c71984
SH
170 $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
171 list($instance, $plugin) = $manager->get_user_enrolment_components($ue);
172 if ($instance && $plugin && $plugin->allow_manage($instance) && has_capability("enrol/$instance->enrol:manage", $manager->get_context())) {
173 $user = $DB->get_record('user', array('id'=>$ue->userid), '*', MUST_EXIST);
174 $mform = new enrol_users_edit_form(NULL, array('user'=>$user, 'course'=>$course, 'ue'=>$ue));
175 $mform->set_data($pageurl->params());
176 $data = $mform->get_data();
177 if ($mform->is_cancelled() || ($data && $manager->edit_enrolment($ue, $data))) {
178 redirect($pageurl);
179 } else {
180 $pagetitle = fullname($user);
181 }
182 $actiontaken = true;
df997f84 183 }
a70eb30f 184 break;
df997f84 185 }
df997f84 186
a3c71984 187 // If we took an action display we need to display something special.
a70eb30f
SH
188 if ($actiontaken) {
189 if (empty($pageheading)) {
190 $pageheading = $pagetitle;
df997f84 191 }
a70eb30f
SH
192 $PAGE->set_title($pagetitle);
193 $PAGE->set_heading($pageheading);
194 echo $OUTPUT->header();
195 echo $OUTPUT->heading(fullname($user));
196 if (!is_null($mform)) {
197 $mform->display();
df997f84 198 } else {
a70eb30f 199 echo $pagecontent;
df997f84 200 }
a70eb30f
SH
201 echo $OUTPUT->footer();
202 exit;
df997f84 203 }
df997f84
PS
204}
205
df997f84 206
9fe92ae8 207$renderer = $PAGE->get_renderer('core_enrol');
a70eb30f
SH
208$fields = array(
209 'userdetails' => array (
210 'picture' => false,
211 'firstname' => get_string('firstname'),
212 'lastname' => get_string('lastname'),
213 'email' => get_string('email')
214 ),
215 'lastseen' => get_string('lastaccess'),
216 'role' => get_string('roles', 'role'),
217 'group' => get_string('groups', 'group'),
218 'enrol' => get_string('enrolmentinstances', 'enrol')
219);
9fe92ae8 220$table->set_fields($fields, $renderer);
a70eb30f 221
a70eb30f
SH
222$canassign = has_capability('moodle/role:assign', $manager->get_context());
223$users = $manager->get_users_for_display($renderer, $pageurl, $table->sort, $table->sortdirection, $table->page, $table->perpage);
224foreach ($users as $userid=>&$user) {
225 $user['picture'] = $OUTPUT->render($user['picture']);
226 $user['role'] = $renderer->user_roles_and_actions($userid, $user['roles'], $manager->get_assignable_roles(), $canassign, $pageurl);
227 $user['group'] = $renderer->user_groups_and_actions($userid, $user['groups'], $manager->get_all_groups(), has_capability('moodle/course:managegroups', $manager->get_context()), $pageurl);
228 $user['enrol'] = $renderer->user_enrolments_and_actions($userid, $user['enrolments'], $pageurl);
229}
6db3eee0 230$table->set_total_users($manager->get_total_users());
a70eb30f 231$table->set_users($users);
df997f84 232
2c770b7a
SH
233$PAGE->set_title($PAGE->course->fullname.': '.get_string('totalenrolledusers', 'enrol', $manager->get_total_users()));
234$PAGE->set_heading($PAGE->title);
df997f84 235
a70eb30f
SH
236echo $OUTPUT->header();
237echo $OUTPUT->heading(get_string('enrolledusers', 'enrol'));
238echo $renderer->render($table);
23cee7a4 239echo $OUTPUT->footer();