Full example implementation of the convert course step

This commit is contained in:
Mark Nielsen 2011-03-14 12:01:31 -07:00 committed by David Mudrak
parent 5c10f37b8a
commit bb2e1609c5
7 changed files with 123 additions and 18 deletions

View File

@ -43,7 +43,7 @@ class moodle1_course_task extends convert_task {
*/
public function build() {
$this->add_step(new moodle1_course_structure_step('course_info', $this));
$this->add_step(new moodle1_course_structure_step('course_info'));
// At the end, mark it as built
$this->built = true;
@ -51,18 +51,108 @@ class moodle1_course_task extends convert_task {
}
class moodle1_course_structure_step extends convert_structure_step {
protected $id;
/**
* @var xml_writer
*/
protected $xmlwriter;
protected $deprecated = array(
'roles_overrides',
'roles_assignments',
'cost',
'currancy',
'defaultrole',
'enrol',
'enrolenddate',
'enrollable',
'enrolperiod',
'enrolstartdate',
'expirynotify',
'expirythreshold',
'guest',
'notifystudents',
'password',
'student',
'students',
'teacher',
'teachers',
'metacourse',
);
/**
* 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('courseinfo', '/MOODLE_BACKUP/COURSE/HEADER');
$paths[] = new convert_path_element('course', '/MOODLE_BACKUP/COURSE/HEADER');
$paths[] = new convert_path_element('category', '/MOODLE_BACKUP/COURSE/HEADER/CATEGORY');
return $paths;
}
public function convert_courseinfo($data) {
print_object($data);
public function open_writer() {
if (!$this->xmlwriter instanceof xml_writer) {
if (empty($this->id)) {
throw new backup_exception('noidfound'); // @todo define string or dynamically make id
}
$directory = $this->get_converter()->get_convertdir().'/course';
if (!check_dir_exists($directory)) {
throw new backup_exception('failedtomakeconvertdir'); // @todo Define this string
}
$this->xmlwriter = new xml_writer(
new file_xml_output($directory.'/course.xml')
);
$this->xmlwriter->start();
$this->xmlwriter->begin_tag('course', array('id' => $this->id, 'contextid' => 'TODO')); // @todo make contextid
}
}
/**
* This is actually called twice because category is defined
* right after ID in the XML... any way around that? Only
* idea is to patch Moodle 1.9
*
* @throws backup_exception
* @param $data
* @return void
*/
public function convert_course($data) {
// print_object($data); // DEBUG
if (array_key_exists('ID', $data)) {
$this->id = $data['ID'];
unset($data['ID']);
}
if (empty($data)) {
return;
}
$this->open_writer();
foreach ($data as $name => $value) {
$name = strtolower($name);
if (in_array($name, $this->deprecated)) {
continue;
}
$this->xmlwriter->full_tag($name, $value);
}
}
public function convert_category($data) {
// print_object($data); // DEBUG
$this->open_writer();
$this->xmlwriter->begin_tag('category', array('id' => $data['ID']));
$this->xmlwriter->full_tag('name', $data['NAME']);
$this->xmlwriter->end_tag('category');
}
public function execute_after_convert() {
if ($this->xmlwriter instanceof xml_writer) {
$this->xmlwriter->end_tag('course');
$this->xmlwriter->stop();
unset($this->xmlwriter);
// var_dump(file_get_contents($this->get_converter()->get_convertdir().'/course/course.xml')); // DEBUG
}
}
}

View File

@ -47,6 +47,7 @@ abstract class plan_converter extends base_converter {
$this->get_plan()->build(); // Ends up calling $this->build_plan()
$this->get_plan()->execute();
$this->xmlparser->process(); // @todo When to really do this?
$this->get_plan()->execute_after_convert();
}
public function destroy() {

View File

@ -6,10 +6,13 @@ if (!defined('MOODLE_INTERNAL')) {
}
// Include all the convert stuff needed
require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php');
require_once($CFG->dirroot . '/backup/util/interfaces/executable.class.php');
require_once($CFG->dirroot . '/backup/util/interfaces/loggable.class.php');
require_once($CFG->dirroot.'/backup/util/interfaces/checksumable.class.php');
require_once($CFG->dirroot.'/backup/util/interfaces/executable.class.php');
require_once($CFG->dirroot.'/backup/util/interfaces/loggable.class.php');
require_once($CFG->dirroot.'/backup/backup.class.php');
require_once($CFG->dirroot.'/backup/util/xml/xml_writer.class.php');
require_once($CFG->dirroot.'/backup/util/xml/output/xml_output.class.php');
require_once($CFG->dirroot.'/backup/util/xml/output/file_xml_output.class.php');
require_once($CFG->dirroot.'/backup/util/factories/convert_factory.class.php');
require_once($CFG->dirroot.'/backup/util/converter/base_converter.class.php');
require_once($CFG->dirroot.'/backup/util/converter/plan_converter.class.php');

View File

@ -47,4 +47,14 @@ class convert_plan extends base_plan implements loggable {
$this->converter->build_plan();
$this->built = true;
}
/**
* Execute the after_restore methods of all the executed tasks in the plan
*/
public function execute_after_convert() {
// Simply iterate over each task in the plan and delegate to them the execution
foreach ($this->tasks as $task) {
$task->execute_after_convert();
}
}
}

View File

@ -27,4 +27,8 @@ abstract class convert_step extends base_step {
}
return $this->task->get_converter();
}
public function execute_after_convert() {
// Default nothing
}
}

View File

@ -24,17 +24,6 @@ abstract class convert_structure_step extends convert_step {
return $this->task;
}
/**
* This method will be executed after the whole structure step have been processed
*
* After execution method for code needed to be executed after the whole structure
* has been processed. Useful for cleaning tasks, files process and others. Simply
* overwrite in in your steps if needed
*/
protected function after_execute() {
// do nothing by default
}
/**
* To conditionally decide if one step will be executed or no
*

View File

@ -21,4 +21,12 @@ abstract class convert_task extends base_task {
protected function define_settings() {
// None
}
public function execute_after_convert() {
if ($this->executed) {
foreach ($this->steps as $step) {
$step->execute_after_convert();
}
}
}
}