diff --git a/backup/moodle2/backup_custom_fields.php b/backup/moodle2/backup_custom_fields.php index a56ad659e99..ada8306362e 100644 --- a/backup/moodle2/backup_custom_fields.php +++ b/backup/moodle2/backup_custom_fields.php @@ -101,3 +101,9 @@ class file_nested_element extends backup_nested_element { backup_file_manager::copy_file_moodle2backup($this->backupid, $values); } } + +/** + * Implementation of backup_optigroup_element to be used by subplugins stuff. + * Split just for better separation and future especialitation + */ +class backup_subplugin_element extends backup_optigroup_element { } diff --git a/backup/moodle2/backup_plan_builder.class.php b/backup/moodle2/backup_plan_builder.class.php index 4b12efdd1da..5e81d80d218 100644 --- a/backup/moodle2/backup_plan_builder.class.php +++ b/backup/moodle2/backup_plan_builder.class.php @@ -30,6 +30,7 @@ require_once($CFG->dirroot . '/backup/moodle2/backup_final_task.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_block_task.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_default_block_task.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_xml_transformer.class.php'); +require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_custom_fields.php'); diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index 0ab76fc2803..e9631b39b25 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -75,10 +75,48 @@ class create_taskbasepath_directory extends backup_execution_step { /** * Abtract tructure step, parent of all the activity structure steps. Used to wrap the - * activity structure definition within the main tag + * activity structure definition within the main tag. Also provides + * subplugin support for activities (that must be properly declared) */ abstract class backup_activity_structure_step extends backup_structure_step { + protected function add_subplugin_structure($subpluginname, $element, $multiple) { + + global $CFG; + + // Check the requested subpluginname is a valid one + $subpluginsfile = $CFG->dirroot . '/mod/' . $this->task->get_modulename() . '/db/subplugins.php'; + if (!file_exists($subpluginsfile)) { + throw new backup_step_exception('activity_missing_subplugins_php_file', $this->task->get_modulename()); + } + include($subpluginsfile); + if (!array_key_exists($subpluginname, $subplugins)) { + throw new backup_step_exception('incorrect_subplugin_type', $subpluginname); + } + + // Arrived here, subplugin is correct, let's create the optigroup + $optigroupname = $subpluginname . '_' . $element->get_name() . '_subplugin'; + $optigroup = new backup_optigroup($optigroupname, null, $multiple); + + // Get all the optigroup_elements, looking across al the subplugin dirs + $elements = array(); + $subpluginsdirs = get_plugin_list($subpluginname); + foreach ($subpluginsdirs as $name => $subpluginsdir) { + $classname = 'backup_' . $subpluginname . '_' . $name . '_subplugin'; + $backupfile = $subpluginsdir . '/backup/moodle2/' . $classname . '.class.php'; + if (file_exists($backupfile)) { + require_once($backupfile); + $backupsubplugin = new $classname($subpluginname, $name); + // Add subplugin returned structure to optigroup (must be optigroup_element instance) + if ($subpluginstructure = $backupsubplugin->define_subplugin_structure($element->get_name())) { + $optigroup->add_child($subpluginstructure); + } + } + } + // Finished, add optigroup to element + $element->add_child($optigroup); + } + protected function prepare_activity_structure($activitystructure) { // Create the wrap element diff --git a/backup/moodle2/backup_subplugin.class.php b/backup/moodle2/backup_subplugin.class.php new file mode 100644 index 00000000000..2b2c7985c3d --- /dev/null +++ b/backup/moodle2/backup_subplugin.class.php @@ -0,0 +1,70 @@ +. + +/** + * @package moodlecore + * @subpackage backup-moodle2 + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Class implementing the subplugins support for moodle2 backups + * + * TODO: Finish phpdocs + */ +abstract class backup_subplugin { + + protected $subplugintype; + protected $subpluginname; + + public function __construct($subplugintype, $subpluginname) { + $this->subplugintype = $subplugintype; + $this->subpluginname = $subpluginname; + } + + public function define_subplugin_structure($connectionpoint) { + + $methodname = 'define_' . $connectionpoint . '_subplugin_structure'; + + if (method_exists($this, $methodname)) { + return $this->$methodname($connectionpoint); + } + + return false; + } + + /** + * Factory method that will return one backup_subplugin_element (backup_optigroup_element) + * with its name automatically calculated, based one the subplugin being handled (type, name) + */ + protected function get_subplugin_element($connectionpoint, $final_elements = null, $conditionparam = null, $conditionvalue = null) { + // Something exclusive for this backup_subplugin_element (backup_optigroup_element) + // because it hasn't XML representation + $name = 'optigroup_' . $this->subplugintype . '_' . $this->subpluginname . '_' . $connectionpoint; + return new backup_subplugin_element($name, $final_elements, $conditionparam, $conditionvalue); + } + + /** + * Simple helper function that suggests one name for the main nested element in subplugins + * It's not mandatory to use it but recommended ;-) + */ + protected function get_recommended_name($connectionpoint) { + return 'subplugin_' . $this->subplugintype . '_' . $this->subpluginname . '_' . $connectionpoint; + } + +} diff --git a/backup/util/structure/base_optigroup.class.php b/backup/util/structure/base_optigroup.class.php index 67a01e30b5b..e3f0dfd1815 100644 --- a/backup/util/structure/base_optigroup.class.php +++ b/backup/util/structure/base_optigroup.class.php @@ -131,7 +131,7 @@ abstract class base_optigroup extends base_nested_element { * Recalculate all the used elements in the optigroup, observing * restrictions and passing the new used to outer level */ - function add_used($element) { + protected function add_used($element) { $newused = array(); // Iterate over all the element useds, filling $newused and // observing the multiple setting