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