Skip to content

Commit

Permalink
Adding in activity support and adding base forum classes as proof of …
Browse files Browse the repository at this point in the history
…concept
  • Loading branch information
polothy authored and mudrd8mz committed May 10, 2011
1 parent 534d429 commit 72c089f
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 18 deletions.
68 changes: 68 additions & 0 deletions backup/converter/moodle1/converter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
* This will be the Moodle 1 to Moodle 2 Converter
*/
class moodle1_converter extends plan_converter {
/**
* The current module being processed
*
* @var string
*/
protected $currentmod = '';

/**
* The current block being processed
*
* @var string
*/
protected $currentblock = '';

/**
* @return boolean
*/
Expand All @@ -31,14 +45,68 @@ public function can_convert() {
return false;
}


/**
* Path transformation for modules and blocks. Here we
* are collapsing paths that use the plugin's name.
*/
public function add_structures($processingobject, array $structures) {
parent::add_structures($processingobject, $structures);

foreach ($structures as $element) {
$path = $element->get_path();

// @todo Add same for blocks
$path = preg_replace('/^\/MOODLE_BACKUP\/COURSE\/MODULES\/MOD\/(\w+)\//', '/MOODLE_BACKUP/COURSE/MODULES/MOD/', $path);
if (!empty($path) and $path != $element->get_path()) {
$this->xmlprocessor->add_path($path, false);
}
}
}

/**
* Path transformation for modules and blocks. Here we
* are expanding paths to include the plugin's name.
*/
public function process($data) {
$path = $data['path'];

// @todo Same path manipulation for blocks
if ($path == '/MOODLE_BACKUP/COURSE/MODULES/MOD') {
$this->currentmod = strtoupper($data['tags']['MODTYPE']);
$path = '/MOODLE_BACKUP/COURSE/MODULES/MOD/'.$this->currentmod;

} else if (strpos($path, '/MOODLE_BACKUP/COURSE/MODULES/MOD') === 0) {
$path = str_replace('/MOODLE_BACKUP/COURSE/MODULES/MOD', '/MOODLE_BACKUP/COURSE/MODULES/MOD/'.$this->currentmod, $path);
}
if ($path != $data['path']) {
// Have relaxed error handling on path transformations...
if (!array_key_exists($path, $this->pathelements)) {
debugging("Path transformation error, $path is not registered, probably similar to another plugin");
return;
}
$data['path'] = $path;
}
parent::process($data);
}

public function build_plan() {
$this->xmlparser = new progressive_parser();
$this->xmlparser->set_file($this->get_tempdir() . '/moodle.xml');
$this->xmlprocessor = new convert_structure_parser_processor($this); // @todo Probably move this
$this->xmlparser->set_processor($this->xmlprocessor);

// These paths are dispatched by the converter through path transformation
$this->xmlprocessor->add_path('/MOODLE_BACKUP/COURSE/MODULES/MOD', false);
// @todo Add the same for blocks

$this->get_plan()->add_task(new moodle1_root_task('root_task'));
$this->get_plan()->add_task(new moodle1_course_task('courseinfo'));

// Build plugin tasks
convert_factory::build_plugin_tasks($this, 'mod', 'activity');
convert_factory::build_plugin_tasks($this, 'block');

$this->get_plan()->add_task(new moodle1_final_task('final_task'));
}
}
24 changes: 24 additions & 0 deletions backup/converter/moodle1/stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,28 @@ public function execute_after_convert() {
$this->write_section_xml();
}
}
}

// @todo DELETE, NOT USED ANYMORE - OLD PROTOTYPE CODE
class moodle1_mod_structure_step extends convert_structure_step {
/**
* Function that will return the structure to be processed by this convert_step.
* Must return one array of @convert_path_element elements
*/
protected function define_structure() {
$paths = array();
$paths[] = new convert_path_element('mod', '/MOODLE_BACKUP/COURSE/MODULES/MOD');

return $paths;
}

public function convert_mod($data) {
// What this will do...
$task = convert_factory::activity_task($this->get_converter(), $data['MODTYPE'], $data);
$this->get_converter()->get_plan()->add_task($task);

// Build and execute now
$task->build();
$task->execute();
}
}
14 changes: 14 additions & 0 deletions backup/converter/moodle1/taskslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,17 @@ public function build() {
}
}

// @todo finnish this class...
abstract class moodle1_activity_task extends convert_task {
/**
* @var array
*/
// protected $instancedata;
/*
public function __construct($name, array $data, convert_plan $plan = null) {
$this->instancedata = $data;
print_object($data);
parent::__construct($name, $plan);
}
*/
}
25 changes: 7 additions & 18 deletions backup/util/converter/plan_converter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,13 @@ public function destroy() {
$this->get_plan()->destroy();
}

// @todo Validation here is weak, probably should validate against a data members that keep track of all names/paths
public function add_structures($processingobject, array $structures) {
// Override if using class convert_structure_step
$this->prepare_pathelements($processingobject, $structures);

// Add pathelements to processor
foreach ($this->pathelements as $element) {
$this->xmlprocessor->add_path($element->get_path(), $element->is_grouped());
}
}

/**
* Prepare the pathelements for processing, looking for duplicates, applying
* processing objects and other adjustments
*/
protected function prepare_pathelements($processingobject, $elementsarr) {
// First iteration, push them to new array, indexed by name
// detecting duplicates in names or paths
$names = array();
$paths = array();
foreach($elementsarr as $element) {
foreach($structures as $element) {
if (!$element instanceof convert_path_element) {
throw new restore_step_exception('restore_path_element_wrong_class', get_class($element)); // @todo Change exception
}
Expand All @@ -90,10 +77,12 @@ protected function prepare_pathelements($processingobject, $elementsarr) {
// Now, for each element not having one processing object, if
// not child of grouped element, assign $this (the step itself) as processing element
// Note method must exist or we'll get one @restore_path_element_exception
foreach($paths as $key => $pelement) {
if ($pelement->get_processing_object() === null && !$this->grouped_parent_exists($pelement, $paths)) {
foreach($paths as $key => $element) {
if ($element->get_processing_object() === null && !$this->grouped_parent_exists($element, $paths)) {
$paths[$key]->set_processing_object($processingobject);
}
// Add element path to the processor
$this->xmlprocessor->add_path($element->get_path(), $element->is_grouped());
}
// Done, add them to pathelements (dupes by key - path - are discarded)
$this->pathelements = array_merge($this->pathelements, $paths);
Expand All @@ -119,7 +108,7 @@ protected function grouped_parent_exists($pelement, $elements) {
* Receive one chunk of information form the xml parser processor and
* dispatch it, following the naming rules
*/
final public function process($data) {
public function process($data) {
if (!array_key_exists($data['path'], $this->pathelements)) { // Incorrect path, must not happen
throw new restore_step_exception('restore_structure_step_missing_path', $data['path']); // @todo Change exception
}
Expand Down
84 changes: 84 additions & 0 deletions backup/util/factories/convert_factory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,88 @@ public static function converters($tempdir) {
}
return $converters;
}

// @todo DELETE part of prototype code
public static function activity_task(base_converter $converter, $name, array $data) {
global $CFG;

static $classmap = array();

$convertname = $converter->get_name();

if (!array_key_exists($convertname, $classmap)) {
$classmap[$convertname] = array();
}
if (!array_key_exists($name, $classmap[$convertname])) {
// @TODO include the class file and make sure class exists
$classfile = "$CFG->dirroot/mod/$name/backup/$convertname/convert_{$name}_activity_task.class.php";
$classname = "{$convertname}_{$name}_activity_task";

if (!class_exists($classname)) {
if (!file_exists($classfile)) {
throw new coding_exception("Conversion for $name for format $convertname not supported: class file not found $classfile");
}
require_once($classfile);

if (!class_exists($classname)) {
throw new coding_exception("Conversion for $name for format $convertname not supported: class not found $classname");
}
}
$classmap[$convertname][$name] = $classname;
}
$classname = $classmap[$convertname][$name];

return new $classname($name, $data);
}

/**
* Runs through all plugins of a specific type and instantiates
* their task class.
*
* @static
* @throws coding_exception
* @param string $type The plugin type
* @param string $format The convert format
* @param string $extra Extra naming structure
* @return array
*/
public static function get_plugin_tasks($type, $format, $extra = NULL) {
global $CFG; // REQUIRED by task file includes

if (is_null($extra)) {
$extra = $type;
}
$tasks = array();
$plugins = get_plugin_list($type);
foreach ($plugins as $name => $dir) {
$taskfile = "$dir/backup/$format/convert_{$name}_{$extra}_task.class.php";
$taskclass = "{$format}_{$name}_{$extra}_task";
if (!file_exists($taskfile)) {
continue;
}
require_once($taskfile);

if (!class_exists($taskclass)) {
throw new coding_exception("The class name should be $taskclass in $taskfile");
}
$tasks[] = new $taskclass("{$type}_$name");
}
return $tasks;
}

/**
* This will add all of the plugin tasks to the converter's plan
*
* @static
* @param plan_converter $converter The converter to add the plugin tasks to
* @param string $type The plugin type
* @param string $extra Extra naming structure
* @return void
*/
public static function build_plugin_tasks(plan_converter $converter, $type, $extra = NULL) {
$tasks = self::get_plugin_tasks($type, $converter->get_name(), $extra);
foreach ($tasks as $task) {
$converter->get_plan()->add_task($task);
}
}
}
17 changes: 17 additions & 0 deletions mod/forum/backup/moodle1/convert_forum_activity_task.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require_once($CFG->dirroot.'/mod/forum/backup/moodle1/convert_forum_stepslib.php');

/**
* Convert from Moodle 1 forum task
*/
class moodle1_forum_activity_task extends moodle1_activity_task {
/**
* Function responsible for building the steps of any task
* (must set the $built property to true)
*/
public function build() {
$this->add_step(new moodle1_forum_activity_structure_step('forum'));
$this->built = true;
}
}
27 changes: 27 additions & 0 deletions mod/forum/backup/moodle1/convert_forum_stepslib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Convert forum
*/
class moodle1_forum_activity_structure_step extends convert_structure_step {
/**
* Function that will return the structure to be processed by this convert_step.
* Must return one array of @convert_path_element elements
*
* NOTE: /MOD/ACTIVITYNAME XML path does not actually exist. The moodle1_converter
* class automatically transforms the /MOD path to include the activity name.
*/
protected function define_structure() {
return array(
new convert_path_element('forum', '/MOODLE_BACKUP/COURSE/MODULES/MOD/FORUM'),
// new convert_path_element('foo', '/MOODLE_BACKUP/COURSE/MODULES/MOD/FORUM/FOO'), // Example of sub-path
);
}

public function convert_forum($data) {
print_object($data);
}

public function convert_foo($data) {
print_object($data);
}
}

0 comments on commit 72c089f

Please sign in to comment.