Skip to content

Commit

Permalink
Creating stepslib.php and taskslib.php
Browse files Browse the repository at this point in the history
  • Loading branch information
polothy authored and mudrd8mz committed May 10, 2011
1 parent 56bd1ab commit be14592
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 152 deletions.
158 changes: 6 additions & 152 deletions backup/converter/moodle1/converter.class.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* All of the task and step classes specific to moodle1 conversion
*/
require_once($CFG->dirroot.'/backup/converter/moodle1/taskslib.php');
require_once($CFG->dirroot.'/backup/converter/moodle1/stepslib.php');

/**
* This will be the Moodle 1 to Moodle 2 Converter
*/
Expand Down Expand Up @@ -45,155 +51,3 @@ public function destroy() {


}

// @todo Where to store these classes?
class moodle1_root_task extends convert_task {
/**
* Function responsible for building the steps of any task
* (must set the $built property to true)
*/
public function build() {
$this->add_step(new convert_create_and_clean_temp_stuff('create_and_clean_temp_stuff'));

// At the end, mark it as built
$this->built = true;
}

}

class moodle1_final_task extends convert_task {
/**
* Function responsible for building the steps of any task
* (must set the $built property to true)
*/
public function build() {
$this->add_step(new convert_drop_and_clean_temp_stuff('drop_and_clean_temp_stuff'));

// At the end, mark it as built
$this->built = true;
}
}

class moodle1_course_task extends convert_task {
/**
* Create all the steps that will be part of this task
*/
public function build() {

$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('course', '/MOODLE_BACKUP/COURSE/HEADER');
$paths[] = new convert_path_element('category', '/MOODLE_BACKUP/COURSE/HEADER/CATEGORY');

return $paths;
}

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' => convert_helper::get_contextid($this->id, 'course')
));
}
}

/**
* 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
}
}
}
110 changes: 110 additions & 0 deletions backup/converter/moodle1/stepslib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
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('course', '/MOODLE_BACKUP/COURSE/HEADER');
$paths[] = new convert_path_element('category', '/MOODLE_BACKUP/COURSE/HEADER/CATEGORY');

return $paths;
}

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' => convert_helper::get_contextid($this->id, 'course')
));
}
}

/**
* 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
}
}
}
44 changes: 44 additions & 0 deletions backup/converter/moodle1/taskslib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
class moodle1_root_task extends convert_task {
/**
* Function responsible for building the steps of any task
* (must set the $built property to true)
*/
public function build() {
$this->add_step(new convert_create_and_clean_temp_stuff('create_and_clean_temp_stuff'));

// At the end, mark it as built
$this->built = true;
}

}

/**
* @todo Not used at the moment
*/
class moodle1_final_task extends convert_task {
/**
* Function responsible for building the steps of any task
* (must set the $built property to true)
*/
public function build() {
$this->add_step(new convert_drop_and_clean_temp_stuff('drop_and_clean_temp_stuff'));

// At the end, mark it as built
$this->built = true;
}
}

class moodle1_course_task extends convert_task {
/**
* Create all the steps that will be part of this task
*/
public function build() {

$this->add_step(new moodle1_course_structure_step('course_info'));

// At the end, mark it as built
$this->built = true;
}
}

0 comments on commit be14592

Please sign in to comment.