roles explanation: MDL-13538 Make a table showing the result of has_capability for every capabiltiy in a context.
This implements the page showing the table. It is not yet integrated into the tab bar. To try this, go to an Assign roles page (one with URL .../admin/roles/assign.php?contextid=...) and change the 'assign' to 'explain'.
This commit is contained in:
@@ -6115,3 +6115,134 @@ class admin_setting_managerepository extends admin_setting {
|
||||
return highlight($query, $output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class represents a table with one row for each of a list of capabilities
|
||||
* where the first cell in the row contains the capability name, and there is
|
||||
* arbitrary stuff in the rest of the row. This class is used by
|
||||
* admin/roles/manage.php, override.php and explain.php. There is also some
|
||||
* ajaxy search UI shown at the top, if JavaScript is on.
|
||||
*/
|
||||
abstract class cabability_table_base {
|
||||
protected $context;
|
||||
protected $capabilities = array();
|
||||
protected $id;
|
||||
protected $classes = array('rolecap');
|
||||
const NUM_CAPS_FOR_SEARCH = 12;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param object $context the context this table relates to.
|
||||
* @param string $id what to put in the id="" attribute.
|
||||
*/
|
||||
public function __construct($context, $id) {
|
||||
$this->context = $context;
|
||||
$this->capabilities = fetch_context_capabilities($context);
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this to add class="" attributes to the table. You get the rolecap by
|
||||
* default.
|
||||
* @param array $classnames of class names.
|
||||
*/
|
||||
public function add_classes($classnames) {
|
||||
$this->classes = array_unique(array_merge($this->classes, $classnames));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the table.
|
||||
*/
|
||||
public function display() {
|
||||
echo '<table class="' . implode(' ', $this->classes) . '" id="' . $this->id . '">' . "\n<thead>\n";
|
||||
echo '<tr><th class="name" align="left" scope="col">' . get_string('capability','role') . '</th>';
|
||||
$this->add_header_cells();
|
||||
echo "</tr>\n</thead>\n<tbody>\n";
|
||||
|
||||
/// Loop over capabilities.
|
||||
$contextlevel = 0;
|
||||
$component = '';
|
||||
foreach ($this->capabilities as $capability) {
|
||||
if ($this->skip_row($capability)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/// Prints a breaker if component or name or context level has changed
|
||||
if (component_level_changed($capability, $component, $contextlevel)) {
|
||||
$this->print_heading_row($capability);
|
||||
}
|
||||
$contextlevel = $capability->contextlevel;
|
||||
$component = $capability->component;
|
||||
|
||||
/// Start the row.
|
||||
echo '<tr class="' . implode(' ', array_unique(array_merge(array('rolecap'),
|
||||
$this->get_row_classes($capability)))) . '">';
|
||||
|
||||
/// Table cell for the capability name.
|
||||
echo '<td class="name"><span class="cap-desc">' . get_capability_docs_link($capability) .
|
||||
'<span class="cap-name">' . $capability->name . '</span></span></td>';
|
||||
|
||||
/// Add the cells specific to this table.
|
||||
$this->add_row_cells($capability);
|
||||
|
||||
/// End the row.
|
||||
echo "</tr>\n";
|
||||
}
|
||||
|
||||
/// End of the table.
|
||||
echo "</tbody>\n</table>\n";
|
||||
if (count($this->capabilities) > cabability_table_base::NUM_CAPS_FOR_SEARCH) {
|
||||
print_js_call('cap_table_filter.init',
|
||||
array($this->id, get_string('search'), get_string('clear')));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to output a heading rows when the context level or component changes.
|
||||
* @param object $capability gives the new component and contextlevel.
|
||||
*/
|
||||
protected function print_heading_row($capability) {
|
||||
echo '<tr class="rolecapheading header"><td colspan="' . (1 + $this->num_extra_columns()) . '" class="header"><strong>' .
|
||||
get_component_string($capability->component, $capability->contextlevel) .
|
||||
'</strong></td></tr>';
|
||||
|
||||
}
|
||||
|
||||
/** For subclasses to override, output header cells, after the initial capability one. */
|
||||
protected abstract function add_header_cells();
|
||||
|
||||
/** For subclasses to override, return the number of cells that add_header_cells/add_row_cells output. */
|
||||
protected abstract function num_extra_columns();
|
||||
|
||||
/**
|
||||
* For subclasses to override. Allows certain capabilties (e.g. legacy capabilities)
|
||||
* to be left out of the table.
|
||||
*
|
||||
* @param object $capability the capability this row relates to.
|
||||
* @return boolean. If true, this row is omitted from the table.
|
||||
*/
|
||||
protected function skip_row($capability) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* For subclasses to override. A change to reaturn class names that are added
|
||||
* to the class="" attribute on the <tr> for this capability.
|
||||
*
|
||||
* @param object $capability the capability this row relates to.
|
||||
* @return array of class name strings.
|
||||
*/
|
||||
protected function get_row_classes($capability) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* For subclasses to override. Output the data cells for this capability. The
|
||||
* capability name cell will already have been output.
|
||||
*
|
||||
* You can rely on get_row_classes always being called before add_row_cells.
|
||||
*
|
||||
* @param object $capability the capability this row relates to.
|
||||
*/
|
||||
protected abstract function add_row_cells($capability);
|
||||
}
|
||||
Reference in New Issue
Block a user