id); $contexts = array(); $number = count($contextids); foreach ($contextids as $contextid) { $contexts[$contextid] = get_context_instance_by_id($contextid); $contexts[$contextid]->name = print_context_name($contexts[$contextid], true, true); $contexts[$contextid]->number = $number--; } // Validate the user id. if ($userid) { $user = $DB->get_record('user', array('id' => $userid)); if (!$user) { print_error('nosuchuser'); } } else { $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID); if (!empty($CFG->forcelogin) || ($context->contextlevel >= CONTEXT_COURSE && !in_array($frontpagecontext->id, $contextids))) { print_error('cannotgetherewithoutloggingin', 'role'); } } // Check access permissions. require_login(); if (!has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:assign'), $context)) { print_error('nopermissions', '', get_string('explainpermissions')); } // This duplicates code in load_all_capabilities and has_capability. $systempath = '/' . SYSCONTEXTID; if ($userid == 0) { if (!empty($CFG->notloggedinroleid)) { $accessdata = get_role_access($CFG->notloggedinroleid); $accessdata['ra'][$systempath] = array($CFG->notloggedinroleid); } else { $accessdata = array(); $accessdata['ra'] = array(); $accessdata['rdef'] = array(); $accessdata['loaded'] = array(); } } else if (isguestuser($user)) { $guestrole = get_guest_role(); $accessdata = get_role_access($guestrole->id); $accessdata['ra'][$systempath] = array($guestrole->id); } else { $accessdata = load_user_accessdata($userid); } if ($context->contextlevel > CONTEXT_COURSE && !path_inaccessdata($context->path, $accessdata)) { load_subcontext($userid, $context, $accessdata); } // Load the roles we need. $roleids = array(); foreach ($accessdata['ra'] as $roleassignments) { $roleids = array_merge($roleassignments, $roleids); } $roles = $DB->get_records_list('role', 'id', $roleids); $rolenames = array(); foreach ($roles as $role) { $rolenames[$role->id] = $role->name; } $rolenames = role_fix_names($rolenames, $context); // Pass over the data once, to find the cell that determines the result. $userhascapability = has_capability($capability, $context, $userid, false); $areprohibits = false; $decisiveassigncon = 0; $decisiveoverridecon = 0; foreach ($contexts as $con) { if (!empty($accessdata['ra'][$con->path])) { // The array_unique here is to work around bug MDL-14817. Once that bug is // fixed, it can be removed $ras = array_unique($accessdata['ra'][$con->path]); } else { $ras = array(); } $con->firstoverride = 0; foreach ($contexts as $ocon) { $summedpermission = 0; $gotsomething = false; foreach ($ras as $roleid) { if (isset($accessdata['rdef'][$ocon->path . ':' . $roleid][$capability])) { $perm = $accessdata['rdef'][$ocon->path . ':' . $roleid][$capability]; } else { $perm = CAP_INHERIT; } if ($perm && !$gotsomething) { $gotsomething = true; $con->firstoverride = $ocon->id; } if ($perm == CAP_PROHIBIT) { $areprohibits = true; $decisiveassigncon = 0; $decisiveoverridecon = 0; break; } $summedpermission += $perm; } if (!$areprohibits && !$decisiveassigncon && $summedpermission) { $decisiveassigncon = $con->id; $decisiveoverridecon = $ocon->id; break; } else if ($gotsomething) { break; } } } if (!$areprohibits && !$decisiveassigncon) { $decisiveassigncon = SYSCONTEXTID; $decisiveoverridecon = SYSCONTEXTID; } // Make a fake role to simplify rendering the table below. $rolenames[0] = get_string('none'); // Prepare some arrays of strings. $cssclasses = array( CAP_INHERIT => 'inherit', CAP_ALLOW => 'allow', CAP_PREVENT => 'prevent', CAP_PROHIBIT => 'prohibit', '' => '' ); $strperm = array( CAP_INHERIT => get_string('inherit', 'role'), CAP_ALLOW => get_string('allow', 'role'), CAP_PREVENT => get_string('prevent', 'role'), CAP_PROHIBIT => get_string('prohibit', 'role'), '' => '' ); // Start the output. print_header(get_string('explainpermission', 'role')); print_heading(get_string('explainpermission', 'role')); // Print a summary of what we are doing. $a = new stdClass; if ($userid) { $a->fullname = fullname($user); } else { $a->fullname = get_string('nobody'); } $a->capability = $capability; $a->context = reset($contexts)->name; if ($userhascapability) { echo '

' . get_string('whydoesuserhavecap', 'role', $a) . '

'; } else { echo '

' . get_string('whydoesusernothavecap', 'role', $a) . '

'; } // Print the table header rows. echo ''; echo ''; if (count($contexts) > 1) { echo ''; } echo ''; echo ''; echo ''; foreach (array_slice($contexts, 0, count($contexts) - 1) as $con) { echo ''; } echo ''; // Now print the bulk of the table. foreach ($contexts as $con) { if (!empty($accessdata['ra'][$con->path])) { // The array_unique here is to work around bug MDL-14817. Once that bug is // fixed, it can be removed $ras = array_unique($accessdata['ra'][$con->path]); } else { $ras = array(0); } $firstcell = ''; $rowclass = ' class="newcontext"'; foreach ($ras as $roleid) { $extraclass = ''; if (!$roleid) { $extraclass = ' noroles'; } echo '' . $firstcell . ''; $overridden = false; foreach ($contexts as $ocon) { if ($roleid == 0) { $perm = ''; } else { if (isset($accessdata['rdef'][$ocon->path . ':' . $roleid][$capability])) { $perm = $accessdata['rdef'][$ocon->path . ':' . $roleid][$capability]; } else { $perm = CAP_INHERIT; } } if ($perm === CAP_INHERIT && $ocon->id == SYSCONTEXTID) { $permission = get_string('notset', 'role'); } else { $permission = $strperm[$perm]; } $classes = $cssclasses[$perm]; if (!$areprohibits && $decisiveassigncon == $con->id && $decisiveoverridecon == $ocon->id) { $classes .= ' decisive'; if ($userhascapability) { $classes .= ' has'; } else { $classes .= ' hasnot'; } } if ($overridden) { $classes .= ' overridden'; } echo ''; if ($con->firstoverride == $ocon->id) { $overridden = true; } } echo ''; $firstcell = ''; $rowclass = ''; } } echo '
' . get_string('roleassignments', 'role') . '' . get_string('overridesbycontext', 'role') . '' . get_string('roledefinitions', 'role') . '
' . get_string('context', 'role') . '' . get_string('role') . '' . $con->number . '
' . $con->number . '. ' . $con->name . '' . $rolenames[$roleid] . '' . $permission . '
'; // Finish the page. echo get_string('explainpermissionsinfo', 'role'); if ($userid && $capability != 'moodle/site:doanything' && !$userhascapability && has_capability('moodle/site:doanything', $context, $userid)) { echo '

' . get_string('explainpermissionsdoanything', 'role', $capability) . '

'; } close_window_button(); print_footer('empty'); ?>