MDL-24817 backup - added support for decode contents in plugins (and qtypes)

This commit is contained in:
Eloy Lafuente 2010-11-01 15:49:01 +00:00
parent b5e58c1831
commit 9f68f2d5a6
12 changed files with 119 additions and 193 deletions

View file

@ -26,6 +26,7 @@
* Class implementing the subplugins support for moodle2 backups
*
* TODO: Finish phpdocs
* TODO: Make this subclass of backup_plugin
*/
abstract class backup_subplugin {

View file

@ -81,6 +81,59 @@ abstract class restore_plugin {
}
}
/**
* Returns one array with all the decode contents
* to be processed by the links decoder
*
* This method, given one plugin type, returns one
* array of {@link restore_decode_content} objects
* that will be added to the restore decoder in order
* to perform modifications under the plugin contents.
*
* The objects are retrieved by calling to the {@link define_decode_contents}
* method (when available), first in the main restore_xxxx_plugin class
* and later on each of the available subclasses
*/
static public function get_restore_decode_contents($plugintype) {
$decodecontents = array();
// Check the requested plugintype is a valid one
if (!array_key_exists($plugintype, get_plugin_types($plugintype))) {
throw new backup_step_exception('incorrect_plugin_type', $plugintype);
}
// Check the base plugin class exists
$classname = 'restore_' . $plugintype . '_plugin';
if (!class_exists($classname)) {
throw new backup_step_exception('plugin_class_not_found', $classname);
}
// First, call to the define_plugin_decode_contents in the base plugin class
// (must exist by design in all the plugin base classes)
if (method_exists($classname, 'define_plugin_decode_contents')) {
$decodecontents = array_merge($decodecontents, call_user_func(array($classname, 'define_plugin_decode_contents')));
}
// Now, iterate over all the possible plugins available
// (only the needed ones have been loaded, so they will
// be the ones being asked here). Fetch their restore contents
// by calling (if exists) to their define_decode_contents() method
$plugins = get_plugin_list($plugintype);
foreach ($plugins as $plugin => $plugindir) {
$classname = 'restore_' . $plugintype . '_' . $plugin . '_plugin';
if (class_exists($classname)) {
if (method_exists($classname, 'define_decode_contents')) {
$decodecontents = array_merge($decodecontents, call_user_func(array($classname, 'define_decode_contents')));
}
}
}
return $decodecontents;
}
/**
* Define the contents in the plugin that must be
* processed by the link decoder
*/
static public function define_plugin_decode_contents() {
throw new coding_exception('define_plugin_decode_contents() method needs to be overridden in each subclass of restore_plugin');
}
// Protected API starts here
// restore_step/structure_step/task wrappers

View file

@ -299,4 +299,23 @@ abstract class restore_qtype_plugin extends restore_plugin {
// By default, return answer unmodified, qtypes needing recode will override this
return $state->answer;
}
/**
* Return the contents of the questions stuff that must be processed by the links decoder
*
* Only common stuff to all plugins, in this case:
* - question: text and feedback
* - question_answers: text and feedbak
*
* Note each qtype will have, if needed, its own define_decode_contents method
*/
static public function define_plugin_decode_contents() {
$contents = array();
$contents[] = new restore_decode_content('question', array('questiontext', 'generalfeedback'), 'question_created');
$contents[] = new restore_decode_content('question_answers', array('answer', 'feedback'), 'question_answer');
return $contents;
}
}

View file

@ -348,8 +348,11 @@ class restore_gradebook_structure_step extends restore_structure_step {
class restore_decode_interlinks extends restore_execution_step {
protected function define_execution() {
// Just that
$this->task->get_decoder()->execute();
// Get the decoder (from the plan)
$decoder = $this->task->get_decoder();
restore_decode_processor::register_link_decoders($decoder); // Add decoder contents and rules
// And launch it, everything will be processed
$decoder->execute();
}
}

View file

@ -26,6 +26,7 @@
* Class implementing the subplugins support for moodle2 restore
*
* TODO: Finish phpdocs
* TODO: Make this subclass of restore_plugin
* TODO: Add support for declaring decode_contents (not decode_rules)
*/
abstract class restore_subplugin {

View file

@ -128,12 +128,6 @@ class restore_decode_processor {
}
}
// Add the course format ones
// TODO: Same than blocks, need to know how courseformats are going to handle restore
// Add local encodes
// TODO: Any interest? 1.9 never had that.
// We have all the tasks registered, let's iterate over them, getting
// contents and rules and adding them to the processor
foreach ($tasks as $classname) {
@ -154,6 +148,19 @@ class restore_decode_processor {
$processor->add_rule($rule);
}
}
// Now process all the plugins contents (note plugins don't have support for rules)
// TODO: Add other plugin types (course formats, local...) here if we add them to backup/restore
$plugins = array('qtype');
foreach ($plugins as $plugin) {
$contents = restore_plugin::get_restore_decode_contents($plugin);
if (!is_array($contents)) {
throw new restore_decode_processor_exception('get_restore_decode_contents_not_array', $plugin);
}
foreach ($contents as $content) {
$processor->add_content($content);
}
}
}
// Protected API starts here

View file

@ -61,7 +61,6 @@ class restore_plan extends base_plan implements loggable {
public function build() {
restore_plan_builder::build_plan($this->controller); // We are moodle2 always, go straight to builder
restore_decode_processor::register_link_decoders($this->decoder); // Add decoder contents and rules
$this->built = true;
}