Skip to content

Commit

Permalink
Full example implementation of the convert course step
Browse files Browse the repository at this point in the history
  • Loading branch information
polothy authored and mudrd8mz committed May 10, 2011
1 parent 5c10f37 commit bb2e160
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 18 deletions.
98 changes: 94 additions & 4 deletions backup/converter/moodle1/converter.class.php
Expand Up @@ -43,26 +43,116 @@ 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;
}
}

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
}
}
}
1 change: 1 addition & 0 deletions backup/util/converter/plan_converter.class.php
Expand Up @@ -47,6 +47,7 @@ public function execute() {
$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() {
Expand Down
9 changes: 6 additions & 3 deletions backup/util/includes/convert_includes.php
Expand Up @@ -6,10 +6,13 @@
}

// 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');
Expand Down
10 changes: 10 additions & 0 deletions backup/util/plan/convert_plan.class.php
Expand Up @@ -47,4 +47,14 @@ public function build() {
$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();
}
}
}
4 changes: 4 additions & 0 deletions backup/util/plan/convert_step.class.php
Expand Up @@ -27,4 +27,8 @@ protected function get_converter() {
}
return $this->task->get_converter();
}

public function execute_after_convert() {
// Default nothing
}
}
11 changes: 0 additions & 11 deletions backup/util/plan/convert_structure_step.class.php
Expand Up @@ -24,17 +24,6 @@ public function get_task() {
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
*
Expand Down
8 changes: 8 additions & 0 deletions backup/util/plan/convert_task.class.php
Expand Up @@ -21,4 +21,12 @@ public function get_converter() {
protected function define_settings() {
// None
}

public function execute_after_convert() {
if ($this->executed) {
foreach ($this->steps as $step) {
$step->execute_after_convert();
}
}
}
}

0 comments on commit bb2e160

Please sign in to comment.