Merge branch 'MDL-29256-resource-format' of git://github.com/mudrd8mz/moodle
[moodle.git] / mod / page / backup / moodle1 / lib.php
CommitLineData
5da3af58
AD
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
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Page conversion handler. This resource handler is called by moodle1_mod_resource_handler
31 */
48fcecff
DM
32class moodle1_mod_page_handler extends moodle1_resource_successor_handler {
33
5da3af58
AD
34 /** @var moodle1_file_manager instance */
35 protected $fileman = null;
36
5da3af58
AD
37 /**
38 * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
39 * Called by moodle1_mod_resource_handler::process_resource()
40 */
9b2ab50d 41 public function process_legacy_resource(array $data) {
5da3af58
AD
42
43 // get the course module id and context id
44 $instanceid = $data['id'];
45 $cminfo = $this->get_cminfo($instanceid, 'resource');
46 $moduleid = $cminfo['id'];
47 $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
48
9b2ab50d
DM
49 // convert the legacy data onto the new page record
50 $page = array();
51 $page['id'] = $data['id'];
52 $page['name'] = $data['name'];
53 $page['intro'] = $data['intro'];
54 $page['introformat'] = $data['introformat'];
55 $page['content'] = $data['alltext'];
9b2ab50d 56
cb3ef220
DM
57 if ($data['type'] === 'html') {
58 // legacy Resource of the type Web page
59 $page['contentformat'] = FORMAT_HTML;
60
61 } else {
62 // legacy Resource of the type Plain text page
63 $page['contentformat'] = (int)$data['reference'];
64
65 if ($page['contentformat'] < 0 or $page['contentformat'] > 4) {
66 $page['contentformat'] = FORMAT_MOODLE;
67 }
5da3af58
AD
68 }
69
9b2ab50d
DM
70 $page['legacyfiles'] = RESOURCELIB_LEGACYFILES_ACTIVE;
71 $page['legacyfileslast'] = null;
72 $page['revision'] = 1;
73 $page['timemodified'] = $data['timemodified'];
5da3af58 74
9b2ab50d
DM
75 // populate display and displayoptions fields
76 $options = array('printheading' => 0, 'printintro' => 0);
77 if ($data['popup']) {
78 $page['display'] = RESOURCELIB_DISPLAY_POPUP;
79 $rawoptions = explode(',', $data['popup']);
80 foreach ($rawoptions as $rawoption) {
81 list($name, $value) = explode('=', trim($rawoption), 2);
82 if ($value > 0 and ($name == 'width' or $name == 'height')) {
83 $options['popup'.$name] = $value;
84 continue;
85 }
86 }
87 } else {
88 $page['display'] = RESOURCELIB_DISPLAY_OPEN;
48fcecff 89 }
9b2ab50d
DM
90 $page['displayoptions'] = serialize($options);
91
92 // get a fresh new file manager for this instance
93 $this->fileman = $this->converter->get_file_manager($contextid, 'mod_page');
94
95 // convert course files embedded into the intro
baa44e3d
DM
96 $this->fileman->filearea = 'intro';
97 $this->fileman->itemid = 0;
98 $page['intro'] = moodle1_converter::migrate_referenced_files($page['intro'], $this->fileman);
5da3af58 99
9b2ab50d 100 // convert course files embedded into the content
baa44e3d
DM
101 $this->fileman->filearea = 'content';
102 $this->fileman->itemid = 0;
103 $page['content'] = moodle1_converter::migrate_referenced_files($page['content'], $this->fileman);
5da3af58 104
48fcecff 105 // write page.xml
5da3af58
AD
106 $this->open_xml_writer("activities/page_{$moduleid}/page.xml");
107 $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
108 'modulename' => 'page', 'contextid' => $contextid));
9b2ab50d 109 $this->write_xml('page', $page, array('/page/id'));
48fcecff
DM
110 $this->xmlwriter->end_tag('activity');
111 $this->close_xml_writer();
5da3af58 112
48fcecff
DM
113 // write inforef.xml for migrated resource file.
114 $this->open_xml_writer("activities/page_{$moduleid}/inforef.xml");
115 $this->xmlwriter->begin_tag('inforef');
116 $this->xmlwriter->begin_tag('fileref');
117 foreach ($this->fileman->get_fileids() as $fileid) {
118 $this->write_xml('file', array('id' => $fileid));
5da3af58 119 }
48fcecff
DM
120 $this->xmlwriter->end_tag('fileref');
121 $this->xmlwriter->end_tag('inforef');
5da3af58
AD
122 $this->close_xml_writer();
123 }
48fcecff 124}