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  
   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_final_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   * Final task that provides all the final steps necessary in order to finish one
  32   * restore like gradebook, interlinks... apart from some final cleaning
  33   *
  34   * TODO: Finish phpdocs
  35   */
  36  class restore_final_task extends restore_task {
  37  
  38      /**
  39       * Create all the steps that will be part of this task
  40       */
  41      public function build() {
  42  
  43          // Move all the CONTEXT_MODULE question qcats to their
  44          // final (newly created) module context
  45          $this->add_step(new restore_move_module_questions_categories('move_module_question_categories'));
  46  
  47          // Create all the question files now that every question is in place
  48          // and every category has its final contextid associated
  49          $this->add_step(new restore_create_question_files('create_question_files'));
  50  
  51          // Review all the block_position records in backup_ids in order
  52          // match them now that all the contexts are created populating DB
  53          // as needed. Only if we are restoring blocks.
  54          if ($this->get_setting_value('blocks')) {
  55              $this->add_step(new restore_review_pending_block_positions('review_block_positions'));
  56          }
  57  
  58          // Gradebook. Don't restore the gradebook unless activities are being restored.
  59          if ($this->get_setting_value('activities')) {
  60              $this->add_step(new restore_gradebook_structure_step('gradebook_step','gradebook.xml'));
  61              $this->add_step(new restore_grade_history_structure_step('grade_history', 'grade_history.xml'));
  62          }
  63  
  64          // Course completion.
  65          $this->add_step(new restore_course_completion_structure_step('course_completion', 'completion.xml'));
  66  
  67          // Conditionally restore course badges.
  68          if ($this->get_setting_value('badges')) {
  69              $this->add_step(new restore_badges_structure_step('course_badges', 'badges.xml'));
  70          }
  71  
  72          // Review all the legacy module_availability records in backup_ids in
  73          // order to match them with existing modules / grade items and convert
  74          // into the new system.
  75          $this->add_step(new restore_process_course_modules_availability('process_modules_availability'));
  76  
  77          // Update restored availability data to account for changes in IDs
  78          // during backup/restore.
  79          $this->add_step(new restore_update_availability('update_availability'));
  80  
  81          // Refresh action events conditionally.
  82          if ($this->get_setting_value('activities')) {
  83              $this->add_step(new restore_calendar_action_events('restoring_action_events'));
  84          }
  85  
  86          // Decode all the interlinks
  87          $this->add_step(new restore_decode_interlinks('decode_interlinks'));
  88  
  89          // Restore course logs (conditionally). They are restored here because we need all
  90          // the activities to be already restored.
  91          if ($this->get_setting_value('logs')) {
  92              // Legacy logs.
  93              $this->add_step(new restore_course_logs_structure_step('course_logs', 'course/logs.xml'));
  94              // New log stores.
  95              $this->add_step(new restore_course_logstores_structure_step('course_logstores', 'course/logstores.xml'));
  96          }
  97  
  98          // Review all the executed tasks having one after_restore method
  99          // executing it to perform some final adjustments of information
 100          // not available when the task was executed.
 101          // This step is always the last one performing modifications on restored information
 102          // Don't add any new step after it. Only aliases queue, cache rebuild and clean are allowed.
 103          $this->add_step(new restore_execute_after_restore('executing_after_restore'));
 104  
 105          // All files were sent to the filepool by now. We need to process
 106          // the aliases yet as they were not actually created but stashed for us instead.
 107          // We execute this step after executing_after_restore so that there can't be no
 108          // more files sent to the filepool after this.
 109          $this->add_step(new restore_process_file_aliases_queue('process_file_aliases_queue'));
 110  
 111          // Rebuild course cache to see results, whoah!
 112          $this->add_step(new restore_rebuild_course_cache('rebuild_course_cache'));
 113  
 114          // Clean the temp dir (conditionally) and drop temp table
 115          $this->add_step(new restore_drop_and_clean_temp_stuff('drop_and_clean_temp_stuff'));
 116  
 117          // If restoring to a new course or overwriting config, reindex the whole course.
 118          if (\core_search\manager::is_indexing_enabled()) {
 119              $wholecourse = $this->get_target() == backup::TARGET_NEW_COURSE;
 120              $wholecourse = $wholecourse || $this->setting_exists('overwrite_conf') && $this->get_setting_value('overwrite_conf');
 121              if ($wholecourse) {
 122                  $this->add_step(new restore_course_search_index('course_search_index'));
 123              }
 124          }
 125  
 126          $this->built = true;
 127      }
 128  
 129      /**
 130       * Special method, only available in the restore_final_task, able to invoke the
 131       * restore_plan execute_after_restore() method, so restore_execute_after_restore step
 132       * will be able to launch all the after_restore() methods of the executed tasks
 133       */
 134      public function launch_execute_after_restore() {
 135          $this->plan->execute_after_restore();
 136      }
 137  
 138      /**
 139       * Define the restore log rules that will be applied
 140       * by the {@link restore_logs_processor} when restoring
 141       * course logs. It must return one array
 142       * of {@link restore_log_rule} objects
 143       *
 144       * Note these are course logs, but are defined and restored
 145       * in final task because we need all the activities to be
 146       * restored in order to handle some log records properly
 147       */
 148      static public function define_restore_log_rules() {
 149          $rules = array();
 150  
 151          // module 'course' rules
 152          $rules[] = new restore_log_rule('course', 'view', 'view.php?id={course}', '{course}');
 153          $rules[] = new restore_log_rule('course', 'guest', 'view.php?id={course}', null);
 154          $rules[] = new restore_log_rule('course', 'user report', 'user.php?id={course}&user={user}&mode=[mode]', null);
 155          $rules[] = new restore_log_rule('course', 'add mod', '../mod/[modname]/view.php?id={course_module}', '[modname] {[modname]}');
 156          $rules[] = new restore_log_rule('course', 'update mod', '../mod/[modname]/view.php?id={course_module}', '[modname] {[modname]}');
 157          $rules[] = new restore_log_rule('course', 'delete mod', 'view.php?id={course}', null);
 158          $rules[] = new restore_log_rule('course', 'update', 'view.php?id={course}', '');
 159          $rules[] = new restore_log_rule('course', 'enrol', 'view.php?id={course}', '{user}');
 160          $rules[] = new restore_log_rule('course', 'unenrol', 'view.php?id={course}', '{user}');
 161          $rules[] = new restore_log_rule('course', 'editsection', 'editsection.php?id={course_section}', null);
 162          $rules[] = new restore_log_rule('course', 'new', 'view.php?id={course}', '');
 163          $rules[] = new restore_log_rule('course', 'recent', 'recent.php?id={course}', '');
 164          $rules[] = new restore_log_rule('course', 'report log', 'report/log/index.php?id={course}', '{course}');
 165          $rules[] = new restore_log_rule('course', 'report live', 'report/live/index.php?id={course}', '{course}');
 166          $rules[] = new restore_log_rule('course', 'report outline', 'report/outline/index.php?id={course}', '{course}');
 167          $rules[] = new restore_log_rule('course', 'report participation', 'report/participation/index.php?id={course}', '{course}');
 168          $rules[] = new restore_log_rule('course', 'report stats', 'report/stats/index.php?id={course}', '{course}');
 169          $rules[] = new restore_log_rule('course', 'view section', 'view.php?id={course}&sectionid={course_section}', '{course_section}');
 170  
 171          // module 'grade' rules
 172          $rules[] = new restore_log_rule('grade', 'update', 'report/grader/index.php?id={course}', null);
 173  
 174          // module 'user' rules
 175          $rules[] = new restore_log_rule('user', 'view', 'view.php?id={user}&course={course}', '{user}');
 176          $rules[] = new restore_log_rule('user', 'change password', 'view.php?id={user}&course={course}', '{user}');
 177          $rules[] = new restore_log_rule('user', 'login', 'view.php?id={user}&course={course}', '{user}');
 178          $rules[] = new restore_log_rule('user', 'logout', 'view.php?id={user}&course={course}', '{user}');
 179          $rules[] = new restore_log_rule('user', 'view all', 'index.php?id={course}', '');
 180          $rules[] = new restore_log_rule('user', 'update', 'view.php?id={user}&course={course}', '');
 181  
 182          // rules from other tasks (activities) not belonging to one module instance (cmid = 0), so are restored here
 183          $rules = array_merge($rules, restore_logs_processor::register_log_rules_for_course());
 184  
 185          // Calendar rules.
 186          $rules[] = new restore_log_rule('calendar', 'add', 'event.php?action=edit&id={event}', '[name]');
 187          $rules[] = new restore_log_rule('calendar', 'edit', 'event.php?action=edit&id={event}', '[name]');
 188          $rules[] = new restore_log_rule('calendar', 'edit all', 'event.php?action=edit&id={event}', '[name]');
 189  
 190          // TODO: Other logs like 'upload'... will go here
 191  
 192          return $rules;
 193      }
 194  
 195  
 196  // Protected API starts here
 197  
 198      /**
 199       * Define the common setting that any restore type will have
 200       */
 201      protected function define_settings() {
 202          // This task has not settings (could have them, like destination or so in the future, let's see)
 203      }
 204  }