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  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Base class with shared stuff between backup controller and restore
  19   * controller.
  20   *
  21   * @package core_backup
  22   * @copyright 2013 The Open University
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  abstract class base_controller extends backup implements loggable {
  26      /**
  27       * @var \core\progress\base Progress reporting object.
  28       */
  29      protected $progress;
  30  
  31      /**
  32       * @var base_logger Logging chain object (moodle, inline, fs, db, syslog)
  33       */
  34      protected $logger;
  35  
  36      /** @var bool Whether this backup should release the session. */
  37      protected $releasesession = backup::RELEASESESSION_NO;
  38  
  39      /**
  40       * Holds the relevant destination information for course copy operations.
  41       *
  42       * @var \stdClass.
  43       */
  44      protected $copy;
  45  
  46      /**
  47       * Gets the progress reporter, which can be used to report progress within
  48       * the backup or restore process.
  49       *
  50       * @return \core\progress\base Progress reporting object
  51       */
  52      public function get_progress() {
  53          return $this->progress;
  54      }
  55  
  56      /**
  57       * Sets the progress reporter.
  58       *
  59       * @param \core\progress\base $progress Progress reporting object
  60       */
  61      public function set_progress(\core\progress\base $progress) {
  62          $this->progress = $progress;
  63      }
  64  
  65      /**
  66       * Gets first logger in logging chain.
  67       *
  68       * @return base_logger Logger
  69       */
  70      public function get_logger() {
  71          return $this->logger;
  72      }
  73  
  74      /**
  75       * Inserts a new logger at end of logging chain.
  76       *
  77       * @param base_logger $logger New logger to add
  78       */
  79      public function add_logger(base_logger $logger) {
  80          $existing = $this->logger;
  81          while ($existing->get_next()) {
  82              $existing = $existing->get_next();
  83          }
  84          $existing->set_next($logger);
  85      }
  86  
  87      /**
  88       * Logs data to the logger chain.
  89       *
  90       * @see loggable::log()
  91       */
  92      public function log($message, $level, $a = null, $depth = null, $display = false) {
  93          backup_helper::log($message, $level, $a, $depth, $display, $this->logger);
  94      }
  95  
  96      /**
  97       * Returns the set value of releasesession.
  98       * This is used to indicate if the session should be closed during the backup/restore.
  99       *
 100       * @return bool Indicates whether the session should be released.
 101       */
 102      public function get_releasesession() {
 103          return $this->releasesession;
 104      }
 105  
 106      /**
 107       * Store extra data for course copy operations.
 108       *
 109       * For a course copying these is data required to be passed to the restore step.
 110       * We store this data in its own section of the backup controller
 111       *
 112       * @param \stdClass $data The course copy data.
 113       * @throws backup_controller_exception
 114       */
 115      public function set_copy(\stdClass $data): void {
 116          // Only allow setting of copy data when controller is in copy mode.
 117          if ($this->mode != backup::MODE_COPY) {
 118              throw new backup_controller_exception('cannot_set_copy_vars_wrong_mode');
 119          }
 120          $this->copy = $data;
 121      }
 122  
 123      /**
 124       * Get the course copy data.
 125       *
 126       * @return \stdClass
 127       */
 128      public function get_copy(): \stdClass {
 129          return $this->copy;
 130      }
 131  }