Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Defines backup_resource_activity_task class
  20   *
  21   * @package     mod_resource
  22   * @category    backup
  23   * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  require_once($CFG->dirroot . '/mod/resource/backup/moodle2/backup_resource_stepslib.php');
  30  
  31  /**
  32   * Provides the steps to perform one complete backup of the Resource instance
  33   */
  34  class backup_resource_activity_task extends backup_activity_task {
  35  
  36      /**
  37       * @param bool $resourceoldexists True if there are records in the resource_old table.
  38       */
  39      protected static $resourceoldexists = null;
  40  
  41      /**
  42       * No specific settings for this activity
  43       */
  44      protected function define_my_settings() {
  45      }
  46  
  47      /**
  48       * Defines a backup step to store the instance data in the resource.xml file
  49       */
  50      protected function define_my_steps() {
  51          $this->add_step(new backup_resource_activity_structure_step('resource_structure', 'resource.xml'));
  52      }
  53  
  54      /**
  55       * Encodes URLs to the index.php and view.php scripts
  56       *
  57       * @param string $content some HTML text that eventually contains URLs to the activity instance scripts
  58       * @return string the content with the URLs encoded
  59       */
  60      static public function encode_content_links($content) {
  61          global $CFG, $DB;
  62  
  63          $base = preg_quote($CFG->wwwroot,"/");
  64  
  65          // Link to the list of resources.
  66          $search="/(".$base."\/mod\/resource\/index.php\?id\=)([0-9]+)/";
  67          $content= preg_replace($search, '$@RESOURCEINDEX*$2@$', $content);
  68  
  69          // Link to resource view by moduleid.
  70          $search = "/(".$base."\/mod\/resource\/view.php\?id\=)([0-9]+)/";
  71          // Link to resource view by recordid
  72          $search2 = "/(".$base."\/mod\/resource\/view.php\?r\=)([0-9]+)/";
  73  
  74          // Check whether there are contents in the resource old table.
  75          if (static::$resourceoldexists === null) {
  76              static::$resourceoldexists = $DB->record_exists('resource_old', array());
  77          }
  78  
  79          // If there are links to items in the resource_old table, rewrite them to be links to the correct URL
  80          // for their new module.
  81          if (static::$resourceoldexists) {
  82              // Match all of the resources.
  83              $result = preg_match_all($search, $content, $matches, PREG_PATTERN_ORDER);
  84  
  85              // Course module ID resource links.
  86              if ($result) {
  87                  list($insql, $params) = $DB->get_in_or_equal($matches[2]);
  88                  $oldrecs = $DB->get_records_select('resource_old', "cmid $insql", $params, '', 'cmid, newmodule');
  89  
  90                  for ($i = 0; $i < count($matches[0]); $i++) {
  91                      $cmid = $matches[2][$i];
  92                      if (isset($oldrecs[$cmid])) {
  93                          // Resource_old item, rewrite it
  94                          $replace = '$@' . strtoupper($oldrecs[$cmid]->newmodule) . 'VIEWBYID*' . $cmid . '@$';
  95                      } else {
  96                          // Not in the resource old table, don't rewrite
  97                          $replace = '$@RESOURCEVIEWBYID*'.$cmid.'@$';
  98                      }
  99                      $content = str_replace($matches[0][$i], $replace, $content);
 100                  }
 101              }
 102  
 103              $matches = null;
 104              $result = preg_match_all($search2, $content, $matches, PREG_PATTERN_ORDER);
 105  
 106              // No resource links.
 107              if (!$result) {
 108                  return $content;
 109              }
 110              // Resource ID links.
 111              list($insql, $params) = $DB->get_in_or_equal($matches[2]);
 112              $oldrecs = $DB->get_records_select('resource_old', "oldid $insql", $params, '', 'oldid, cmid, newmodule');
 113  
 114              for ($i = 0; $i < count($matches[0]); $i++) {
 115                  $recordid = $matches[2][$i];
 116                  if (isset($oldrecs[$recordid])) {
 117                      // Resource_old item, rewrite it
 118                      $replace = '$@' . strtoupper($oldrecs[$recordid]->newmodule) . 'VIEWBYID*' . $oldrecs[$recordid]->cmid . '@$';
 119                      $content = str_replace($matches[0][$i], $replace, $content);
 120                  }
 121              }
 122          } else {
 123              $content = preg_replace($search, '$@RESOURCEVIEWBYID*$2@$', $content);
 124          }
 125          return $content;
 126      }
 127  }