PageRenderTime 28ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle/enrol/ldap/settingslib.php

#
PHP | 171 lines | 82 code | 16 blank | 73 comment | 7 complexity | 786290ad6d3e08bba6fee3231c8ffa5e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0
  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.
  13. //
  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/>.
  16. /**
  17. * LDAP enrolment plugin admin setting classes
  18. *
  19. * @package enrol
  20. * @subpackage 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
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. class admin_setting_configtext_trim_lower extends admin_setting_configtext {
  27. /* @var boolean whether to lowercase the value or not before writing in to the db */
  28. private $lowercase;
  29. /**
  30. * Constructor: uses parent::__construct
  31. *
  32. * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
  33. * @param string $visiblename localised
  34. * @param string $description long localised info
  35. * @param string $defaultsetting default value for the setting
  36. * @param boolean $lowercase if true, lowercase the value before writing it to the db.
  37. */
  38. public function __construct($name, $visiblename, $description, $defaultsetting, $lowercase=false) {
  39. $this->lowercase = $lowercase;
  40. parent::__construct($name, $visiblename, $description, $defaultsetting);
  41. }
  42. /**
  43. * Saves the setting(s) provided in $data
  44. *
  45. * @param array $data An array of data, if not array returns empty str
  46. * @return mixed empty string on useless data or success, error string if failed
  47. */
  48. public function write_setting($data) {
  49. if ($this->paramtype === PARAM_INT and $data === '') {
  50. // do not complain if '' used instead of 0
  51. $data = 0;
  52. }
  53. // $data is a string
  54. $validated = $this->validate($data);
  55. if ($validated !== true) {
  56. return $validated;
  57. }
  58. if ($this->lowercase) {
  59. $data = moodle_strtolower($data);
  60. }
  61. return ($this->config_write($this->name, trim($data)) ? '' : get_string('errorsetting', 'admin'));
  62. }
  63. }
  64. class admin_setting_ldap_rolemapping extends admin_setting {
  65. /**
  66. * Constructor: uses parent::__construct
  67. *
  68. * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
  69. * @param string $visiblename localised
  70. * @param string $description long localised info
  71. * @param string $defaultsetting default value for the setting (actually unused)
  72. */
  73. public function __construct($name, $visiblename, $description, $defaultsetting) {
  74. parent::__construct($name, $visiblename, $description, $defaultsetting);
  75. }
  76. /**
  77. * Returns the current setting if it is set
  78. *
  79. * @return mixed null if null, else an array
  80. */
  81. public function get_setting() {
  82. $roles = get_all_roles();
  83. $result = array();
  84. foreach ($roles as $role) {
  85. $contexts = $this->config_read('contexts_role'.$role->id);
  86. $memberattribute = $this->config_read('memberattribute_role'.$role->id);
  87. $result[] = array('id' => $role->id,
  88. 'name' => $role->name,
  89. 'contexts' => $contexts,
  90. 'memberattribute' => $memberattribute);
  91. }
  92. return $result;
  93. }
  94. /**
  95. * Saves the setting(s) provided in $data
  96. *
  97. * @param array $data An array of data, if not array returns empty str
  98. * @return mixed empty string on useless data or success, error string if failed
  99. */
  100. public function write_setting($data) {
  101. if(!is_array($data)) {
  102. return ''; // ignore it
  103. }
  104. $result = '';
  105. foreach ($data as $roleid => $data) {
  106. if (!$this->config_write('contexts_role'.$roleid, trim($data['contexts']))) {
  107. $return = get_string('errorsetting', 'admin');
  108. }
  109. if (!$this->config_write('memberattribute_role'.$roleid, moodle_strtolower(trim($data['memberattribute'])))) {
  110. $return = get_string('errorsetting', 'admin');
  111. }
  112. }
  113. return $result;
  114. }
  115. /**
  116. * Returns XHTML field(s) as required by choices
  117. *
  118. * Relies on data being an array should data ever be another valid vartype with
  119. * acceptable value this may cause a warning/error
  120. * if (!is_array($data)) would fix the problem
  121. *
  122. * @todo Add vartype handling to ensure $data is an array
  123. *
  124. * @param array $data An array of checked values
  125. * @param string $query
  126. * @return string XHTML field
  127. */
  128. public function output_html($data, $query='') {
  129. $return = '<div style="float:left; width:auto; margin-right: 0.5em;">';
  130. $return .= '<div style="height: 2em;">'.get_string('roles', 'role').'</div>';
  131. foreach ($data as $role) {
  132. $return .= '<div style="height: 2em;">'.s($role['name']).'</div>';
  133. }
  134. $return .= '</div>';
  135. $return .= '<div style="float:left; width:auto; margin-right: 0.5em;">';
  136. $return .= '<div style="height: 2em;">'.get_string('contexts', 'enrol_ldap').'</div>';
  137. foreach ($data as $role) {
  138. $contextid = $this->get_id().'['.$role['id'].'][contexts]';
  139. $contextname = $this->get_full_name().'['.$role['id'].'][contexts]';
  140. $return .= '<div style="height: 2em;"><input type="text" size="40" id="'.$contextid.'" name="'.$contextname.'" value="'.s($role['contexts']).'"/></div>';
  141. }
  142. $return .= '</div>';
  143. $return .= '<div style="float:left; width:auto; margin-right: 0.5em;">';
  144. $return .= '<div style="height: 2em;">'.get_string('memberattribute', 'enrol_ldap').'</div>';
  145. foreach ($data as $role) {
  146. $memberattrid = $this->get_id().'['.$role['id'].'][memberattribute]';
  147. $memberattrname = $this->get_full_name().'['.$role['id'].'][memberattribute]';
  148. $return .= '<div style="height: 2em;"><input type="text" size="15" id="'.$memberattrid.'" name="'.$memberattrname.'" value="'.s($role['memberattribute']).'"/></div>';
  149. }
  150. $return .= '</div>';
  151. $return .= '<div style="clear:both;"></div>';
  152. return format_admin_setting($this, $this->visiblename, $return,
  153. $this->description, true, '', '', $query);
  154. }
  155. }