1441 |
ariadna |
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 |
namespace core_cache\form;
|
|
|
18 |
|
|
|
19 |
use core_cache\administration_helper;
|
|
|
20 |
use core_cache\definition;
|
|
|
21 |
use moodleform;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Form to set definition sharing option
|
|
|
25 |
*
|
|
|
26 |
* @package core_cache
|
|
|
27 |
* @category cache
|
|
|
28 |
* @copyright 2013 Sam Hemelryk
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class cache_definition_sharing_form extends moodleform {
|
|
|
32 |
/**
|
|
|
33 |
* The definition of the form
|
|
|
34 |
*/
|
|
|
35 |
final protected function definition() {
|
|
|
36 |
$definition = $this->_customdata['definition'];
|
|
|
37 |
$sharingoptions = $this->_customdata['sharingoptions'];
|
|
|
38 |
$form = $this->_form;
|
|
|
39 |
|
|
|
40 |
$form->addElement('hidden', 'definition', $definition);
|
|
|
41 |
$form->setType('definition', PARAM_SAFEPATH);
|
|
|
42 |
$form->addElement('hidden', 'action', 'editdefinitionsharing');
|
|
|
43 |
$form->setType('action', PARAM_ALPHA);
|
|
|
44 |
|
|
|
45 |
// We use a group here for validation.
|
|
|
46 |
$count = 0;
|
|
|
47 |
$group = [];
|
|
|
48 |
foreach ($sharingoptions as $value => $text) {
|
|
|
49 |
$count++;
|
|
|
50 |
$group[] = $form->createElement('checkbox', $value, null, $text);
|
|
|
51 |
}
|
|
|
52 |
$form->addGroup($group, 'sharing', get_string('sharing', 'cache'), '<br />');
|
|
|
53 |
$form->setType('sharing', PARAM_INT);
|
|
|
54 |
|
|
|
55 |
$form->addElement('text', 'userinputsharingkey', get_string('userinputsharingkey', 'cache'));
|
|
|
56 |
$form->addHelpButton('userinputsharingkey', 'userinputsharingkey', 'cache');
|
|
|
57 |
$form->disabledIf('userinputsharingkey', 'sharing[' . definition::SHARING_INPUT . ']', 'notchecked');
|
|
|
58 |
$form->setType('userinputsharingkey', PARAM_ALPHANUMEXT);
|
|
|
59 |
|
|
|
60 |
$values = array_keys($sharingoptions);
|
|
|
61 |
if (in_array(definition::SHARING_ALL, $values)) {
|
|
|
62 |
// If you share with all thenthe other options don't really make sense.
|
|
|
63 |
foreach ($values as $value) {
|
|
|
64 |
$form->disabledIf('sharing[' . $value . ']', 'sharing[' . definition::SHARING_ALL . ']', 'checked');
|
|
|
65 |
}
|
|
|
66 |
$form->disabledIf('userinputsharingkey', 'sharing[' . definition::SHARING_ALL . ']', 'checked');
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
$this->add_action_buttons();
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Sets the data for this form.
|
|
|
74 |
*
|
|
|
75 |
* @param array $data
|
|
|
76 |
*/
|
|
|
77 |
public function set_data($data) {
|
|
|
78 |
if (!isset($data['sharing'])) {
|
|
|
79 |
// Set the default value here. mforms doesn't handle defaults very nicely.
|
|
|
80 |
$data['sharing'] = administration_helper::get_definition_sharing_options(definition::SHARING_DEFAULT);
|
|
|
81 |
}
|
|
|
82 |
parent::set_data($data);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Validates this form
|
|
|
87 |
*
|
|
|
88 |
* @param array $data
|
|
|
89 |
* @param array $files
|
|
|
90 |
* @return array
|
|
|
91 |
*/
|
|
|
92 |
public function validation($data, $files) {
|
|
|
93 |
$errors = parent::validation($data, $files);
|
|
|
94 |
if (count($errors) === 0 && !isset($data['sharing'])) {
|
|
|
95 |
// They must select at least one sharing option.
|
|
|
96 |
$errors['sharing'] = get_string('sharingrequired', 'cache');
|
|
|
97 |
}
|
|
|
98 |
return $errors;
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
// Alias this class to the old name.
|
|
|
103 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
104 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
105 |
class_alias(cache_definition_sharing_form::class, \cache_definition_sharing_form::class);
|