Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * Defines restore_course_task 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  /**
  31   * course task that provides all the properties and common steps to be performed
  32   * when one course is being restored
  33   *
  34   * TODO: Finish phpdocs
  35   */
  36  class restore_course_task extends restore_task {
  37  
  38      protected $info; // info related to course gathered from backup file
  39      protected $contextid; // course context id
  40  
  41      /**
  42       * Constructor - instantiates one object of this class
  43       */
  44      public function __construct($name, $info, $plan = null) {
  45          $this->info = $info;
  46          parent::__construct($name, $plan);
  47      }
  48  
  49      /**
  50       * Course tasks have their own directory to read files
  51       */
  52      public function get_taskbasepath() {
  53  
  54          return $this->get_basepath() . '/course';
  55      }
  56  
  57      public function get_contextid() {
  58          return $this->contextid;
  59      }
  60  
  61      /**
  62       * Create all the steps that will be part of this task
  63       */
  64      public function build() {
  65  
  66          // Define the task contextid (the course one)
  67          $this->contextid = context_course::instance($this->get_courseid())->id;
  68  
  69          // Executed conditionally if restoring to new course or if overwrite_conf setting is enabled
  70          if ($this->get_target() == backup::TARGET_NEW_COURSE || $this->get_setting_value('overwrite_conf') == true) {
  71              $this->add_step(new restore_course_structure_step('course_info', 'course.xml'));
  72  
  73              // Search reindexing (if enabled).
  74              if (\core_search\manager::is_indexing_enabled()) {
  75                  $this->add_step(new restore_course_search_index('course_search_index'));
  76              }
  77          }
  78  
  79          $this->add_step(new restore_course_legacy_files_step('legacy_files'));
  80  
  81          // Deal with enrolment methods and user enrolments.
  82          if ($this->plan->get_mode() == backup::MODE_IMPORT) {
  83              // No need to do anything with enrolments.
  84  
  85          } else if (!$this->get_setting_value('users') or $this->plan->get_mode() == backup::MODE_HUB) {
  86              if ($this->get_setting_value('enrolments') == backup::ENROL_ALWAYS && $this->plan->get_mode() != backup::MODE_HUB) {
  87                  // Restore enrolment methods.
  88                  $this->add_step(new restore_enrolments_structure_step('course_enrolments', 'enrolments.xml'));
  89              } else if ($this->get_target() == backup::TARGET_CURRENT_ADDING or $this->get_target() == backup::TARGET_EXISTING_ADDING) {
  90                  // Keep current enrolments unchanged.
  91              } else {
  92                  // If no instances yet add default enrol methods the same way as when creating new course in UI.
  93                  $this->add_step(new restore_default_enrolments_step('default_enrolments'));
  94              }
  95  
  96          } else {
  97              // Restore course enrolment data.
  98              $this->add_step(new restore_enrolments_structure_step('course_enrolments', 'enrolments.xml'));
  99          }
 100  
 101          // Populate groups, this must be done after enrolments because only enrolled users may be in groups.
 102          $this->add_step(new restore_groups_members_structure_step('create_groups_members', '../groups.xml'));
 103  
 104          // Restore course role assignments and overrides (internally will observe the role_assignments setting),
 105          // this must be done after all users are enrolled.
 106          $this->add_step(new restore_ras_and_caps_structure_step('course_ras_and_caps', 'roles.xml'));
 107  
 108          // Restore course filters (conditionally)
 109          if ($this->get_setting_value('filters')) {
 110              $this->add_step(new restore_filters_structure_step('course_filters', 'filters.xml'));
 111          }
 112  
 113          // Restore course comments (conditionally)
 114          if ($this->get_setting_value('comments')) {
 115              $this->add_step(new restore_comments_structure_step('course_comments', 'comments.xml'));
 116          }
 117  
 118          // Calendar events (conditionally)
 119          if ($this->get_setting_value('calendarevents')) {
 120              $this->add_step(new restore_calendarevents_structure_step('course_calendar', 'calendar.xml'));
 121          }
 122  
 123          // Course competencies.
 124          $this->add_step(new restore_course_competencies_structure_step('course_competencies', 'competencies.xml'));
 125  
 126          // Activity completion defaults.
 127          $this->add_step(new restore_completion_defaults_structure_step('course_completion_defaults', 'completiondefaults.xml'));
 128  
 129          // Content bank content (conditionally).
 130          if ($this->get_setting_value('contentbankcontent')) {
 131              $this->add_step(new restore_contentbankcontent_structure_step('course_contentbank', 'contentbank.xml'));
 132          }
 133  
 134          // At the end, mark it as built
 135          $this->built = true;
 136      }
 137  
 138      /**
 139       * Define the contents in the course that must be
 140       * processed by the link decoder
 141       */
 142      static public function define_decode_contents() {
 143          $contents = array();
 144  
 145          $contents[] = new restore_decode_content('course', 'summary');
 146          $contents[] = new restore_decode_content('event', 'description');
 147  
 148          return $contents;
 149      }
 150  
 151      /**
 152       * Define the decoding rules for links belonging
 153       * to the course to be executed by the link decoder
 154       */
 155      static public function define_decode_rules() {
 156          $rules = array();
 157  
 158          // Link to the course main page (it also covers "&topic=xx" and "&week=xx"
 159          // because they don't become transformed (section number) in backup/restore.
 160          $rules[] = new restore_decode_rule('COURSEVIEWBYID',       '/course/view.php?id=$1',        'course');
 161  
 162          // A few other key course links.
 163          $rules[] = new restore_decode_rule('GRADEINDEXBYID',       '/grade/index.php?id=$1',        'course');
 164          $rules[] = new restore_decode_rule('GRADEREPORTINDEXBYID', '/grade/report/index.php?id=$1', 'course');
 165          $rules[] = new restore_decode_rule('BADGESVIEWBYID',       '/badges/view.php?type=2&id=$1', 'course');
 166          $rules[] = new restore_decode_rule('USERINDEXVIEWBYID',    '/user/index.php?id=$1',         'course');
 167  
 168          return $rules;
 169      }
 170  
 171  // Protected API starts here
 172  
 173      /**
 174       * Define the common setting that any restore course will have
 175       */
 176      protected function define_settings() {
 177  
 178          // Define overwrite_conf to decide if course configuration will be restored over existing one.
 179          $overwrite = new restore_course_overwrite_conf_setting('overwrite_conf', base_setting::IS_BOOLEAN, false);
 180          $overwrite->set_ui(new backup_setting_ui_select($overwrite, $overwrite->get_name(),
 181              array(1 => get_string('yes'), 0 => get_string('no'))));
 182          $overwrite->get_ui()->set_label(get_string('setting_overwrite_conf', 'backup'));
 183          if ($this->get_target() == backup::TARGET_NEW_COURSE) {
 184              $overwrite->set_value(true);
 185              $overwrite->set_status(backup_setting::LOCKED_BY_CONFIG);
 186              $overwrite->set_visibility(backup_setting::HIDDEN);
 187              $course = (object)['fullname' => null, 'shortname' => null, 'startdate' => null];
 188          } else {
 189              $course = get_course($this->get_courseid());
 190          }
 191          $this->add_setting($overwrite);
 192  
 193          $fullnamedefaultvalue = $this->get_info()->original_course_fullname;
 194          $fullname = new restore_course_defaultcustom_setting('course_fullname', base_setting::IS_TEXT, $fullnamedefaultvalue);
 195          $fullname->set_ui(new backup_setting_ui_defaultcustom($fullname, get_string('setting_course_fullname', 'backup'),
 196              ['customvalue' => $fullnamedefaultvalue, 'defaultvalue' => $course->fullname]));
 197          $this->add_setting($fullname);
 198  
 199          $shortnamedefaultvalue = $this->get_info()->original_course_shortname;
 200          $shortname = new restore_course_defaultcustom_setting('course_shortname', base_setting::IS_TEXT, $shortnamedefaultvalue);
 201          $shortname->set_ui(new backup_setting_ui_defaultcustom($shortname, get_string('setting_course_shortname', 'backup'),
 202              ['customvalue' => $shortnamedefaultvalue, 'defaultvalue' => $course->shortname]));
 203          $this->add_setting($shortname);
 204  
 205          $startdatedefaultvalue = $this->get_info()->original_course_startdate;
 206          $startdate = new restore_course_defaultcustom_setting('course_startdate', base_setting::IS_INTEGER, $startdatedefaultvalue);
 207          $startdate->set_ui(new backup_setting_ui_defaultcustom($startdate, get_string('setting_course_startdate', 'backup'),
 208              ['customvalue' => $startdatedefaultvalue, 'defaultvalue' => $course->startdate, 'type' => 'date_time_selector']));
 209          $this->add_setting($startdate);
 210  
 211          $keep_enrols = new restore_course_generic_setting('keep_roles_and_enrolments', base_setting::IS_BOOLEAN, false);
 212          $keep_enrols->set_ui(new backup_setting_ui_select($keep_enrols, $keep_enrols->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
 213          $keep_enrols->get_ui()->set_label(get_string('setting_keep_roles_and_enrolments', 'backup'));
 214          if ($this->get_target() != backup::TARGET_CURRENT_DELETING and $this->get_target() != backup::TARGET_EXISTING_DELETING) {
 215              $keep_enrols->set_value(false);
 216              $keep_enrols->set_status(backup_setting::LOCKED_BY_CONFIG);
 217              $keep_enrols->set_visibility(backup_setting::HIDDEN);
 218          }
 219          $this->add_setting($keep_enrols);
 220  
 221          $keep_groups = new restore_course_generic_setting('keep_groups_and_groupings', base_setting::IS_BOOLEAN, false);
 222          $keep_groups->set_ui(new backup_setting_ui_select($keep_groups, $keep_groups->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
 223          $keep_groups->get_ui()->set_label(get_string('setting_keep_groups_and_groupings', 'backup'));
 224          if ($this->get_target() != backup::TARGET_CURRENT_DELETING and $this->get_target() != backup::TARGET_EXISTING_DELETING) {
 225              $keep_groups->set_value(false);
 226              $keep_groups->set_status(backup_setting::LOCKED_BY_CONFIG);
 227              $keep_groups->set_visibility(backup_setting::HIDDEN);
 228          }
 229          $this->add_setting($keep_groups);
 230  
 231      }
 232  }