Merge branch 'MDL-72597' of git://github.com/paulholden/moodle
[moodle.git] / backup / moodle2 / backup_block_task.class.php
blob3a1fcdd944891d974840c8ef6ca5fcd24336c315
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 * @package core_backup
20 * @subpackage moodle2
21 * @category backup
22 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * abstract block task that provides all the properties and common steps to be performed
30 * when one block is being backup
32 * TODO: Finish phpdocs
34 abstract class backup_block_task extends backup_task {
36 protected $blockid;
37 protected $blockname;
38 protected $contextid;
39 protected $moduleid;
40 protected $modulename;
41 protected $parentcontextid;
43 /**
44 * Constructor - instantiates one object of this class
46 public function __construct($name, $blockid, $moduleid = null, $plan = null) {
47 global $DB;
49 // Check blockid exists
50 if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) {
51 throw new backup_task_exception('block_task_block_instance_not_found', $blockid);
54 $this->blockid = $blockid;
55 $this->blockname = $block->blockname;
56 $this->contextid = context_block::instance($this->blockid)->id;
57 $this->moduleid = $moduleid;
58 $this->modulename = null;
59 $this->parentcontextid = null;
61 // If moduleid passed, check exists, supports moodle2 format and save info
62 // Check moduleid exists
63 if (!empty($moduleid)) {
64 if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
65 throw new backup_task_exception('block_task_coursemodule_not_found', $moduleid);
67 // Check activity supports this moodle2 backup format
68 if (!plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) {
69 throw new backup_task_exception('block_task_activity_lacks_moodle2_backup_support', $coursemodule->modname);
72 $this->moduleid = $moduleid;
73 $this->modulename = $coursemodule->modname;
74 $this->parentcontextid = context_module::instance($this->moduleid)->id;
77 parent::__construct($name, $plan);
80 public function get_blockid() {
81 return $this->blockid;
84 public function get_blockname() {
85 return $this->blockname;
88 public function get_moduleid() {
89 return $this->moduleid;
92 public function get_modulename() {
93 return $this->modulename;
96 public function get_contextid() {
97 return $this->contextid;
100 public function get_parentcontextid() {
101 return $this->parentcontextid;
105 * Block tasks have their own directory to write files
107 public function get_taskbasepath() {
108 $basepath = $this->get_basepath();
110 // Module blocks are under module dir
111 if (!empty($this->moduleid)) {
112 $basepath .= '/activities/' . $this->modulename . '_' . $this->moduleid .
113 '/blocks/' . $this->blockname . '_' . $this->blockid;
115 // Course blocks are under course dir
116 } else {
117 $basepath .= '/course/blocks/' . $this->blockname . '_' . $this->blockid;
119 return $basepath;
123 * Create all the steps that will be part of this task
125 public function build() {
127 // If we have decided not to backup blocks, prevent anything to be built
128 if (!$this->get_setting_value('blocks')) {
129 $this->built = true;
130 return;
133 // If "child" of activity task and it has been excluded, nothing to do
134 if (!empty($this->moduleid)) {
135 $includedsetting = $this->modulename . '_' . $this->moduleid . '_included';
136 if (!$this->get_setting_value($includedsetting)) {
137 $this->built = true;
138 return;
142 // Add some extra settings that related processors are going to need
143 $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKID, base_setting::IS_INTEGER, $this->blockid));
144 $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKNAME, base_setting::IS_FILENAME, $this->blockname));
145 $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid));
146 $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename));
147 $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
148 $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid));
150 // Create the block directory
151 $this->add_step(new create_taskbasepath_directory('create_block_directory'));
153 // Create the block.xml common file (instance + positions)
154 $this->add_step(new backup_block_instance_structure_step('block_commons', 'block.xml'));
156 // Here we add all the common steps for any block and, in the point of interest
157 // we call to define_my_steps() in order to get the particular ones inserted in place.
158 $this->define_my_steps();
160 // Generate the roles file (optionally role assignments and always role overrides)
161 $this->add_step(new backup_roles_structure_step('block_roles', 'roles.xml'));
163 // Generate the comments file (conditionally)
164 if ($this->get_setting_value('comments')) {
165 $this->add_step(new backup_comments_structure_step('block_comments', 'comments.xml'));
168 // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
169 $this->add_step(new backup_inforef_structure_step('block_inforef', 'inforef.xml'));
171 // Migrate the already exported inforef entries to final ones
172 $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
174 // At the end, mark it as built
175 $this->built = true;
178 // Protected API starts here
181 * Define the common setting that any backup block will have
183 protected function define_settings() {
185 // Nothing to add, blocks doesn't have common settings (for now)
187 // End of common activity settings, let's add the particular ones
188 $this->define_my_settings();
192 * Define (add) particular settings that each block can have
194 abstract protected function define_my_settings();
197 * Define (add) particular steps that each block can have
199 abstract protected function define_my_steps();
202 * Define one array() of fileareas that each block controls
204 abstract public function get_fileareas();
207 * Define one array() of configdata attributes
208 * that need to be processed by the contenttransformer
210 abstract public function get_configdata_encoded_attributes();
213 * Code the transformations to perform in the block in
214 * order to get transportable (encoded) links
216 static public function encode_content_links($content) {
217 throw new coding_exception('encode_content_links() method needs to be overridden in each subclass of backup_block_task');