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 restore_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 restored
  33   *
  34   * TODO: Finish phpdocs
  35   */
  36  class restore_section_task extends restore_task {
  37  
  38      protected $info; // info related to section gathered from backup file
  39      protected $contextid; // course context id
  40      protected $sectionid; // new (target) id of the course section
  41  
  42      /**
  43       * Constructor - instantiates one object of this class
  44       */
  45      public function __construct($name, $info, $plan = null) {
  46          $this->info = $info;
  47          $this->sectionid = 0;
  48          parent::__construct($name, $plan);
  49      }
  50  
  51      /**
  52       * Section tasks have their own directory to read files
  53       */
  54      public function get_taskbasepath() {
  55  
  56          return $this->get_basepath() . '/sections/section_' . $this->info->sectionid;
  57      }
  58  
  59      public function set_sectionid($sectionid) {
  60          $this->sectionid = $sectionid;
  61      }
  62  
  63      public function get_contextid() {
  64          return $this->contextid;
  65      }
  66  
  67      public function get_sectionid() {
  68          return $this->sectionid;
  69      }
  70  
  71      /**
  72       * Create all the steps that will be part of this task
  73       */
  74      public function build() {
  75  
  76          // Define the task contextid (the course one)
  77          $this->contextid = context_course::instance($this->get_courseid())->id;
  78  
  79          // We always try to restore as much info from sections as possible, no matter of the type
  80          // of restore (new, existing, deleting, import...). MDL-27764
  81          $this->add_step(new restore_section_structure_step('course_info', 'section.xml'));
  82  
  83          // At the end, mark it as built
  84          $this->built = true;
  85      }
  86  
  87      /**
  88       * Exceptionally override the execute method, so, based in the section_included setting, we are able
  89       * to skip the execution of one task completely
  90       */
  91      public function execute() {
  92  
  93          // Find activity_included_setting
  94          if (!$this->get_setting_value('included')) {
  95              $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
  96  
  97          } else { // Setting tells us it's ok to execute
  98              parent::execute();
  99          }
 100      }
 101  
 102      /**
 103       * Specialisation that, first of all, looks for the setting within
 104       * the task with the the prefix added and later, delegates to parent
 105       * without adding anything
 106       */
 107      public function get_setting($name) {
 108          $namewithprefix = 'section_' . $this->info->sectionid . '_' . $name;
 109          $result = null;
 110          foreach ($this->settings as $key => $setting) {
 111              if ($setting->get_name() == $namewithprefix) {
 112                  if ($result != null) {
 113                      throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
 114                  } else {
 115                      $result = $setting;
 116                  }
 117              }
 118          }
 119          if ($result) {
 120              return $result;
 121          } else {
 122              // Fallback to parent
 123              return parent::get_setting($name);
 124          }
 125      }
 126  
 127      /**
 128       * Define the contents in the course that must be
 129       * processed by the link decoder
 130       */
 131      static public function define_decode_contents() {
 132          $contents = array();
 133  
 134          $contents[] = new restore_decode_content('course_sections', 'summary', 'course_section');
 135  
 136          return $contents;
 137      }
 138  
 139      /**
 140       * Define the decoding rules for links belonging
 141       * to the sections to be executed by the link decoder
 142       */
 143      static public function define_decode_rules() {
 144          return array();
 145      }
 146  
 147  // Protected API starts here
 148  
 149      /**
 150       * Define the common setting that any restore section will have
 151       */
 152      protected function define_settings() {
 153  
 154          // All the settings related to this activity will include this prefix
 155          $settingprefix = 'section_' . $this->info->sectionid . '_';
 156  
 157          // All these are common settings to be shared by all sections
 158  
 159          // Define section_included (to decide if the whole task must be really executed)
 160          $settingname = $settingprefix . 'included';
 161          $section_included = new restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
 162          if (is_number($this->info->title)) {
 163              $label = get_string('includesection', 'backup', $this->info->title);
 164          } elseif (empty($this->info->title)) { // Don't throw error if title is empty, gracefully continue restore.
 165              $this->log('Section title missing in backup for section id '.$this->info->sectionid, backup::LOG_WARNING, $this->name);
 166              $label = get_string('unnamedsection', 'backup');
 167          } else {
 168              $label = $this->info->title;
 169          }
 170          $section_included->get_ui()->set_label($label);
 171          $this->add_setting($section_included);
 172  
 173          // Define section_userinfo. Dependent of:
 174          // - users root setting
 175          // - section_included setting.
 176          $settingname = $settingprefix . 'userinfo';
 177          $defaultvalue = false;
 178          if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
 179              $defaultvalue = true;
 180          }
 181  
 182          $section_userinfo = new restore_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
 183          if (!$defaultvalue) {
 184              // This is a bit hacky, but if there is no user data to restore, then
 185              // we replace the standard check-box with a select menu with the
 186              // single choice 'No', and the select menu is clever enough that if
 187              // there is only one choice, it just displays a static string.
 188              //
 189              // It would probably be better design to have a special UI class
 190              // setting_ui_checkbox_or_no, rather than this hack, but I am not
 191              // going to do that today.
 192              $section_userinfo->set_ui(new backup_setting_ui_select($section_userinfo, get_string('includeuserinfo','backup'),
 193                      array(0 => get_string('no'))));
 194          } else {
 195              $section_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
 196          }
 197  
 198          $this->add_setting($section_userinfo);
 199  
 200          // Look for "users" root setting.
 201          $users = $this->plan->get_setting('users');
 202          $users->add_dependency($section_userinfo);
 203  
 204          // Look for "section_included" section setting.
 205          $section_included->add_dependency($section_userinfo);
 206      }
 207  }