2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * LDAP enrolment plugin admin setting classes
21 * @author Iñaki Arenaza
22 * @copyright 2010 Iñaki Arenaza <iarenaza@eps.mondragon.edu>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 class admin_setting_configtext_trim_lower
extends admin_setting_configtext
{
29 /* @var boolean whether to lowercase the value or not before writing in to the db */
33 * Constructor: uses parent::__construct
35 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
36 * @param string $visiblename localised
37 * @param string $description long localised info
38 * @param string $defaultsetting default value for the setting
39 * @param boolean $lowercase if true, lowercase the value before writing it to the db.
40 * @param boolean $enabled if true, the input field is enabled, otherwise it's disabled.
42 public function __construct($name, $visiblename, $description, $defaultsetting, $lowercase=false, $enabled=true) {
43 $this->lowercase
= $lowercase;
44 $this->enabled
= $enabled;
45 parent
::__construct($name, $visiblename, $description, $defaultsetting);
49 * Saves the setting(s) provided in $data
51 * @param array $data An array of data, if not array returns empty str
52 * @return mixed empty string on useless data or success, error string if failed
54 public function write_setting($data) {
55 if ($this->paramtype
=== PARAM_INT
and $data === '') {
56 // do not complain if '' used instead of 0
61 $validated = $this->validate($data);
62 if ($validated !== true) {
65 if ($this->lowercase
) {
66 $data = core_text
::strtolower($data);
68 if (!$this->enabled
) {
71 return ($this->config_write($this->name
, trim($data)) ?
'' : get_string('errorsetting', 'admin'));
75 * Return an XHTML string for the setting
76 * @return string Returns an XHTML string
78 public function output_html($data, $query='') {
79 $default = $this->get_defaultsetting();
80 $disabled = $this->enabled ?
'': ' disabled="disabled"';
81 return format_admin_setting($this, $this->visiblename
,
82 '<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>',
83 $this->description
, true, '', $default, $query);
88 class admin_setting_ldap_rolemapping
extends admin_setting
{
91 * Constructor: uses parent::__construct
93 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
94 * @param string $visiblename localised
95 * @param string $description long localised info
96 * @param string $defaultsetting default value for the setting (actually unused)
98 public function __construct($name, $visiblename, $description, $defaultsetting) {
99 parent
::__construct($name, $visiblename, $description, $defaultsetting);
103 * Returns the current setting if it is set
105 * @return mixed null if null, else an array
107 public function get_setting() {
108 $roles = role_fix_names(get_all_roles());
110 foreach ($roles as $role) {
111 $contexts = $this->config_read('contexts_role'.$role->id
);
112 $memberattribute = $this->config_read('memberattribute_role'.$role->id
);
113 $result[] = array('id' => $role->id
,
114 'name' => $role->localname
,
115 'contexts' => $contexts,
116 'memberattribute' => $memberattribute);
122 * Saves the setting(s) provided in $data
124 * @param array $data An array of data, if not array returns empty str
125 * @return mixed empty string on useless data or success, error string if failed
127 public function write_setting($data) {
128 if(!is_array($data)) {
129 return ''; // ignore it
133 foreach ($data as $roleid => $data) {
134 if (!$this->config_write('contexts_role'.$roleid, trim($data['contexts']))) {
135 $return = get_string('errorsetting', 'admin');
137 if (!$this->config_write('memberattribute_role'.$roleid, core_text
::strtolower(trim($data['memberattribute'])))) {
138 $return = get_string('errorsetting', 'admin');
145 * Returns XHTML field(s) as required by choices
147 * Relies on data being an array should data ever be another valid vartype with
148 * acceptable value this may cause a warning/error
149 * if (!is_array($data)) would fix the problem
151 * @todo Add vartype handling to ensure $data is an array
153 * @param array $data An array of checked values
154 * @param string $query
155 * @return string XHTML field
157 public function output_html($data, $query='') {
158 $return = html_writer
::start_tag('div', array('style' =>'float:left; width:auto; margin-right: 0.5em;'));
159 $return .= html_writer
::tag('div', get_string('roles', 'role'), array('style' => 'height: 2em;'));
160 foreach ($data as $role) {
161 $return .= html_writer
::tag('div', s($role['name']), array('style' => 'height: 2em;'));
163 $return .= html_writer
::end_tag('div');
165 $return .= html_writer
::start_tag('div', array('style' => 'float:left; width:auto; margin-right: 0.5em;'));
166 $return .= html_writer
::tag('div', get_string('contexts', 'enrol_ldap'), array('style' => 'height: 2em;'));
167 foreach ($data as $role) {
168 $contextid = $this->get_id().'['.$role['id'].'][contexts]';
169 $contextname = $this->get_full_name().'['.$role['id'].'][contexts]';
170 $return .= html_writer
::start_tag('div', array('style' => 'height: 2em;'));
171 $return .= html_writer
::label(get_string('role_mapping_context', 'enrol_ldap', $role['name']), $contextid, false, array('class' => 'accesshide'));
172 $attrs = array('type' => 'text', 'size' => '40', 'id' => $contextid, 'name' => $contextname, 'value' => s($role['contexts']));
173 $return .= html_writer
::empty_tag('input', $attrs);
174 $return .= html_writer
::end_tag('div');
176 $return .= html_writer
::end_tag('div');
178 $return .= html_writer
::start_tag('div', array('style' => 'float:left; width:auto; margin-right: 0.5em;'));
179 $return .= html_writer
::tag('div', get_string('memberattribute', 'enrol_ldap'), array('style' => 'height: 2em;'));
180 foreach ($data as $role) {
181 $memberattrid = $this->get_id().'['.$role['id'].'][memberattribute]';
182 $memberattrname = $this->get_full_name().'['.$role['id'].'][memberattribute]';
183 $return .= html_writer
::start_tag('div', array('style' => 'height: 2em;'));
184 $return .= html_writer
::label(get_string('role_mapping_attribute', 'enrol_ldap', $role['name']), $memberattrid, false, array('class' => 'accesshide'));
185 $attrs = array('type' => 'text', 'size' => '15', 'id' => $memberattrid, 'name' => $memberattrname, 'value' => s($role['memberattribute']));
186 $return .= html_writer
::empty_tag('input', $attrs);
187 $return .= html_writer
::end_tag('div');
189 $return .= html_writer
::end_tag('div');
190 $return .= html_writer
::tag('div', '', array('style' => 'clear:both;'));
192 return format_admin_setting($this, $this->visiblename
, $return,
193 $this->description
, true, '', '', $query);