usersroles.php 8.27 KB
Newer Older
1
<?php
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
18
19
20
21

/**
 * User roles report list all the users who have been assigned a particular
 * role in all contexts.
 *
Petr Skoda's avatar
Petr Skoda committed
22
 * @package    core
23
24
25
26
27
 * @subpackage role
 * @copyright  &copy; 2007 The Open University and others
 * @author     t.j.hunt@open.ac.uk and others
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
28
29
30
31
32
33
34
35

require_once(dirname(__FILE__) . '/../../config.php');

// Get params.
$userid = required_param('userid', PARAM_INT);
$courseid = required_param('courseid', PARAM_INT);

// Validate them and get the corresponding objects.
Petr Skoda's avatar
Petr Skoda committed
36
37
38
$user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);

39
40
$usercontext = get_context_instance(CONTEXT_USER, $user->id);
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
41
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
42

43
$baseurl = new moodle_url('/admin/roles/usersroles.php', array('userid'=>$userid, 'courseid'=>$courseid));
Petr Skoda's avatar
Petr Skoda committed
44

45
$PAGE->set_url($baseurl);
Petr Skoda's avatar
Petr Skoda committed
46
$PAGE->set_context($coursecontext);
47
$PAGE->set_pagelayout('admin');
48
49
50
51
52
53
54
55
56

/// Check login and permissions.
require_login($course);
$canview = has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
        'moodle/role:override', 'moodle/role:manage'), $usercontext);
if (!$canview) {
    print_error('nopermissions', 'error', '', get_string('checkpermissions', 'role'));
}

57
58
/// Now get the role assignments for this user.
$sql = "SELECT
David Mudrak's avatar
David Mudrak committed
59
        ra.id, ra.userid, ra.contextid, ra.roleid, ra.component, ra.itemid,
60
61
62
63
64
65
66
67
68
69
        c.path,
        r.name AS rolename,
        COALESCE(rn.name, r.name) AS localname
    FROM
        {role_assignments} ra
        JOIN {context} c ON ra.contextid = c.id
        JOIN {role} r ON ra.roleid = r.id
        LEFT JOIN {role_names} rn ON rn.roleid = ra.roleid AND rn.contextid = ra.contextid
    WHERE
        ra.userid = ?
70
    "./*AND ra.active = 1*/"
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    ORDER BY
        contextlevel DESC, contextid ASC, r.sortorder ASC";
$roleassignments = $DB->get_records_sql($sql, array($user->id));

/// In order to display a nice tree of contexts, we need to get all the
/// ancestors of all the contexts in the query we just did.
$requiredcontexts = array();
foreach ($roleassignments as $ra) {
    $requiredcontexts = array_merge($requiredcontexts, explode('/', trim($ra->path, '/')));
}
$requiredcontexts = array_unique($requiredcontexts);

/// Now load those contexts.
if ($requiredcontexts) {
    list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
86
    $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
87
88
89
90
91
92
93
94
95
96
97
98
} else {
    $contexts = array();
}

/// Prepare some empty arrays to hold the data we are about to compute.
foreach ($contexts as $conid => $con) {
    $contexts[$conid]->children = array();
    $contexts[$conid]->roleassignments = array();
}

/// Put the contexts into a tree structure.
foreach ($contexts as $conid => $con) {
Petr Skoda's avatar
Petr Skoda committed
99
100
    $context = context::instance_by_id($conid);
    $parentcontextid = get_parent_contextid($context);
101
102
103
104
105
    if ($parentcontextid) {
        $contexts[$parentcontextid]->children[] = $conid;
    }
}

106
/// Put the role capabilities into the context tree.
107
108
109
110
foreach ($roleassignments as $ra) {
    $contexts[$ra->contextid]->roleassignments[$ra->roleid] = $ra;
}

111
112
113
$assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH);
$overridableroles = get_overridable_roles($usercontext, ROLENAME_BOTH);

114
/// Print the header
115
116
117
118
119
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
$straction = get_string('thisusersroles', 'role');
$title = get_string('xroleassignments', 'role', $fullname);

/// Course header
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)));
125
126
    }
} else {
127
    $PAGE->set_heading($course->fullname);
128
}
129
$PAGE->navbar->add($fullname, new moodle_url("$CFG->wwwroot/user/view.php", array('id'=>$userid,'course'=>$courseid)));
130
131
$PAGE->navbar->add($straction);
echo $OUTPUT->header();
132
echo $OUTPUT->heading($title, 3);
133
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
134
135
136
137
138
139
140
141
142

// Display them.
if (!$roleassignments) {
    echo '<p>', get_string('noroleassignments', 'role'), '</p>';
} else {
    print_report_tree($systemcontext->id, $contexts, $systemcontext, $fullname);
}

/// End of page.
143
echo $OUTPUT->box_end();
144
echo $OUTPUT->footer();
145

146
function print_report_tree($contextid, $contexts, $systemcontext, $fullname) {
147
    global $CFG, $OUTPUT;
148
149
150
151
152
153
154
155
156
157

    // Only compute lang strings, etc once.
    static $stredit = null, $strcheckpermissions, $globalroleassigner, $assignurl, $checkurl;
    if (is_null($stredit)) {
        $stredit = get_string('edit');
        $strcheckpermissions = get_string('checkpermissions', 'role');
        $globalroleassigner = has_capability('moodle/role:assign', $systemcontext);
        $assignurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php';
        $checkurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/check.php';
    }
158

159
    // Pull the current context into an array for convinience.
Petr Skoda's avatar
Petr Skoda committed
160
    $context = context::instance_by_id($contextid);
161

162
    // Print the context name.
Petr Skoda's avatar
Petr Skoda committed
163
    echo $OUTPUT->heading($context->get_context_name(), 4, 'contextname');
164
165

    // If there are any role assignments here, print them.
Petr Skoda's avatar
Petr Skoda committed
166
    foreach ($contexts[$contextid]->roleassignments as $ra) {
167
168
169
170
171
172
        $value = $ra->contextid . ',' . $ra->roleid;
        $inputid = 'unassign' . $value;

        echo '<p>';
        if ($ra->rolename == $ra->localname) {
            echo strip_tags(format_string($ra->localname));
173
        } else {
174
            echo strip_tags(format_string($ra->localname . ' (' . $ra->rolename . ')'));
175
        }
176
177
178
        if (has_capability('moodle/role:assign', $context)) {
            $raurl = $assignurl . '?contextid=' . $ra->contextid . '&amp;roleid=' .
                    $ra->roleid . '&amp;removeselect[]=' . $ra->userid;
tjhunt's avatar
tjhunt committed
179
            $churl = $checkurl . '?contextid=' . $ra->contextid . '&amp;reportuser=' . $ra->userid;
180
181
182
            if ($context->contextlevel == CONTEXT_USER) {
                $raurl .= '&amp;userid=' . $context->instanceid;
                $churl .= '&amp;userid=' . $context->instanceid;
183
            }
184
185
186
187
188
189
190
191
192
            $a = new stdClass;
            $a->fullname = $fullname;
            $a->contextlevel = get_contextlevel_name($context->contextlevel);
            if ($context->contextlevel == CONTEXT_SYSTEM) {
                $strgoto = get_string('gotoassignsystemroles', 'role');
                $strcheck = get_string('checksystempermissionsfor', 'role', $a);
            } else {
                $strgoto = get_string('gotoassignroles', 'role', $a);
                $strcheck = get_string('checkuserspermissionshere', 'role', $a);
193
            }
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> ';
198
            echo "</p>\n";
199
        }
200
    }
201

202
203
204
205
206
207
208
    // If there are any child contexts, print them recursively.
    if (!empty($contexts[$contextid]->children)) {
        echo '<ul>';
        foreach ($contexts[$contextid]->children as $childcontextid) {
            echo '<li>';
            print_report_tree($childcontextid, $contexts, $systemcontext, $fullname);
            echo '</li>';
209
        }
210
        echo '</ul>';
211
212
    }
}