3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Provides support for the conversion of moodle1 backup to the moodle2 format
23 * @copyright 2011 Rossiani Wijaya <rwijaya@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 * Lesson conversion handler
32 class moodle1_mod_lesson_handler
extends moodle1_mod_handler
{
33 // @var array of answers, when there are more that 4 answers, we need to fix <jumpto>.
36 // @var stdClass a page object of the current page
38 // @var array of page objects to store entire pages, to help generate nextpageid and prevpageid in data
41 /** @var moodle1_file_manager */
42 protected $fileman = null;
45 protected $moduleid = null;
48 * Declare the paths in moodle.xml we are able to convert
50 * The method returns list of {@link convert_path} instances.
51 * For each path returned, the corresponding conversion method must be
54 * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON does not
55 * actually exist in the file. The last element with the module name was
56 * appended by the moodle1_converter class.
58 * @return array of {@link convert_path} instances
60 public function get_paths() {
63 'lesson', '/MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON',
65 'renamefields' => array(
66 'usegrademax' => 'usemaxgrade',
71 'lesson_page', '/MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON/PAGES/PAGE',
74 'contentsformat' => FORMAT_MOODLE
,
75 'nextpageid' => 0, //set to default to the next sequencial page in process_lesson_page()
81 'lesson_pages', '/MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON/PAGES'
84 'lesson_answer', '/MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON/PAGES/PAGE/ANSWERS/ANSWER',
88 'responseformat' => 0,
90 'renamefields' => array(
91 'answertext' => 'answer_text',
99 * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON
102 public function process_lesson($data) {
104 // get the course module id and context id
105 $instanceid = $data['id'];
106 $cminfo = $this->get_cminfo($instanceid);
107 $this->moduleid
= $cminfo['id'];
108 $contextid = $this->converter
->get_contextid(CONTEXT_MODULE
, $this->moduleid
);
110 // get a fresh new file manager for this instance
111 $this->fileman
= $this->converter
->get_file_manager($contextid, 'mod_lesson');
113 // migrate referenced local media files
114 if (!empty($data['mediafile']) and strpos($data['mediafile'], '://') === false) {
115 $this->fileman
->filearea
= 'mediafile';
116 $this->fileman
->itemid
= 0;
118 $this->fileman
->migrate_file('course_files/'.$data['mediafile']);
119 } catch (moodle1_convert_exception
$e) {
120 // the file probably does not exist
121 $this->log('error migrating lesson mediafile', backup
::LOG_WARNING
, 'course_files/'.$data['mediafile']);
125 // start writing lesson.xml
126 $this->open_xml_writer("activities/lesson_{$this->moduleid}/lesson.xml");
127 $this->xmlwriter
->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid
,
128 'modulename' => 'lesson', 'contextid' => $contextid));
129 $this->xmlwriter
->begin_tag('lesson', array('id' => $instanceid));
132 foreach ($data as $field => $value) {
133 $this->xmlwriter
->full_tag($field, $value);
139 public function on_lesson_pages_start() {
140 $this->xmlwriter
->begin_tag('pages');
144 * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON/PAGES/PAGE
147 public function process_lesson_page($data) {
150 // replay the upgrade step 2009120801
151 if ($CFG->texteditors
!== 'textarea') {
152 $data['contents'] = text_to_html($data['contents'], false, false, true);
153 $data['contentsformat'] = FORMAT_HTML
;
156 // store page in pages
157 $this->page
= new stdClass();
158 $this->page
->id
= $data['pageid'];
159 unset($data['pageid']);
160 $this->page
->data
= $data;
164 * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON/PAGES/PAGE/ANSWERS/ANSWER
167 public function process_lesson_answer($data) {
169 // replay the upgrade step 2010072003
170 $flags = intval($data['flags']);
172 $data['answer_text'] = text_to_html($data['answer_text'], false, false, true);
173 $data['answerformat'] = FORMAT_HTML
;
176 $data['response'] = text_to_html($data['response'], false, false, true);
177 $data['responseformat'] = FORMAT_HTML
;
180 // buffer for conversion of <jumpto> in line with
181 // upgrade step 2010121400 from mod/lesson/db/upgrade.php
182 $this->answers
[] = $data;
185 public function on_lesson_page_end() {
186 $this->page
->answers
= $this->answers
;
187 unset($this->answers
);
188 $this->pages
[] = $this->page
;
192 public function on_lesson_pages_end() {
195 foreach ($this->pages
as $page) {
196 $curpageid = $page->id
;
197 // if there are more pages, set nextpageid
198 if (isset($this->pages
[1])) {
199 $nextpage = $this->pages
[1];
200 $nextpageid = $nextpage->id
;
202 //theres only one page left
205 //mince nextpageid and prevpageid
206 $page->data
['nextpageid'] = $nextpageid;
207 $page->data
['prevpageid'] = $prevpageid;
209 array_shift($this->pages
);
211 // write out each page data
212 $this->xmlwriter
->begin_tag('page', array('id' => $page->id
));
214 foreach ($page->data
as $field => $value) {
215 $this->xmlwriter
->full_tag($field, $value);
218 //effectively on_lesson_answers_end(), where we write out answers for current page.
219 $answers = $page->answers
;
221 $this->xmlwriter
->begin_tag('answers');
222 if (count($answers) > 3) {
223 if ($answers[0]['jumpto'] !== '0' ||
$answers[1]['jumpto'] !== '0') {
224 if ($answers[2]['jumpto'] !== '0') {
225 $answers[0]['jumpto'] = $answers[2]['jumpto'];
226 $answers[2]['jumpto'] = '0';
228 if ($answers[3]['jumpto'] !== '0') {
229 $answers[1]['jumpto'] = $answers[3]['jumpto'];
230 $answers[3]['jumpto'] = '0';
235 foreach ($answers as $data) {
236 $this->write_xml('answer', $data, array('/answer/id'));
238 $this->xmlwriter
->end_tag('answers');
240 // answers is now closed for current page. Ending the page.
241 $this->xmlwriter
->end_tag('page');
244 $prevpageid = $curpageid;
246 $this->xmlwriter
->end_tag('pages');
250 * This is executed when we reach the closing </MOD> tag of our 'lesson' path
252 public function on_lesson_end() {
253 // finish writing lesson.xml
254 $this->xmlwriter
->end_tag('lesson');
255 $this->xmlwriter
->end_tag('activity');
256 $this->close_xml_writer();
259 $this->open_xml_writer("activities/lesson_{$this->moduleid}/inforef.xml");
260 $this->xmlwriter
->begin_tag('inforef');
261 $this->xmlwriter
->begin_tag('fileref');
262 foreach ($this->fileman
->get_fileids() as $fileid) {
263 $this->write_xml('file', array('id' => $fileid));
265 $this->xmlwriter
->end_tag('fileref');
266 $this->xmlwriter
->end_tag('inforef');
267 $this->close_xml_writer();