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 * abstract block task that provides all the properties and common steps to be performed
27 * when one block is being backup
29 * TODO: Finish phpdocs
31 abstract class backup_block_task extends backup_task {
37 protected $modulename;
38 protected $parentcontextid;
41 * Constructor - instantiates one object of this class
43 public function __construct($name, $blockid, $moduleid = null, $plan = null) {
46 // Check blockid exists
47 if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) {
48 throw new backup_task_exception('block_task_block_instance_not_found', $blockid);
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;
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);
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);
69 $this->moduleid = $moduleid;
70 $this->modulename = $coursemodule->modname;
71 $this->parentcontextid = get_context_instance(CONTEXT_MODULE, $this->moduleid)->id;
74 parent::__construct($name, $plan);
77 public function get_blockid() {
78 return $this->blockid;
81 public function get_blockname() {
82 return $this->blockname;
85 public function get_moduleid() {
86 return $this->moduleid;
89 public function get_modulename() {
90 return $this->modulename;
93 public function get_contextid() {
94 return $this->contextid;
97 public function get_parentcontextid() {
98 return $this->parentcontextid;
102 * Block tasks have their own directory to write files
104 public function get_taskbasepath() {
105 $basepath = $this->get_basepath();
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;
112 // Course blocks are under course dir
114 $basepath .= '/course/blocks/' . $this->blockname . '_' . $this->blockid;
120 * Create all the steps that will be part of this task
122 public function build() {
124 // If we have decided not to backup blocks, prevent anything to be built
125 if (!$this->get_setting_value('blocks')) {
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)) {
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));
147 // Create the block directory
148 $this->add_step(new create_taskbasepath_directory('create_block_directory'));
150 // Create the block.xml common file (instance + positions)
151 $this->add_step(new backup_block_instance_structure_step('block_commons', 'block.xml'));
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();
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'));
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'));
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'));
168 // Migrate the already exported inforef entries to final ones
169 $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
171 // At the end, mark it as built
175 // Protected API starts here
178 * Define the common setting that any backup block will have
180 protected function define_settings() {
182 // Nothing to add, blocks doesn't have common settings (for now)
184 // End of common activity settings, let's add the particular ones
185 $this->define_my_settings();
189 * Define (add) particular settings that each block can have
191 abstract protected function define_my_settings();
194 * Define (add) particular steps that each block can have
196 abstract protected function define_my_steps();
199 * Define one array() of fileareas that each block controls
201 abstract public function get_fileareas();
204 * Define one array() of configdata attributes
205 * that need to be processed by the contenttransformer
207 abstract public function get_configdata_encoded_attributes();
210 * Code the transformations to perform in the block in
211 * order to get transportable (encoded) links
213 abstract static public function encode_content_links($content);