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