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 backup_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/backup_root_task.class.php');
  31  require_once($CFG->dirroot . '/backup/moodle2/backup_activity_task.class.php');
  32  require_once($CFG->dirroot . '/backup/moodle2/backup_section_task.class.php');
  33  require_once($CFG->dirroot . '/backup/moodle2/backup_course_task.class.php');
  34  require_once($CFG->dirroot . '/backup/moodle2/backup_final_task.class.php');
  35  require_once($CFG->dirroot . '/backup/moodle2/backup_block_task.class.php');
  36  require_once($CFG->dirroot . '/backup/moodle2/backup_default_block_task.class.php');
  37  require_once($CFG->dirroot . '/backup/moodle2/backup_xml_transformer.class.php');
  38  require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
  39  require_once($CFG->dirroot . '/backup/moodle2/backup_qbank_plugin.class.php');
  40  require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
  41  require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_extrafields_plugin.class.php');
  42  require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php');
  43  require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
  44  require_once($CFG->dirroot . '/backup/moodle2/backup_local_plugin.class.php');
  45  require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
  46  require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php');
  47  require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php');
  48  require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php');
  49  require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php');
  50  require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php');
  51  require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php');
  52  require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php');
  53  require_once($CFG->dirroot . '/backup/moodle2/backup_custom_fields.php');
  54  
  55  // Load all the activity tasks for moodle2 format
  56  $mods = core_component::get_plugin_list('mod');
  57  foreach ($mods as $mod => $moddir) {
  58      $taskpath = $moddir . '/backup/moodle2/backup_' . $mod . '_activity_task.class.php';
  59      if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
  60          if (file_exists($taskpath)) {
  61              require_once($taskpath);
  62          }
  63      }
  64  }
  65  
  66  // Load all the block tasks for moodle2 format
  67  $blocks = core_component::get_plugin_list('block');
  68  foreach ($blocks as $block => $blockdir) {
  69      $taskpath = $blockdir . '/backup/moodle2/backup_' . $block . '_block_task.class.php';
  70      if (file_exists($taskpath)) {
  71          require_once($taskpath);
  72      }
  73  }
  74  
  75  /**
  76   * Abstract class defining the static method in charge of building the whole
  77   * backup plan, based in @backup_controller preferences.
  78   *
  79   * TODO: Finish phpdocs
  80   */
  81  abstract class backup_plan_builder {
  82  
  83      /**
  84       * Dispatches, based on type to specialised builders
  85       */
  86      static public function build_plan($controller) {
  87  
  88          $plan = $controller->get_plan();
  89  
  90          // Add the root task, responsible for storing global settings
  91          // and some init tasks
  92          $plan->add_task(new backup_root_task('root_task'));
  93  
  94          switch ($controller->get_type()) {
  95              case backup::TYPE_1ACTIVITY:
  96                  self::build_activity_plan($controller, $controller->get_id());
  97                  break;
  98              case backup::TYPE_1SECTION:
  99                  self::build_section_plan($controller, $controller->get_id());
 100                  break;
 101              case backup::TYPE_1COURSE:
 102                  self::build_course_plan($controller, $controller->get_id());
 103                  break;
 104          }
 105  
 106          // Add the final task, responsible for outputting
 107          // all the global xml files (groups, users,
 108          // gradebook, questions, roles, files...) and
 109          // the main moodle_backup.xml file
 110          // and perform other various final actions.
 111          $plan->add_task(new backup_final_task('final_task'));
 112      }
 113  
 114  
 115      /**
 116       * Return one array of supported backup types
 117       */
 118      static public function supported_backup_types() {
 119          return array(backup::TYPE_1COURSE, backup::TYPE_1SECTION, backup::TYPE_1ACTIVITY);
 120      }
 121  
 122  // Protected API starts here
 123  
 124      /**
 125       * Build one 1-activity backup
 126       */
 127      static protected function build_activity_plan($controller, $id) {
 128  
 129          $plan = $controller->get_plan();
 130  
 131          // Add the activity task, responsible for outputting
 132          // all the module related information
 133          try {
 134              $plan->add_task(backup_factory::get_backup_activity_task($controller->get_format(), $id));
 135  
 136              // For the given activity, add as many block tasks as necessary
 137              $blockids = backup_plan_dbops::get_blockids_from_moduleid($id);
 138              foreach ($blockids as $blockid) {
 139                  try {
 140                      $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid, $id));
 141                  } catch (backup_task_exception $e) {
 142                      $a = stdClass();
 143                      $a->mid = $id;
 144                      $a->bid = $blockid;
 145                      $controller->log(get_string('error_block_for_module_not_found', 'backup', $a), backup::LOG_WARNING);
 146                  }
 147              }
 148          } catch (backup_task_exception $e) {
 149              $controller->log(get_string('error_course_module_not_found', 'backup', $id), backup::LOG_WARNING);
 150          }
 151      }
 152  
 153      /**
 154       * Build one 1-section backup
 155       */
 156      static protected function build_section_plan($controller, $id) {
 157  
 158          $plan = $controller->get_plan();
 159  
 160          // Add the section task, responsible for outputting
 161          // all the section related information
 162          $plan->add_task(backup_factory::get_backup_section_task($controller->get_format(), $id));
 163  
 164          // For the given section, add as many activity tasks as necessary
 165          $coursemodules = backup_plan_dbops::get_modules_from_sectionid($id);
 166          foreach ($coursemodules as $coursemodule) {
 167              if (plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
 168                  self::build_activity_plan($controller, $coursemodule->id);
 169              } else {
 170                  // TODO: Debug information about module not supported
 171              }
 172          }
 173      }
 174  
 175      /**
 176       * Build one 1-course backup
 177       */
 178      static protected function build_course_plan($controller, $id) {
 179  
 180          $plan = $controller->get_plan();
 181  
 182          // Add the course task, responsible for outputting
 183          // all the course related information
 184          $plan->add_task(backup_factory::get_backup_course_task($controller->get_format(), $id));
 185  
 186          // For the given course, add as many section tasks as necessary
 187          $sections = backup_plan_dbops::get_sections_from_courseid($id);
 188          foreach ($sections as $section) {
 189              self::build_section_plan($controller, $section);
 190          }
 191  
 192          // For the given course, add as many block tasks as necessary
 193          $blockids = backup_plan_dbops::get_blockids_from_courseid($id);
 194          foreach ($blockids as $blockid) {
 195              $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid));
 196          }
 197      }
 198  }