PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/roles/classes/capability_table_base.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 179 lines | 69 code | 24 blank | 86 comment | 4 complexity | 34e72ce08b4d41f3bcda29c5ffa9c126 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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');
  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. echo '<tr class="' . implode(' ', array_unique(array_merge(array('rolecap'),
  97. $this->get_row_classes($capability)))) . '">';
  98. // Table cell for the capability name.
  99. echo '<th scope="row" class="name"><span class="cap-desc">' . get_capability_docs_link($capability) .
  100. '<span class="cap-name">' . $capability->name . '</span></span></th>';
  101. // Add the cells specific to this table.
  102. $this->add_row_cells($capability);
  103. // End the row.
  104. echo "</tr>\n";
  105. }
  106. // End of the table.
  107. echo "</tbody>\n</table>\n";
  108. }
  109. /**
  110. * Used to output a heading rows when the context level or component changes.
  111. * @param stdClass $capability gives the new component and contextlevel.
  112. */
  113. protected function print_heading_row($capability) {
  114. echo '<tr class="rolecapheading header"><td colspan="' . (1 + $this->num_extra_columns()) . '" class="header"><strong>' .
  115. get_component_string($capability->component, $capability->contextlevel) .
  116. '</strong></td></tr>';
  117. }
  118. /**
  119. * For subclasses to override, output header cells, after the initial capability one.
  120. */
  121. protected abstract function add_header_cells();
  122. /**
  123. * For subclasses to override, return the number of cells that add_header_cells/add_row_cells output.
  124. */
  125. protected abstract function num_extra_columns();
  126. /**
  127. * For subclasses to override. Allows certain capabilties
  128. * to be left out of the table.
  129. *
  130. * @param object $capability the capability this row relates to.
  131. * @return boolean. If true, this row is omitted from the table.
  132. */
  133. protected function skip_row($capability) {
  134. return false;
  135. }
  136. /**
  137. * For subclasses to override. A change to reaturn class names that are added
  138. * to the class="" attribute on the &lt;tr> for this capability.
  139. *
  140. * @param stdClass $capability the capability this row relates to.
  141. * @return array of class name strings.
  142. */
  143. protected function get_row_classes($capability) {
  144. return array();
  145. }
  146. /**
  147. * For subclasses to override. Output the data cells for this capability. The
  148. * capability name cell will already have been output.
  149. *
  150. * You can rely on get_row_classes always being called before add_row_cells.
  151. *
  152. * @param stdClass $capability the capability this row relates to.
  153. */
  154. protected abstract function add_row_cells($capability);
  155. }