3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Provides support for the conversion of moodle1 backup to the moodle2 format
23 * @copyright 2011 Andrew Davis <andrew@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 * imscp conversion handler. This resource handler is called by moodle1_mod_resource_handler
32 class moodle1_mod_imscp_handler extends moodle1_resource_successor_handler {
34 /** @var moodle1_file_manager the file manager instance */
35 protected $fileman = null;
38 * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
39 * Called by moodle1_mod_resource_handler::process_resource()
41 public function process_legacy_resource(array $data, array $raw = null) {
43 $instanceid = $data['id'];
44 $currentcminfo = $this->get_cminfo($instanceid);
45 $moduleid = $currentcminfo['id'];
46 $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
48 // prepare the new imscp instance record
50 $imscp['id'] = $data['id'];
51 $imscp['name'] = $data['name'];
52 $imscp['intro'] = $data['intro'];
53 $imscp['introformat'] = $data['introformat'];
54 $imscp['revision'] = 1;
55 $imscp['keepold'] = 1;
56 $imscp['structure'] = null;
57 $imscp['timemodified'] = $data['timemodified'];
59 // prepare a fresh new file manager for this instance
60 $this->fileman = $this->converter->get_file_manager($contextid, 'mod_imscp');
62 // convert course files embedded into the intro
63 $this->fileman->filearea = 'intro';
64 $this->fileman->itemid = 0;
65 $imscp['intro'] = moodle1_converter::migrate_referenced_files($imscp['intro'], $this->fileman);
67 // migrate package backup file
68 if ($data['reference']) {
69 $packagename = basename($data['reference']);
70 $packagepath = $this->converter->get_tempdir_path().'/moddata/resource/'.$data['id'].'/'.$packagename;
71 if (file_exists($packagepath)) {
72 $this->fileman->filearea = 'backup';
73 $this->fileman->itemid = 1;
74 $this->fileman->migrate_file('moddata/resource/'.$data['id'].'/'.$packagename);
76 $this->log('missing imscp package', backup::LOG_WARNING, 'moddata/resource/'.$data['id'].'/'.$packagename);
80 // migrate extracted package data
81 $this->fileman->filearea = 'content';
82 $this->fileman->itemid = 1;
83 $this->fileman->migrate_directory('moddata/resource/'.$data['id']);
86 $structure = $this->parse_structure($this->converter->get_tempdir_path().'/moddata/resource/'.$data['id'].'/imsmanifest.xml');
87 $imscp['structure'] = is_array($structure) ? serialize($structure) : null;
90 $this->open_xml_writer("activities/imscp_{$moduleid}/imscp.xml");
91 $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
92 'modulename' => 'imscp', 'contextid' => $contextid));
93 $this->write_xml('imscp', $imscp, array('/imscp/id'));
94 $this->xmlwriter->end_tag('activity');
95 $this->close_xml_writer();
98 $this->open_xml_writer("activities/imscp_{$moduleid}/inforef.xml");
99 $this->xmlwriter->begin_tag('inforef');
100 $this->xmlwriter->begin_tag('fileref');
101 foreach ($this->fileman->get_fileids() as $fileid) {
102 $this->write_xml('file', array('id' => $fileid));
104 $this->xmlwriter->end_tag('fileref');
105 $this->xmlwriter->end_tag('inforef');
106 $this->close_xml_writer();
109 /// internal implementation details follow /////////////////////////////////
112 * Parse the IMS package structure for the $imscp->structure field
114 * @param string $manifestfilepath the full path to the manifest file to parse
116 protected function parse_structure($manifestfilepath) {
119 if (!file_exists($manifestfilepath)) {
120 $this->log('missing imscp manifest file', backup::LOG_WARNING);
123 $manifestfilecontents = file_get_contents($manifestfilepath);
124 if (empty($manifestfilecontents)) {
125 $this->log('empty imscp manifest file', backup::LOG_WARNING);
129 require_once($CFG->dirroot.'/mod/imscp/locallib.php');
130 return imscp_parse_manifestfile($manifestfilecontents);