Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-27448 resource conversion support to restore 1.9 backup to 2.1

  • Loading branch information...
commit 5da3af58a0b80b9f4d716ff9dd59ed478e98d24d 1 parent c818e2d
Andrew Davis andyjdavis authored mudrd8mz committed
91 mod/folder/backup/moodle1/lib.php
... ... @@ -0,0 +1,91 @@
  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 + * Provides support for the conversion of moodle1 backup to the moodle2 format
  20 + *
  21 + * @package mod
  22 + * @subpackage folder
  23 + * @copyright 2011 Andrew Davis <andrew@moodle.com>
  24 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25 + */
  26 +
  27 +defined('MOODLE_INTERNAL') || die();
  28 +
  29 +/**
  30 + * Folder conversion handler. This resource handler is called by moodle1_mod_resource_handler
  31 + */
  32 +class moodle1_mod_folder_handler extends moodle1_mod_handler {
  33 + /** @var array in-memory cache for the course module information */
  34 + protected $currentcminfo = null;
  35 + /** @var moodle1_file_manager instance */
  36 + protected $fileman = null;
  37 +
  38 + /**
  39 + * Declare the paths in moodle.xml we are able to convert
  40 + *
  41 + * @return array of {@link convert_path} instances
  42 + */
  43 + public function get_paths() {
  44 + return array();
  45 + }
  46 +
  47 + /**
  48 + * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
  49 + * Called by moodle1_mod_resource_handler::process_resource()
  50 + */
  51 + public function process_resource($data) {
  52 + // get the course module id and context id
  53 + $instanceid = $data['id'];
  54 + $this->currentcminfo = $this->get_cminfo($instanceid);
  55 + $moduleid = $this->currentcminfo['id'];
  56 + $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
  57 +
  58 + // we now have all information needed to start writing into the file
  59 + $this->open_xml_writer("activities/folder_{$moduleid}/folder.xml");
  60 + $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
  61 + 'modulename' => 'folder', 'contextid' => $contextid));
  62 + $this->xmlwriter->begin_tag('folder', array('id' => $instanceid));
  63 +
  64 + unset($data['id']); // we already write it as attribute, do not repeat it as child element
  65 + foreach ($data as $field => $value) {
  66 + $this->xmlwriter->full_tag($field, $value);
  67 + }
  68 +
  69 + // prepare file manager for migrating the folder
  70 + $this->fileman = $this->converter->get_file_manager($contextid, 'mod_folder', 'content');
  71 + $this->fileman->migrate_directory('course_files/'.$data['reference']);
  72 + }
  73 +
  74 + public function on_resource_end() {
  75 + // close folder.xml
  76 + $this->xmlwriter->end_tag('folder');
  77 + $this->xmlwriter->end_tag('activity');
  78 + $this->close_xml_writer();
  79 +
  80 + // write inforef.xml for migrated folder
  81 + $this->open_xml_writer("activities/folder_{$this->currentcminfo['id']}/inforef.xml");
  82 + $this->xmlwriter->begin_tag('inforef');
  83 + $this->xmlwriter->begin_tag('fileref');
  84 + foreach ($this->fileman->get_fileids() as $fileid) {
  85 + $this->write_xml('file', array('id' => $fileid));
  86 + }
  87 + $this->xmlwriter->end_tag('fileref');
  88 + $this->xmlwriter->end_tag('inforef');
  89 + $this->close_xml_writer();
  90 + }
  91 +}
134 mod/imscp/backup/moodle1/lib.php
... ... @@ -0,0 +1,134 @@
  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 + * Provides support for the conversion of moodle1 backup to the moodle2 format
  20 + *
  21 + * @package mod
  22 + * @subpackage imscp
  23 + * @copyright 2011 Andrew Davis <andrew@moodle.com>
  24 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25 + */
  26 +
  27 +defined('MOODLE_INTERNAL') || die();
  28 +
  29 +/**
  30 + * imscp conversion handler. This resource handler is called by moodle1_mod_resource_handler
  31 + */
  32 +class moodle1_mod_imscp_handler extends moodle1_mod_handler {
  33 + /** @var array in-memory cache for the course module information for the current imscp */
  34 + protected $currentcminfo = null;
  35 +
  36 + //there are two file manager instances as we need to put files in two file areas
  37 +
  38 + /** @var moodle1_file_manager the file manager instance */
  39 + protected $fileman = null;
  40 +
  41 + /**
  42 + * Declare the paths in moodle.xml we are able to convert
  43 + *
  44 + * @return array of {@link convert_path} instances
  45 + */
  46 + public function get_paths() {
  47 + return array();
  48 + }
  49 +
  50 + /**
  51 + * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
  52 + * Called by moodle1_mod_resource_handler::process_resource()
  53 + */
  54 + public function process_resource($data) {
  55 + global $CFG;
  56 +
  57 + $instanceid = $data['id'];
  58 + $this->currentcminfo = $this->get_cminfo($instanceid);
  59 + $moduleid = $this->currentcminfo['id'];
  60 + $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
  61 +
  62 + if ($CFG->texteditors !== 'textarea') {
  63 + $data['intro'] = text_to_html($data['intro'], false, false, true);
  64 + $data['introformat'] = FORMAT_HTML;
  65 + } else {
  66 + $data['intro'] = $data['intro'];
  67 + $data['introformat'] = FORMAT_MOODLE;
  68 + }
  69 +
  70 + $data['revision'] = 1;
  71 + $data['keepold'] = 1;
  72 +
  73 + //Prepare to migrate the deployed (ie extracted) version of the content package
  74 + $this->fileman = $this->converter->get_file_manager($contextid, 'mod_imscp', 'content', $data['revision']);
  75 + $this->fileman->migrate_directory('moddata/resource/'.$data['id']);
  76 +
  77 + // parse manifest
  78 + $structure = $this->parse_structure($data, $contextid);
  79 + $data['structure'] = is_array($structure) ? serialize($structure) : null;
  80 +
  81 + // we now have all information needed to start writing into the module file
  82 +
  83 + $this->open_xml_writer("activities/imscp_{$moduleid}/imscp.xml");
  84 + $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
  85 + 'modulename' => 'imscp', 'contextid' => $contextid));
  86 + $this->xmlwriter->begin_tag('imscp', array('id' => $instanceid));
  87 +
  88 + unset($data['id']); // we already write it as attribute, do not repeat it as child element
  89 + foreach ($data as $field => $value) {
  90 + $this->xmlwriter->full_tag($field, $value);
  91 + }
  92 +
  93 + /* We currently do not support undeployed IMS packages
  94 + * They need to be deployed (unzipped) to the mod data area then have the ims structure figured out
  95 + */
  96 + }
  97 +
  98 + public function on_resource_end() {
  99 + //close imscp.xml
  100 + $this->xmlwriter->end_tag('imscp');
  101 + $this->xmlwriter->end_tag('activity');
  102 + $this->close_xml_writer();
  103 +
  104 + // write inforef.xml for migrated imscp files
  105 + $this->open_xml_writer("activities/imscp_{$this->currentcminfo['id']}/inforef.xml");
  106 + $this->xmlwriter->begin_tag('inforef');
  107 + $this->xmlwriter->begin_tag('fileref');
  108 + foreach ($this->fileman->get_fileids() as $fileid) {
  109 + $this->write_xml('file', array('id' => $fileid));
  110 + }
  111 + $this->xmlwriter->end_tag('fileref');
  112 + $this->xmlwriter->end_tag('inforef');
  113 + $this->close_xml_writer();
  114 + }
  115 +
  116 + /// internal implementation details follow /////////////////////////////////
  117 +
  118 + /**
  119 + * Parse the IMS package structure for the $imscp->structure field
  120 + * @param array $data the array of ims package data
  121 + */
  122 + protected function parse_structure($data, $contextid) {
  123 + global $CFG;
  124 +
  125 + $temppath = $this->converter->get_tempdir_path();
  126 + $manifestfilecontents = file_get_contents($temppath.'/moddata/resource/'.$data['id'].'/imsmanifest.xml');
  127 + if (empty($manifestfilecontents)) {
  128 + return null;
  129 + }
  130 +
  131 + require_once($CFG->dirroot.'/mod/imscp/locallib.php');
  132 + return imscp_parse_manifestfile($manifestfilecontents);
  133 + }
  134 +}
16 mod/imscp/locallib.php
@@ -78,6 +78,12 @@ function imscp_htmllize_item($item, $imscp, $cm) {
78 78 return $result;
79 79 }
80 80
  81 +/**
  82 + * Parse an IMS content package's manifest file to determine its structure
  83 + * @param object $imscp
  84 + * @param object $context
  85 + * @return array
  86 + */
81 87 function imscp_parse_structure($imscp, $context) {
82 88 $fs = get_file_storage();
83 89
@@ -85,9 +91,17 @@ function imscp_parse_structure($imscp, $context) {
85 91 return null;
86 92 }
87 93
  94 + imscp_parse_manifestfile($manifestfile->get_content());
  95 +}
88 96
  97 +/**
  98 + * Parse the contents of a IMS package's manifest file
  99 + * @param string $manifestfilecontents the contents of the manifest file
  100 + * @return array
  101 + */
  102 +function imscp_parse_manifestfile($manifestfilecontents) {
89 103 $doc = new DOMDocument();
90   - if (!$doc->loadXML($manifestfile->get_content(), LIBXML_NONET)) {
  104 + if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET)) {
91 105 return null;
92 106 }
93 107
131 mod/page/backup/moodle1/lib.php
... ... @@ -0,0 +1,131 @@
  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 + * Provides support for the conversion of moodle1 backup to the moodle2 format
  20 + *
  21 + * @package mod
  22 + * @subpackage page
  23 + * @copyright 2011 Andrew Davis <andrew@moodle.com>
  24 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25 + */
  26 +
  27 +defined('MOODLE_INTERNAL') || die();
  28 +
  29 +/**
  30 + * Page conversion handler. This resource handler is called by moodle1_mod_resource_handler
  31 + */
  32 +class moodle1_mod_page_handler extends moodle1_mod_handler {
  33 + /** @var array in-memory cache for the course module information */
  34 + protected $currentcminfo = null;
  35 + /** @var moodle1_file_manager instance */
  36 + protected $fileman = null;
  37 +
  38 + /**
  39 + * Declare the paths in moodle.xml we are able to convert
  40 + *
  41 + * @return array of {@link convert_path} instances
  42 + */
  43 + public function get_paths() {
  44 + return array();
  45 + }
  46 +
  47 + /**
  48 + * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
  49 + * Called by moodle1_mod_resource_handler::process_resource()
  50 + */
  51 + public function process_resource($data) {
  52 + global $CFG;
  53 +
  54 + // get the course module id and context id
  55 + $instanceid = $data['id'];
  56 + $cminfo = $this->get_cminfo($instanceid, 'resource');
  57 + $moduleid = $cminfo['id'];
  58 + $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
  59 +
  60 + //we now only support html intros
  61 + if ($data['type'] == 'text') {
  62 + $data['intro'] = text_to_html($data['intro'], false, false, true);
  63 + $data['introformat'] = FORMAT_HTML;
  64 + }
  65 +
  66 + $data['contentformat'] = (int)$data['reference'];
  67 + if ($data['contentformat'] < 0 || $data['contentformat'] > 4) {
  68 + $data['contentformat'] = FORMAT_MOODLE;
  69 + }
  70 +
  71 + $data['content'] = $data['alltext'];
  72 + $data['revision'] = 1;
  73 + $data['timemodified'] = time();
  74 +
  75 + // convert links to old course files
  76 + $originalcourseinfo = $this->converter->get_stash('original_course_info');
  77 + if (!empty($originalcourseinfo) && array_key_exists('original_course_id', $originalcourseinfo)) {
  78 + $courseid = $originalcourseinfo['original_course_id'];
  79 +
  80 + $usedfiles = array("$CFG->wwwroot/file.php/$courseid/", "$CFG->wwwroot/file.php?file=/$courseid/");
  81 + $data['content'] = str_ireplace($usedfiles, '@@PLUGINFILE@@/', $data['content']);
  82 + if (strpos($data['content'], '@@PLUGINFILE@@/') === false) {
  83 + $data['legacyfiles'] = RESOURCELIB_LEGACYFILES_NO;
  84 + } else {
  85 + $data['legacyfiles'] = RESOURCELIB_LEGACYFILES_ACTIVE;
  86 + }
  87 + } else {
  88 + $data['legacyfiles'] = RESOURCELIB_LEGACYFILES_NO;
  89 + }
  90 +
  91 + $options = array('printheading'=>0, 'printintro'=>0);
  92 + if ($data['popup']) {
  93 + $data['display'] = RESOURCELIB_DISPLAY_POPUP;
  94 + if ($data['popup']) {
  95 + $rawoptions = explode(',', $data['popup']);
  96 + foreach ($rawoptions as $rawoption) {
  97 + list($name, $value) = explode('=', trim($rawoption), 2);
  98 + if ($value > 0 and ($name == 'width' or $name == 'height')) {
  99 + $options['popup'.$name] = $value;
  100 + continue;
  101 + }
  102 + }
  103 + }
  104 + } else {
  105 + $data['display'] = RESOURCELIB_DISPLAY_OPEN;
  106 + }
  107 + $data['displayoptions'] = serialize($options);
  108 +
  109 + // prepare file manager for migrating the folder
  110 + $this->fileman = $this->converter->get_file_manager($contextid, 'mod_page', 'content', 0);
  111 + $this->fileman->migrate_directory('moddata/page/'.$data['id']);
  112 +
  113 + // we now have all information needed to start writing into the file
  114 + $this->open_xml_writer("activities/page_{$moduleid}/page.xml");
  115 + $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
  116 + 'modulename' => 'page', 'contextid' => $contextid));
  117 + $this->xmlwriter->begin_tag('page', array('id' => $instanceid));
  118 +
  119 + unset($data['id']); // we already write it as attribute, do not repeat it as child element
  120 + foreach ($data as $field => $value) {
  121 + $this->xmlwriter->full_tag($field, $value);
  122 + }
  123 + }
  124 +
  125 + public function on_resource_end($data) {
  126 + // close page.xml
  127 + $this->xmlwriter->end_tag('page');
  128 + $this->xmlwriter->end_tag('activity');
  129 + $this->close_xml_writer();
  130 + }
  131 +}
267 mod/resource/backup/moodle1/lib.php
... ... @@ -0,0 +1,267 @@
  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 + * Provides support for the conversion of moodle1 backup to the moodle2 format
  20 + *
  21 + * @package mod
  22 + * @subpackage forum
  23 + * @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
  24 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25 + */
  26 +
  27 +defined('MOODLE_INTERNAL') || die();
  28 +
  29 +/**
  30 + * Forum conversion handler
  31 + */
  32 +class moodle1_mod_resource_handler extends moodle1_mod_handler {
  33 + /** @var array in-memory cache for the course module information */
  34 + protected $currentcminfo = null;
  35 + /** @var moodle1_file_manager instance */
  36 + protected $fileman = null;
  37 +
  38 + /**
  39 + * Declare the paths in moodle.xml we are able to convert
  40 + *
  41 + * The method returns list of {@link convert_path} instances.
  42 + * For each path returned, the corresponding conversion method must be
  43 + * defined.
  44 + *
  45 + * Note that the paths /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE do not
  46 + * actually exist in the file. The last element with the module name was
  47 + * appended by the moodle1_converter class.
  48 + *
  49 + * @return array of {@link convert_path} instances
  50 + */
  51 + public function get_paths() {
  52 + return array(
  53 + new convert_path(
  54 + 'resource', '/MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE',
  55 + array(
  56 + 'renamefields' => array(
  57 + 'summary' => 'intro',
  58 + ),
  59 + 'newfields' => array(
  60 + 'introformat' => 0,
  61 + ),
  62 + 'dropfields' => array(
  63 + 'modtype',
  64 + ),
  65 + )
  66 + )
  67 + );
  68 + }
  69 +
  70 + /**
  71 + * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
  72 + */
  73 + public function process_resource(array $data, array $raw) {
  74 + global $CFG;
  75 + require_once("$CFG->libdir/resourcelib.php");
  76 +
  77 + //if this is a file or URL resource we need to deal with the options
  78 + //before possibly branching out to the URL successor
  79 + if ($data['type'] == 'file') {
  80 + $options = array('printheading'=>0, 'printintro'=>1);
  81 + if ($data['options'] == 'frame') {
  82 + $data['display'] = RESOURCELIB_DISPLAY_FRAME;
  83 +
  84 + } else if ($data['options'] == 'objectframe') {
  85 + $data['display'] = RESOURCELIB_DISPLAY_EMBED;
  86 +
  87 + } else if ($data['options'] == 'forcedownload') {
  88 + $data['display'] = RESOURCELIB_DISPLAY_DOWNLOAD;
  89 +
  90 + } else if ($data['popup']) {
  91 + $data['display'] = RESOURCELIB_DISPLAY_POPUP;
  92 + if ($data['popup']) {
  93 + $rawoptions = explode(',', $data['popup']);
  94 + foreach ($rawoptions as $rawoption) {
  95 + list($name, $value) = explode('=', trim($rawoption), 2);
  96 + if ($value > 0 and ($name == 'width' or $name == 'height')) {
  97 + $options['popup'.$name] = $value;
  98 + continue;
  99 + }
  100 + }
  101 + }
  102 +
  103 + } else {
  104 + $data['display'] = RESOURCELIB_DISPLAY_AUTO;
  105 + }
  106 + $data['displayoptions'] = serialize($options);
  107 + unset($data['popup']);
  108 + }
  109 +
  110 + // fix invalid NULL popup and options data in old mysql databases
  111 + if (!array_key_exists ('popup', $data) || $data['popup'] === null) {
  112 + $data['popup'] = '';
  113 + }
  114 + if (!array_key_exists ('options', $data) || $data['options'] === null) {
  115 + $data['options'] = '';
  116 + }
  117 +
  118 + if ($successor = $this->get_successor($data['type'], $data['reference'])) {
  119 + // the instance id will be kept
  120 + $instanceid = $data['id'];
  121 +
  122 + // move the instance from the resource's modinfo stash to the successor's
  123 + // modinfo stash
  124 + $resourcemodinfo = $this->converter->get_stash('modinfo_resource');
  125 + $successormodinfo = $this->converter->get_stash('modinfo_'.$successor->get_modname());
  126 + $successormodinfo['instances'][$instanceid] = $resourcemodinfo['instances'][$instanceid];
  127 + unset($resourcemodinfo['instances'][$instanceid]);
  128 + $this->converter->set_stash('modinfo_resource', $resourcemodinfo);
  129 + $this->converter->set_stash('modinfo_'.$successor->get_modname(), $successormodinfo);
  130 +
  131 + // get the course module information for the legacy resource module
  132 + $cminfo = $this->get_cminfo($instanceid);
  133 +
  134 + // use the version of the successor instead of the current mod/resource
  135 + // beware - the version.php declares info via $module object, do not use
  136 + // a variable of such name here
  137 + include $CFG->dirroot.'/mod/'.$successor->get_modname().'/version.php';
  138 + $cminfo['version'] = $module->version;
  139 +
  140 + // stash the new course module information for this successor
  141 + $cminfo['modulename'] = $successor->get_modname();
  142 + $this->converter->set_stash('cminfo_'.$cminfo['modulename'], $cminfo, $instanceid);
  143 +
  144 + // delegate the processing to the successor handler
  145 + return $successor->process_resource($data, $raw);
  146 + }
  147 +
  148 + //only $data['type'] == "file" should get to here
  149 +
  150 + // get the course module id and context id
  151 + $instanceid = $data['id'];
  152 + $this->currentcminfo = $this->get_cminfo($instanceid);
  153 + $moduleid = $this->currentcminfo['id'];
  154 + $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
  155 +
  156 + unset($data['type']);
  157 + unset($data['alltext']);
  158 + unset($data['popup']);
  159 + unset($data['options']);
  160 +
  161 + $data['tobemigrated'] = 0;
  162 + $data['mainfile'] = null;
  163 + $data['legacyfiles'] = 0;
  164 + $data['legacyfileslast'] = null;
  165 + $data['display'] = 0;
  166 + $data['displayoptions'] = null;
  167 + $data['filterfiles'] = 0;
  168 + $data['revision'] = 0;
  169 + unset($data['mainfile']);
  170 +
  171 + // we now have all information needed to start writing into the file
  172 + $this->open_xml_writer("activities/resource_{$moduleid}/resource.xml");
  173 + $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
  174 + 'modulename' => 'resource', 'contextid' => $contextid));
  175 + $this->xmlwriter->begin_tag('resource', array('id' => $instanceid));
  176 +
  177 + unset($data['id']); // we already write it as attribute, do not repeat it as child element
  178 + foreach ($data as $field => $value) {
  179 + $this->xmlwriter->full_tag($field, $value);
  180 + }
  181 +
  182 + // prepare file manager for migrating the resource file
  183 + $this->fileman = $this->converter->get_file_manager($contextid, 'mod_resource', 'content');
  184 + $this->fileman->migrate_file('course_files/'.$data['reference']);
  185 + }
  186 +
  187 + public function on_resource_end(array $data) {
  188 + if ($successor = $this->get_successor($data['type'], $data['reference'])) {
  189 + $successor->on_resource_end($data);
  190 + } else {
  191 + $this->xmlwriter->end_tag('resource');
  192 + $this->xmlwriter->end_tag('activity');
  193 + $this->close_xml_writer();
  194 +
  195 + // write inforef.xml for migrated resource file.
  196 + $this->open_xml_writer("activities/resource_{$this->currentcminfo['id']}/inforef.xml");
  197 + $this->xmlwriter->begin_tag('inforef');
  198 + $this->xmlwriter->begin_tag('fileref');
  199 + foreach ($this->fileman->get_fileids() as $fileid) {
  200 + $this->write_xml('file', array('id' => $fileid));
  201 + }
  202 + $this->xmlwriter->end_tag('fileref');
  203 + $this->xmlwriter->end_tag('inforef');
  204 + $this->close_xml_writer();
  205 + }
  206 + }
  207 +
  208 + /// internal implementation details follow /////////////////////////////////
  209 +
  210 + /**
  211 + * Returns the handler of the new 2.0 mod type according the given type of the legacy 1.9 resource
  212 + *
  213 + * @param string $type the value of the 'type' field in 1.9 resource
  214 + * @param string $reference a file path. Necessary to differentiate files from web URLs
  215 + * @throws moodle1_convert_exception for the unknown types
  216 + * @return null|moodle1_mod_handler the instance of the handler, or null if the type does not have a successor
  217 + */
  218 + protected function get_successor($type, $reference) {
  219 + static $successors = array();
  220 +
  221 + switch ($type) {
  222 + case 'text':
  223 + case 'html':
  224 + $name = 'page';
  225 + break;
  226 + case 'directory':
  227 + $name = 'folder';
  228 + break;
  229 + case 'ims':
  230 + $name = 'imscp';
  231 + break;
  232 + case 'file':
  233 + // if http:// https:// ftp:// OR starts with slash need to be converted to URL
  234 + if (strpos($reference, '://') or strpos($reference, '/') === 0) {
  235 + $name = 'url';
  236 + } else {
  237 + $name = null;
  238 + }
  239 + break;
  240 + default:
  241 + throw new moodle1_convert_exception('unknown_resource_successor', $type);
  242 + }
  243 +
  244 + if (is_null($name)) {
  245 + return null;
  246 + }
  247 +
  248 + if (!isset($successors[$name])) {
  249 + $class = 'moodle1_mod_'.$name.'_handler';
  250 + $successors[$name] = new $class($this->converter, 'mod', $name);
  251 +
  252 + // add the successor into the modlist stash
  253 + $modnames = $this->converter->get_stash('modnameslist');
  254 + $modnames[] = $name;
  255 + $modnames = array_unique($modnames); // should not be needed but just in case
  256 + $this->converter->set_stash('modnameslist', $modnames);
  257 +
  258 + // add the successor's modinfo stash
  259 + $modinfo = $this->converter->get_stash('modinfo_resource');
  260 + $modinfo['name'] = $name;
  261 + $modinfo['instances'] = array();
  262 + $this->converter->set_stash('modinfo_'.$name, $modinfo);
  263 + }
  264 +
  265 + return $successors[$name];
  266 + }
  267 +}
74 mod/url/backup/moodle1/lib.php
... ... @@ -0,0 +1,74 @@
  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 + * Provides support for the conversion of moodle1 backup to the moodle2 format
  20 + *
  21 + * @package mod
  22 + * @subpackage url
  23 + * @copyright 2011 Andrew Davis <andrew@moodle.com>
  24 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25 + */
  26 +
  27 +defined('MOODLE_INTERNAL') || die();
  28 +
  29 +/**
  30 + * URL conversion handler. This resource handler is called by moodle1_mod_resource_handler
  31 + */
  32 +class moodle1_mod_url_handler extends moodle1_mod_handler {
  33 +
  34 + /**
  35 + * Declare the paths in moodle.xml we are able to convert
  36 + *
  37 + * @return array of {@link convert_path} instances
  38 + */
  39 + public function get_paths() {
  40 + return array();
  41 + }
  42 +
  43 + /**
  44 + * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
  45 + * Called by moodle1_mod_resource_handler::process_resource()
  46 + */
  47 + public function process_resource($data) {
  48 + $data['externalurl'] = $data['reference'];
  49 + unset($data['reference']);
  50 +
  51 + // get the course module id and context id
  52 + $instanceid = $data['id'];
  53 + $cminfo = $this->get_cminfo($instanceid, 'resource');
  54 + $moduleid = $cminfo['id'];
  55 + $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
  56 +
  57 + // we now have all information needed to start writing into the file
  58 + $this->open_xml_writer("activities/url_{$moduleid}/url.xml");
  59 + $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
  60 + 'modulename' => 'url', 'contextid' => $contextid));
  61 + $this->xmlwriter->begin_tag('url', array('id' => $instanceid));
  62 +
  63 + unset($data['id']); // we already write it as attribute, do not repeat it as child element
  64 + foreach ($data as $field => $value) {
  65 + $this->xmlwriter->full_tag($field, $value);
  66 + }
  67 + }
  68 +
  69 + public function on_resource_end() {
  70 + $this->xmlwriter->end_tag('url');
  71 + $this->xmlwriter->end_tag('activity');
  72 + $this->close_xml_writer();
  73 + }
  74 +}

0 comments on commit 5da3af5

Please sign in to comment.
Something went wrong with that request. Please try again.