PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/moodle2/backup_plugin.class.php

http://github.com/moodle/moodle
PHP | 115 lines | 40 code | 17 blank | 58 comment | 3 complexity | 49198e8f934ea50e8d7979f0585e053f MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Defines backup_plugin class
  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
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * Class implementing the plugins support for moodle2 backups
  28. *
  29. * TODO: Finish phpdocs
  30. */
  31. abstract class backup_plugin {
  32. /** @var string */
  33. protected $plugintype;
  34. /** @var string */
  35. protected $pluginname;
  36. /** @var string */
  37. protected $connectionpoint;
  38. /** @var backup_optigroup_element */
  39. protected $optigroup; // Optigroup, parent of all optigroup elements
  40. /** @var backup_structure_step */
  41. protected $step;
  42. /** @var backup_course_task|backup_activity_task */
  43. protected $task;
  44. /**
  45. * backup_plugin constructor.
  46. *
  47. * @param string $plugintype
  48. * @param string $pluginname
  49. * @param backup_optigroup_element $optigroup
  50. * @param backup_structure_step $step
  51. */
  52. public function __construct($plugintype, $pluginname, $optigroup, $step) {
  53. $this->plugintype = $plugintype;
  54. $this->pluginname = $pluginname;
  55. $this->optigroup = $optigroup;
  56. $this->connectionpoint = '';
  57. $this->step = $step;
  58. $this->task = $step->get_task();
  59. }
  60. public function define_plugin_structure($connectionpoint) {
  61. $this->connectionpoint = $connectionpoint;
  62. $methodname = 'define_' . $connectionpoint . '_plugin_structure';
  63. if (method_exists($this, $methodname)) {
  64. $this->$methodname();
  65. }
  66. }
  67. // Protected API starts here
  68. // backup_step/structure_step/task wrappers
  69. /**
  70. * Returns the value of one (task/plan) setting
  71. */
  72. protected function get_setting_value($name) {
  73. if (is_null($this->task)) {
  74. throw new backup_step_exception('not_specified_backup_task');
  75. }
  76. return $this->task->get_setting_value($name);
  77. }
  78. // end of backup_step/structure_step/task wrappers
  79. /**
  80. * Factory method that will return one backup_plugin_element (backup_optigroup_element)
  81. * with its name automatically calculated, based one the plugin being handled (type, name)
  82. */
  83. protected function get_plugin_element($final_elements = null, $conditionparam = null, $conditionvalue = null) {
  84. // Something exclusive for this backup_plugin_element (backup_optigroup_element)
  85. // because it hasn't XML representation
  86. $name = 'optigroup_' . $this->plugintype . '_' . $this->pluginname . '_' . $this->connectionpoint;
  87. $optigroup_element = new backup_plugin_element($name, $final_elements, $conditionparam, $conditionvalue);
  88. $this->optigroup->add_child($optigroup_element); // Add optigroup_element to stay connected since beginning
  89. return $optigroup_element;
  90. }
  91. /**
  92. * Simple helper function that suggests one name for the main nested element in plugins
  93. * It's not mandatory to use it but recommended ;-)
  94. */
  95. protected function get_recommended_name() {
  96. return 'plugin_' . $this->plugintype . '_' . $this->pluginname . '_' . $this->connectionpoint;
  97. }
  98. }