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