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
d0445cf7 75 // Supply items that maybe missing from previous versions.
25345cb4
JMV
76 if (!isset($data->completionendreached)) {
77 $data->completionendreached = 0;
78 }
10e2355f
JMV
79 if (!isset($data->completiontimespent)) {
80 $data->completiontimespent = 0;
d0445cf7 81 }
25345cb4 82
4948b68f
JMV
83 if (!isset($data->intro)) {
84 $data->intro = '';
85 $data->introformat = FORMAT_HTML;
86 }
87
a1acc001
JMV
88 // Compatibility with old backups with maxtime and timed fields.
89 if (!isset($data->timelimit)) {
90 if (isset($data->timed) && isset($data->maxtime) && $data->timed) {
91 $data->timelimit = 60 * $data->maxtime;
92 } else {
93 $data->timelimit = 0;
94 }
95 }
a6f34943
DM
96 // insert the lesson record
97 $newitemid = $DB->insert_record('lesson', $data);
034ef761 98 // immediately after inserting "activity" record, call this
a6f34943
DM
99 $this->apply_activity_instance($newitemid);
100 }
101
102 protected function process_lesson_page($data) {
103 global $DB;
104
105 $data = (object)$data;
106 $oldid = $data->id;
107 $data->lessonid = $this->get_new_parentid('lesson');
3643aadf
EL
108
109 // We'll remap all the prevpageid and nextpageid at the end, once all pages have been created
53ab8106
DM
110 $data->timemodified = $this->apply_date_offset($data->timemodified);
111 $data->timecreated = $this->apply_date_offset($data->timecreated);
a6f34943
DM
112
113 $newitemid = $DB->insert_record('lesson_pages', $data);
113065d2 114 $this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas
a6f34943
DM
115 }
116
117 protected function process_lesson_answer($data) {
118 global $DB;
119
120 $data = (object)$data;
a6f34943
DM
121 $data->lessonid = $this->get_new_parentid('lesson');
122 $data->pageid = $this->get_new_parentid('lesson_page');
123 $data->answer = $data->answer_text;
53ab8106
DM
124 $data->timemodified = $this->apply_date_offset($data->timemodified);
125 $data->timecreated = $this->apply_date_offset($data->timecreated);
a6f34943 126
38a8779d
ARN
127 // Set a dummy mapping to get the old ID so that it can be used by get_old_parentid when
128 // processing attempts. It will be corrected in after_execute
0abc18cf 129 $this->set_mapping('lesson_answer', $data->id, 0, true); // Has related fileareas.
38a8779d 130
b9e7d892
ARN
131 // Answers need to be processed in order, so we store them in an
132 // instance variable and insert them in the after_execute stage
133 $this->answers[$data->id] = $data;
a6f34943
DM
134 }
135
136 protected function process_lesson_attempt($data) {
137 global $DB;
138
139 $data = (object)$data;
140 $oldid = $data->id;
141 $data->lessonid = $this->get_new_parentid('lesson');
142 $data->pageid = $this->get_new_parentid('lesson_page');
38a8779d
ARN
143
144 // We use the old answerid here as the answer isn't created until after_execute
145 $data->answerid = $this->get_old_parentid('lesson_answer');
a6f34943 146 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 147 $data->timeseen = $this->apply_date_offset($data->timeseen);
a6f34943
DM
148
149 $newitemid = $DB->insert_record('lesson_attempts', $data);
ebf5ad4f 150 $this->set_mapping('lesson_attempt', $oldid, $newitemid, true); // Has related fileareas.
a6f34943
DM
151 }
152
153 protected function process_lesson_grade($data) {
154 global $DB;
155
156 $data = (object)$data;
157 $oldid = $data->id;
158 $data->lessonid = $this->get_new_parentid('lesson');
159 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 160 $data->completed = $this->apply_date_offset($data->completed);
a6f34943
DM
161
162 $newitemid = $DB->insert_record('lesson_grades', $data);
163 $this->set_mapping('lesson_grade', $oldid, $newitemid);
164 }
165
166 protected function process_lesson_branch($data) {
167 global $DB;
168
169 $data = (object)$data;
170 $oldid = $data->id;
171 $data->lessonid = $this->get_new_parentid('lesson');
172 $data->pageid = $this->get_new_parentid('lesson_page');
173 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 174 $data->timeseen = $this->apply_date_offset($data->timeseen);
a6f34943
DM
175
176 $newitemid = $DB->insert_record('lesson_branch', $data);
a6f34943
DM
177 }
178
179 protected function process_lesson_highscore($data) {
180 global $DB;
181
182 $data = (object)$data;
183 $oldid = $data->id;
184 $data->lessonid = $this->get_new_parentid('lesson');
185 $data->userid = $this->get_mappingid('user', $data->userid);
186 $data->gradeid = $this->get_mappingid('lesson_grade', $data->gradeid);
187
188 $newitemid = $DB->insert_record('lesson_high_scores', $data);
189 }
190
191 protected function process_lesson_timer($data) {
192 global $DB;
193
194 $data = (object)$data;
195 $oldid = $data->id;
196 $data->lessonid = $this->get_new_parentid('lesson');
197 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2
EL
198 $data->starttime = $this->apply_date_offset($data->starttime);
199 $data->lessontime = $this->apply_date_offset($data->lessontime);
25345cb4
JMV
200 // Supply item that maybe missing from previous versions.
201 if (!isset($data->completed)) {
202 $data->completed = 0;
203 }
a6f34943
DM
204 $newitemid = $DB->insert_record('lesson_timer', $data);
205 }
206
207 protected function after_execute() {
3643aadf
EL
208 global $DB;
209
b9e7d892
ARN
210 // Answers must be sorted by id to ensure that they're shown correctly
211 ksort($this->answers);
212 foreach ($this->answers as $answer) {
213 $newitemid = $DB->insert_record('lesson_answers', $answer);
0abc18cf 214 $this->set_mapping('lesson_answer', $answer->id, $newitemid, true);
38a8779d
ARN
215
216 // Update the lesson attempts to use the newly created answerid
217 $DB->set_field('lesson_attempts', 'answerid', $newitemid, array(
218 'lessonid' => $answer->lessonid,
219 'pageid' => $answer->pageid,
220 'answerid' => $answer->id));
b9e7d892
ARN
221 }
222
4948b68f
JMV
223 // Add lesson files, no need to match by itemname (just internally handled context).
224 $this->add_related_files('mod_lesson', 'intro', null);
113065d2
EL
225 $this->add_related_files('mod_lesson', 'mediafile', null);
226 // Add lesson page files, by lesson_page itemname
227 $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
0abc18cf
JMV
228 $this->add_related_files('mod_lesson', 'page_answers', 'lesson_answer');
229 $this->add_related_files('mod_lesson', 'page_responses', 'lesson_answer');
ebf5ad4f 230 $this->add_related_files('mod_lesson', 'essay_responses', 'lesson_attempt');
113065d2 231
3643aadf
EL
232 // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
233 $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
234 '', 'id, prevpageid, nextpageid');
235 foreach ($rs as $page) {
236 $page->prevpageid = (empty($page->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->prevpageid);
237 $page->nextpageid = (empty($page->nextpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->nextpageid);
238 $DB->update_record('lesson_pages', $page);
239 }
240 $rs->close();
241
bd9e6629
EL
242 // Remap all the restored 'jumpto' fields now that we have all the pages and their mappings
243 $rs = $DB->get_recordset('lesson_answers', array('lessonid' => $this->task->get_activityid()),
244 '', 'id, jumpto');
245 foreach ($rs as $answer) {
246 if ($answer->jumpto > 0) {
247 $answer->jumpto = $this->get_mappingid('lesson_page', $answer->jumpto);
248 $DB->update_record('lesson_answers', $answer);
249 }
250 }
251 $rs->close();
252
6695d594
SB
253 // Remap all the restored 'nextpageid' fields now that we have all the pages and their mappings.
254 $rs = $DB->get_recordset('lesson_branch', array('lessonid' => $this->task->get_activityid()),
255 '', 'id, nextpageid');
256 foreach ($rs as $answer) {
257 if ($answer->nextpageid > 0) {
258 $answer->nextpageid = $this->get_mappingid('lesson_page', $answer->nextpageid);
259 $DB->update_record('lesson_branch', $answer);
260 }
261 }
262 $rs->close();
263
7e0551fc 264 // Replay the upgrade step 2015030301
a1300e98
JMV
265 // to clean lesson answers that should be plain text.
266 // 1 = LESSON_PAGE_SHORTANSWER, 8 = LESSON_PAGE_NUMERICAL, 20 = LESSON_PAGE_BRANCHTABLE.
267
268 $sql = 'SELECT a.*
269 FROM {lesson_answers} a
270 JOIN {lesson_pages} p ON p.id = a.pageid
271 WHERE a.answerformat <> :format
272 AND a.lessonid = :lessonid
273 AND p.qtype IN (1, 8, 20)';
274 $badanswers = $DB->get_recordset_sql($sql, array('lessonid' => $this->task->get_activityid(), 'format' => FORMAT_MOODLE));
275
276 foreach ($badanswers as $badanswer) {
277 // Strip tags from answer text and convert back the format to FORMAT_MOODLE.
278 $badanswer->answer = strip_tags($badanswer->answer);
279 $badanswer->answerformat = FORMAT_MOODLE;
280 $DB->update_record('lesson_answers', $badanswer);
281 }
6442803f 282 $badanswers->close();
a1300e98 283
7e0551fc
JMV
284 // Replay the upgrade step 2015032700.
285 // Delete any orphaned lesson_branch record.
286 if ($DB->get_dbfamily() === 'mysql') {
287 $sql = "DELETE {lesson_branch}
288 FROM {lesson_branch}
289 LEFT JOIN {lesson_pages}
290 ON {lesson_branch}.pageid = {lesson_pages}.id
291 WHERE {lesson_pages}.id IS NULL";
292 } else {
293 $sql = "DELETE FROM {lesson_branch}
294 WHERE NOT EXISTS (
295 SELECT 'x' FROM {lesson_pages}
296 WHERE {lesson_branch}.pageid = {lesson_pages}.id)";
297 }
298
299 $DB->execute($sql);
300
ab48e731
ARN
301 // Re-map the dependency and activitylink information
302 // If a depency or activitylink has no mapping in the backup data then it could either be a duplication of a
303 // lesson, or a backup/restore of a single lesson. We have no way to determine which and whether this is the
304 // same site and/or course. Therefore we try and retrieve a mapping, but fallback to the original value if one
305 // was not found. We then test to see whether the value found is valid for the course being restored into.
306 $lesson = $DB->get_record('lesson', array('id' => $this->task->get_activityid()), 'id, course, dependency, activitylink');
307 $updaterequired = false;
308 if (!empty($lesson->dependency)) {
309 $updaterequired = true;
310 $lesson->dependency = $this->get_mappingid('lesson', $lesson->dependency, $lesson->dependency);
311 if (!$DB->record_exists('lesson', array('id' => $lesson->dependency, 'course' => $lesson->course))) {
312 $lesson->dependency = 0;
313 }
314 }
315
316 if (!empty($lesson->activitylink)) {
317 $updaterequired = true;
318 $lesson->activitylink = $this->get_mappingid('course_module', $lesson->activitylink, $lesson->activitylink);
319 if (!$DB->record_exists('course_modules', array('id' => $lesson->activitylink, 'course' => $lesson->course))) {
320 $lesson->activitylink = 0;
321 }
322 }
323
324 if ($updaterequired) {
325 $DB->update_record('lesson', $lesson);
326 }
a6f34943
DM
327 }
328}