Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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      /**
  33       * Constructor: uses parent::__construct
  34       *
  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.
  41       */
  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);
  46      }
  47  
  48      /**
  49       * Saves the setting(s) provided in $data
  50       *
  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
  53       */
  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;
  58          }
  59  
  60          // $data is a string
  61          $validated = $this->validate($data);
  62          if ($validated !== true) {
  63              return $validated;
  64          }
  65          if ($this->lowercase) {
  66              $data = core_text::strtolower($data);
  67          }
  68          if (!$this->enabled) {
  69              return '';
  70          }
  71          return ($this->config_write($this->name, trim($data)) ? '' : get_string('errorsetting', 'admin'));
  72      }
  73  
  74  }
  75  
  76  class admin_setting_ldap_rolemapping extends admin_setting {
  77  
  78      /**
  79       * Constructor: uses parent::__construct
  80       *
  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)
  85       */
  86      public function __construct($name, $visiblename, $description, $defaultsetting) {
  87          parent::__construct($name, $visiblename, $description, $defaultsetting);
  88      }
  89  
  90      /**
  91       * Returns the current setting if it is set
  92       *
  93       * @return mixed null if null, else an array
  94       */
  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);
 105          }
 106          return $result;
 107      }
 108  
 109      /**
 110       * Saves the setting(s) provided in $data
 111       *
 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
 114       */
 115      public function write_setting($data) {
 116          if(!is_array($data)) {
 117              return ''; // ignore it
 118          }
 119  
 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');
 124              }
 125              if (!$this->config_write('memberattribute_role'.$roleid, core_text::strtolower(trim($data['memberattribute'])))) {
 126                  $return = get_string('errorsetting', 'admin');
 127              }
 128          }
 129          return $result;
 130      }
 131  
 132      /**
 133       * Returns XHTML field(s) as required by choices
 134       *
 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
 138       *
 139       * @todo Add vartype handling to ensure $data is an array
 140       *
 141       * @param array $data An array of checked values
 142       * @param string $query
 143       * @return string XHTML field
 144       */
 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;'));
 150          }
 151          $return .= html_writer::end_tag('div');
 152  
 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');
 164          }
 165          $return .= html_writer::end_tag('div');
 166  
 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');
 178          }
 179          $return .= html_writer::end_tag('div');
 180          $return .= html_writer::tag('div', '', array('style' => 'clear:both;'));
 181  
 182          return format_admin_setting($this, $this->visiblename, $return,
 183                                      $this->description, true, '', '', $query);
 184      }
 185  }
 186  
 187  /**
 188   * Class implements new specialized setting for course categories that are loaded
 189   * only when required
 190   * @author Darko Miletic
 191   *
 192   */
 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);
 196      }
 197  
 198      public function load_choices() {
 199          if (is_array($this->choices)) {
 200              return true;
 201          }
 202  
 203          $this->choices = core_course_category::make_categories_list('', 0, ' / ');
 204          return true;
 205      }
 206  }