3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
26 * Define all the restore steps that will be used by the restore_data_activity_task
30 * Structure step to restore one data activity
32 class restore_data_activity_structure_step extends restore_activity_structure_step {
34 protected function define_structure() {
37 $userinfo = $this->get_setting_value('userinfo');
39 $paths[] = new restore_path_element('data', '/activity/data');
40 $paths[] = new restore_path_element('data_field', '/activity/data/fields/field');
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');
47 // Return the paths wrapped into standard activity structure
48 return $this->prepare_activity_structure($paths);
51 protected function process_data($data) {
54 $data = (object)$data;
56 $data->course = $this->get_courseid();
58 $data->timeavailablefrom = $this->apply_date_offset($data->timeavailablefrom);
59 $data->timeavailableto = $this->apply_date_offset($data->timeavailableto);
60 $data->timeviewfrom = $this->apply_date_offset($data->timeviewfrom);
61 $data->timeviewto = $this->apply_date_offset($data->timeviewto);
62 $data->assesstimestart = $this->apply_date_offset($data->assesstimestart);
63 $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish);
64 // Added in 3.1, hence conditional.
65 $data->timemodified = isset($data->timemodified) ? $this->apply_date_offset($data->timemodified) : time();
67 if ($data->scale < 0) { // scale found, get mapping
68 $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
71 // Some old backups can arrive with data->notification = null (MDL-24470)
72 // convert them to proper column default (zero)
73 if (is_null($data->notification)) {
74 $data->notification = 0;
77 // insert the data record
78 $newitemid = $DB->insert_record('data', $data);
79 $this->apply_activity_instance($newitemid);
82 protected function process_data_field($data) {
85 $data = (object)$data;
88 $data->dataid = $this->get_new_parentid('data');
90 // insert the data_fields record
91 $newitemid = $DB->insert_record('data_fields', $data);
92 $this->set_mapping('data_field', $oldid, $newitemid, false); // no files associated
95 protected function process_data_record($data) {
98 $data = (object)$data;
101 $data->timecreated = $this->apply_date_offset($data->timecreated);
102 $data->timemodified = $this->apply_date_offset($data->timemodified);
104 $data->userid = $this->get_mappingid('user', $data->userid);
105 $data->groupid = $this->get_mappingid('group', $data->groupid);
106 $data->dataid = $this->get_new_parentid('data');
108 // insert the data_records record
109 $newitemid = $DB->insert_record('data_records', $data);
110 $this->set_mapping('data_record', $oldid, $newitemid, false); // no files associated
113 protected function process_data_content($data) {
116 $data = (object)$data;
119 $data->fieldid = $this->get_mappingid('data_field', $data->fieldid);
120 $data->recordid = $this->get_new_parentid('data_record');
122 // insert the data_content record
123 $newitemid = $DB->insert_record('data_content', $data);
124 $this->set_mapping('data_content', $oldid, $newitemid, true); // files by this itemname
127 protected function process_data_rating($data) {
130 $data = (object)$data;
132 // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
133 $data->contextid = $this->task->get_contextid();
134 $data->itemid = $this->get_new_parentid('data_record');
135 if ($data->scaleid < 0) { // scale found, get mapping
136 $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
138 $data->rating = $data->value;
139 $data->userid = $this->get_mappingid('user', $data->userid);
140 $data->timecreated = $this->apply_date_offset($data->timecreated);
141 $data->timemodified = $this->apply_date_offset($data->timemodified);
143 // We need to check that component and ratingarea are both set here.
144 if (empty($data->component)) {
145 $data->component = 'mod_data';
147 if (empty($data->ratingarea)) {
148 $data->ratingarea = 'entry';
151 $newitemid = $DB->insert_record('rating', $data);
154 protected function after_execute() {
156 // Add data related files, no need to match by itemname (just internally handled context)
157 $this->add_related_files('mod_data', 'intro', null);
158 // Add content related files, matching by itemname (data_content)
159 $this->add_related_files('mod_data', 'content', 'data_content');
160 // Adjust the data->defaultsort field
161 if ($defaultsort = $DB->get_field('data', 'defaultsort', array('id' => $this->get_new_parentid('data')))) {
162 if ($defaultsort = $this->get_mappingid('data_field', $defaultsort)) {
163 $DB->set_field('data', 'defaultsort', $defaultsort, array('id' => $this->get_new_parentid('data')));