648dcad954274312e9a27b4daea07e3083d70085
[moodle.git] / mod / assign / backup / moodle2 / restore_assign_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  * Define all the restore steps that will be used by the restore_assign_activity_task
19  *
20  * @package   mod_assign
21  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 /**
28  * Define the complete assignment structure for restore, with file and id annotations
29  *
30  * @package   mod_assign
31  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
32  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33  */
34 class restore_assign_activity_structure_step extends restore_activity_structure_step {
36     /**
37      * Define the structure of the restore workflow.
38      *
39      * @return restore_path_element $structure
40      */
41     protected function define_structure() {
43         $paths = array();
44         // To know if we are including userinfo.
45         $userinfo = $this->get_setting_value('userinfo');
47         // Define each element separated.
48         $paths[] = new restore_path_element('assign', '/activity/assign');
49         if ($userinfo) {
50             $submission = new restore_path_element('assign_submission',
51                                                    '/activity/assign/submissions/submission');
52             $paths[] = $submission;
53             $this->add_subplugin_structure('assignsubmission', $submission);
54             $grade = new restore_path_element('assign_grade', '/activity/assign/grades/grade');
55             $paths[] = $grade;
56             $this->add_subplugin_structure('assignfeedback', $grade);
57         }
58         $paths[] = new restore_path_element('assign_plugin_config',
59                                             '/activity/assign/plugin_configs/plugin_config');
61         return $this->prepare_activity_structure($paths);
62     }
64     /**
65      * Process an assign restore.
66      *
67      * @param object $data The data in object form
68      * @return void
69      */
70     protected function process_assign($data) {
71         global $DB;
73         $data = (object)$data;
74         $oldid = $data->id;
75         $data->course = $this->get_courseid();
77         $data->timemodified = $this->apply_date_offset($data->timemodified);
78         $data->allowsubmissionsfromdate = $this->apply_date_offset($data->allowsubmissionsfromdate);
79         $data->duedate = $this->apply_date_offset($data->duedate);
80         if (!empty($data->teamsubmissiongroupingid)) {
81             $data->teamsubmissiongroupingid = $this->get_mappingid('grouping',
82                                                                    $data->teamsubmissiongroupingid);
83         } else {
84             $data->teamsubmissiongroupingid = 0;
85         }
87         if (!isset($data->cutoffdate)) {
88             $data->cutoffdate = 0;
89         }
90         if (!isset($data->markingworkflow)) {
91             $data->markingworkflow = 0;
92         }
93         if (!isset($data->markingallocation)) {
94             $data->markingallocation = 0;
95         }
97         if (!empty($data->preventlatesubmissions)) {
98             $data->cutoffdate = $data->duedate;
99         } else {
100             $data->cutoffdate = $this->apply_date_offset($data->cutoffdate);
101         }
103         $newitemid = $DB->insert_record('assign', $data);
105         $this->apply_activity_instance($newitemid);
106     }
108     /**
109      * Process a submission restore
110      * @param object $data The data in object form
111      * @return void
112      */
113     protected function process_assign_submission($data) {
114         global $DB;
116         $data = (object)$data;
117         $oldid = $data->id;
119         $data->assignment = $this->get_new_parentid('assign');
121         $data->timemodified = $this->apply_date_offset($data->timemodified);
122         $data->timecreated = $this->apply_date_offset($data->timecreated);
123         if ($data->userid > 0) {
124             $data->userid = $this->get_mappingid('user', $data->userid);
125         }
126         if (!empty($data->groupid)) {
127             $data->groupid = $this->get_mappingid('group', $data->groupid);
128         } else {
129             $data->groupid = 0;
130         }
132         $newitemid = $DB->insert_record('assign_submission', $data);
134         // Note - the old contextid is required in order to be able to restore files stored in
135         // sub plugin file areas attached to the submissionid.
136         $this->set_mapping('submission', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
137     }
139     /**
140      * Process a user_flags restore
141      * @param object $data The data in object form
142      * @return void
143      */
144     protected function process_assign_userflags($data) {
145         global $DB;
147         $data = (object)$data;
148         $oldid = $data->id;
150         $data->assignment = $this->get_new_parentid('assign');
152         $data->userid = $this->get_mappingid('user', $data->userid);
153         if (!empty($data->extensionduedate)) {
154             $data->extensionduedate = $this->apply_date_offset($data->extensionduedate);
155         } else {
156             $data->extensionduedate = 0;
157         }
158         // Flags mailed and locked need no translation on restore.
160         $newitemid = $DB->insert_record('assign_user_flags', $data);
161     }
163     /**
164      * Process a grade restore
165      * @param object $data The data in object form
166      * @return void
167      */
168     protected function process_assign_grade($data) {
169         global $DB;
171         $data = (object)$data;
172         $oldid = $data->id;
174         $data->assignment = $this->get_new_parentid('assign');
176         $data->timemodified = $this->apply_date_offset($data->timemodified);
177         $data->timecreated = $this->apply_date_offset($data->timecreated);
178         $data->userid = $this->get_mappingid('user', $data->userid);
179         $data->grader = $this->get_mappingid('user', $data->grader);
181         // Handle flags restore to a different table.
182         $flags = new stdClass();
183         $flags->assignment = $this->get_new_parentid('assign');
184         if (!empty($data->extensionduedate)) {
185             $flags->extensionduedate = $this->apply_date_offset($data->extensionduedate);
186         }
187         if (!empty($data->mailed)) {
188             $flags->mailed = $data->mailed;
189         }
190         if (!empty($data->locked)) {
191             $flags->locked = $data->locked;
192         }
193         $DB->insert_record('assign_user_flags', $flags);
195         $newitemid = $DB->insert_record('assign_grades', $data);
197         // Note - the old contextid is required in order to be able to restore files stored in
198         // sub plugin file areas attached to the gradeid.
199         $this->set_mapping('grade', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
200     }
202     /**
203      * Process a plugin-config restore
204      * @param object $data The data in object form
205      * @return void
206      */
207     protected function process_assign_plugin_config($data) {
208         global $DB;
210         $data = (object)$data;
211         $oldid = $data->id;
213         $data->assignment = $this->get_new_parentid('assign');
215         $newitemid = $DB->insert_record('assign_plugin_config', $data);
216     }
218     /**
219      * Once the database tables have been fully restored, restore the files
220      * @return void
221      */
222     protected function after_execute() {
223         $this->add_related_files('mod_assign', 'intro', null);
224     }