MDL-57455 mod_data: Implement tagging
[moodle.git] / mod / data / backup / moodle2 / restore_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 restore steps that will be used by the restore_data_activity_task
27 */
28
29/**
30 * Structure step to restore one data activity
31 */
32class restore_data_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('data', '/activity/data');
40 $paths[] = new restore_path_element('data_field', '/activity/data/fields/field');
41 if ($userinfo) {
42 $paths[] = new restore_path_element('data_record', '/activity/data/records/record');
43 $paths[] = new restore_path_element('data_content', '/activity/data/records/record/contents/content');
44 $paths[] = new restore_path_element('data_rating', '/activity/data/records/record/ratings/rating');
ca5f3e0a 45 $paths[] = new restore_path_element('data_tag', '/activity/data/records/record/tags/tag');
fd3f6bf9
EL
46 }
47
48 // Return the paths wrapped into standard activity structure
49 return $this->prepare_activity_structure($paths);
50 }
51
52 protected function process_data($data) {
53 global $DB;
54
55 $data = (object)$data;
56 $oldid = $data->id;
57 $data->course = $this->get_courseid();
58
0d14fcbc
AA
59 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
60 // See MDL-9367.
fd3f6bf9
EL
61 $data->timeavailablefrom = $this->apply_date_offset($data->timeavailablefrom);
62 $data->timeavailableto = $this->apply_date_offset($data->timeavailableto);
63 $data->timeviewfrom = $this->apply_date_offset($data->timeviewfrom);
64 $data->timeviewto = $this->apply_date_offset($data->timeviewto);
65 $data->assesstimestart = $this->apply_date_offset($data->assesstimestart);
66 $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish);
67
68 if ($data->scale < 0) { // scale found, get mapping
69 $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
70 }
71
84dcdbdb
EL
72 // Some old backups can arrive with data->notification = null (MDL-24470)
73 // convert them to proper column default (zero)
74 if (is_null($data->notification)) {
75 $data->notification = 0;
76 }
77
fd3f6bf9
EL
78 // insert the data record
79 $newitemid = $DB->insert_record('data', $data);
80 $this->apply_activity_instance($newitemid);
81 }
82
83 protected function process_data_field($data) {
84 global $DB;
85
86 $data = (object)$data;
87 $oldid = $data->id;
88
89 $data->dataid = $this->get_new_parentid('data');
90
91 // insert the data_fields record
92 $newitemid = $DB->insert_record('data_fields', $data);
93 $this->set_mapping('data_field', $oldid, $newitemid, false); // no files associated
94 }
95
96 protected function process_data_record($data) {
97 global $DB;
98
99 $data = (object)$data;
100 $oldid = $data->id;
101
fd3f6bf9
EL
102 $data->userid = $this->get_mappingid('user', $data->userid);
103 $data->groupid = $this->get_mappingid('group', $data->groupid);
104 $data->dataid = $this->get_new_parentid('data');
105
106 // insert the data_records record
107 $newitemid = $DB->insert_record('data_records', $data);
108 $this->set_mapping('data_record', $oldid, $newitemid, false); // no files associated
109 }
110
111 protected function process_data_content($data) {
112 global $DB;
113
114 $data = (object)$data;
115 $oldid = $data->id;
116
117 $data->fieldid = $this->get_mappingid('data_field', $data->fieldid);
118 $data->recordid = $this->get_new_parentid('data_record');
119
120 // insert the data_content record
121 $newitemid = $DB->insert_record('data_content', $data);
122 $this->set_mapping('data_content', $oldid, $newitemid, true); // files by this itemname
123 }
124
ca5f3e0a
AH
125 /**
126 * Add tags to restored records.
127 *
128 * @param stdClass $data Tag
129 */
130 protected function process_data_tag($data) {
131 $data = (object)$data;
132
133 if (!core_tag_tag::is_enabled('mod_data', 'data_records')) { // Tags disabled in server, nothing to process.
134 return;
135 }
136
137 $tag = $data->rawname;
138 $itemid = $this->get_new_parentid('data_record');
139
140 $context = context_module::instance($this->task->get_moduleid());
141 core_tag_tag::add_item_tag('mod_data', 'data_records', $itemid, $context, $tag);
142 }
143
fd3f6bf9
EL
144 protected function process_data_rating($data) {
145 global $DB;
146
147 $data = (object)$data;
148
149 // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
150 $data->contextid = $this->task->get_contextid();
151 $data->itemid = $this->get_new_parentid('data_record');
152 if ($data->scaleid < 0) { // scale found, get mapping
153 $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
154 }
155 $data->rating = $data->value;
156 $data->userid = $this->get_mappingid('user', $data->userid);
fd3f6bf9 157
2b04c41c
SH
158 // We need to check that component and ratingarea are both set here.
159 if (empty($data->component)) {
160 $data->component = 'mod_data';
161 }
162 if (empty($data->ratingarea)) {
163 $data->ratingarea = 'entry';
164 }
165
fd3f6bf9
EL
166 $newitemid = $DB->insert_record('rating', $data);
167 }
168
169 protected function after_execute() {
170 global $DB;
171 // Add data related files, no need to match by itemname (just internally handled context)
172 $this->add_related_files('mod_data', 'intro', null);
173 // Add content related files, matching by itemname (data_content)
174 $this->add_related_files('mod_data', 'content', 'data_content');
175 // Adjust the data->defaultsort field
176 if ($defaultsort = $DB->get_field('data', 'defaultsort', array('id' => $this->get_new_parentid('data')))) {
177 if ($defaultsort = $this->get_mappingid('data_field', $defaultsort)) {
178 $DB->set_field('data', 'defaultsort', $defaultsort, array('id' => $this->get_new_parentid('data')));
179 }
180 }
181 }
182}