PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/roles/classes/capability_table_base.php

https://bitbucket.org/moodle/moodle
PHP | 195 lines | 76 code | 25 blank | 94 comment | 5 complexity | a1ef6affecf121e107d359107fb0b60b MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Base capability table.
  18. *
  19. * @package core_role
  20. * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * This class represents a table with one row for each of a list of capabilities
  26. * where the first cell in the row contains the capability name, and there is
  27. * arbitrary stuff in the rest of the row. This class is used by
  28. * admin/roles/manage.php, override.php and check.php.
  29. *
  30. * An ajaxy search UI shown at the top, if JavaScript is on.
  31. */
  32. abstract class core_role_capability_table_base {
  33. /** The context this table relates to. */
  34. protected $context;
  35. /** The capabilities to display. Initialised as $context->get_capabilities(). */
  36. protected $capabilities = array();
  37. /** Added as an id="" attribute to the table on output. */
  38. protected $id;
  39. /** Added to the class="" attribute on output. */
  40. protected $classes = array('rolecap table-hover');
  41. /** Default number of capabilities in the table for the search UI to be shown. */
  42. const NUM_CAPS_FOR_SEARCH = 12;
  43. /**
  44. * Constructor.
  45. * @param context $context the context this table relates to.
  46. * @param string $id what to put in the id="" attribute.
  47. */
  48. public function __construct(context $context, $id) {
  49. $this->context = $context;
  50. $this->capabilities = $context->get_capabilities();
  51. $this->id = $id;
  52. }
  53. /**
  54. * Use this to add class="" attributes to the table. You get the rolecap by
  55. * default.
  56. * @param array $classnames of class names.
  57. */
  58. public function add_classes($classnames) {
  59. $this->classes = array_unique(array_merge($this->classes, $classnames));
  60. }
  61. /**
  62. * Display the table.
  63. */
  64. public function display() {
  65. if (count($this->capabilities) > self::NUM_CAPS_FOR_SEARCH) {
  66. global $PAGE;
  67. $jsmodule = array(
  68. 'name' => 'rolescapfilter',
  69. 'fullpath' => '/admin/roles/module.js',
  70. 'strings' => array(
  71. array('filter', 'moodle'),
  72. array('clear', 'moodle'), ),
  73. 'requires' => array('node', 'cookie', 'escape')
  74. );
  75. $PAGE->requires->js_init_call('M.core_role.init_cap_table_filter', array($this->id, $this->context->id), false,
  76. $jsmodule);
  77. }
  78. echo '<table class="' . implode(' ', $this->classes) . '" id="' . $this->id . '">' . "\n<thead>\n";
  79. echo '<tr><th class="name" align="left" scope="col">' . get_string('capability', 'core_role') . '</th>';
  80. $this->add_header_cells();
  81. echo "</tr>\n</thead>\n<tbody>\n";
  82. // Loop over capabilities.
  83. $contextlevel = 0;
  84. $component = '';
  85. foreach ($this->capabilities as $capability) {
  86. if ($this->skip_row($capability)) {
  87. continue;
  88. }
  89. // Prints a breaker if component or name or context level has changed.
  90. if (component_level_changed($capability, $component, $contextlevel)) {
  91. $this->print_heading_row($capability);
  92. }
  93. $contextlevel = $capability->contextlevel;
  94. $component = $capability->component;
  95. // Start the row.
  96. $rowattributes = $this->get_row_attributes($capability);
  97. // Handle class attributes same as other.
  98. $rowclasses = array_unique(array_merge(array('rolecap'), $this->get_row_classes($capability)));
  99. if (array_key_exists('class', $rowattributes)) {
  100. $rowclasses = array_unique(array_merge($rowclasses, array($rowattributes['class'])));
  101. }
  102. $rowattributes['class'] = implode(' ', $rowclasses);
  103. // Table cell for the capability name.
  104. $contents = '<th scope="row" class="name"><span class="cap-desc">' . get_capability_docs_link($capability) .
  105. '<span class="cap-name">' . $capability->name . '</span></span></th>';
  106. // Add the cells specific to this table.
  107. $contents .= $this->add_row_cells($capability);
  108. echo html_writer::tag('tr', $contents, $rowattributes);
  109. }
  110. // End of the table.
  111. echo "</tbody>\n</table>\n";
  112. }
  113. /**
  114. * Used to output a heading rows when the context level or component changes.
  115. * @param stdClass $capability gives the new component and contextlevel.
  116. */
  117. protected function print_heading_row($capability) {
  118. echo '<tr class="rolecapheading header"><td colspan="' . (1 + $this->num_extra_columns()) . '" class="header"><strong>' .
  119. get_component_string($capability->component, $capability->contextlevel) .
  120. '</strong></td></tr>';
  121. }
  122. /**
  123. * For subclasses to override, output header cells, after the initial capability one.
  124. */
  125. protected abstract function add_header_cells();
  126. /**
  127. * For subclasses to override, return the number of cells that add_header_cells/add_row_cells output.
  128. */
  129. protected abstract function num_extra_columns();
  130. /**
  131. * For subclasses to override. Allows certain capabilties
  132. * to be left out of the table.
  133. *
  134. * @param object $capability the capability this row relates to.
  135. * @return boolean. If true, this row is omitted from the table.
  136. */
  137. protected function skip_row($capability) {
  138. return false;
  139. }
  140. /**
  141. * For subclasses to override. A change to reaturn class names that are added
  142. * to the class="" attribute on the &lt;tr> for this capability.
  143. *
  144. * @param stdClass $capability the capability this row relates to.
  145. * @return array of class name strings.
  146. */
  147. protected function get_row_classes($capability) {
  148. return array();
  149. }
  150. /**
  151. * For subclasses to override. Additional attributes to be added to
  152. * each table row for the capability
  153. *
  154. * @param stdClass $capability the capability this row relates to.
  155. * @return array attribute names and their values.
  156. */
  157. protected function get_row_attributes($capability) {
  158. return array();
  159. }
  160. /**
  161. * For subclasses to override. Output the data cells for this capability. The
  162. * capability name cell will already have been output.
  163. *
  164. * You can rely on get_row_classes always being called before add_row_cells.
  165. *
  166. * @param stdClass $capability the capability this row relates to.
  167. * @return string html of row cells
  168. */
  169. protected abstract function add_row_cells($capability);
  170. }