MDL-53367 mod_forum: Subscribe current users when importing a forum
[moodle.git] / mod / forum / backup / moodle2 / restore_forum_stepslib.php
1 <?php
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/>.
18 /**
19  * @package    mod_forum
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  */
25 /**
26  * Define all the restore steps that will be used by the restore_forum_activity_task
27  */
29 /**
30  * Structure step to restore one forum activity
31  */
32 class restore_forum_activity_structure_step extends restore_activity_structure_step {
34     protected function define_structure() {
36         $paths = array();
37         $userinfo = $this->get_setting_value('userinfo');
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');
43             $paths[] = new restore_path_element('forum_discussion_sub', '/activity/forum/discussions/discussion/discussion_subs/discussion_sub');
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');
46             $paths[] = new restore_path_element('forum_digest', '/activity/forum/digests/digest');
47             $paths[] = new restore_path_element('forum_read', '/activity/forum/readposts/read');
48             $paths[] = new restore_path_element('forum_track', '/activity/forum/trackedprefs/track');
49         }
51         // Return the paths wrapped into standard activity structure
52         return $this->prepare_activity_structure($paths);
53     }
55     protected function process_forum($data) {
56         global $DB;
58         $data = (object)$data;
59         $oldid = $data->id;
60         $data->course = $this->get_courseid();
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         }
68         $newitemid = $DB->insert_record('forum', $data);
69         $this->apply_activity_instance($newitemid);
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);
75     }
77     protected function process_forum_discussion($data) {
78         global $DB;
80         $data = (object)$data;
81         $oldid = $data->id;
82         $data->course = $this->get_courseid();
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);
92         $newitemid = $DB->insert_record('forum_discussions', $data);
93         $this->set_mapping('forum_discussion', $oldid, $newitemid);
94     }
96     protected function process_forum_post($data) {
97         global $DB;
99         $data = (object)$data;
100         $oldid = $data->id;
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         }
111         $newitemid = $DB->insert_record('forum_posts', $data);
112         $this->set_mapping('forum_post', $oldid, $newitemid, true);
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     }
120     protected function process_forum_rating($data) {
121         global $DB;
123         $data = (object)$data;
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);
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         }
144         $newitemid = $DB->insert_record('rating', $data);
145     }
147     protected function process_forum_subscription($data) {
148         global $DB;
150         $data = (object)$data;
151         $oldid = $data->id;
153         $data->forum = $this->get_new_parentid('forum');
154         $data->userid = $this->get_mappingid('user', $data->userid);
156         $newitemid = $DB->insert_record('forum_subscriptions', $data);
157         $this->set_mapping('forum_subscription', $oldid, $newitemid, true);
159     }
161     protected function process_forum_discussion_sub($data) {
162         global $DB;
164         $data = (object)$data;
165         $oldid = $data->id;
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);
171         $newitemid = $DB->insert_record('forum_discussion_subs', $data);
172         $this->set_mapping('forum_discussion_sub', $oldid, $newitemid, true);
173     }
175     protected function process_forum_digest($data) {
176         global $DB;
178         $data = (object)$data;
179         $oldid = $data->id;
181         $data->forum = $this->get_new_parentid('forum');
182         $data->userid = $this->get_mappingid('user', $data->userid);
184         $newitemid = $DB->insert_record('forum_digests', $data);
185     }
187     protected function process_forum_read($data) {
188         global $DB;
190         $data = (object)$data;
191         $oldid = $data->id;
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);
198         $newitemid = $DB->insert_record('forum_read', $data);
199     }
201     protected function process_forum_track($data) {
202         global $DB;
204         $data = (object)$data;
205         $oldid = $data->id;
207         $data->forumid = $this->get_new_parentid('forum');
208         $data->userid = $this->get_mappingid('user', $data->userid);
210         $newitemid = $DB->insert_record('forum_track_prefs', $data);
211     }
213     protected function after_execute() {
214         // Add forum related files, no need to match by itemname (just internally handled context)
215         $this->add_related_files('mod_forum', 'intro', null);
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     }
222     protected function after_restore() {
223         global $DB;
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
232             $sd = new stdClass();
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;
241             $sdid = forum_add_discussion($sd, null, null, $this->task->get_userid());
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) {
248                 $newfilerecord = new stdClass();
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         }
254     }