MDL-23478 backup - rss_client block restore supporting own structures
authorEloy Lafuente <stronk7@moodle.org>
Sat, 24 Jul 2010 23:05:44 +0000 (23:05 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Sat, 24 Jul 2010 23:05:44 +0000 (23:05 +0000)
blocks/rss_client/backup/moodle2/backup_rss_client_block_task.class.php
blocks/rss_client/backup/moodle2/restore_rss_client_block_task.class.php [new file with mode: 0644]
blocks/rss_client/backup/moodle2/restore_rss_client_stepslib.php [new file with mode: 0644]

index 41d1dd6..45a4df2 100644 (file)
@@ -31,8 +31,6 @@ require_once($CFG->dirroot . '/blocks/rss_client/backup/moodle2/backup_rss_clien
  * TODO: Finish phpdocs
  */
 class backup_rss_client_block_task extends backup_block_task {
-    // Nothing to do, it's just the backup_block_task in action
-    // with required methods doing nothing special
 
     protected function define_my_settings() {
     }
diff --git a/blocks/rss_client/backup/moodle2/restore_rss_client_block_task.class.php b/blocks/rss_client/backup/moodle2/restore_rss_client_block_task.class.php
new file mode 100644 (file)
index 0000000..9d72d6d
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-moodle2
+ * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once($CFG->dirroot . '/blocks/rss_client/backup/moodle2/restore_rss_client_stepslib.php'); // We have structure steps
+
+/**
+ * Specialised restore task for the rss_client block
+ * (has own DB structures to backup)
+ *
+ * TODO: Finish phpdocs
+ */
+class restore_rss_client_block_task extends restore_block_task {
+
+    protected function define_my_settings() {
+    }
+
+    protected function define_my_steps() {
+        // rss_client has one structure step
+        $this->add_step(new restore_rss_client_block_structure_step('rss_client_structure', 'rss_client.xml'));
+    }
+
+    public function get_fileareas() {
+        return array(); // No associated fileareas
+    }
+
+    public function get_configdata_encoded_attributes() {
+        return array(); // No special handling of configdata
+    }
+
+    static public function decode_content_links($content) {
+        return $content; // No special encoding of links
+    }
+}
+
diff --git a/blocks/rss_client/backup/moodle2/restore_rss_client_stepslib.php b/blocks/rss_client/backup/moodle2/restore_rss_client_stepslib.php
new file mode 100644 (file)
index 0000000..8e28ac2
--- /dev/null
@@ -0,0 +1,88 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-moodle2
+ * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Define all the restore steps that wll be used by the restore_rss_client_block_task
+ */
+
+/**
+ * Define the complete rss_client  structure for restore
+ */
+class restore_rss_client_block_structure_step extends restore_structure_step {
+
+    protected function define_structure() {
+
+        $paths = array();
+
+        $paths[] = new restore_path_element('block', '/block', true);
+        $paths[] = new restore_path_element('rss_client', '/block/rss_client');
+        $paths[] = new restore_path_element('feed', '/block/rss_client/feeds/feed');
+
+        return $paths;
+    }
+
+    public function process_block($data) {
+        global $DB;
+
+        $data = (object)$data;
+        $feedsarr = array(); // To accumulate feeds
+
+        // For any reason (non multiple, dupe detected...) block not restored, return
+        if (!$this->task->get_blockid()) {
+            return;
+        }
+
+        // Iterate over all the feed elements, creating them if needed
+        if (isset($data->rss_client['feeds']['feed'])) {
+            foreach ($data->rss_client['feeds']['feed'] as $feed) {
+                $feed = (object)$feed;
+                // Look if the same feed is available by url and (shared or userid)
+                $select = 'url = :url AND (shared = 1 OR userid = :userid)';
+                $params = array('url' => $feed->url, 'userid' => $this->task->get_userid());
+                // The feed already exists, use it
+                if ($feedid = $DB->get_field_select('block_rss_client', 'id', $select, $params, IGNORE_MULTIPLE)) {
+                    $feedsarr[] = $feedid;
+
+                // The feed doesn't exist, create it
+                } else {
+                    $feed->userid = $this->task->get_userid();
+                    $feedid = $DB->insert_record('block_rss_client', $feed);
+                    $feedsarr[] = $feedid;
+                }
+            }
+        }
+
+        // Adjust the serialized configdata->rssid to the created/mapped feeds
+        // Get the configdata
+        $configdata = $DB->get_field('block_instances', 'configdata', array('id' => $this->task->get_blockid()));
+        // Extract configdata
+        $config = unserialize(base64_decode($configdata));
+        // Set array of used rss feeds
+        $config->rssid = $feedsarr;
+        // Serialize back the configdata
+        $configdata = base64_encode(serialize($config));
+        // Set the configdata back
+        $DB->set_field('block_instances', 'configdata', $configdata, array('id' => $this->task->get_blockid()));
+    }
+}