|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00031 abstract class backup_block_task extends backup_task { 00032 00033 protected $blockid; 00034 protected $blockname; 00035 protected $contextid; 00036 protected $moduleid; 00037 protected $modulename; 00038 protected $parentcontextid; 00039 00043 public function __construct($name, $blockid, $moduleid = null, $plan = null) { 00044 global $DB; 00045 00046 // Check blockid exists 00047 if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) { 00048 throw new backup_task_exception('block_task_block_instance_not_found', $blockid); 00049 } 00050 00051 $this->blockid = $blockid; 00052 $this->blockname = $block->blockname; 00053 $this->contextid = get_context_instance(CONTEXT_BLOCK, $this->blockid)->id; 00054 $this->moduleid = $moduleid; 00055 $this->modulename = null; 00056 $this->parentcontextid = null; 00057 00058 // If moduleid passed, check exists, supports moodle2 format and save info 00059 // Check moduleid exists 00060 if (!empty($moduleid)) { 00061 if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) { 00062 throw new backup_task_exception('block_task_coursemodule_not_found', $moduleid); 00063 } 00064 // Check activity supports this moodle2 backup format 00065 if (!plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) { 00066 throw new backup_task_exception('block_task_activity_lacks_moodle2_backup_support', $coursemodule->modname); 00067 } 00068 00069 $this->moduleid = $moduleid; 00070 $this->modulename = $coursemodule->modname; 00071 $this->parentcontextid = get_context_instance(CONTEXT_MODULE, $this->moduleid)->id; 00072 } 00073 00074 parent::__construct($name, $plan); 00075 } 00076 00077 public function get_blockid() { 00078 return $this->blockid; 00079 } 00080 00081 public function get_blockname() { 00082 return $this->blockname; 00083 } 00084 00085 public function get_moduleid() { 00086 return $this->moduleid; 00087 } 00088 00089 public function get_modulename() { 00090 return $this->modulename; 00091 } 00092 00093 public function get_contextid() { 00094 return $this->contextid; 00095 } 00096 00097 public function get_parentcontextid() { 00098 return $this->parentcontextid; 00099 } 00100 00104 public function get_taskbasepath() { 00105 $basepath = $this->get_basepath(); 00106 00107 // Module blocks are under module dir 00108 if (!empty($this->moduleid)) { 00109 $basepath .= '/activities/' . $this->modulename . '_' . $this->moduleid . 00110 '/blocks/' . $this->blockname . '_' . $this->blockid; 00111 00112 // Course blocks are under course dir 00113 } else { 00114 $basepath .= '/course/blocks/' . $this->blockname . '_' . $this->blockid; 00115 } 00116 return $basepath; 00117 } 00118 00122 public function build() { 00123 00124 // If we have decided not to backup blocks, prevent anything to be built 00125 if (!$this->get_setting_value('blocks')) { 00126 $this->built = true; 00127 return; 00128 } 00129 00130 // If "child" of activity task and it has been excluded, nothing to do 00131 if (!empty($this->moduleid)) { 00132 $includedsetting = $this->modulename . '_' . $this->moduleid . '_included'; 00133 if (!$this->get_setting_value($includedsetting)) { 00134 $this->built = true; 00135 return; 00136 } 00137 } 00138 00139 // Add some extra settings that related processors are going to need 00140 $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKID, base_setting::IS_INTEGER, $this->blockid)); 00141 $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKNAME, base_setting::IS_FILENAME, $this->blockname)); 00142 $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid)); 00143 $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename)); 00144 $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid())); 00145 $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid)); 00146 00147 // Create the block directory 00148 $this->add_step(new create_taskbasepath_directory('create_block_directory')); 00149 00150 // Create the block.xml common file (instance + positions) 00151 $this->add_step(new backup_block_instance_structure_step('block_commons', 'block.xml')); 00152 00153 // Here we add all the common steps for any block and, in the point of interest 00154 // we call to define_my_steps() in order to get the particular ones inserted in place. 00155 $this->define_my_steps(); 00156 00157 // Generate the roles file (optionally role assignments and always role overrides) 00158 $this->add_step(new backup_roles_structure_step('block_roles', 'roles.xml')); 00159 00160 // Generate the comments file (conditionally) 00161 if ($this->get_setting_value('comments')) { 00162 $this->add_step(new backup_comments_structure_step('block_comments', 'comments.xml')); 00163 } 00164 00165 // Generate the inforef file (must be after ALL steps gathering annotations of ANY type) 00166 $this->add_step(new backup_inforef_structure_step('block_inforef', 'inforef.xml')); 00167 00168 // Migrate the already exported inforef entries to final ones 00169 $this->add_step(new move_inforef_annotations_to_final('migrate_inforef')); 00170 00171 // At the end, mark it as built 00172 $this->built = true; 00173 } 00174 00175 // Protected API starts here 00176 00180 protected function define_settings() { 00181 00182 // Nothing to add, blocks doesn't have common settings (for now) 00183 00184 // End of common activity settings, let's add the particular ones 00185 $this->define_my_settings(); 00186 } 00187 00191 abstract protected function define_my_settings(); 00192 00196 abstract protected function define_my_steps(); 00197 00201 abstract public function get_fileareas(); 00202 00207 abstract public function get_configdata_encoded_attributes(); 00208 00213 static public function encode_content_links($content) { 00214 throw new coding_exception('encode_content_links() method needs to be overridden in each subclass of backup_block_task'); 00215 } 00216 }