MDL-24177 backup - fix page next/prev ids to be propely mapped
[moodle.git] / mod / lesson / backup / moodle2 / restore_lesson_stepslib.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  * @package moodlecore
20  * @subpackage backup-moodle2
21  * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 /**
26  * Define all the restore steps that will be used by the restore_lesson_activity_task
27  */
29 /**
30  * Structure step to restore one lesson activity
31  */
32 class restore_lesson_activity_structure_step extends restore_activity_structure_step {
34     protected function define_structure() {
36         $paths = array();
37         $userinfo = $this->get_setting_value('userinfo');
39         $paths[] = new restore_path_element('lesson', '/activity/lesson');
40         $paths[] = new restore_path_element('lesson_page', '/activity/lesson/pages/page');
41         $paths[] = new restore_path_element('lesson_answer', '/activity/lesson/pages/page/answers/answer');
42         if ($userinfo) {
43             $paths[] = new restore_path_element('lesson_attempt', '/activity/lesson/pages/page/answers/answer/attempts/attempt');
44             $paths[] = new restore_path_element('lesson_grade', '/activity/lesson/grades/grade');
45             $paths[] = new restore_path_element('lesson_branch', '/activity/lesson/pages/page/branches/branch');
46             $paths[] = new restore_path_element('lesson_highscore', '/activity/lesson/highscores/highscore');
47             $paths[] = new restore_path_element('lesson_timer', '/activity/lesson/timers/timer');
48         }
50         // Return the paths wrapped into standard activity structure
51         return $this->prepare_activity_structure($paths);
52     }
54     protected function process_lesson($data) {
55         global $DB;
57         $data = (object)$data;
58         $oldid = $data->id;
59         $data->course = $this->get_courseid();
61         $data->available = $this->apply_date_offset($data->available);
62         $data->deadline = $this->apply_date_offset($data->deadline);
63         $data->timemodified = $this->apply_date_offset($data->timemodified);
65         // insert the lesson record
66         $newitemid = $DB->insert_record('lesson', $data);
67         // inmediately after inserting "activity" record, call this
68         $this->apply_activity_instance($newitemid);
69     }
71     protected function process_lesson_page($data) {
72         global $DB;
74         $data = (object)$data;
75         $oldid = $data->id;
76         $data->lessonid = $this->get_new_parentid('lesson');
78         // We'll remap all the prevpageid and nextpageid at the end, once all pages have been created
79         $data->timemodified = $this->apply_date_offset($data->timemodified);
80         $data->timecreated = $this->apply_date_offset($data->timecreated);
82         $newitemid = $DB->insert_record('lesson_pages', $data);
83         $this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas
85         //now update previous page with newitemid as the nextpageid
86         if (!empty($data->prevpageid)) {
87             $DB->set_field('lesson_pages', 'nextpageid', $newitemid, array('id' => $data->prevpageid));
88         }
89     }
91     protected function process_lesson_answer($data) {
92         global $DB;
94         $data = (object)$data;
95         $oldid = $data->id;
96         $data->lessonid = $this->get_new_parentid('lesson');
97         $data->pageid = $this->get_new_parentid('lesson_page');
98         $data->answer = $data->answer_text;
99         $data->timemodified = $this->apply_date_offset($data->timemodified);
100         $data->timecreated = $this->apply_date_offset($data->timecreated);
102         $newitemid = $DB->insert_record('lesson_answers', $data);
103         $this->set_mapping('lesson_answer', $oldid, $newitemid);
104     }
106     protected function process_lesson_attempt($data) {
107         global $DB;
109         $data = (object)$data;
110         $oldid = $data->id;
111         $data->lessonid = $this->get_new_parentid('lesson');
112         $data->pageid = $this->get_new_parentid('lesson_page');
113         $data->answerid = $this->get_new_parentid('lesson_answer');
114         $data->userid = $this->get_mappingid('user', $data->userid);
115         $data->timeseen = $this->apply_date_offset($data->timeseen);
117         $newitemid = $DB->insert_record('lesson_attempts', $data);
118     }
120     protected function process_lesson_grade($data) {
121         global $DB;
123         $data = (object)$data;
124         $oldid = $data->id;
125         $data->lessonid = $this->get_new_parentid('lesson');
126         $data->userid = $this->get_mappingid('user', $data->userid);
127         $data->completed = $this->apply_date_offset($data->completed);
129         $newitemid = $DB->insert_record('lesson_grades', $data);
130         $this->set_mapping('lesson_grade', $oldid, $newitemid);
131     }
133     protected function process_lesson_branch($data) {
134         global $DB;
136         $data = (object)$data;
137         $oldid = $data->id;
138         $data->lessonid = $this->get_new_parentid('lesson');
139         $data->pageid = $this->get_new_parentid('lesson_page');
140         $data->userid = $this->get_mappingid('user', $data->userid);
141         $data->timeseen = $this->apply_date_offset($data->timeseen);
143         $newitemid = $DB->insert_record('lesson_branch', $data);
144     }
146     protected function process_lesson_highscore($data) {
147         global $DB;
149         $data = (object)$data;
150         $oldid = $data->id;
151         $data->lessonid = $this->get_new_parentid('lesson');
152         $data->userid = $this->get_mappingid('user', $data->userid);
153         $data->gradeid = $this->get_mappingid('lesson_grade', $data->gradeid);
155         $newitemid = $DB->insert_record('lesson_high_scores', $data);
156     }
158     protected function process_lesson_timer($data) {
159         global $DB;
161         $data = (object)$data;
162         $oldid = $data->id;
163         $data->lessonid = $this->get_new_parentid('lesson');
164         $data->userid = $this->get_mappingid('user', $data->userid);
165         $data->starttime = $this->apply_date_offset($data->starttime);
166         $data->lessontime = $this->apply_date_offset($data->lessontime);
168         $newitemid = $DB->insert_record('lesson_timer', $data);
169     }
171     protected function after_execute() {
172         global $DB;
174         // Add lesson mediafile, no need to match by itemname (just internally handled context)
175         $this->add_related_files('mod_lesson', 'mediafile', null);
176         // Add lesson page files, by lesson_page itemname
177         $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
179         // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
180         $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
181                                  '', 'id, prevpageid, nextpageid');
182         foreach ($rs as $page) {
183             $page->prevpageid = (empty($page->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->prevpageid);
184             $page->nextpageid = (empty($page->nextpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->nextpageid);
185             $DB->update_record('lesson_pages', $page);
186         }
187         $rs->close();
189         // TODO: somewhere at the end of the restore... when all the activities have been restored
190         // TODO: we need to decode the lesson->activitylink that points to another activity in the course
191         // TODO: great functionality that breaks self-contained principles, grrr
192     }