MDL-31355 mod_forum: backup and restore of duedate and cutoffdate fields
[moodle.git] / mod / forum / backup / moodle2 / restore_forum_stepslib.php
CommitLineData
01b4ca01
EL
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/**
01030f1b 19 * @package mod_forum
01b4ca01 20 * @subpackage backup-moodle2
01030f1b
SH
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
01b4ca01
EL
23 */
24
25/**
26 * Define all the restore steps that will be used by the restore_forum_activity_task
27 */
28
29/**
30 * Structure step to restore one forum activity
31 */
32class restore_forum_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('forum', '/activity/forum');
40 if ($userinfo) {
41 $paths[] = new restore_path_element('forum_discussion', '/activity/forum/discussions/discussion');
42 $paths[] = new restore_path_element('forum_post', '/activity/forum/discussions/discussion/posts/post');
151113f9 43 $paths[] = new restore_path_element('forum_tag', '/activity/forum/poststags/tag');
361a41d3 44 $paths[] = new restore_path_element('forum_discussion_sub', '/activity/forum/discussions/discussion/discussion_subs/discussion_sub');
01b4ca01
EL
45 $paths[] = new restore_path_element('forum_rating', '/activity/forum/discussions/discussion/posts/post/ratings/rating');
46 $paths[] = new restore_path_element('forum_subscription', '/activity/forum/subscriptions/subscription');
2eb67dfa 47 $paths[] = new restore_path_element('forum_digest', '/activity/forum/digests/digest');
01b4ca01 48 $paths[] = new restore_path_element('forum_read', '/activity/forum/readposts/read');
41941110 49 $paths[] = new restore_path_element('forum_track', '/activity/forum/trackedprefs/track');
01b4ca01
EL
50 }
51
52 // Return the paths wrapped into standard activity structure
53 return $this->prepare_activity_structure($paths);
54 }
55
56 protected function process_forum($data) {
57 global $DB;
58
59 $data = (object)$data;
60 $oldid = $data->id;
61 $data->course = $this->get_courseid();
62
0d14fcbc
AA
63 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
64 // See MDL-9367.
0556f393
SR
65 if (!isset($data->duedate)) {
66 $data->duedate = 0;
67 }
68 $data->duedate = $this->apply_date_offset($data->duedate);
69 if (!isset($data->cutoffdate)) {
70 $data->cutoffdate = 0;
71 }
72 $data->cutoffdate = $this->apply_date_offset($data->cutoffdate);
01b4ca01
EL
73 $data->assesstimestart = $this->apply_date_offset($data->assesstimestart);
74 $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish);
75 if ($data->scale < 0) { // scale found, get mapping
76 $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
77 }
78
79 $newitemid = $DB->insert_record('forum', $data);
80 $this->apply_activity_instance($newitemid);
669da91d
JSM
81
82 // Add current enrolled user subscriptions if necessary.
83 $data->id = $newitemid;
84 $ctx = context_module::instance($this->task->get_moduleid());
85 forum_instance_created($ctx, $data);
01b4ca01
EL
86 }
87
88 protected function process_forum_discussion($data) {
89 global $DB;
90
91 $data = (object)$data;
92 $oldid = $data->id;
93 $data->course = $this->get_courseid();
94
95 $data->forum = $this->get_new_parentid('forum');
01b4ca01
EL
96 $data->timestart = $this->apply_date_offset($data->timestart);
97 $data->timeend = $this->apply_date_offset($data->timeend);
98 $data->userid = $this->get_mappingid('user', $data->userid);
99 $data->groupid = $this->get_mappingid('group', $data->groupid);
100 $data->usermodified = $this->get_mappingid('user', $data->usermodified);
101
102 $newitemid = $DB->insert_record('forum_discussions', $data);
103 $this->set_mapping('forum_discussion', $oldid, $newitemid);
104 }
105
106 protected function process_forum_post($data) {
107 global $DB;
108
109 $data = (object)$data;
110 $oldid = $data->id;
111
112 $data->discussion = $this->get_new_parentid('forum_discussion');
01b4ca01
EL
113 $data->userid = $this->get_mappingid('user', $data->userid);
114 // If post has parent, map it (it has been already restored)
115 if (!empty($data->parent)) {
116 $data->parent = $this->get_mappingid('forum_post', $data->parent);
117 }
118
119 $newitemid = $DB->insert_record('forum_posts', $data);
120 $this->set_mapping('forum_post', $oldid, $newitemid, true);
121
122 // If !post->parent, it's the 1st post. Set it in discussion
123 if (empty($data->parent)) {
124 $DB->set_field('forum_discussions', 'firstpost', $newitemid, array('id' => $data->discussion));
125 }
126 }
127
d2ba493c
AH
128 protected function process_forum_tag($data) {
129 $data = (object)$data;
130
131 if (!core_tag_tag::is_enabled('mod_forum', 'forum_posts')) { // Tags disabled in server, nothing to process.
132 return;
133 }
134
135 $tag = $data->rawname;
151113f9
MG
136 if (!$itemid = $this->get_mappingid('forum_post', $data->itemid)) {
137 // Some orphaned tag, we could not find the restored post for it - ignore.
138 return;
139 }
d2ba493c
AH
140
141 $context = context_module::instance($this->task->get_moduleid());
142 core_tag_tag::add_item_tag('mod_forum', 'forum_posts', $itemid, $context, $tag);
143 }
144
01b4ca01
EL
145 protected function process_forum_rating($data) {
146 global $DB;
147
148 $data = (object)$data;
149
150 // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
151 $data->contextid = $this->task->get_contextid();
152 $data->itemid = $this->get_new_parentid('forum_post');
153 if ($data->scaleid < 0) { // scale found, get mapping
154 $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
155 }
156 $data->rating = $data->value;
157 $data->userid = $this->get_mappingid('user', $data->userid);
01b4ca01 158
2b04c41c
SH
159 // We need to check that component and ratingarea are both set here.
160 if (empty($data->component)) {
161 $data->component = 'mod_forum';
162 }
163 if (empty($data->ratingarea)) {
164 $data->ratingarea = 'post';
165 }
166
01b4ca01
EL
167 $newitemid = $DB->insert_record('rating', $data);
168 }
169
170 protected function process_forum_subscription($data) {
171 global $DB;
172
173 $data = (object)$data;
174 $oldid = $data->id;
175
176 $data->forum = $this->get_new_parentid('forum');
177 $data->userid = $this->get_mappingid('user', $data->userid);
178
9bd68076
TR
179 // Create only a new subscription if it does not already exist (see MDL-59854).
180 if ($subscription = $DB->get_record('forum_subscriptions',
181 array('forum' => $data->forum, 'userid' => $data->userid))) {
182 $this->set_mapping('forum_subscription', $oldid, $subscription->id, true);
183 } else {
184 $newitemid = $DB->insert_record('forum_subscriptions', $data);
185 $this->set_mapping('forum_subscription', $oldid, $newitemid, true);
186 }
ef15eae4 187
01b4ca01
EL
188 }
189
361a41d3
AN
190 protected function process_forum_discussion_sub($data) {
191 global $DB;
192
193 $data = (object)$data;
194 $oldid = $data->id;
195
196 $data->discussion = $this->get_new_parentid('forum_discussion');
197 $data->forum = $this->get_new_parentid('forum');
198 $data->userid = $this->get_mappingid('user', $data->userid);
199
200 $newitemid = $DB->insert_record('forum_discussion_subs', $data);
ef15eae4 201 $this->set_mapping('forum_discussion_sub', $oldid, $newitemid, true);
361a41d3
AN
202 }
203
2eb67dfa
AN
204 protected function process_forum_digest($data) {
205 global $DB;
206
207 $data = (object)$data;
208 $oldid = $data->id;
209
210 $data->forum = $this->get_new_parentid('forum');
211 $data->userid = $this->get_mappingid('user', $data->userid);
212
213 $newitemid = $DB->insert_record('forum_digests', $data);
214 }
215
01b4ca01
EL
216 protected function process_forum_read($data) {
217 global $DB;
218
219 $data = (object)$data;
220 $oldid = $data->id;
221
222 $data->forumid = $this->get_new_parentid('forum');
223 $data->discussionid = $this->get_mappingid('forum_discussion', $data->discussionid);
224 $data->postid = $this->get_mappingid('forum_post', $data->postid);
225 $data->userid = $this->get_mappingid('user', $data->userid);
226
227 $newitemid = $DB->insert_record('forum_read', $data);
228 }
229
41941110
EL
230 protected function process_forum_track($data) {
231 global $DB;
232
233 $data = (object)$data;
234 $oldid = $data->id;
235
236 $data->forumid = $this->get_new_parentid('forum');
237 $data->userid = $this->get_mappingid('user', $data->userid);
238
239 $newitemid = $DB->insert_record('forum_track_prefs', $data);
240 }
241
01b4ca01 242 protected function after_execute() {
01b4ca01
EL
243 // Add forum related files, no need to match by itemname (just internally handled context)
244 $this->add_related_files('mod_forum', 'intro', null);
245
9258045b
MN
246 // Add post related files, matching by itemname = 'forum_post'
247 $this->add_related_files('mod_forum', 'post', 'forum_post');
248 $this->add_related_files('mod_forum', 'attachment', 'forum_post');
249 }
250
251 protected function after_restore() {
252 global $DB;
253
01b4ca01
EL
254 // If the forum is of type 'single' and no discussion has been ignited
255 // (non-userinfo backup/restore) create the discussion here, using forum
256 // information as base for the initial post.
257 $forumid = $this->task->get_activityid();
258 $forumrec = $DB->get_record('forum', array('id' => $forumid));
259 if ($forumrec->type == 'single' && !$DB->record_exists('forum_discussions', array('forum' => $forumid))) {
260 // Create single discussion/lead post from forum data
39b2d870 261 $sd = new stdClass();
01b4ca01
EL
262 $sd->course = $forumrec->course;
263 $sd->forum = $forumrec->id;
264 $sd->name = $forumrec->name;
265 $sd->assessed = $forumrec->assessed;
266 $sd->message = $forumrec->intro;
267 $sd->messageformat = $forumrec->introformat;
268 $sd->messagetrust = true;
269 $sd->mailnow = false;
ce2d9273 270 $sdid = forum_add_discussion($sd, null, null, $this->task->get_userid());
01b4ca01
EL
271 // Mark the post as mailed
272 $DB->set_field ('forum_posts','mailed', '1', array('discussion' => $sdid));
273 // Copy all the files from mod_foum/intro to mod_forum/post
274 $fs = get_file_storage();
275 $files = $fs->get_area_files($this->task->get_contextid(), 'mod_forum', 'intro');
276 foreach ($files as $file) {
39b2d870 277 $newfilerecord = new stdClass();
01b4ca01
EL
278 $newfilerecord->filearea = 'post';
279 $newfilerecord->itemid = $DB->get_field('forum_discussions', 'firstpost', array('id' => $sdid));
280 $fs->create_file_from_storedfile($newfilerecord, $file);
281 }
282 }
01b4ca01
EL
283 }
284}