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 401 and 402] [Versions 401 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   * This file contains extension of the backup classes that override some methods
  19   * and functionality in order to customise the backup UI for the purposes of
  20   * import.
  21   *
  22   * @package   core_backup
  23   * @copyright 2010 Sam Hemelryk
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  /**
  28   * Import UI class
  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 import_ui extends backup_ui {
  35  
  36      /**
  37       * The stages of the backup user interface
  38       * The precheck/selection stage of the backup - here you choose the initial settings.
  39       */
  40      const STAGE_PRECHECK = 0;
  41  
  42      /**
  43       * Customises the backup progress bar
  44       *
  45       * @global moodle_page $PAGE
  46       * @return array[] An array of arrays
  47       */
  48      public function get_progress_bar() {
  49          global $PAGE;
  50          $stage = self::STAGE_COMPLETE;
  51          $currentstage = $this->stage->get_stage();
  52          $items = array();
  53          while ($stage > 0) {
  54              $classes = array('backup_stage');
  55              if (floor($stage / 2) == $currentstage) {
  56                  $classes[] = 'backup_stage_next';
  57              } else if ($stage == $currentstage) {
  58                  $classes[] = 'backup_stage_current';
  59              } else if ($stage < $currentstage) {
  60                  $classes[] = 'backup_stage_complete';
  61              }
  62              $item = array(
  63                  'text' => strlen(decbin($stage * 2)).'. '.get_string('importcurrentstage'.$stage, 'backup'),
  64                  'class' => join(' ', $classes)
  65              );
  66              if ($stage < $currentstage && $currentstage < self::STAGE_COMPLETE && (!self::$skipcurrentstage || $stage * 2 != $currentstage)) {
  67                  $item['link'] = new moodle_url(
  68                      $PAGE->url,
  69                      $this->stage->get_params() + array('backup' => $this->get_backupid(), 'stage' => $stage)
  70                  );
  71              }
  72              array_unshift($items, $item);
  73              $stage = floor($stage / 2);
  74          }
  75          $selectorlink = new moodle_url($PAGE->url, $this->stage->get_params());
  76          $selectorlink->remove_params('importid');
  77  
  78          $classes = ["backup_stage"];
  79          if ($currentstage == 0) {
  80              $classes[] = "backup_stage_current";
  81          }
  82          array_unshift($items, array(
  83                  'text' => '1. '.get_string('importcurrentstage0', 'backup'),
  84                  'class' => join(' ', $classes),
  85                  'link' => $selectorlink));
  86          return $items;
  87      }
  88  
  89      /**
  90       * Intialises what ever stage is requested. If none are requested we check
  91       * params for 'stage' and default to initial
  92       *
  93       * @param int|null $stage The desired stage to intialise or null for the default
  94       * @param array $params
  95       * @return backup_ui_stage_initial|backup_ui_stage_schema|backup_ui_stage_confirmation|backup_ui_stage_final
  96       */
  97      protected function initialise_stage($stage = null, array $params = null) {
  98          if ($stage == null) {
  99              $stage = optional_param('stage', self::STAGE_PRECHECK, PARAM_INT);
 100          }
 101          if (self::$skipcurrentstage) {
 102              $stage *= 2;
 103          }
 104          switch ($stage) {
 105              case backup_ui::STAGE_INITIAL:
 106                  $stage = new import_ui_stage_inital($this, $params);
 107                  break;
 108              case backup_ui::STAGE_SCHEMA:
 109                  $stage = new import_ui_stage_schema($this, $params);
 110                  break;
 111              case backup_ui::STAGE_CONFIRMATION:
 112                  $stage = new import_ui_stage_confirmation($this, $params);
 113                  break;
 114              case backup_ui::STAGE_FINAL:
 115                  $stage = new import_ui_stage_final($this, $params);
 116                  break;
 117              case self::STAGE_PRECHECK:
 118                  $stage = new import_ui_stage_precheck($this, $params);
 119                  break;
 120              default:
 121                  $stage = false;
 122                  break;
 123          }
 124          return $stage;
 125      }
 126  }
 127  
 128  /**
 129   * Extends the initial stage
 130   *
 131   * @package   core_backup
 132   * @copyright 2010 Sam Hemelryk
 133   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 134   */
 135  class import_ui_stage_inital extends backup_ui_stage_initial {}
 136  
 137  /**
 138   * Class representing the precheck/selection stage of a import.
 139   *
 140   * In this stage the user is required to perform initial selections.
 141   * That is a choice of which course to import from.
 142   *
 143   * @package   core_backup
 144   * @copyright 2019 Peter Dias
 145   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 146   */
 147  class import_ui_stage_precheck extends backup_ui_stage {
 148      /**
 149       * Precheck/selection import stage constructor
 150       * @param backup_ui $ui
 151       * @param array $params
 152       */
 153      public function __construct(backup_ui $ui, array $params = null) {
 154          $this->stage = import_ui::STAGE_PRECHECK;
 155          parent::__construct($ui, $params);
 156      }
 157  
 158      /**
 159       * Processes the precheck/selection import stage
 160       *
 161       * @param base_moodleform|null $form
 162       */
 163      public function process(base_moodleform $form = null) {
 164          // Dummy functions. We don't have to do anything here.
 165          return;
 166      }
 167  
 168      /**
 169       * Gets the next stage for the import.
 170       *
 171       * @return int
 172       */
 173      public function get_next_stage() {
 174          return backup_ui::STAGE_INITIAL;
 175      }
 176  
 177      /**
 178       * Initialises the backup_moodleform instance for this stage
 179       *
 180       * @return backup_moodleform|void
 181       */
 182      public function initialise_stage_form() {
 183          // Dummy functions. We don't have to do anything here.
 184      }
 185  }
 186  
 187  /**
 188   * Extends the schema stage
 189   *
 190   * @package   core_backup
 191   * @copyright 2010 Sam Hemelryk
 192   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 193   */
 194  class import_ui_stage_schema extends backup_ui_stage_schema {}
 195  
 196  /**
 197   * Extends the confirmation stage.
 198   *
 199   * This overides the initialise stage form to remove the filenamesetting heading
 200   * as it is always hidden.
 201   *
 202   * @package   core_backup
 203   * @copyright 2010 Sam Hemelryk
 204   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 205   */
 206  class import_ui_stage_confirmation extends backup_ui_stage_confirmation {
 207  
 208      /**
 209       * Initialises the stages moodleform
 210       * @return moodleform
 211       */
 212      protected function initialise_stage_form() {
 213          $form = parent::initialise_stage_form();
 214          $form->remove_element('filenamesetting');
 215          return $form;
 216      }
 217  
 218      /**
 219       * Displays the stage
 220       *
 221       * This function is overriden so that we can manipulate the strings on the
 222       * buttons.
 223       *
 224       * @param core_backup_renderer $renderer
 225       * @return string HTML code to echo
 226       */
 227      public function display(core_backup_renderer $renderer) {
 228          $form = $this->initialise_stage_form();
 229          $form->require_definition_after_data();
 230          if ($e = $form->get_element('submitbutton')) {
 231              $e->setLabel(get_string('import'.$this->get_ui()->get_name().'stage'.$this->get_stage().'action', 'backup'));
 232          } else {
 233              $elements = $form->get_element('buttonar')->getElements();
 234              foreach ($elements as &$element) {
 235                  if ($element->getName() == 'submitbutton') {
 236                      $element->setValue(
 237                          get_string('import'.$this->get_ui()->get_name().'stage'.$this->get_stage().'action', 'backup')
 238                      );
 239                  }
 240              }
 241          }
 242  
 243          // A nasty hack follows to work around the sad fact that moodle quickforms
 244          // do not allow to actually return the HTML content, just to echo it.
 245          flush();
 246          ob_start();
 247          $form->display();
 248          $output = ob_get_contents();
 249          ob_end_clean();
 250  
 251          return $output;
 252      }
 253  }
 254  /**
 255   * Overrides the final stage.
 256   *
 257   * @package   core_backup
 258   * @copyright 2010 Sam Hemelryk
 259   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 260   */
 261  class import_ui_stage_final extends backup_ui_stage_final {}
 262  
 263  /**
 264   * Extends the restore course search to search for import courses.
 265   *
 266   * @package   core_backup
 267   * @copyright 2010 Sam Hemelryk
 268   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 269   */
 270  class import_course_search extends restore_course_search {
 271      /**
 272       * Sets up any access restrictions for the courses to be displayed in the search.
 273       *
 274       * This will typically call $this->require_capability().
 275       */
 276      protected function setup_restrictions() {
 277          $this->require_capability('moodle/backup:backuptargetimport');
 278      }
 279  }