MDL-22138 backup - I know smaller cathedrals than this, yay quizzes!
[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/**
19 * @package moodlecore
20 * @subpackage backup-moodle2
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
23 */
24
25/**
26 * Define all the restore steps that will be used by the restore_quiz_activity_task
27 */
28
29/**
30 * Structure step to restore one quiz activity
31 */
32class restore_quiz_activity_structure_step extends restore_questions_activity_structure_step {
33
34 protected function define_structure() {
35
36 $paths = array();
37 $userinfo = $this->get_setting_value('userinfo');
38
39 $paths[] = new restore_path_element('quiz', '/activity/quiz');
40 $paths[] = new restore_path_element('quiz_question_instance', '/activity/quiz/question_instances/question_instance');
41 $paths[] = new restore_path_element('quiz_feedback', '/activity/quiz/feedbacks/feedback');
42 $paths[] = new restore_path_element('quiz_override', '/activity/quiz/overrides/override');
43 if ($userinfo) {
44 $paths[] = new restore_path_element('quiz_grade', '/activity/quiz/grades/grade');
45 $quizattempt = new restore_path_element('quiz_attempt', '/activity/quiz/attempts/attempt');
46 $paths[] = $quizattempt;
47 // Add states and sessions
48 $this->add_question_attempts_states($quizattempt, $paths);
49 $this->add_question_attempts_sessions($quizattempt, $paths);
50 }
51
52 // Return the paths wrapped into standard activity structure
53 return $this->prepare_activity_structure($paths);
54 }
55
56 protected function process_quiz($data) {
57 global $DB;
58
59 $data = (object)$data;
60 $oldid = $data->id;
61 $data->course = $this->get_courseid();
62
63 $data->timeopen = $this->apply_date_offset($data->timeopen);
64 $data->timeclose = $this->apply_date_offset($data->timeclose);
65 $data->timecreated = $this->apply_date_offset($data->timecreated);
66 $data->timemodified = $this->apply_date_offset($data->timemodified);
67
68 $data->questions = $this->questions_recode_layout($data->questions);
69
70 // insert the quiz record
71 $newitemid = $DB->insert_record('quiz', $data);
72 // immediately after inserting "activity" record, call this
73 $this->apply_activity_instance($newitemid);
74 }
75
76 protected function process_quiz_question_instance($data) {
77 global $DB;
78
79 $data = (object)$data;
80 $oldid = $data->id;
81
82 $data->quiz = $this->get_new_parentid('quiz');
83
84 $data->question = $this->get_mappingid('question', $data->question);
85
86 $DB->insert_record('quiz_question_instances', $data);
87 }
88
89 protected function process_quiz_feedback($data) {
90 global $DB;
91
92 $data = (object)$data;
93 $oldid = $data->id;
94
95 $data->quizid = $this->get_new_parentid('quiz');
96
97 $newitemid = $DB->insert_record('quiz_feedback', $data);
98 $this->set_mapping('quiz_feedback', $oldid, $newitemid, true); // Has related files
99 }
100
101 protected function process_quiz_override($data) {
102 global $DB;
103
104 $data = (object)$data;
105 $oldid = $data->id;
106
107 // Based on userinfo, we'll restore user overides or no
108 $userinfo = $this->get_setting_value('userinfo');
109
110 // Skip user overrides if we are not restoring userinfo
111 if (!$userinfo && !is_null($data->userid)) {
112 return;
113 }
114
115 $data->quiz = $this->get_new_parentid('quiz');
116
117 $data->userid = $this->get_mappingid('user', $data->userid);
118 $data->groupid = $this->get_mappingid('group', $data->groupid);
119
120 $data->timeopen = $this->apply_date_offset($data->timeopen);
121 $data->timeclose = $this->apply_date_offset($data->timeclose);
122
123 $DB->insert_record('quiz_overrides', $data);
124 }
125
126 protected function process_quiz_grade($data) {
127 global $DB;
128
129 $data = (object)$data;
130 $oldid = $data->id;
131
132 $data->quiz = $this->get_new_parentid('quiz');
133
134 $data->userid = $this->get_mappingid('user', $data->userid);
135 $data->grade = $data->gradeval;
136
137 $data->timemodified = $this->apply_date_offset($data->timemodified);
138
139 $DB->insert_record('quiz_grades', $data);
140 }
141
142 protected function process_quiz_attempt($data) {
143 global $DB;
144
145 $data = (object)$data;
146 $oldid = $data->id;
147 $olduniqueid = $data->uniqueid;
148
149 $data->quiz = $this->get_new_parentid('quiz');
150 $data->attempt = $data->attemptnum;
151
152 $data->uniqueid = question_new_attempt_uniqueid('quiz');
153
154 $data->userid = $this->get_mappingid('user', $data->userid);
155
156 $data->timestart = $this->apply_date_offset($data->timestart);
157 $data->timefinish = $this->apply_date_offset($data->timefinish);
158 $data->timemodified = $this->apply_date_offset($data->timemodified);
159
160 $data->layout = $this->questions_recode_layout($data->layout);
161
162 $newitemid = $DB->insert_record('quiz_attempts', $data);
163
164 // Save quiz_attempt->uniqueid as quiz_attempt mapping, both question_states and
165 // question_sessions have Fk to it and not to quiz_attempts->id at all. In fact
166 // quiz_attempt->id isn't use by anybody
167 $this->set_mapping('quiz_attempt', $olduniqueid, $data->uniqueid, false);
168 }
169
170 protected function after_execute() {
171 // Add quiz related files, no need to match by itemname (just internally handled context)
172 $this->add_related_files('mod_quiz', 'intro', null);
173 // Add feedback related files, matching by itemname = 'quiz_feedback'
174 $this->add_related_files('mod_quiz', 'feedback', 'quiz_feedback');
175 }
176}