MDL-23478 backup - adding support for fileareas to blocks
[moodle.git] / backup / moodle2 / backup_block_task.class.php
CommitLineData
77547b46
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 * abstract block task that provides all the properties and common steps to be performed
27 * when one block is being backup
28 *
29 * TODO: Finish phpdocs
30 */
31abstract class backup_block_task extends backup_task {
32
33 protected $blockid;
34 protected $blockname;
35 protected $contextid;
36 protected $moduleid;
37 protected $modulename;
38 protected $parentcontextid;
39
40 /**
41 * Constructor - instantiates one object of this class
42 */
43 public function __construct($name, $blockid, $moduleid = null, $plan = null) {
44 global $DB;
45
46 // Check blockid exists
47 if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) {
2d3522ae 48 throw new backup_task_exception('block_task_block_instance_not_found', $blockid);
77547b46
EL
49 }
50
51 $this->blockid = $blockid;
52 $this->blockname = $block->blockname;
53 $this->contextid = get_context_instance(CONTEXT_BLOCK, $this->blockid)->id;
54 $this->moduleid = $moduleid;
55 $this->modulename = null;
56 $this->parentcontextid = null;
57
58 // If moduleid passed, check exists, supports moodle2 format and save info
59 // Check moduleid exists
60 if (!empty($moduleid)) {
61 if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
62 throw new backup_task_exception('block_task_coursemodule_not_found', $moduleid);
63 }
64 // Check activity supports this moodle2 backup format
65 if (!plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) {
66 throw new backup_task_exception('block_task_activity_lacks_moodle2_backup_support', $coursemodule->modname);
67 }
68
69 $this->moduleid = $moduleid;
70 $this->modulename = $coursemodule->modname;
71 $this->parentcontextid = get_context_instance(CONTEXT_MODULE, $this->moduleid)->id;
72 }
73
74 parent::__construct($name, $plan);
75 }
76
77 public function get_blockid() {
78 return $this->blockid;
79 }
80
81 public function get_blockname() {
82 return $this->blockname;
83 }
84
85 public function get_moduleid() {
86 return $this->moduleid;
87 }
88
89 public function get_modulename() {
90 return $this->modulename;
91 }
92
93 public function get_contextid() {
94 return $this->contextid;
95 }
96
97 public function get_parentcontextid() {
98 return $this->parentcontextid;
99 }
100
101 /**
102 * Block tasks have their own directory to write files
103 */
104 public function get_taskbasepath() {
105 $basepath = $this->get_basepath();
106
107 // Module blocks are under module dir
108 if (!empty($this->moduleid)) {
109 $basepath .= '/activities/' . $this->modulename . '_' . $this->moduleid .
110 '/blocks/' . $this->blockname . '_' . $this->blockid;
111
112 // Course blocks are under course dir
113 } else {
114 $basepath .= '/course/blocks/' . $this->blockname . '_' . $this->blockid;
115 }
116 return $basepath;
117 }
118
119 /**
120 * Create all the steps that will be part of this task
121 */
122 public function build() {
123
124 // If we have decided not to backup blocks, prevent anything to be built
125 if (!$this->get_setting_value('blocks')) {
126 $this->built = true;
127 return;
128 }
129
ccb5335f
EL
130 // If "child" of activity task and it has been excluded, nothing to do
131 if (!empty($this->moduleid)) {
132 $includedsetting = $this->modulename . '_' . $this->moduleid . '_included';
133 if (!$this->get_setting_value($includedsetting)) {
134 $this->built = true;
135 return;
136 }
137 }
138
77547b46
EL
139 // Add some extra settings that related processors are going to need
140 $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKID, base_setting::IS_INTEGER, $this->blockid));
141 $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKNAME, base_setting::IS_FILENAME, $this->blockname));
142 $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid));
143 $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename));
144 $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
145 $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid));
146
147 // Create the block directory
148 $this->add_step(new create_taskbasepath_directory('create_block_directory'));
149
150 // Create the block.xml common file (instance + positions)
151 $this->add_step(new backup_block_instance_structure_step('block_commons', 'block.xml'));
152
153 // Here we add all the common steps for any block and, in the point of interest
154 // we call to define_my_steps() is order to get the particular ones inserted in place.
155 $this->define_my_steps();
156
157 // Generate the roles file (optionally role assignments and always role overrides)
158 $this->add_step(new backup_roles_structure_step('block_roles', 'roles.xml'));
159
160 // Generate the comments file (conditionally)
161 if ($this->get_setting_value('comments')) {
162 $this->add_step(new backup_comments_structure_step('block_comments', 'comments.xml'));
163 }
164
165 // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
166 $this->add_step(new backup_inforef_structure_step('block_inforef', 'inforef.xml'));
167
168 // Migrate the already exported inforef entries to final ones
169 $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
170
171 // At the end, mark it as built
172 $this->built = true;
173 }
174
175// Protected API starts here
176
177 /**
178 * Define the common setting that any backup block will have
179 */
180 protected function define_settings() {
181
182 // Nothing to add, blocks doesn't have common settings (for now)
183
184 // End of common activity settings, let's add the particular ones
185 $this->define_my_settings();
186 }
187
188 /**
189 * Define (add) particular settings that each block can have
190 */
191 abstract protected function define_my_settings();
192
193 /**
194 * Define (add) particular steps that each block can have
195 */
196 abstract protected function define_my_steps();
197
4a15bb76
EL
198 /**
199 * Define one array() of fileareas that each block controls
200 */
201 abstract public function get_fileareas();
202
77547b46
EL
203 /**
204 * Define one array() of configdata attributes
205 * that need to be processed by the contenttransformer
206 */
207 abstract public function get_configdata_encoded_attributes();
208
209 /**
210 * Code the transformations to perform in the block in
211 * order to get transportable (encoded) links
212 */
213 abstract static public function encode_content_links($content);
214}