MDL-67264 core_course: Activity chooser new feature
[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 *
1baf3af3
TH
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().
8139ad13
SH
47 * @return string HTML
48 */
1baf3af3 49 public function store_instance_summariers(array $storeinstancesummaries, array $storepluginsummaries) {
8139ad13
SH
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'),
32bc8981 58 get_string('locking', 'cache') . ' ' . $this->output->help_icon('locking', 'cache'),
8139ad13
SH
59 get_string('actions', 'cache'),
60 );
61 $table->colclasses = array(
62 'storename',
63 'plugin',
64 'storeready',
65 'mappings',
66 'modes',
67 'supports',
acf49f4b 68 'locking',
8139ad13
SH
69 'actions'
70 );
71 $table->data = array();
72
73 $defaultstoreactions = get_string('defaultstoreactions', 'cache');
74
1baf3af3
TH
75 foreach ($storeinstancesummaries as $name => $storesummary) {
76 $actions = cache_administration_helper::get_store_instance_actions($name, $storesummary);
8139ad13 77 $modes = array();
1baf3af3 78 foreach ($storesummary['modes'] as $mode => $enabled) {
8139ad13
SH
79 if ($enabled) {
80 $modes[] = get_string('mode_'.$mode, 'cache');
81 }
82 }
83
84 $supports = array();
1baf3af3 85 foreach ($storesummary['supports'] as $support => $enabled) {
8139ad13
SH
86 if ($enabled) {
87 $supports[] = get_string('supports_'.$support, 'cache');
88 }
89 }
90
91 $info = '';
1baf3af3 92 if (!empty($storesummary['default'])) {
fa9c0aab 93 $info = $this->output->pix_icon('i/info', $defaultstoreactions, '', array('class' => 'icon'));
8139ad13
SH
94 }
95 $htmlactions = array();
96 foreach ($actions as $action) {
97 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
98 }
99
1baf3af3 100 $isready = $storesummary['isready'] && $storesummary['requirementsmet'];
c643628f
SH
101 $readycell = new html_table_cell;
102 if ($isready) {
103 $readycell->text = $this->output->pix_icon('i/valid', '1');
104 }
105
1baf3af3
TH
106 $storename = $storesummary['name'];
107 if (!empty($storesummary['default'])) {
108 $storename = get_string('store_'.$storesummary['name'], 'cache');
8139ad13 109 }
1baf3af3 110 if (!$isready && (int)$storesummary['mappings'] > 0) {
c643628f
SH
111 $readycell->text = $this->output->help_icon('storerequiresattention', 'cache');
112 $readycell->attributes['class'] = 'store-requires-attention';
113 }
8139ad13 114
1baf3af3
TH
115 $lock = $storesummary['lock']['name'];
116 if (!empty($storesummary['lock']['default'])) {
117 $lock = get_string($storesummary['lock']['name'], 'cache');
acf49f4b
SH
118 }
119
8139ad13
SH
120 $row = new html_table_row(array(
121 $storename,
1baf3af3 122 get_string('pluginname', 'cachestore_'.$storesummary['plugin']),
c643628f 123 $readycell,
1baf3af3 124 $storesummary['mappings'],
8139ad13
SH
125 join(', ', $modes),
126 join(', ', $supports),
acf49f4b 127 $lock,
8139ad13
SH
128 $info.join(', ', $htmlactions)
129 ));
130 $row->attributes['class'] = 'store-'.$name;
1baf3af3 131 if ($storesummary['default']) {
8139ad13
SH
132 $row->attributes['class'] .= ' default-store';
133 }
134 $table->data[] = $row;
135 }
136
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;
142 }
143
144 /**
1baf3af3 145 * Displays plugin summaries.
8139ad13 146 *
1baf3af3
TH
147 * @param array $storepluginsummaries information about each store plugin as
148 * returned by cache_administration_helper::get_store_plugin_summaries().
8139ad13
SH
149 * @return string HTML
150 */
1baf3af3 151 public function store_plugin_summaries(array $storepluginsummaries) {
8139ad13
SH
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'),
160 );
161 $table->colclasses = array(
162 'plugin',
163 'storeready',
164 'stores',
165 'modes',
166 'supports',
167 'actions'
168 );
169 $table->data = array();
170
1baf3af3 171 foreach ($storepluginsummaries as $name => $plugin) {
26ce56fd 172 $actions = cache_administration_helper::get_store_plugin_actions($name, $plugin);
8139ad13
SH
173
174 $modes = array();
175 foreach ($plugin['modes'] as $mode => $enabled) {
176 if ($enabled) {
177 $modes[] = get_string('mode_'.$mode, 'cache');
178 }
179 }
180
181 $supports = array();
182 foreach ($plugin['supports'] as $support => $enabled) {
183 if ($enabled) {
184 $supports[] = get_string('supports_'.$support, 'cache');
185 }
186 }
187
188 $htmlactions = array();
189 foreach ($actions as $action) {
190 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
191 }
192
193 $row = new html_table_row(array(
194 $plugin['name'],
c3847443 195 ($plugin['requirementsmet']) ? $this->output->pix_icon('i/valid', '1') : '',
8139ad13
SH
196 $plugin['instances'],
197 join(', ', $modes),
198 join(', ', $supports),
199 join(', ', $htmlactions)
200 ));
201
202 $row->attributes['class'] = 'plugin-'.$name;
203 $table->data[] = $row;
204 }
205
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;
211 }
212
213 /**
1baf3af3 214 * Displays definition summaries.
8139ad13 215 *
1baf3af3
TH
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.
219 *
220 * @return string HTML.
8139ad13 221 */
1baf3af3 222 public function definition_summaries(array $definitionsummaries, context $context) {
8139ad13
SH
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'),
46e17f04 230 get_string('sharing', 'cache'),
8e86a8d2
MN
231 get_string('canuselocalstore', 'cache'),
232 get_string('actions', 'cache')
8139ad13
SH
233 );
234 $table->colclasses = array(
235 'definition',
236 'mode',
237 'component',
238 'area',
239 'mappings',
46e17f04 240 'sharing',
8e86a8d2 241 'canuselocalstore',
8139ad13
SH
242 'actions'
243 );
42f5d164 244 $table->data = array();
8139ad13 245
1baf3af3 246 core_collator::asort_array_of_arrays_by_key($definitionsummaries, 'name');
6e247996 247
170f821b 248 $none = new lang_string('none', 'cache');
1baf3af3 249 foreach ($definitionsummaries as $id => $definition) {
54519330 250 $actions = cache_administration_helper::get_definition_actions($context, $definition);
8139ad13
SH
251 $htmlactions = array();
252 foreach ($actions as $action) {
253 $action['url']->param('definition', $id);
254 $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
255 }
170f821b
SH
256 if (!empty($definition['mappings'])) {
257 $mapping = join(', ', $definition['mappings']);
258 } else {
259 $mapping = '<em>'.$none.'</em>';
260 }
8139ad13 261
8e86a8d2
MN
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');
266 }
267 }
268
8139ad13
SH
269 $row = new html_table_row(array(
270 $definition['name'],
271 get_string('mode_'.$definition['mode'], 'cache'),
272 $definition['component'],
273 $definition['area'],
170f821b 274 $mapping,
46e17f04 275 join(', ', $definition['selectedsharingoption']),
8e86a8d2 276 $uselocalcachecol,
8139ad13
SH
277 join(', ', $htmlactions)
278 ));
279 $row->attributes['class'] = 'definition-'.$definition['component'].'-'.$definition['area'];
280 $table->data[] = $row;
281 }
282
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);
286
170f821b
SH
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'));
8139ad13
SH
290
291 $html .= html_writer::end_tag('div');
292 return $html;
293 }
294
295 /**
296 * Displays mode mappings
297 *
298 * @param string $applicationstore
299 * @param string $sessionstore
300 * @param string $requeststore
301 * @param moodle_url $editurl
302 * @return string HTML
303 */
304 public function mode_mappings($applicationstore, $sessionstore, $requeststore, moodle_url $editurl) {
305 $table = new html_table();
306 $table->colclasses = array(
307 'mode',
308 'mapping',
309 );
310 $table->rowclasses = array(
311 'mode_application',
312 'mode_session',
313 'mode_request'
314 );
315 $table->head = array(
316 get_string('mode', 'cache'),
317 get_string('mappings', 'cache'),
318 );
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)
323 );
324
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);
170f821b
SH
328 $link = html_writer::link($editurl, get_string('editmappings', 'cache'));
329 $html .= html_writer::tag('div', $link, array('class' => 'edit-link'));
8139ad13
SH
330 $html .= html_writer::end_tag('div');
331 return $html;
332 }
167ad91e
SH
333
334 /**
335 * Display basic information about lock instances.
336 *
337 * @todo Add some actions so that people can configure lock instances.
338 *
339 * @param array $locks
340 * @return string
341 */
342 public function lock_summaries(array $locks) {
343 $table = new html_table();
344 $table->colclasses = array(
345 'name',
acf49f4b 346 'type',
167ad91e
SH
347 'default',
348 'uses',
acf49f4b 349 'actions'
167ad91e
SH
350 );
351 $table->rowclasses = array(
352 'lock_name',
acf49f4b 353 'lock_type',
167ad91e
SH
354 'lock_default',
355 'lock_uses',
acf49f4b 356 'lock_actions',
167ad91e
SH
357 );
358 $table->head = array(
359 get_string('lockname', 'cache'),
acf49f4b 360 get_string('locktype', 'cache'),
167ad91e
SH
361 get_string('lockdefault', 'cache'),
362 get_string('lockuses', 'cache'),
acf49f4b 363 get_string('actions', 'cache')
167ad91e
SH
364 );
365 $table->data = array();
c3847443 366 $tick = $this->output->pix_icon('i/valid', '');
167ad91e 367 foreach ($locks as $lock) {
acf49f4b
SH
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'));
372 }
167ad91e
SH
373 $table->data[] = new html_table_row(array(
374 new html_table_cell($lock['name']),
acf49f4b 375 new html_table_cell($lock['type']),
167ad91e
SH
376 new html_table_cell($lock['default'] ? $tick : ''),
377 new html_table_cell($lock['uses']),
acf49f4b 378 new html_table_cell(join(' ', $actions))
167ad91e
SH
379 ));
380 }
381
acf49f4b
SH
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');
385
167ad91e
SH
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);
acf49f4b 389 $html .= html_writer::tag('div', $this->output->render($select), array('class' => 'new-instance'));
167ad91e
SH
390 $html .= html_writer::end_tag('div');
391 return $html;
392 }
727c2173
SH
393
394 /**
395 * Renders an array of notifications for the cache configuration screen.
396 *
915140c9
SH
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 * );
402 *
727c2173
SH
403 * @param array $notifications
404 * @return string
405 */
915140c9 406 public function notifications(array $notifications = array()) {
727c2173
SH
407 if (count($notifications) === 0) {
408 // There are no notifications to render.
409 return '';
410 }
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');
415 }
416 $html .= html_writer::end_div();
417 return $html;
418 }
1baf3af3 419}