weekly release 4.0dev
[moodle.git] / mod / forum / backup / moodle1 / lib.php
CommitLineData
72c089f1 1<?php
e48477d9
DM
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 *
01030f1b 21 * @package mod_forum
e48477d9
DM
22 * @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
72c089f1 28/**
1e2c7351 29 * Forum conversion handler
72c089f1 30 */
1e2c7351 31class moodle1_mod_forum_handler extends moodle1_mod_handler {
0164592b 32
c5c315e2
DM
33 /** @var moodle1_file_manager */
34 protected $fileman = null;
35
36 /** @var int cmid */
37 protected $moduleid = null;
38
72c089f1 39 /**
1e2c7351 40 * Declare the paths in moodle.xml we are able to convert
72c089f1 41 *
1e2c7351
DM
42 * The method returns list of {@link convert_path} instances.
43 * For each path returned, the corresponding conversion method must be
44 * defined.
45 *
46 * Note that the paths /MOODLE_BACKUP/COURSE/MODULES/MOD/FORUM do not
47 * actually exist in the file. The last element with the module name was
48 * appended by the moodle1_converter class.
49 *
50 * @return array of {@link convert_path} instances
72c089f1 51 */
1e2c7351 52 public function get_paths() {
72c089f1 53 return array(
ab1da267
AD
54 new convert_path('forum', '/MOODLE_BACKUP/COURSE/MODULES/MOD/FORUM',
55 array(
56 'renamefields' => array(
57 'format' => 'messageformat',
58 ),
59 'newfields' => array(
60 'completiondiscussions' => 0,
61 'completionreplies' => 0,
62 'completionpost' => 0,
63 'maxattachments' => 1,
64 'introformat' => 0,
65 ),
66 )
67 ),
72c089f1
MN
68 );
69 }
70
1e2c7351
DM
71 /**
72 * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/FORUM data
73 */
74 public function process_forum($data) {
3ed2c6e5
CF
75 global $CFG;
76
ab1da267 77 // get the course module id and context id
c5c315e2
DM
78 $instanceid = $data['id'];
79 $cminfo = $this->get_cminfo($instanceid);
80 $this->moduleid = $cminfo['id'];
81 $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
82
83 // get a fresh new file manager for this instance
84 $this->fileman = $this->converter->get_file_manager($contextid, 'mod_forum');
85
86 // convert course files embedded into the intro
87 $this->fileman->filearea = 'intro';
88 $this->fileman->itemid = 0;
89 $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
90
3ed2c6e5
CF
91 // Convert the introformat if necessary.
92 if ($CFG->texteditors !== 'textarea') {
93 $data['intro'] = text_to_html($data['intro'], false, false, true);
94 $data['introformat'] = FORMAT_HTML;
95 }
96
c5c315e2
DM
97 // start writing forum.xml
98 $this->open_xml_writer("activities/forum_{$this->moduleid}/forum.xml");
99 $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
ab1da267
AD
100 'modulename' => 'forum', 'contextid' => $contextid));
101 $this->xmlwriter->begin_tag('forum', array('id' => $instanceid));
102
ab1da267 103 foreach ($data as $field => $value) {
c5c315e2
DM
104 if ($field <> 'id') {
105 $this->xmlwriter->full_tag($field, $value);
106 }
ab1da267
AD
107 }
108
109 $this->xmlwriter->begin_tag('discussions');
c5c315e2
DM
110
111 return $data;
ab1da267
AD
112 }
113
114 /**
115 * This is executed when we reach the closing </MOD> tag of our 'forum' path
116 */
117 public function on_forum_end() {
c5c315e2 118 // finish writing forum.xml
ab1da267
AD
119 $this->xmlwriter->end_tag('discussions');
120 $this->xmlwriter->end_tag('forum');
121 $this->xmlwriter->end_tag('activity');
122 $this->close_xml_writer();
ab1da267 123
c5c315e2
DM
124 // write inforef.xml
125 $this->open_xml_writer("activities/forum_{$this->moduleid}/inforef.xml");
126 $this->xmlwriter->begin_tag('inforef');
127 $this->xmlwriter->begin_tag('fileref');
128 foreach ($this->fileman->get_fileids() as $fileid) {
129 $this->write_xml('file', array('id' => $fileid));
130 }
131 $this->xmlwriter->end_tag('fileref');
132 $this->xmlwriter->end_tag('inforef');
133 $this->close_xml_writer();
134 }
e48477d9 135}