Merge branch 'MDL-20367-master' of git://github.com/damyon/moodle
[moodle.git] / backup / moodle2 / restore_section_task.class.php
blobb46ed6b906ac0b2b7254f58606d5844fd716c264
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 public function set_sectionid($sectionid) {
60 $this->sectionid = $sectionid;
63 public function get_contextid() {
64 return $this->contextid;
67 public function get_sectionid() {
68 return $this->sectionid;
71 /**
72 * Create all the steps that will be part of this task
74 public function build() {
76 // Define the task contextid (the course one)
77 $this->contextid = context_course::instance($this->get_courseid())->id;
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'));
83 // At the end, mark it as built
84 $this->built = true;
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
91 public function execute() {
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);
97 } else { // Setting tells us it's ok to execute
98 parent::execute();
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
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;
119 if ($result) {
120 return $result;
121 } else {
122 // Fallback to parent
123 return parent::get_setting($name);
128 * Define the contents in the course that must be
129 * processed by the link decoder
131 static public function define_decode_contents() {
132 $contents = array();
134 $contents[] = new restore_decode_content('course_sections', 'summary', 'course_section');
136 return $contents;
140 * Define the decoding rules for links belonging
141 * to the sections to be executed by the link decoder
143 static public function define_decode_rules() {
144 return array();
147 // Protected API starts here
150 * Define the common setting that any restore section will have
152 protected function define_settings() {
154 // All the settings related to this activity will include this prefix
155 $settingprefix = 'section_' . $this->info->sectionid . '_';
157 // All these are common settings to be shared by all sections
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;
170 $section_included->get_ui()->set_label($label);
171 $this->add_setting($section_included);
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;
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.
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'));
198 $this->add_setting($section_userinfo);
200 // Look for "users" root setting.
201 $users = $this->plan->get_setting('users');
202 $users->add_dependency($section_userinfo);
204 // Look for "section_included" section setting.
205 $section_included->add_dependency($section_userinfo);