weekly release 2.9dev
[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/**
9b24f68b 19 * @package mod_lesson
a6f34943
DM
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 {
b9e7d892
ARN
33 // Store the answers as they're received but only process them at the
34 // end of the lesson
35 protected $answers = array();
a6f34943
DM
36
37 protected function define_structure() {
38
39 $paths = array();
40 $userinfo = $this->get_setting_value('userinfo');
41
42 $paths[] = new restore_path_element('lesson', '/activity/lesson');
43 $paths[] = new restore_path_element('lesson_page', '/activity/lesson/pages/page');
44 $paths[] = new restore_path_element('lesson_answer', '/activity/lesson/pages/page/answers/answer');
45 if ($userinfo) {
46 $paths[] = new restore_path_element('lesson_attempt', '/activity/lesson/pages/page/answers/answer/attempts/attempt');
47 $paths[] = new restore_path_element('lesson_grade', '/activity/lesson/grades/grade');
48 $paths[] = new restore_path_element('lesson_branch', '/activity/lesson/pages/page/branches/branch');
49 $paths[] = new restore_path_element('lesson_highscore', '/activity/lesson/highscores/highscore');
50 $paths[] = new restore_path_element('lesson_timer', '/activity/lesson/timers/timer');
51 }
52
53 // Return the paths wrapped into standard activity structure
54 return $this->prepare_activity_structure($paths);
55 }
56
57 protected function process_lesson($data) {
58 global $DB;
59
60 $data = (object)$data;
61 $oldid = $data->id;
62 $data->course = $this->get_courseid();
63
64 $data->available = $this->apply_date_offset($data->available);
65 $data->deadline = $this->apply_date_offset($data->deadline);
53ab8106 66 $data->timemodified = $this->apply_date_offset($data->timemodified);
a6f34943 67
7cff4927
EL
68 // lesson->highscores can come both in data->highscores and
69 // data->showhighscores, handle both. MDL-26229
70 if (isset($data->showhighscores)) {
71 $data->highscores = $data->showhighscores;
72 unset($data->showhighscores);
73 }
74
25345cb4
JMV
75 // Supply item that maybe missing from previous versions.
76 if (!isset($data->completionendreached)) {
77 $data->completionendreached = 0;
78 }
79
a6f34943
DM
80 // insert the lesson record
81 $newitemid = $DB->insert_record('lesson', $data);
034ef761 82 // immediately after inserting "activity" record, call this
a6f34943
DM
83 $this->apply_activity_instance($newitemid);
84 }
85
86 protected function process_lesson_page($data) {
87 global $DB;
88
89 $data = (object)$data;
90 $oldid = $data->id;
91 $data->lessonid = $this->get_new_parentid('lesson');
3643aadf
EL
92
93 // We'll remap all the prevpageid and nextpageid at the end, once all pages have been created
53ab8106
DM
94 $data->timemodified = $this->apply_date_offset($data->timemodified);
95 $data->timecreated = $this->apply_date_offset($data->timecreated);
a6f34943
DM
96
97 $newitemid = $DB->insert_record('lesson_pages', $data);
113065d2 98 $this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas
a6f34943
DM
99 }
100
101 protected function process_lesson_answer($data) {
102 global $DB;
103
104 $data = (object)$data;
a6f34943
DM
105 $data->lessonid = $this->get_new_parentid('lesson');
106 $data->pageid = $this->get_new_parentid('lesson_page');
107 $data->answer = $data->answer_text;
53ab8106
DM
108 $data->timemodified = $this->apply_date_offset($data->timemodified);
109 $data->timecreated = $this->apply_date_offset($data->timecreated);
a6f34943 110
38a8779d
ARN
111 // Set a dummy mapping to get the old ID so that it can be used by get_old_parentid when
112 // processing attempts. It will be corrected in after_execute
0abc18cf 113 $this->set_mapping('lesson_answer', $data->id, 0, true); // Has related fileareas.
38a8779d 114
b9e7d892
ARN
115 // Answers need to be processed in order, so we store them in an
116 // instance variable and insert them in the after_execute stage
117 $this->answers[$data->id] = $data;
a6f34943
DM
118 }
119
120 protected function process_lesson_attempt($data) {
121 global $DB;
122
123 $data = (object)$data;
124 $oldid = $data->id;
125 $data->lessonid = $this->get_new_parentid('lesson');
126 $data->pageid = $this->get_new_parentid('lesson_page');
38a8779d
ARN
127
128 // We use the old answerid here as the answer isn't created until after_execute
129 $data->answerid = $this->get_old_parentid('lesson_answer');
a6f34943 130 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 131 $data->timeseen = $this->apply_date_offset($data->timeseen);
a6f34943
DM
132
133 $newitemid = $DB->insert_record('lesson_attempts', $data);
ebf5ad4f 134 $this->set_mapping('lesson_attempt', $oldid, $newitemid, true); // Has related fileareas.
a6f34943
DM
135 }
136
137 protected function process_lesson_grade($data) {
138 global $DB;
139
140 $data = (object)$data;
141 $oldid = $data->id;
142 $data->lessonid = $this->get_new_parentid('lesson');
143 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 144 $data->completed = $this->apply_date_offset($data->completed);
a6f34943
DM
145
146 $newitemid = $DB->insert_record('lesson_grades', $data);
147 $this->set_mapping('lesson_grade', $oldid, $newitemid);
148 }
149
150 protected function process_lesson_branch($data) {
151 global $DB;
152
153 $data = (object)$data;
154 $oldid = $data->id;
155 $data->lessonid = $this->get_new_parentid('lesson');
156 $data->pageid = $this->get_new_parentid('lesson_page');
157 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 158 $data->timeseen = $this->apply_date_offset($data->timeseen);
a6f34943
DM
159
160 $newitemid = $DB->insert_record('lesson_branch', $data);
a6f34943
DM
161 }
162
163 protected function process_lesson_highscore($data) {
164 global $DB;
165
166 $data = (object)$data;
167 $oldid = $data->id;
168 $data->lessonid = $this->get_new_parentid('lesson');
169 $data->userid = $this->get_mappingid('user', $data->userid);
170 $data->gradeid = $this->get_mappingid('lesson_grade', $data->gradeid);
171
172 $newitemid = $DB->insert_record('lesson_high_scores', $data);
173 }
174
175 protected function process_lesson_timer($data) {
176 global $DB;
177
178 $data = (object)$data;
179 $oldid = $data->id;
180 $data->lessonid = $this->get_new_parentid('lesson');
181 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2
EL
182 $data->starttime = $this->apply_date_offset($data->starttime);
183 $data->lessontime = $this->apply_date_offset($data->lessontime);
25345cb4
JMV
184 // Supply item that maybe missing from previous versions.
185 if (!isset($data->completed)) {
186 $data->completed = 0;
187 }
a6f34943
DM
188 $newitemid = $DB->insert_record('lesson_timer', $data);
189 }
190
191 protected function after_execute() {
3643aadf
EL
192 global $DB;
193
b9e7d892
ARN
194 // Answers must be sorted by id to ensure that they're shown correctly
195 ksort($this->answers);
196 foreach ($this->answers as $answer) {
197 $newitemid = $DB->insert_record('lesson_answers', $answer);
0abc18cf 198 $this->set_mapping('lesson_answer', $answer->id, $newitemid, true);
38a8779d
ARN
199
200 // Update the lesson attempts to use the newly created answerid
201 $DB->set_field('lesson_attempts', 'answerid', $newitemid, array(
202 'lessonid' => $answer->lessonid,
203 'pageid' => $answer->pageid,
204 'answerid' => $answer->id));
b9e7d892
ARN
205 }
206
113065d2
EL
207 // Add lesson mediafile, no need to match by itemname (just internally handled context)
208 $this->add_related_files('mod_lesson', 'mediafile', null);
209 // Add lesson page files, by lesson_page itemname
210 $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
0abc18cf
JMV
211 $this->add_related_files('mod_lesson', 'page_answers', 'lesson_answer');
212 $this->add_related_files('mod_lesson', 'page_responses', 'lesson_answer');
ebf5ad4f 213 $this->add_related_files('mod_lesson', 'essay_responses', 'lesson_attempt');
113065d2 214
3643aadf
EL
215 // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
216 $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
217 '', 'id, prevpageid, nextpageid');
218 foreach ($rs as $page) {
219 $page->prevpageid = (empty($page->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->prevpageid);
220 $page->nextpageid = (empty($page->nextpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->nextpageid);
221 $DB->update_record('lesson_pages', $page);
222 }
223 $rs->close();
224
bd9e6629
EL
225 // Remap all the restored 'jumpto' fields now that we have all the pages and their mappings
226 $rs = $DB->get_recordset('lesson_answers', array('lessonid' => $this->task->get_activityid()),
227 '', 'id, jumpto');
228 foreach ($rs as $answer) {
229 if ($answer->jumpto > 0) {
230 $answer->jumpto = $this->get_mappingid('lesson_page', $answer->jumpto);
231 $DB->update_record('lesson_answers', $answer);
232 }
233 }
234 $rs->close();
235
ab48e731
ARN
236 // Re-map the dependency and activitylink information
237 // If a depency or activitylink has no mapping in the backup data then it could either be a duplication of a
238 // lesson, or a backup/restore of a single lesson. We have no way to determine which and whether this is the
239 // same site and/or course. Therefore we try and retrieve a mapping, but fallback to the original value if one
240 // was not found. We then test to see whether the value found is valid for the course being restored into.
241 $lesson = $DB->get_record('lesson', array('id' => $this->task->get_activityid()), 'id, course, dependency, activitylink');
242 $updaterequired = false;
243 if (!empty($lesson->dependency)) {
244 $updaterequired = true;
245 $lesson->dependency = $this->get_mappingid('lesson', $lesson->dependency, $lesson->dependency);
246 if (!$DB->record_exists('lesson', array('id' => $lesson->dependency, 'course' => $lesson->course))) {
247 $lesson->dependency = 0;
248 }
249 }
250
251 if (!empty($lesson->activitylink)) {
252 $updaterequired = true;
253 $lesson->activitylink = $this->get_mappingid('course_module', $lesson->activitylink, $lesson->activitylink);
254 if (!$DB->record_exists('course_modules', array('id' => $lesson->activitylink, 'course' => $lesson->course))) {
255 $lesson->activitylink = 0;
256 }
257 }
258
259 if ($updaterequired) {
260 $DB->update_record('lesson', $lesson);
261 }
a6f34943
DM
262 }
263}