MDL-21432 backup - restore sections
[moodle.git] / backup / moodle2 / restore_section_task.class.php
CommitLineData
864842aa
EL
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 * @package moodlecore
20 * @subpackage backup-moodle2
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * section task that provides all the properties and common steps to be performed
27 * when one section is being restored
28 *
29 * TODO: Finish phpdocs
30 */
31class restore_section_task extends restore_task {
32
33 protected $info; // info related to section gathered from backup file
3223cc95 34 protected $contextid; // course context id
864842aa
EL
35
36 /**
37 * Constructor - instantiates one object of this class
38 */
39 public function __construct($name, $info, $plan = null) {
40 $this->info = $info;
41 parent::__construct($name, $plan);
42 }
43
44 /**
45 * Section tasks have their own directory to read files
46 */
47 public function get_taskbasepath() {
48
482aac65 49 return $this->get_basepath() . '/sections/section_' . $this->info->sectionid;
864842aa
EL
50 }
51
3223cc95
EL
52 public function get_contextid() {
53 return $this->contextid;
54 }
55
864842aa
EL
56 /**
57 * Create all the steps that will be part of this task
58 */
59 public function build() {
60
3223cc95
EL
61 // Define the task contextid (the course one)
62 $this->contextid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id;
63
64 // Executed conditionally if restoring to new course or deleting or if overwrite_conf setting is enabled
65 if ($this->get_target() == backup::TARGET_NEW_COURSE || $this->get_setting_value('overwrite_conf') == true) {
66 $this->add_step(new restore_section_structure_step('course_info', 'section.xml'));
67 }
864842aa
EL
68
69 // At the end, mark it as built
70 $this->built = true;
71 }
72
482aac65
EL
73 /**
74 * Exceptionally override the execute method, so, based in the section_included setting, we are able
75 * to skip the execution of one task completely
76 */
77 public function execute() {
78
79 // Find activity_included_setting
80 if (!$this->get_setting_value('included')) {
81 $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
82
83 } else { // Setting tells us it's ok to execute
84 parent::execute();
85 }
86 }
87
88 /**
89 * Specialisation that, first of all, looks for the setting within
90 * the task with the the prefix added and later, delegates to parent
91 * without adding anything
92 */
93 public function get_setting($name) {
94 $namewithprefix = 'section_' . $this->info->sectionid . '_' . $name;
95 $result = null;
96 foreach ($this->settings as $key => $setting) {
97 if ($setting->get_name() == $namewithprefix) {
98 if ($result != null) {
99 throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
100 } else {
101 $result = $setting;
102 }
103 }
104 }
105 if ($result) {
106 return $result;
107 } else {
108 // Fallback to parent
109 return parent::get_setting($name);
110 }
111 }
112
864842aa
EL
113// Protected API starts here
114
115 /**
116 * Define the common setting that any restore section will have
117 */
118 protected function define_settings() {
119
120 // All the settings related to this activity will include this prefix
121 $settingprefix = 'section_' . $this->info->sectionid . '_';
122
123 // All these are common settings to be shared by all sections
124
125 // Define section_included (to decide if the whole task must be really executed)
126 $settingname = $settingprefix . 'included';
127 $section_included = new restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
128 $this->add_setting($section_included);
129
130 // Define section_userinfo. Dependent of:
131 // - users root setting
132 // - section_included setting
133 $settingname = $settingprefix . 'userinfo';
134 $selectvalues = array(0=>get_string('no')); // Safer options
135 $defaultvalue = false; // Safer default
136 if (isset($info->settings[$settingname]) && $info->settings[$settingname]) { // Only enabled when available
137 $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
138 $defaultvalue = true;
139 }
140 $section_userinfo = new restore_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
141 $section_userinfo->set_ui(new backup_setting_ui_select($section_userinfo, get_string('includeuserinfo','backup'), $selectvalues));
142 $this->add_setting($section_userinfo);
143 // Look for "users" root setting
144 $users = $this->plan->get_setting('users');
145 $users->add_dependency($section_userinfo);
146 // Look for "section_included" section setting
147 $section_included->add_dependency($section_userinfo);
148 }
149}