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