MDL-20636 Merge vle/master into ouqe2
[moodle.git] / mod / quiz / backup / moodle2 / restore_quiz_stepslib.php
CommitLineData
41941110
EL
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
f7970e3c 19 * @package moodlecore
41941110 20 * @subpackage backup-moodle2
f7970e3c
TH
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41941110
EL
23 */
24
a17b297d
TH
25
26defined('MOODLE_INTERNAL') || die();
27
41941110
EL
28
29/**
30 * Structure step to restore one quiz activity
f7970e3c
TH
31 *
32 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41941110
EL
34 */
35class restore_quiz_activity_structure_step extends restore_questions_activity_structure_step {
36
37 protected function define_structure() {
38
39 $paths = array();
40 $userinfo = $this->get_setting_value('userinfo');
41
42 $paths[] = new restore_path_element('quiz', '/activity/quiz');
43 $paths[] = new restore_path_element('quiz_question_instance', '/activity/quiz/question_instances/question_instance');
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');
48 $quizattempt = new restore_path_element('quiz_attempt', '/activity/quiz/attempts/attempt');
49 $paths[] = $quizattempt;
50 // Add states and sessions
51 $this->add_question_attempts_states($quizattempt, $paths);
52 $this->add_question_attempts_sessions($quizattempt, $paths);
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) {
60 global $DB;
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
41941110
EL
80 // insert the quiz record
81 $newitemid = $DB->insert_record('quiz', $data);
82 // immediately after inserting "activity" record, call this
83 $this->apply_activity_instance($newitemid);
84 }
85
86 protected function process_quiz_question_instance($data) {
87 global $DB;
88
89 $data = (object)$data;
90 $oldid = $data->id;
91
92 $data->quiz = $this->get_new_parentid('quiz');
93
94 $data->question = $this->get_mappingid('question', $data->question);
95
96 $DB->insert_record('quiz_question_instances', $data);
97 }
98
99 protected function process_quiz_feedback($data) {
100 global $DB;
101
102 $data = (object)$data;
103 $oldid = $data->id;
104
105 $data->quizid = $this->get_new_parentid('quiz');
106
107 $newitemid = $DB->insert_record('quiz_feedback', $data);
108 $this->set_mapping('quiz_feedback', $oldid, $newitemid, true); // Has related files
109 }
110
111 protected function process_quiz_override($data) {
112 global $DB;
113
114 $data = (object)$data;
115 $oldid = $data->id;
116
117 // Based on userinfo, we'll restore user overides or no
118 $userinfo = $this->get_setting_value('userinfo');
119
120 // Skip user overrides if we are not restoring userinfo
121 if (!$userinfo && !is_null($data->userid)) {
122 return;
123 }
124
125 $data->quiz = $this->get_new_parentid('quiz');
126
127 $data->userid = $this->get_mappingid('user', $data->userid);
128 $data->groupid = $this->get_mappingid('group', $data->groupid);
129
130 $data->timeopen = $this->apply_date_offset($data->timeopen);
131 $data->timeclose = $this->apply_date_offset($data->timeclose);
132
0f66aced
EL
133 $newitemid = $DB->insert_record('quiz_overrides', $data);
134
135 // Add mapping, restore of logs needs it
136 $this->set_mapping('quiz_override', $oldid, $newitemid);
41941110
EL
137 }
138
139 protected function process_quiz_grade($data) {
140 global $DB;
141
142 $data = (object)$data;
143 $oldid = $data->id;
144
145 $data->quiz = $this->get_new_parentid('quiz');
146
147 $data->userid = $this->get_mappingid('user', $data->userid);
148 $data->grade = $data->gradeval;
149
150 $data->timemodified = $this->apply_date_offset($data->timemodified);
151
152 $DB->insert_record('quiz_grades', $data);
153 }
154
155 protected function process_quiz_attempt($data) {
156 global $DB;
157
158 $data = (object)$data;
159 $oldid = $data->id;
160 $olduniqueid = $data->uniqueid;
161
162 $data->quiz = $this->get_new_parentid('quiz');
163 $data->attempt = $data->attemptnum;
164
165 $data->uniqueid = question_new_attempt_uniqueid('quiz');
166
167 $data->userid = $this->get_mappingid('user', $data->userid);
168
169 $data->timestart = $this->apply_date_offset($data->timestart);
170 $data->timefinish = $this->apply_date_offset($data->timefinish);
171 $data->timemodified = $this->apply_date_offset($data->timemodified);
172
173 $data->layout = $this->questions_recode_layout($data->layout);
174
175 $newitemid = $DB->insert_record('quiz_attempts', $data);
176
177 // Save quiz_attempt->uniqueid as quiz_attempt mapping, both question_states and
0f66aced 178 // question_sessions have Fk to it and not to quiz_attempts->id at all.
41941110 179 $this->set_mapping('quiz_attempt', $olduniqueid, $data->uniqueid, false);
0f66aced
EL
180 // Also save quiz_attempt->id mapping, because logs use it
181 $this->set_mapping('quiz_attempt_id', $oldid, $newitemid, false);
41941110
EL
182 }
183
184 protected function after_execute() {
185 // Add quiz related files, no need to match by itemname (just internally handled context)
186 $this->add_related_files('mod_quiz', 'intro', null);
187 // Add feedback related files, matching by itemname = 'quiz_feedback'
188 $this->add_related_files('mod_quiz', 'feedback', 'quiz_feedback');
189 }
190}