Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Full example implementation of the convert course step

  • Loading branch information...
commit bb2e1609c5ff30e920e6ce3907323d80ff73fddb 1 parent 5c10f37
@mrmark mrmark authored mudrd8mz committed
View
98 backup/converter/moodle1/converter.class.php
@@ -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 @@ public function build() {
}
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
1  backup/util/converter/plan_converter.class.php
@@ -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() {
View
9 backup/util/includes/convert_includes.php
@@ -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');
View
10 backup/util/plan/convert_plan.class.php
@@ -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();
+ }
+ }
}
View
4 backup/util/plan/convert_step.class.php
@@ -27,4 +27,8 @@ protected function get_converter() {
}
return $this->task->get_converter();
}
+
+ public function execute_after_convert() {
+ // Default nothing
+ }
}
View
11 backup/util/plan/convert_structure_step.class.php
@@ -25,17 +25,6 @@ public function get_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
*
* For steps needing to be executed conditionally, based in dynamic
View
8 backup/util/plan/convert_task.class.php
@@ -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();
+ }
+ }
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.