MDL-31355 mod_forum: backup and restore of duedate and cutoffdate fields
[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(
0556f393 42 'type', 'name', 'intro', 'introformat', 'duedate', 'cutoffdate',
a8fb4530
EL
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
151113f9
MG
63 $tags = new backup_nested_element('poststags');
64 $tag = new backup_nested_element('tag', array('id'), array('itemid', 'rawname'));
d2ba493c 65
a8fb4530
EL
66 $ratings = new backup_nested_element('ratings');
67
68 $rating = new backup_nested_element('rating', array('id'), array(
2b04c41c 69 'component', 'ratingarea', 'scaleid', 'value', 'userid', 'timecreated', 'timemodified'));
a8fb4530 70
361a41d3
AN
71 $discussionsubs = new backup_nested_element('discussion_subs');
72
73 $discussionsub = new backup_nested_element('discussion_sub', array('id'), array(
74 'userid',
75 'preference',
76 ));
77
a8fb4530
EL
78 $subscriptions = new backup_nested_element('subscriptions');
79
80 $subscription = new backup_nested_element('subscription', array('id'), array(
81 'userid'));
82
2eb67dfa
AN
83 $digests = new backup_nested_element('digests');
84
85 $digest = new backup_nested_element('digest', array('id'), array(
86 'userid', 'maildigest'));
87
a8fb4530
EL
88 $readposts = new backup_nested_element('readposts');
89
90 $read = new backup_nested_element('read', array('id'), array(
91 'userid', 'discussionid', 'postid', 'firstread',
92 'lastread'));
93
41941110
EL
94 $trackedprefs = new backup_nested_element('trackedprefs');
95
96 $track = new backup_nested_element('track', array('id'), array(
97 'userid'));
98
a8fb4530
EL
99 // Build the tree
100
101 $forum->add_child($discussions);
102 $discussions->add_child($discussion);
103
104 $forum->add_child($subscriptions);
105 $subscriptions->add_child($subscription);
106
2eb67dfa
AN
107 $forum->add_child($digests);
108 $digests->add_child($digest);
109
a8fb4530
EL
110 $forum->add_child($readposts);
111 $readposts->add_child($read);
112
41941110
EL
113 $forum->add_child($trackedprefs);
114 $trackedprefs->add_child($track);
115
151113f9
MG
116 $forum->add_child($tags);
117 $tags->add_child($tag);
118
a8fb4530
EL
119 $discussion->add_child($posts);
120 $posts->add_child($post);
121
122 $post->add_child($ratings);
123 $ratings->add_child($rating);
124
361a41d3
AN
125 $discussion->add_child($discussionsubs);
126 $discussionsubs->add_child($discussionsub);
127
a8fb4530
EL
128 // Define sources
129
130 $forum->set_source_table('forum', array('id' => backup::VAR_ACTIVITYID));
131
132 // All these source definitions only happen if we are including user info
133 if ($userinfo) {
134 $discussion->set_source_sql('
135 SELECT *
136 FROM {forum_discussions}
137 WHERE forum = ?',
138 array(backup::VAR_PARENTID));
139
01b4ca01 140 // Need posts ordered by id so parents are always before childs on restore
52514503 141 $post->set_source_table('forum_posts', array('discussion' => backup::VAR_PARENTID), 'id ASC');
361a41d3 142 $discussionsub->set_source_table('forum_discussion_subs', array('discussion' => backup::VAR_PARENTID));
a8fb4530
EL
143
144 $subscription->set_source_table('forum_subscriptions', array('forum' => backup::VAR_PARENTID));
2eb67dfa
AN
145 $digest->set_source_table('forum_digests', array('forum' => backup::VAR_PARENTID));
146
a8fb4530
EL
147 $read->set_source_table('forum_read', array('forumid' => backup::VAR_PARENTID));
148
41941110
EL
149 $track->set_source_table('forum_track_prefs', array('forumid' => backup::VAR_PARENTID));
150
2b04c41c 151 $rating->set_source_table('rating', array('contextid' => backup::VAR_CONTEXTID,
5edac71d
EL
152 'component' => backup_helper::is_sqlparam('mod_forum'),
153 'ratingarea' => backup_helper::is_sqlparam('post'),
2b04c41c 154 'itemid' => backup::VAR_PARENTID));
a8fb4530 155 $rating->set_source_alias('rating', 'value');
d2ba493c 156
151113f9
MG
157 if (core_tag_tag::is_enabled('mod_forum', 'forum_posts')) {
158 // Backup all tags for all forum posts in this forum.
159 $tag->set_source_sql('SELECT t.id, ti.itemid, t.rawname
160 FROM {tag} t
161 JOIN {tag_instance} ti ON ti.tagid = t.id
162 WHERE ti.itemtype = ?
163 AND ti.component = ?
164 AND ti.contextid = ?', array(
165 backup_helper::is_sqlparam('forum_posts'),
166 backup_helper::is_sqlparam('mod_forum'),
167 backup::VAR_CONTEXTID));
168 }
a8fb4530
EL
169 }
170
171 // Define id annotations
172
173 $forum->annotate_ids('scale', 'scale');
174
175 $discussion->annotate_ids('group', 'groupid');
176
177 $post->annotate_ids('user', 'userid');
178
361a41d3
AN
179 $discussionsub->annotate_ids('user', 'userid');
180
a8fb4530
EL
181 $rating->annotate_ids('scale', 'scaleid');
182
183 $rating->annotate_ids('user', 'userid');
184
185 $subscription->annotate_ids('user', 'userid');
186
2eb67dfa
AN
187 $digest->annotate_ids('user', 'userid');
188
a8fb4530
EL
189 $read->annotate_ids('user', 'userid');
190
41941110
EL
191 $track->annotate_ids('user', 'userid');
192
a8fb4530
EL
193 // Define file annotations
194
64f93798 195 $forum->annotate_files('mod_forum', 'intro', null); // This file area hasn't itemid
a8fb4530 196
64f93798
PS
197 $post->annotate_files('mod_forum', 'post', 'id');
198 $post->annotate_files('mod_forum', 'attachment', 'id');
a8fb4530
EL
199
200 // Return the root element (forum), wrapped into standard activity structure
201 return $this->prepare_activity_structure($forum);
202 }
203
204}