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.
   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   * This file contains the backup user interface class
  19   *
  20   * @package   core_backup
  21   * @copyright 2010 Sam Hemelryk
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * This is the backup user interface class
  27   *
  28   * The backup user interface class manages the user interface and backup for Moodle.
  29   *
  30   * @package core_backup
  31   * @copyright 2010 Sam Hemelryk
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class backup_ui extends base_ui {
  35      /**
  36       * The stages of the backup user interface.
  37       * The initial stage of the backup - settings are here.
  38       */
  39      const STAGE_INITIAL = 1;
  40  
  41      /**
  42       * The stages of the backup user interface.
  43       * The schema stage of the backup - here you choose the bits you include.
  44       */
  45      const STAGE_SCHEMA = 2;
  46  
  47      /**
  48       * The stages of the backup user interface.
  49       * The confirmation stage of the backup.
  50       */
  51      const STAGE_CONFIRMATION = 4;
  52  
  53      /**
  54       * The stages of the backup user interface.
  55       * The final stage of the backup - where it is being processed.
  56       */
  57      const STAGE_FINAL = 8;
  58  
  59      /**
  60       * The stages of the backup user interface.
  61       * The backup is now complete.
  62       */
  63      const STAGE_COMPLETE = 16;
  64  
  65      /**
  66       * If set to true the current stage is skipped.
  67       * @var bool
  68       */
  69      protected static $skipcurrentstage = false;
  70  
  71      /**
  72       * Intialises what ever stage is requested. If none are requested we check
  73       * params for 'stage' and default to initial
  74       *
  75       * @param int $stage The desired stage to intialise or null for the default
  76       * @param array $params
  77       * @return backup_ui_stage_initial|backup_ui_stage_schema|backup_ui_stage_confirmation|backup_ui_stage_final
  78       */
  79      protected function initialise_stage($stage = null, array $params = null) {
  80          if ($stage == null) {
  81              $stage = optional_param('stage', self::STAGE_INITIAL, PARAM_INT);
  82          }
  83          if (self::$skipcurrentstage) {
  84              $stage *= 2;
  85          }
  86          switch ($stage) {
  87              case backup_ui::STAGE_INITIAL:
  88                  $stage = new backup_ui_stage_initial($this, $params);
  89                  break;
  90              case backup_ui::STAGE_SCHEMA:
  91                  $stage = new backup_ui_stage_schema($this, $params);
  92                  break;
  93              case backup_ui::STAGE_CONFIRMATION:
  94                  $stage = new backup_ui_stage_confirmation($this, $params);
  95                  break;
  96              case backup_ui::STAGE_FINAL:
  97                  $stage = new backup_ui_stage_final($this, $params);
  98                  break;
  99              default:
 100                  $stage = false;
 101                  break;
 102          }
 103          return $stage;
 104      }
 105  
 106      /**
 107       * Returns the backup id
 108       * @return string
 109       */
 110      public function get_uniqueid() {
 111          return $this->get_backupid();
 112      }
 113  
 114      /**
 115       * Gets the backup id from the controller
 116       * @return string
 117       */
 118      public function get_backupid() {
 119          return $this->controller->get_backupid();
 120      }
 121  
 122      /**
 123       * Executes the backup plan
 124       * @throws backup_ui_exception when the steps are wrong.
 125       * @return bool
 126       */
 127      public function execute() {
 128          if ($this->progress >= self::PROGRESS_EXECUTED) {
 129              throw new backup_ui_exception('backupuialreadyexecuted');
 130          }
 131          if ($this->stage->get_stage() < self::STAGE_FINAL) {
 132              throw new backup_ui_exception('backupuifinalisedbeforeexecute');
 133          }
 134          $this->progress = self::PROGRESS_EXECUTED;
 135          $this->controller->finish_ui();
 136          $this->controller->execute_plan();
 137          $this->stage = new backup_ui_stage_complete($this, $this->stage->get_params(), $this->controller->get_results());
 138          return true;
 139      }
 140  
 141      /**
 142       * Loads the backup controller if we are tracking one
 143       * @param string $backupid
 144       * @return backup_controller|false
 145       */
 146      final public static function load_controller($backupid = false) {
 147          // Get the backup id optional param.
 148          if ($backupid) {
 149              try {
 150                  // Try to load the controller with it.
 151                  // If it fails at this point it is likely because this is the first load.
 152                  $controller = backup_controller::load_controller($backupid);
 153                  return $controller;
 154              } catch (Exception $e) {
 155                  return false;
 156              }
 157          }
 158          return $backupid;
 159      }
 160  
 161      /**
 162       * Gets an array of progress bar items that can be displayed through the backup renderer.
 163       * @return array Array of items for the progress bar
 164       */
 165      public function get_progress_bar() {
 166          global $PAGE;
 167  
 168          $stage = self::STAGE_COMPLETE;
 169          $currentstage = $this->stage->get_stage();
 170          $items = array();
 171          while ($stage > 0) {
 172              $classes = array('backup_stage');
 173              if (floor($stage / 2) == $currentstage) {
 174                  $classes[] = 'backup_stage_next';
 175              } else if ($stage == $currentstage) {
 176                  $classes[] = 'backup_stage_current';
 177              } else if ($stage < $currentstage) {
 178                  $classes[] = 'backup_stage_complete';
 179              }
 180              $item = array('text' => strlen(decbin($stage)).'. '.get_string('currentstage'.$stage, 'backup'), 'class' => join(' ', $classes));
 181              if ($stage < $currentstage && $currentstage < self::STAGE_COMPLETE && (!self::$skipcurrentstage || ($stage * 2) != $currentstage)) {
 182                  $params = $this->stage->get_params();
 183                  if (empty($params)) {
 184                      $params = array();
 185                  }
 186                  $params = array_merge($params, array('backup' => $this->get_backupid(), 'stage' => $stage));
 187                  $item['link'] = new moodle_url($PAGE->url, $params);
 188              }
 189              array_unshift($items, $item);
 190              $stage = floor($stage / 2);
 191          }
 192          return $items;
 193      }
 194      /**
 195       * Gets the name related to the operation of this UI
 196       * @return string
 197       */
 198      public function get_name() {
 199          return 'backup';
 200      }
 201      /**
 202       * Gets the id of the first stage this UI is reponsible for
 203       * @return int
 204       */
 205      public function get_first_stage_id() {
 206          return self::STAGE_INITIAL;
 207      }
 208      /**
 209       * If called with default arg the current stage gets skipped.
 210       * @static
 211       * @param bool $setting Set to true (default) if you want to skip this stage, false otherwise.
 212       */
 213      public static function skip_current_stage($setting = true) {
 214          self::$skipcurrentstage = $setting;
 215      }
 216  }
 217  
 218  /**
 219   * Backup user interface exception. Modelled off the backup_exception class
 220   *
 221   * @package   core_backup
 222   * @copyright 2010 Sam Hemelryk
 223   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 224   */
 225  class backup_ui_exception extends base_ui_exception {}