Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/cache/ -> renderer.php (source)

Differences Between: [Versions 39 and 310] [Versions 39 and 311]

   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  
  28  defined('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   */
  38  class core_cache_renderer extends plugin_renderer_base {
  39  
  40      /**
  41       * Displays store summaries.
  42       *
  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
  48       */
  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'),
  60          );
  61          $table->colclasses = array(
  62              'storename',
  63              'plugin',
  64              'storeready',
  65              'mappings',
  66              'modes',
  67              'supports',
  68              'locking',
  69              'actions'
  70          );
  71          $table->data = array();
  72  
  73          $defaultstoreactions = get_string('defaultstoreactions', 'cache');
  74  
  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');
  81                  }
  82              }
  83  
  84              $supports = array();
  85              foreach ($storesummary['supports'] as $support => $enabled) {
  86                  if ($enabled) {
  87                      $supports[] = get_string('supports_'.$support, 'cache');
  88                  }
  89              }
  90  
  91              $info = '';
  92              if (!empty($storesummary['default'])) {
  93                  $info = $this->output->pix_icon('i/info', $defaultstoreactions, '', array('class' => 'icon'));
  94              }
  95              $htmlactions = array();
  96              foreach ($actions as $action) {
  97                  $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
  98              }
  99  
 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');
 104              }
 105  
 106              $storename = $storesummary['name'];
 107              if (!empty($storesummary['default'])) {
 108                  $storename = get_string('store_'.$storesummary['name'], 'cache');
 109              }
 110              if (!$isready && (int)$storesummary['mappings'] > 0) {
 111                  $readycell->text = $this->output->help_icon('storerequiresattention', 'cache');
 112                  $readycell->attributes['class'] = 'store-requires-attention';
 113              }
 114  
 115              $lock = $storesummary['lock']['name'];
 116              if (!empty($storesummary['lock']['default'])) {
 117                  $lock = get_string($storesummary['lock']['name'], 'cache');
 118              }
 119  
 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)
 129              ));
 130              $row->attributes['class'] = 'store-'.$name;
 131              if ($storesummary['default']) {
 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      /**
 145       * Displays plugin summaries.
 146       *
 147       * @param array $storepluginsummaries information about each store plugin as
 148       *      returned by cache_administration_helper::get_store_plugin_summaries().
 149       * @return string HTML
 150       */
 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'),
 160          );
 161          $table->colclasses = array(
 162              'plugin',
 163              'storeready',
 164              'stores',
 165              'modes',
 166              'supports',
 167              'actions'
 168          );
 169          $table->data = array();
 170  
 171          foreach ($storepluginsummaries as $name => $plugin) {
 172              $actions = cache_administration_helper::get_store_plugin_actions($name, $plugin);
 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'],
 195                  ($plugin['requirementsmet']) ? $this->output->pix_icon('i/valid', '1') : '',
 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      /**
 214       * Displays definition summaries.
 215       *
 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.
 221       */
 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')
 233          );
 234          $table->colclasses = array(
 235              'definition',
 236              'mode',
 237              'component',
 238              'area',
 239              'mappings',
 240              'sharing',
 241              'canuselocalstore',
 242              'actions'
 243          );
 244          $table->data = array();
 245  
 246          core_collator::asort_array_of_arrays_by_key($definitionsummaries, 'name');
 247  
 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']);
 255              }
 256              if (!empty($definition['mappings'])) {
 257                  $mapping = join(', ', $definition['mappings']);
 258              } else {
 259                  $mapping = '<em>'.$none.'</em>';
 260              }
 261  
 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  
 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)
 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  
 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'));
 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);
 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;
 332      }
 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',
 346              'type',
 347              'default',
 348              'uses',
 349              'actions'
 350          );
 351          $table->rowclasses = array(
 352              'lock_name',
 353              'lock_type',
 354              'lock_default',
 355              'lock_uses',
 356              'lock_actions',
 357          );
 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')
 364          );
 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'));
 372              }
 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))
 379              ));
 380          }
 381  
 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  
 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;
 392      }
 393  
 394      /**
 395       * Renders an array of notifications for the cache configuration screen.
 396       *
 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       *
 403       * @param array $notifications
 404       * @return string
 405       */
 406      public function notifications(array $notifications = array()) {
 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      }
 419  }