b89955cdc91d1e8d0e0bc65b423d3cb0938fa180
[moodle.git] / mod / data / backup / moodle2 / restore_data_stepslib.php
1 <?php
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/>.
18 /**
19  * @package moodlecore
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  */
25 /**
26  * Define all the restore steps that will be used by the restore_data_activity_task
27  */
29 /**
30  * Structure step to restore one data activity
31  */
32 class restore_data_activity_structure_step extends restore_activity_structure_step {
34     protected function define_structure() {
36         $paths = array();
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');
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');
45         }
47         // Return the paths wrapped into standard activity structure
48         return $this->prepare_activity_structure($paths);
49     }
51     protected function process_data($data) {
52         global $DB;
54         $data = (object)$data;
55         $oldid = $data->id;
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);
65         if ($data->scale < 0) { // scale found, get mapping
66             $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
67         }
69         // insert the data record
70         $newitemid = $DB->insert_record('data', $data);
71         $this->apply_activity_instance($newitemid);
72     }
74     protected function process_data_field($data) {
75         global $DB;
77         $data = (object)$data;
78         $oldid = $data->id;
80         $data->dataid = $this->get_new_parentid('data');
82         // insert the data_fields record
83         $newitemid = $DB->insert_record('data_fields', $data);
84         $this->set_mapping('data_field', $oldid, $newitemid, false); // no files associated
85     }
87     protected function process_data_record($data) {
88         global $DB;
90         $data = (object)$data;
91         $oldid = $data->id;
93         $data->timecreated = $this->apply_date_offset($data->timecreated);
94         $data->timemodified = $this->apply_date_offset($data->timemodified);
96         $data->userid = $this->get_mappingid('user', $data->userid);
97         $data->groupid = $this->get_mappingid('group', $data->groupid);
98         $data->dataid = $this->get_new_parentid('data');
100         // insert the data_records record
101         $newitemid = $DB->insert_record('data_records', $data);
102         $this->set_mapping('data_record', $oldid, $newitemid, false); // no files associated
103     }
105     protected function process_data_content($data) {
106         global $DB;
108         $data = (object)$data;
109         $oldid = $data->id;
111         $data->fieldid = $this->get_mappingid('data_field', $data->fieldid);
112         $data->recordid = $this->get_new_parentid('data_record');
114         // insert the data_content record
115         $newitemid = $DB->insert_record('data_content', $data);
116         $this->set_mapping('data_content', $oldid, $newitemid, true); // files by this itemname
117     }
119     protected function process_data_rating($data) {
120         global $DB;
122         $data = (object)$data;
124         // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
125         $data->contextid = $this->task->get_contextid();
126         $data->itemid    = $this->get_new_parentid('data_record');
127         if ($data->scaleid < 0) { // scale found, get mapping
128             $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
129         }
130         $data->rating = $data->value;
131         $data->userid = $this->get_mappingid('user', $data->userid);
132         $data->timecreated = $this->apply_date_offset($data->timecreated);
133         $data->timemodified = $this->apply_date_offset($data->timemodified);
135         $newitemid = $DB->insert_record('rating', $data);
136     }
138     protected function after_execute() {
139         global $DB;
140         // Add data related files, no need to match by itemname (just internally handled context)
141         $this->add_related_files('mod_data', 'intro', null);
142         // Add content related files, matching by itemname (data_content)
143         $this->add_related_files('mod_data', 'content', 'data_content');
144         // Adjust the data->defaultsort field
145         if ($defaultsort = $DB->get_field('data', 'defaultsort', array('id' => $this->get_new_parentid('data')))) {
146             if ($defaultsort = $this->get_mappingid('data_field', $defaultsort)) {
147                 $DB->set_field('data', 'defaultsort', $defaultsort, array('id' => $this->get_new_parentid('data')));
148             }
149         }
150     }