3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
26 * Define all the restore steps that will be used by the restore_lesson_activity_task
30 * Structure step to restore one lesson activity
32 class restore_lesson_activity_structure_step extends restore_activity_structure_step {
33 // Store the answers as they're received but only process them at the
35 protected $answers = array();
37 protected function define_structure() {
40 $userinfo = $this->get_setting_value('userinfo');
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 $paths[] = new restore_path_element('lesson_override', '/activity/lesson/overrides/override');
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');
54 // Return the paths wrapped into standard activity structure
55 return $this->prepare_activity_structure($paths);
58 protected function process_lesson($data) {
61 $data = (object)$data;
63 $data->course = $this->get_courseid();
65 $data->available = $this->apply_date_offset($data->available);
66 $data->deadline = $this->apply_date_offset($data->deadline);
67 $data->timemodified = $this->apply_date_offset($data->timemodified);
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);
76 // Supply items that maybe missing from previous versions.
77 if (!isset($data->completionendreached)) {
78 $data->completionendreached = 0;
80 if (!isset($data->completiontimespent)) {
81 $data->completiontimespent = 0;
84 if (!isset($data->intro)) {
86 $data->introformat = FORMAT_HTML;
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;
97 // insert the lesson record
98 $newitemid = $DB->insert_record('lesson', $data);
99 // immediately after inserting "activity" record, call this
100 $this->apply_activity_instance($newitemid);
103 protected function process_lesson_page($data) {
106 $data = (object)$data;
108 $data->lessonid = $this->get_new_parentid('lesson');
110 // We'll remap all the prevpageid and nextpageid at the end, once all pages have been created
111 $data->timemodified = $this->apply_date_offset($data->timemodified);
112 $data->timecreated = $this->apply_date_offset($data->timecreated);
114 $newitemid = $DB->insert_record('lesson_pages', $data);
115 $this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas
118 protected function process_lesson_answer($data) {
121 $data = (object)$data;
122 $data->lessonid = $this->get_new_parentid('lesson');
123 $data->pageid = $this->get_new_parentid('lesson_page');
124 $data->answer = $data->answer_text;
125 $data->timemodified = $this->apply_date_offset($data->timemodified);
126 $data->timecreated = $this->apply_date_offset($data->timecreated);
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
130 $this->set_mapping('lesson_answer', $data->id, 0, true); // Has related fileareas.
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;
137 protected function process_lesson_attempt($data) {
140 $data = (object)$data;
142 $data->lessonid = $this->get_new_parentid('lesson');
143 $data->pageid = $this->get_new_parentid('lesson_page');
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');
147 $data->userid = $this->get_mappingid('user', $data->userid);
148 $data->timeseen = $this->apply_date_offset($data->timeseen);
150 $newitemid = $DB->insert_record('lesson_attempts', $data);
151 $this->set_mapping('lesson_attempt', $oldid, $newitemid, true); // Has related fileareas.
154 protected function process_lesson_grade($data) {
157 $data = (object)$data;
159 $data->lessonid = $this->get_new_parentid('lesson');
160 $data->userid = $this->get_mappingid('user', $data->userid);
161 $data->completed = $this->apply_date_offset($data->completed);
163 $newitemid = $DB->insert_record('lesson_grades', $data);
164 $this->set_mapping('lesson_grade', $oldid, $newitemid);
167 protected function process_lesson_branch($data) {
170 $data = (object)$data;
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);
175 $data->timeseen = $this->apply_date_offset($data->timeseen);
177 $newitemid = $DB->insert_record('lesson_branch', $data);
180 protected function process_lesson_highscore($data) {
183 $data = (object)$data;
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);
189 $newitemid = $DB->insert_record('lesson_high_scores', $data);
192 protected function process_lesson_timer($data) {
195 $data = (object)$data;
197 $data->lessonid = $this->get_new_parentid('lesson');
198 $data->userid = $this->get_mappingid('user', $data->userid);
199 $data->starttime = $this->apply_date_offset($data->starttime);
200 $data->lessontime = $this->apply_date_offset($data->lessontime);
201 // Supply item that maybe missing from previous versions.
202 if (!isset($data->completed)) {
203 $data->completed = 0;
205 $newitemid = $DB->insert_record('lesson_timer', $data);
209 * Process a lesson override restore
210 * @param object $data The data in object form
213 protected function process_lesson_override($data) {
216 $data = (object)$data;
219 // Based on userinfo, we'll restore user overides or no.
220 $userinfo = $this->get_setting_value('userinfo');
222 // Skip user overrides if we are not restoring userinfo.
223 if (!$userinfo && !is_null($data->userid)) {
227 $data->lessonid = $this->get_new_parentid('lesson');
229 if (!is_null($data->userid)) {
230 $data->userid = $this->get_mappingid('user', $data->userid);
232 if (!is_null($data->groupid)) {
233 $data->groupid = $this->get_mappingid('group', $data->groupid);
236 $data->available = $this->apply_date_offset($data->available);
237 $data->deadline = $this->apply_date_offset($data->deadline);
239 $newitemid = $DB->insert_record('lesson_overrides', $data);
241 // Add mapping, restore of logs needs it.
242 $this->set_mapping('lesson_override', $oldid, $newitemid);
245 protected function after_execute() {
248 // Answers must be sorted by id to ensure that they're shown correctly
249 ksort($this->answers);
250 foreach ($this->answers as $answer) {
251 $newitemid = $DB->insert_record('lesson_answers', $answer);
252 $this->set_mapping('lesson_answer', $answer->id, $newitemid, true);
254 // Update the lesson attempts to use the newly created answerid
255 $DB->set_field('lesson_attempts', 'answerid', $newitemid, array(
256 'lessonid' => $answer->lessonid,
257 'pageid' => $answer->pageid,
258 'answerid' => $answer->id));
261 // Add lesson files, no need to match by itemname (just internally handled context).
262 $this->add_related_files('mod_lesson', 'intro', null);
263 $this->add_related_files('mod_lesson', 'mediafile', null);
264 // Add lesson page files, by lesson_page itemname
265 $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
266 $this->add_related_files('mod_lesson', 'page_answers', 'lesson_answer');
267 $this->add_related_files('mod_lesson', 'page_responses', 'lesson_answer');
268 $this->add_related_files('mod_lesson', 'essay_responses', 'lesson_attempt');
270 // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
271 $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
272 '', 'id, prevpageid, nextpageid');
273 foreach ($rs as $page) {
274 $page->prevpageid = (empty($page->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->prevpageid);
275 $page->nextpageid = (empty($page->nextpageid)) ? 0 : $this->get_mappingid('lesson_page', $page->nextpageid);
276 $DB->update_record('lesson_pages', $page);
280 // Remap all the restored 'jumpto' fields now that we have all the pages and their mappings
281 $rs = $DB->get_recordset('lesson_answers', array('lessonid' => $this->task->get_activityid()),
283 foreach ($rs as $answer) {
284 if ($answer->jumpto > 0) {
285 $answer->jumpto = $this->get_mappingid('lesson_page', $answer->jumpto);
286 $DB->update_record('lesson_answers', $answer);
291 // Remap all the restored 'nextpageid' fields now that we have all the pages and their mappings.
292 $rs = $DB->get_recordset('lesson_branch', array('lessonid' => $this->task->get_activityid()),
293 '', 'id, nextpageid');
294 foreach ($rs as $answer) {
295 if ($answer->nextpageid > 0) {
296 $answer->nextpageid = $this->get_mappingid('lesson_page', $answer->nextpageid);
297 $DB->update_record('lesson_branch', $answer);
302 // Replay the upgrade step 2015030301
303 // to clean lesson answers that should be plain text.
304 // 1 = LESSON_PAGE_SHORTANSWER, 8 = LESSON_PAGE_NUMERICAL, 20 = LESSON_PAGE_BRANCHTABLE.
307 FROM {lesson_answers} a
308 JOIN {lesson_pages} p ON p.id = a.pageid
309 WHERE a.answerformat <> :format
310 AND a.lessonid = :lessonid
311 AND p.qtype IN (1, 8, 20)';
312 $badanswers = $DB->get_recordset_sql($sql, array('lessonid' => $this->task->get_activityid(), 'format' => FORMAT_MOODLE));
314 foreach ($badanswers as $badanswer) {
315 // Strip tags from answer text and convert back the format to FORMAT_MOODLE.
316 $badanswer->answer = strip_tags($badanswer->answer);
317 $badanswer->answerformat = FORMAT_MOODLE;
318 $DB->update_record('lesson_answers', $badanswer);
320 $badanswers->close();
322 // Replay the upgrade step 2015032700.
323 // Delete any orphaned lesson_branch record.
324 if ($DB->get_dbfamily() === 'mysql') {
325 $sql = "DELETE {lesson_branch}
327 LEFT JOIN {lesson_pages}
328 ON {lesson_branch}.pageid = {lesson_pages}.id
329 WHERE {lesson_pages}.id IS NULL";
331 $sql = "DELETE FROM {lesson_branch}
333 SELECT 'x' FROM {lesson_pages}
334 WHERE {lesson_branch}.pageid = {lesson_pages}.id)";
339 // Re-map the dependency and activitylink information
340 // If a depency or activitylink has no mapping in the backup data then it could either be a duplication of a
341 // lesson, or a backup/restore of a single lesson. We have no way to determine which and whether this is the
342 // same site and/or course. Therefore we try and retrieve a mapping, but fallback to the original value if one
343 // was not found. We then test to see whether the value found is valid for the course being restored into.
344 $lesson = $DB->get_record('lesson', array('id' => $this->task->get_activityid()), 'id, course, dependency, activitylink');
345 $updaterequired = false;
346 if (!empty($lesson->dependency)) {
347 $updaterequired = true;
348 $lesson->dependency = $this->get_mappingid('lesson', $lesson->dependency, $lesson->dependency);
349 if (!$DB->record_exists('lesson', array('id' => $lesson->dependency, 'course' => $lesson->course))) {
350 $lesson->dependency = 0;
354 if (!empty($lesson->activitylink)) {
355 $updaterequired = true;
356 $lesson->activitylink = $this->get_mappingid('course_module', $lesson->activitylink, $lesson->activitylink);
357 if (!$DB->record_exists('course_modules', array('id' => $lesson->activitylink, 'course' => $lesson->course))) {
358 $lesson->activitylink = 0;
362 if ($updaterequired) {
363 $DB->update_record('lesson', $lesson);