MDL-34627 feedback: Convert default format value during restore
[moodle.git] / mod / feedback / backup / moodle1 / lib.php
CommitLineData
a6486a82 1<?php
a6486a82
RW
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Provides support for the conversion of moodle1 backup to the moodle2 format
19 *
9714baa1 20 * @package mod_feedback
a6486a82
RW
21 * @copyright 2011 Rossiani Wijaya <rwijaya@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
c5c315e2 28 * Feedback module conversion handler
a6486a82
RW
29 */
30class moodle1_mod_feedback_handler extends moodle1_mod_handler {
31
c5c315e2
DM
32 /** @var moodle1_file_manager */
33 protected $fileman = null;
34
35 /** @var int cmid */
36 protected $moduleid = null;
37
a6486a82
RW
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 path /MOODLE_BACKUP/COURSE/MODULES/MOD/FEEDBACK does 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 'feedback', '/MOODLE_BACKUP/COURSE/MODULES/MOD/FEEDBACK',
55 array(
56 'renamefields' => array(
57 'summary' => 'intro',
58 'pageaftersub' => 'page_after_submit',
59 ),
60 'newfields' => array(
61 'autonumbering' => 1,
62 'site_after_submit' => '',
63 'introformat' => 0,
64 'page_after_submitformat' => 0,
65 'completionsubmit' => 0,
66 ),
67 )
68 ),
69 new convert_path(
70 'feedback_item', '/MOODLE_BACKUP/COURSE/MODULES/MOD/FEEDBACK/ITEMS/ITEM',
71 array (
72 'newfields' => array(
73 'label' => '',
74 'options' => '',
75 'dependitem' => 0,
76 'dependvalue' => '',
77 ),
78 )
79 ),
80 );
81 }
82
83 /**
84 * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/FEEDBACK
85 * data available
86 */
87 public function process_feedback($data) {
f8857122
CF
88 global $CFG;
89
a6486a82 90 // get the course module id and context id
c5c315e2
DM
91 $instanceid = $data['id'];
92 $cminfo = $this->get_cminfo($instanceid);
93 $this->moduleid = $cminfo['id'];
94 $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
95
96 // get a fresh new file manager for this instance
97 $this->fileman = $this->converter->get_file_manager($contextid, 'mod_feedback');
98
99 // convert course files embedded into the intro
100 $this->fileman->filearea = 'intro';
101 $this->fileman->itemid = 0;
102 $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
103
f8857122
CF
104 // Convert the introformat if necessary.
105 if ($CFG->texteditors !== 'textarea') {
106 $data['intro'] = text_to_html($data['intro'], false, false, true);
107 $data['introformat'] = FORMAT_HTML;
108 }
109
c5c315e2
DM
110 // start writing feedback.xml
111 $this->open_xml_writer("activities/feedback_{$this->moduleid}/feedback.xml");
112 $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
a6486a82
RW
113 'modulename' => 'feedback', 'contextid' => $contextid));
114 $this->xmlwriter->begin_tag('feedback', array('id' => $instanceid));
115
a6486a82 116 foreach ($data as $field => $value) {
c5c315e2
DM
117 if ($field <> 'id') {
118 $this->xmlwriter->full_tag($field, $value);
119 }
a6486a82
RW
120 }
121
122 $this->xmlwriter->begin_tag('items');
c5c315e2
DM
123
124 return $data;
a6486a82
RW
125 }
126
127 /**
128 * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/FEEDBACK/ITEMS/ITEM
129 * data available
130 */
131 public function process_feedback_item($data) {
132 $this->write_xml('item', $data, array('/item/id'));
133 }
134
135 /**
136 * This is executed when we reach the closing </MOD> tag of our 'feedback' path
137 */
138 public function on_feedback_end() {
c5c315e2 139 // finish writing feedback.xml
a6486a82
RW
140 $this->xmlwriter->end_tag('items');
141 $this->xmlwriter->end_tag('feedback');
142 $this->xmlwriter->end_tag('activity');
143 $this->close_xml_writer();
c5c315e2
DM
144
145 // write inforef.xml
146 $this->open_xml_writer("activities/feedback_{$this->moduleid}/inforef.xml");
147 $this->xmlwriter->begin_tag('inforef');
148 $this->xmlwriter->begin_tag('fileref');
149 foreach ($this->fileman->get_fileids() as $fileid) {
150 $this->write_xml('file', array('id' => $fileid));
151 }
152 $this->xmlwriter->end_tag('fileref');
153 $this->xmlwriter->end_tag('inforef');
154 $this->close_xml_writer();
a6486a82 155 }
c5c315e2 156}