MDL-22135 - logs restore, course and activities + cleanup
[moodle.git] / mod / quiz / backup / moodle2 / restore_quiz_stepslib.php
1 <?php
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/>.
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  */
25 /**
26  * Define all the restore steps that will be used by the restore_quiz_activity_task
27  */
29 /**
30  * Structure step to restore one quiz activity
31  */
32 class restore_quiz_activity_structure_step extends restore_questions_activity_structure_step {
34     protected function define_structure() {
36         $paths = array();
37         $userinfo = $this->get_setting_value('userinfo');
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         }
52         // Return the paths wrapped into standard activity structure
53         return $this->prepare_activity_structure($paths);
54     }
56     protected function process_quiz($data) {
57         global $DB;
59         $data = (object)$data;
60         $oldid = $data->id;
61         $data->course = $this->get_courseid();
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);
68         $data->questions = $this->questions_recode_layout($data->questions);
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     }
76     protected function process_quiz_question_instance($data) {
77         global $DB;
79         $data = (object)$data;
80         $oldid = $data->id;
82         $data->quiz = $this->get_new_parentid('quiz');
84         $data->question = $this->get_mappingid('question', $data->question);
86         $DB->insert_record('quiz_question_instances', $data);
87     }
89     protected function process_quiz_feedback($data) {
90         global $DB;
92         $data = (object)$data;
93         $oldid = $data->id;
95         $data->quizid = $this->get_new_parentid('quiz');
97         $newitemid = $DB->insert_record('quiz_feedback', $data);
98         $this->set_mapping('quiz_feedback', $oldid, $newitemid, true); // Has related files
99     }
101     protected function process_quiz_override($data) {
102         global $DB;
104         $data = (object)$data;
105         $oldid = $data->id;
107         // Based on userinfo, we'll restore user overides or no
108         $userinfo = $this->get_setting_value('userinfo');
110         // Skip user overrides if we are not restoring userinfo
111         if (!$userinfo && !is_null($data->userid)) {
112             return;
113         }
115         $data->quiz = $this->get_new_parentid('quiz');
117         $data->userid = $this->get_mappingid('user', $data->userid);
118         $data->groupid = $this->get_mappingid('group', $data->groupid);
120         $data->timeopen = $this->apply_date_offset($data->timeopen);
121         $data->timeclose = $this->apply_date_offset($data->timeclose);
123         $newitemid = $DB->insert_record('quiz_overrides', $data);
125         // Add mapping, restore of logs needs it
126         $this->set_mapping('quiz_override', $oldid, $newitemid);
127     }
129     protected function process_quiz_grade($data) {
130         global $DB;
132         $data = (object)$data;
133         $oldid = $data->id;
135         $data->quiz = $this->get_new_parentid('quiz');
137         $data->userid = $this->get_mappingid('user', $data->userid);
138         $data->grade = $data->gradeval;
140         $data->timemodified = $this->apply_date_offset($data->timemodified);
142         $DB->insert_record('quiz_grades', $data);
143     }
145     protected function process_quiz_attempt($data) {
146         global $DB;
148         $data = (object)$data;
149         $oldid = $data->id;
150         $olduniqueid = $data->uniqueid;
152         $data->quiz = $this->get_new_parentid('quiz');
153         $data->attempt = $data->attemptnum;
155         $data->uniqueid = question_new_attempt_uniqueid('quiz');
157         $data->userid = $this->get_mappingid('user', $data->userid);
159         $data->timestart = $this->apply_date_offset($data->timestart);
160         $data->timefinish = $this->apply_date_offset($data->timefinish);
161         $data->timemodified = $this->apply_date_offset($data->timemodified);
163         $data->layout = $this->questions_recode_layout($data->layout);
165         $newitemid = $DB->insert_record('quiz_attempts', $data);
167         // Save quiz_attempt->uniqueid as quiz_attempt mapping, both question_states and
168         // question_sessions have Fk to it and not to quiz_attempts->id at all.
169         $this->set_mapping('quiz_attempt', $olduniqueid, $data->uniqueid, false);
170         // Also save quiz_attempt->id mapping, because logs use it
171         $this->set_mapping('quiz_attempt_id', $oldid, $newitemid, false);
172     }
174     protected function after_execute() {
175         // Add quiz related files, no need to match by itemname (just internally handled context)
176         $this->add_related_files('mod_quiz', 'intro', null);
177         // Add feedback related files, matching by itemname = 'quiz_feedback'
178         $this->add_related_files('mod_quiz', 'feedback', 'quiz_feedback');
179     }