MDL-53367 mod_forum: Subscribe current users when importing a forum
[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');
361a41d3 43 $paths[] = new restore_path_element('forum_discussion_sub', '/activity/forum/discussions/discussion/discussion_subs/discussion_sub');
01b4ca01
EL
44 $paths[] = new restore_path_element('forum_rating', '/activity/forum/discussions/discussion/posts/post/ratings/rating');
45 $paths[] = new restore_path_element('forum_subscription', '/activity/forum/subscriptions/subscription');
2eb67dfa 46 $paths[] = new restore_path_element('forum_digest', '/activity/forum/digests/digest');
01b4ca01 47 $paths[] = new restore_path_element('forum_read', '/activity/forum/readposts/read');
41941110 48 $paths[] = new restore_path_element('forum_track', '/activity/forum/trackedprefs/track');
01b4ca01
EL
49 }
50
51 // Return the paths wrapped into standard activity structure
52 return $this->prepare_activity_structure($paths);
53 }
54
55 protected function process_forum($data) {
56 global $DB;
57
58 $data = (object)$data;
59 $oldid = $data->id;
60 $data->course = $this->get_courseid();
61
62 $data->assesstimestart = $this->apply_date_offset($data->assesstimestart);
63 $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish);
64 if ($data->scale < 0) { // scale found, get mapping
65 $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
66 }
67
68 $newitemid = $DB->insert_record('forum', $data);
69 $this->apply_activity_instance($newitemid);
669da91d
JSM
70
71 // Add current enrolled user subscriptions if necessary.
72 $data->id = $newitemid;
73 $ctx = context_module::instance($this->task->get_moduleid());
74 forum_instance_created($ctx, $data);
01b4ca01
EL
75 }
76
77 protected function process_forum_discussion($data) {
78 global $DB;
79
80 $data = (object)$data;
81 $oldid = $data->id;
82 $data->course = $this->get_courseid();
83
84 $data->forum = $this->get_new_parentid('forum');
85 $data->timemodified = $this->apply_date_offset($data->timemodified);
86 $data->timestart = $this->apply_date_offset($data->timestart);
87 $data->timeend = $this->apply_date_offset($data->timeend);
88 $data->userid = $this->get_mappingid('user', $data->userid);
89 $data->groupid = $this->get_mappingid('group', $data->groupid);
90 $data->usermodified = $this->get_mappingid('user', $data->usermodified);
91
92 $newitemid = $DB->insert_record('forum_discussions', $data);
93 $this->set_mapping('forum_discussion', $oldid, $newitemid);
94 }
95
96 protected function process_forum_post($data) {
97 global $DB;
98
99 $data = (object)$data;
100 $oldid = $data->id;
101
102 $data->discussion = $this->get_new_parentid('forum_discussion');
103 $data->created = $this->apply_date_offset($data->created);
104 $data->modified = $this->apply_date_offset($data->modified);
105 $data->userid = $this->get_mappingid('user', $data->userid);
106 // If post has parent, map it (it has been already restored)
107 if (!empty($data->parent)) {
108 $data->parent = $this->get_mappingid('forum_post', $data->parent);
109 }
110
111 $newitemid = $DB->insert_record('forum_posts', $data);
112 $this->set_mapping('forum_post', $oldid, $newitemid, true);
113
114 // If !post->parent, it's the 1st post. Set it in discussion
115 if (empty($data->parent)) {
116 $DB->set_field('forum_discussions', 'firstpost', $newitemid, array('id' => $data->discussion));
117 }
118 }
119
120 protected function process_forum_rating($data) {
121 global $DB;
122
123 $data = (object)$data;
124
125 // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
126 $data->contextid = $this->task->get_contextid();
127 $data->itemid = $this->get_new_parentid('forum_post');
128 if ($data->scaleid < 0) { // scale found, get mapping
129 $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
130 }
131 $data->rating = $data->value;
132 $data->userid = $this->get_mappingid('user', $data->userid);
133 $data->timecreated = $this->apply_date_offset($data->timecreated);
134 $data->timemodified = $this->apply_date_offset($data->timemodified);
135
2b04c41c
SH
136 // We need to check that component and ratingarea are both set here.
137 if (empty($data->component)) {
138 $data->component = 'mod_forum';
139 }
140 if (empty($data->ratingarea)) {
141 $data->ratingarea = 'post';
142 }
143
01b4ca01
EL
144 $newitemid = $DB->insert_record('rating', $data);
145 }
146
147 protected function process_forum_subscription($data) {
148 global $DB;
149
150 $data = (object)$data;
151 $oldid = $data->id;
152
153 $data->forum = $this->get_new_parentid('forum');
154 $data->userid = $this->get_mappingid('user', $data->userid);
155
156 $newitemid = $DB->insert_record('forum_subscriptions', $data);
ef15eae4
MN
157 $this->set_mapping('forum_subscription', $oldid, $newitemid, true);
158
01b4ca01
EL
159 }
160
361a41d3
AN
161 protected function process_forum_discussion_sub($data) {
162 global $DB;
163
164 $data = (object)$data;
165 $oldid = $data->id;
166
167 $data->discussion = $this->get_new_parentid('forum_discussion');
168 $data->forum = $this->get_new_parentid('forum');
169 $data->userid = $this->get_mappingid('user', $data->userid);
170
171 $newitemid = $DB->insert_record('forum_discussion_subs', $data);
ef15eae4 172 $this->set_mapping('forum_discussion_sub', $oldid, $newitemid, true);
361a41d3
AN
173 }
174
2eb67dfa
AN
175 protected function process_forum_digest($data) {
176 global $DB;
177
178 $data = (object)$data;
179 $oldid = $data->id;
180
181 $data->forum = $this->get_new_parentid('forum');
182 $data->userid = $this->get_mappingid('user', $data->userid);
183
184 $newitemid = $DB->insert_record('forum_digests', $data);
185 }
186
01b4ca01
EL
187 protected function process_forum_read($data) {
188 global $DB;
189
190 $data = (object)$data;
191 $oldid = $data->id;
192
193 $data->forumid = $this->get_new_parentid('forum');
194 $data->discussionid = $this->get_mappingid('forum_discussion', $data->discussionid);
195 $data->postid = $this->get_mappingid('forum_post', $data->postid);
196 $data->userid = $this->get_mappingid('user', $data->userid);
197
198 $newitemid = $DB->insert_record('forum_read', $data);
199 }
200
41941110
EL
201 protected function process_forum_track($data) {
202 global $DB;
203
204 $data = (object)$data;
205 $oldid = $data->id;
206
207 $data->forumid = $this->get_new_parentid('forum');
208 $data->userid = $this->get_mappingid('user', $data->userid);
209
210 $newitemid = $DB->insert_record('forum_track_prefs', $data);
211 }
212
01b4ca01 213 protected function after_execute() {
01b4ca01
EL
214 // Add forum related files, no need to match by itemname (just internally handled context)
215 $this->add_related_files('mod_forum', 'intro', null);
216
9258045b
MN
217 // Add post related files, matching by itemname = 'forum_post'
218 $this->add_related_files('mod_forum', 'post', 'forum_post');
219 $this->add_related_files('mod_forum', 'attachment', 'forum_post');
220 }
221
222 protected function after_restore() {
223 global $DB;
224
01b4ca01
EL
225 // If the forum is of type 'single' and no discussion has been ignited
226 // (non-userinfo backup/restore) create the discussion here, using forum
227 // information as base for the initial post.
228 $forumid = $this->task->get_activityid();
229 $forumrec = $DB->get_record('forum', array('id' => $forumid));
230 if ($forumrec->type == 'single' && !$DB->record_exists('forum_discussions', array('forum' => $forumid))) {
231 // Create single discussion/lead post from forum data
39b2d870 232 $sd = new stdClass();
01b4ca01
EL
233 $sd->course = $forumrec->course;
234 $sd->forum = $forumrec->id;
235 $sd->name = $forumrec->name;
236 $sd->assessed = $forumrec->assessed;
237 $sd->message = $forumrec->intro;
238 $sd->messageformat = $forumrec->introformat;
239 $sd->messagetrust = true;
240 $sd->mailnow = false;
ce2d9273 241 $sdid = forum_add_discussion($sd, null, null, $this->task->get_userid());
01b4ca01
EL
242 // Mark the post as mailed
243 $DB->set_field ('forum_posts','mailed', '1', array('discussion' => $sdid));
244 // Copy all the files from mod_foum/intro to mod_forum/post
245 $fs = get_file_storage();
246 $files = $fs->get_area_files($this->task->get_contextid(), 'mod_forum', 'intro');
247 foreach ($files as $file) {
39b2d870 248 $newfilerecord = new stdClass();
01b4ca01
EL
249 $newfilerecord->filearea = 'post';
250 $newfilerecord->itemid = $DB->get_field('forum_discussions', 'firstpost', array('id' => $sdid));
251 $fs->create_file_from_storedfile($newfilerecord, $file);
252 }
253 }
01b4ca01
EL
254 }
255}