Merge branch 'MDL-31841_feedback_align' of git://github.com/andyjdavis/moodle
[moodle.git] / enrol / users.php
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/>.
17 /**
18  * Main course enrolment management UI, this is not compatible with frontpage course.
19  *
20  * @package    core
21  * @subpackage enrol
22  * @copyright  2010 Petr Skoda {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 require('../config.php');
27 require_once("$CFG->dirroot/enrol/locallib.php");
28 require_once("$CFG->dirroot/enrol/users_forms.php");
29 require_once("$CFG->dirroot/enrol/renderer.php");
30 require_once("$CFG->dirroot/group/lib.php");
32 $id      = required_param('id', PARAM_INT); // course id
33 $action  = optional_param('action', '', PARAM_ACTION);
34 $filter  = optional_param('ifilter', 0, PARAM_INT);
36 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
37 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
39 if ($course->id == SITEID) {
40     redirect(new moodle_url('/'));
41 }
43 require_login($course);
44 require_capability('moodle/course:enrolreview', $context);
45 $PAGE->set_pagelayout('admin');
47 $manager = new course_enrolment_manager($PAGE, $course, $filter);
48 $table = new course_enrolment_users_table($manager, $PAGE);
49 $PAGE->set_url('/enrol/users.php', $manager->get_url_params()+$table->get_url_params());
50 navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id' => $id)));
52 // Check if there is an action to take
53 if ($action) {
55     // Check if the page is confirmed (and sesskey is correct)
56     $confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey();
58     $actiontaken = false;
59     $pagetitle = '';
60     $pageheading = '';
61     $mform = null;
62     $pagecontent = null;
64     switch ($action) {
65         /**
66          * Removes a role from the user with this course
67          */
68         case 'unassign':
69             if (has_capability('moodle/role:assign', $manager->get_context())) {
70                 $role = required_param('role', PARAM_INT);
71                 $user = required_param('user', PARAM_INT);
72                 if ($confirm && $manager->unassign_role_from_user($user, $role)) {
73                     redirect($PAGE->url);
74                 } else {
75                     $user = $DB->get_record('user', array('id'=>$user), '*', MUST_EXIST);
76                     $allroles = $manager->get_all_roles();
77                     $role = $allroles[$role];
78                     $yesurl = new moodle_url($PAGE->url, array('action'=>'unassign', 'role'=>$role->id, 'user'=>$user->id, 'confirm'=>1, 'sesskey'=>sesskey()));
79                     $message = get_string('unassignconfirm', 'role', array('user'=>fullname($user, true), 'role'=>$role->localname));
80                     $pagetitle = get_string('unassignarole', 'role', $role->localname);
81                     $pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url);
82                 }
83                 $actiontaken = true;
84             }
85             break;
86         /**
87          * Assigns a new role to a user enrolled within this course.
88          * A user must be enrolled in the course in order for this script to action
89          */
90         case 'assign':
91             $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
92             if (is_enrolled($context, $user) && has_capability('moodle/role:assign', $manager->get_context())) {
93                 $mform = new enrol_users_assign_form(NULL, array('user'=>$user, 'course'=>$course, 'assignable'=>$manager->get_assignable_roles()));
94                 $mform->set_data($PAGE->url->params());
95                 $data = $mform->get_data();
96                 if ($mform->is_cancelled() || ($data && array_key_exists($data->roleid, $manager->get_assignable_roles()) && $manager->assign_role_to_user($data->roleid, $user->id))) {
97                     redirect($PAGE->url);
98                 } else {
99                     $pagetitle = get_string('assignroles', 'role');
100                 }
101                 $actiontaken = true;
102             }
103             break;
104         /**
105          * Removes the user from the given group
106          */
107         case 'removemember':
108             if (has_capability('moodle/course:managegroups', $manager->get_context())) {
109                 $groupid = required_param('group', PARAM_INT);
110                 $userid  = required_param('user', PARAM_INT);
111                 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
112                 if ($confirm && $manager->remove_user_from_group($user, $groupid)) {
113                     redirect($PAGE->url);
114                 } else {
115                     $group = $manager->get_group($groupid);
116                     if (!$group) {
117                         break;
118                     }
119                     $yesurl = new moodle_url($PAGE->url, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey()));
120                     $message = get_string('removefromgroupconfirm', 'group', array('user'=>fullname($user, true), 'group'=>$group->name));
121                     $pagetitle = get_string('removefromgroup', 'group', $group->name);
122                     $pagecontent = $OUTPUT->confirm($message, $yesurl, $PAGE->url);
123                 }
124                 $actiontaken = true;
125             }
126             break;
127         /**
128          * Makes the user a member of a given group
129          */
130         case 'addmember':
131             if (has_capability('moodle/course:managegroups', $manager->get_context())) {
132                 $userid = required_param('user', PARAM_INT);
133                 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
135                 $mform = new enrol_users_addmember_form(NULL, array('user'=>$user, 'course'=>$course, 'allgroups'=>$manager->get_all_groups()));
136                 $mform->set_data($PAGE->url->params());
137                 $data = $mform->get_data();
138                 if ($mform->is_cancelled() || ($data && $manager->add_user_to_group($user, $data->groupid))) {
139                     redirect($PAGE->url);
140                 } else {
141                     $pagetitle = get_string('addgroup', 'group');
142                 }
143                 $actiontaken = true;
144             }
145             break;
146     }
148     // If we took an action display we need to display something special.
149     if ($actiontaken) {
150         if (empty($pageheading)) {
151             $pageheading = $pagetitle;
152         }
153         $PAGE->set_title($pagetitle);
154         $PAGE->set_heading($pageheading);
155         echo $OUTPUT->header();
156         echo $OUTPUT->heading(fullname($user));
157         if (!is_null($mform)) {
158             $mform->display();
159         } else {
160             echo $pagecontent;
161         }
162         echo $OUTPUT->footer();
163         exit;
164     }
168 $renderer = $PAGE->get_renderer('core_enrol');
169 $userdetails = array (
170     'picture' => false,
171     'firstname' => get_string('firstname'),
172     'lastname' => get_string('lastname'),
173 );
174 $extrafields = get_extra_user_fields($context);
175 foreach ($extrafields as $field) {
176     $userdetails[$field] = get_user_field_name($field);
179 $fields = array(
180     'userdetails' => $userdetails,
181     'lastseen' => get_string('lastaccess'),
182     'role' => get_string('roles', 'role'),
183     'group' => get_string('groups', 'group'),
184     'enrol' => get_string('enrolmentinstances', 'enrol')
185 );
186 $table->set_fields($fields, $renderer);
188 $canassign = has_capability('moodle/role:assign', $manager->get_context());
189 $users = $manager->get_users_for_display($manager, $table->sort, $table->sortdirection, $table->page, $table->perpage);
190 foreach ($users as $userid=>&$user) {
191     $user['picture'] = $OUTPUT->render($user['picture']);
192     $user['role'] = $renderer->user_roles_and_actions($userid, $user['roles'], $manager->get_assignable_roles(), $canassign, $PAGE->url);
193     $user['group'] = $renderer->user_groups_and_actions($userid, $user['groups'], $manager->get_all_groups(), has_capability('moodle/course:managegroups', $manager->get_context()), $PAGE->url);
194     $user['enrol'] = $renderer->user_enrolments_and_actions($user['enrolments']);;
196 $table->set_total_users($manager->get_total_users());
197 $table->set_users($users);
199 $PAGE->set_title($PAGE->course->fullname.': '.get_string('totalenrolledusers', 'enrol', $manager->get_total_users()));
200 $PAGE->set_heading($PAGE->title);
202 echo $OUTPUT->header();
203 echo $OUTPUT->heading(get_string('enrolledusers', 'enrol'));
204 echo $renderer->render($table);
205 echo $OUTPUT->footer();