backup MDL-23918 Fixed up wording in the backup and restore stages.
[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
5ab8d2de
EL
113 /**
114 * Define the contents in the course that must be
115 * processed by the link decoder
116 */
117 static public function define_decode_contents() {
118 $contents = array();
119
120 $contents[] = new restore_decode_content('course_sections', 'summary', 'course_section');
121
122 return $contents;
123 }
124
125 /**
126 * Define the decoding rules for links belonging
127 * to the sections to be executed by the link decoder
128 */
129 static public function define_decode_rules() {
130 return array();
131 }
132
864842aa
EL
133// Protected API starts here
134
135 /**
136 * Define the common setting that any restore section will have
137 */
138 protected function define_settings() {
139
140 // All the settings related to this activity will include this prefix
141 $settingprefix = 'section_' . $this->info->sectionid . '_';
142
143 // All these are common settings to be shared by all sections
144
145 // Define section_included (to decide if the whole task must be really executed)
146 $settingname = $settingprefix . 'included';
147 $section_included = new restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
70a10238
SH
148 if (is_number($this->info->title)) {
149 $label = get_string('includesection', 'backup', $this->info->title);
150 } else {
151 $label = $this->info->title;
152 }
153 $section_included->get_ui()->set_label($label);
864842aa
EL
154 $this->add_setting($section_included);
155
156 // Define section_userinfo. Dependent of:
157 // - users root setting
158 // - section_included setting
159 $settingname = $settingprefix . 'userinfo';
160 $selectvalues = array(0=>get_string('no')); // Safer options
161 $defaultvalue = false; // Safer default
5ab8d2de 162 if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
864842aa
EL
163 $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
164 $defaultvalue = true;
165 }
166 $section_userinfo = new restore_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
167 $section_userinfo->set_ui(new backup_setting_ui_select($section_userinfo, get_string('includeuserinfo','backup'), $selectvalues));
168 $this->add_setting($section_userinfo);
169 // Look for "users" root setting
170 $users = $this->plan->get_setting('users');
171 $users->add_dependency($section_userinfo);
172 // Look for "section_included" section setting
173 $section_included->add_dependency($section_userinfo);
174 }
175}