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 | /** |
---|
18 | * Base capability table. |
---|
19 | * |
---|
20 | * @package core_role |
---|
21 | * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) |
---|
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
---|
23 | */ |
---|
24 | |
---|
25 | defined('MOODLE_INTERNAL') || die(); |
---|
26 | |
---|
27 | /** |
---|
28 | * This class represents a table with one row for each of a list of capabilities |
---|
29 | * where the first cell in the row contains the capability name, and there is |
---|
30 | * arbitrary stuff in the rest of the row. This class is used by |
---|
31 | * admin/roles/manage.php, override.php and check.php. |
---|
32 | * |
---|
33 | * An ajaxy search UI shown at the top, if JavaScript is on. |
---|
34 | */ |
---|
35 | abstract class core_role_capability_table_base { |
---|
36 | /** The context this table relates to. */ |
---|
37 | protected $context; |
---|
38 | |
---|
39 | /** The capabilities to display. Initialised as $context->get_capabilities(). */ |
---|
40 | protected $capabilities = array(); |
---|
41 | |
---|
42 | /** Added as an id="" attribute to the table on output. */ |
---|
43 | protected $id; |
---|
44 | |
---|
45 | /** Added to the class="" attribute on output. */ |
---|
46 | protected $classes = array('rolecap'); |
---|
47 | |
---|
48 | /** Default number of capabilities in the table for the search UI to be shown. */ |
---|
49 | const NUM_CAPS_FOR_SEARCH = 12; |
---|
50 | |
---|
51 | /** |
---|
52 | * Constructor. |
---|
53 | * @param context $context the context this table relates to. |
---|
54 | * @param string $id what to put in the id="" attribute. |
---|
55 | */ |
---|
56 | public function __construct(context $context, $id) { |
---|
57 | $this->context = $context; |
---|
58 | $this->capabilities = $context->get_capabilities(); |
---|
59 | $this->id = $id; |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Use this to add class="" attributes to the table. You get the rolecap by |
---|
64 | * default. |
---|
65 | * @param array $classnames of class names. |
---|
66 | */ |
---|
67 | public function add_classes($classnames) { |
---|
68 | $this->classes = array_unique(array_merge($this->classes, $classnames)); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Display the table. |
---|
73 | */ |
---|
74 | public function display() { |
---|
75 | if (count($this->capabilities) > self::NUM_CAPS_FOR_SEARCH) { |
---|
76 | global $PAGE; |
---|
77 | $jsmodule = array( |
---|
78 | 'name' => 'rolescapfilter', |
---|
79 | 'fullpath' => '/admin/roles/module.js', |
---|
80 | 'strings' => array( |
---|
81 | array('filter', 'moodle'), |
---|
82 | array('clear', 'moodle'), ), |
---|
83 | 'requires' => array('node', 'cookie', 'escape') |
---|
84 | ); |
---|
85 | $PAGE->requires->js_init_call('M.core_role.init_cap_table_filter', array($this->id, $this->context->id), false, |
---|
86 | $jsmodule); |
---|
87 | } |
---|
88 | echo '<table class="' . implode(' ', $this->classes) . '" id="' . $this->id . '">' . "\n<thead>\n"; |
---|
89 | echo '<tr><th class="name" align="left" scope="col">' . get_string('capability', 'core_role') . '</th>'; |
---|
90 | $this->add_header_cells(); |
---|
91 | echo "</tr>\n</thead>\n<tbody>\n"; |
---|
92 | |
---|
93 | // Loop over capabilities. |
---|
94 | $contextlevel = 0; |
---|
95 | $component = ''; |
---|
96 | foreach ($this->capabilities as $capability) { |
---|
97 | if ($this->skip_row($capability)) { |
---|
98 | continue; |
---|
99 | } |
---|
100 | |
---|
101 | // Prints a breaker if component or name or context level has changed. |
---|
102 | if (component_level_changed($capability, $component, $contextlevel)) { |
---|
103 | $this->print_heading_row($capability); |
---|
104 | } |
---|
105 | $contextlevel = $capability->contextlevel; |
---|
106 | $component = $capability->component; |
---|
107 | |
---|
108 | // Start the row. |
---|
109 | echo '<tr class="' . implode(' ', array_unique(array_merge(array('rolecap'), |
---|
110 | $this->get_row_classes($capability)))) . '">'; |
---|
111 | |
---|
112 | // Table cell for the capability name. |
---|
113 | echo '<th scope="row" class="name"><span class="cap-desc">' . get_capability_docs_link($capability) . |
---|
114 | '<span class="cap-name">' . $capability->name . '</span></span></th>'; |
---|
115 | |
---|
116 | // Add the cells specific to this table. |
---|
117 | $this->add_row_cells($capability); |
---|
118 | |
---|
119 | // End the row. |
---|
120 | echo "</tr>\n"; |
---|
121 | } |
---|
122 | |
---|
123 | // End of the table. |
---|
124 | echo "</tbody>\n</table>\n"; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Used to output a heading rows when the context level or component changes. |
---|
129 | * @param stdClass $capability gives the new component and contextlevel. |
---|
130 | */ |
---|
131 | protected function print_heading_row($capability) { |
---|
132 | echo '<tr class="rolecapheading header"><td colspan="' . (1 + $this->num_extra_columns()) . '" class="header"><strong>' . |
---|
133 | get_component_string($capability->component, $capability->contextlevel) . |
---|
134 | '</strong></td></tr>'; |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * For subclasses to override, output header cells, after the initial capability one. |
---|
140 | */ |
---|
141 | protected abstract function add_header_cells(); |
---|
142 | |
---|
143 | /** |
---|
144 | * For subclasses to override, return the number of cells that add_header_cells/add_row_cells output. |
---|
145 | */ |
---|
146 | protected abstract function num_extra_columns(); |
---|
147 | |
---|
148 | /** |
---|
149 | * For subclasses to override. Allows certain capabilties |
---|
150 | * to be left out of the table. |
---|
151 | * |
---|
152 | * @param object $capability the capability this row relates to. |
---|
153 | * @return boolean. If true, this row is omitted from the table. |
---|
154 | */ |
---|
155 | protected function skip_row($capability) { |
---|
156 | return false; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * For subclasses to override. A change to reaturn class names that are added |
---|
161 | * to the class="" attribute on the <tr> for this capability. |
---|
162 | * |
---|
163 | * @param stdClass $capability the capability this row relates to. |
---|
164 | * @return array of class name strings. |
---|
165 | */ |
---|
166 | protected function get_row_classes($capability) { |
---|
167 | return array(); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * For subclasses to override. Output the data cells for this capability. The |
---|
172 | * capability name cell will already have been output. |
---|
173 | * |
---|
174 | * You can rely on get_row_classes always being called before add_row_cells. |
---|
175 | * |
---|
176 | * @param stdClass $capability the capability this row relates to. |
---|
177 | */ |
---|
178 | protected abstract function add_row_cells($capability); |
---|
179 | } |
---|