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/ -> forms.php (source)

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Forms used for the administration and managemement of the cache setup.
  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  require_once($CFG->dirroot.'/lib/formslib.php');
  31  
  32  /**
  33   * Add store instance form.
  34   *
  35   * @package    core
  36   * @category   cache
  37   * @copyright  2012 Sam Hemelryk
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class cachestore_addinstance_form extends moodleform {
  41  
  42      /**
  43       * The definition of the add instance form
  44       */
  45      protected final function definition() {
  46          $form = $this->_form;
  47          $store = $this->_customdata['store'];
  48          $plugin = $this->_customdata['plugin'];
  49          $locks = $this->_customdata['locks'];
  50  
  51          $form->addElement('hidden', 'plugin', $plugin);
  52          $form->setType('plugin', PARAM_PLUGIN);
  53          $form->addElement('hidden', 'editing', !empty($this->_customdata['store']));
  54          $form->setType('editing', PARAM_BOOL);
  55  
  56          if (!$store) {
  57              $form->addElement('text', 'name', get_string('storename', 'cache'));
  58              $form->addHelpButton('name', 'storename', 'cache');
  59              $form->addRule('name', get_string('required'), 'required');
  60              $form->setType('name', PARAM_NOTAGS);
  61          } else {
  62              $form->addElement('hidden', 'name', $store);
  63              $form->addElement('static', 'name-value', get_string('storename', 'cache'), $store);
  64              $form->setType('name', PARAM_NOTAGS);
  65          }
  66  
  67          if (is_array($locks)) {
  68              $form->addElement('select', 'lock', get_string('locking', 'cache'), $locks);
  69              $form->addHelpButton('lock', 'locking', 'cache');
  70              $form->setType('lock', PARAM_ALPHANUMEXT);
  71          } else {
  72              $form->addElement('hidden', 'lock', '');
  73              $form->setType('lock', PARAM_ALPHANUMEXT);
  74              $form->addElement('static', 'lock-value', get_string('locking', 'cache'),
  75                      '<em>'.get_string('nativelocking', 'cache').'</em>');
  76          }
  77  
  78          if (method_exists($this, 'configuration_definition')) {
  79              $form->addElement('header', 'storeconfiguration', get_string('storeconfiguration', 'cache'));
  80              $this->configuration_definition();
  81          }
  82  
  83          $this->add_action_buttons();
  84      }
  85  
  86      /**
  87       * Validates the add instance form data
  88       *
  89       * @param array $data
  90       * @param array $files
  91       * @return array
  92       */
  93      public function validation($data, $files) {
  94          $errors = parent::validation($data, $files);
  95  
  96          if (!array_key_exists('name', $errors)) {
  97              if (!preg_match('#^[a-zA-Z0-9\-_ ]+$#', $data['name'])) {
  98                  $errors['name'] = get_string('storenameinvalid', 'cache');
  99              } else if (empty($this->_customdata['store'])) {
 100                  $stores = cache_administration_helper::get_store_instance_summaries();
 101                  if (array_key_exists($data['name'], $stores)) {
 102                      $errors['name'] = get_string('storenamealreadyused', 'cache');
 103                  }
 104              }
 105          }
 106  
 107          if (method_exists($this, 'configuration_validation')) {
 108              $newerrors = $this->configuration_validation($data, $files, $errors);
 109              // We need to selectiviliy merge here
 110              foreach ($newerrors as $element => $error) {
 111                  if (!array_key_exists($element, $errors)) {
 112                      $errors[$element] = $error;
 113                  }
 114              }
 115          }
 116  
 117          return $errors;
 118      }
 119  }
 120  
 121  /**
 122   * Form to set definition mappings
 123   *
 124   * @package    core
 125   * @category   cache
 126   * @copyright  2012 Sam Hemelryk
 127   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 128   */
 129  class cache_definition_mappings_form extends moodleform {
 130  
 131      /**
 132       * The definition of the form
 133       */
 134      protected final function definition() {
 135          global $OUTPUT;
 136  
 137          $definition = $this->_customdata['definition'];
 138          $form = $this->_form;
 139  
 140          list($component, $area) = explode('/', $definition, 2);
 141          list($currentstores, $storeoptions, $defaults) =
 142                  cache_administration_helper::get_definition_store_options($component, $area);
 143  
 144          $storedata = cache_administration_helper::get_definition_summaries();
 145          if ($storedata[$definition]['mode'] != cache_store::MODE_REQUEST) {
 146              if (isset($storedata[$definition]['canuselocalstore']) && $storedata[$definition]['canuselocalstore']) {
 147                  $form->addElement('html', $OUTPUT->notification(get_string('localstorenotification', 'cache'), 'notifymessage'));
 148              } else {
 149                  $form->addElement('html', $OUTPUT->notification(get_string('sharedstorenotification', 'cache'), 'notifymessage'));
 150              }
 151          }
 152          $form->addElement('hidden', 'definition', $definition);
 153          $form->setType('definition', PARAM_SAFEPATH);
 154          $form->addElement('hidden', 'action', 'editdefinitionmapping');
 155          $form->setType('action', PARAM_ALPHA);
 156  
 157          $requiredoptions = max(3, count($currentstores)+1);
 158          $requiredoptions = min($requiredoptions, count($storeoptions));
 159  
 160          $options = array('' => get_string('none'));
 161          foreach ($storeoptions as $option => $def) {
 162              $options[$option] = $option;
 163              if ($def['default']) {
 164                  $options[$option] .= ' '.get_string('mappingdefault', 'cache');
 165              }
 166          }
 167  
 168          for ($i = 0; $i < $requiredoptions; $i++) {
 169              $title = '...';
 170              if ($i === 0) {
 171                  $title = get_string('mappingprimary', 'cache');
 172              } else if ($i === $requiredoptions-1) {
 173                  $title = get_string('mappingfinal', 'cache');
 174              }
 175              $form->addElement('select', 'mappings['.$i.']', $title, $options);
 176          }
 177          $i = 0;
 178          foreach ($currentstores as $store => $def) {
 179              $form->setDefault('mappings['.$i.']', $store);
 180              $i++;
 181          }
 182  
 183          if (!empty($defaults)) {
 184              $form->addElement('static', 'defaults', get_string('defaultmappings', 'cache'),
 185                      html_writer::tag('strong', join(', ', $defaults)));
 186              $form->addHelpButton('defaults', 'defaultmappings', 'cache');
 187          }
 188  
 189          $this->add_action_buttons();
 190      }
 191  }
 192  
 193  /**
 194   * Form to set definition sharing option
 195   *
 196   * @package    core
 197   * @category   cache
 198   * @copyright  2013 Sam Hemelryk
 199   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 200   */
 201  class cache_definition_sharing_form extends moodleform {
 202      /**
 203       * The definition of the form
 204       */
 205      protected final function definition() {
 206          $definition = $this->_customdata['definition'];
 207          $sharingoptions = $this->_customdata['sharingoptions'];
 208          $form = $this->_form;
 209  
 210          $form->addElement('hidden', 'definition', $definition);
 211          $form->setType('definition', PARAM_SAFEPATH);
 212          $form->addElement('hidden', 'action', 'editdefinitionsharing');
 213          $form->setType('action', PARAM_ALPHA);
 214  
 215          // We use a group here for validation.
 216          $count = 0;
 217          $group = array();
 218          foreach ($sharingoptions as $value => $text) {
 219              $count++;
 220              $group[] = $form->createElement('checkbox', $value, null, $text);
 221          }
 222          $form->addGroup($group, 'sharing', get_string('sharing', 'cache'), '<br />');
 223          $form->setType('sharing', PARAM_INT);
 224  
 225          $form->addElement('text', 'userinputsharingkey', get_string('userinputsharingkey', 'cache'));
 226          $form->addHelpButton('userinputsharingkey', 'userinputsharingkey', 'cache');
 227          $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_INPUT.']', 'notchecked');
 228          $form->setType('userinputsharingkey', PARAM_ALPHANUMEXT);
 229  
 230          $values = array_keys($sharingoptions);
 231          if (in_array(cache_definition::SHARING_ALL, $values)) {
 232              // If you share with all thenthe other options don't really make sense.
 233              foreach ($values as $value) {
 234                  $form->disabledIf('sharing['.$value.']', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
 235              }
 236              $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
 237          }
 238  
 239          $this->add_action_buttons();
 240      }
 241  
 242      /**
 243       * Sets the data for this form.
 244       *
 245       * @param array $data
 246       */
 247      public function set_data($data) {
 248          if (!isset($data['sharing'])) {
 249              // Set the default value here. mforms doesn't handle defaults very nicely.
 250              $data['sharing'] = cache_administration_helper::get_definition_sharing_options(cache_definition::SHARING_DEFAULT);
 251          }
 252          parent::set_data($data);
 253      }
 254  
 255      /**
 256       * Validates this form
 257       *
 258       * @param array $data
 259       * @param array $files
 260       * @return array
 261       */
 262      public function validation($data, $files) {
 263          $errors = parent::validation($data, $files);
 264          if (count($errors) === 0 && !isset($data['sharing'])) {
 265              // They must select at least one sharing option.
 266              $errors['sharing'] = get_string('sharingrequired', 'cache');
 267          }
 268          return $errors;
 269      }
 270  }
 271  
 272  /**
 273   * Form to set the mappings for a mode.
 274   *
 275   * @package    core
 276   * @category   cache
 277   * @copyright  2012 Sam Hemelryk
 278   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 279   */
 280  class cache_mode_mappings_form extends moodleform {
 281      /**
 282       * The definition of the form
 283       */
 284      protected function definition() {
 285          $form = $this->_form;
 286          $stores = $this->_customdata;
 287  
 288          $options = array(
 289              cache_store::MODE_APPLICATION => array(),
 290              cache_store::MODE_SESSION => array(),
 291              cache_store::MODE_REQUEST => array()
 292          );
 293          foreach ($stores as $storename => $store) {
 294              foreach ($store['modes'] as $mode => $enabled) {
 295                  if ($enabled && ($mode !== cache_store::MODE_SESSION || $store['supports']['searchable'])) {
 296                      if (empty($store['default'])) {
 297                          $options[$mode][$storename] = $store['name'];
 298                      } else {
 299                          $options[$mode][$storename] = get_string('store_'.$store['name'], 'cache');
 300                      }
 301                  }
 302              }
 303          }
 304  
 305          $form->addElement('hidden', 'action', 'editmodemappings');
 306          $form->setType('action', PARAM_ALPHA);
 307          foreach ($options as $mode => $optionset) {
 308              $form->addElement('select', 'mode_'.$mode, get_string('mode_'.$mode, 'cache'), $optionset);
 309          }
 310  
 311          $this->add_action_buttons();
 312      }
 313  }
 314  
 315  /**
 316   * Form to add a cache lock instance.
 317   *
 318   * All cache lock plugins that wish to have custom configuration should override
 319   * this form, and more explicitly the plugin_definition and plugin_validation methods.
 320   *
 321   * @package    core
 322   * @category   cache
 323   * @copyright  2013 Sam Hemelryk
 324   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 325   */
 326  class cache_lock_form extends moodleform {
 327  
 328      /**
 329       * Defines this form.
 330       */
 331      final public function definition() {
 332          $plugin = $this->_customdata['lock'];
 333  
 334          $this->_form->addElement('hidden', 'action', 'newlockinstance');
 335          $this->_form->setType('action', PARAM_ALPHANUMEXT);
 336          $this->_form->addElement('hidden', 'lock', $plugin);
 337          $this->_form->setType('lock', PARAM_COMPONENT);
 338          $this->_form->addElement('text', 'name', get_string('lockname', 'cache'));
 339          $this->_form->setType('name', PARAM_ALPHANUMEXT);
 340          $this->_form->addRule('name', get_string('required'), 'required');
 341          $this->_form->addElement('static', 'namedesc', '', get_string('locknamedesc', 'cache'));
 342  
 343          $this->plugin_definition();
 344  
 345          $this->add_action_buttons();
 346      }
 347  
 348      /**
 349       * Validates this form.
 350       *
 351       * @param array $data
 352       * @param array $files
 353       * @return array
 354       */
 355      final public function validation($data, $files) {
 356          $errors = parent::validation($data, $files);
 357          if (!isset($errors['name'])) {
 358              $config = cache_config::instance();
 359              if (in_array($data['name'], array_keys($config->get_locks()))) {
 360                  $errors['name'] = get_string('locknamenotunique', 'cache');
 361              }
 362          }
 363          $errors = $this->plugin_validation($data, $files, $errors);
 364          return $errors;
 365      }
 366  
 367      /**
 368       * Plugin specific definition.
 369       */
 370      public function plugin_definition() {
 371          // No custom validation going on here.
 372      }
 373  
 374      /**
 375       * Plugin specific validation.
 376       *
 377       * @param array $data
 378       * @param array $files
 379       * @param array $errors
 380       * @return array
 381       */
 382      public function plugin_validation($data, $files, array $errors) {
 383          return $errors;
 384      }
 385  }