weekly release 2.3dev
[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/**
19 * @package moodlecore
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
a6f34943
DM
75 // insert the lesson record
76 $newitemid = $DB->insert_record('lesson', $data);
034ef761 77 // immediately after inserting "activity" record, call this
a6f34943
DM
78 $this->apply_activity_instance($newitemid);
79 }
80
81 protected function process_lesson_page($data) {
82 global $DB;
83
84 $data = (object)$data;
85 $oldid = $data->id;
86 $data->lessonid = $this->get_new_parentid('lesson');
3643aadf
EL
87
88 // We'll remap all the prevpageid and nextpageid at the end, once all pages have been created
53ab8106
DM
89 $data->timemodified = $this->apply_date_offset($data->timemodified);
90 $data->timecreated = $this->apply_date_offset($data->timecreated);
a6f34943
DM
91
92 $newitemid = $DB->insert_record('lesson_pages', $data);
113065d2 93 $this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas
a6f34943
DM
94 }
95
96 protected function process_lesson_answer($data) {
97 global $DB;
98
99 $data = (object)$data;
a6f34943
DM
100 $data->lessonid = $this->get_new_parentid('lesson');
101 $data->pageid = $this->get_new_parentid('lesson_page');
102 $data->answer = $data->answer_text;
53ab8106
DM
103 $data->timemodified = $this->apply_date_offset($data->timemodified);
104 $data->timecreated = $this->apply_date_offset($data->timecreated);
a6f34943 105
b9e7d892
ARN
106 // Answers need to be processed in order, so we store them in an
107 // instance variable and insert them in the after_execute stage
108 $this->answers[$data->id] = $data;
a6f34943
DM
109 }
110
111 protected function process_lesson_attempt($data) {
112 global $DB;
113
114 $data = (object)$data;
115 $oldid = $data->id;
116 $data->lessonid = $this->get_new_parentid('lesson');
117 $data->pageid = $this->get_new_parentid('lesson_page');
118 $data->answerid = $this->get_new_parentid('lesson_answer');
119 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 120 $data->timeseen = $this->apply_date_offset($data->timeseen);
a6f34943
DM
121
122 $newitemid = $DB->insert_record('lesson_attempts', $data);
123 }
124
125 protected function process_lesson_grade($data) {
126 global $DB;
127
128 $data = (object)$data;
129 $oldid = $data->id;
130 $data->lessonid = $this->get_new_parentid('lesson');
131 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 132 $data->completed = $this->apply_date_offset($data->completed);
a6f34943
DM
133
134 $newitemid = $DB->insert_record('lesson_grades', $data);
135 $this->set_mapping('lesson_grade', $oldid, $newitemid);
136 }
137
138 protected function process_lesson_branch($data) {
139 global $DB;
140
141 $data = (object)$data;
142 $oldid = $data->id;
143 $data->lessonid = $this->get_new_parentid('lesson');
144 $data->pageid = $this->get_new_parentid('lesson_page');
145 $data->userid = $this->get_mappingid('user', $data->userid);
113065d2 146 $data->timeseen = $this->apply_date_offset($data->timeseen);
a6f34943
DM
147
148 $newitemid = $DB->insert_record('lesson_branch', $data);
a6f34943
DM
149 }
150
151 protected function process_lesson_highscore($data) {
152 global $DB;
153
154 $data = (object)$data;
155 $oldid = $data->id;
156 $data->lessonid = $this->get_new_parentid('lesson');
157 $data->userid = $this->get_mappingid('user', $data->userid);
158 $data->gradeid = $this->get_mappingid('lesson_grade', $data->gradeid);
159
160 $newitemid = $DB->insert_record('lesson_high_scores', $data);
161 }
162
163 protected function process_lesson_timer($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);
113065d2
EL
170 $data->starttime = $this->apply_date_offset($data->starttime);
171 $data->lessontime = $this->apply_date_offset($data->lessontime);
a6f34943
DM
172
173 $newitemid = $DB->insert_record('lesson_timer', $data);
174 }
175
176 protected function after_execute() {
3643aadf
EL
177 global $DB;
178
b9e7d892
ARN
179 // Answers must be sorted by id to ensure that they're shown correctly
180 ksort($this->answers);
181 foreach ($this->answers as $answer) {
182 $newitemid = $DB->insert_record('lesson_answers', $answer);
183 $this->set_mapping('lesson_answer', $answer->id, $newitemid);
184 }
185
113065d2
EL
186 // Add lesson mediafile, no need to match by itemname (just internally handled context)
187 $this->add_related_files('mod_lesson', 'mediafile', null);
188 // Add lesson page files, by lesson_page itemname
189 $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
190
3643aadf
EL
191 // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
192 $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
193 '', 'id, prevpageid, nextpageid');
194 foreach ($rs as $page) {
195 $page->prevpageid = (empty($page->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->prevpageid);
196 $page->nextpageid = (empty($page->nextpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->nextpageid);
197 $DB->update_record('lesson_pages', $page);
198 }
199 $rs->close();
200
bd9e6629
EL
201 // Remap all the restored 'jumpto' fields now that we have all the pages and their mappings
202 $rs = $DB->get_recordset('lesson_answers', array('lessonid' => $this->task->get_activityid()),
203 '', 'id, jumpto');
204 foreach ($rs as $answer) {
205 if ($answer->jumpto > 0) {
206 $answer->jumpto = $this->get_mappingid('lesson_page', $answer->jumpto);
207 $DB->update_record('lesson_answers', $answer);
208 }
209 }
210 $rs->close();
211
ab48e731
ARN
212 // Re-map the dependency and activitylink information
213 // If a depency or activitylink has no mapping in the backup data then it could either be a duplication of a
214 // lesson, or a backup/restore of a single lesson. We have no way to determine which and whether this is the
215 // same site and/or course. Therefore we try and retrieve a mapping, but fallback to the original value if one
216 // was not found. We then test to see whether the value found is valid for the course being restored into.
217 $lesson = $DB->get_record('lesson', array('id' => $this->task->get_activityid()), 'id, course, dependency, activitylink');
218 $updaterequired = false;
219 if (!empty($lesson->dependency)) {
220 $updaterequired = true;
221 $lesson->dependency = $this->get_mappingid('lesson', $lesson->dependency, $lesson->dependency);
222 if (!$DB->record_exists('lesson', array('id' => $lesson->dependency, 'course' => $lesson->course))) {
223 $lesson->dependency = 0;
224 }
225 }
226
227 if (!empty($lesson->activitylink)) {
228 $updaterequired = true;
229 $lesson->activitylink = $this->get_mappingid('course_module', $lesson->activitylink, $lesson->activitylink);
230 if (!$DB->record_exists('course_modules', array('id' => $lesson->activitylink, 'course' => $lesson->course))) {
231 $lesson->activitylink = 0;
232 }
233 }
234
235 if ($updaterequired) {
236 $DB->update_record('lesson', $lesson);
237 }
a6f34943
DM
238 }
239}