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.
   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   * The library file for the apcu cache store.
  18   *
  19   * This file is part of the apcu cache store, it contains the API for interacting with an instance of the store.
  20   *
  21   * @package    cachestore_apcu
  22   * @copyright  2014 Sam Hemelryk
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  defined('MOODLE_INTERNAL') || die();
  26  require_once($CFG->dirroot.'/cache/forms.php');
  27  /**
  28   * Form for adding a apcu instance.
  29   *
  30   * @copyright  2014 Sam Hemelryk
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class cachestore_apcu_addinstance_form extends cachestore_addinstance_form {
  34      /**
  35       * Add the desired form elements.
  36       */
  37      protected function configuration_definition() {
  38          global $CFG;
  39          $form = $this->_form;
  40          $form->addElement('text', 'prefix', get_string('prefix', 'cachestore_apcu'),
  41              array('maxlength' => 5, 'size' => 5));
  42          $form->addHelpButton('prefix', 'prefix', 'cachestore_apcu');
  43          $form->setType('prefix', PARAM_TEXT); // We set to text but we have a rule to limit to alphanumext.
  44          $form->setDefault('prefix', $CFG->prefix);
  45          $form->addRule('prefix', get_string('prefixinvalid', 'cachestore_apcu'), 'regex', '#^[a-zA-Z0-9\-_]+$#');
  46          $form->addElement('header', 'apc_notice', get_string('notice', 'cachestore_apcu'));
  47          $form->setExpanded('apc_notice');
  48          $link = get_docs_url('Caching#APC');
  49          $form->addElement('html', nl2br(get_string('clusternotice', 'cachestore_apcu', $link)));
  50      }
  51  
  52      /**
  53       * Validates the configuration data.
  54       *
  55       * We need to check that prefix is unique.
  56       *
  57       * @param array $data
  58       * @param array $files
  59       * @param array $errors
  60       * @return array
  61       * @throws coding_exception
  62       */
  63      public function configuration_validation($data, $files, array $errors) {
  64          if (empty($errors['prefix'])) {
  65              $factory = cache_factory::instance();
  66              $config = $factory->create_config_instance();
  67              foreach ($config->get_all_stores() as $store) {
  68                  if ($store['plugin'] === 'apcu') {
  69                      if (isset($store['configuration']['prefix'])) {
  70                          if ($data['prefix'] === $store['configuration']['prefix']) {
  71                              // The new store has the same prefix as an existing store, thats a problem.
  72                              $errors['prefix'] = get_string('prefixnotunique', 'cachestore_apcu');
  73                              break;
  74                          }
  75                      } else if (empty($data['prefix'])) {
  76                          // The existing store hasn't got a prefix and neither does the new store, that's a problem.
  77                          $errors['prefix'] = get_string('prefixnotunique', 'cachestore_apcu');
  78                          break;
  79                      }
  80                  }
  81              }
  82          }
  83          return $errors;
  84      }
  85  }