MDL-27960 assignment subplugin moodle1 conversion handling and handlers added
[moodle.git] / mod / assignment / backup / moodle1 / lib.php
CommitLineData
be53cf4e
AB
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 * 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 */
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * Assignment conversion handler
32 */
33class moodle1_mod_assignment_handler extends moodle1_mod_handler {
34
c5c315e2
DM
35 /** @var moodle1_file_manager */
36 protected $fileman = null;
37
38 /** @var int cmid */
39 protected $moduleid = null;
40
b74b347a
AB
41 /** @var string current subplugin being processed*/
42 private $currentsubpluginname = null;
43
44 /** @var array of a moodle1_assignment_[subplugin_name]_handler instances */
45 private $subpluginhandlers = null;
46
be53cf4e
AB
47 /**
48 * Declare the paths in moodle.xml we are able to convert
49 *
50 * The method returns list of {@link convert_path} instances.
51 * For each path returned, the corresponding conversion method must be
52 * defined.
53 *
54 * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT does not
55 * actually exist in the file. The last element with the module name was
56 * appended by the moodle1_converter class.
57 *
58 * @return array of {@link convert_path} instances
59 */
60 public function get_paths() {
61 return array(
62 new convert_path(
63 'assignment', '/MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT',
64 array(
65 'renamefields' => array(
66 'description' => 'intro',
67 'format' => 'introformat',
68 )
69 )
70 )
71 //@todo process user data
72 //new convert_path('assignment_submission', '/MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT/SUBMISSIONS/SUBMISSION')
73 );
74 }
75
76 /**
77 * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT
78 * data available
79 */
80 public function process_assignment($data) {
81 // get the course module id and context id
c5c315e2
DM
82 $instanceid = $data['id'];
83 $cminfo = $this->get_cminfo($instanceid);
84 $this->moduleid = $cminfo['id'];
85 $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
86
b74b347a
AB
87 //store assignment type for possible subplugin conversions.
88 $this->currentsubpluginname = $data['assignmenttype'];
89
c5c315e2
DM
90 // get a fresh new file manager for this instance
91 $this->fileman = $this->converter->get_file_manager($contextid, 'mod_assignment');
92
93 // convert course files embedded into the intro
94 $this->fileman->filearea = 'intro';
95 $this->fileman->itemid = 0;
96 $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
97
98 // start writing assignment.xml
99 $this->open_xml_writer("activities/assignment_{$this->moduleid}/assignment.xml");
100 $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
be53cf4e
AB
101 'modulename' => 'assignment', 'contextid' => $contextid));
102 $this->xmlwriter->begin_tag('assignment', array('id' => $instanceid));
103
be53cf4e 104 foreach ($data as $field => $value) {
c5c315e2
DM
105 if ($field <> 'id') {
106 $this->xmlwriter->full_tag($field, $value);
107 }
be53cf4e
AB
108 }
109
b74b347a
AB
110 //after writing the assignment type element, let the subplugin add on whatever it wants.
111 $this->handle_assignment_subplugin($data);
112
be53cf4e 113 $this->xmlwriter->begin_tag('submissions');
c5c315e2
DM
114
115 return $data;
be53cf4e
AB
116 }
117
118 /**
119 * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT/SUBMISSIONS/SUBMISSION
120 * data available
121 */
122 public function process_assignment_submission($data) {
123 //@todo process user data
124 //$this->write_xml('submission', $data, array('/submission/id'));
125 }
126
b74b347a
AB
127 /**
128 * This handles calls to subplugin conversion classes.
129 * called from <ASSIGNMENTTYPE> within process_assignment()
130 */
131 public function handle_assignment_subplugin($data) {
132 $handler = $this->get_subplugin_handler($this->currentsubpluginname);
133 $this->log('Instantiated assignment subplugin handler for '.$this->currentsubpluginname.'.', backup::LOG_DEBUG);
134 $handler->use_xml_writer($this->xmlwriter);
135
136 $this->log('Processing assignment subplugin handler callback for '.$this->currentsubpluginname.'.', backup::LOG_DEBUG);
137 $handler->append_subplugin_data($data);
138 }
139
be53cf4e
AB
140 /**
141 * This is executed when we reach the closing </MOD> tag of our 'assignment' path
142 */
143 public function on_assignment_end() {
c5c315e2 144 // finish writing assignment.xml
be53cf4e
AB
145 $this->xmlwriter->end_tag('submissions');
146 $this->xmlwriter->end_tag('assignment');
147 $this->xmlwriter->end_tag('activity');
148 $this->close_xml_writer();
c5c315e2
DM
149
150 // write inforef.xml
151 $this->open_xml_writer("activities/assignment_{$this->moduleid}/inforef.xml");
152 $this->xmlwriter->begin_tag('inforef');
153 $this->xmlwriter->begin_tag('fileref');
154 foreach ($this->fileman->get_fileids() as $fileid) {
155 $this->write_xml('file', array('id' => $fileid));
156 }
157 $this->xmlwriter->end_tag('fileref');
158 $this->xmlwriter->end_tag('inforef');
159 $this->close_xml_writer();
be53cf4e 160 }
b74b347a
AB
161
162 /// internal implementation details follow /////////////////////////////////
163
164 /**
165 * Factory method returning the handler of the given assignment subplugin
166 *
167 * @param string $subplugin the name of the subplugin
168 * @throws moodle1_convert_exception
169 * @return moodle1_assignment_subplugin_handler the instance of the handler
170 */
171 protected function get_subplugin_handler($subplugin) {
172 global $CFG; // we include other files here
173
174 if (is_null($this->subpluginhandlers)) {
175 $this->subpluginhandlers = array();
176 $subplugins = get_plugin_list('assignment');
177 foreach ($subplugins as $name => $dir) {
178 $handlerfile = $dir.'/backup/moodle1/lib.php';
179 $handlerclass = "moodle1_mod_assignment_{$name}_subplugin_handler";
180 if (!file_exists($handlerfile)) {
181 continue;
182 }
183 require_once($handlerfile);
184
185 if (!class_exists($handlerclass)) {
186 throw new moodle1_convert_exception('missing_handler_class', $handlerclass);
187 }
188 $this->log('preparing assignment subplugin handler', backup::LOG_DEBUG, $handlerclass);
189 $this->subpluginhandlers[$name] = new $handlerclass($this, $name);
190 if (!$this->subpluginhandlers[$name] instanceof moodle1_assignment_subplugin_handler) {
191 throw new moodle1_convert_exception('wrong_handler_class', get_class($this->subpluginhandlers[$name]));
192 }
193 }
194 }
195
196 if (!isset($this->subpluginhandlers[$subplugin])) {
197 throw new moodle1_convert_exception('unsupported_subplugin', 'assignment_'.$subplugin);
198 }
199
200 return $this->subpluginhandlers[$subplugin];
201 }
be53cf4e 202}
b74b347a
AB
203
204
205/**
206 * Base class for the assignment subplugin handler
207 * Extend this for your own subplugin conversion handling purposes.
208 */
209abstract class moodle1_assignment_subplugin_handler extends moodle1_submod_handler {
210
211 /**
212 * @param moodle1_mod_handler $assignmenthandler the handler of a module we are subplugin of
213 * @param string $subpluginname the name of the subplugin
214 */
215 public function __construct(moodle1_mod_handler $assignmenthandler, $subpluginname) {
216 parent::__construct($assignmenthandler, 'assignment', $subpluginname);
217 }
218
219 /**
220 * Provides a xml_writer instance to this assignment subplugin type handler
221 *
222 * @param xml_writer $xmlwriter
223 */
224 public function use_xml_writer(xml_writer $xmlwriter) {
225 $this->xmlwriter = $xmlwriter;
226 }
227
228 /**
229 * a call back (entry point) to the subplugin conversion handler class.
230 * $data are the elements of <assignment>, any (@todo sub paths containing subplugindata isn't handed through).
231 */
232
233 public function append_subplugin_data($data) {
234 // an example that does nothing - you'll do nothing if you don't overide it
235 return false;
236
237 //you will probably want to do stuff with $this->xmlwriter here (within your overridden method) to write plugin specific data.
238 }
239}