Skip to content

Commit

Permalink
MDL-40337 cleanup users roles code
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jun 27, 2013
1 parent 72de139 commit 282ed3e
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions admin/roles/usersroles.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

// Get params.
$userid = required_param('userid', PARAM_INT);
Expand All @@ -44,12 +44,12 @@
$PAGE->set_context($coursecontext);
$PAGE->set_pagelayout('admin');

/// Check login and permissions.
// 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'));
print_error('nopermissions', 'error', '', get_string('checkpermissions', 'core_role'));
}

if ($userid != $USER->id) {
Expand All @@ -63,46 +63,40 @@
$PAGE->navbar->includesettingsbase = true;
}

/// Now get the role assignments for this user.
$sql = "SELECT
ra.id, ra.userid, ra.contextid, ra.roleid, ra.component, ra.itemid,
c.path
FROM
{role_assignments} ra
JOIN {context} c ON ra.contextid = c.id
JOIN {role} r ON ra.roleid = r.id
WHERE
ra.userid = ?
"./*AND ra.active = 1*/"
ORDER BY
contextlevel DESC, contextid ASC, r.sortorder ASC";
// Now get the role assignments for this user.
$sql = "SELECT ra.id, ra.userid, ra.contextid, ra.roleid, ra.component, ra.itemid, c.path
FROM {role_assignments} ra
JOIN {context} c ON ra.contextid = c.id
JOIN {role} r ON ra.roleid = r.id
WHERE ra.userid = ?
ORDER BY contextlevel DESC, contextid ASC, r.sortorder ASC";
$roleassignments = $DB->get_records_sql($sql, array($user->id));

$allroles = role_fix_names(get_all_roles());

/// 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.
// 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.
// Now load those contexts.
if ($requiredcontexts) {
list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
$contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
} else {
$contexts = array();
}

/// Prepare some empty arrays to hold the data we are about to compute.
// 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.
// Put the contexts into a tree structure.
foreach ($contexts as $conid => $con) {
$context = context::instance_by_id($conid);
$parentcontextid = get_parent_contextid($context);
Expand All @@ -111,20 +105,20 @@
}
}

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

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

/// Print the header
// Print the header.
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
$straction = get_string('thisusersroles', 'role');
$title = get_string('xroleassignments', 'role', $fullname);

/// Course header
// Course header.
$PAGE->set_title($title);
if ($courseid != SITEID) {
$PAGE->set_heading($fullname);
Expand All @@ -137,12 +131,12 @@

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

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

Expand All @@ -153,13 +147,13 @@ function print_report_tree($contextid, $contexts, $systemcontext, $fullname, $al
static $stredit = null, $strcheckpermissions, $globalroleassigner, $assignurl, $checkurl;
if (is_null($stredit)) {
$stredit = get_string('edit');
$strcheckpermissions = get_string('checkpermissions', 'role');
$strcheckpermissions = get_string('checkpermissions', 'core_role');
$globalroleassigner = has_capability('moodle/role:assign', $systemcontext);
$assignurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php';
$checkurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/check.php';
}

// Pull the current context into an array for convinience.
// Pull the current context into an array for convenience.
$context = context::instance_by_id($contextid);

// Print the context name.
Expand Down

0 comments on commit 282ed3e

Please sign in to comment.