restore_rss_client_stepslib.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package block_rss_client
  18. * @subpackage backup-moodle2
  19. * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. /**
  23. * Define all the restore steps that wll be used by the restore_rss_client_block_task
  24. */
  25. /**
  26. * Define the complete rss_client structure for restore
  27. */
  28. class restore_rss_client_block_structure_step extends restore_structure_step {
  29. protected function define_structure() {
  30. $paths = array();
  31. $paths[] = new restore_path_element('block', '/block', true);
  32. $paths[] = new restore_path_element('rss_client', '/block/rss_client');
  33. $paths[] = new restore_path_element('feed', '/block/rss_client/feeds/feed');
  34. return $paths;
  35. }
  36. public function process_block($data) {
  37. global $DB;
  38. $data = (object)$data;
  39. $feedsarr = array(); // To accumulate feeds
  40. // For any reason (non multiple, dupe detected...) block not restored, return
  41. if (!$this->task->get_blockid()) {
  42. return;
  43. }
  44. // Iterate over all the feed elements, creating them if needed
  45. if (isset($data->rss_client['feeds']['feed'])) {
  46. foreach ($data->rss_client['feeds']['feed'] as $feed) {
  47. $feed = (object)$feed;
  48. // Look if the same feed is available by url and (shared or userid)
  49. $select = 'url = :url AND (shared = 1 OR userid = :userid)';
  50. $params = array('url' => $feed->url, 'userid' => $this->task->get_userid());
  51. // The feed already exists, use it
  52. if ($feedid = $DB->get_field_select('block_rss_client', 'id', $select, $params, IGNORE_MULTIPLE)) {
  53. $feedsarr[] = $feedid;
  54. // The feed doesn't exist, create it
  55. } else {
  56. $feed->userid = $this->task->get_userid();
  57. $feedid = $DB->insert_record('block_rss_client', $feed);
  58. $feedsarr[] = $feedid;
  59. }
  60. }
  61. }
  62. // Adjust the serialized configdata->rssid to the created/mapped feeds
  63. // Get the configdata
  64. $configdata = $DB->get_field('block_instances', 'configdata', array('id' => $this->task->get_blockid()));
  65. // Extract configdata
  66. $config = unserialize(base64_decode($configdata));
  67. if (empty($config)) {
  68. $config = new stdClass();
  69. }
  70. // Set array of used rss feeds
  71. $config->rssid = $feedsarr;
  72. // Serialize back the configdata
  73. $configdata = base64_encode(serialize($config));
  74. // Set the configdata back
  75. $DB->set_field('block_instances', 'configdata', $configdata, array('id' => $this->task->get_blockid()));
  76. }
  77. }