1<?php
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 *
20 * @package    mod_feedback
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/**
28 * Feedback module conversion handler
29 */
30class moodle1_mod_feedback_handler extends moodle1_mod_handler {
31
32    /** @var moodle1_file_manager */
33    protected $fileman = null;
34
35    /** @var int cmid */
36    protected $moduleid = null;
37
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) {
88        global $CFG;
89
90        // get the course module id and context id
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
97
98        // get a fresh new file manager for this instance
99        $this->fileman = $this->converter->get_file_manager($contextid, 'mod_feedback');
100
101        // convert course files embedded into the intro
102        $this->fileman->filearea = 'intro';
103        $this->fileman->itemid   = 0;
104        $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
105
106        // Convert the introformat if necessary.
107        if ($CFG->texteditors !== 'textarea') {
108            $data['intro'] = text_to_html($data['intro'], false, false, true);
109            $data['introformat'] = FORMAT_HTML;
110        }
111
112        // start writing feedback.xml
113        $this->open_xml_writer("activities/feedback_{$this->moduleid}/feedback.xml");
114        $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
115            'modulename' => 'feedback', 'contextid' => $contextid));
116        $this->xmlwriter->begin_tag('feedback', array('id' => $instanceid));
117
118        foreach ($data as $field => $value) {
119            if ($field <> 'id') {
120                $this->xmlwriter->full_tag($field, $value);
121            }
122        }
123
124        $this->xmlwriter->begin_tag('items');
125
126        return $data;
127    }
128
129    /**
130     * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/FEEDBACK/ITEMS/ITEM
131     * data available
132     */
133    public function process_feedback_item($data) {
134        $this->write_xml('item', $data, array('/item/id'));
135    }
136
137    /**
138     * This is executed when we reach the closing </MOD> tag of our 'feedback' path
139     */
140    public function on_feedback_end() {
141        // finish writing feedback.xml
142        $this->xmlwriter->end_tag('items');
143        $this->xmlwriter->end_tag('feedback');
144        $this->xmlwriter->end_tag('activity');
145        $this->close_xml_writer();
146
147        // write inforef.xml
148        $this->open_xml_writer("activities/feedback_{$this->moduleid}/inforef.xml");
149        $this->xmlwriter->begin_tag('inforef');
150        $this->xmlwriter->begin_tag('fileref');
151        foreach ($this->fileman->get_fileids() as $fileid) {
152            $this->write_xml('file', array('id' => $fileid));
153        }
154        $this->xmlwriter->end_tag('fileref');
155        $this->xmlwriter->end_tag('inforef');
156        $this->close_xml_writer();
157    }
158}
159