weekly release 4.5dev
[moodle.git] / backup / moodle2 / restore_section_task.class.php
bloba9eb5730d3c83e03e093ecfa196e1fe288b99fa8
1 <?php
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.
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/>.
18 /**
19 * Defines restore_section_task class
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
28 defined('MOODLE_INTERNAL') || die();
30 /**
31 * section task that provides all the properties and common steps to be performed
32 * when one section is being restored
34 * TODO: Finish phpdocs
36 class restore_section_task extends restore_task {
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
42 /**
43 * Constructor - instantiates one object of this class
45 public function __construct($name, $info, $plan = null) {
46 $this->info = $info;
47 $this->sectionid = 0;
48 parent::__construct($name, $plan);
51 /**
52 * Section tasks have their own directory to read files
54 public function get_taskbasepath() {
56 return $this->get_basepath() . '/sections/section_' . $this->info->sectionid;
59 /**
60 * Get the course module that is delegating this section.
62 * @return int|null the course module id that is delegating this section
64 public function get_delegated_cm(): ?int {
65 if (!isset($this->info->parentcmid) || empty($this->info->parentcmid)) {
66 return null;
68 return intval($this->info->parentcmid);
71 /**
72 * Get the delegated activity modname if any.
74 * @return string|null the modname of the delegated activity
76 public function get_modname(): ?string {
77 if (!isset($this->info->modname) || empty($this->info->modname)) {
78 return null;
80 return $this->info->modname;
83 public function set_sectionid($sectionid) {
84 $this->sectionid = $sectionid;
87 public function get_contextid() {
88 return $this->contextid;
91 public function get_sectionid() {
92 return $this->sectionid;
95 /**
96 * Create all the steps that will be part of this task
98 public function build() {
100 // Define the task contextid (the course one)
101 $this->contextid = context_course::instance($this->get_courseid())->id;
103 // We always try to restore as much info from sections as possible, no matter of the type
104 // of restore (new, existing, deleting, import...). MDL-27764
105 $this->add_step(new restore_section_structure_step('course_info', 'section.xml'));
107 // At the end, mark it as built
108 $this->built = true;
112 * Exceptionally override the execute method, so, based in the section_included setting, we are able
113 * to skip the execution of one task completely
115 public function execute() {
117 // Find activity_included_setting
118 if (!$this->get_setting_value('included')) {
119 $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
121 } else { // Setting tells us it's ok to execute
122 parent::execute();
127 * Specialisation that, first of all, looks for the setting within
128 * the task with the the prefix added and later, delegates to parent
129 * without adding anything
131 public function get_setting($name) {
132 $namewithprefix = 'section_' . $this->info->sectionid . '_' . $name;
133 $result = null;
134 foreach ($this->settings as $key => $setting) {
135 if ($setting->get_name() == $namewithprefix) {
136 if ($result != null) {
137 throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
138 } else {
139 $result = $setting;
143 if ($result) {
144 return $result;
145 } else {
146 // Fallback to parent
147 return parent::get_setting($name);
152 * Define the contents in the course that must be
153 * processed by the link decoder
155 public static function define_decode_contents() {
156 $contents = array();
158 $contents[] = new restore_decode_content('course_sections', 'summary', 'course_section');
160 return $contents;
164 * Define the decoding rules for links belonging
165 * to the sections to be executed by the link decoder
167 public static function define_decode_rules() {
168 return array();
171 // Protected API starts here
174 * Define the common setting that any restore section will have
176 protected function define_settings() {
177 // All the settings related to this activity will include this prefix
178 $settingprefix = 'section_' . $this->info->sectionid . '_';
180 // All these are common settings to be shared by all sections.
181 $sectionincluded = $this->add_section_included_setting($settingprefix);
182 $this->add_section_userinfo_setting($settingprefix, $sectionincluded);
186 * Add the section included setting to the task.
188 * @param string $settingprefix the identifier of the setting
189 * @return section_backup_setting the setting added
191 protected function add_section_included_setting(string $settingprefix): section_backup_setting {
192 global $DB;
193 // Define sectionincluded (to decide if the whole task must be really executed).
194 $settingname = $settingprefix . 'included';
196 $delegatedcmid = $this->get_delegated_cm();
197 if ($delegatedcmid) {
198 $sectionincluded = new restore_subsection_included_setting($settingname, base_setting::IS_BOOLEAN, true);
199 // Subsections depends on the parent activity included setting.
200 $settingname = $this->get_modname() . '_' . $delegatedcmid . '_included';
201 if ($this->plan->setting_exists($settingname)) {
202 $cmincluded = $this->plan->get_setting($settingname);
203 $cmincluded->add_dependency(
204 $sectionincluded,
207 $label = get_string('subsectioncontent', 'backup');
208 } else {
209 $sectionincluded = new restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
211 if (is_number($this->info->title)) {
212 $label = get_string('includesection', 'backup', $this->info->title);
213 } else if (empty($this->info->title)) { // Don't throw error if title is empty, gracefully continue restore.
214 $this->log(
215 'Section title missing in backup for section id ' . $this->info->sectionid,
216 backup::LOG_WARNING,
217 $this->name
219 $label = get_string('unnamedsection', 'backup');
220 } else {
221 $label = $this->info->title;
225 $sectionincluded->get_ui()->set_label($label);
226 $this->add_setting($sectionincluded);
228 return $sectionincluded;
232 * Add the section userinfo setting to the task.
234 * @param string $settingprefix the identifier of the setting
235 * @param section_backup_setting $includefield the section included setting
236 * @return section_backup_setting the setting added
238 protected function add_section_userinfo_setting(
239 string $settingprefix,
240 section_backup_setting $includefield
241 ): section_backup_setting {
242 // Define sectionuserinfo. Dependent of:
243 // - users root setting.
244 // - sectionincluded setting.
245 $settingname = $settingprefix . 'userinfo';
246 $defaultvalue = false;
247 if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
248 $defaultvalue = true;
251 $delegatedcmid = $this->get_delegated_cm();
252 if ($delegatedcmid) {
253 $sectionuserinfo = new restore_subsection_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
254 // Subsections depends on the parent activity included setting.
255 $settingname = $this->get_modname() . '_' . $delegatedcmid . '_userinfo';
256 if ($this->plan->setting_exists($settingname)) {
257 $cmincluded = $this->plan->get_setting($settingname);
258 $cmincluded->add_dependency(
259 $sectionuserinfo,
262 } else {
263 $sectionuserinfo = new restore_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
266 if (!$defaultvalue) {
267 // This is a bit hacky, but if there is no user data to restore, then
268 // we replace the standard check-box with a select menu with the
269 // single choice 'No', and the select menu is clever enough that if
270 // there is only one choice, it just displays a static string.
272 // It would probably be better design to have a special UI class
273 // setting_ui_checkbox_or_no, rather than this hack, but I am not
274 // going to do that today.
275 $sectionuserinfo->set_ui(
276 new backup_setting_ui_select($sectionuserinfo,
277 get_string('includeuserinfo', 'backup'), [0 => get_string('no')])
279 } else {
280 $sectionuserinfo->get_ui()->set_label(get_string('includeuserinfo', 'backup'));
283 $this->add_setting($sectionuserinfo);
285 // Look for "users" root setting.
286 $users = $this->plan->get_setting('users');
287 $users->add_dependency($sectionuserinfo);
289 // Look for "section included" section setting.
290 $includefield->add_dependency($sectionuserinfo);
292 return $sectionuserinfo;