MDL-23479 lesson Restore Code
[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 {
33
34 protected function define_structure() {
35
36 $paths = array();
37 $userinfo = $this->get_setting_value('userinfo');
38
39 $paths[] = new restore_path_element('lesson', '/activity/lesson');
40 $paths[] = new restore_path_element('lesson_page', '/activity/lesson/pages/page');
41 $paths[] = new restore_path_element('lesson_answer', '/activity/lesson/pages/page/answers/answer');
42 if ($userinfo) {
43 $paths[] = new restore_path_element('lesson_attempt', '/activity/lesson/pages/page/answers/answer/attempts/attempt');
44 $paths[] = new restore_path_element('lesson_grade', '/activity/lesson/grades/grade');
45 $paths[] = new restore_path_element('lesson_branch', '/activity/lesson/pages/page/branches/branch');
46 $paths[] = new restore_path_element('lesson_highscore', '/activity/lesson/highscores/highscore');
47 $paths[] = new restore_path_element('lesson_timer', '/activity/lesson/timers/timer');
48 }
49
50 // Return the paths wrapped into standard activity structure
51 return $this->prepare_activity_structure($paths);
52 }
53
54 protected function process_lesson($data) {
55 global $DB;
56
57 $data = (object)$data;
58 $oldid = $data->id;
59 $data->course = $this->get_courseid();
60
61 $data->available = $this->apply_date_offset($data->available);
62 $data->deadline = $this->apply_date_offset($data->deadline);
63
64 // insert the lesson record
65 $newitemid = $DB->insert_record('lesson', $data);
66 // inmediately after inserting "activity" record, call this
67 $this->apply_activity_instance($newitemid);
68 }
69
70 protected function process_lesson_page($data) {
71 global $DB;
72
73 $data = (object)$data;
74 $oldid = $data->id;
75 $data->lessonid = $this->get_new_parentid('lesson');
76 $data->prevpageid = (empty($data->prevpageid)) ? 0 : $this->get_mappingid('lesson_page', $data->prevpageid);
77 $data->nextpageid = 0; //we don't know the id of the next page as it hasn't been created yet.
78
79 $newitemid = $DB->insert_record('lesson_pages', $data);
80 $this->set_mapping('lesson_page', $oldid, $newitemid);
81
82 //now update previous page with newid as the nextpageid
83 if (!empty($data->prevpageid)) {
84 $prevpage = $DB->get_record('lesson_pages', array('lessonid'=>$data->lessonid, 'id'=>$data->prevpageid));
85 if (!empty($prevpage)) {
86 $prevpage->nextpageid = $newitemid;
87 $DB->update_record('lesson_pages', $prevpage);
88 }
89 }
90 }
91
92 protected function process_lesson_answer($data) {
93 global $DB;
94
95 $data = (object)$data;
96 $oldid = $data->id;
97 $data->lessonid = $this->get_new_parentid('lesson');
98 $data->pageid = $this->get_new_parentid('lesson_page');
99 $data->answer = $data->answer_text;
100
101 $newitemid = $DB->insert_record('lesson_answers', $data);
102 $this->set_mapping('lesson_answer', $oldid, $newitemid);
103 }
104
105 protected function process_lesson_attempt($data) {
106 global $DB;
107
108 $data = (object)$data;
109 $oldid = $data->id;
110 $data->lessonid = $this->get_new_parentid('lesson');
111 $data->pageid = $this->get_new_parentid('lesson_page');
112 $data->answerid = $this->get_new_parentid('lesson_answer');
113 $data->userid = $this->get_mappingid('user', $data->userid);
114
115 $newitemid = $DB->insert_record('lesson_attempts', $data);
116 }
117
118 protected function process_lesson_grade($data) {
119 global $DB;
120
121 $data = (object)$data;
122 $oldid = $data->id;
123 $data->lessonid = $this->get_new_parentid('lesson');
124 $data->userid = $this->get_mappingid('user', $data->userid);
125
126 $newitemid = $DB->insert_record('lesson_grades', $data);
127 $this->set_mapping('lesson_grade', $oldid, $newitemid);
128 }
129
130 protected function process_lesson_branch($data) {
131 global $DB;
132
133 $data = (object)$data;
134 $oldid = $data->id;
135 $data->lessonid = $this->get_new_parentid('lesson');
136 $data->pageid = $this->get_new_parentid('lesson_page');
137 $data->userid = $this->get_mappingid('user', $data->userid);
138
139 $newitemid = $DB->insert_record('lesson_branch', $data);
140 $this->set_mapping('lesson_branch', $oldid, $newitemid);
141 }
142
143 protected function process_lesson_highscore($data) {
144 global $DB;
145
146 $data = (object)$data;
147 $oldid = $data->id;
148 $data->lessonid = $this->get_new_parentid('lesson');
149 $data->userid = $this->get_mappingid('user', $data->userid);
150 $data->gradeid = $this->get_mappingid('lesson_grade', $data->gradeid);
151
152 $newitemid = $DB->insert_record('lesson_high_scores', $data);
153 }
154
155 protected function process_lesson_timer($data) {
156 global $DB;
157
158 $data = (object)$data;
159 $oldid = $data->id;
160 $data->lessonid = $this->get_new_parentid('lesson');
161 $data->userid = $this->get_mappingid('user', $data->userid);
162
163 $newitemid = $DB->insert_record('lesson_timer', $data);
164 }
165
166 protected function after_execute() {
167 // Add lesson related files, no need to match by itemname (just internally handled context)
168 $this->add_related_files('mod_lesson', 'mediafile', 'id');
169 $this->add_related_files('mod_lesson', 'page_contents', 'id');
170 }
171}