3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * User roles report list all the users who have been assigned a particular
20 * role in all contexts.
24 * @copyright © 2007 The Open University and others
25 * @author t.j.hunt@open.ac.uk and others
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once(dirname(__FILE__) . '/../../config.php');
32 $userid = required_param('userid', PARAM_INT);
33 $courseid = required_param('courseid', PARAM_INT);
35 // Validate them and get the corresponding objects.
36 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
37 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
39 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
40 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
41 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
43 $baseurl = new moodle_url('/admin/roles/usersroles.php', array('userid'=>$userid, 'courseid'=>$courseid));
45 $PAGE->set_url($baseurl);
46 $PAGE->set_context($coursecontext);
47 $PAGE->set_pagelayout('admin');
49 /// Check login and permissions.
50 require_login($course);
51 $canview = has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
52 'moodle/role:override', 'moodle/role:manage'), $usercontext);
54 print_error('nopermissions', 'error', '', get_string('checkpermissions', 'role'));
57 /// Now get the role assignments for this user.
59 ra.id, ra.userid, ra.contextid, ra.roleid, ra.component, ra.itemid,
62 COALESCE(rn.name, r.name) AS localname
65 JOIN {context} c ON ra.contextid = c.id
66 JOIN {role} r ON ra.roleid = r.id
67 LEFT JOIN {role_names} rn ON rn.roleid = ra.roleid AND rn.contextid = ra.contextid
70 "./*AND ra.active = 1*/"
72 contextlevel DESC, contextid ASC, r.sortorder ASC";
73 $roleassignments = $DB->get_records_sql($sql, array($user->id));
75 /// In order to display a nice tree of contexts, we need to get all the
76 /// ancestors of all the contexts in the query we just did.
77 $requiredcontexts = array();
78 foreach ($roleassignments as $ra) {
79 $requiredcontexts = array_merge($requiredcontexts, explode('/', trim($ra->path, '/')));
81 $requiredcontexts = array_unique($requiredcontexts);
83 /// Now load those contexts.
84 if ($requiredcontexts) {
85 list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
86 $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
91 /// Prepare some empty arrays to hold the data we are about to compute.
92 foreach ($contexts as $conid => $con) {
93 $contexts[$conid]->children = array();
94 $contexts[$conid]->roleassignments = array();
97 /// Put the contexts into a tree structure.
98 foreach ($contexts as $conid => $con) {
99 $context = context::instance_by_id($conid);
100 $parentcontextid = get_parent_contextid($context);
101 if ($parentcontextid) {
102 $contexts[$parentcontextid]->children[] = $conid;
106 /// Put the role capabilities into the context tree.
107 foreach ($roleassignments as $ra) {
108 $contexts[$ra->contextid]->roleassignments[$ra->roleid] = $ra;
111 $assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH);
112 $overridableroles = get_overridable_roles($usercontext, ROLENAME_BOTH);
115 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
116 $straction = get_string('thisusersroles', 'role');
117 $title = get_string('xroleassignments', 'role', $fullname);
120 $PAGE->set_title($title);
121 if ($courseid != SITEID) {
122 $PAGE->set_heading($fullname);
123 if (has_capability('moodle/course:viewparticipants', $coursecontext)) {
124 $PAGE->navbar->add(get_string('participants'),new moodle_url('/user/index.php', array('id'=>$courseid)));
127 $PAGE->set_heading($course->fullname);
129 $PAGE->navbar->add($fullname, new moodle_url("$CFG->wwwroot/user/view.php", array('id'=>$userid,'course'=>$courseid)));
130 $PAGE->navbar->add($straction);
131 echo $OUTPUT->header();
132 echo $OUTPUT->heading($title, 3);
133 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
136 if (!$roleassignments) {
137 echo '<p>', get_string('noroleassignments', 'role'), '</p>';
139 print_report_tree($systemcontext->id, $contexts, $systemcontext, $fullname);
143 echo $OUTPUT->box_end();
144 echo $OUTPUT->footer();
146 function print_report_tree($contextid, $contexts, $systemcontext, $fullname) {
147 global $CFG, $OUTPUT;
149 // Only compute lang strings, etc once.
150 static $stredit = null, $strcheckpermissions, $globalroleassigner, $assignurl, $checkurl;
151 if (is_null($stredit)) {
152 $stredit = get_string('edit');
153 $strcheckpermissions = get_string('checkpermissions', 'role');
154 $globalroleassigner = has_capability('moodle/role:assign', $systemcontext);
155 $assignurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php';
156 $checkurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/check.php';
159 // Pull the current context into an array for convinience.
160 $context = context::instance_by_id($contextid);
162 // Print the context name.
163 echo $OUTPUT->heading($context->get_context_name(), 4, 'contextname');
165 // If there are any role assignments here, print them.
166 foreach ($contexts[$contextid]->roleassignments as $ra) {
167 $value = $ra->contextid . ',' . $ra->roleid;
168 $inputid = 'unassign' . $value;
171 if ($ra->rolename == $ra->localname) {
172 echo strip_tags(format_string($ra->localname));
174 echo strip_tags(format_string($ra->localname . ' (' . $ra->rolename . ')'));
176 if (has_capability('moodle/role:assign', $context)) {
177 $raurl = $assignurl . '?contextid=' . $ra->contextid . '&roleid=' .
178 $ra->roleid . '&removeselect[]=' . $ra->userid;
179 $churl = $checkurl . '?contextid=' . $ra->contextid . '&reportuser=' . $ra->userid;
180 if ($context->contextlevel == CONTEXT_USER) {
181 $raurl .= '&userid=' . $context->instanceid;
182 $churl .= '&userid=' . $context->instanceid;
185 $a->fullname = $fullname;
186 $a->contextlevel = get_contextlevel_name($context->contextlevel);
187 if ($context->contextlevel == CONTEXT_SYSTEM) {
188 $strgoto = get_string('gotoassignsystemroles', 'role');
189 $strcheck = get_string('checksystempermissionsfor', 'role', $a);
191 $strgoto = get_string('gotoassignroles', 'role', $a);
192 $strcheck = get_string('checkuserspermissionshere', 'role', $a);
194 echo ' <a title="' . $strgoto . '" href="' . $raurl . '"><img class="iconsmall" src="' .
195 $OUTPUT->pix_url('t/edit') . '" alt="' . $stredit . '" /></a> ';
196 echo ' <a title="' . $strcheck . '" href="' . $churl . '"><img class="iconsmall" src="' .
197 $OUTPUT->pix_url('t/preview') . '" alt="' . $strcheckpermissions . '" /></a> ';
202 // If there are any child contexts, print them recursively.
203 if (!empty($contexts[$contextid]->children)) {
205 foreach ($contexts[$contextid]->children as $childcontextid) {
207 print_report_tree($childcontextid, $contexts, $systemcontext, $fullname);