Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
  • Differences Between: [Versions 37 and 311] [Versions 38 and 311] [Versions 39 and 311]

       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 Topics course format.
      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 Topics course format.
      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       * @return void
      82       */
      83      public function process_dummy_course() {
      84  
      85      }
      86  
      87      /**
      88       * Executed after course restore is complete.
      89       *
      90       * This method is only executed if course configuration was overridden.
      91       *
      92       * @return void
      93       */
      94      public function after_restore_course() {
      95          global $DB;
      96  
      97          if (!$this->need_restore_numsections()) {
      98              // Backup file was made in Moodle 3.3 or later, we don't need to process 'numsecitons'.
      99              return;
     100          }
     101  
     102          $data = $this->connectionpoint->get_data();
     103          $backupinfo = $this->step->get_task()->get_info();
     104          if ($backupinfo->original_course_format !== 'topics' || !isset($data['tags']['numsections'])) {
     105              // Backup from another course format or backup file does not even have 'numsections'.
     106              return;
     107          }
     108  
     109          $numsections = (int)$data['tags']['numsections'];
     110          foreach ($backupinfo->sections as $key => $section) {
     111              // For each section from the backup file check if it was restored and if was "orphaned" in the original
     112              // course and mark it as hidden. This will leave all activities in it visible and available just as it was
     113              // in the original course.
     114              // Exception is when we restore with merging and the course already had a section with this section number,
     115              // in this case we don't modify the visibility.
     116              if ($this->step->get_task()->get_setting_value($key . '_included')) {
     117                  $sectionnum = (int)$section->title;
     118                  if ($sectionnum > $numsections && $sectionnum > $this->originalnumsections) {
     119                      $DB->execute("UPDATE {course_sections} SET visible = 0 WHERE course = ? AND section = ?",
     120                          [$this->step->get_task()->get_courseid(), $sectionnum]);
     121                  }
     122              }
     123          }
     124      }
     125  }