PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/resource/backup/moodle2/backup_resource_activity_task.class.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 127 lines | 55 code | 19 blank | 53 comment | 11 complexity | a6c045e1eeb8350597606e1ccc0e4fea MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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. * Defines backup_resource_activity_task class
  18. *
  19. * @package mod_resource
  20. * @category backup
  21. * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die;
  25. require_once($CFG->dirroot . '/mod/resource/backup/moodle2/backup_resource_stepslib.php');
  26. /**
  27. * Provides the steps to perform one complete backup of the Resource instance
  28. */
  29. class backup_resource_activity_task extends backup_activity_task {
  30. /**
  31. * @param bool $resourceoldexists True if there are records in the resource_old table.
  32. */
  33. protected static $resourceoldexists = null;
  34. /**
  35. * No specific settings for this activity
  36. */
  37. protected function define_my_settings() {
  38. }
  39. /**
  40. * Defines a backup step to store the instance data in the resource.xml file
  41. */
  42. protected function define_my_steps() {
  43. $this->add_step(new backup_resource_activity_structure_step('resource_structure', 'resource.xml'));
  44. }
  45. /**
  46. * Encodes URLs to the index.php and view.php scripts
  47. *
  48. * @param string $content some HTML text that eventually contains URLs to the activity instance scripts
  49. * @return string the content with the URLs encoded
  50. */
  51. static public function encode_content_links($content) {
  52. global $CFG, $DB;
  53. $base = preg_quote($CFG->wwwroot,"/");
  54. // Link to the list of resources.
  55. $search="/(".$base."\/mod\/resource\/index.php\?id\=)([0-9]+)/";
  56. $content= preg_replace($search, '$@RESOURCEINDEX*$2@$', $content);
  57. // Link to resource view by moduleid.
  58. $search = "/(".$base."\/mod\/resource\/view.php\?id\=)([0-9]+)/";
  59. // Link to resource view by recordid
  60. $search2 = "/(".$base."\/mod\/resource\/view.php\?r\=)([0-9]+)/";
  61. // Check whether there are contents in the resource old table.
  62. if (static::$resourceoldexists === null) {
  63. static::$resourceoldexists = $DB->record_exists('resource_old', array());
  64. }
  65. // If there are links to items in the resource_old table, rewrite them to be links to the correct URL
  66. // for their new module.
  67. if (static::$resourceoldexists) {
  68. // Match all of the resources.
  69. $result = preg_match_all($search, $content, $matches, PREG_PATTERN_ORDER);
  70. // Course module ID resource links.
  71. if ($result) {
  72. list($insql, $params) = $DB->get_in_or_equal($matches[2]);
  73. $oldrecs = $DB->get_records_select('resource_old', "cmid $insql", $params, '', 'cmid, newmodule');
  74. for ($i = 0; $i < count($matches[0]); $i++) {
  75. $cmid = $matches[2][$i];
  76. if (isset($oldrecs[$cmid])) {
  77. // Resource_old item, rewrite it
  78. $replace = '$@' . strtoupper($oldrecs[$cmid]->newmodule) . 'VIEWBYID*' . $cmid . '@$';
  79. } else {
  80. // Not in the resource old table, don't rewrite
  81. $replace = '$@RESOURCEVIEWBYID*'.$cmid.'@$';
  82. }
  83. $content = str_replace($matches[0][$i], $replace, $content);
  84. }
  85. }
  86. $matches = null;
  87. $result = preg_match_all($search2, $content, $matches, PREG_PATTERN_ORDER);
  88. // No resource links.
  89. if (!$result) {
  90. return $content;
  91. }
  92. // Resource ID links.
  93. list($insql, $params) = $DB->get_in_or_equal($matches[2]);
  94. $oldrecs = $DB->get_records_select('resource_old', "oldid $insql", $params, '', 'oldid, cmid, newmodule');
  95. for ($i = 0; $i < count($matches[0]); $i++) {
  96. $recordid = $matches[2][$i];
  97. if (isset($oldrecs[$recordid])) {
  98. // Resource_old item, rewrite it
  99. $replace = '$@' . strtoupper($oldrecs[$recordid]->newmodule) . 'VIEWBYID*' . $oldrecs[$recordid]->cmid . '@$';
  100. $content = str_replace($matches[0][$i], $replace, $content);
  101. }
  102. }
  103. } else {
  104. $content = preg_replace($search, '$@RESOURCEVIEWBYID*$2@$', $content);
  105. }
  106. return $content;
  107. }
  108. }