Merge branch 'MDL-59015-master' of git://github.com/mihailges/moodle
[moodle.git] / enrol / ldap / settingslib.php
blob79a237fd41f3f8acff8443affed92f47257bdc6d
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.
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/>.
17 /**
18 * LDAP enrolment plugin admin setting classes
20 * @package enrol_ldap
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 */
30 private $lowercase;
32 /**
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);
48 /**
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
57 $data = 0;
60 // $data is a string
61 $validated = $this->validate($data);
62 if ($validated !== true) {
63 return $validated;
65 if ($this->lowercase) {
66 $data = core_text::strtolower($data);
68 if (!$this->enabled) {
69 return '';
71 return ($this->config_write($this->name, trim($data)) ? '' : get_string('errorsetting', 'admin'));
76 class admin_setting_ldap_rolemapping extends admin_setting {
78 /**
79 * Constructor: uses parent::__construct
81 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
82 * @param string $visiblename localised
83 * @param string $description long localised info
84 * @param string $defaultsetting default value for the setting (actually unused)
86 public function __construct($name, $visiblename, $description, $defaultsetting) {
87 parent::__construct($name, $visiblename, $description, $defaultsetting);
90 /**
91 * Returns the current setting if it is set
93 * @return mixed null if null, else an array
95 public function get_setting() {
96 $roles = role_fix_names(get_all_roles());
97 $result = array();
98 foreach ($roles as $role) {
99 $contexts = $this->config_read('contexts_role'.$role->id);
100 $memberattribute = $this->config_read('memberattribute_role'.$role->id);
101 $result[] = array('id' => $role->id,
102 'name' => $role->localname,
103 'contexts' => $contexts,
104 'memberattribute' => $memberattribute);
106 return $result;
110 * Saves the setting(s) provided in $data
112 * @param array $data An array of data, if not array returns empty str
113 * @return mixed empty string on useless data or success, error string if failed
115 public function write_setting($data) {
116 if(!is_array($data)) {
117 return ''; // ignore it
120 $result = '';
121 foreach ($data as $roleid => $data) {
122 if (!$this->config_write('contexts_role'.$roleid, trim($data['contexts']))) {
123 $return = get_string('errorsetting', 'admin');
125 if (!$this->config_write('memberattribute_role'.$roleid, core_text::strtolower(trim($data['memberattribute'])))) {
126 $return = get_string('errorsetting', 'admin');
129 return $result;
133 * Returns XHTML field(s) as required by choices
135 * Relies on data being an array should data ever be another valid vartype with
136 * acceptable value this may cause a warning/error
137 * if (!is_array($data)) would fix the problem
139 * @todo Add vartype handling to ensure $data is an array
141 * @param array $data An array of checked values
142 * @param string $query
143 * @return string XHTML field
145 public function output_html($data, $query='') {
146 $return = html_writer::start_tag('div', array('style' =>'float:left; width:auto; margin-right: 0.5em;'));
147 $return .= html_writer::tag('div', get_string('roles', 'role'), array('style' => 'height: 2em;'));
148 foreach ($data as $role) {
149 $return .= html_writer::tag('div', s($role['name']), array('style' => 'height: 2em;'));
151 $return .= html_writer::end_tag('div');
153 $return .= html_writer::start_tag('div', array('style' => 'float:left; width:auto; margin-right: 0.5em;'));
154 $return .= html_writer::tag('div', get_string('contexts', 'enrol_ldap'), array('style' => 'height: 2em;'));
155 foreach ($data as $role) {
156 $contextid = $this->get_id().'['.$role['id'].'][contexts]';
157 $contextname = $this->get_full_name().'['.$role['id'].'][contexts]';
158 $return .= html_writer::start_tag('div', array('style' => 'height: 2em;'));
159 $return .= html_writer::label(get_string('role_mapping_context', 'enrol_ldap', $role['name']), $contextid, false, array('class' => 'accesshide'));
160 $attrs = array('type' => 'text', 'size' => '40', 'id' => $contextid, 'name' => $contextname,
161 'value' => s($role['contexts']), 'class' => 'text-ltr');
162 $return .= html_writer::empty_tag('input', $attrs);
163 $return .= html_writer::end_tag('div');
165 $return .= html_writer::end_tag('div');
167 $return .= html_writer::start_tag('div', array('style' => 'float:left; width:auto; margin-right: 0.5em;'));
168 $return .= html_writer::tag('div', get_string('memberattribute', 'enrol_ldap'), array('style' => 'height: 2em;'));
169 foreach ($data as $role) {
170 $memberattrid = $this->get_id().'['.$role['id'].'][memberattribute]';
171 $memberattrname = $this->get_full_name().'['.$role['id'].'][memberattribute]';
172 $return .= html_writer::start_tag('div', array('style' => 'height: 2em;'));
173 $return .= html_writer::label(get_string('role_mapping_attribute', 'enrol_ldap', $role['name']), $memberattrid, false, array('class' => 'accesshide'));
174 $attrs = array('type' => 'text', 'size' => '15', 'id' => $memberattrid, 'name' => $memberattrname,
175 'value' => s($role['memberattribute']), 'class' => 'text-ltr');
176 $return .= html_writer::empty_tag('input', $attrs);
177 $return .= html_writer::end_tag('div');
179 $return .= html_writer::end_tag('div');
180 $return .= html_writer::tag('div', '', array('style' => 'clear:both;'));
182 return format_admin_setting($this, $this->visiblename, $return,
183 $this->description, true, '', '', $query);
188 * Class implements new specialized setting for course categories that are loaded
189 * only when required
190 * @author Darko Miletic
193 class enrol_ldap_admin_setting_category extends admin_setting_configselect {
194 public function __construct($name, $visiblename, $description) {
195 parent::__construct($name, $visiblename, $description, 1, null);
198 public function load_choices() {
199 if (is_array($this->choices)) {
200 return true;
203 $this->choices = make_categories_options();
204 return true;