Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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