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] [Versions 39 and 310]

   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_root_task class
  20   * @package     core_backup
  21   * @subpackage  moodle2
  22   * @category    backup
  23   * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Start task that provides all the settings common to all restores and other initial steps
  31   *
  32   * TODO: Finish phpdocs
  33   */
  34  class restore_root_task extends restore_task {
  35  
  36      /**
  37       * Create all the steps that will be part of this task
  38       */
  39      public function build() {
  40  
  41          // Conditionally create the temp table (can exist from prechecks) and delete old stuff
  42          $this->add_step(new restore_create_and_clean_temp_stuff('create_and_clean_temp_stuff'));
  43  
  44          // Now make sure the user that is running the restore can actually access the course
  45          // before executing any other step (potentially performing permission checks)
  46          $this->add_step(new restore_fix_restorer_access_step('fix_restorer_access'));
  47  
  48          // If we haven't preloaded information, load all the included inforef records to temp_ids table
  49          $this->add_step(new restore_load_included_inforef_records('load_inforef_records'));
  50  
  51          // Load all the needed files to temp_ids table
  52          $this->add_step(new restore_load_included_files('load_file_records', 'files.xml'));
  53  
  54          // If we haven't preloaded information, load all the needed roles to temp_ids_table
  55          $this->add_step(new restore_load_and_map_roles('load_and_map_roles'));
  56  
  57          // If we haven't preloaded information and are restoring user info, load all the needed users to temp_ids table
  58          $this->add_step(new restore_load_included_users('load_user_records'));
  59  
  60          // If we haven't preloaded information and are restoring user info, process all those needed users
  61          // marking for create/map them as needed. Any problem here will cause exception as far as prechecks have
  62          // performed the same process so, it's not possible to have errors here
  63          $this->add_step(new restore_process_included_users('process_user_records'));
  64  
  65          // Unconditionally, create all the needed users calculated in the previous step
  66          $this->add_step(new restore_create_included_users('create_users'));
  67  
  68          // Unconditionally, load create all the needed groups and groupings
  69          $this->add_step(new restore_groups_structure_step('create_groups_and_groupings', 'groups.xml'));
  70  
  71          // Unconditionally, load create all the needed scales
  72          $this->add_step(new restore_scales_structure_step('create_scales', 'scales.xml'));
  73  
  74          // Unconditionally, load create all the needed outcomes
  75          $this->add_step(new restore_outcomes_structure_step('create_scales', 'outcomes.xml'));
  76  
  77          // If we haven't preloaded information, load all the needed categories and questions (reduced) to temp_ids_table
  78          $this->add_step(new restore_load_categories_and_questions('load_categories_and_questions'));
  79  
  80          // If we haven't preloaded information, process all the loaded categories and questions
  81          // marking them for creation/mapping as needed. Any problem here will cause exception
  82          // because this same process has been executed and reported by restore prechecks, so
  83          // it is not possible to have errors here.
  84          $this->add_step(new restore_process_categories_and_questions('process_categories_and_questions'));
  85  
  86          // Unconditionally, create and map all the categories and questions
  87          $this->add_step(new restore_create_categories_and_questions('create_categories_and_questions', 'questions.xml'));
  88  
  89          // At the end, mark it as built
  90          $this->built = true;
  91      }
  92  
  93  // Protected API starts here
  94  
  95      /**
  96       * Define the common setting that any restore type will have
  97       */
  98      protected function define_settings() {
  99  
 100          // Load all the root settings found in backup file from controller
 101          $rootsettings = $this->get_info()->root_settings;
 102  
 103          // Define users setting (keeping it on hand to define dependencies)
 104          $defaultvalue = false;                      // Safer default
 105          $changeable = false;
 106          if (isset($rootsettings['users']) && $rootsettings['users']) { // Only enabled when available
 107              $defaultvalue = true;
 108              $changeable = true;
 109          }
 110          $users = new restore_users_setting('users', base_setting::IS_BOOLEAN, $defaultvalue);
 111          $users->set_ui(new backup_setting_ui_checkbox($users, get_string('rootsettingusers', 'backup')));
 112          $users->get_ui()->set_changeable($changeable);
 113          $this->add_setting($users);
 114  
 115          // Restore enrolment methods.
 116          if ($changeable) {
 117              $options = [
 118                  backup::ENROL_NEVER     => get_string('rootsettingenrolments_never', 'backup'),
 119                  backup::ENROL_WITHUSERS => get_string('rootsettingenrolments_withusers', 'backup'),
 120                  backup::ENROL_ALWAYS    => get_string('rootsettingenrolments_always', 'backup'),
 121              ];
 122              $enroldefault = backup::ENROL_WITHUSERS;
 123          } else {
 124              // Users can not be restored, simplify the dropdown.
 125              $options = [
 126                  backup::ENROL_NEVER     => get_string('no'),
 127                  backup::ENROL_ALWAYS    => get_string('yes')
 128              ];
 129              $enroldefault = backup::ENROL_NEVER;
 130          }
 131          $enrolments = new restore_users_setting('enrolments', base_setting::IS_INTEGER, $enroldefault);
 132          $enrolments->set_ui(new backup_setting_ui_select($enrolments, get_string('rootsettingenrolments', 'backup'),
 133              $options));
 134          $this->add_setting($enrolments);
 135  
 136          // Define role_assignments (dependent of users)
 137          $defaultvalue = false;                      // Safer default
 138          $changeable = false;
 139          if (isset($rootsettings['role_assignments']) && $rootsettings['role_assignments']) { // Only enabled when available
 140              $defaultvalue = true;
 141              $changeable = true;
 142          }
 143          $roleassignments = new restore_role_assignments_setting('role_assignments', base_setting::IS_BOOLEAN, $defaultvalue);
 144          $roleassignments->set_ui(new backup_setting_ui_checkbox($roleassignments,get_string('rootsettingroleassignments', 'backup')));
 145          $roleassignments->get_ui()->set_changeable($changeable);
 146          $this->add_setting($roleassignments);
 147          $users->add_dependency($roleassignments);
 148  
 149          // Define activitites
 150          $defaultvalue = false;                      // Safer default
 151          $changeable = false;
 152          if (isset($rootsettings['activities']) && $rootsettings['activities']) { // Only enabled when available
 153              $defaultvalue = true;
 154              $changeable = true;
 155          }
 156          $activities = new restore_activities_setting('activities', base_setting::IS_BOOLEAN, $defaultvalue);
 157          $activities->set_ui(new backup_setting_ui_checkbox($activities, get_string('rootsettingactivities', 'backup')));
 158          $activities->get_ui()->set_changeable($changeable);
 159          $this->add_setting($activities);
 160  
 161          // Define blocks
 162          $defaultvalue = false;                      // Safer default
 163          $changeable = false;
 164          if (isset($rootsettings['blocks']) && $rootsettings['blocks']) { // Only enabled when available
 165              $defaultvalue = true;
 166              $changeable = true;
 167          }
 168          $blocks = new restore_generic_setting('blocks', base_setting::IS_BOOLEAN, $defaultvalue);
 169          $blocks->set_ui(new backup_setting_ui_checkbox($blocks, get_string('rootsettingblocks', 'backup')));
 170          $blocks->get_ui()->set_changeable($changeable);
 171          $this->add_setting($blocks);
 172  
 173          // Define filters
 174          $defaultvalue = false;                      // Safer default
 175          $changeable = false;
 176          if (isset($rootsettings['filters']) && $rootsettings['filters']) { // Only enabled when available
 177              $defaultvalue = true;
 178              $changeable = true;
 179          }
 180          $filters = new restore_generic_setting('filters', base_setting::IS_BOOLEAN, $defaultvalue);
 181          $filters->set_ui(new backup_setting_ui_checkbox($filters, get_string('rootsettingfilters', 'backup')));
 182          $filters->get_ui()->set_changeable($changeable);
 183          $this->add_setting($filters);
 184  
 185          // Define comments (dependent of users)
 186          $defaultvalue = false;                      // Safer default
 187          $changeable = false;
 188          if (isset($rootsettings['comments']) && $rootsettings['comments']) { // Only enabled when available
 189              $defaultvalue = true;
 190              $changeable = true;
 191          }
 192          $comments = new restore_comments_setting('comments', base_setting::IS_BOOLEAN, $defaultvalue);
 193          $comments->set_ui(new backup_setting_ui_checkbox($comments, get_string('rootsettingcomments', 'backup')));
 194          $comments->get_ui()->set_changeable($changeable);
 195          $this->add_setting($comments);
 196          $users->add_dependency($comments);
 197  
 198          // Define badges (dependent of activities).
 199          $defaultvalue = false;                      // Safer default.
 200          $changeable = false;
 201          if (isset($rootsettings['badges']) && $rootsettings['badges']) { // Only enabled when available.
 202              $defaultvalue = true;
 203              $changeable = true;
 204          }
 205          $badges = new restore_badges_setting('badges', base_setting::IS_BOOLEAN, $defaultvalue);
 206          $badges->set_ui(new backup_setting_ui_checkbox($badges, get_string('rootsettingbadges', 'backup')));
 207          $badges->get_ui()->set_changeable($changeable);
 208          $this->add_setting($badges);
 209          $activities->add_dependency($badges);
 210          $users->add_dependency($badges);
 211  
 212          // Define Calendar events.
 213          $defaultvalue = false;                      // Safer default
 214          $changeable = false;
 215          if (isset($rootsettings['calendarevents']) && $rootsettings['calendarevents']) { // Only enabled when available
 216              $defaultvalue = true;
 217              $changeable = true;
 218          }
 219          $events = new restore_calendarevents_setting('calendarevents', base_setting::IS_BOOLEAN, $defaultvalue);
 220          $events->set_ui(new backup_setting_ui_checkbox($events, get_string('rootsettingcalendarevents', 'backup')));
 221          $events->get_ui()->set_changeable($changeable);
 222          $this->add_setting($events);
 223  
 224          // Define completion (dependent of users)
 225          $defaultvalue = false;                      // Safer default
 226          $changeable = false;
 227          if (isset($rootsettings['userscompletion']) && $rootsettings['userscompletion']) { // Only enabled when available
 228              $defaultvalue = true;
 229              $changeable = true;
 230          }
 231          $completion = new restore_userscompletion_setting('userscompletion', base_setting::IS_BOOLEAN, $defaultvalue);
 232          $completion->set_ui(new backup_setting_ui_checkbox($completion, get_string('rootsettinguserscompletion', 'backup')));
 233          $completion->get_ui()->set_changeable($changeable);
 234          $this->add_setting($completion);
 235          $users->add_dependency($completion);
 236  
 237          // Define logs (dependent of users)
 238          $defaultvalue = false;                      // Safer default
 239          $changeable = false;
 240          if (isset($rootsettings['logs']) && $rootsettings['logs']) { // Only enabled when available
 241              $defaultvalue = true;
 242              $changeable = true;
 243          }
 244          $logs = new restore_logs_setting('logs', base_setting::IS_BOOLEAN, $defaultvalue);
 245          $logs->set_ui(new backup_setting_ui_checkbox($logs, get_string('rootsettinglogs', 'backup')));
 246          $logs->get_ui()->set_changeable($changeable);
 247          $this->add_setting($logs);
 248          $users->add_dependency($logs);
 249  
 250          // Define grade_histories (dependent of users)
 251          $defaultvalue = false;                      // Safer default
 252          $changeable = false;
 253          if (isset($rootsettings['grade_histories']) && $rootsettings['grade_histories']) { // Only enabled when available
 254              $defaultvalue = true;
 255              $changeable = true;
 256          }
 257          $gradehistories = new restore_grade_histories_setting('grade_histories', base_setting::IS_BOOLEAN, $defaultvalue);
 258          $gradehistories->set_ui(new backup_setting_ui_checkbox($gradehistories, get_string('rootsettinggradehistories', 'backup')));
 259          $gradehistories->get_ui()->set_changeable($changeable);
 260          $this->add_setting($gradehistories);
 261          $users->add_dependency($gradehistories);
 262  
 263          // The restore does not process the grade histories when some activities are ignored.
 264          // So let's define a dependency to prevent false expectations from our users.
 265          $activities->add_dependency($gradehistories);
 266  
 267          // Define groups and groupings.
 268          $defaultvalue = false;
 269          $changeable = false;
 270          if (isset($rootsettings['groups']) && $rootsettings['groups']) { // Only enabled when available.
 271              $defaultvalue = true;
 272              $changeable = true;
 273          } else if (!isset($rootsettings['groups'])) {
 274              // It is likely this is an older backup that does not contain information on the group setting,
 275              // in which case groups should be restored and this setting can be changed.
 276              $defaultvalue = true;
 277              $changeable = true;
 278          }
 279          $groups = new restore_groups_setting('groups', base_setting::IS_BOOLEAN, $defaultvalue);
 280          $groups->set_ui(new backup_setting_ui_checkbox($groups, get_string('rootsettinggroups', 'backup')));
 281          $groups->get_ui()->set_changeable($changeable);
 282          $this->add_setting($groups);
 283  
 284          // Competencies restore setting. Show when competencies is enabled and the setting is available.
 285          $hascompetencies = !empty($rootsettings['competencies']);
 286          $competencies = new restore_competencies_setting($hascompetencies);
 287          $competencies->set_ui(new backup_setting_ui_checkbox($competencies, get_string('rootsettingcompetencies', 'backup')));
 288          $this->add_setting($competencies);
 289  
 290          $customfields = new restore_customfield_setting('customfields', base_setting::IS_BOOLEAN, $defaultvalue);
 291          $customfields->set_ui(new backup_setting_ui_checkbox($customfields, get_string('rootsettingcustomfield', 'backup')));
 292          $this->add_setting($customfields);
 293  
 294          // Define Content bank content.
 295          $defaultvalue = false;
 296          $changeable = false;
 297          if (isset($rootsettings['contentbankcontent']) && $rootsettings['contentbankcontent']) { // Only enabled when available.
 298              $defaultvalue = true;
 299              $changeable = true;
 300          }
 301          $contents = new restore_contentbankcontent_setting('contentbankcontent', base_setting::IS_BOOLEAN, $defaultvalue);
 302          $contents->set_ui(new backup_setting_ui_checkbox($contents, get_string('rootsettingcontentbankcontent', 'backup')));
 303          $contents->get_ui()->set_changeable($changeable);
 304          $this->add_setting($contents);
 305  
 306          // Include legacy files.
 307          $defaultvalue = true;
 308          $changeable = true;
 309          $legacyfiles = new restore_generic_setting('legacyfiles', base_setting::IS_BOOLEAN, $defaultvalue);
 310          $legacyfiles->set_ui(new backup_setting_ui_checkbox($legacyfiles, get_string('rootsettinglegacyfiles', 'backup')));
 311          $legacyfiles->get_ui()->set_changeable($changeable);
 312          $this->add_setting($legacyfiles);
 313      }
 314  }