Merge branch 'install_21_STABLE' of git://github.com/amosbot/moodle into MOODLE_21_STABLE
[moodle.git] / enrol / ldap / settingslib.php
blob62cae7094b4d3235e255186a5ae571863cee3d2f
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * LDAP enrolment plugin admin setting classes
21 * @package enrol
22 * @subpackage ldap
23 * @author Iñaki Arenaza
24 * @copyright 2010 Iñaki Arenaza <iarenaza@eps.mondragon.edu>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 class admin_setting_configtext_trim_lower extends admin_setting_configtext {
31 /* @var boolean whether to lowercase the value or not before writing in to the db */
32 private $lowercase;
34 /**
35 * Constructor: uses parent::__construct
37 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
38 * @param string $visiblename localised
39 * @param string $description long localised info
40 * @param string $defaultsetting default value for the setting
41 * @param boolean $lowercase if true, lowercase the value before writing it to the db.
43 public function __construct($name, $visiblename, $description, $defaultsetting, $lowercase=false) {
44 $this->lowercase = $lowercase;
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 = moodle_strtolower($data);
68 return ($this->config_write($this->name, trim($data)) ? '' : get_string('errorsetting', 'admin'));
72 class admin_setting_ldap_rolemapping extends admin_setting {
74 /**
75 * Constructor: uses parent::__construct
77 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
78 * @param string $visiblename localised
79 * @param string $description long localised info
80 * @param string $defaultsetting default value for the setting (actually unused)
82 public function __construct($name, $visiblename, $description, $defaultsetting) {
83 parent::__construct($name, $visiblename, $description, $defaultsetting);
86 /**
87 * Returns the current setting if it is set
89 * @return mixed null if null, else an array
91 public function get_setting() {
92 $roles = get_all_roles();
93 $result = array();
94 foreach ($roles as $role) {
95 $contexts = $this->config_read('contexts_role'.$role->id);
96 $memberattribute = $this->config_read('memberattribute_role'.$role->id);
97 $result[] = array('id' => $role->id,
98 'name' => $role->name,
99 'contexts' => $contexts,
100 'memberattribute' => $memberattribute);
102 return $result;
106 * Saves the setting(s) provided in $data
108 * @param array $data An array of data, if not array returns empty str
109 * @return mixed empty string on useless data or success, error string if failed
111 public function write_setting($data) {
112 if(!is_array($data)) {
113 return ''; // ignore it
116 $result = '';
117 foreach ($data as $roleid => $data) {
118 if (!$this->config_write('contexts_role'.$roleid, trim($data['contexts']))) {
119 $return = get_string('errorsetting', 'admin');
121 if (!$this->config_write('memberattribute_role'.$roleid, moodle_strtolower(trim($data['memberattribute'])))) {
122 $return = get_string('errorsetting', 'admin');
125 return $result;
129 * Returns XHTML field(s) as required by choices
131 * Relies on data being an array should data ever be another valid vartype with
132 * acceptable value this may cause a warning/error
133 * if (!is_array($data)) would fix the problem
135 * @todo Add vartype handling to ensure $data is an array
137 * @param array $data An array of checked values
138 * @param string $query
139 * @return string XHTML field
141 public function output_html($data, $query='') {
142 $return = '<div style="float:left; width:auto; margin-right: 0.5em;">';
143 $return .= '<div style="height: 2em;">'.get_string('roles', 'role').'</div>';
144 foreach ($data as $role) {
145 $return .= '<div style="height: 2em;">'.s($role['name']).'</div>';
147 $return .= '</div>';
149 $return .= '<div style="float:left; width:auto; margin-right: 0.5em;">';
150 $return .= '<div style="height: 2em;">'.get_string('contexts', 'enrol_ldap').'</div>';
151 foreach ($data as $role) {
152 $contextid = $this->get_id().'['.$role['id'].'][contexts]';
153 $contextname = $this->get_full_name().'['.$role['id'].'][contexts]';
154 $return .= '<div style="height: 2em;"><input type="text" size="40" id="'.$contextid.'" name="'.$contextname.'" value="'.s($role['contexts']).'"/></div>';
156 $return .= '</div>';
158 $return .= '<div style="float:left; width:auto; margin-right: 0.5em;">';
159 $return .= '<div style="height: 2em;">'.get_string('memberattribute', 'enrol_ldap').'</div>';
160 foreach ($data as $role) {
161 $memberattrid = $this->get_id().'['.$role['id'].'][memberattribute]';
162 $memberattrname = $this->get_full_name().'['.$role['id'].'][memberattribute]';
163 $return .= '<div style="height: 2em;"><input type="text" size="15" id="'.$memberattrid.'" name="'.$memberattrname.'" value="'.s($role['memberattribute']).'"/></div>';
165 $return .= '</div>';
166 $return .= '<div style="clear:both;"></div>';
168 return format_admin_setting($this, $this->visiblename, $return,
169 $this->description, true, '', '', $query);