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.
   1  <?php
   2  
   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.
  14  //
  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/>.
  17  
  18  /**
  19   * Defines backup_setting class
  20   *
  21   * @package     core_backup
  22   * @category    backup
  23   * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Parent class for all backup settings
  31   */
  32  abstract class backup_setting extends base_setting implements checksumable {
  33  
  34      // Some constants defining levels of setting
  35      const ROOT_LEVEL     = 1;
  36      const COURSE_LEVEL   = 5;
  37      const SECTION_LEVEL  = 9;
  38      const ACTIVITY_LEVEL = 13;
  39  
  40      /** @var int Level of the setting, eg {@link self::ROOT_LEVEL} */
  41      protected $level;
  42  
  43      /**
  44       * {@inheritdoc}
  45       */
  46      public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
  47          parent::__construct($name, $vtype, $value, $visibility, $status);
  48          // Generate a default ui
  49          $this->uisetting = new backup_setting_ui_checkbox($this, $name);
  50      }
  51  
  52      /**
  53       * @return int Level of the setting, eg {@link self::ROOT_LEVEL}
  54       */
  55      public function get_level() {
  56          return $this->level;
  57      }
  58  
  59      /**
  60       * Creates and sets a user interface for this setting given appropriate arguments
  61       *
  62       * @param int $type
  63       * @param string $label
  64       * @param array $attributes
  65       * @param array $options
  66       */
  67      public function make_ui($type, $label, array $attributes = null, array $options = null) {
  68          $this->uisetting = backup_setting_ui::make($this, $type, $label, $attributes, $options);
  69          if (is_array($options) || is_object($options)) {
  70              $options = (array)$options;
  71              switch (get_class($this->uisetting)) {
  72                  case 'backup_setting_ui_radio' :
  73                      // text
  74                      if (array_key_exists('text', $options)) {
  75                          $this->uisetting->set_text($options['text']);
  76                      }
  77                  case 'backup_setting_ui_checkbox' :
  78                      // value
  79                      if (array_key_exists('value', $options)) {
  80                          $this->uisetting->set_value($options['value']);
  81                      }
  82                      break;
  83                  case 'backup_setting_ui_select' :
  84                      // options
  85                      if (array_key_exists('options', $options)) {
  86                          $this->uisetting->set_values($options['options']);
  87                      }
  88                      break;
  89              }
  90          }
  91      }
  92  
  93      public function add_dependency(base_setting $dependentsetting, $type=setting_dependency::DISABLED_VALUE, $options=array()) {
  94          if (!($dependentsetting instanceof backup_setting)) {
  95              throw new backup_setting_exception('invalid_backup_setting_parameter');
  96          }
  97          // Check the dependency level is >= current level
  98          if ($dependentsetting->get_level() < $this->level) {
  99              throw new backup_setting_exception('cannot_add_upper_level_dependency');
 100          }
 101          parent::add_dependency($dependentsetting, $type, $options);
 102      }
 103  
 104  // checksumable interface methods
 105  
 106      public function calculate_checksum() {
 107          // Checksum is a simple md5 hash of name, value, level
 108          // Not following dependencies at all. Each setting will
 109          // calculate its own checksum
 110          return md5($this->name . '-' . $this->value . '-' . $this->level);
 111      }
 112  
 113      public function is_checksum_correct($checksum) {
 114          return $this->calculate_checksum() === $checksum;
 115      }
 116  }
 117  
 118  /**
 119   * Exception class used by all the @backup_setting stuff
 120   */
 121  class backup_setting_exception extends base_setting_exception {
 122  }