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   * @package     core_backup
  20   * @subpackage  moodle2
  21   * @category    backup
  22   * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * abstract block task that provides all the properties and common steps to be performed
  30   * when one block is being backup
  31   *
  32   * TODO: Finish phpdocs
  33   */
  34  abstract class backup_block_task extends backup_task {
  35  
  36      protected $blockid;
  37      protected $blockname;
  38      protected $contextid;
  39      protected $moduleid;
  40      protected $modulename;
  41      protected $parentcontextid;
  42  
  43      /**
  44       * Constructor - instantiates one object of this class
  45       */
  46      public function __construct($name, $blockid, $moduleid = null, $plan = null) {
  47          global $DB;
  48  
  49          // Check blockid exists
  50          if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) {
  51              throw new backup_task_exception('block_task_block_instance_not_found', $blockid);
  52          }
  53  
  54          $this->blockid    = $blockid;
  55          $this->blockname  = $block->blockname;
  56          $this->contextid  = context_block::instance($this->blockid)->id;
  57          $this->moduleid   = $moduleid;
  58          $this->modulename = null;
  59          $this->parentcontextid = null;
  60  
  61          // If moduleid passed, check exists, supports moodle2 format and save info
  62          // Check moduleid exists
  63          if (!empty($moduleid)) {
  64              if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
  65                  throw new backup_task_exception('block_task_coursemodule_not_found', $moduleid);
  66              }
  67              // Check activity supports this moodle2 backup format
  68              if (!plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) {
  69                  throw new backup_task_exception('block_task_activity_lacks_moodle2_backup_support', $coursemodule->modname);
  70              }
  71  
  72              $this->moduleid   = $moduleid;
  73              $this->modulename = $coursemodule->modname;
  74              $this->parentcontextid  = context_module::instance($this->moduleid)->id;
  75          }
  76  
  77          parent::__construct($name, $plan);
  78      }
  79  
  80      public function get_blockid() {
  81          return $this->blockid;
  82      }
  83  
  84      public function get_blockname() {
  85          return $this->blockname;
  86      }
  87  
  88      public function get_moduleid() {
  89          return $this->moduleid;
  90      }
  91  
  92      public function get_modulename() {
  93          return $this->modulename;
  94      }
  95  
  96      public function get_contextid() {
  97          return $this->contextid;
  98      }
  99  
 100      public function get_parentcontextid() {
 101          return $this->parentcontextid;
 102      }
 103  
 104      /**
 105       * Block tasks have their own directory to write files
 106       */
 107      public function get_taskbasepath() {
 108          $basepath = $this->get_basepath();
 109  
 110          // Module blocks are under module dir
 111          if (!empty($this->moduleid)) {
 112              $basepath .= '/activities/' . $this->modulename . '_' . $this->moduleid .
 113                           '/blocks/' . $this->blockname . '_' . $this->blockid;
 114  
 115          // Course blocks are under course dir
 116          } else {
 117              $basepath .= '/course/blocks/' . $this->blockname . '_' . $this->blockid;
 118          }
 119          return $basepath;
 120      }
 121  
 122      /**
 123       * Create all the steps that will be part of this task
 124       */
 125      public function build() {
 126  
 127          // If we have decided not to backup blocks, prevent anything to be built
 128          if (!$this->get_setting_value('blocks')) {
 129              $this->built = true;
 130              return;
 131          }
 132  
 133          // If "child" of activity task and it has been excluded, nothing to do
 134          if (!empty($this->moduleid)) {
 135              $includedsetting = $this->modulename . '_' . $this->moduleid . '_included';
 136              if (!$this->get_setting_value($includedsetting)) {
 137                  $this->built = true;
 138                  return;
 139              }
 140          }
 141  
 142          // Add some extra settings that related processors are going to need
 143          $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKID, base_setting::IS_INTEGER, $this->blockid));
 144          $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKNAME, base_setting::IS_FILENAME, $this->blockname));
 145          $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid));
 146          $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename));
 147          $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
 148          $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid));
 149  
 150          // Create the block directory
 151          $this->add_step(new create_taskbasepath_directory('create_block_directory'));
 152  
 153          // Create the block.xml common file (instance + positions)
 154          $this->add_step(new backup_block_instance_structure_step('block_commons', 'block.xml'));
 155  
 156          // Here we add all the common steps for any block and, in the point of interest
 157          // we call to define_my_steps() in order to get the particular ones inserted in place.
 158          $this->define_my_steps();
 159  
 160          // Generate the roles file (optionally role assignments and always role overrides)
 161          $this->add_step(new backup_roles_structure_step('block_roles', 'roles.xml'));
 162  
 163          // Generate the comments file (conditionally)
 164          if ($this->get_setting_value('comments')) {
 165              $this->add_step(new backup_comments_structure_step('block_comments', 'comments.xml'));
 166          }
 167  
 168          // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
 169          $this->add_step(new backup_inforef_structure_step('block_inforef', 'inforef.xml'));
 170  
 171          // Migrate the already exported inforef entries to final ones
 172          $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
 173  
 174          // At the end, mark it as built
 175          $this->built = true;
 176      }
 177  
 178  // Protected API starts here
 179  
 180      /**
 181       * Define the common setting that any backup block will have
 182       */
 183      protected function define_settings() {
 184  
 185          // Nothing to add, blocks doesn't have common settings (for now)
 186  
 187          // End of common activity settings, let's add the particular ones
 188          $this->define_my_settings();
 189      }
 190  
 191      /**
 192       * Define (add) particular settings that each block can have
 193       */
 194      abstract protected function define_my_settings();
 195  
 196      /**
 197       * Define (add) particular steps that each block can have
 198       */
 199      abstract protected function define_my_steps();
 200  
 201      /**
 202       * Define one array() of fileareas that each block controls
 203       */
 204      abstract public function get_fileareas();
 205  
 206      /**
 207       * Define one array() of configdata attributes
 208       * that need to be processed by the contenttransformer
 209       */
 210      abstract public function get_configdata_encoded_attributes();
 211  
 212      /**
 213       * Code the transformations to perform in the block in
 214       * order to get transportable (encoded) links
 215       */
 216      static public function encode_content_links($content) {
 217          throw new coding_exception('encode_content_links() method needs to be overridden in each subclass of backup_block_task');
 218      }
 219  }