.
/**
* Library code used by the roles administration interfaces.
*
* @package core_role
* @copyright 2009 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Subclass of core_role_capability_table_base for use on the Permissions page.
*/
class core_role_permissions_table extends core_role_capability_table_base {
protected $contextname;
protected $allowoverrides;
protected $allowsafeoverrides;
protected $overridableroles;
protected $roles;
protected $icons = array();
/**
* Constructor.
* @param context $context the context this table relates to.
* @param string $contextname $context->get_context_name() - to save recomputing.
* @param array $allowoverrides
* @param array $allowsafeoverrides
* @param array $overridableroles
*/
public function __construct($context, $contextname, $allowoverrides, $allowsafeoverrides, $overridableroles) {
parent::__construct($context, 'permissions');
$this->contextname = $contextname;
$this->allowoverrides = $allowoverrides;
$this->allowsafeoverrides = $allowsafeoverrides;
$this->overridableroles = $overridableroles;
$roles = get_all_roles($context);
$this->roles = role_fix_names(array_reverse($roles, true), $context, ROLENAME_ALIAS, true);
}
protected function add_header_cells() {
echo '
' . get_string('risks', 'core_role') . ' | ';
echo '' . get_string('neededroles', 'core_role') . ' | ';
echo '' . get_string('prohibitedroles', 'core_role') . ' | ';
}
protected function num_extra_columns() {
return 3;
}
protected function add_row_cells($capability) {
global $OUTPUT, $PAGE;
$context = $this->context;
$contextid = $this->context->id;
$allowoverrides = $this->allowoverrides;
$allowsafeoverrides = $this->allowsafeoverrides;
$overridableroles = $this->overridableroles;
$roles = $this->roles;
list($needed, $forbidden) = get_roles_with_cap_in_context($context, $capability->name);
$neededroles = array();
$forbiddenroles = array();
$allowable = $overridableroles;
$forbitable = $overridableroles;
foreach ($neededroles as $id => $unused) {
unset($allowable[$id]);
}
foreach ($forbidden as $id => $unused) {
unset($allowable[$id]);
unset($forbitable[$id]);
}
foreach ($roles as $id => $name) {
if (isset($needed[$id])) {
$neededroles[$id] = $roles[$id];
if (isset($overridableroles[$id]) and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
$preventurl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'roleid'=>$id, 'capability'=>$capability->name, 'prevent'=>1));
$neededroles[$id] .= $OUTPUT->action_icon($preventurl, new pix_icon('t/delete', get_string('prevent', 'core_role')));
}
}
}
$neededroles = implode(', ', $neededroles);
foreach ($roles as $id => $name) {
if (isset($forbidden[$id]) and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
$forbiddenroles[$id] = $roles[$id];
if (isset($overridableroles[$id]) and prohibit_is_removable($id, $context, $capability->name)) {
$unprohibiturl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'roleid'=>$id, 'capability'=>$capability->name, 'unprohibit'=>1));
$forbiddenroles[$id] .= $OUTPUT->action_icon($unprohibiturl, new pix_icon('t/delete', get_string('delete')));
}
}
}
$forbiddenroles = implode(', ', $forbiddenroles);
if ($allowable and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
$allowurl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'capability'=>$capability->name, 'allow'=>1));
$neededroles .= ''.$OUTPUT->action_icon($allowurl, new pix_icon('t/add', get_string('allow', 'core_role'))).'
';
}
if ($forbitable and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
$prohibiturl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'capability'=>$capability->name, 'prohibit'=>1));
$forbiddenroles .= ''.$OUTPUT->action_icon($prohibiturl, new pix_icon('t/add', get_string('prohibit', 'core_role'))).'
';
}
$risks = $this->get_risks($capability);
echo '' . $risks . ' | ';
echo '' . $neededroles . ' | ';
echo '' . $forbiddenroles . ' | ';
}
protected function get_risks($capability) {
global $OUTPUT;
$allrisks = get_all_risks();
$risksurl = new moodle_url(get_docs_url(s(get_string('risks', 'core_role'))));
$return = '';
foreach ($allrisks as $type => $risk) {
if ($risk & (int)$capability->riskbitmask) {
if (!isset($this->icons[$type])) {
$pixicon = new pix_icon('/i/' . str_replace('risk', 'risk_', $type), get_string($type . 'short', 'admin'));
$this->icons[$type] = $OUTPUT->action_icon($risksurl, $pixicon, new popup_action('click', $risksurl));
}
$return .= $this->icons[$type];
}
}
return $return;
}
}