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 *
21 * @package    mod_choice
22 * @copyright  2011 David Mudrak <david@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 * Choice conversion handler
30 */
31class moodle1_mod_choice_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/CHOICE 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                'choice', '/MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE',
56                array(
57                    'renamefields' => array(
58                        'text' => 'intro',
59                        'format' => 'introformat',
60                    ),
61                    'newfields' => array(
62                        'completionsubmit' => 0,
63                    ),
64                    'dropfields' => array(
65                        'modtype'
66                    ),
67                )
68            ),
69            new convert_path('choice_options', '/MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE/OPTIONS'),
70            new convert_path('choice_option', '/MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE/OPTIONS/OPTION'),
71        );
72    }
73
74    /**
75     * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE
76     * data available
77     */
78    public function process_choice($data) {
79
80        // get the course module id and context id
81        $instanceid     = $data['id'];
82        $cminfo         = $this->get_cminfo($instanceid);
83        $this->moduleid = $cminfo['id'];
84        $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
85
86        // get a fresh new file manager for this instance
87        $this->fileman = $this->converter->get_file_manager($contextid, 'mod_choice');
88
89        // convert course files embedded into the intro
90        $this->fileman->filearea = 'intro';
91        $this->fileman->itemid   = 0;
92        $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
93
94        // start writing choice.xml
95        $this->open_xml_writer("activities/choice_{$this->moduleid}/choice.xml");
96        $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
97            'modulename' => 'choice', 'contextid' => $contextid));
98        $this->xmlwriter->begin_tag('choice', array('id' => $instanceid));
99
100        foreach ($data as $field => $value) {
101            if ($field <> 'id') {
102                $this->xmlwriter->full_tag($field, $value);
103            }
104        }
105
106        return $data;
107    }
108
109    /**
110     * This is executed when the parser reaches the <OPTIONS> opening element
111     */
112    public function on_choice_options_start() {
113        $this->xmlwriter->begin_tag('options');
114    }
115
116    /**
117     * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE/OPTIONS/OPTION
118     * data available
119     */
120    public function process_choice_option($data) {
121        $this->write_xml('option', $data, array('/option/id'));
122    }
123
124    /**
125     * This is executed when the parser reaches the closing </OPTIONS> element
126     */
127    public function on_choice_options_end() {
128        $this->xmlwriter->end_tag('options');
129    }
130
131    /**
132     * This is executed when we reach the closing </MOD> tag of our 'choice' path
133     */
134    public function on_choice_end() {
135        // finalize choice.xml
136        $this->xmlwriter->end_tag('choice');
137        $this->xmlwriter->end_tag('activity');
138        $this->close_xml_writer();
139
140        // write inforef.xml
141        $this->open_xml_writer("activities/choice_{$this->moduleid}/inforef.xml");
142        $this->xmlwriter->begin_tag('inforef');
143        $this->xmlwriter->begin_tag('fileref');
144        foreach ($this->fileman->get_fileids() as $fileid) {
145            $this->write_xml('file', array('id' => $fileid));
146        }
147        $this->xmlwriter->end_tag('fileref');
148        $this->xmlwriter->end_tag('inforef');
149        $this->close_xml_writer();
150    }
151}
152