MDL-57455 mod_data: Implement tagging
[moodle.git] / mod / data / backup / moodle2 / backup_data_stepslib.php
CommitLineData
fd3f6bf9
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/**
002a2a44 19 * @package mod_data
fd3f6bf9
EL
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 */
24
25/**
26 * Define all the backup steps that will be used by the backup_data_activity_task
27 */
28
29/**
30 * Define the complete data structure for backup, with file and id annotations
31 */
32class backup_data_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 $data = new backup_nested_element('data', array('id'), array(
41 'name', 'intro', 'introformat', 'comments',
42 'timeavailablefrom', 'timeavailableto', 'timeviewfrom', 'timeviewto',
43 'requiredentries', 'requiredentriestoview', 'maxentries', 'rssarticles',
44 'singletemplate', 'listtemplate', 'listtemplateheader', 'listtemplatefooter',
45 'addtemplate', 'rsstemplate', 'rsstitletemplate', 'csstemplate',
97fe80ad 46 'jstemplate', 'asearchtemplate', 'approval', 'manageapproved', 'scale',
fd3f6bf9 47 'assessed', 'assesstimestart', 'assesstimefinish', 'defaultsort',
f9729dcf 48 'defaultsortdir', 'editany', 'notification', 'timemodified', 'config', 'completionentries'));
fd3f6bf9 49
ca5f3e0a
AH
50 $tags = new backup_nested_element('tags');
51 $tag = new backup_nested_element('tag', array('id'), array('name', 'rawname'));
52
fd3f6bf9
EL
53 $fields = new backup_nested_element('fields');
54
55 $field = new backup_nested_element('field', array('id'), array(
b89cca19 56 'type', 'name', 'description', 'required', 'param1', 'param2',
fd3f6bf9
EL
57 'param3', 'param4', 'param5', 'param6',
58 'param7', 'param8', 'param9', 'param10'));
59
60 $records = new backup_nested_element('records');
61
62 $record = new backup_nested_element('record', array('id'), array(
63 'userid', 'groupid', 'timecreated', 'timemodified',
64 'approved'));
65
66 $contents = new backup_nested_element('contents');
67
68 $content = new backup_nested_element('content', array('id'), array(
69 'fieldid', 'content', 'content1', 'content2',
70 'content3', 'content4'));
71
72 $ratings = new backup_nested_element('ratings');
73
74 $rating = new backup_nested_element('rating', array('id'), array(
2b04c41c 75 'component', 'ratingarea', 'scaleid', 'value', 'userid', 'timecreated', 'timemodified'));
fd3f6bf9
EL
76
77 // Build the tree
78 $data->add_child($fields);
79 $fields->add_child($field);
80
81 $data->add_child($records);
82 $records->add_child($record);
83
84 $record->add_child($contents);
85 $contents->add_child($content);
86
87 $record->add_child($ratings);
88 $ratings->add_child($rating);
89
ca5f3e0a
AH
90 $record->add_child($tags);
91 $tags->add_child($tag);
92
fd3f6bf9
EL
93 // Define sources
94 $data->set_source_table('data', array('id' => backup::VAR_ACTIVITYID));
95
96 $field->set_source_sql('
97 SELECT *
98 FROM {data_fields}
99 WHERE dataid = ?',
100 array(backup::VAR_PARENTID));
101
102 // All the rest of elements only happen if we are including user info
103 if ($userinfo) {
104 $record->set_source_table('data_records', array('dataid' => backup::VAR_PARENTID));
105
106 $content->set_source_table('data_content', array('recordid' => backup::VAR_PARENTID));
107
2b04c41c
SH
108 $rating->set_source_table('rating', array('contextid' => backup::VAR_CONTEXTID,
109 'itemid' => backup::VAR_PARENTID,
5edac71d
EL
110 'component' => backup_helper::is_sqlparam('mod_data'),
111 'ratingarea' => backup_helper::is_sqlparam('entry')));
fd3f6bf9 112 $rating->set_source_alias('rating', 'value');
ca5f3e0a
AH
113 $tag->set_source_sql('SELECT t.id, t.name, t.rawname
114 FROM {tag} t
115 JOIN {tag_instance} ti
116 ON ti.tagid = t.id
117 WHERE ti.itemtype = ?
118 AND ti.component = ?
119 AND ti.itemid = ?', array(
120 backup_helper::is_sqlparam('data_records'),
121 backup_helper::is_sqlparam('mod_data'),
122 backup::VAR_PARENTID));
fd3f6bf9
EL
123 }
124
125 // Define id annotations
126 $data->annotate_ids('scale', 'scale');
127
128 $record->annotate_ids('user', 'userid');
129 $record->annotate_ids('group', 'groupid');
130
131 $rating->annotate_ids('scale', 'scaleid');
132 $rating->annotate_ids('user', 'userid');
133
134 // Define file annotations
135 $data->annotate_files('mod_data', 'intro', null); // This file area hasn't itemid
136 $content->annotate_files('mod_data', 'content', 'id'); // By content->id
137
138 // Return the root element (data), wrapped into standard activity structure
139 return $this->prepare_activity_structure($data);
140 }
141}