MDL-52896 moodlelib: Remove APD support relic
[moodle.git] / cache / forms.php
blob5a2ef3b3cc11cf1333a68a62b2783cce28434da0
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 $definition = $this->_customdata['definition'];
136 $form = $this->_form;
138 list($component, $area) = explode('/', $definition, 2);
139 list($currentstores, $storeoptions, $defaults) =
140 cache_administration_helper::get_definition_store_options($component, $area);
142 $form->addElement('hidden', 'definition', $definition);
143 $form->setType('definition', PARAM_SAFEPATH);
144 $form->addElement('hidden', 'action', 'editdefinitionmapping');
145 $form->setType('action', PARAM_ALPHA);
147 $requiredoptions = max(3, count($currentstores)+1);
148 $requiredoptions = min($requiredoptions, count($storeoptions));
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');
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');
165 $form->addElement('select', 'mappings['.$i.']', $title, $options);
167 $i = 0;
168 foreach ($currentstores as $store => $def) {
169 $form->setDefault('mappings['.$i.']', $store);
170 $i++;
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');
179 $this->add_action_buttons();
184 * Form to set definition sharing option
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
191 class cache_definition_sharing_form extends moodleform {
193 * The definition of the form
195 protected final function definition() {
196 $definition = $this->_customdata['definition'];
197 $sharingoptions = $this->_customdata['sharingoptions'];
198 $form = $this->_form;
200 $form->addElement('hidden', 'definition', $definition);
201 $form->setType('definition', PARAM_SAFEPATH);
202 $form->addElement('hidden', 'action', 'editdefinitionsharing');
203 $form->setType('action', PARAM_ALPHA);
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);
212 $form->addGroup($group, 'sharing', get_string('sharing', 'cache'), '<br />');
213 $form->setType('sharing', PARAM_INT);
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);
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');
226 $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
229 $this->add_action_buttons();
233 * Sets the data for this form.
235 * @param array $data
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);
242 parent::set_data($data);
246 * Validates this form
248 * @param array $data
249 * @param array $files
250 * @return array
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');
258 return $errors;
263 * Form to set the mappings for a mode.
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
270 class cache_mode_mappings_form extends moodleform {
272 * The definition of the form
274 protected function definition() {
275 $form = $this->_form;
276 $stores = $this->_customdata;
278 $options = array(
279 cache_store::MODE_APPLICATION => array(),
280 cache_store::MODE_SESSION => array(),
281 cache_store::MODE_REQUEST => array()
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');
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);
301 $this->add_action_buttons();
306 * Form to add a cache lock instance.
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.
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
316 class cache_lock_form extends moodleform {
319 * Defines this form.
321 final public function definition() {
322 $plugin = $this->_customdata['lock'];
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'));
333 $this->plugin_definition();
335 $this->add_action_buttons();
339 * Validates this form.
341 * @param array $data
342 * @param array $files
343 * @return array
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');
353 $errors = $this->plugin_validation($data, $files, $errors);
354 return $errors;
358 * Plugin specific definition.
360 public function plugin_definition() {
361 // No custom validation going on here.
365 * Plugin specific validation.
367 * @param array $data
368 * @param array $files
369 * @param array $errors
370 * @return array
372 public function plugin_validation($data, $files, array $errors) {
373 return $errors;