MDL-20636 tinyMCE boxes for each sub-question are enormous. #355
[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) {
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
104 define('QUIZ_OLD_RESPONSES', 1*0x1041); // Show responses
105 define('QUIZ_OLD_SCORES', 2*0x1041); // Show scores
106 define('QUIZ_OLD_FEEDBACK', 4*0x1041); // Show question feedback
107 define('QUIZ_OLD_ANSWERS', 8*0x1041); // Show correct answers
108 define('QUIZ_OLD_SOLUTIONS', 16*0x1041); // Show solutions
109 define('QUIZ_OLD_GENERALFEEDBACK',32*0x1041); // Show question general feedback
110 define('QUIZ_OLD_OVERALLFEEDBACK', 1*0x4440000); // Show quiz overall feedback
111 }
112
113 $oldreview = $data->review;
114
115 $data->reviewattempt =
116 mod_quiz_display_options::DURING |
117 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_RESPONSES ? mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
118 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_RESPONSES ? mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
119 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_RESPONSES ? mod_quiz_display_options::AFTER_CLOSE : 0);
120
121 $data->reviewcorrectness =
122 mod_quiz_display_options::DURING |
123 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES ? mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
124 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_SCORES ? mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
125 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES ? mod_quiz_display_options::AFTER_CLOSE : 0);
126
127 $data->reviewmarks =
128 mod_quiz_display_options::DURING |
129 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES ? mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
130 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_SCORES ? mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
131 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES ? mod_quiz_display_options::AFTER_CLOSE : 0);
132
133 $data->reviewspecificfeedback =
134 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK ? mod_quiz_display_options::DURING : 0) |
135 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK ? mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
136 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_FEEDBACK ? mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
137 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_FEEDBACK ? mod_quiz_display_options::AFTER_CLOSE : 0);
138
139 $data->reviewgeneralfeedback =
140 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK ? mod_quiz_display_options::DURING : 0) |
141 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK ? mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
142 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_GENERALFEEDBACK ? mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
143 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_GENERALFEEDBACK ? mod_quiz_display_options::AFTER_CLOSE : 0);
144
145 $data->reviewrightanswer =
146 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS ? mod_quiz_display_options::DURING : 0) |
147 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS ? mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
148 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_ANSWERS ? mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
149 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_ANSWERS ? mod_quiz_display_options::AFTER_CLOSE : 0);
150
151 $data->reviewoverallfeedback =
152 0 |
153 ($oldreview & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_OVERALLFEEDBACK ? mod_quiz_display_options::IMMEDIATELY_AFTER : 0) |
154 ($oldreview & QUIZ_OLD_OPEN & QUIZ_OLD_OVERALLFEEDBACK ? mod_quiz_display_options::LATER_WHILE_OPEN : 0) |
155 ($oldreview & QUIZ_OLD_CLOSED & QUIZ_OLD_OVERALLFEEDBACK ? mod_quiz_display_options::AFTER_CLOSE : 0);
156 }
157
41941110
EL
158 // insert the quiz record
159 $newitemid = $DB->insert_record('quiz', $data);
160 // immediately after inserting "activity" record, call this
161 $this->apply_activity_instance($newitemid);
162 }
163
164 protected function process_quiz_question_instance($data) {
165 global $DB;
166
167 $data = (object)$data;
168 $oldid = $data->id;
169
170 $data->quiz = $this->get_new_parentid('quiz');
171
172 $data->question = $this->get_mappingid('question', $data->question);
173
174 $DB->insert_record('quiz_question_instances', $data);
175 }
176
177 protected function process_quiz_feedback($data) {
178 global $DB;
179
180 $data = (object)$data;
181 $oldid = $data->id;
182
183 $data->quizid = $this->get_new_parentid('quiz');
184
185 $newitemid = $DB->insert_record('quiz_feedback', $data);
186 $this->set_mapping('quiz_feedback', $oldid, $newitemid, true); // Has related files
187 }
188
189 protected function process_quiz_override($data) {
190 global $DB;
191
192 $data = (object)$data;
193 $oldid = $data->id;
194
195 // Based on userinfo, we'll restore user overides or no
196 $userinfo = $this->get_setting_value('userinfo');
197
198 // Skip user overrides if we are not restoring userinfo
199 if (!$userinfo && !is_null($data->userid)) {
200 return;
201 }
202
203 $data->quiz = $this->get_new_parentid('quiz');
204
205 $data->userid = $this->get_mappingid('user', $data->userid);
206 $data->groupid = $this->get_mappingid('group', $data->groupid);
207
208 $data->timeopen = $this->apply_date_offset($data->timeopen);
209 $data->timeclose = $this->apply_date_offset($data->timeclose);
210
0f66aced
EL
211 $newitemid = $DB->insert_record('quiz_overrides', $data);
212
213 // Add mapping, restore of logs needs it
214 $this->set_mapping('quiz_override', $oldid, $newitemid);
41941110
EL
215 }
216
217 protected function process_quiz_grade($data) {
218 global $DB;
219
220 $data = (object)$data;
221 $oldid = $data->id;
222
223 $data->quiz = $this->get_new_parentid('quiz');
224
225 $data->userid = $this->get_mappingid('user', $data->userid);
226 $data->grade = $data->gradeval;
227
228 $data->timemodified = $this->apply_date_offset($data->timemodified);
229
230 $DB->insert_record('quiz_grades', $data);
231 }
232
233 protected function process_quiz_attempt($data) {
234 global $DB;
235
236 $data = (object)$data;
237 $oldid = $data->id;
238 $olduniqueid = $data->uniqueid;
239
240 $data->quiz = $this->get_new_parentid('quiz');
241 $data->attempt = $data->attemptnum;
242
243 $data->uniqueid = question_new_attempt_uniqueid('quiz');
244
245 $data->userid = $this->get_mappingid('user', $data->userid);
246
247 $data->timestart = $this->apply_date_offset($data->timestart);
248 $data->timefinish = $this->apply_date_offset($data->timefinish);
249 $data->timemodified = $this->apply_date_offset($data->timemodified);
250
251 $data->layout = $this->questions_recode_layout($data->layout);
252
253 $newitemid = $DB->insert_record('quiz_attempts', $data);
254
255 // Save quiz_attempt->uniqueid as quiz_attempt mapping, both question_states and
0f66aced 256 // question_sessions have Fk to it and not to quiz_attempts->id at all.
41941110 257 $this->set_mapping('quiz_attempt', $olduniqueid, $data->uniqueid, false);
0f66aced
EL
258 // Also save quiz_attempt->id mapping, because logs use it
259 $this->set_mapping('quiz_attempt_id', $oldid, $newitemid, false);
41941110
EL
260 }
261
262 protected function after_execute() {
263 // Add quiz related files, no need to match by itemname (just internally handled context)
264 $this->add_related_files('mod_quiz', 'intro', null);
265 // Add feedback related files, matching by itemname = 'quiz_feedback'
266 $this->add_related_files('mod_quiz', 'feedback', 'quiz_feedback');
267 }
268}