Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  /**
  18   * LDAP enrolment plugin admin setting classes
  19   *
  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
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  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;
  31  
  32      /**  @var bool To store enable/disabled status of the input field. */
  33      protected $enabled;
  34  
  35      /**
  36       * Constructor: uses parent::__construct
  37       *
  38       * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
  39       * @param string $visiblename localised
  40       * @param string $description long localised info
  41       * @param string $defaultsetting default value for the setting
  42       * @param boolean $lowercase if true, lowercase the value before writing it to the db.
  43       * @param boolean $enabled if true, the input field is enabled, otherwise it's disabled.
  44       */
  45      public function __construct($name, $visiblename, $description, $defaultsetting, $lowercase=false, $enabled=true) {
  46          $this->lowercase = $lowercase;
  47          $this->enabled = $enabled;
  48          parent::__construct($name, $visiblename, $description, $defaultsetting);
  49      }
  50  
  51      /**
  52       * Saves the setting(s) provided in $data
  53       *
  54       * @param array $data An array of data, if not array returns empty str
  55       * @return mixed empty string on useless data or success, error string if failed
  56       */
  57      public function write_setting($data) {
  58          if ($this->paramtype === PARAM_INT and $data === '') {
  59              // do not complain if '' used instead of 0
  60              $data = 0;
  61          }
  62  
  63          // $data is a string
  64          $validated = $this->validate($data);
  65          if ($validated !== true) {
  66              return $validated;
  67          }
  68          if ($this->lowercase) {
  69              $data = core_text::strtolower($data);
  70          }
  71          if (!$this->enabled) {
  72              return '';
  73          }
  74          return ($this->config_write($this->name, trim($data)) ? '' : get_string('errorsetting', 'admin'));
  75      }
  76  
  77  }
  78  
  79  class admin_setting_ldap_rolemapping extends admin_setting {
  80  
  81      /**
  82       * Constructor: uses parent::__construct
  83       *
  84       * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
  85       * @param string $visiblename localised
  86       * @param string $description long localised info
  87       * @param string $defaultsetting default value for the setting (actually unused)
  88       */
  89      public function __construct($name, $visiblename, $description, $defaultsetting) {
  90          parent::__construct($name, $visiblename, $description, $defaultsetting);
  91      }
  92  
  93      /**
  94       * Returns the current setting if it is set
  95       *
  96       * @return mixed null if null, else an array
  97       */
  98      public function get_setting() {
  99          $roles = role_fix_names(get_all_roles());
 100          $result = array();
 101          foreach ($roles as $role) {
 102              $contexts = $this->config_read('contexts_role'.$role->id);
 103              $memberattribute = $this->config_read('memberattribute_role'.$role->id);
 104              $result[] = array('id' => $role->id,
 105                                'name' => $role->localname,
 106                                'contexts' => $contexts,
 107                                'memberattribute' => $memberattribute);
 108          }
 109          return $result;
 110      }
 111  
 112      /**
 113       * Saves the setting(s) provided in $data
 114       *
 115       * @param array $data An array of data, if not array returns empty str
 116       * @return mixed empty string on useless data or success, error string if failed
 117       */
 118      public function write_setting($data) {
 119          if(!is_array($data)) {
 120              return ''; // ignore it
 121          }
 122  
 123          $result = '';
 124          foreach ($data as $roleid => $data) {
 125              if (!$this->config_write('contexts_role'.$roleid, trim($data['contexts']))) {
 126                  $return = get_string('errorsetting', 'admin');
 127              }
 128              if (!$this->config_write('memberattribute_role'.$roleid, core_text::strtolower(trim($data['memberattribute'])))) {
 129                  $return = get_string('errorsetting', 'admin');
 130              }
 131          }
 132          return $result;
 133      }
 134  
 135      /**
 136       * Returns XHTML field(s) as required by choices
 137       *
 138       * Relies on data being an array should data ever be another valid vartype with
 139       * acceptable value this may cause a warning/error
 140       * if (!is_array($data)) would fix the problem
 141       *
 142       * @todo Add vartype handling to ensure $data is an array
 143       *
 144       * @param array $data An array of checked values
 145       * @param string $query
 146       * @return string XHTML field
 147       */
 148      public function output_html($data, $query='') {
 149          $return  = html_writer::start_tag('div', array('style' =>'float:left; width:auto; margin-right: 0.5em;'));
 150          $return .= html_writer::tag('div', get_string('roles', 'role'), array('style' => 'height: 2em;'));
 151          foreach ($data as $role) {
 152              $return .= html_writer::tag('div', s($role['name']), array('style' => 'height: 2em;'));
 153          }
 154          $return .= html_writer::end_tag('div');
 155  
 156          $return .= html_writer::start_tag('div', array('style' => 'float:left; width:auto; margin-right: 0.5em;'));
 157          $return .= html_writer::tag('div', get_string('contexts', 'enrol_ldap'), array('style' => 'height: 2em;'));
 158          foreach ($data as $role) {
 159              $contextid = $this->get_id().'['.$role['id'].'][contexts]';
 160              $contextname = $this->get_full_name().'['.$role['id'].'][contexts]';
 161              $return .= html_writer::start_tag('div', array('style' => 'height: 2em;'));
 162              $return .= html_writer::label(get_string('role_mapping_context', 'enrol_ldap', $role['name']), $contextid, false, array('class' => 'accesshide'));
 163              $attrs = array('type' => 'text', 'size' => '40', 'id' => $contextid, 'name' => $contextname,
 164                  'value' => s($role['contexts']), 'class' => 'text-ltr');
 165              $return .= html_writer::empty_tag('input', $attrs);
 166              $return .= html_writer::end_tag('div');
 167          }
 168          $return .= html_writer::end_tag('div');
 169  
 170          $return .= html_writer::start_tag('div', array('style' => 'float:left; width:auto; margin-right: 0.5em;'));
 171          $return .= html_writer::tag('div', get_string('memberattribute', 'enrol_ldap'), array('style' => 'height: 2em;'));
 172          foreach ($data as $role) {
 173              $memberattrid = $this->get_id().'['.$role['id'].'][memberattribute]';
 174              $memberattrname = $this->get_full_name().'['.$role['id'].'][memberattribute]';
 175              $return .= html_writer::start_tag('div', array('style' => 'height: 2em;'));
 176              $return .= html_writer::label(get_string('role_mapping_attribute', 'enrol_ldap', $role['name']), $memberattrid, false, array('class' => 'accesshide'));
 177              $attrs = array('type' => 'text', 'size' => '15', 'id' => $memberattrid, 'name' => $memberattrname,
 178                  'value' => s($role['memberattribute']), 'class' => 'text-ltr');
 179              $return .= html_writer::empty_tag('input', $attrs);
 180              $return .= html_writer::end_tag('div');
 181          }
 182          $return .= html_writer::end_tag('div');
 183          $return .= html_writer::tag('div', '', array('style' => 'clear:both;'));
 184  
 185          return format_admin_setting($this, $this->visiblename, $return,
 186                                      $this->description, true, '', '', $query);
 187      }
 188  }
 189  
 190  /**
 191   * Class implements new specialized setting for course categories that are loaded
 192   * only when required
 193   * @author Darko Miletic
 194   *
 195   */
 196  class enrol_ldap_admin_setting_category extends admin_setting_configselect {
 197      public function __construct($name, $visiblename, $description) {
 198          parent::__construct($name, $visiblename, $description, 1, null);
 199      }
 200  
 201      public function load_choices() {
 202          if (is_array($this->choices)) {
 203              return true;
 204          }
 205  
 206          $this->choices = core_course_category::make_categories_list('', 0, ' / ');
 207          return true;
 208      }
 209  }