Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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  // 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  /**
  18   * Provides support for the conversion of moodle1 backup to the moodle2 format
  19   *
  20   * @package mod_imscp
  21   * @copyright  2011 Andrew Davis <andrew@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * imscp conversion handler. This resource handler is called by moodle1_mod_resource_handler
  29   *
  30   * @copyright  2011 Andrew Davis <andrew@moodle.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class moodle1_mod_imscp_handler extends moodle1_resource_successor_handler {
  34  
  35      /** @var moodle1_file_manager the file manager instance */
  36      protected $fileman = null;
  37  
  38      /**
  39       * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
  40       * Called by moodle1_mod_resource_handler::process_resource()
  41       */
  42      public function process_legacy_resource(array $data, array $raw = null) {
  43  
  44          $instanceid    = $data['id'];
  45          $currentcminfo = $this->get_cminfo($instanceid);
  46          $moduleid      = $currentcminfo['id'];
  47          $contextid     = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
  48  
  49          // Prepare the new imscp instance record.
  50          $imscp                  = array();
  51          $imscp['id']            = $data['id'];
  52          $imscp['name']          = $data['name'];
  53          $imscp['intro']         = $data['intro'];
  54          $imscp['introformat']   = $data['introformat'];
  55          $imscp['revision']      = 1;
  56          $imscp['keepold']       = 1;
  57          $imscp['structure']     = null;
  58          $imscp['timemodified']  = $data['timemodified'];
  59  
  60          // Prepare a fresh new file manager for this instance.
  61          $this->fileman = $this->converter->get_file_manager($contextid, 'mod_imscp');
  62  
  63          // Convert course files embedded into the intro.
  64          $this->fileman->filearea = 'intro';
  65          $this->fileman->itemid   = 0;
  66          $imscp['intro'] = moodle1_converter::migrate_referenced_files($imscp['intro'], $this->fileman);
  67  
  68          // Migrate package backup file.
  69          if ($data['reference']) {
  70              $packagename = basename($data['reference']);
  71              $packagepath = $this->converter->get_tempdir_path().'/moddata/resource/'.$data['id'].'/'.$packagename;
  72              if (file_exists($packagepath)) {
  73                  $this->fileman->filearea = 'backup';
  74                  $this->fileman->itemid   = 1;
  75                  $this->fileman->migrate_file('moddata/resource/'.$data['id'].'/'.$packagename);
  76              } else {
  77                  $this->log('missing imscp package', backup::LOG_WARNING, 'moddata/resource/'.$data['id'].'/'.$packagename);
  78              }
  79          }
  80  
  81          // Migrate extracted package data.
  82          $this->fileman->filearea = 'content';
  83          $this->fileman->itemid   = 1;
  84          $this->fileman->migrate_directory('moddata/resource/'.$data['id']);
  85  
  86          // Parse manifest.
  87          $structure = $this->parse_structure($this->converter->get_tempdir_path().
  88                      '/moddata/resource/'.$data['id'].'/imsmanifest.xml', $imscp, $contextid);
  89          $imscp['structure'] = is_array($structure) ? serialize($structure) : null;
  90  
  91          // Write imscp.xml.
  92          $this->open_xml_writer("activities/imscp_{$moduleid}/imscp.xml");
  93          $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
  94              'modulename' => 'imscp', 'contextid' => $contextid));
  95          $this->write_xml('imscp', $imscp, array('/imscp/id'));
  96          $this->xmlwriter->end_tag('activity');
  97          $this->close_xml_writer();
  98  
  99          // Write inforef.xml.
 100          $this->open_xml_writer("activities/imscp_{$moduleid}/inforef.xml");
 101          $this->xmlwriter->begin_tag('inforef');
 102          $this->xmlwriter->begin_tag('fileref');
 103          foreach ($this->fileman->get_fileids() as $fileid) {
 104              $this->write_xml('file', array('id' => $fileid));
 105          }
 106          $this->xmlwriter->end_tag('fileref');
 107          $this->xmlwriter->end_tag('inforef');
 108          $this->close_xml_writer();
 109      }
 110  
 111      // Internal implementation details follow.
 112  
 113      /**
 114       * Parse the IMS package structure for the $imscp->structure field
 115       *
 116       * @param string $manifestfilepath the full path to the manifest file to parse
 117       */
 118      protected function parse_structure($manifestfilepath, $imscp, $context) {
 119          global $CFG;
 120  
 121          if (!file_exists($manifestfilepath)) {
 122              $this->log('missing imscp manifest file', backup::LOG_WARNING);
 123              return null;
 124          }
 125          $manifestfilecontents = file_get_contents($manifestfilepath);
 126          if (empty($manifestfilecontents)) {
 127              $this->log('empty imscp manifest file', backup::LOG_WARNING);
 128              return null;
 129          }
 130  
 131          require_once($CFG->dirroot.'/mod/imscp/locallib.php');
 132          return imscp_parse_manifestfile($manifestfilecontents, $imscp, $context);
 133      }
 134  }