MDL-55982 mod_forum: Add time-based discussion locking
[moodle.git] / mod / forum / backup / moodle2 / backup_forum_stepslib.php
CommitLineData
a8fb4530
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
a8fb4530 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
a8fb4530
EL
23 */
24
25/**
26 * Define all the backup steps that will be used by the backup_forum_activity_task
27 */
28
29/**
30 * Define the complete forum structure for backup, with file and id annotations
31 */
32class backup_forum_activity_structure_step extends backup_activity_structure_step {
33
34 protected function define_structure() {
35
36 // To know if we are including userinfo
37 $userinfo = $this->get_setting_value('userinfo');
38
39 // Define each element separated
40
41 $forum = new backup_nested_element('forum', array('id'), array(
42 'type', 'name', 'intro', 'introformat',
43 'assessed', 'assesstimestart', 'assesstimefinish', 'scale',
44 'maxbytes', 'maxattachments', 'forcesubscribe', 'trackingtype',
45 'rsstype', 'rssarticles', 'timemodified', 'warnafter',
46 'blockafter', 'blockperiod', 'completiondiscussions', 'completionreplies',
0f3bbfd4 47 'completionposts', 'displaywordcount', 'lockdiscussionafter'));
a8fb4530
EL
48
49 $discussions = new backup_nested_element('discussions');
50
51 $discussion = new backup_nested_element('discussion', array('id'), array(
52 'name', 'firstpost', 'userid', 'groupid',
53 'assessed', 'timemodified', 'usermodified', 'timestart',
87b007b4 54 'timeend', 'pinned'));
a8fb4530
EL
55
56 $posts = new backup_nested_element('posts');
57
58 $post = new backup_nested_element('post', array('id'), array(
59 'parent', 'userid', 'created', 'modified',
60 'mailed', 'subject', 'message', 'messageformat',
61 'messagetrust', 'attachment', 'totalscore', 'mailnow'));
62
63 $ratings = new backup_nested_element('ratings');
64
65 $rating = new backup_nested_element('rating', array('id'), array(
2b04c41c 66 'component', 'ratingarea', 'scaleid', 'value', 'userid', 'timecreated', 'timemodified'));
a8fb4530 67
361a41d3
AN
68 $discussionsubs = new backup_nested_element('discussion_subs');
69
70 $discussionsub = new backup_nested_element('discussion_sub', array('id'), array(
71 'userid',
72 'preference',
73 ));
74
a8fb4530
EL
75 $subscriptions = new backup_nested_element('subscriptions');
76
77 $subscription = new backup_nested_element('subscription', array('id'), array(
78 'userid'));
79
2eb67dfa
AN
80 $digests = new backup_nested_element('digests');
81
82 $digest = new backup_nested_element('digest', array('id'), array(
83 'userid', 'maildigest'));
84
a8fb4530
EL
85 $readposts = new backup_nested_element('readposts');
86
87 $read = new backup_nested_element('read', array('id'), array(
88 'userid', 'discussionid', 'postid', 'firstread',
89 'lastread'));
90
41941110
EL
91 $trackedprefs = new backup_nested_element('trackedprefs');
92
93 $track = new backup_nested_element('track', array('id'), array(
94 'userid'));
95
a8fb4530
EL
96 // Build the tree
97
98 $forum->add_child($discussions);
99 $discussions->add_child($discussion);
100
101 $forum->add_child($subscriptions);
102 $subscriptions->add_child($subscription);
103
2eb67dfa
AN
104 $forum->add_child($digests);
105 $digests->add_child($digest);
106
a8fb4530
EL
107 $forum->add_child($readposts);
108 $readposts->add_child($read);
109
41941110
EL
110 $forum->add_child($trackedprefs);
111 $trackedprefs->add_child($track);
112
a8fb4530
EL
113 $discussion->add_child($posts);
114 $posts->add_child($post);
115
116 $post->add_child($ratings);
117 $ratings->add_child($rating);
118
361a41d3
AN
119 $discussion->add_child($discussionsubs);
120 $discussionsubs->add_child($discussionsub);
121
a8fb4530
EL
122 // Define sources
123
124 $forum->set_source_table('forum', array('id' => backup::VAR_ACTIVITYID));
125
126 // All these source definitions only happen if we are including user info
127 if ($userinfo) {
128 $discussion->set_source_sql('
129 SELECT *
130 FROM {forum_discussions}
131 WHERE forum = ?',
132 array(backup::VAR_PARENTID));
133
01b4ca01 134 // Need posts ordered by id so parents are always before childs on restore
52514503 135 $post->set_source_table('forum_posts', array('discussion' => backup::VAR_PARENTID), 'id ASC');
361a41d3 136 $discussionsub->set_source_table('forum_discussion_subs', array('discussion' => backup::VAR_PARENTID));
a8fb4530
EL
137
138 $subscription->set_source_table('forum_subscriptions', array('forum' => backup::VAR_PARENTID));
2eb67dfa
AN
139 $digest->set_source_table('forum_digests', array('forum' => backup::VAR_PARENTID));
140
a8fb4530
EL
141 $read->set_source_table('forum_read', array('forumid' => backup::VAR_PARENTID));
142
41941110
EL
143 $track->set_source_table('forum_track_prefs', array('forumid' => backup::VAR_PARENTID));
144
2b04c41c 145 $rating->set_source_table('rating', array('contextid' => backup::VAR_CONTEXTID,
5edac71d
EL
146 'component' => backup_helper::is_sqlparam('mod_forum'),
147 'ratingarea' => backup_helper::is_sqlparam('post'),
2b04c41c 148 'itemid' => backup::VAR_PARENTID));
a8fb4530
EL
149 $rating->set_source_alias('rating', 'value');
150 }
151
152 // Define id annotations
153
154 $forum->annotate_ids('scale', 'scale');
155
156 $discussion->annotate_ids('group', 'groupid');
157
158 $post->annotate_ids('user', 'userid');
159
361a41d3
AN
160 $discussionsub->annotate_ids('user', 'userid');
161
a8fb4530
EL
162 $rating->annotate_ids('scale', 'scaleid');
163
164 $rating->annotate_ids('user', 'userid');
165
166 $subscription->annotate_ids('user', 'userid');
167
2eb67dfa
AN
168 $digest->annotate_ids('user', 'userid');
169
a8fb4530
EL
170 $read->annotate_ids('user', 'userid');
171
41941110
EL
172 $track->annotate_ids('user', 'userid');
173
a8fb4530
EL
174 // Define file annotations
175
64f93798 176 $forum->annotate_files('mod_forum', 'intro', null); // This file area hasn't itemid
a8fb4530 177
64f93798
PS
178 $post->annotate_files('mod_forum', 'post', 'id');
179 $post->annotate_files('mod_forum', 'attachment', 'id');
a8fb4530
EL
180
181 // Return the root element (forum), wrapped into standard activity structure
182 return $this->prepare_activity_structure($forum);
183 }
184
185}