5930e1938d33855c060debfeea3a38b477670996
[moodle.git] / mod / assignment / backup / moodle1 / lib.php
1 <?php
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/>.
18 /**
19  * Provides support for the conversion of moodle1 backup to the moodle2 format
20  * Based off of a template @ http://docs.moodle.org/en/Development:Backup_1.9_conversion_for_developers
21  *
22  * @package    mod
23  * @subpackage assignment
24  * @copyright  2011 Aparup Banerjee <aparup@moodle.com>
25  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26  */
28 defined('MOODLE_INTERNAL') || die();
30 /**
31  * Assignment conversion handler
32  */
33 class moodle1_mod_assignment_handler extends moodle1_mod_handler {
35     /** @var moodle1_file_manager */
36     protected $fileman = null;
38     /** @var int cmid */
39     protected $moduleid = null;
41     /**
42      * Declare the paths in moodle.xml we are able to convert
43      *
44      * The method returns list of {@link convert_path} instances.
45      * For each path returned, the corresponding conversion method must be
46      * defined.
47      *
48      * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT does not
49      * actually exist in the file. The last element with the module name was
50      * appended by the moodle1_converter class.
51      *
52      * @return array of {@link convert_path} instances
53      */
54     public function get_paths() {
55         return array(
56             new convert_path(
57                 'assignment', '/MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT',
58                 array(
59                     'renamefields' => array(
60                         'description' => 'intro',
61                         'format' => 'introformat',
62                     )
63                 )
64             )
65             //@todo process user data
66             //new convert_path('assignment_submission', '/MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT/SUBMISSIONS/SUBMISSION')
67         );
68     }
70     /**
71      * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT
72      * data available
73      */
74     public function process_assignment($data) {
75         // get the course module id and context id
76         $instanceid     = $data['id'];
77         $cminfo         = $this->get_cminfo($instanceid);
78         $this->moduleid = $cminfo['id'];
79         $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
81         // get a fresh new file manager for this instance
82         $this->fileman = $this->converter->get_file_manager($contextid, 'mod_assignment');
84         // convert course files embedded into the intro
85         $this->fileman->filearea = 'intro';
86         $this->fileman->itemid   = 0;
87         $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
89         // start writing assignment.xml
90         $this->open_xml_writer("activities/assignment_{$this->moduleid}/assignment.xml");
91         $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
92             'modulename' => 'assignment', 'contextid' => $contextid));
93         $this->xmlwriter->begin_tag('assignment', array('id' => $instanceid));
95         foreach ($data as $field => $value) {
96             if ($field <> 'id') {
97                 $this->xmlwriter->full_tag($field, $value);
98             }
99         }
101         $this->xmlwriter->begin_tag('submissions');
103         return $data;
104     }
106     /**
107      * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT/SUBMISSIONS/SUBMISSION
108      * data available
109      */
110     public function process_assignment_submission($data) {
111         //@todo process user data
112         //$this->write_xml('submission', $data, array('/submission/id'));
113     }
115     /**
116      * This is executed when we reach the closing </MOD> tag of our 'assignment' path
117      */
118     public function on_assignment_end() {
119         // finish writing assignment.xml
120         $this->xmlwriter->end_tag('submissions');
121         $this->xmlwriter->end_tag('assignment');
122         $this->xmlwriter->end_tag('activity');
123         $this->close_xml_writer();
125         // write inforef.xml
126         $this->open_xml_writer("activities/assignment_{$this->moduleid}/inforef.xml");
127         $this->xmlwriter->begin_tag('inforef');
128         $this->xmlwriter->begin_tag('fileref');
129         foreach ($this->fileman->get_fileids() as $fileid) {
130             $this->write_xml('file', array('id' => $fileid));
131         }
132         $this->xmlwriter->end_tag('fileref');
133         $this->xmlwriter->end_tag('inforef');
134         $this->close_xml_writer();
135     }