Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • /cache/ -> forms.php (source)

    Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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('lockmethod', 'cache'), $locks);
      69              $form->addHelpButton('lock', 'lockmethod', '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('lockmethod', '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          $definition = $this->_customdata['definition'];
     136          $form = $this->_form;
     137  
     138          list($component, $area) = explode('/', $definition, 2);
     139          list($currentstores, $storeoptions, $defaults) =
     140                  cache_administration_helper::get_definition_store_options($component, $area);
     141  
     142          $form->addElement('hidden', 'definition', $definition);
     143          $form->setType('definition', PARAM_SAFEPATH);
     144          $form->addElement('hidden', 'action', 'editdefinitionmapping');
     145          $form->setType('action', PARAM_ALPHA);
     146  
     147          $requiredoptions = max(3, count($currentstores)+1);
     148          $requiredoptions = min($requiredoptions, count($storeoptions));
     149  
     150          $options = array('' => get_string('none'));
     151          foreach ($storeoptions as $option => $def) {
     152              $options[$option] = $option;
     153              if ($def['default']) {
     154                  $options[$option] .= ' '.get_string('mappingdefault', 'cache');
     155              }
     156          }
     157  
     158          for ($i = 0; $i < $requiredoptions; $i++) {
     159              $title = '...';
     160              if ($i === 0) {
     161                  $title = get_string('mappingprimary', 'cache');
     162              } else if ($i === $requiredoptions-1) {
     163                  $title = get_string('mappingfinal', 'cache');
     164              }
     165              $form->addElement('select', 'mappings['.$i.']', $title, $options);
     166          }
     167          $i = 0;
     168          foreach ($currentstores as $store => $def) {
     169              $form->setDefault('mappings['.$i.']', $store);
     170              $i++;
     171          }
     172  
     173          if (!empty($defaults)) {
     174              $form->addElement('static', 'defaults', get_string('defaultmappings', 'cache'),
     175                      html_writer::tag('strong', join(', ', $defaults)));
     176              $form->addHelpButton('defaults', 'defaultmappings', 'cache');
     177          }
     178  
     179          $this->add_action_buttons();
     180      }
     181  }
     182  
     183  /**
     184   * Form to set definition sharing option
     185   *
     186   * @package    core
     187   * @category   cache
     188   * @copyright  2013 Sam Hemelryk
     189   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     190   */
     191  class cache_definition_sharing_form extends moodleform {
     192      /**
     193       * The definition of the form
     194       */
     195      protected final function definition() {
     196          $definition = $this->_customdata['definition'];
     197          $sharingoptions = $this->_customdata['sharingoptions'];
     198          $form = $this->_form;
     199  
     200          $form->addElement('hidden', 'definition', $definition);
     201          $form->setType('definition', PARAM_SAFEPATH);
     202          $form->addElement('hidden', 'action', 'editdefinitionsharing');
     203          $form->setType('action', PARAM_ALPHA);
     204  
     205          // We use a group here for validation.
     206          $count = 0;
     207          $group = array();
     208          foreach ($sharingoptions as $value => $text) {
     209              $count++;
     210              $group[] = $form->createElement('checkbox', $value, null, $text);
     211          }
     212          $form->addGroup($group, 'sharing', get_string('sharing', 'cache'), '<br />');
     213          $form->setType('sharing', PARAM_INT);
     214  
     215          $form->addElement('text', 'userinputsharingkey', get_string('userinputsharingkey', 'cache'));
     216          $form->addHelpButton('userinputsharingkey', 'userinputsharingkey', 'cache');
     217          $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_INPUT.']', 'notchecked');
     218          $form->setType('userinputsharingkey', PARAM_ALPHANUMEXT);
     219  
     220          $values = array_keys($sharingoptions);
     221          if (in_array(cache_definition::SHARING_ALL, $values)) {
     222              // If you share with all thenthe other options don't really make sense.
     223              foreach ($values as $value) {
     224                  $form->disabledIf('sharing['.$value.']', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
     225              }
     226              $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
     227          }
     228  
     229          $this->add_action_buttons();
     230      }
     231  
     232      /**
     233       * Sets the data for this form.
     234       *
     235       * @param array $data
     236       */
     237      public function set_data($data) {
     238          if (!isset($data['sharing'])) {
     239              // Set the default value here. mforms doesn't handle defaults very nicely.
     240              $data['sharing'] = cache_administration_helper::get_definition_sharing_options(cache_definition::SHARING_DEFAULT);
     241          }
     242          parent::set_data($data);
     243      }
     244  
     245      /**
     246       * Validates this form
     247       *
     248       * @param array $data
     249       * @param array $files
     250       * @return array
     251       */
     252      public function validation($data, $files) {
     253          $errors = parent::validation($data, $files);
     254          if (count($errors) === 0 && !isset($data['sharing'])) {
     255              // They must select at least one sharing option.
     256              $errors['sharing'] = get_string('sharingrequired', 'cache');
     257          }
     258          return $errors;
     259      }
     260  }
     261  
     262  /**
     263   * Form to set the mappings for a mode.
     264   *
     265   * @package    core
     266   * @category   cache
     267   * @copyright  2012 Sam Hemelryk
     268   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     269   */
     270  class cache_mode_mappings_form extends moodleform {
     271      /**
     272       * The definition of the form
     273       */
     274      protected function definition() {
     275          $form = $this->_form;
     276          $stores = $this->_customdata;
     277  
     278          $options = array(
     279              cache_store::MODE_APPLICATION => array(),
     280              cache_store::MODE_SESSION => array(),
     281              cache_store::MODE_REQUEST => array()
     282          );
     283          foreach ($stores as $storename => $store) {
     284              foreach ($store['modes'] as $mode => $enabled) {
     285                  if ($enabled && ($mode !== cache_store::MODE_SESSION || $store['supports']['searchable'])) {
     286                      if (empty($store['default'])) {
     287                          $options[$mode][$storename] = $store['name'];
     288                      } else {
     289                          $options[$mode][$storename] = get_string('store_'.$store['name'], 'cache');
     290                      }
     291                  }
     292              }
     293          }
     294  
     295          $form->addElement('hidden', 'action', 'editmodemappings');
     296          $form->setType('action', PARAM_ALPHA);
     297          foreach ($options as $mode => $optionset) {
     298              $form->addElement('select', 'mode_'.$mode, get_string('mode_'.$mode, 'cache'), $optionset);
     299          }
     300  
     301          $this->add_action_buttons();
     302      }
     303  }
     304  
     305  /**
     306   * Form to add a cache lock instance.
     307   *
     308   * All cache lock plugins that wish to have custom configuration should override
     309   * this form, and more explicitly the plugin_definition and plugin_validation methods.
     310   *
     311   * @package    core
     312   * @category   cache
     313   * @copyright  2013 Sam Hemelryk
     314   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     315   */
     316  class cache_lock_form extends moodleform {
     317  
     318      /**
     319       * Defines this form.
     320       */
     321      final public function definition() {
     322          $plugin = $this->_customdata['lock'];
     323  
     324          $this->_form->addElement('hidden', 'action', 'newlockinstance');
     325          $this->_form->setType('action', PARAM_ALPHANUMEXT);
     326          $this->_form->addElement('hidden', 'lock', $plugin);
     327          $this->_form->setType('lock', PARAM_COMPONENT);
     328          $this->_form->addElement('text', 'name', get_string('lockname', 'cache'));
     329          $this->_form->setType('name', PARAM_ALPHANUMEXT);
     330          $this->_form->addRule('name', get_string('required'), 'required');
     331          $this->_form->addElement('static', 'namedesc', '', get_string('locknamedesc', 'cache'));
     332  
     333          $this->plugin_definition();
     334  
     335          $this->add_action_buttons();
     336      }
     337  
     338      /**
     339       * Validates this form.
     340       *
     341       * @param array $data
     342       * @param array $files
     343       * @return array
     344       */
     345      final public function validation($data, $files) {
     346          $errors = parent::validation($data, $files);
     347          if (!isset($errors['name'])) {
     348              $config = cache_config::instance();
     349              if (in_array($data['name'], array_keys($config->get_locks()))) {
     350                  $errors['name'] = get_string('locknamenotunique', 'cache');
     351              }
     352          }
     353          $errors = $this->plugin_validation($data, $files, $errors);
     354          return $errors;
     355      }
     356  
     357      /**
     358       * Plugin specific definition.
     359       */
     360      public function plugin_definition() {
     361          // No custom validation going on here.
     362      }
     363  
     364      /**
     365       * Plugin specific validation.
     366       *
     367       * @param array $data
     368       * @param array $files
     369       * @param array $errors
     370       * @return array
     371       */
     372      public function plugin_validation($data, $files, array $errors) {
     373          return $errors;
     374      }
     375  }
    

    Search This Site: