weekly release 2.3dev
[moodle.git] / mod / quiz / backup / moodle2 / restore_quiz_stepslib.php
CommitLineData
41941110 1<?php
41941110
EL
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/**
f7970e3c 18 * @package moodlecore
41941110 19 * @subpackage backup-moodle2
f7970e3c
TH
20 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41941110
EL
22 */
23
a17b297d
TH
24
25defined('MOODLE_INTERNAL') || die();
26
41941110
EL
27
28/**
29 * Structure step to restore one quiz activity
f7970e3c
TH
30 *
31 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41941110
EL
33 */
34class restore_quiz_activity_structure_step extends restore_questions_activity_structure_step {
35
36 protected function define_structure() {
37
38 $paths = array();
39 $userinfo = $this->get_setting_value('userinfo');
40
5adace99
TH
41 $quiz = new restore_path_element('quiz', '/activity/quiz');
42 $paths[] = $quiz;
43
44 // A chance for access subplugings to set up their quiz data.
45 $this->add_subplugin_structure('quizaccess', $quiz);
46
55ca80ed
TH
47 $paths[] = new restore_path_element('quiz_question_instance',
48 '/activity/quiz/question_instances/question_instance');
41941110
EL
49 $paths[] = new restore_path_element('quiz_feedback', '/activity/quiz/feedbacks/feedback');
50 $paths[] = new restore_path_element('quiz_override', '/activity/quiz/overrides/override');
5adace99 51
41941110
EL
52 if ($userinfo) {
53 $paths[] = new restore_path_element('quiz_grade', '/activity/quiz/grades/grade');
18ab06ba
TH
54
55 if ($this->task->get_old_moduleversion() > 2011010100) {
56 // Restoring from a version 2.1 dev or later.
57 // Process the new-style attempt data.
58 $quizattempt = new restore_path_element('quiz_attempt',
59 '/activity/quiz/attempts/attempt');
60 $paths[] = $quizattempt;
5adace99 61
18ab06ba
TH
62 // Add states and sessions
63 $this->add_question_usages($quizattempt, $paths);
64
5adace99
TH
65 // A chance for access subplugings to set up their attempt data.
66 $this->add_subplugin_structure('quizaccess', $quizattempt);
67
18ab06ba
TH
68 } else {
69 // Restoring from a version 2.0.x+ or earlier.
70 // Upgrade the legacy attempt data.
71 $quizattempt = new restore_path_element('quiz_attempt_legacy',
72 '/activity/quiz/attempts/attempt',
73 true);
74 $paths[] = $quizattempt;
75 $this->add_legacy_question_attempt_data($quizattempt, $paths);
76 }
41941110
EL
77 }
78
79 // Return the paths wrapped into standard activity structure
80 return $this->prepare_activity_structure($paths);
81 }
82
83 protected function process_quiz($data) {
f3ca24e4 84 global $CFG, $DB;
41941110
EL
85
86 $data = (object)$data;
87 $oldid = $data->id;
88 $data->course = $this->get_courseid();
89
90 $data->timeopen = $this->apply_date_offset($data->timeopen);
91 $data->timeclose = $this->apply_date_offset($data->timeclose);
92 $data->timecreated = $this->apply_date_offset($data->timecreated);
93 $data->timemodified = $this->apply_date_offset($data->timemodified);
94
18ab06ba
TH
95 // Needed by {@link process_quiz_attempt_legacy}
96 $this->oldquizlayout = $data->questions;
41941110
EL
97 $data->questions = $this->questions_recode_layout($data->questions);
98
7cff4927
EL
99 // quiz->attempts can come both in data->attempts and
100 // data->attempts_number, handle both. MDL-26229
101 if (isset($data->attempts_number)) {
102 $data->attempts = $data->attempts_number;
103 unset($data->attempts_number);
104 }
105
f3ca24e4
TH
106 // The old optionflags and penaltyscheme from 2.0 need to be mapped to
107 // the new preferredbehaviour. MDL-20636
108 if (!isset($data->preferredbehaviour)) {
109 if (empty($data->optionflags)) {
110 $data->preferredbehaviour = 'deferredfeedback';
111 } else if (empty($data->penaltyscheme)) {
112 $data->preferredbehaviour = 'adaptivenopenalty';
113 } else {
114 $data->preferredbehaviour = 'adaptive';
115 }
116 unset($data->optionflags);
117 unset($data->penaltyscheme);
118 }
119
120 // The old review column from 2.0 need to be split into the seven new
121 // review columns. MDL-20636
122 if (isset($data->review)) {
123 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
124
125 if (!defined('QUIZ_OLD_IMMEDIATELY')) {
126 define('QUIZ_OLD_IMMEDIATELY', 0x3c003f);
127 define('QUIZ_OLD_OPEN', 0x3c00fc0);
128 define('QUIZ_OLD_CLOSED', 0x3c03f000);
129
55ca80ed
TH
130 define('QUIZ_OLD_RESPONSES', 1*0x1041);
131 define('QUIZ_OLD_SCORES', 2*0x1041);
132 define('QUIZ_OLD_FEEDBACK', 4*0x1041);
133 define('QUIZ_OLD_ANSWERS', 8*0x1041);
134 define('QUIZ_OLD_SOLUTIONS', 16*0x1041);
135 define('QUIZ_OLD_GENERALFEEDBACK', 32*0x1041);
136 define('QUIZ_OLD_OVERALLFEEDBACK', 1*0x4440000);
f3ca24e4
TH
137 }
138
139 $oldreview = $data->review;
140
141 $data->reviewattempt =
142 mod_quiz_display_options::DURING |
55ca80ed
TH
143 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_RESPONSES ?
144 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
145 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_RESPONSES ?
146 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
147 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_RESPONSES ?
148 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
149
150 $data->reviewcorrectness =
151 mod_quiz_display_options::DURING |
55ca80ed
TH
152 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES ?
153 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
154 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_SCORES ?
155 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
156 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES ?
157 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
158
159 $data->reviewmarks =
160 mod_quiz_display_options::DURING |
55ca80ed
TH
161 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES ?
162 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
163 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_SCORES ?
164 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
165 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES ?
166 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
167
168 $data->reviewspecificfeedback =
55ca80ed
TH
169 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK ?
170 mod_quiz_display_options::DURING : 0) |
171 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK ?
172 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
173 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_FEEDBACK ?
174 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
175 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_FEEDBACK ?
176 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
177
178 $data->reviewgeneralfeedback =
55ca80ed
TH
179 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK ?
180 mod_quiz_display_options::DURING : 0) |
181 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK ?
182 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
183 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_GENERALFEEDBACK ?
184 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
185 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_GENERALFEEDBACK ?
186 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
187
188 $data->reviewrightanswer =
55ca80ed
TH
189 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS ?
190 mod_quiz_display_options::DURING : 0) |
191 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS ?
192 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
193 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_ANSWERS ?
194 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
195 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_ANSWERS ?
196 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
197
198 $data->reviewoverallfeedback =
199 0 |
55ca80ed
TH
200 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_OVERALLFEEDBACK ?
201 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
202 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_OVERALLFEEDBACK ?
203 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
204 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_OVERALLFEEDBACK ?
205 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
206 }
207
4344c5d5
TH
208 // The old popup column from from <= 2.1 need to be mapped to
209 // the new browsersecurity. MDL-29627
210 if (!isset($data->browsersecurity)) {
211 if (empty($data->popup)) {
212 $data->browsersecurity = '-';
213 } else if ($data->popup == 1) {
214 $data->browsersecurity = 'securewindow';
215 } else if ($data->popup == 2) {
216 $data->browsersecurity = 'safebrowser';
217 } else {
218 $data->preferredbehaviour = '-';
219 }
220 unset($data->popup);
221 }
222
41941110
EL
223 // insert the quiz record
224 $newitemid = $DB->insert_record('quiz', $data);
225 // immediately after inserting "activity" record, call this
226 $this->apply_activity_instance($newitemid);
227 }
228
229 protected function process_quiz_question_instance($data) {
230 global $DB;
231
232 $data = (object)$data;
233 $oldid = $data->id;
234
235 $data->quiz = $this->get_new_parentid('quiz');
236
237 $data->question = $this->get_mappingid('question', $data->question);
238
239 $DB->insert_record('quiz_question_instances', $data);
240 }
241
242 protected function process_quiz_feedback($data) {
243 global $DB;
244
245 $data = (object)$data;
246 $oldid = $data->id;
247
248 $data->quizid = $this->get_new_parentid('quiz');
249
250 $newitemid = $DB->insert_record('quiz_feedback', $data);
251 $this->set_mapping('quiz_feedback', $oldid, $newitemid, true); // Has related files
252 }
253
254 protected function process_quiz_override($data) {
255 global $DB;
256
257 $data = (object)$data;
258 $oldid = $data->id;
259
260 // Based on userinfo, we'll restore user overides or no
261 $userinfo = $this->get_setting_value('userinfo');
262
263 // Skip user overrides if we are not restoring userinfo
264 if (!$userinfo && !is_null($data->userid)) {
265 return;
266 }
267
268 $data->quiz = $this->get_new_parentid('quiz');
269
270 $data->userid = $this->get_mappingid('user', $data->userid);
271 $data->groupid = $this->get_mappingid('group', $data->groupid);
272
273 $data->timeopen = $this->apply_date_offset($data->timeopen);
274 $data->timeclose = $this->apply_date_offset($data->timeclose);
275
0f66aced
EL
276 $newitemid = $DB->insert_record('quiz_overrides', $data);
277
278 // Add mapping, restore of logs needs it
279 $this->set_mapping('quiz_override', $oldid, $newitemid);
41941110
EL
280 }
281
282 protected function process_quiz_grade($data) {
283 global $DB;
284
285 $data = (object)$data;
286 $oldid = $data->id;
287
288 $data->quiz = $this->get_new_parentid('quiz');
289
290 $data->userid = $this->get_mappingid('user', $data->userid);
291 $data->grade = $data->gradeval;
292
293 $data->timemodified = $this->apply_date_offset($data->timemodified);
294
295 $DB->insert_record('quiz_grades', $data);
296 }
297
298 protected function process_quiz_attempt($data) {
41941110 299 $data = (object)$data;
41941110
EL
300
301 $data->quiz = $this->get_new_parentid('quiz');
302 $data->attempt = $data->attemptnum;
303
41941110
EL
304 $data->userid = $this->get_mappingid('user', $data->userid);
305
306 $data->timestart = $this->apply_date_offset($data->timestart);
307 $data->timefinish = $this->apply_date_offset($data->timefinish);
308 $data->timemodified = $this->apply_date_offset($data->timemodified);
309
50de6ad0
TH
310 // The data is actually inserted into the database later in inform_new_usage_id.
311 $this->currentquizattempt = clone($data);
c749527b
TH
312 }
313
18ab06ba
TH
314 protected function process_quiz_attempt_legacy($data) {
315 global $DB;
316
317 $this->process_quiz_attempt($data);
318
319 $quiz = $DB->get_record('quiz', array('id' => $this->get_new_parentid('quiz')));
320 $quiz->oldquestions = $this->oldquizlayout;
321 $this->process_legacy_quiz_attempt_data($data, $quiz);
322 }
323
c749527b
TH
324 protected function inform_new_usage_id($newusageid) {
325 global $DB;
50de6ad0
TH
326
327 $data = $this->currentquizattempt;
328
329 $oldid = $data->id;
330 $data->uniqueid = $newusageid;
331
332 $newitemid = $DB->insert_record('quiz_attempts', $data);
333
334 // Save quiz_attempt->id mapping, because logs use it
335 $this->set_mapping('quiz_attempt', $oldid, $newitemid, false);
41941110
EL
336 }
337
338 protected function after_execute() {
c749527b 339 parent::after_execute();
41941110
EL
340 // Add quiz related files, no need to match by itemname (just internally handled context)
341 $this->add_related_files('mod_quiz', 'intro', null);
342 // Add feedback related files, matching by itemname = 'quiz_feedback'
343 $this->add_related_files('mod_quiz', 'feedback', 'quiz_feedback');
344 }
345}