Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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  namespace core_adminpresets\local\setting;
  18  
  19  use admin_setting;
  20  use moodle_exception;
  21  use stdClass;
  22  
  23  /**
  24   * Admin tool presets plugin to load some settings.
  25   *
  26   * @package          core_adminpresets
  27   * @copyright        2021 Pimenko <support@pimenko.com><pimenko.com>
  28   * @author           Jordan Kesraoui | Sylvain Revenu | Pimenko based on David MonllaĆ³ <david.monllao@urv.cat> code
  29   * @license          http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class adminpresets_setting {
  32  
  33      /**
  34       * @var admin_setting
  35       */
  36      protected $settingdata;
  37  
  38      /**
  39       * @var delegation
  40       */
  41      protected $delegation;
  42  
  43      /**
  44       * The setting DB value
  45       *
  46       * @var mixed
  47       */
  48      protected $value;
  49  
  50      /**
  51       * Stores the visible value of the setting DB value
  52       *
  53       * @var string
  54       */
  55      protected $visiblevalue;
  56  
  57      /**
  58       * For multiple value settings, used to look for the other values
  59       *
  60       * @var string
  61       */
  62      protected $attributes = false;
  63  
  64      /**
  65       * To store the setting attributes
  66       *
  67       * @var array
  68       */
  69      protected $attributesvalues;
  70  
  71      /**
  72       * Stores the setting data and the selected value
  73       *
  74       * @param admin_setting $settingdata admin_setting subclass
  75       * @param mixed $dbsettingvalue Actual value
  76       */
  77      public function __construct(admin_setting $settingdata, $dbsettingvalue) {
  78          $this->settingdata = $settingdata;
  79          $this->delegation = new delegation();
  80  
  81          if ($this->settingdata->plugin == '') {
  82              $this->settingdata->plugin = 'none';
  83          }
  84  
  85          // Applies specific children behaviors.
  86          $this->set_behaviors();
  87          $this->apply_behaviors();
  88  
  89          // Cleaning value.
  90          $this->set_value($dbsettingvalue);
  91      }
  92  
  93      /**
  94       * Each class can overwrite this method to specify extra processes
  95       */
  96      protected function set_behaviors() {
  97      }
  98  
  99      /**
 100       * Applies the children class specific behaviors
 101       *
 102       * See delegation class for the available extra behaviors
 103       */
 104      protected function apply_behaviors() {
 105          if (!empty($this->behaviors)) {
 106  
 107              foreach ($this->behaviors as $behavior => $arguments) {
 108  
 109                  // The arguments of the behavior depends on the caller.
 110                  $methodname = 'extra_' . $behavior;
 111                  $this->delegation->{$methodname}($arguments);
 112              }
 113          }
 114      }
 115  
 116      /**
 117       * Gets the setting value.
 118       *
 119       * @return mixed The setting value
 120       */
 121      public function get_value() {
 122          return $this->value;
 123      }
 124  
 125      /**
 126       * Sets the setting value cleaning it
 127       *
 128       * Child classes should overwrite method to clean more acurately
 129       *
 130       * @param mixed $value Setting value
 131       * @return mixed Returns false if wrong param value
 132       */
 133      protected function set_value($value) {
 134          $this->value = $value;
 135          $this->set_visiblevalue();
 136      }
 137  
 138      public function get_visiblevalue() {
 139          return $this->visiblevalue;
 140      }
 141  
 142      /**
 143       * Sets the visible name for the setting selected value
 144       *
 145       * In most cases the child classes will overwrite
 146       */
 147      protected function set_visiblevalue() {
 148          $this->visiblevalue = $this->value;
 149      }
 150  
 151      public function get_attributes() {
 152          return $this->attributes;
 153      }
 154  
 155      public function get_attributes_values() {
 156          return $this->attributesvalues;
 157      }
 158  
 159      public function get_settingdata() {
 160          return $this->settingdata;
 161      }
 162  
 163      public function set_attribute_value($name, $value) {
 164          $this->attributesvalues[$name] = $value;
 165      }
 166  
 167      /**
 168       * Saves the setting attributes values
 169       *
 170       * @return     array        Array of inserted ids (in config_log)
 171       */
 172      public function save_attributes_values() {
 173          // Plugin name or null.
 174          $plugin = $this->settingdata->plugin;
 175          if ($plugin == 'none' || $plugin == '') {
 176              $plugin = null;
 177          }
 178  
 179          if (!$this->attributesvalues) {
 180              return false;
 181          }
 182  
 183          // To store inserted ids.
 184          $ids = [];
 185          foreach ($this->attributesvalues as $name => $value) {
 186  
 187              // Getting actual setting.
 188              $actualsetting = get_config($plugin, $name);
 189  
 190              // If it's the actual setting get off.
 191              if ($value == $actualsetting) {
 192                  return false;
 193              }
 194  
 195              if ($id = $this->save_value($name, $value)) {
 196                  $ids[] = $id;
 197              }
 198          }
 199  
 200          return $ids;
 201      }
 202  
 203      /**
 204       * Stores the setting into database, logs the change and returns the config_log inserted id
 205       *
 206       * @param bool $name Setting name to store.
 207       * @param mixed $value Setting value to store.
 208       * @return int|false config_log inserted id or false whenever the new value is the same as old value.
 209       * @throws dml_exception
 210       * @throws moodle_exception
 211       */
 212      public function save_value($name = false, $value = null) {
 213          // Object values if no arguments.
 214          if ($value === null) {
 215              $value = $this->value;
 216          }
 217          if (!$name) {
 218              $name = $this->settingdata->name;
 219          }
 220  
 221          // Plugin name or null.
 222          $plugin = $this->settingdata->plugin;
 223          if ($plugin == 'none' || $plugin == '') {
 224              $plugin = null;
 225          }
 226  
 227          // Getting the actual value.
 228          $actualvalue = get_config($plugin, $name);
 229  
 230          // If it's the same it's not necessary.
 231          if ($actualvalue == $value) {
 232              return false;
 233          }
 234  
 235          set_config($name, $value, $plugin);
 236  
 237          return $this->to_log($plugin, $name, $value, $actualvalue);
 238      }
 239  
 240      /**
 241       * Copy of config_write method of the admin_setting class
 242       *
 243       * @param string $plugin
 244       * @param string $name
 245       * @param mixed $value
 246       * @param mixed $actualvalue
 247       * @return  integer The stored config_log id
 248       */
 249      protected function to_log($plugin, $name, $value, $actualvalue) {
 250          global $DB, $USER;
 251  
 252          // Log the change (pasted from admin_setting class).
 253          $log = new stdClass();
 254          $log->userid = during_initial_install() ? 0 : $USER->id; // 0 as user id during install.
 255          $log->timemodified = time();
 256          $log->plugin = $plugin;
 257          $log->name = $name;
 258          $log->value = $value;
 259          $log->oldvalue = $actualvalue;
 260  
 261          // Getting the inserted config_log id.
 262          if (!$id = $DB->insert_record('config_log', $log)) {
 263              throw new moodle_exception('errorinserting', 'core_adminpresets');
 264          }
 265  
 266          return $id;
 267      }
 268  }