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 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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_plan_builder 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  require_once($CFG->dirroot . '/backup/moodle2/restore_root_task.class.php');
  31  require_once($CFG->dirroot . '/backup/moodle2/restore_course_task.class.php');
  32  require_once($CFG->dirroot . '/backup/moodle2/restore_section_task.class.php');
  33  require_once($CFG->dirroot . '/backup/moodle2/restore_activity_task.class.php');
  34  require_once($CFG->dirroot . '/backup/moodle2/restore_final_task.class.php');
  35  require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
  36  require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
  37  require_once($CFG->dirroot . '/backup/moodle2/restore_plugin.class.php');
  38  require_once($CFG->dirroot . '/backup/moodle2/restore_qbank_plugin.class.php');
  39  require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_plugin.class.php');
  40  require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_extrafields_plugin.class.php');
  41  require_once($CFG->dirroot . '/backup/moodle2/restore_format_plugin.class.php');
  42  require_once($CFG->dirroot . '/backup/moodle2/restore_local_plugin.class.php');
  43  require_once($CFG->dirroot . '/backup/moodle2/restore_theme_plugin.class.php');
  44  require_once($CFG->dirroot . '/backup/moodle2/restore_report_plugin.class.php');
  45  require_once($CFG->dirroot . '/backup/moodle2/restore_coursereport_plugin.class.php');
  46  require_once($CFG->dirroot . '/backup/moodle2/restore_plagiarism_plugin.class.php');
  47  require_once($CFG->dirroot . '/backup/moodle2/restore_gradingform_plugin.class.php');
  48  require_once($CFG->dirroot . '/backup/moodle2/restore_enrol_plugin.class.php');
  49  require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
  50  require_once($CFG->dirroot . '/backup/moodle2/backup_qbank_plugin.class.php');
  51  require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
  52  require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_extrafields_plugin.class.php');
  53  require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
  54  require_once($CFG->dirroot . '/backup/moodle2/backup_local_plugin.class.php');
  55  require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
  56  require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php');
  57  require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php');
  58  require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php');
  59  require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php');
  60  require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php');
  61  require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
  62  require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
  63  require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
  64  
  65  // Load all the activity tasks for moodle2 format
  66  $mods = core_component::get_plugin_list('mod');
  67  foreach ($mods as $mod => $moddir) {
  68      $taskpath = $moddir . '/backup/moodle2/restore_' . $mod . '_activity_task.class.php';
  69      if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
  70          if (file_exists($taskpath)) {
  71              require_once($taskpath);
  72          }
  73      }
  74  }
  75  
  76  // Load all the block tasks for moodle2 format
  77  $blocks = core_component::get_plugin_list('block');
  78  foreach ($blocks as $block => $blockdir) {
  79      $taskpath = $blockdir . '/backup/moodle2/restore_' . $block . '_block_task.class.php';
  80      if (file_exists($taskpath)) {
  81          require_once($taskpath);
  82      }
  83  }
  84  
  85  /**
  86   * Abstract class defining the static method in charge of building the whole
  87   * restore plan, based in @restore_controller preferences.
  88   *
  89   * TODO: Finish phpdocs
  90   */
  91  abstract class restore_plan_builder {
  92  
  93      /**
  94       * Dispatches, based on type to specialised builders
  95       */
  96      static public function build_plan($controller) {
  97  
  98          $plan = $controller->get_plan();
  99  
 100          // Add the root task, responsible for
 101          // preparing everything, creating the
 102          // needed structures (users, roles),
 103          // preloading information to temp table
 104          // and other init tasks
 105          $plan->add_task(new restore_root_task('root_task'));
 106          $controller->get_progress()->progress();
 107  
 108          switch ($controller->get_type()) {
 109              case backup::TYPE_1ACTIVITY:
 110                  self::build_activity_plan($controller, key($controller->get_info()->activities));
 111                  break;
 112              case backup::TYPE_1SECTION:
 113                  self::build_section_plan($controller, key($controller->get_info()->sections));
 114                  break;
 115              case backup::TYPE_1COURSE:
 116                  self::build_course_plan($controller, $controller->get_courseid());
 117                  break;
 118          }
 119  
 120          // Add the final task, responsible for closing
 121          // all the pending bits (remapings, inter-links
 122          // conversion...)
 123          // and perform other various final actions.
 124          $plan->add_task(new restore_final_task('final_task'));
 125          $controller->get_progress()->progress();
 126      }
 127  
 128  
 129  // Protected API starts here
 130  
 131      /**
 132       * Restore one 1-activity backup
 133       */
 134      static protected function build_activity_plan($controller, $activityid) {
 135  
 136          $plan = $controller->get_plan();
 137          $info = $controller->get_info();
 138          $infoactivity = $info->activities[$activityid];
 139  
 140          // Add the activity task, responsible for restoring
 141          // all the module related information. So it conditionally
 142          // as far as the module can be missing on restore
 143          if ($task = restore_factory::get_restore_activity_task($infoactivity)) { // can be missing
 144              $plan->add_task($task);
 145              $controller->get_progress()->progress();
 146  
 147              // For the given activity path, add as many block tasks as necessary
 148              // TODO: Add blocks, we need to introspect xml here
 149              $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
 150              foreach ($blocks as $basepath => $name) {
 151                  if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
 152                      $plan->add_task($task);
 153                      $controller->get_progress()->progress();
 154                  } else {
 155                      // TODO: Debug information about block not supported
 156                  }
 157              }
 158          } else { // Activity is missing in target site, inform plan about that
 159              $plan->set_missing_modules();
 160          }
 161  
 162      }
 163  
 164      /**
 165       * Restore one 1-section backup
 166       */
 167      static protected function build_section_plan($controller, $sectionid) {
 168  
 169          $plan = $controller->get_plan();
 170          $info = $controller->get_info();
 171          $infosection = $info->sections[$sectionid];
 172  
 173          // Add the section task, responsible for restoring
 174          // all the section related information
 175          $plan->add_task(restore_factory::get_restore_section_task($infosection));
 176          $controller->get_progress()->progress();
 177          // For the given section, add as many activity tasks as necessary
 178          foreach ($info->activities as $activityid => $activity) {
 179              if ($activity->sectionid != $infosection->sectionid) {
 180                  continue;
 181              }
 182              if (plugin_supports('mod', $activity->modulename, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
 183                  self::build_activity_plan($controller, $activityid);
 184              } else {
 185                  // TODO: Debug information about module not supported
 186              }
 187          }
 188      }
 189  
 190      /**
 191       * Restore one 1-course backup
 192       */
 193      static protected function build_course_plan($controller, $courseid) {
 194  
 195          $plan = $controller->get_plan();
 196          $info = $controller->get_info();
 197  
 198          // Add the course task, responsible for restoring
 199          // all the course related information
 200          $task = restore_factory::get_restore_course_task($info->course, $courseid);
 201          $plan->add_task($task);
 202          $controller->get_progress()->progress();
 203  
 204          // For the given course path, add as many block tasks as necessary
 205          // TODO: Add blocks, we need to introspect xml here
 206          $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
 207          foreach ($blocks as $basepath => $name) {
 208              if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
 209                  $plan->add_task($task);
 210                  $controller->get_progress()->progress();
 211              } else {
 212                  // TODO: Debug information about block not supported
 213              }
 214          }
 215  
 216          // For the given course, add as many section tasks as necessary
 217          foreach ($info->sections as $sectionid => $section) {
 218              self::build_section_plan($controller, $sectionid);
 219          }
 220      }
 221  }