diff --git a/backup/converter/moodle1/converter.class.php b/backup/converter/moodle1/converter.class.php index 0e701241d615d..db3d9d74b6537 100644 --- a/backup/converter/moodle1/converter.class.php +++ b/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 + } } } \ No newline at end of file diff --git a/backup/util/converter/plan_converter.class.php b/backup/util/converter/plan_converter.class.php index 68474c6b0fdb4..d53cf389531c8 100644 --- a/backup/util/converter/plan_converter.class.php +++ b/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() { diff --git a/backup/util/includes/convert_includes.php b/backup/util/includes/convert_includes.php index 1f3475d8fe770..23c1a7c66e908 100644 --- a/backup/util/includes/convert_includes.php +++ b/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'); diff --git a/backup/util/plan/convert_plan.class.php b/backup/util/plan/convert_plan.class.php index d05d822acc06d..cd2d6b7a22b0f 100644 --- a/backup/util/plan/convert_plan.class.php +++ b/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(); + } + } } \ No newline at end of file diff --git a/backup/util/plan/convert_step.class.php b/backup/util/plan/convert_step.class.php index 1379e2ea3393b..86f064beb9a64 100644 --- a/backup/util/plan/convert_step.class.php +++ b/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 + } } \ No newline at end of file diff --git a/backup/util/plan/convert_structure_step.class.php b/backup/util/plan/convert_structure_step.class.php index 3ebd1bd4e675f..58ada9f6cb968 100644 --- a/backup/util/plan/convert_structure_step.class.php +++ b/backup/util/plan/convert_structure_step.class.php @@ -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 * diff --git a/backup/util/plan/convert_task.class.php b/backup/util/plan/convert_task.class.php index ccee3655a7177..650d04ecd8758 100644 --- a/backup/util/plan/convert_task.class.php +++ b/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(); + } + } + } } \ No newline at end of file