MDL-41806 Add assessors to moodle_url class
[moodle.git] / admin / roles / usersroles.php
blobc62c979a67336d88e19e34d2f5ca59f5e0100a54
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 * User roles report list all the users who have been assigned a particular
19 * role in all contexts.
21 * @package core_role
22 * @copyright &copy; 2007 The Open University and others
23 * @author t.j.hunt@open.ac.uk and others
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once(__DIR__ . '/../../config.php');
29 // Get params.
30 $userid = required_param('userid', PARAM_INT);
31 $courseid = required_param('courseid', PARAM_INT);
33 // Validate them and get the corresponding objects.
34 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
35 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
37 $usercontext = context_user::instance($user->id);
38 $coursecontext = context_course::instance($course->id);
39 $systemcontext = context_system::instance();
41 $baseurl = new moodle_url('/admin/roles/usersroles.php', array('userid'=>$userid, 'courseid'=>$courseid));
43 $PAGE->set_url($baseurl);
44 $PAGE->set_context($coursecontext);
45 $PAGE->set_pagelayout('admin');
47 // Check login and permissions.
48 require_login($course);
49 $canview = has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
50 'moodle/role:override', 'moodle/role:manage'), $usercontext);
51 if (!$canview) {
52 print_error('nopermissions', 'error', '', get_string('checkpermissions', 'core_role'));
55 if ($userid != $USER->id) {
56 // If its not the current user we need to extend the navigation for that user to ensure
57 // their navigation is loaded and this page found upon it.
58 $PAGE->navigation->extend_for_user($user);
60 if ($course->id != $SITE->id || $userid != $USER->id) {
61 // If we're within a course OR if we're viewing another user then we need to include the
62 // settings base on the navigation to ensure that the navbar will contain the users name.
63 $PAGE->navbar->includesettingsbase = true;
66 // Now get the role assignments for this user.
67 $sql = "SELECT ra.id, ra.userid, ra.contextid, ra.roleid, ra.component, ra.itemid, c.path
68 FROM {role_assignments} ra
69 JOIN {context} c ON ra.contextid = c.id
70 JOIN {role} r ON ra.roleid = r.id
71 WHERE ra.userid = ?
72 ORDER BY contextlevel DESC, contextid ASC, r.sortorder ASC";
73 $roleassignments = $DB->get_records_sql($sql, array($user->id));
75 $allroles = role_fix_names(get_all_roles());
77 // In order to display a nice tree of contexts, we need to get all the
78 // ancestors of all the contexts in the query we just did.
79 $requiredcontexts = array();
80 foreach ($roleassignments as $ra) {
81 $requiredcontexts = array_merge($requiredcontexts, explode('/', trim($ra->path, '/')));
83 $requiredcontexts = array_unique($requiredcontexts);
85 // Now load those contexts.
86 if ($requiredcontexts) {
87 list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
88 $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
89 } else {
90 $contexts = array();
93 // Prepare some empty arrays to hold the data we are about to compute.
94 foreach ($contexts as $conid => $con) {
95 $contexts[$conid]->children = array();
96 $contexts[$conid]->roleassignments = array();
99 // Put the contexts into a tree structure.
100 foreach ($contexts as $conid => $con) {
101 $context = context::instance_by_id($conid);
102 $parentcontext = $context->get_parent_context();
103 if ($parentcontext) {
104 $contexts[$parentcontext->id]->children[] = $conid;
108 // Put the role capabilities into the context tree.
109 foreach ($roleassignments as $ra) {
110 $contexts[$ra->contextid]->roleassignments[$ra->roleid] = $ra;
113 $assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH);
114 $overridableroles = get_overridable_roles($usercontext, ROLENAME_BOTH);
116 // Print the header.
117 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
118 $straction = get_string('thisusersroles', 'core_role');
119 $title = get_string('xroleassignments', 'core_role', $fullname);
121 // Course header.
122 $PAGE->set_title($title);
123 if ($courseid != SITEID) {
124 $PAGE->set_heading($fullname);
125 } else {
126 $PAGE->set_heading($course->fullname);
128 echo $OUTPUT->header();
129 echo $OUTPUT->heading($title, 3);
130 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
132 // Display them.
133 if (!$roleassignments) {
134 echo '<p>', get_string('noroleassignments', 'core_role'), '</p>';
135 } else {
136 print_report_tree($systemcontext->id, $contexts, $systemcontext, $fullname, $allroles);
139 // End of page.
140 echo $OUTPUT->box_end();
141 echo $OUTPUT->footer();
143 function print_report_tree($contextid, $contexts, $systemcontext, $fullname, $allroles) {
144 global $CFG, $OUTPUT;
146 // Only compute lang strings, etc once.
147 static $stredit = null, $strcheckpermissions, $globalroleassigner, $assignurl, $checkurl;
148 if (is_null($stredit)) {
149 $stredit = get_string('edit');
150 $strcheckpermissions = get_string('checkpermissions', 'core_role');
151 $globalroleassigner = has_capability('moodle/role:assign', $systemcontext);
152 $assignurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php';
153 $checkurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/check.php';
156 // Pull the current context into an array for convenience.
157 $context = context::instance_by_id($contextid);
159 // Print the context name.
160 echo $OUTPUT->heading(html_writer::link($context->get_url(), $context->get_context_name()),
161 4, 'contextname');
163 // If there are any role assignments here, print them.
164 foreach ($contexts[$contextid]->roleassignments as $ra) {
165 $role = $allroles[$ra->roleid];
167 $value = $ra->contextid . ',' . $ra->roleid;
168 $inputid = 'unassign' . $value;
170 echo '<p>';
171 echo $role->localname;
172 if (has_capability('moodle/role:assign', $context)) {
173 $raurl = $assignurl . '?contextid=' . $ra->contextid . '&amp;roleid=' .
174 $ra->roleid . '&amp;removeselect[]=' . $ra->userid;
175 $churl = $checkurl . '?contextid=' . $ra->contextid . '&amp;reportuser=' . $ra->userid;
176 if ($context->contextlevel == CONTEXT_USER) {
177 $raurl .= '&amp;userid=' . $context->instanceid;
178 $churl .= '&amp;userid=' . $context->instanceid;
180 $a = new stdClass;
181 $a->fullname = $fullname;
182 $a->contextlevel = $context->get_level_name();
183 if ($context->contextlevel == CONTEXT_SYSTEM) {
184 $strgoto = get_string('gotoassignsystemroles', 'core_role');
185 $strcheck = get_string('checksystempermissionsfor', 'core_role', $a);
186 } else {
187 $strgoto = get_string('gotoassignroles', 'core_role', $a);
188 $strcheck = get_string('checkuserspermissionshere', 'core_role', $a);
190 echo ' <a title="' . $strgoto . '" href="' . $raurl . '"><img class="iconsmall" src="' .
191 $OUTPUT->pix_url('t/edit') . '" alt="' . $stredit . '" /></a> ';
192 echo ' <a title="' . $strcheck . '" href="' . $churl . '"><img class="iconsmall" src="' .
193 $OUTPUT->pix_url('t/preview') . '" alt="' . $strcheckpermissions . '" /></a> ';
194 echo "</p>\n";
198 // If there are any child contexts, print them recursively.
199 if (!empty($contexts[$contextid]->children)) {
200 echo '<ul>';
201 foreach ($contexts[$contextid]->children as $childcontextid) {
202 echo '<li>';
203 print_report_tree($childcontextid, $contexts, $systemcontext, $fullname, $allroles);
204 echo '</li>';
206 echo '</ul>';