MDL-27639 add two missing language strings.
[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
41 $paths[] = new restore_path_element('quiz', '/activity/quiz');
55ca80ed
TH
42 $paths[] = new restore_path_element('quiz_question_instance',
43 '/activity/quiz/question_instances/question_instance');
41941110
EL
44 $paths[] = new restore_path_element('quiz_feedback', '/activity/quiz/feedbacks/feedback');
45 $paths[] = new restore_path_element('quiz_override', '/activity/quiz/overrides/override');
46 if ($userinfo) {
47 $paths[] = new restore_path_element('quiz_grade', '/activity/quiz/grades/grade');
55ca80ed
TH
48 $quizattempt = new restore_path_element('quiz_attempt',
49 '/activity/quiz/attempts/attempt');
41941110
EL
50 $paths[] = $quizattempt;
51 // Add states and sessions
bea1a6a7 52 $this->add_question_usages($quizattempt, $paths);
41941110
EL
53 }
54
55 // Return the paths wrapped into standard activity structure
56 return $this->prepare_activity_structure($paths);
57 }
58
59 protected function process_quiz($data) {
f3ca24e4 60 global $CFG, $DB;
41941110
EL
61
62 $data = (object)$data;
63 $oldid = $data->id;
64 $data->course = $this->get_courseid();
65
66 $data->timeopen = $this->apply_date_offset($data->timeopen);
67 $data->timeclose = $this->apply_date_offset($data->timeclose);
68 $data->timecreated = $this->apply_date_offset($data->timecreated);
69 $data->timemodified = $this->apply_date_offset($data->timemodified);
70
71 $data->questions = $this->questions_recode_layout($data->questions);
72
7cff4927
EL
73 // quiz->attempts can come both in data->attempts and
74 // data->attempts_number, handle both. MDL-26229
75 if (isset($data->attempts_number)) {
76 $data->attempts = $data->attempts_number;
77 unset($data->attempts_number);
78 }
79
f3ca24e4
TH
80 // The old optionflags and penaltyscheme from 2.0 need to be mapped to
81 // the new preferredbehaviour. MDL-20636
82 if (!isset($data->preferredbehaviour)) {
83 if (empty($data->optionflags)) {
84 $data->preferredbehaviour = 'deferredfeedback';
85 } else if (empty($data->penaltyscheme)) {
86 $data->preferredbehaviour = 'adaptivenopenalty';
87 } else {
88 $data->preferredbehaviour = 'adaptive';
89 }
90 unset($data->optionflags);
91 unset($data->penaltyscheme);
92 }
93
94 // The old review column from 2.0 need to be split into the seven new
95 // review columns. MDL-20636
96 if (isset($data->review)) {
97 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
98
99 if (!defined('QUIZ_OLD_IMMEDIATELY')) {
100 define('QUIZ_OLD_IMMEDIATELY', 0x3c003f);
101 define('QUIZ_OLD_OPEN', 0x3c00fc0);
102 define('QUIZ_OLD_CLOSED', 0x3c03f000);
103
55ca80ed
TH
104 define('QUIZ_OLD_RESPONSES', 1*0x1041);
105 define('QUIZ_OLD_SCORES', 2*0x1041);
106 define('QUIZ_OLD_FEEDBACK', 4*0x1041);
107 define('QUIZ_OLD_ANSWERS', 8*0x1041);
108 define('QUIZ_OLD_SOLUTIONS', 16*0x1041);
109 define('QUIZ_OLD_GENERALFEEDBACK', 32*0x1041);
110 define('QUIZ_OLD_OVERALLFEEDBACK', 1*0x4440000);
f3ca24e4
TH
111 }
112
113 $oldreview = $data->review;
114
115 $data->reviewattempt =
116 mod_quiz_display_options::DURING |
55ca80ed
TH
117 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_RESPONSES ?
118 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
119 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_RESPONSES ?
120 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
121 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_RESPONSES ?
122 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
123
124 $data->reviewcorrectness =
125 mod_quiz_display_options::DURING |
55ca80ed
TH
126 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES ?
127 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
128 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_SCORES ?
129 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
130 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES ?
131 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
132
133 $data->reviewmarks =
134 mod_quiz_display_options::DURING |
55ca80ed
TH
135 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES ?
136 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
137 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_SCORES ?
138 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
139 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES ?
140 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
141
142 $data->reviewspecificfeedback =
55ca80ed
TH
143 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK ?
144 mod_quiz_display_options::DURING : 0) |
145 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK ?
146 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
147 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_FEEDBACK ?
148 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
149 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_FEEDBACK ?
150 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
151
152 $data->reviewgeneralfeedback =
55ca80ed
TH
153 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK ?
154 mod_quiz_display_options::DURING : 0) |
155 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK ?
156 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
157 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_GENERALFEEDBACK ?
158 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
159 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_GENERALFEEDBACK ?
160 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
161
162 $data->reviewrightanswer =
55ca80ed
TH
163 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS ?
164 mod_quiz_display_options::DURING : 0) |
165 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS ?
166 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
167 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_ANSWERS ?
168 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
169 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_ANSWERS ?
170 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
171
172 $data->reviewoverallfeedback =
173 0 |
55ca80ed
TH
174 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_OVERALLFEEDBACK ?
175 mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
176 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_OVERALLFEEDBACK ?
177 mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
178 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_OVERALLFEEDBACK ?
179 mod_quiz_display_options::AFTER_CLOSE : 0);
f3ca24e4
TH
180 }
181
41941110
EL
182 // insert the quiz record
183 $newitemid = $DB->insert_record('quiz', $data);
184 // immediately after inserting "activity" record, call this
185 $this->apply_activity_instance($newitemid);
186 }
187
188 protected function process_quiz_question_instance($data) {
189 global $DB;
190
191 $data = (object)$data;
192 $oldid = $data->id;
193
194 $data->quiz = $this->get_new_parentid('quiz');
195
196 $data->question = $this->get_mappingid('question', $data->question);
197
198 $DB->insert_record('quiz_question_instances', $data);
199 }
200
201 protected function process_quiz_feedback($data) {
202 global $DB;
203
204 $data = (object)$data;
205 $oldid = $data->id;
206
207 $data->quizid = $this->get_new_parentid('quiz');
208
209 $newitemid = $DB->insert_record('quiz_feedback', $data);
210 $this->set_mapping('quiz_feedback', $oldid, $newitemid, true); // Has related files
211 }
212
213 protected function process_quiz_override($data) {
214 global $DB;
215
216 $data = (object)$data;
217 $oldid = $data->id;
218
219 // Based on userinfo, we'll restore user overides or no
220 $userinfo = $this->get_setting_value('userinfo');
221
222 // Skip user overrides if we are not restoring userinfo
223 if (!$userinfo && !is_null($data->userid)) {
224 return;
225 }
226
227 $data->quiz = $this->get_new_parentid('quiz');
228
229 $data->userid = $this->get_mappingid('user', $data->userid);
230 $data->groupid = $this->get_mappingid('group', $data->groupid);
231
232 $data->timeopen = $this->apply_date_offset($data->timeopen);
233 $data->timeclose = $this->apply_date_offset($data->timeclose);
234
0f66aced
EL
235 $newitemid = $DB->insert_record('quiz_overrides', $data);
236
237 // Add mapping, restore of logs needs it
238 $this->set_mapping('quiz_override', $oldid, $newitemid);
41941110
EL
239 }
240
241 protected function process_quiz_grade($data) {
242 global $DB;
243
244 $data = (object)$data;
245 $oldid = $data->id;
246
247 $data->quiz = $this->get_new_parentid('quiz');
248
249 $data->userid = $this->get_mappingid('user', $data->userid);
250 $data->grade = $data->gradeval;
251
252 $data->timemodified = $this->apply_date_offset($data->timemodified);
253
254 $DB->insert_record('quiz_grades', $data);
255 }
256
257 protected function process_quiz_attempt($data) {
258 global $DB;
259
260 $data = (object)$data;
261 $oldid = $data->id;
262 $olduniqueid = $data->uniqueid;
263
264 $data->quiz = $this->get_new_parentid('quiz');
265 $data->attempt = $data->attemptnum;
266
c749527b 267 $data->uniqueid = 0; // filled in later by {@link inform_new_usage_id()}
41941110
EL
268
269 $data->userid = $this->get_mappingid('user', $data->userid);
270
271 $data->timestart = $this->apply_date_offset($data->timestart);
272 $data->timefinish = $this->apply_date_offset($data->timefinish);
273 $data->timemodified = $this->apply_date_offset($data->timemodified);
274
41941110
EL
275 $newitemid = $DB->insert_record('quiz_attempts', $data);
276
c749527b
TH
277 // Save quiz_attempt->id mapping, because logs use it
278 $this->set_mapping('quiz_attempt', $oldid, $newitemid, false);
279 }
280
281 protected function inform_new_usage_id($newusageid) {
282 global $DB;
283 $DB->set_field('quiz_attempts', 'uniqueid', $newusageid, array('id' =>
284 $this->get_new_parentid('quiz_attempt')));
41941110
EL
285 }
286
287 protected function after_execute() {
c749527b 288 parent::after_execute();
41941110
EL
289 // Add quiz related files, no need to match by itemname (just internally handled context)
290 $this->add_related_files('mod_quiz', 'intro', null);
291 // Add feedback related files, matching by itemname = 'quiz_feedback'
292 $this->add_related_files('mod_quiz', 'feedback', 'quiz_feedback');
293 }
294}