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