Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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 restore_block_task class
  20   *
  21   * @package     core_backup
  22   * @subpackage  moodle2
  23   * @category    backup
  24   * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  25   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * abstract block task that provides all the properties and common steps to be performed
  32   * when one block is being restored
  33   *
  34   * TODO: Finish phpdocs
  35   */
  36  abstract class restore_block_task extends restore_task {
  37  
  38      protected $taskbasepath; // To store the basepath of this block
  39      protected $blockname;    // Name of the block
  40      protected $contextid;   // new (target) context of the block
  41      protected $oldcontextid;// old (original) context of the block
  42      protected $blockid;     // new (target) id of the block
  43      protected $oldblockid;  // old (original) id of the block
  44  
  45      /**
  46       * Constructor - instantiates one object of this class
  47       */
  48      public function __construct($name, $taskbasepath, $plan = null) {
  49          $this->taskbasepath = $taskbasepath;
  50          $this->blockname = '';
  51          $this->contextid = 0;
  52          $this->oldcontextid = 0;
  53          $this->blockid = 0;
  54          $this->oldblockid = 0;
  55          parent::__construct($name, $plan);
  56      }
  57  
  58      /**
  59       * Block tasks have their own directory to write files
  60       */
  61      public function get_taskbasepath() {
  62          return $this->taskbasepath;
  63      }
  64  
  65      /**
  66       * Create all the steps that will be part of this task
  67       */
  68      public function build() {
  69  
  70          // If we have decided not to backup blocks, prevent anything to be built
  71          if (!$this->get_setting_value('blocks')) {
  72              $this->built = true;
  73              return;
  74          }
  75  
  76          // If "child" of activity task and it has been excluded, nothing to do
  77          $parent = basename(dirname(dirname($this->taskbasepath)));
  78          if ($parent != 'course') {
  79              $includedsetting = $parent . '_included';
  80              if (!$this->get_setting_value($includedsetting)) {
  81                  $this->built = true;
  82                  return;
  83              }
  84          }
  85  
  86          // Process the block.xml common file (instance + positions)
  87          $this->add_step(new restore_block_instance_structure_step('block_commons', 'block.xml'));
  88  
  89          // Here we add all the common steps for any block and, in the point of interest
  90          // we call to define_my_steps() in order to get the particular ones inserted in place.
  91          $this->define_my_steps();
  92  
  93          // Restore block role assignments and overrides (internally will observe the role_assignments setting)
  94          $this->add_step(new restore_ras_and_caps_structure_step('block_ras_and_caps', 'roles.xml'));
  95  
  96          // Restore block comments (conditionally)
  97          if ($this->get_setting_value('comments')) {
  98              $this->add_step(new restore_comments_structure_step('block_comments', 'comments.xml'));
  99          }
 100  
 101          // Search reindexing (if enabled).
 102          if (\core_search\manager::is_indexing_enabled()) {
 103              $wholecourse = $this->get_target() == backup::TARGET_NEW_COURSE;
 104              $wholecourse = $wholecourse || $this->setting_exists('overwrite_conf') && $this->get_setting_value('overwrite_conf');
 105              if (!$wholecourse) {
 106                  $this->add_step(new restore_block_search_index('block_search_index'));
 107              }
 108          }
 109  
 110          // At the end, mark it as built
 111          $this->built = true;
 112      }
 113  
 114      public function set_blockname($blockname) {
 115          $this->blockname = $blockname;
 116      }
 117  
 118      public function get_blockname() {
 119          return $this->blockname;
 120      }
 121  
 122      public function set_blockid($blockid) {
 123          $this->blockid = $blockid;
 124      }
 125  
 126      public function get_blockid() {
 127          return $this->blockid;
 128      }
 129  
 130      public function set_old_blockid($blockid) {
 131          $this->oldblockid = $blockid;
 132      }
 133  
 134      public function get_old_blockid() {
 135          return $this->oldblockid;
 136      }
 137  
 138      public function set_contextid($contextid) {
 139          $this->contextid = $contextid;
 140      }
 141  
 142      public function get_contextid() {
 143          return $this->contextid;
 144      }
 145  
 146      public function set_old_contextid($contextid) {
 147          $this->oldcontextid = $contextid;
 148      }
 149  
 150      public function get_old_contextid() {
 151          return $this->oldcontextid;
 152      }
 153  
 154      /**
 155       * Define one array() of fileareas that each block controls
 156       */
 157      abstract public function get_fileareas();
 158  
 159      /**
 160       * Define one array() of configdata attributes
 161       * that need to be decoded
 162       */
 163      abstract public function get_configdata_encoded_attributes();
 164  
 165      /**
 166       * Helper method to safely unserialize block configuration during restore
 167       *
 168       * @param string $configdata The original base64 encoded block config, as retrieved from the block_instances table
 169       * @return stdClass
 170       */
 171      protected function decode_configdata(string $configdata): stdClass {
 172          return unserialize_object(base64_decode($configdata));
 173      }
 174  
 175      /**
 176       * Define the contents in the activity that must be
 177       * processed by the link decoder
 178       */
 179      static public function define_decode_contents() {
 180          throw new coding_exception('define_decode_contents() method needs to be overridden in each subclass of restore_block_task');
 181      }
 182  
 183      /**
 184       * Define the decoding rules for links belonging
 185       * to the activity to be executed by the link decoder
 186       */
 187      static public function define_decode_rules() {
 188          throw new coding_exception('define_decode_rules() method needs to be overridden in each subclass of restore_block_task');
 189      }
 190  
 191  // Protected API starts here
 192  
 193      /**
 194       * Define the common setting that any backup block will have
 195       */
 196      protected function define_settings() {
 197  
 198          // Nothing to add, blocks doesn't have common settings (for now)
 199  
 200          // End of common activity settings, let's add the particular ones
 201          $this->define_my_settings();
 202      }
 203  
 204      /**
 205       * Define (add) particular settings that each block can have
 206       */
 207      abstract protected function define_my_settings();
 208  
 209      /**
 210       * Define (add) particular steps that each block can have
 211       */
 212      abstract protected function define_my_steps();
 213  }