MDL-24470 data->notification - apply correct default if null is found on restore
[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/**
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 */
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');
45 }
46
47 // Return the paths wrapped into standard activity structure
48 return $this->prepare_activity_structure($paths);
49 }
50
51 protected function process_data($data) {
52 global $DB;
53
54 $data = (object)$data;
55 $oldid = $data->id;
56 $data->course = $this->get_courseid();
57
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
65 if ($data->scale < 0) { // scale found, get mapping
66 $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
67 }
68
84dcdbdb
EL
69 // Some old backups can arrive with data->notification = null (MDL-24470)
70 // convert them to proper column default (zero)
71 if (is_null($data->notification)) {
72 $data->notification = 0;
73 }
74
fd3f6bf9
EL
75 // insert the data record
76 $newitemid = $DB->insert_record('data', $data);
77 $this->apply_activity_instance($newitemid);
78 }
79
80 protected function process_data_field($data) {
81 global $DB;
82
83 $data = (object)$data;
84 $oldid = $data->id;
85
86 $data->dataid = $this->get_new_parentid('data');
87
88 // insert the data_fields record
89 $newitemid = $DB->insert_record('data_fields', $data);
90 $this->set_mapping('data_field', $oldid, $newitemid, false); // no files associated
91 }
92
93 protected function process_data_record($data) {
94 global $DB;
95
96 $data = (object)$data;
97 $oldid = $data->id;
98
99 $data->timecreated = $this->apply_date_offset($data->timecreated);
100 $data->timemodified = $this->apply_date_offset($data->timemodified);
101
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
125 protected function process_data_rating($data) {
126 global $DB;
127
128 $data = (object)$data;
129
130 // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
131 $data->contextid = $this->task->get_contextid();
132 $data->itemid = $this->get_new_parentid('data_record');
133 if ($data->scaleid < 0) { // scale found, get mapping
134 $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
135 }
136 $data->rating = $data->value;
137 $data->userid = $this->get_mappingid('user', $data->userid);
138 $data->timecreated = $this->apply_date_offset($data->timecreated);
139 $data->timemodified = $this->apply_date_offset($data->timemodified);
140
141 $newitemid = $DB->insert_record('rating', $data);
142 }
143
144 protected function after_execute() {
145 global $DB;
146 // Add data related files, no need to match by itemname (just internally handled context)
147 $this->add_related_files('mod_data', 'intro', null);
148 // Add content related files, matching by itemname (data_content)
149 $this->add_related_files('mod_data', 'content', 'data_content');
150 // Adjust the data->defaultsort field
151 if ($defaultsort = $DB->get_field('data', 'defaultsort', array('id' => $this->get_new_parentid('data')))) {
152 if ($defaultsort = $this->get_mappingid('data_field', $defaultsort)) {
153 $DB->set_field('data', 'defaultsort', $defaultsort, array('id' => $this->get_new_parentid('data')));
154 }
155 }
156 }
157}