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 * Based off of a template @ http://docs.moodle.org/dev/Backup_1.9_conversion_for_developers
20 *
21 * @package    mod_chat
22 * @copyright  2011 Aparup Banerjee <aparup@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 * Chat conversion handler
30 */
31class moodle1_mod_chat_handler extends moodle1_mod_handler {
32
33    /** @var moodle1_file_manager */
34    protected $fileman = null;
35
36    /** @var int cmid */
37    protected $moduleid = null;
38
39    /**
40     * Declare the paths in moodle.xml we are able to convert
41     *
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 path /MOODLE_BACKUP/COURSE/MODULES/MOD/CHAT does 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
51     */
52    public function get_paths() {
53        return array(
54            new convert_path(
55                'chat', '/MOODLE_BACKUP/COURSE/MODULES/MOD/CHAT',
56                array(
57                    'newfields' => array(
58                        'introformat' => 0
59                    )
60                )
61            )
62        );
63    }
64
65    /**
66     * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/CHAT
67     * data available
68     */
69    public function process_chat($data) {
70        global $CFG;
71
72        // Get the course module id and context id.
73        $instanceid     = $data['id'];
74        $cminfo         = $this->get_cminfo($instanceid);
75        $this->moduleid = $cminfo['id'];
76        $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
77
78        // Replay the upgrade step 2010050101.
79        if ($CFG->texteditors !== 'textarea') {
80            $data['intro']       = text_to_html($data['intro'], false, false, true);
81            $data['introformat'] = FORMAT_HTML;
82        }
83
84        // Get a fresh new file manager for this instance.
85        $this->fileman = $this->converter->get_file_manager($contextid, 'mod_chat');
86
87        // Convert course files embedded into the intro.
88        $this->fileman->filearea = 'intro';
89        $this->fileman->itemid   = 0;
90        $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
91
92        // Start writing chat.xml.
93        $this->open_xml_writer("activities/chat_{$this->moduleid}/chat.xml");
94        $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
95            'modulename' => 'chat', 'contextid' => $contextid));
96        $this->xmlwriter->begin_tag('chat', array('id' => $instanceid));
97
98        foreach ($data as $field => $value) {
99            if ($field <> 'id') {
100                $this->xmlwriter->full_tag($field, $value);
101            }
102        }
103
104        $this->xmlwriter->begin_tag('messages');
105
106        return $data;
107    }
108
109    /**
110     * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/CHAT/MESSAGES/MESSAGE
111     * data available
112     */
113    public function process_chat_message($data) {
114        // MDL-46466 - Should this be empty?
115    }
116
117    /**
118     * This is executed when we reach the closing </MOD> tag of our 'chat' path
119     */
120    public function on_chat_end() {
121        // Close chat.xml.
122        $this->xmlwriter->end_tag('messages');
123        $this->xmlwriter->end_tag('chat');
124        $this->xmlwriter->end_tag('activity');
125        $this->close_xml_writer();
126
127        // Write inforef.xml.
128        $this->open_xml_writer("activities/chat_{$this->moduleid}/inforef.xml");
129        $this->xmlwriter->begin_tag('inforef');
130        $this->xmlwriter->begin_tag('fileref');
131        foreach ($this->fileman->get_fileids() as $fileid) {
132            $this->write_xml('file', array('id' => $fileid));
133        }
134        $this->xmlwriter->end_tag('fileref');
135        $this->xmlwriter->end_tag('inforef');
136        $this->close_xml_writer();
137    }
138}
139