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