weekly release 3.9.6+
[moodle.git] / cache / renderer.php
blob38ef769901b2f843196b8658ffbafc01434b650b
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 $storeinstancesummaries information about each store instance,
44 * as returned by cache_administration_helper::get_store_instance_summaries().
45 * @param array $storepluginsummaries information about each store plugin as
46 * returned by cache_administration_helper::get_store_plugin_summaries().
47 * @return string HTML
49 public function store_instance_summariers(array $storeinstancesummaries, array $storepluginsummaries) {
50 $table = new html_table();
51 $table->head = array(
52 get_string('storename', 'cache'),
53 get_string('plugin', 'cache'),
54 get_string('storeready', 'cache'),
55 get_string('mappings', 'cache'),
56 get_string('modes', 'cache'),
57 get_string('supports', 'cache'),
58 get_string('locking', 'cache') . ' ' . $this->output->help_icon('locking', 'cache'),
59 get_string('actions', 'cache'),
61 $table->colclasses = array(
62 'storename',
63 'plugin',
64 'storeready',
65 'mappings',
66 'modes',
67 'supports',
68 'locking',
69 'actions'
71 $table->data = array();
73 $defaultstoreactions = get_string('defaultstoreactions', 'cache');
75 foreach ($storeinstancesummaries as $name => $storesummary) {
76 $actions = cache_administration_helper::get_store_instance_actions($name, $storesummary);
77 $modes = array();
78 foreach ($storesummary['modes'] as $mode => $enabled) {
79 if ($enabled) {
80 $modes[] = get_string('mode_'.$mode, 'cache');
84 $supports = array();
85 foreach ($storesummary['supports'] as $support => $enabled) {
86 if ($enabled) {
87 $supports[] = get_string('supports_'.$support, 'cache');
91 $info = '';
92 if (!empty($storesummary['default'])) {
93 $info = $this->output->pix_icon('i/info', $defaultstoreactions, '', array('class' => 'icon'));
95 $htmlactions = array();
96 foreach ($actions as $action) {
97 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
100 $isready = $storesummary['isready'] && $storesummary['requirementsmet'];
101 $readycell = new html_table_cell;
102 if ($isready) {
103 $readycell->text = $this->output->pix_icon('i/valid', '1');
106 $storename = $storesummary['name'];
107 if (!empty($storesummary['default'])) {
108 $storename = get_string('store_'.$storesummary['name'], 'cache');
110 if (!$isready && (int)$storesummary['mappings'] > 0) {
111 $readycell->text = $this->output->help_icon('storerequiresattention', 'cache');
112 $readycell->attributes['class'] = 'store-requires-attention';
115 $lock = $storesummary['lock']['name'];
116 if (!empty($storesummary['lock']['default'])) {
117 $lock = get_string($storesummary['lock']['name'], 'cache');
120 $row = new html_table_row(array(
121 $storename,
122 get_string('pluginname', 'cachestore_'.$storesummary['plugin']),
123 $readycell,
124 $storesummary['mappings'],
125 join(', ', $modes),
126 join(', ', $supports),
127 $lock,
128 $info.join(', ', $htmlactions)
130 $row->attributes['class'] = 'store-'.$name;
131 if ($storesummary['default']) {
132 $row->attributes['class'] .= ' default-store';
134 $table->data[] = $row;
137 $html = html_writer::start_tag('div', array('id' => 'core-cache-store-summaries'));
138 $html .= $this->output->heading(get_string('storesummaries', 'cache'), 3);
139 $html .= html_writer::table($table);
140 $html .= html_writer::end_tag('div');
141 return $html;
145 * Displays plugin summaries.
147 * @param array $storepluginsummaries information about each store plugin as
148 * returned by cache_administration_helper::get_store_plugin_summaries().
149 * @return string HTML
151 public function store_plugin_summaries(array $storepluginsummaries) {
152 $table = new html_table();
153 $table->head = array(
154 get_string('plugin', 'cache'),
155 get_string('storeready', 'cache'),
156 get_string('stores', 'cache'),
157 get_string('modes', 'cache'),
158 get_string('supports', 'cache'),
159 get_string('actions', 'cache'),
161 $table->colclasses = array(
162 'plugin',
163 'storeready',
164 'stores',
165 'modes',
166 'supports',
167 'actions'
169 $table->data = array();
171 foreach ($storepluginsummaries as $name => $plugin) {
172 $actions = cache_administration_helper::get_store_plugin_actions($name, $plugin);
174 $modes = array();
175 foreach ($plugin['modes'] as $mode => $enabled) {
176 if ($enabled) {
177 $modes[] = get_string('mode_'.$mode, 'cache');
181 $supports = array();
182 foreach ($plugin['supports'] as $support => $enabled) {
183 if ($enabled) {
184 $supports[] = get_string('supports_'.$support, 'cache');
188 $htmlactions = array();
189 foreach ($actions as $action) {
190 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
193 $row = new html_table_row(array(
194 $plugin['name'],
195 ($plugin['requirementsmet']) ? $this->output->pix_icon('i/valid', '1') : '',
196 $plugin['instances'],
197 join(', ', $modes),
198 join(', ', $supports),
199 join(', ', $htmlactions)
202 $row->attributes['class'] = 'plugin-'.$name;
203 $table->data[] = $row;
206 $html = html_writer::start_tag('div', array('id' => 'core-cache-plugin-summaries'));
207 $html .= $this->output->heading(get_string('pluginsummaries', 'cache'), 3);
208 $html .= html_writer::table($table);
209 $html .= html_writer::end_tag('div');
210 return $html;
214 * Displays definition summaries.
216 * @param array $definitionsummaries information about each definition, as returned by
217 * cache_administration_helper::get_definition_summaries().
218 * @param context $context the system context.
220 * @return string HTML.
222 public function definition_summaries(array $definitionsummaries, context $context) {
223 $table = new html_table();
224 $table->head = array(
225 get_string('definition', 'cache'),
226 get_string('mode', 'cache'),
227 get_string('component', 'cache'),
228 get_string('area', 'cache'),
229 get_string('mappings', 'cache'),
230 get_string('sharing', 'cache'),
231 get_string('canuselocalstore', 'cache'),
232 get_string('actions', 'cache')
234 $table->colclasses = array(
235 'definition',
236 'mode',
237 'component',
238 'area',
239 'mappings',
240 'sharing',
241 'canuselocalstore',
242 'actions'
244 $table->data = array();
246 core_collator::asort_array_of_arrays_by_key($definitionsummaries, 'name');
248 $none = new lang_string('none', 'cache');
249 foreach ($definitionsummaries as $id => $definition) {
250 $actions = cache_administration_helper::get_definition_actions($context, $definition);
251 $htmlactions = array();
252 foreach ($actions as $action) {
253 $action['url']->param('definition', $id);
254 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
256 if (!empty($definition['mappings'])) {
257 $mapping = join(', ', $definition['mappings']);
258 } else {
259 $mapping = '<em>'.$none.'</em>';
262 $uselocalcachecol = get_string('no');
263 if ($definition['mode'] != cache_store::MODE_REQUEST) {
264 if (isset($definition['canuselocalstore']) && $definition['canuselocalstore']) {
265 $uselocalcachecol = get_string('yes');
269 $row = new html_table_row(array(
270 $definition['name'],
271 get_string('mode_'.$definition['mode'], 'cache'),
272 $definition['component'],
273 $definition['area'],
274 $mapping,
275 join(', ', $definition['selectedsharingoption']),
276 $uselocalcachecol,
277 join(', ', $htmlactions)
279 $row->attributes['class'] = 'definition-'.$definition['component'].'-'.$definition['area'];
280 $table->data[] = $row;
283 $html = html_writer::start_tag('div', array('id' => 'core-cache-definition-summaries'));
284 $html .= $this->output->heading(get_string('definitionsummaries', 'cache'), 3);
285 $html .= html_writer::table($table);
287 $url = new moodle_url('/cache/admin.php', array('action' => 'rescandefinitions', 'sesskey' => sesskey()));
288 $link = html_writer::link($url, get_string('rescandefinitions', 'cache'));
289 $html .= html_writer::tag('div', $link, array('id' => 'core-cache-rescan-definitions'));
291 $html .= html_writer::end_tag('div');
292 return $html;
296 * Displays mode mappings
298 * @param string $applicationstore
299 * @param string $sessionstore
300 * @param string $requeststore
301 * @param moodle_url $editurl
302 * @return string HTML
304 public function mode_mappings($applicationstore, $sessionstore, $requeststore, moodle_url $editurl) {
305 $table = new html_table();
306 $table->colclasses = array(
307 'mode',
308 'mapping',
310 $table->rowclasses = array(
311 'mode_application',
312 'mode_session',
313 'mode_request'
315 $table->head = array(
316 get_string('mode', 'cache'),
317 get_string('mappings', 'cache'),
319 $table->data = array(
320 array(get_string('mode_'.cache_store::MODE_APPLICATION, 'cache'), $applicationstore),
321 array(get_string('mode_'.cache_store::MODE_SESSION, 'cache'), $sessionstore),
322 array(get_string('mode_'.cache_store::MODE_REQUEST, 'cache'), $requeststore)
325 $html = html_writer::start_tag('div', array('id' => 'core-cache-mode-mappings'));
326 $html .= $this->output->heading(get_string('defaultmappings', 'cache'), 3);
327 $html .= html_writer::table($table);
328 $link = html_writer::link($editurl, get_string('editmappings', 'cache'));
329 $html .= html_writer::tag('div', $link, array('class' => 'edit-link'));
330 $html .= html_writer::end_tag('div');
331 return $html;
335 * Display basic information about lock instances.
337 * @todo Add some actions so that people can configure lock instances.
339 * @param array $locks
340 * @return string
342 public function lock_summaries(array $locks) {
343 $table = new html_table();
344 $table->colclasses = array(
345 'name',
346 'type',
347 'default',
348 'uses',
349 'actions'
351 $table->rowclasses = array(
352 'lock_name',
353 'lock_type',
354 'lock_default',
355 'lock_uses',
356 'lock_actions',
358 $table->head = array(
359 get_string('lockname', 'cache'),
360 get_string('locktype', 'cache'),
361 get_string('lockdefault', 'cache'),
362 get_string('lockuses', 'cache'),
363 get_string('actions', 'cache')
365 $table->data = array();
366 $tick = $this->output->pix_icon('i/valid', '');
367 foreach ($locks as $lock) {
368 $actions = array();
369 if ($lock['uses'] === 0 && !$lock['default']) {
370 $url = new moodle_url('/cache/admin.php', array('lock' => $lock['name'], 'action' => 'deletelock', 'sesskey' => sesskey()));
371 $actions[] = html_writer::link($url, get_string('delete', 'cache'));
373 $table->data[] = new html_table_row(array(
374 new html_table_cell($lock['name']),
375 new html_table_cell($lock['type']),
376 new html_table_cell($lock['default'] ? $tick : ''),
377 new html_table_cell($lock['uses']),
378 new html_table_cell(join(' ', $actions))
382 $url = new moodle_url('/cache/admin.php', array('action' => 'newlockinstance', 'sesskey' => sesskey()));
383 $select = new single_select($url, 'lock', cache_administration_helper::get_addable_lock_options());
384 $select->label = get_string('addnewlockinstance', 'cache');
386 $html = html_writer::start_tag('div', array('id' => 'core-cache-lock-summary'));
387 $html .= $this->output->heading(get_string('locksummary', 'cache'), 3);
388 $html .= html_writer::table($table);
389 $html .= html_writer::tag('div', $this->output->render($select), array('class' => 'new-instance'));
390 $html .= html_writer::end_tag('div');
391 return $html;
395 * Renders an array of notifications for the cache configuration screen.
397 * Takes an array of notifications with the form:
398 * $notifications = array(
399 * array('This is a success message', true),
400 * array('This is a failure message', false),
401 * );
403 * @param array $notifications
404 * @return string
406 public function notifications(array $notifications = array()) {
407 if (count($notifications) === 0) {
408 // There are no notifications to render.
409 return '';
411 $html = html_writer::start_div('notifications');
412 foreach ($notifications as $notification) {
413 list($message, $notifysuccess) = $notification;
414 $html .= $this->notification($message, ($notifysuccess) ? 'notifysuccess' : 'notifyproblem');
416 $html .= html_writer::end_div();
417 return $html;