PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/backup/moodle2/backup_block_task.class.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 219 lines | 101 code | 41 blank | 77 comment | 11 complexity | 21bae579fec1c3c8d915f9024549fdd9 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package core_backup
  18. * @subpackage moodle2
  19. * @category backup
  20. * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * abstract block task that provides all the properties and common steps to be performed
  26. * when one block is being backup
  27. *
  28. * TODO: Finish phpdocs
  29. */
  30. abstract class backup_block_task extends backup_task {
  31. protected $blockid;
  32. protected $blockname;
  33. protected $contextid;
  34. protected $moduleid;
  35. protected $modulename;
  36. protected $parentcontextid;
  37. /**
  38. * Constructor - instantiates one object of this class
  39. */
  40. public function __construct($name, $blockid, $moduleid = null, $plan = null) {
  41. global $DB;
  42. // Check blockid exists
  43. if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) {
  44. throw new backup_task_exception('block_task_block_instance_not_found', $blockid);
  45. }
  46. $this->blockid = $blockid;
  47. $this->blockname = $block->blockname;
  48. $this->contextid = context_block::instance($this->blockid)->id;
  49. $this->moduleid = $moduleid;
  50. $this->modulename = null;
  51. $this->parentcontextid = null;
  52. // If moduleid passed, check exists, supports moodle2 format and save info
  53. // Check moduleid exists
  54. if (!empty($moduleid)) {
  55. if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
  56. throw new backup_task_exception('block_task_coursemodule_not_found', $moduleid);
  57. }
  58. // Check activity supports this moodle2 backup format
  59. if (!plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) {
  60. throw new backup_task_exception('block_task_activity_lacks_moodle2_backup_support', $coursemodule->modname);
  61. }
  62. $this->moduleid = $moduleid;
  63. $this->modulename = $coursemodule->modname;
  64. $this->parentcontextid = context_module::instance($this->moduleid)->id;
  65. }
  66. parent::__construct($name, $plan);
  67. }
  68. public function get_blockid() {
  69. return $this->blockid;
  70. }
  71. public function get_blockname() {
  72. return $this->blockname;
  73. }
  74. public function get_moduleid() {
  75. return $this->moduleid;
  76. }
  77. public function get_modulename() {
  78. return $this->modulename;
  79. }
  80. public function get_contextid() {
  81. return $this->contextid;
  82. }
  83. public function get_parentcontextid() {
  84. return $this->parentcontextid;
  85. }
  86. /**
  87. * Block tasks have their own directory to write files
  88. */
  89. public function get_taskbasepath() {
  90. $basepath = $this->get_basepath();
  91. // Module blocks are under module dir
  92. if (!empty($this->moduleid)) {
  93. $basepath .= '/activities/' . $this->modulename . '_' . $this->moduleid .
  94. '/blocks/' . $this->blockname . '_' . $this->blockid;
  95. // Course blocks are under course dir
  96. } else {
  97. $basepath .= '/course/blocks/' . $this->blockname . '_' . $this->blockid;
  98. }
  99. return $basepath;
  100. }
  101. /**
  102. * Create all the steps that will be part of this task
  103. */
  104. public function build() {
  105. // If we have decided not to backup blocks, prevent anything to be built
  106. if (!$this->get_setting_value('blocks')) {
  107. $this->built = true;
  108. return;
  109. }
  110. // If "child" of activity task and it has been excluded, nothing to do
  111. if (!empty($this->moduleid)) {
  112. $includedsetting = $this->modulename . '_' . $this->moduleid . '_included';
  113. if (!$this->get_setting_value($includedsetting)) {
  114. $this->built = true;
  115. return;
  116. }
  117. }
  118. // Add some extra settings that related processors are going to need
  119. $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKID, base_setting::IS_INTEGER, $this->blockid));
  120. $this->add_setting(new backup_activity_generic_setting(backup::VAR_BLOCKNAME, base_setting::IS_FILENAME, $this->blockname));
  121. $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid));
  122. $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename));
  123. $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
  124. $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid));
  125. // Create the block directory
  126. $this->add_step(new create_taskbasepath_directory('create_block_directory'));
  127. // Create the block.xml common file (instance + positions)
  128. $this->add_step(new backup_block_instance_structure_step('block_commons', 'block.xml'));
  129. // Here we add all the common steps for any block and, in the point of interest
  130. // we call to define_my_steps() in order to get the particular ones inserted in place.
  131. $this->define_my_steps();
  132. // Generate the roles file (optionally role assignments and always role overrides)
  133. $this->add_step(new backup_roles_structure_step('block_roles', 'roles.xml'));
  134. // Generate the comments file (conditionally)
  135. if ($this->get_setting_value('comments')) {
  136. $this->add_step(new backup_comments_structure_step('block_comments', 'comments.xml'));
  137. }
  138. // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
  139. $this->add_step(new backup_inforef_structure_step('block_inforef', 'inforef.xml'));
  140. // Migrate the already exported inforef entries to final ones
  141. $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
  142. // At the end, mark it as built
  143. $this->built = true;
  144. }
  145. // Protected API starts here
  146. /**
  147. * Define the common setting that any backup block will have
  148. */
  149. protected function define_settings() {
  150. // Nothing to add, blocks doesn't have common settings (for now)
  151. // End of common activity settings, let's add the particular ones
  152. $this->define_my_settings();
  153. }
  154. /**
  155. * Define (add) particular settings that each block can have
  156. */
  157. abstract protected function define_my_settings();
  158. /**
  159. * Define (add) particular steps that each block can have
  160. */
  161. abstract protected function define_my_steps();
  162. /**
  163. * Define one array() of fileareas that each block controls
  164. */
  165. abstract public function get_fileareas();
  166. /**
  167. * Define one array() of configdata attributes
  168. * that need to be processed by the contenttransformer
  169. */
  170. abstract public function get_configdata_encoded_attributes();
  171. /**
  172. * Code the transformations to perform in the block in
  173. * order to get transportable (encoded) links
  174. */
  175. static public function encode_content_links($content) {
  176. throw new coding_exception('encode_content_links() method needs to be overridden in each subclass of backup_block_task');
  177. }
  178. }