Moodle release 3.3.8
[moodle.git] / cache / forms.php
blobe482702c424f9460d17a52c22881ab3f9a645a66
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.
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/>.
17 /**
18 * Forms used for the administration and managemement of the cache setup.
20 * This file is part of Moodle's cache API, affectionately called MUC.
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
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot.'/lib/formslib.php');
32 /**
33 * Add store instance form.
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
40 class cachestore_addinstance_form extends moodleform {
42 /**
43 * The definition of the add instance form
45 protected final function definition() {
46 $form = $this->_form;
47 $store = $this->_customdata['store'];
48 $plugin = $this->_customdata['plugin'];
49 $locks = $this->_customdata['locks'];
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);
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);
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>');
78 if (method_exists($this, 'configuration_definition')) {
79 $form->addElement('header', 'storeconfiguration', get_string('storeconfiguration', 'cache'));
80 $this->configuration_definition();
83 $this->add_action_buttons();
86 /**
87 * Validates the add instance form data
89 * @param array $data
90 * @param array $files
91 * @return array
93 public function validation($data, $files) {
94 $errors = parent::validation($data, $files);
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');
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;
117 return $errors;
122 * Form to set definition mappings
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
129 class cache_definition_mappings_form extends moodleform {
132 * The definition of the form
134 protected final function definition() {
135 global $OUTPUT;
137 $definition = $this->_customdata['definition'];
138 $form = $this->_form;
140 list($component, $area) = explode('/', $definition, 2);
141 list($currentstores, $storeoptions, $defaults) =
142 cache_administration_helper::get_definition_store_options($component, $area);
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'));
152 $form->addElement('hidden', 'definition', $definition);
153 $form->setType('definition', PARAM_SAFEPATH);
154 $form->addElement('hidden', 'action', 'editdefinitionmapping');
155 $form->setType('action', PARAM_ALPHA);
157 $requiredoptions = max(3, count($currentstores)+1);
158 $requiredoptions = min($requiredoptions, count($storeoptions));
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');
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');
175 $form->addElement('select', 'mappings['.$i.']', $title, $options);
177 $i = 0;
178 foreach ($currentstores as $store => $def) {
179 $form->setDefault('mappings['.$i.']', $store);
180 $i++;
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');
189 $this->add_action_buttons();
194 * Form to set definition sharing option
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
201 class cache_definition_sharing_form extends moodleform {
203 * The definition of the form
205 protected final function definition() {
206 $definition = $this->_customdata['definition'];
207 $sharingoptions = $this->_customdata['sharingoptions'];
208 $form = $this->_form;
210 $form->addElement('hidden', 'definition', $definition);
211 $form->setType('definition', PARAM_SAFEPATH);
212 $form->addElement('hidden', 'action', 'editdefinitionsharing');
213 $form->setType('action', PARAM_ALPHA);
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);
222 $form->addGroup($group, 'sharing', get_string('sharing', 'cache'), '<br />');
223 $form->setType('sharing', PARAM_INT);
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);
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');
236 $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
239 $this->add_action_buttons();
243 * Sets the data for this form.
245 * @param array $data
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);
252 parent::set_data($data);
256 * Validates this form
258 * @param array $data
259 * @param array $files
260 * @return array
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');
268 return $errors;
273 * Form to set the mappings for a mode.
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
280 class cache_mode_mappings_form extends moodleform {
282 * The definition of the form
284 protected function definition() {
285 $form = $this->_form;
286 $stores = $this->_customdata;
288 $options = array(
289 cache_store::MODE_APPLICATION => array(),
290 cache_store::MODE_SESSION => array(),
291 cache_store::MODE_REQUEST => array()
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');
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);
311 $this->add_action_buttons();
316 * Form to add a cache lock instance.
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.
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
326 class cache_lock_form extends moodleform {
329 * Defines this form.
331 final public function definition() {
332 $plugin = $this->_customdata['lock'];
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'));
343 $this->plugin_definition();
345 $this->add_action_buttons();
349 * Validates this form.
351 * @param array $data
352 * @param array $files
353 * @return array
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');
363 $errors = $this->plugin_validation($data, $files, $errors);
364 return $errors;
368 * Plugin specific definition.
370 public function plugin_definition() {
371 // No custom validation going on here.
375 * Plugin specific validation.
377 * @param array $data
378 * @param array $files
379 * @param array $errors
380 * @return array
382 public function plugin_validation($data, $files, array $errors) {
383 return $errors;