weekly release 2.8.9+
[moodle.git] / cache / renderer.php
blob1ccb4bbd78aefa6aae190f1bb64dedbedc30d5f7
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.
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/>.
17 /**
18 * The Cache renderer.
20 * This file is part of Moodle's cache API, affectionately called MUC.
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
28 defined('MOODLE_INTERNAL') || die();
30 /**
31 * The cache renderer (mainly admin interfaces).
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
38 class core_cache_renderer extends plugin_renderer_base {
40 /**
41 * Displays store summaries.
43 * @param array $stores
44 * @param array $plugins
45 * @return string HTML
47 public function store_instance_summariers(array $stores, array $plugins) {
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'),
56 get_string('lockingmeans', 'cache'),
57 get_string('actions', 'cache'),
59 $table->colclasses = array(
60 'storename',
61 'plugin',
62 'storeready',
63 'mappings',
64 'modes',
65 'supports',
66 'locking',
67 'actions'
69 $table->data = array();
71 $defaultstoreactions = get_string('defaultstoreactions', 'cache');
73 foreach ($stores as $name => $store) {
74 $actions = cache_administration_helper::get_store_instance_actions($name, $store);
75 $modes = array();
76 foreach ($store['modes'] as $mode => $enabled) {
77 if ($enabled) {
78 $modes[] = get_string('mode_'.$mode, 'cache');
82 $supports = array();
83 foreach ($store['supports'] as $support => $enabled) {
84 if ($enabled) {
85 $supports[] = get_string('supports_'.$support, 'cache');
89 $info = '';
90 if (!empty($store['default'])) {
91 $info = $this->output->pix_icon('i/info', $defaultstoreactions, '', array('class' => 'icon'));
93 $htmlactions = array();
94 foreach ($actions as $action) {
95 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
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');
104 $storename = $store['name'];
105 if (!empty($store['default'])) {
106 $storename = get_string('store_'.$store['name'], 'cache');
108 if (!$isready && (int)$store['mappings'] > 0) {
109 $readycell->text = $this->output->help_icon('storerequiresattention', 'cache');
110 $readycell->attributes['class'] = 'store-requires-attention';
113 $lock = $store['lock']['name'];
114 if (!empty($store['lock']['default'])) {
115 $lock = get_string($store['lock']['name'], 'cache');
118 $row = new html_table_row(array(
119 $storename,
120 get_string('pluginname', 'cachestore_'.$store['plugin']),
121 $readycell,
122 $store['mappings'],
123 join(', ', $modes),
124 join(', ', $supports),
125 $lock,
126 $info.join(', ', $htmlactions)
128 $row->attributes['class'] = 'store-'.$name;
129 if ($store['default']) {
130 $row->attributes['class'] .= ' default-store';
132 $table->data[] = $row;
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;
143 * Displays plugin summaries
145 * @param array $plugins
146 * @return string HTML
148 public function store_plugin_summaries(array $plugins) {
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'),
158 $table->colclasses = array(
159 'plugin',
160 'storeready',
161 'stores',
162 'modes',
163 'supports',
164 'actions'
166 $table->data = array();
168 foreach ($plugins as $name => $plugin) {
169 $actions = cache_administration_helper::get_store_plugin_actions($name, $plugin);
171 $modes = array();
172 foreach ($plugin['modes'] as $mode => $enabled) {
173 if ($enabled) {
174 $modes[] = get_string('mode_'.$mode, 'cache');
178 $supports = array();
179 foreach ($plugin['supports'] as $support => $enabled) {
180 if ($enabled) {
181 $supports[] = get_string('supports_'.$support, 'cache');
185 $htmlactions = array();
186 foreach ($actions as $action) {
187 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
190 $row = new html_table_row(array(
191 $plugin['name'],
192 ($plugin['requirementsmet']) ? $this->output->pix_icon('i/valid', '1') : '',
193 $plugin['instances'],
194 join(', ', $modes),
195 join(', ', $supports),
196 join(', ', $htmlactions)
199 $row->attributes['class'] = 'plugin-'.$name;
200 $table->data[] = $row;
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;
211 * Displays definition summaries
213 * @param array $definitions
214 * @return string HTML
216 public function definition_summaries(array $definitions, context $context) {
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'),
224 get_string('sharing', 'cache'),
225 get_string('actions', 'cache'),
227 $table->colclasses = array(
228 'definition',
229 'mode',
230 'component',
231 'area',
232 'mappings',
233 'sharing',
234 'actions'
236 $table->data = array();
238 core_collator::asort_array_of_arrays_by_key($definitions, 'name');
240 $none = new lang_string('none', 'cache');
241 foreach ($definitions as $id => $definition) {
242 $actions = cache_administration_helper::get_definition_actions($context, $definition);
243 $htmlactions = array();
244 foreach ($actions as $action) {
245 $action['url']->param('definition', $id);
246 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
248 if (!empty($definition['mappings'])) {
249 $mapping = join(', ', $definition['mappings']);
250 } else {
251 $mapping = '<em>'.$none.'</em>';
254 $row = new html_table_row(array(
255 $definition['name'],
256 get_string('mode_'.$definition['mode'], 'cache'),
257 $definition['component'],
258 $definition['area'],
259 $mapping,
260 join(', ', $definition['selectedsharingoption']),
261 join(', ', $htmlactions)
263 $row->attributes['class'] = 'definition-'.$definition['component'].'-'.$definition['area'];
264 $table->data[] = $row;
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);
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'));
275 $html .= html_writer::end_tag('div');
276 return $html;
280 * Displays mode mappings
282 * @param string $applicationstore
283 * @param string $sessionstore
284 * @param string $requeststore
285 * @param moodle_url $editurl
286 * @return string HTML
288 public function mode_mappings($applicationstore, $sessionstore, $requeststore, moodle_url $editurl) {
289 $table = new html_table();
290 $table->colclasses = array(
291 'mode',
292 'mapping',
294 $table->rowclasses = array(
295 'mode_application',
296 'mode_session',
297 'mode_request'
299 $table->head = array(
300 get_string('mode', 'cache'),
301 get_string('mappings', 'cache'),
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)
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);
312 $link = html_writer::link($editurl, get_string('editmappings', 'cache'));
313 $html .= html_writer::tag('div', $link, array('class' => 'edit-link'));
314 $html .= html_writer::end_tag('div');
315 return $html;
319 * Display basic information about lock instances.
321 * @todo Add some actions so that people can configure lock instances.
323 * @param array $locks
324 * @return string
326 public function lock_summaries(array $locks) {
327 $table = new html_table();
328 $table->colclasses = array(
329 'name',
330 'type',
331 'default',
332 'uses',
333 'actions'
335 $table->rowclasses = array(
336 'lock_name',
337 'lock_type',
338 'lock_default',
339 'lock_uses',
340 'lock_actions',
342 $table->head = array(
343 get_string('lockname', 'cache'),
344 get_string('locktype', 'cache'),
345 get_string('lockdefault', 'cache'),
346 get_string('lockuses', 'cache'),
347 get_string('actions', 'cache')
349 $table->data = array();
350 $tick = $this->output->pix_icon('i/valid', '');
351 foreach ($locks as $lock) {
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'));
357 $table->data[] = new html_table_row(array(
358 new html_table_cell($lock['name']),
359 new html_table_cell($lock['type']),
360 new html_table_cell($lock['default'] ? $tick : ''),
361 new html_table_cell($lock['uses']),
362 new html_table_cell(join(' ', $actions))
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');
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);
373 $html .= html_writer::tag('div', $this->output->render($select), array('class' => 'new-instance'));
374 $html .= html_writer::end_tag('div');
375 return $html;
379 * Renders an array of notifications for the cache configuration screen.
381 * Takes an array of notifications with the form:
382 * $notifications = array(
383 * array('This is a success message', true),
384 * array('This is a failure message', false),
385 * );
387 * @param array $notifications
388 * @return string
390 public function notifications(array $notifications = array()) {
391 if (count($notifications) === 0) {
392 // There are no notifications to render.
393 return '';
395 $html = html_writer::start_div('notifications');
396 foreach ($notifications as $notification) {
397 list($message, $notifysuccess) = $notification;
398 $html .= $this->notification($message, ($notifysuccess) ? 'notifysuccess' : 'notifyproblem');
400 $html .= html_writer::end_div();
401 return $html;