3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
26 * section task that provides all the properties and common steps to be performed
27 * when one section is being restored
29 * TODO: Finish phpdocs
31 class restore_section_task
extends restore_task
{
33 protected $info; // info related to section gathered from backup file
34 protected $contextid; // course context id
35 protected $sectionid; // new (target) id of the course section
38 * Constructor - instantiates one object of this class
40 public function __construct($name, $info, $plan = null) {
43 parent
::__construct($name, $plan);
47 * Section tasks have their own directory to read files
49 public function get_taskbasepath() {
51 return $this->get_basepath() . '/sections/section_' . $this->info
->sectionid
;
54 public function set_sectionid($sectionid) {
55 $this->sectionid
= $sectionid;
58 public function get_contextid() {
59 return $this->contextid
;
62 public function get_sectionid() {
63 return $this->sectionid
;
67 * Create all the steps that will be part of this task
69 public function build() {
71 // Define the task contextid (the course one)
72 $this->contextid
= get_context_instance(CONTEXT_COURSE
, $this->get_courseid())->id
;
74 // We always try to restore as much info from sections as possible, no matter of the type
75 // of restore (new, existing, deleting, import...). MDL-27764
76 $this->add_step(new restore_section_structure_step('course_info', 'section.xml'));
78 // At the end, mark it as built
83 * Exceptionally override the execute method, so, based in the section_included setting, we are able
84 * to skip the execution of one task completely
86 public function execute() {
88 // Find activity_included_setting
89 if (!$this->get_setting_value('included')) {
90 $this->log('activity skipped by _included setting', backup
::LOG_DEBUG
, $this->name
);
92 } else { // Setting tells us it's ok to execute
98 * Specialisation that, first of all, looks for the setting within
99 * the task with the the prefix added and later, delegates to parent
100 * without adding anything
102 public function get_setting($name) {
103 $namewithprefix = 'section_' . $this->info
->sectionid
. '_' . $name;
105 foreach ($this->settings
as $key => $setting) {
106 if ($setting->get_name() == $namewithprefix) {
107 if ($result != null) {
108 throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
117 // Fallback to parent
118 return parent
::get_setting($name);
123 * Define the contents in the course that must be
124 * processed by the link decoder
126 static public function define_decode_contents() {
129 $contents[] = new restore_decode_content('course_sections', 'summary', 'course_section');
135 * Define the decoding rules for links belonging
136 * to the sections to be executed by the link decoder
138 static public function define_decode_rules() {
142 // Protected API starts here
145 * Define the common setting that any restore section will have
147 protected function define_settings() {
149 // All the settings related to this activity will include this prefix
150 $settingprefix = 'section_' . $this->info
->sectionid
. '_';
152 // All these are common settings to be shared by all sections
154 // Define section_included (to decide if the whole task must be really executed)
155 $settingname = $settingprefix . 'included';
156 $section_included = new restore_section_included_setting($settingname, base_setting
::IS_BOOLEAN
, true);
157 if (is_number($this->info
->title
)) {
158 $label = get_string('includesection', 'backup', $this->info
->title
);
160 $label = $this->info
->title
;
162 $section_included->get_ui()->set_label($label);
163 $this->add_setting($section_included);
165 // Define section_userinfo. Dependent of:
166 // - users root setting
167 // - section_included setting
168 $settingname = $settingprefix . 'userinfo';
169 $selectvalues = array(0=>get_string('no')); // Safer options
170 $defaultvalue = false; // Safer default
171 if (isset($this->info
->settings
[$settingname]) && $this->info
->settings
[$settingname]) { // Only enabled when available
172 $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
173 $defaultvalue = true;
175 $section_userinfo = new restore_section_userinfo_setting($settingname, base_setting
::IS_BOOLEAN
, $defaultvalue);
176 $section_userinfo->set_ui(new backup_setting_ui_select($section_userinfo, get_string('includeuserinfo','backup'), $selectvalues));
177 $this->add_setting($section_userinfo);
178 // Look for "users" root setting
179 $users = $this->plan
->get_setting('users');
180 $users->add_dependency($section_userinfo);
181 // Look for "section_included" section setting
182 $section_included->add_dependency($section_userinfo);