weekly release 2.3dev
[moodle.git] / mod / lesson / backup / moodle2 / restore_lesson_stepslib.php
CommitLineData
a6f34943
DM
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 * @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 */
24
25/**
26 * Define all the restore steps that will be used by the restore_lesson_activity_task
27 */
28
29/**
30 * Structure step to restore one lesson activity
31 */
32class restore_lesson_activity_structure_step extends restore_activity_structure_step {
33
34 protected function define_structure() {
35
36 $paths = array();
37 $userinfo = $this->get_setting_value('userinfo');
38
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 }
49
50 // Return the paths wrapped into standard activity structure
51 return $this->prepare_activity_structure($paths);
52 }
53
54 protected function process_lesson($data) {
55 global $DB;
56
57 $data = (object)$data;
58 $oldid = $data->id;
59 $data->course = $this->get_courseid();
60
61 $data->available = $this->apply_date_offset($data->available);
62 $data->deadline = $this->apply_date_offset($data->deadline);
53ab8106 63 $data->timemodified = $this->apply_date_offset($data->timemodified);
a6f34943 64
7cff4927
EL
65 // lesson->highscores can come both in data->highscores and
66 // data->showhighscores, handle both. MDL-26229
67 if (isset($data->showhighscores)) {
68 $data->highscores = $data->showhighscores;
69 unset($data->showhighscores);
70 }
71
a6f34943
DM
72 // insert the lesson record
73 $newitemid = $DB->insert_record('lesson', $data);
034ef761 74 // immediately after inserting "activity" record, call this
a6f34943
DM
75 $this->apply_activity_instance($newitemid);
76 }
77
78 protected function process_lesson_page($data) {
79 global $DB;
80
81 $data = (object)$data;
82 $oldid = $data->id;
83 $data->lessonid = $this->get_new_parentid('lesson');
3643aadf
EL
84
85 // We'll remap all the prevpageid and nextpageid at the end, once all pages have been created
53ab8106
DM
86 $data->timemodified = $this->apply_date_offset($data->timemodified);
87 $data->timecreated = $this->apply_date_offset($data->timecreated);
a6f34943
DM
88
89 $newitemid = $DB->insert_record('lesson_pages', $data);
113065d2 90 $this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas
a6f34943
DM
91 }
92
93 protected function process_lesson_answer($data) {
94 global $DB;
95
96 $data = (object)$data;
97 $oldid = $data->id;
98 $data->lessonid = $this->get_new_parentid('lesson');
99 $data->pageid = $this->get_new_parentid('lesson_page');
100 $data->answer = $data->answer_text;
53ab8106
DM
101 $data->timemodified = $this->apply_date_offset($data->timemodified);
102 $data->timecreated = $this->apply_date_offset($data->timecreated);
a6f34943
DM
103
104 $newitemid = $DB->insert_record('lesson_answers', $data);
105 $this->set_mapping('lesson_answer', $oldid, $newitemid);
106 }
107
108 protected function process_lesson_attempt($data) {
109 global $DB;
110
111 $data = (object)$data;
112 $oldid = $data->id;
113 $data->lessonid = $this->get_new_parentid('lesson');
114 $data->pageid = $this->get_new_parentid('lesson_page');
115 $data->answerid = $this->get_new_parentid('lesson_answer');
116 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 117 $data->timeseen = $this->apply_date_offset($data->timeseen);
a6f34943
DM
118
119 $newitemid = $DB->insert_record('lesson_attempts', $data);
120 }
121
122 protected function process_lesson_grade($data) {
123 global $DB;
124
125 $data = (object)$data;
126 $oldid = $data->id;
127 $data->lessonid = $this->get_new_parentid('lesson');
128 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 129 $data->completed = $this->apply_date_offset($data->completed);
a6f34943
DM
130
131 $newitemid = $DB->insert_record('lesson_grades', $data);
132 $this->set_mapping('lesson_grade', $oldid, $newitemid);
133 }
134
135 protected function process_lesson_branch($data) {
136 global $DB;
137
138 $data = (object)$data;
139 $oldid = $data->id;
140 $data->lessonid = $this->get_new_parentid('lesson');
141 $data->pageid = $this->get_new_parentid('lesson_page');
142 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 143 $data->timeseen = $this->apply_date_offset($data->timeseen);
a6f34943
DM
144
145 $newitemid = $DB->insert_record('lesson_branch', $data);
a6f34943
DM
146 }
147
148 protected function process_lesson_highscore($data) {
149 global $DB;
150
151 $data = (object)$data;
152 $oldid = $data->id;
153 $data->lessonid = $this->get_new_parentid('lesson');
154 $data->userid = $this->get_mappingid('user', $data->userid);
155 $data->gradeid = $this->get_mappingid('lesson_grade', $data->gradeid);
156
157 $newitemid = $DB->insert_record('lesson_high_scores', $data);
158 }
159
160 protected function process_lesson_timer($data) {
161 global $DB;
162
163 $data = (object)$data;
164 $oldid = $data->id;
165 $data->lessonid = $this->get_new_parentid('lesson');
166 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2
EL
167 $data->starttime = $this->apply_date_offset($data->starttime);
168 $data->lessontime = $this->apply_date_offset($data->lessontime);
a6f34943
DM
169
170 $newitemid = $DB->insert_record('lesson_timer', $data);
171 }
172
173 protected function after_execute() {
3643aadf
EL
174 global $DB;
175
113065d2
EL
176 // Add lesson mediafile, no need to match by itemname (just internally handled context)
177 $this->add_related_files('mod_lesson', 'mediafile', null);
178 // Add lesson page files, by lesson_page itemname
179 $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
180
3643aadf
EL
181 // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
182 $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
183 '', 'id, prevpageid, nextpageid');
184 foreach ($rs as $page) {
185 $page->prevpageid = (empty($page->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->prevpageid);
186 $page->nextpageid = (empty($page->nextpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->nextpageid);
187 $DB->update_record('lesson_pages', $page);
188 }
189 $rs->close();
190
bd9e6629
EL
191 // Remap all the restored 'jumpto' fields now that we have all the pages and their mappings
192 $rs = $DB->get_recordset('lesson_answers', array('lessonid' => $this->task->get_activityid()),
193 '', 'id, jumpto');
194 foreach ($rs as $answer) {
195 if ($answer->jumpto > 0) {
196 $answer->jumpto = $this->get_mappingid('lesson_page', $answer->jumpto);
197 $DB->update_record('lesson_answers', $answer);
198 }
199 }
200 $rs->close();
201
113065d2
EL
202 // TODO: somewhere at the end of the restore... when all the activities have been restored
203 // TODO: we need to decode the lesson->activitylink that points to another activity in the course
204 // TODO: great functionality that breaks self-contained principles, grrr
a6f34943
DM
205 }
206}