Merge branch 'wip-MDL-30797-23' of git://github.com/abgreeve/moodle into MOODLE_23_STABLE
[moodle.git] / mod / quiz / backup / moodle2 / restore_quiz_stepslib.php
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/>.
17 /**
18  * @package    moodlecore
19  * @subpackage backup-moodle2
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
22  */
25 defined('MOODLE_INTERNAL') || die();
28 /**
29  * Structure step to restore one quiz activity
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
33  */
34 class restore_quiz_activity_structure_step extends restore_questions_activity_structure_step {
36     protected function define_structure() {
38         $paths = array();
39         $userinfo = $this->get_setting_value('userinfo');
41         $quiz = new restore_path_element('quiz', '/activity/quiz');
42         $paths[] = $quiz;
44         // A chance for access subplugings to set up their quiz data.
45         $this->add_subplugin_structure('quizaccess', $quiz);
47         $paths[] = new restore_path_element('quiz_question_instance',
48                 '/activity/quiz/question_instances/question_instance');
49         $paths[] = new restore_path_element('quiz_feedback', '/activity/quiz/feedbacks/feedback');
50         $paths[] = new restore_path_element('quiz_override', '/activity/quiz/overrides/override');
52         if ($userinfo) {
53             $paths[] = new restore_path_element('quiz_grade', '/activity/quiz/grades/grade');
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;
62                 // Add states and sessions.
63                 $this->add_question_usages($quizattempt, $paths);
65                 // A chance for access subplugings to set up their attempt data.
66                 $this->add_subplugin_structure('quizaccess', $quizattempt);
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             }
77         }
79         // Return the paths wrapped into standard activity structure.
80         return $this->prepare_activity_structure($paths);
81     }
83     protected function process_quiz($data) {
84         global $CFG, $DB;
86         $data = (object)$data;
87         $oldid = $data->id;
88         $data->course = $this->get_courseid();
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);
95         // Needed by {@link process_quiz_attempt_legacy}.
96         $this->oldquizlayout = $data->questions;
97         $data->questions = $this->questions_recode_layout($data->questions);
99         // The setting 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         }
106         // The old optionflags and penaltyscheme from 2.0 need to be mapped to
107         // the new preferredbehaviour. See 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         }
120         // The old review column from 2.0 need to be split into the seven new
121         // review columns. See MDL-20636.
122         if (isset($data->review)) {
123             require_once($CFG->dirroot . '/mod/quiz/locallib.php');
125             if (!defined('QUIZ_OLD_IMMEDIATELY')) {
126                 define('QUIZ_OLD_IMMEDIATELY', 0x3c003f);
127                 define('QUIZ_OLD_OPEN',        0x3c00fc0);
128                 define('QUIZ_OLD_CLOSED',      0x3c03f000);
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);
137             }
139             $oldreview = $data->review;
141             $data->reviewattempt =
142                     mod_quiz_display_options::DURING |
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);
150             $data->reviewcorrectness =
151                     mod_quiz_display_options::DURING |
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);
159             $data->reviewmarks =
160                     mod_quiz_display_options::DURING |
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);
168             $data->reviewspecificfeedback =
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);
178             $data->reviewgeneralfeedback =
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);
188             $data->reviewrightanswer =
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);
198             $data->reviewoverallfeedback =
199                     0 |
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);
206         }
208         // The old popup column from from <= 2.1 need to be mapped to
209         // the new browsersecurity. See 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         }
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     }
229     protected function process_quiz_question_instance($data) {
230         global $DB;
232         $data = (object)$data;
233         $oldid = $data->id;
235         $data->quiz = $this->get_new_parentid('quiz');
237         $data->question = $this->get_mappingid('question', $data->question);
239         $DB->insert_record('quiz_question_instances', $data);
240     }
242     protected function process_quiz_feedback($data) {
243         global $DB;
245         $data = (object)$data;
246         $oldid = $data->id;
248         $data->quizid = $this->get_new_parentid('quiz');
250         $newitemid = $DB->insert_record('quiz_feedback', $data);
251         $this->set_mapping('quiz_feedback', $oldid, $newitemid, true); // Has related files.
252     }
254     protected function process_quiz_override($data) {
255         global $DB;
257         $data = (object)$data;
258         $oldid = $data->id;
260         // Based on userinfo, we'll restore user overides or no.
261         $userinfo = $this->get_setting_value('userinfo');
263         // Skip user overrides if we are not restoring userinfo.
264         if (!$userinfo && !is_null($data->userid)) {
265             return;
266         }
268         $data->quiz = $this->get_new_parentid('quiz');
270         $data->userid = $this->get_mappingid('user', $data->userid);
271         $data->groupid = $this->get_mappingid('group', $data->groupid);
273         $data->timeopen = $this->apply_date_offset($data->timeopen);
274         $data->timeclose = $this->apply_date_offset($data->timeclose);
276         $newitemid = $DB->insert_record('quiz_overrides', $data);
278         // Add mapping, restore of logs needs it.
279         $this->set_mapping('quiz_override', $oldid, $newitemid);
280     }
282     protected function process_quiz_grade($data) {
283         global $DB;
285         $data = (object)$data;
286         $oldid = $data->id;
288         $data->quiz = $this->get_new_parentid('quiz');
290         $data->userid = $this->get_mappingid('user', $data->userid);
291         $data->grade = $data->gradeval;
293         $data->timemodified = $this->apply_date_offset($data->timemodified);
295         $DB->insert_record('quiz_grades', $data);
296     }
298     protected function process_quiz_attempt($data) {
299         $data = (object)$data;
301         $data->quiz = $this->get_new_parentid('quiz');
302         $data->attempt = $data->attemptnum;
304         $data->userid = $this->get_mappingid('user', $data->userid);
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         $data->timecheckstate = $this->apply_date_offset($data->timecheckstate);
311         // Deals with up-grading pre-2.3 back-ups to 2.3+.
312         if (!isset($data->state)) {
313             if ($data->timefinish > 0) {
314                 $data->state = 'finished';
315             } else {
316                 $data->state = 'inprogress';
317             }
318         }
320         // The data is actually inserted into the database later in inform_new_usage_id.
321         $this->currentquizattempt = clone($data);
322     }
324     protected function process_quiz_attempt_legacy($data) {
325         global $DB;
327         $this->process_quiz_attempt($data);
329         $quiz = $DB->get_record('quiz', array('id' => $this->get_new_parentid('quiz')));
330         $quiz->oldquestions = $this->oldquizlayout;
331         $this->process_legacy_quiz_attempt_data($data, $quiz);
332     }
334     protected function inform_new_usage_id($newusageid) {
335         global $DB;
337         $data = $this->currentquizattempt;
339         $oldid = $data->id;
340         $data->uniqueid = $newusageid;
342         $newitemid = $DB->insert_record('quiz_attempts', $data);
344         // Save quiz_attempt->id mapping, because logs use it.
345         $this->set_mapping('quiz_attempt', $oldid, $newitemid, false);
346     }
348     protected function after_execute() {
349         parent::after_execute();
350         // Add quiz related files, no need to match by itemname (just internally handled context).
351         $this->add_related_files('mod_quiz', 'intro', null);
352         // Add feedback related files, matching by itemname = 'quiz_feedback'.
353         $this->add_related_files('mod_quiz', 'feedback', 'quiz_feedback');
354     }