Merge branch 'MDL-32880_23_3' of git://github.com/pauln/moodle into MOODLE_23_STABLE
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 11 Dec 2012 21:50:08 +0000 (22:50 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 11 Dec 2012 21:50:08 +0000 (22:50 +0100)
backup/converter/moodle1/handlerlib.php
backup/converter/moodle1/lib.php
blocks/html/backup/moodle1/lib.php [new file with mode: 0644]
blocks/rss_client/backup/moodle1/lib.php [new file with mode: 0644]

index 51cad64..a2744c2 100644 (file)
@@ -88,10 +88,13 @@ abstract class moodle1_handlers_factory {
         foreach ($plugins as $name => $dir) {
             $handlerfile  = $dir . '/backup/moodle1/lib.php';
             $handlerclass = "moodle1_{$type}_{$name}_handler";
-            if (!file_exists($handlerfile)) {
+            if (file_exists($handlerfile)) {
+                require_once($handlerfile);
+            } elseif ($type == 'block') {
+                $handlerclass = "moodle1_block_generic_handler";
+            } else {
                 continue;
             }
-            require_once($handlerfile);
 
             if (!class_exists($handlerclass)) {
                 throw new moodle1_convert_exception('missing_handler_class', $handlerclass);
@@ -1989,9 +1992,93 @@ abstract class moodle1_resource_successor_handler extends moodle1_mod_handler {
  */
 abstract class moodle1_block_handler extends moodle1_plugin_handler {
 
+    public function get_paths() {
+        $blockname = strtoupper($this->pluginname);
+        return array(
+            new convert_path('block', "/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/{$blockname}"),
+        );
+    }
+
+    public function process_block(array $data) {
+        $newdata = $this->convert_common_block_data($data);
+
+        $this->write_block_xml($newdata, $data);
+        $this->write_inforef_xml($newdata, $data);
+        $this->write_roles_xml($newdata, $data);
+
+        return $data;
+    }
+
+    protected function convert_common_block_data(array $olddata) {
+        $newdata = array();
+
+        $newdata['blockname'] = $olddata['name'];
+        $newdata['parentcontextid'] = $this->converter->get_contextid(CONTEXT_COURSE, 0);
+        $newdata['showinsubcontexts'] = 0;
+        $newdata['pagetypepattern'] = $olddata['pagetype'].='-*';
+        $newdata['subpagepattern'] = null;
+        $newdata['defaultregion'] = ($olddata['position']=='l')?'side-pre':'side-post';
+        $newdata['defaultweight'] = $olddata['weight'];
+        $newdata['configdata'] = $this->convert_configdata($olddata);
+
+        return $newdata;
+    }
+
+    protected function convert_configdata(array $olddata) {
+        return $olddata['configdata'];
+    }
+
+    protected function write_block_xml($newdata, $data) {
+        $contextid = $this->converter->get_contextid(CONTEXT_BLOCK, $data['id']);
+
+        $this->open_xml_writer("course/blocks/{$data['name']}_{$data['id']}/block.xml");
+        $this->xmlwriter->begin_tag('block', array('id' => $data['id'], 'contextid' => $contextid));
+
+        foreach ($newdata as $field => $value) {
+            $this->xmlwriter->full_tag($field, $value);
+        }
+
+        $this->xmlwriter->begin_tag('block_positions');
+        $this->xmlwriter->begin_tag('block_position', array('id' => 1));
+        $this->xmlwriter->full_tag('contextid', $newdata['parentcontextid']);
+        $this->xmlwriter->full_tag('pagetype', $data['pagetype']);
+        $this->xmlwriter->full_tag('subpage', '');
+        $this->xmlwriter->full_tag('visible', $data['visible']);
+        $this->xmlwriter->full_tag('region', $newdata['defaultregion']);
+        $this->xmlwriter->full_tag('weight', $newdata['defaultweight']);
+        $this->xmlwriter->end_tag('block_position');
+        $this->xmlwriter->end_tag('block_positions');
+        $this->xmlwriter->end_tag('block');
+        $this->close_xml_writer();
+    }
+
+    protected function write_inforef_xml($newdata, $data) {
+        $this->open_xml_writer("course/blocks/{$data['name']}_{$data['id']}/inforef.xml");
+        $this->xmlwriter->begin_tag('inforef');
+        // Subclasses may provide inforef contents if needed
+        $this->xmlwriter->end_tag('inforef');
+        $this->close_xml_writer();
+    }
+
+    protected function write_roles_xml($newdata, $data) {
+        // This is an empty shell, as the moodle1 converter doesn't handle user data.
+        $this->open_xml_writer("course/blocks/{$data['name']}_{$data['id']}/roles.xml");
+        $this->xmlwriter->begin_tag('roles');
+        $this->xmlwriter->full_tag('role_overrides', '');
+        $this->xmlwriter->full_tag('role_assignments', '');
+        $this->xmlwriter->end_tag('roles');
+        $this->close_xml_writer();
+    }
 }
 
 
+/**
+ * Base class for block generic handler
+ */
+class moodle1_block_generic_handler extends moodle1_block_handler {
+
+}
+
 /**
  * Base class for the activity modules' subplugins
  */
index bdfb47c..9edda48 100644 (file)
@@ -254,7 +254,7 @@ class moodle1_converter extends base_converter {
             $path = '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentblock;
 
         } else if (strpos($path, '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') === 0) {
-            $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentmod, $path);
+            $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentblock, $path);
         }
 
         if ($path !== $data['path']) {
@@ -350,12 +350,12 @@ class moodle1_converter extends base_converter {
         }
 
         if ($path === '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') {
-            $this->currentmod = null;
+            $this->currentblock = null;
             $forbidden = true;
 
         } else if (strpos($path, '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') === 0) {
             // expand the BLOCK paths so that they contain the module name
-            $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentmod, $path);
+            $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentblock, $path);
         }
 
         if (empty($this->pathelements[$path])) {
@@ -397,7 +397,7 @@ class moodle1_converter extends base_converter {
             $path = '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentblock;
 
         } else if (strpos($path, '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') === 0) {
-            $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentmod, $path);
+            $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentblock, $path);
         }
 
         if (empty($this->pathelements[$path])) {
diff --git a/blocks/html/backup/moodle1/lib.php b/blocks/html/backup/moodle1/lib.php
new file mode 100644 (file)
index 0000000..d4a491f
--- /dev/null
@@ -0,0 +1,46 @@
+<?php\r
+\r
+/**\r
+ * Provides support for the conversion of moodle1 backup to the moodle2 format\r
+ *\r
+ * @package    block_html\r
+ * @copyright  2012 Paul Nicholls\r
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\r
+ */\r
+\r
+defined('MOODLE_INTERNAL') || die();\r
+\r
+/**\r
+ * Block conversion handler for html\r
+ */\r
+class moodle1_block_html_handler extends moodle1_block_handler {\r
+    private $fileman = null;\r
+    protected function convert_configdata(array $olddata) {\r
+        $instanceid = $olddata['id'];\r
+        $contextid  = $this->converter->get_contextid(CONTEXT_BLOCK, $olddata['id']);\r
+        $configdata = unserialize(base64_decode($olddata['configdata']));\r
+\r
+        // get a fresh new file manager for this instance\r
+        $this->fileman = $this->converter->get_file_manager($contextid, 'block_html');\r
+\r
+        // convert course files embedded in the block content\r
+        $this->fileman->filearea = 'content';\r
+        $this->fileman->itemid   = 0;\r
+        $configdata->text = moodle1_converter::migrate_referenced_files($configdata->text, $this->fileman);\r
+        $configdata->format = FORMAT_HTML;\r
+\r
+        return base64_encode(serialize($configdata));\r
+    }\r
+\r
+    protected function write_inforef_xml($newdata, $data) {\r
+        $this->open_xml_writer("course/blocks/{$data['name']}_{$data['id']}/inforef.xml");\r
+        $this->xmlwriter->begin_tag('inforef');\r
+        $this->xmlwriter->begin_tag('fileref');\r
+        foreach ($this->fileman->get_fileids() as $fileid) {\r
+            $this->write_xml('file', array('id' => $fileid));\r
+        }\r
+        $this->xmlwriter->end_tag('fileref');\r
+        $this->xmlwriter->end_tag('inforef');\r
+        $this->close_xml_writer();\r
+    }\r
+}
\ No newline at end of file
diff --git a/blocks/rss_client/backup/moodle1/lib.php b/blocks/rss_client/backup/moodle1/lib.php
new file mode 100644 (file)
index 0000000..95da634
--- /dev/null
@@ -0,0 +1,34 @@
+<?php\r
+\r
+/**\r
+ * Provides support for the conversion of moodle1 backup to the moodle2 format\r
+ *\r
+ * @package    block_rss_client\r
+ * @copyright  2012 Paul Nicholls\r
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\r
+ */\r
+\r
+defined('MOODLE_INTERNAL') || die();\r
+\r
+/**\r
+ * Block conversion handler for rss_client\r
+ */\r
+class moodle1_block_rss_client_handler extends moodle1_block_handler {\r
+    public function process_block(array $data) {\r
+        parent::process_block($data);\r
+        $instanceid = $data['id'];\r
+        $contextid = $this->converter->get_contextid(CONTEXT_BLOCK, $data['id']);\r
+\r
+        // Moodle 1.9 backups do not include sufficient data to restore feeds, so we need an empty shell rss_client.xml\r
+        // for the restore process to find\r
+        $this->open_xml_writer("course/blocks/{$data['name']}_{$instanceid}/rss_client.xml");\r
+        $this->xmlwriter->begin_tag('block', array('id' => $instanceid, 'contextid' => $contextid, 'blockname' => 'rss_client'));\r
+        $this->xmlwriter->begin_tag('rss_client', array('id' => $instanceid));\r
+        $this->xmlwriter->full_tag('feeds', '');\r
+        $this->xmlwriter->end_tag('rss_client');\r
+        $this->xmlwriter->end_tag('block');\r
+        $this->close_xml_writer();\r
+\r
+        return $data;\r
+    }\r
+}
\ No newline at end of file