MDL-45828 cache: definitions now sorted in the admin UI
[moodle.git] / cache / renderer.php
CommitLineData
8139ad13
SH
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 * The Cache renderer.
19 *
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 *
22 * @package core
23 * @category cache
24 * @copyright 2012 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * The cache renderer (mainly admin interfaces).
32 *
33 * @package core
34 * @category cache
35 * @copyright 2012 Sam Hemelryk
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class core_cache_renderer extends plugin_renderer_base {
39
40 /**
41 * Displays store summaries.
42 *
43 * @param array $stores
44 * @param array $plugins
45 * @return string HTML
46 */
26ce56fd 47 public function store_instance_summariers(array $stores, array $plugins) {
8139ad13
SH
48 $table = new html_table();
49 $table->head = array(
50 get_string('storename', 'cache'),
51 get_string('plugin', 'cache'),
52 get_string('storeready', 'cache'),
53 get_string('mappings', 'cache'),
54 get_string('modes', 'cache'),
55 get_string('supports', 'cache'),
acf49f4b 56 get_string('lockingmeans', 'cache'),
8139ad13
SH
57 get_string('actions', 'cache'),
58 );
59 $table->colclasses = array(
60 'storename',
61 'plugin',
62 'storeready',
63 'mappings',
64 'modes',
65 'supports',
acf49f4b 66 'locking',
8139ad13
SH
67 'actions'
68 );
69 $table->data = array();
70
71 $defaultstoreactions = get_string('defaultstoreactions', 'cache');
72
73 foreach ($stores as $name => $store) {
26ce56fd 74 $actions = cache_administration_helper::get_store_instance_actions($name, $store);
8139ad13
SH
75 $modes = array();
76 foreach ($store['modes'] as $mode => $enabled) {
77 if ($enabled) {
78 $modes[] = get_string('mode_'.$mode, 'cache');
79 }
80 }
81
82 $supports = array();
83 foreach ($store['supports'] as $support => $enabled) {
84 if ($enabled) {
85 $supports[] = get_string('supports_'.$support, 'cache');
86 }
87 }
88
89 $info = '';
90 if (!empty($store['default'])) {
fa9c0aab 91 $info = $this->output->pix_icon('i/info', $defaultstoreactions, '', array('class' => 'icon'));
8139ad13
SH
92 }
93 $htmlactions = array();
94 foreach ($actions as $action) {
95 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
96 }
97
c643628f
SH
98 $isready = $store['isready'] && $store['requirementsmet'];
99 $readycell = new html_table_cell;
100 if ($isready) {
101 $readycell->text = $this->output->pix_icon('i/valid', '1');
102 }
103
8139ad13
SH
104 $storename = $store['name'];
105 if (!empty($store['default'])) {
106 $storename = get_string('store_'.$store['name'], 'cache');
107 }
c643628f
SH
108 if (!$isready && (int)$store['mappings'] > 0) {
109 $readycell->text = $this->output->help_icon('storerequiresattention', 'cache');
110 $readycell->attributes['class'] = 'store-requires-attention';
111 }
8139ad13 112
acf49f4b
SH
113 $lock = $store['lock']['name'];
114 if (!empty($store['lock']['default'])) {
115 $lock = get_string($store['lock']['name'], 'cache');
116 }
117
8139ad13
SH
118 $row = new html_table_row(array(
119 $storename,
6fec1820 120 get_string('pluginname', 'cachestore_'.$store['plugin']),
c643628f 121 $readycell,
8139ad13
SH
122 $store['mappings'],
123 join(', ', $modes),
124 join(', ', $supports),
acf49f4b 125 $lock,
8139ad13
SH
126 $info.join(', ', $htmlactions)
127 ));
128 $row->attributes['class'] = 'store-'.$name;
129 if ($store['default']) {
130 $row->attributes['class'] .= ' default-store';
131 }
132 $table->data[] = $row;
133 }
134
135 $html = html_writer::start_tag('div', array('id' => 'core-cache-store-summaries'));
136 $html .= $this->output->heading(get_string('storesummaries', 'cache'), 3);
137 $html .= html_writer::table($table);
138 $html .= html_writer::end_tag('div');
139 return $html;
140 }
141
142 /**
143 * Displays plugin summaries
144 *
145 * @param array $plugins
146 * @return string HTML
147 */
26ce56fd 148 public function store_plugin_summaries(array $plugins) {
8139ad13
SH
149 $table = new html_table();
150 $table->head = array(
151 get_string('plugin', 'cache'),
152 get_string('storeready', 'cache'),
153 get_string('stores', 'cache'),
154 get_string('modes', 'cache'),
155 get_string('supports', 'cache'),
156 get_string('actions', 'cache'),
157 );
158 $table->colclasses = array(
159 'plugin',
160 'storeready',
161 'stores',
162 'modes',
163 'supports',
164 'actions'
165 );
166 $table->data = array();
167
168 foreach ($plugins as $name => $plugin) {
26ce56fd 169 $actions = cache_administration_helper::get_store_plugin_actions($name, $plugin);
8139ad13
SH
170
171 $modes = array();
172 foreach ($plugin['modes'] as $mode => $enabled) {
173 if ($enabled) {
174 $modes[] = get_string('mode_'.$mode, 'cache');
175 }
176 }
177
178 $supports = array();
179 foreach ($plugin['supports'] as $support => $enabled) {
180 if ($enabled) {
181 $supports[] = get_string('supports_'.$support, 'cache');
182 }
183 }
184
185 $htmlactions = array();
186 foreach ($actions as $action) {
187 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
188 }
189
190 $row = new html_table_row(array(
191 $plugin['name'],
c3847443 192 ($plugin['requirementsmet']) ? $this->output->pix_icon('i/valid', '1') : '',
8139ad13
SH
193 $plugin['instances'],
194 join(', ', $modes),
195 join(', ', $supports),
196 join(', ', $htmlactions)
197 ));
198
199 $row->attributes['class'] = 'plugin-'.$name;
200 $table->data[] = $row;
201 }
202
203 $html = html_writer::start_tag('div', array('id' => 'core-cache-plugin-summaries'));
204 $html .= $this->output->heading(get_string('pluginsummaries', 'cache'), 3);
205 $html .= html_writer::table($table);
206 $html .= html_writer::end_tag('div');
207 return $html;
208 }
209
210 /**
211 * Displays definition summaries
212 *
213 * @param array $definitions
8139ad13
SH
214 * @return string HTML
215 */
54519330 216 public function definition_summaries(array $definitions, context $context) {
8139ad13
SH
217 $table = new html_table();
218 $table->head = array(
219 get_string('definition', 'cache'),
220 get_string('mode', 'cache'),
221 get_string('component', 'cache'),
222 get_string('area', 'cache'),
223 get_string('mappings', 'cache'),
46e17f04 224 get_string('sharing', 'cache'),
8139ad13
SH
225 get_string('actions', 'cache'),
226 );
227 $table->colclasses = array(
228 'definition',
229 'mode',
230 'component',
231 'area',
232 'mappings',
46e17f04 233 'sharing',
8139ad13
SH
234 'actions'
235 );
42f5d164 236 $table->data = array();
8139ad13 237
6e247996
SH
238 core_collator::asort_array_of_arrays_by_key($definitions, 'name');
239
170f821b 240 $none = new lang_string('none', 'cache');
8139ad13 241 foreach ($definitions as $id => $definition) {
54519330 242 $actions = cache_administration_helper::get_definition_actions($context, $definition);
8139ad13
SH
243 $htmlactions = array();
244 foreach ($actions as $action) {
245 $action['url']->param('definition', $id);
246 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
247 }
170f821b
SH
248 if (!empty($definition['mappings'])) {
249 $mapping = join(', ', $definition['mappings']);
250 } else {
251 $mapping = '<em>'.$none.'</em>';
252 }
8139ad13
SH
253
254 $row = new html_table_row(array(
255 $definition['name'],
256 get_string('mode_'.$definition['mode'], 'cache'),
257 $definition['component'],
258 $definition['area'],
170f821b 259 $mapping,
46e17f04 260 join(', ', $definition['selectedsharingoption']),
8139ad13
SH
261 join(', ', $htmlactions)
262 ));
263 $row->attributes['class'] = 'definition-'.$definition['component'].'-'.$definition['area'];
264 $table->data[] = $row;
265 }
266
267 $html = html_writer::start_tag('div', array('id' => 'core-cache-definition-summaries'));
268 $html .= $this->output->heading(get_string('definitionsummaries', 'cache'), 3);
269 $html .= html_writer::table($table);
270
170f821b
SH
271 $url = new moodle_url('/cache/admin.php', array('action' => 'rescandefinitions', 'sesskey' => sesskey()));
272 $link = html_writer::link($url, get_string('rescandefinitions', 'cache'));
273 $html .= html_writer::tag('div', $link, array('id' => 'core-cache-rescan-definitions'));
8139ad13
SH
274
275 $html .= html_writer::end_tag('div');
276 return $html;
277 }
278
279 /**
280 * Displays mode mappings
281 *
282 * @param string $applicationstore
283 * @param string $sessionstore
284 * @param string $requeststore
285 * @param moodle_url $editurl
286 * @return string HTML
287 */
288 public function mode_mappings($applicationstore, $sessionstore, $requeststore, moodle_url $editurl) {
289 $table = new html_table();
290 $table->colclasses = array(
291 'mode',
292 'mapping',
293 );
294 $table->rowclasses = array(
295 'mode_application',
296 'mode_session',
297 'mode_request'
298 );
299 $table->head = array(
300 get_string('mode', 'cache'),
301 get_string('mappings', 'cache'),
302 );
303 $table->data = array(
304 array(get_string('mode_'.cache_store::MODE_APPLICATION, 'cache'), $applicationstore),
305 array(get_string('mode_'.cache_store::MODE_SESSION, 'cache'), $sessionstore),
306 array(get_string('mode_'.cache_store::MODE_REQUEST, 'cache'), $requeststore)
307 );
308
309 $html = html_writer::start_tag('div', array('id' => 'core-cache-mode-mappings'));
310 $html .= $this->output->heading(get_string('defaultmappings', 'cache'), 3);
311 $html .= html_writer::table($table);
170f821b
SH
312 $link = html_writer::link($editurl, get_string('editmappings', 'cache'));
313 $html .= html_writer::tag('div', $link, array('class' => 'edit-link'));
8139ad13
SH
314 $html .= html_writer::end_tag('div');
315 return $html;
316 }
167ad91e
SH
317
318 /**
319 * Display basic information about lock instances.
320 *
321 * @todo Add some actions so that people can configure lock instances.
322 *
323 * @param array $locks
324 * @return string
325 */
326 public function lock_summaries(array $locks) {
327 $table = new html_table();
328 $table->colclasses = array(
329 'name',
acf49f4b 330 'type',
167ad91e
SH
331 'default',
332 'uses',
acf49f4b 333 'actions'
167ad91e
SH
334 );
335 $table->rowclasses = array(
336 'lock_name',
acf49f4b 337 'lock_type',
167ad91e
SH
338 'lock_default',
339 'lock_uses',
acf49f4b 340 'lock_actions',
167ad91e
SH
341 );
342 $table->head = array(
343 get_string('lockname', 'cache'),
acf49f4b 344 get_string('locktype', 'cache'),
167ad91e
SH
345 get_string('lockdefault', 'cache'),
346 get_string('lockuses', 'cache'),
acf49f4b 347 get_string('actions', 'cache')
167ad91e
SH
348 );
349 $table->data = array();
c3847443 350 $tick = $this->output->pix_icon('i/valid', '');
167ad91e 351 foreach ($locks as $lock) {
acf49f4b
SH
352 $actions = array();
353 if ($lock['uses'] === 0 && !$lock['default']) {
354 $url = new moodle_url('/cache/admin.php', array('lock' => $lock['name'], 'action' => 'deletelock', 'sesskey' => sesskey()));
355 $actions[] = html_writer::link($url, get_string('delete', 'cache'));
356 }
167ad91e
SH
357 $table->data[] = new html_table_row(array(
358 new html_table_cell($lock['name']),
acf49f4b 359 new html_table_cell($lock['type']),
167ad91e
SH
360 new html_table_cell($lock['default'] ? $tick : ''),
361 new html_table_cell($lock['uses']),
acf49f4b 362 new html_table_cell(join(' ', $actions))
167ad91e
SH
363 ));
364 }
365
acf49f4b
SH
366 $url = new moodle_url('/cache/admin.php', array('action' => 'newlockinstance', 'sesskey' => sesskey()));
367 $select = new single_select($url, 'lock', cache_administration_helper::get_addable_lock_options());
368 $select->label = get_string('addnewlockinstance', 'cache');
369
167ad91e
SH
370 $html = html_writer::start_tag('div', array('id' => 'core-cache-lock-summary'));
371 $html .= $this->output->heading(get_string('locksummary', 'cache'), 3);
372 $html .= html_writer::table($table);
acf49f4b 373 $html .= html_writer::tag('div', $this->output->render($select), array('class' => 'new-instance'));
167ad91e
SH
374 $html .= html_writer::end_tag('div');
375 return $html;
376 }
8139ad13 377}