forked from gjb2048/moodle-theme_essential
-
Notifications
You must be signed in to change notification settings - Fork 0
/
essential_admin_setting_configradio.php
200 lines (184 loc) · 7.3 KB
/
essential_admin_setting_configradio.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Config text setting with validation.
*
* @package theme
* @subpackage essential
* @copyright © 2016-onwards G J Barnard.
* @author G J Barnard - gjbarnard at gmail dot com and {@link http://moodle.org/user/profile.php?id=442195}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class essential_admin_setting_configradio extends admin_setting {
/** @var array Array of choices value => label */
protected $choices;
/** @var array Array of images value => image name in theme */
protected $images;
/** @var boolean false = vertical and true = horizontal */
protected $inline;
/**
* Constructor
* @param string $name Unique ascii name, either 'mysetting' for settings that in config or
* 'myplugin/mysetting' for ones in config_plugins.
* @param string $visiblename Localised.
* @param string $description Long localised info
* @param string|int $defaultsetting
* @param array $choices array of $value => $label for each selection.
* @param array $inline boolean false = vertical and true = horizontal.
* @param array $images array of $value => image name in theme for each selection.
*/
public function __construct($name, $visiblename, $description, $defaultsetting, $choices, $inline = true, $images = array()) {
$this->choices = $choices;
$this->inline = $inline;
$this->images = $images;
parent::__construct($name, $visiblename, $description, $defaultsetting);
}
/**
* This function may be used in ancestors for lazy loading of choices
*
* Override this method if loading of choices is expensive, such
* as when it requires multiple db requests.
*
* @return bool true if loaded, false if error
*/
public function load_choices() {
return true;
}
/**
* Check if this is $query is related to a choice
*
* @param string $query
* @return bool true if related, false if not
*/
public function is_related($query) {
if (parent::is_related($query)) {
return true;
}
if (!$this->load_choices()) {
return false;
}
foreach ($this->choices as $key => $value) {
if (strpos(core_text::strtolower($key), $query) !== false) {
return true;
}
if (strpos(core_text::strtolower($value), $query) !== false) {
return true;
}
}
return false;
}
/**
* Return the setting
*
* @return mixed returns config if successful else null
*/
public function get_setting() {
return $this->config_read($this->name);
}
/**
* Save a setting
*
* @param string $data
* @return string empty of error string
*/
public function write_setting($data) {
if (!$this->load_choices() or empty($this->choices)) {
return '';
}
if (!array_key_exists($data, $this->choices)) {
return ''; // Ignore it.
}
return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
}
/**
* Returns XHTML select field
*
* Ensure the options are loaded, and generate the XHTML for the select
* element and any warning message. Separating this out from output_html
* makes it easier to subclass this class.
*
* @param string $data the option to show as selected.
* @param string $current the currently selected option in the database, null if none.
* @param string $default the default selected option.
* @return array the HTML for the select element, and a warning message.
*/
public function output_radio_html($data, $current, $default, $extraname = '') {
if (!$this->load_choices() or empty($this->choices)) {
return array('', '');
}
if (is_null($current)) {
// First run.
$warning = '';
if ((!is_null($default)) and (empty($data))) {
$data = $default;
}
} else if (empty($current) and (array_key_exists('', $this->choices) or array_key_exists(0, $this->choices))) {
// No warning.
$warning = '';
} else if (!array_key_exists($current, $this->choices)) {
$warning = get_string('warningcurrentsetting', 'admin', s($current));
if (!is_null($default) and $data == $current) {
$data = $default; // Use default instead of first value when showing the form.
}
} else {
$warning = '';
}
$radiohtml = '';
foreach ($this->choices as $key => $value) {
// The string cast is needed because key may be integer - 0 is equal to most strings!
$checked = ((string)$key == $data ? ' checked="checked"' : '');
$radiohtml .= '<input type="radio" id="'.$this->get_id().'_'.$key.'" name="'.$this->get_full_name().'" value="'.$key.'" '.$checked.' />';
if (array_key_exists($key, $this->images)) {
global $OUTPUT;
$radiohtml .= '<label for="'.$this->get_id().'_'.$key.'" title="'.$value.'">'.
'<img class="img-responsive" src="'.$OUTPUT->pix_url($this->images[$key], $this->plugin).'" alt="'.$value.'">'.
'</label>';
} else {
$radiohtml .= '<label for="'.$this->get_id().'_'.$key.'">'.$value.'</label>';
}
if (!$this->inline) {
$radiohtml .= '<br>';
} else {
$radiohtml .= '<span> </span>';
}
}
return array($radiohtml, $warning);
}
/**
* Returns XHTML select field and wrapping div(s)
*
* @see output_select_html()
*
* @param string $data the option to show as selected
* @param string $query
* @return string XHTML field and wrapping div
*/
public function output_html($data, $query='') {
$default = $this->get_defaultsetting();
$current = $this->get_setting();
list($radiohtml, $warning) = $this->output_radio_html($data, $current, $default);
if (!$radiohtml) {
return '';
}
if (!is_null($default) and array_key_exists($default, $this->choices)) {
$defaultinfo = $this->choices[$default];
} else {
$defaultinfo = null;
}
$return = '<div class="form-radio defaultsnext">' . $radiohtml . '</div>';
return format_admin_setting($this, $this->visiblename, $return, $this->description, true, $warning, $defaultinfo, $query);
}
}