Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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  /**
  18   * Form for selective purging of caches.
  19   *
  20   * @package    core
  21   * @copyright  2018 The Open University
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_admin\form;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir.'/formslib.php');
  30  
  31  /**
  32   * Form for selecting which caches to purge on admin/purgecaches.php
  33   *
  34   * @package   core
  35   * @copyright 2018 The Open University
  36   * @author    Mark Johnson <mark.johnson@open.ac.uk>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class purge_caches extends \moodleform {
  40      /**
  41       * Define a "Purge all caches" button, and a fieldset with checkboxes for selectively purging separate caches.
  42       */
  43      public function definition() {
  44          $mform = $this->_form;
  45          $mform->addElement('hidden', 'returnurl', $this->_customdata['returnurl']);
  46          $mform->setType('returnurl', PARAM_LOCALURL);
  47          $mform->addElement('submit', 'all', get_string('purgecaches', 'admin'));
  48          $mform->addElement('header', 'purgecacheheader', get_string('purgeselectedcaches', 'admin'));
  49          $checkboxes = [
  50              $mform->createElement('advcheckbox', 'theme', '', get_string('purgethemecache', 'admin')),
  51              $mform->createElement('advcheckbox', 'lang', '', get_string('purgelangcache', 'admin')),
  52              $mform->createElement('advcheckbox', 'js', '', get_string('purgejscache', 'admin')),
  53              $mform->createElement('advcheckbox', 'template', '', get_string('purgetemplates', 'admin')),
  54              $mform->createElement('advcheckbox', 'filter', '', get_string('purgefiltercache', 'admin')),
  55              $mform->createElement('advcheckbox', 'muc', '', get_string('purgemuc', 'admin')),
  56              $mform->createElement('advcheckbox', 'other', '', get_string('purgeothercaches', 'admin'))
  57          ];
  58          $mform->addGroup($checkboxes, 'purgeselectedoptions');
  59          $mform->addElement('submit', 'purgeselectedcaches', get_string('purgeselectedcaches', 'admin'));
  60      }
  61  
  62      /**
  63       * If the "Purge selected caches" button was pressed, ensure at least one cache was selected.
  64       *
  65       * @param array $data
  66       * @param array $files
  67       * @return array Error messages
  68       */
  69      public function validation($data, $files) {
  70          $errors = [];
  71          if (isset($data['purgeselectedcaches']) && empty(array_filter($data['purgeselectedoptions']))) {
  72              $errors['purgeselectedoptions'] = get_string('purgecachesnoneselected', 'admin');
  73          }
  74          return $errors;
  75      }
  76  }