Search moodle.org's
Developer Documentation


Long Term Support Release

  • Bug fixes for general core bugs in 3.1.x ended 8 May 2017 (12 months).
  • Bug fixes for security issues in 3.1.x ended 13 May 2019 (36 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x , 5.5.x or 5.6.x on Windows - http://windows.php.net/download/). PHP 7.0.x is supported but has some engine limitations. PHP 7.1.x is not supported.
  • /cache/ -> renderer.php (source)

    Differences Between: [Versions 31 and 38]

       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 $stores
      44       * @param array $plugins
      45       * @return string HTML
      46       */
      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('locking', 'cache') . ' ' . $this->output->help_icon('locking', 'cache'),
      57              get_string('actions', 'cache'),
      58          );
      59          $table->colclasses = array(
      60              'storename',
      61              'plugin',
      62              'storeready',
      63              'mappings',
      64              'modes',
      65              'supports',
      66              'locking',
      67              'actions'
      68          );
      69          $table->data = array();
      70  
      71          $defaultstoreactions = get_string('defaultstoreactions', 'cache');
      72  
      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');
      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'])) {
      91                  $info = $this->output->pix_icon('i/info', $defaultstoreactions, '', array('class' => 'icon'));
      92              }
      93              $htmlactions = array();
      94              foreach ($actions as $action) {
      95                  $htmlactions[] = $this->output->action_link($action['url'], $action['text']);
      96              }
      97  
      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  
     104              $storename = $store['name'];
     105              if (!empty($store['default'])) {
     106                  $storename = get_string('store_'.$store['name'], 'cache');
     107              }
     108              if (!$isready && (int)$store['mappings'] > 0) {
     109                  $readycell->text = $this->output->help_icon('storerequiresattention', 'cache');
     110                  $readycell->attributes['class'] = 'store-requires-attention';
     111              }
     112  
     113              $lock = $store['lock']['name'];
     114              if (!empty($store['lock']['default'])) {
     115                  $lock = get_string($store['lock']['name'], 'cache');
     116              }
     117  
     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)
     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       */
     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'),
     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) {
     169              $actions = cache_administration_helper::get_store_plugin_actions($name, $plugin);
     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'],
     192                  ($plugin['requirementsmet']) ? $this->output->pix_icon('i/valid', '1') : '',
     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
     214       * @return string HTML
     215       */
     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'),
     226          );
     227          $table->colclasses = array(
     228              'definition',
     229              'mode',
     230              'component',
     231              'area',
     232              'mappings',
     233              'sharing',
     234              'actions'
     235          );
     236          $table->data = array();
     237  
     238          core_collator::asort_array_of_arrays_by_key($definitions, 'name');
     239  
     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']);
     247              }
     248              if (!empty($definition['mappings'])) {
     249                  $mapping = join(', ', $definition['mappings']);
     250              } else {
     251                  $mapping = '<em>'.$none.'</em>';
     252              }
     253  
     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)
     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  
     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'));
     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);
     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;
     316      }
     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',
     330              'type',
     331              'default',
     332              'uses',
     333              'actions'
     334          );
     335          $table->rowclasses = array(
     336              'lock_name',
     337              'lock_type',
     338              'lock_default',
     339              'lock_uses',
     340              'lock_actions',
     341          );
     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')
     348          );
     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'));
     356              }
     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))
     363              ));
     364          }
     365  
     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  
     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;
     376      }
     377  
     378      /**
     379       * Renders an array of notifications for the cache configuration screen.
     380       *
     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       * );
     386       *
     387       * @param array $notifications
     388       * @return string
     389       */
     390      public function notifications(array $notifications = array()) {
     391          if (count($notifications) === 0) {
     392              // There are no notifications to render.
     393              return '';
     394          }
     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');
     399          }
     400          $html .= html_writer::end_div();
     401          return $html;
     402      }
     403  }
    

    Search This Site: