MDL-41249 assign: Restore step missing gradingform mapping
[moodle.git] / mod / assign / backup / moodle2 / restore_assign_stepslib.php
CommitLineData
bbd0e548
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Define all the restore steps that will be used by the restore_assign_activity_task
19 *
20 * @package mod_assign
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Define the complete assignment structure for restore, with file and id annotations
29 *
30 * @package mod_assign
31 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class restore_assign_activity_structure_step extends restore_activity_structure_step {
35
36 /**
e5403f8c
DW
37 * Define the structure of the restore workflow.
38 *
bbd0e548
DW
39 * @return restore_path_element $structure
40 */
41 protected function define_structure() {
42
43 $paths = array();
e5403f8c 44 // To know if we are including userinfo.
bbd0e548
DW
45 $userinfo = $this->get_setting_value('userinfo');
46
e5403f8c 47 // Define each element separated.
bbd0e548
DW
48 $paths[] = new restore_path_element('assign', '/activity/assign');
49 if ($userinfo) {
e5403f8c
DW
50 $submission = new restore_path_element('assign_submission',
51 '/activity/assign/submissions/submission');
bbd0e548
DW
52 $paths[] = $submission;
53 $this->add_subplugin_structure('assignsubmission', $submission);
54 $grade = new restore_path_element('assign_grade', '/activity/assign/grades/grade');
55 $paths[] = $grade;
56 $this->add_subplugin_structure('assignfeedback', $grade);
57 }
e5403f8c
DW
58 $paths[] = new restore_path_element('assign_plugin_config',
59 '/activity/assign/plugin_configs/plugin_config');
bbd0e548
DW
60
61 return $this->prepare_activity_structure($paths);
62 }
63
64 /**
e5403f8c
DW
65 * Process an assign restore.
66 *
bbd0e548
DW
67 * @param object $data The data in object form
68 * @return void
69 */
70 protected function process_assign($data) {
71 global $DB;
72
73 $data = (object)$data;
74 $oldid = $data->id;
75 $data->course = $this->get_courseid();
76
77 $data->timemodified = $this->apply_date_offset($data->timemodified);
78 $data->allowsubmissionsfromdate = $this->apply_date_offset($data->allowsubmissionsfromdate);
79 $data->duedate = $this->apply_date_offset($data->duedate);
b3de95af 80 if (!empty($data->teamsubmissiongroupingid)) {
e5403f8c
DW
81 $data->teamsubmissiongroupingid = $this->get_mappingid('grouping',
82 $data->teamsubmissiongroupingid);
b3de95af
DW
83 } else {
84 $data->teamsubmissiongroupingid = 0;
12a1a0da 85 }
bbd0e548 86
9e795179
DW
87 if (!isset($data->cutoffdate)) {
88 $data->cutoffdate = 0;
89 }
f8d107b3
DM
90 if (!isset($data->markingworkflow)) {
91 $data->markingworkflow = 0;
92 }
93 if (!isset($data->markingallocation)) {
94 $data->markingallocation = 0;
95 }
9e795179
DW
96
97 if (!empty($data->preventlatesubmissions)) {
98 $data->cutoffdate = $data->duedate;
99 } else {
100 $data->cutoffdate = $this->apply_date_offset($data->cutoffdate);
101 }
102
bbd0e548
DW
103 $newitemid = $DB->insert_record('assign', $data);
104
105 $this->apply_activity_instance($newitemid);
106 }
107
108 /**
109 * Process a submission restore
110 * @param object $data The data in object form
111 * @return void
112 */
113 protected function process_assign_submission($data) {
114 global $DB;
115
116 $data = (object)$data;
117 $oldid = $data->id;
118
119 $data->assignment = $this->get_new_parentid('assign');
120
121 $data->timemodified = $this->apply_date_offset($data->timemodified);
122 $data->timecreated = $this->apply_date_offset($data->timecreated);
12a1a0da
DW
123 if ($data->userid > 0) {
124 $data->userid = $this->get_mappingid('user', $data->userid);
125 }
b3de95af 126 if (!empty($data->groupid)) {
12a1a0da 127 $data->groupid = $this->get_mappingid('group', $data->groupid);
b3de95af
DW
128 } else {
129 $data->groupid = 0;
12a1a0da 130 }
bbd0e548
DW
131
132 $newitemid = $DB->insert_record('assign_submission', $data);
133
134 // Note - the old contextid is required in order to be able to restore files stored in
e5403f8c 135 // sub plugin file areas attached to the submissionid.
bbd0e548 136 $this->set_mapping('submission', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
b5711fe5 137 $this->set_mapping(restore_gradingform_plugin::itemid_mapping('submissions'), $oldid, $newitemid);
bbd0e548
DW
138 }
139
df211804
DW
140 /**
141 * Process a user_flags restore
142 * @param object $data The data in object form
143 * @return void
144 */
145 protected function process_assign_userflags($data) {
146 global $DB;
147
148 $data = (object)$data;
149 $oldid = $data->id;
150
151 $data->assignment = $this->get_new_parentid('assign');
152
153 $data->userid = $this->get_mappingid('user', $data->userid);
154 if (!empty($data->extensionduedate)) {
155 $data->extensionduedate = $this->apply_date_offset($data->extensionduedate);
156 } else {
157 $data->extensionduedate = 0;
158 }
159 // Flags mailed and locked need no translation on restore.
160
161 $newitemid = $DB->insert_record('assign_user_flags', $data);
162 }
163
bbd0e548
DW
164 /**
165 * Process a grade restore
166 * @param object $data The data in object form
167 * @return void
168 */
169 protected function process_assign_grade($data) {
170 global $DB;
171
172 $data = (object)$data;
173 $oldid = $data->id;
174
175 $data->assignment = $this->get_new_parentid('assign');
176
177 $data->timemodified = $this->apply_date_offset($data->timemodified);
178 $data->timecreated = $this->apply_date_offset($data->timecreated);
179 $data->userid = $this->get_mappingid('user', $data->userid);
180 $data->grader = $this->get_mappingid('user', $data->grader);
df211804
DW
181
182 // Handle flags restore to a different table.
183 $flags = new stdClass();
184 $flags->assignment = $this->get_new_parentid('assign');
b3de95af 185 if (!empty($data->extensionduedate)) {
df211804
DW
186 $flags->extensionduedate = $this->apply_date_offset($data->extensionduedate);
187 }
188 if (!empty($data->mailed)) {
189 $flags->mailed = $data->mailed;
190 }
191 if (!empty($data->locked)) {
192 $flags->locked = $data->locked;
b3de95af 193 }
df211804 194 $DB->insert_record('assign_user_flags', $flags);
bbd0e548
DW
195
196 $newitemid = $DB->insert_record('assign_grades', $data);
197
198 // Note - the old contextid is required in order to be able to restore files stored in
e5403f8c 199 // sub plugin file areas attached to the gradeid.
bbd0e548
DW
200 $this->set_mapping('grade', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
201 }
202
203 /**
204 * Process a plugin-config restore
205 * @param object $data The data in object form
206 * @return void
207 */
208 protected function process_assign_plugin_config($data) {
209 global $DB;
210
211 $data = (object)$data;
212 $oldid = $data->id;
213
214 $data->assignment = $this->get_new_parentid('assign');
215
216 $newitemid = $DB->insert_record('assign_plugin_config', $data);
bbd0e548
DW
217 }
218
219 /**
220 * Once the database tables have been fully restored, restore the files
221 * @return void
222 */
223 protected function after_execute() {
224 $this->add_related_files('mod_assign', 'intro', null);
225 }
226}