Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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 moodlecore
  20   * @subpackage backup-plan
  21   * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Implementable class defining the needed stuf for one backup plan
  27   *
  28   * TODO: Finish phpdocs
  29   */
  30  class backup_plan extends base_plan implements loggable {
  31  
  32      protected $controller; // The backup controller building/executing this plan
  33      protected $basepath;   // Fullpath to dir where backup is created
  34      protected $excludingdactivities;
  35  
  36      /**
  37       * The role ids to keep in a copy operation.
  38       * @var array
  39       */
  40      protected $keptroles = array();
  41  
  42      /**
  43       * Constructor - instantiates one object of this class
  44       */
  45      public function __construct($controller) {
  46          if (! $controller instanceof backup_controller) {
  47              throw new backup_plan_exception('wrong_backup_controller_specified');
  48          }
  49          $backuptempdir    = make_backup_temp_directory('');
  50          $this->controller = $controller;
  51          $this->basepath   = $backuptempdir . '/' . $controller->get_backupid();
  52          $this->excludingdactivities = false;
  53          parent::__construct('backup_plan');
  54      }
  55  
  56      /**
  57       * Destroy all circular references. It helps PHP 5.2 a lot!
  58       */
  59      public function destroy() {
  60          // No need to destroy anything recursively here, direct reset
  61          $this->controller = null;
  62          // Delegate to base plan the rest
  63          parent::destroy();
  64      }
  65  
  66      public function build() {
  67          backup_factory::build_plan($this->controller); // Dispatch to correct format
  68          $this->built = true;
  69      }
  70  
  71      public function get_backupid() {
  72          return $this->controller->get_backupid();
  73      }
  74  
  75      public function get_type() {
  76          return $this->controller->get_type();
  77      }
  78  
  79      public function get_mode() {
  80          return $this->controller->get_mode();
  81      }
  82  
  83      public function get_courseid() {
  84          return $this->controller->get_courseid();
  85      }
  86  
  87      public function get_basepath() {
  88          return $this->basepath;
  89      }
  90  
  91      public function get_logger() {
  92          return $this->controller->get_logger();
  93      }
  94  
  95      /**
  96       * Gets the progress reporter, which can be used to report progress within
  97       * the backup or restore process.
  98       *
  99       * @return \core\progress\base Progress reporting object
 100       */
 101      public function get_progress() {
 102          return $this->controller->get_progress();
 103      }
 104  
 105      public function is_excluding_activities() {
 106          return $this->excludingdactivities;
 107      }
 108  
 109      public function set_excluding_activities() {
 110          $this->excludingdactivities = true;
 111      }
 112  
 113      /**
 114       * Sets the user roles that should be kept in the destination course
 115       * for a course copy operation.
 116       *
 117       * @param array $roleids
 118       */
 119      public function set_kept_roles(array $roleids): void {
 120          $this->keptroles = $roleids;
 121      }
 122  
 123      /**
 124       * Get the user roles that should be kept in the destination course
 125       * for a course copy operation.
 126       *
 127       * @return array
 128       */
 129      public function get_kept_roles(): array {
 130          return $this->keptroles;
 131      }
 132  
 133      public function log($message, $level, $a = null, $depth = null, $display = false) {
 134          backup_helper::log($message, $level, $a, $depth, $display, $this->get_logger());
 135      }
 136  
 137      /**
 138       * Function responsible for executing the tasks of any plan
 139       */
 140      public function execute() {
 141          if ($this->controller->get_status() != backup::STATUS_AWAITING) {
 142              throw new backup_controller_exception('backup_not_executable_awaiting_required', $this->controller->get_status());
 143          }
 144          $this->controller->set_status(backup::STATUS_EXECUTING);
 145          parent::execute();
 146          $this->controller->set_status(backup::STATUS_FINISHED_OK);
 147  
 148          if ($this->controller->get_type() === backup::TYPE_1COURSE) {
 149              // Trigger a course_backup_created event.
 150              $otherarray = array('format' => $this->controller->get_format(),
 151                                  'mode' => $this->controller->get_mode(),
 152                                  'interactive' => $this->controller->get_interactive(),
 153                                  'type' => $this->controller->get_type(),
 154              );
 155              $event = \core\event\course_backup_created::create(array(
 156                  'objectid' => $this->get_courseid(),
 157                  'context' => context_course::instance($this->get_courseid()),
 158                  'other' => $otherarray
 159              ));
 160              $event->trigger();
 161          }
 162      }
 163  }
 164  
 165  /*
 166   * Exception class used by all the @backup_plan stuff
 167   */
 168  class backup_plan_exception extends base_plan_exception {
 169  
 170      public function __construct($errorcode, $a=NULL, $debuginfo=null) {
 171          parent::__construct($errorcode, $a, $debuginfo);
 172      }
 173  }