Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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 backup_section_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   * section task that provides all the properties and common steps to be performed
  32   * when one section is being backup
  33   *
  34   * TODO: Finish phpdocs
  35   */
  36  class backup_section_task extends backup_task {
  37  
  38      protected $sectionid;
  39  
  40      /**
  41       * Constructor - instantiates one object of this class
  42       */
  43      public function __construct($name, $sectionid, $plan = null) {
  44          global $DB;
  45  
  46          // Check section exists
  47          if (!$section = $DB->get_record('course_sections', array('id' => $sectionid))) {
  48              throw new backup_task_exception('section_task_section_not_found', $sectionid);
  49          }
  50  
  51          $this->sectionid  = $sectionid;
  52  
  53          parent::__construct($name, $plan);
  54      }
  55  
  56      public function get_sectionid() {
  57          return $this->sectionid;
  58      }
  59  
  60      /**
  61       * Section tasks have their own directory to write files
  62       */
  63      public function get_taskbasepath() {
  64  
  65          return $this->get_basepath() . '/sections/section_' . $this->sectionid;
  66      }
  67  
  68      /**
  69       * Create all the steps that will be part of this task
  70       */
  71      public function build() {
  72  
  73          // Set the backup::VAR_CONTEXTID setting to course context as far as next steps require that
  74          $coursectxid = context_course::instance($this->get_courseid())->id;
  75          $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid));
  76  
  77          // Add some extra settings that related processors are going to need
  78          $this->add_setting(new backup_activity_generic_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid));
  79          $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
  80  
  81          // Create the section directory
  82          $this->add_step(new create_taskbasepath_directory('create_section_directory'));
  83  
  84          // Create the section.xml common file (course_sections)
  85          $this->add_step(new backup_section_structure_step('section_commons', 'section.xml'));
  86  
  87          // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
  88          $this->add_step(new backup_inforef_structure_step('section_inforef', 'inforef.xml'));
  89  
  90          // Migrate the already exported inforef entries to final ones
  91          $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
  92  
  93          // At the end, mark it as built
  94          $this->built = true;
  95      }
  96  
  97      /**
  98       * Exceptionally override the execute method, so, based in the section_included setting, we are able
  99       * to skip the execution of one task completely
 100       */
 101      public function execute() {
 102  
 103          // Find section_included_setting
 104          if (!$this->get_setting_value('included')) {
 105              $this->log('section skipped by _included setting', backup::LOG_DEBUG, $this->name);
 106  
 107          } else { // Setting tells us it's ok to execute
 108              parent::execute();
 109          }
 110      }
 111  
 112      /**
 113       * Specialisation that, first of all, looks for the setting within
 114       * the task with the the prefix added and later, delegates to parent
 115       * without adding anything
 116       */
 117      public function get_setting($name) {
 118          $namewithprefix = 'section_' . $this->sectionid . '_' . $name;
 119          $result = null;
 120          foreach ($this->settings as $key => $setting) {
 121              if ($setting->get_name() == $namewithprefix) {
 122                  if ($result != null) {
 123                      throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
 124                  } else {
 125                      $result = $setting;
 126                  }
 127              }
 128          }
 129          if ($result) {
 130              return $result;
 131          } else {
 132              // Fallback to parent
 133              return parent::get_setting($name);
 134          }
 135      }
 136  
 137  // Protected API starts here
 138  
 139      /**
 140       * Define the common setting that any backup section will have
 141       */
 142      protected function define_settings() {
 143          global $DB;
 144  
 145          // All the settings related to this activity will include this prefix
 146          $settingprefix = 'section_' . $this->sectionid . '_';
 147  
 148          // All these are common settings to be shared by all sections
 149  
 150          $section = $DB->get_record('course_sections', array('id' => $this->sectionid), '*', MUST_EXIST);
 151          $course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST);
 152  
 153          // Define section_included (to decide if the whole task must be really executed)
 154          $settingname = $settingprefix . 'included';
 155          $section_included = new backup_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
 156          $section_included->get_ui()->set_label(get_section_name($course, $section));
 157          $this->add_setting($section_included);
 158  
 159          // Define section_userinfo. Dependent of:
 160          // - users root setting
 161          // - section_included setting
 162          $settingname = $settingprefix . 'userinfo';
 163          $section_userinfo = new backup_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
 164          $section_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
 165          $this->add_setting($section_userinfo);
 166          // Look for "users" root setting
 167          $users = $this->plan->get_setting('users');
 168          $users->add_dependency($section_userinfo);
 169          // Look for "section_included" section setting
 170          $section_included->add_dependency($section_userinfo);
 171      }
 172  }