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  // 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   * Specialised restore for format_topics
  19   *
  20   * @package   format_topics
  21   * @category  backup
  22   * @copyright 2017 Marina Glancy
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Specialised restore for format_topics
  30   *
  31   * Processes 'numsections' from the old backup files and hides sections that used to be "orphaned"
  32   *
  33   * @package   format_topics
  34   * @category  backup
  35   * @copyright 2017 Marina Glancy
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class restore_format_topics_plugin extends restore_format_plugin {
  39  
  40      /** @var int */
  41      protected $originalnumsections = 0;
  42  
  43      /**
  44       * Checks if backup file was made on Moodle before 3.3 and we should respect the 'numsections'
  45       * and potential "orphaned" sections in the end of the course.
  46       *
  47       * @return bool
  48       */
  49      protected function need_restore_numsections() {
  50          $backupinfo = $this->step->get_task()->get_info();
  51          $backuprelease = $backupinfo->backup_release; // The major version: 2.9, 3.0, 3.10...
  52          return version_compare($backuprelease, '3.3', '<');
  53      }
  54  
  55      /**
  56       * Creates a dummy path element in order to be able to execute code after restore
  57       *
  58       * @return restore_path_element[]
  59       */
  60      public function define_course_plugin_structure() {
  61          global $DB;
  62  
  63          // Since this method is executed before the restore we can do some pre-checks here.
  64          // In case of merging backup into existing course find the current number of sections.
  65          $target = $this->step->get_task()->get_target();
  66          if (($target == backup::TARGET_CURRENT_ADDING || $target == backup::TARGET_EXISTING_ADDING) &&
  67                  $this->need_restore_numsections()) {
  68              $maxsection = $DB->get_field_sql(
  69                  'SELECT max(section) FROM {course_sections} WHERE course = ?',
  70                  [$this->step->get_task()->get_courseid()]);
  71              $this->originalnumsections = (int)$maxsection;
  72          }
  73  
  74          // Dummy path element is needed in order for after_restore_course() to be called.
  75          return [new restore_path_element('dummy_course', $this->get_pathfor('/dummycourse'))];
  76      }
  77  
  78      /**
  79       * Dummy process method
  80       */
  81      public function process_dummy_course() {
  82  
  83      }
  84  
  85      /**
  86       * Executed after course restore is complete
  87       *
  88       * This method is only executed if course configuration was overridden
  89       */
  90      public function after_restore_course() {
  91          global $DB;
  92  
  93          if (!$this->need_restore_numsections()) {
  94              // Backup file was made in Moodle 3.3 or later, we don't need to process 'numsecitons'.
  95              return;
  96          }
  97  
  98          $data = $this->connectionpoint->get_data();
  99          $backupinfo = $this->step->get_task()->get_info();
 100          if ($backupinfo->original_course_format !== 'topics' || !isset($data['tags']['numsections'])) {
 101              // Backup from another course format or backup file does not even have 'numsections'.
 102              return;
 103          }
 104  
 105          $numsections = (int)$data['tags']['numsections'];
 106          foreach ($backupinfo->sections as $key => $section) {
 107              // For each section from the backup file check if it was restored and if was "orphaned" in the original
 108              // course and mark it as hidden. This will leave all activities in it visible and available just as it was
 109              // in the original course.
 110              // Exception is when we restore with merging and the course already had a section with this section number,
 111              // in this case we don't modify the visibility.
 112              if ($this->step->get_task()->get_setting_value($key . '_included')) {
 113                  $sectionnum = (int)$section->title;
 114                  if ($sectionnum > $numsections && $sectionnum > $this->originalnumsections) {
 115                      $DB->execute("UPDATE {course_sections} SET visible = 0 WHERE course = ? AND section = ?",
 116                          [$this->step->get_task()->get_courseid(), $sectionnum]);
 117                  }
 118              }
 119          }
 120      }
 121  }