forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settingslib.php
216 lines (196 loc) · 9.13 KB
/
settingslib.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?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/>.
/**
* LDAP enrolment plugin admin setting classes
*
* @package enrol_ldap
* @author Iñaki Arenaza
* @copyright 2010 Iñaki Arenaza <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class admin_setting_configtext_trim_lower extends admin_setting_configtext {
/* @var boolean whether to lowercase the value or not before writing in to the db */
private $lowercase;
/**
* Constructor: uses parent::__construct
*
* @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 $defaultsetting default value for the setting
* @param boolean $lowercase if true, lowercase the value before writing it to the db.
* @param boolean $enabled if true, the input field is enabled, otherwise it's disabled.
*/
public function __construct($name, $visiblename, $description, $defaultsetting, $lowercase=false, $enabled=true) {
$this->lowercase = $lowercase;
$this->enabled = $enabled;
parent::__construct($name, $visiblename, $description, $defaultsetting);
}
/**
* Saves the setting(s) provided in $data
*
* @param array $data An array of data, if not array returns empty str
* @return mixed empty string on useless data or success, error string if failed
*/
public function write_setting($data) {
if ($this->paramtype === PARAM_INT and $data === '') {
// do not complain if '' used instead of 0
$data = 0;
}
// $data is a string
$validated = $this->validate($data);
if ($validated !== true) {
return $validated;
}
if ($this->lowercase) {
$data = core_text::strtolower($data);
}
if (!$this->enabled) {
return '';
}
return ($this->config_write($this->name, trim($data)) ? '' : get_string('errorsetting', 'admin'));
}
/**
* Return an XHTML string for the setting
* @return string Returns an XHTML string
*/
public function output_html($data, $query='') {
$default = $this->get_defaultsetting();
$disabled = $this->enabled ? '': ' disabled="disabled"';
return format_admin_setting($this, $this->visiblename,
'<div class="form-text defaultsnext"><input type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" '.$disabled.' /></div>',
$this->description, true, '', $default, $query);
}
}
class admin_setting_ldap_rolemapping extends admin_setting {
/**
* Constructor: uses parent::__construct
*
* @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 $defaultsetting default value for the setting (actually unused)
*/
public function __construct($name, $visiblename, $description, $defaultsetting) {
parent::__construct($name, $visiblename, $description, $defaultsetting);
}
/**
* Returns the current setting if it is set
*
* @return mixed null if null, else an array
*/
public function get_setting() {
$roles = role_fix_names(get_all_roles());
$result = array();
foreach ($roles as $role) {
$contexts = $this->config_read('contexts_role'.$role->id);
$memberattribute = $this->config_read('memberattribute_role'.$role->id);
$result[] = array('id' => $role->id,
'name' => $role->localname,
'contexts' => $contexts,
'memberattribute' => $memberattribute);
}
return $result;
}
/**
* Saves the setting(s) provided in $data
*
* @param array $data An array of data, if not array returns empty str
* @return mixed empty string on useless data or success, error string if failed
*/
public function write_setting($data) {
if(!is_array($data)) {
return ''; // ignore it
}
$result = '';
foreach ($data as $roleid => $data) {
if (!$this->config_write('contexts_role'.$roleid, trim($data['contexts']))) {
$return = get_string('errorsetting', 'admin');
}
if (!$this->config_write('memberattribute_role'.$roleid, core_text::strtolower(trim($data['memberattribute'])))) {
$return = get_string('errorsetting', 'admin');
}
}
return $result;
}
/**
* Returns XHTML field(s) as required by choices
*
* Relies on data being an array should data ever be another valid vartype with
* acceptable value this may cause a warning/error
* if (!is_array($data)) would fix the problem
*
* @todo Add vartype handling to ensure $data is an array
*
* @param array $data An array of checked values
* @param string $query
* @return string XHTML field
*/
public function output_html($data, $query='') {
$return = html_writer::start_tag('div', array('style' =>'float:left; width:auto; margin-right: 0.5em;'));
$return .= html_writer::tag('div', get_string('roles', 'role'), array('style' => 'height: 2em;'));
foreach ($data as $role) {
$return .= html_writer::tag('div', s($role['name']), array('style' => 'height: 2em;'));
}
$return .= html_writer::end_tag('div');
$return .= html_writer::start_tag('div', array('style' => 'float:left; width:auto; margin-right: 0.5em;'));
$return .= html_writer::tag('div', get_string('contexts', 'enrol_ldap'), array('style' => 'height: 2em;'));
foreach ($data as $role) {
$contextid = $this->get_id().'['.$role['id'].'][contexts]';
$contextname = $this->get_full_name().'['.$role['id'].'][contexts]';
$return .= html_writer::start_tag('div', array('style' => 'height: 2em;'));
$return .= html_writer::label(get_string('role_mapping_context', 'enrol_ldap', $role['name']), $contextid, false, array('class' => 'accesshide'));
$attrs = array('type' => 'text', 'size' => '40', 'id' => $contextid, 'name' => $contextname, 'value' => s($role['contexts']));
$return .= html_writer::empty_tag('input', $attrs);
$return .= html_writer::end_tag('div');
}
$return .= html_writer::end_tag('div');
$return .= html_writer::start_tag('div', array('style' => 'float:left; width:auto; margin-right: 0.5em;'));
$return .= html_writer::tag('div', get_string('memberattribute', 'enrol_ldap'), array('style' => 'height: 2em;'));
foreach ($data as $role) {
$memberattrid = $this->get_id().'['.$role['id'].'][memberattribute]';
$memberattrname = $this->get_full_name().'['.$role['id'].'][memberattribute]';
$return .= html_writer::start_tag('div', array('style' => 'height: 2em;'));
$return .= html_writer::label(get_string('role_mapping_attribute', 'enrol_ldap', $role['name']), $memberattrid, false, array('class' => 'accesshide'));
$attrs = array('type' => 'text', 'size' => '15', 'id' => $memberattrid, 'name' => $memberattrname, 'value' => s($role['memberattribute']));
$return .= html_writer::empty_tag('input', $attrs);
$return .= html_writer::end_tag('div');
}
$return .= html_writer::end_tag('div');
$return .= html_writer::tag('div', '', array('style' => 'clear:both;'));
return format_admin_setting($this, $this->visiblename, $return,
$this->description, true, '', '', $query);
}
}
/**
* Class implements new specialized setting for course categories that are loaded
* only when required
* @author Darko Miletic
*
*/
class enrol_ldap_admin_setting_category extends admin_setting_configselect {
public function __construct($name, $visiblename, $description) {
parent::__construct($name, $visiblename, $description, 1, null);
}
public function load_choices() {
if (is_array($this->choices)) {
return true;
}
$this->choices = make_categories_options();
return true;
}
}