2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Define all the restore steps that will be used by the restore_assign_activity_task
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 * Define the complete assignment structure for restore, with file and id annotations
31 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class restore_assign_activity_structure_step extends restore_activity_structure_step {
37 * Define the structure of the restore workflow.
39 * @return restore_path_element $structure
41 protected function define_structure() {
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');
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');
56 $this->add_subplugin_structure('assignfeedback', $grade);
58 $paths[] = new restore_path_element('assign_plugin_config',
59 '/activity/assign/plugin_configs/plugin_config');
61 return $this->prepare_activity_structure($paths);
65 * Process an assign restore.
67 * @param object $data The data in object form
70 protected function process_assign($data) {
73 $data = (object)$data;
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);
84 $data->teamsubmissiongroupingid = 0;
87 if (!isset($data->cutoffdate)) {
88 $data->cutoffdate = 0;
90 if (!isset($data->markingworkflow)) {
91 $data->markingworkflow = 0;
93 if (!isset($data->markingallocation)) {
94 $data->markingallocation = 0;
97 if (!empty($data->preventlatesubmissions)) {
98 $data->cutoffdate = $data->duedate;
100 $data->cutoffdate = $this->apply_date_offset($data->cutoffdate);
103 $newitemid = $DB->insert_record('assign', $data);
105 $this->apply_activity_instance($newitemid);
109 * Process a submission restore
110 * @param object $data The data in object form
113 protected function process_assign_submission($data) {
116 $data = (object)$data;
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);
126 if (!empty($data->groupid)) {
127 $data->groupid = $this->get_mappingid('group', $data->groupid);
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());
140 * Process a user_flags restore
141 * @param object $data The data in object form
144 protected function process_assign_userflags($data) {
147 $data = (object)$data;
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);
156 $data->extensionduedate = 0;
158 // Flags mailed and locked need no translation on restore.
160 $newitemid = $DB->insert_record('assign_user_flags', $data);
164 * Process a grade restore
165 * @param object $data The data in object form
168 protected function process_assign_grade($data) {
171 $data = (object)$data;
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);
187 if (!empty($data->mailed)) {
188 $flags->mailed = $data->mailed;
190 if (!empty($data->locked)) {
191 $flags->locked = $data->locked;
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());
203 * Process a plugin-config restore
204 * @param object $data The data in object form
207 protected function process_assign_plugin_config($data) {
210 $data = (object)$data;
213 $data->assignment = $this->get_new_parentid('assign');
215 $newitemid = $DB->insert_record('assign_plugin_config', $data);
219 * Once the database tables have been fully restored, restore the files
222 protected function after_execute() {
223 $this->add_related_files('mod_assign', 'intro', null);