|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00032 class restore_rss_client_block_structure_step extends restore_structure_step { 00033 00034 protected function define_structure() { 00035 00036 $paths = array(); 00037 00038 $paths[] = new restore_path_element('block', '/block', true); 00039 $paths[] = new restore_path_element('rss_client', '/block/rss_client'); 00040 $paths[] = new restore_path_element('feed', '/block/rss_client/feeds/feed'); 00041 00042 return $paths; 00043 } 00044 00045 public function process_block($data) { 00046 global $DB; 00047 00048 $data = (object)$data; 00049 $feedsarr = array(); // To accumulate feeds 00050 00051 // For any reason (non multiple, dupe detected...) block not restored, return 00052 if (!$this->task->get_blockid()) { 00053 return; 00054 } 00055 00056 // Iterate over all the feed elements, creating them if needed 00057 if (isset($data->rss_client['feeds']['feed'])) { 00058 foreach ($data->rss_client['feeds']['feed'] as $feed) { 00059 $feed = (object)$feed; 00060 // Look if the same feed is available by url and (shared or userid) 00061 $select = 'url = :url AND (shared = 1 OR userid = :userid)'; 00062 $params = array('url' => $feed->url, 'userid' => $this->task->get_userid()); 00063 // The feed already exists, use it 00064 if ($feedid = $DB->get_field_select('block_rss_client', 'id', $select, $params, IGNORE_MULTIPLE)) { 00065 $feedsarr[] = $feedid; 00066 00067 // The feed doesn't exist, create it 00068 } else { 00069 $feed->userid = $this->task->get_userid(); 00070 $feedid = $DB->insert_record('block_rss_client', $feed); 00071 $feedsarr[] = $feedid; 00072 } 00073 } 00074 } 00075 00076 // Adjust the serialized configdata->rssid to the created/mapped feeds 00077 // Get the configdata 00078 $configdata = $DB->get_field('block_instances', 'configdata', array('id' => $this->task->get_blockid())); 00079 // Extract configdata 00080 $config = unserialize(base64_decode($configdata)); 00081 // Set array of used rss feeds 00082 $config->rssid = $feedsarr; 00083 // Serialize back the configdata 00084 $configdata = base64_encode(serialize($config)); 00085 // Set the configdata back 00086 $DB->set_field('block_instances', 'configdata', $configdata, array('id' => $this->task->get_blockid())); 00087 } 00088 }