mirrored from git://git.moodle.org/moodle.git
/
restore_assign_stepslib.php
347 lines (291 loc) · 13.1 KB
/
restore_assign_stepslib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Define all the restore steps that will be used by the restore_assign_activity_task
*
* @package mod_assign
* @copyright 2012 NetSpot {@link http://www.netspot.com.au}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Define the complete assignment structure for restore, with file and id annotations
*
* @package mod_assign
* @copyright 2012 NetSpot {@link http://www.netspot.com.au}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_assign_activity_structure_step extends restore_activity_structure_step {
/**
* Store whether submission details should be included. Details may not be included if the
* this is a team submission, but groups/grouping information was not included in the backup.
*/
protected $includesubmission = true;
/**
* Define the structure of the restore workflow.
*
* @return restore_path_element $structure
*/
protected function define_structure() {
$paths = array();
// To know if we are including userinfo.
$userinfo = $this->get_setting_value('userinfo');
// Define each element separated.
$paths[] = new restore_path_element('assign', '/activity/assign');
if ($userinfo) {
$submission = new restore_path_element('assign_submission',
'/activity/assign/submissions/submission');
$paths[] = $submission;
$this->add_subplugin_structure('assignsubmission', $submission);
$grade = new restore_path_element('assign_grade', '/activity/assign/grades/grade');
$paths[] = $grade;
$this->add_subplugin_structure('assignfeedback', $grade);
$userflag = new restore_path_element('assign_userflag',
'/activity/assign/userflags/userflag');
$paths[] = $userflag;
}
$paths[] = new restore_path_element('assign_plugin_config',
'/activity/assign/plugin_configs/plugin_config');
return $this->prepare_activity_structure($paths);
}
/**
* Process an assign restore.
*
* @param object $data The data in object form
* @return void
*/
protected function process_assign($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->course = $this->get_courseid();
$data->timemodified = $this->apply_date_offset($data->timemodified);
$data->allowsubmissionsfromdate = $this->apply_date_offset($data->allowsubmissionsfromdate);
$data->duedate = $this->apply_date_offset($data->duedate);
// If this is a team submission, but there is no group info we need to flag that the submission
// information should not be included. It should not be restored.
$groupinfo = $this->task->get_setting_value('groups');
if ($data->teamsubmission && !$groupinfo) {
$this->includesubmission = false;
}
if (!empty($data->teamsubmissiongroupingid)) {
$data->teamsubmissiongroupingid = $this->get_mappingid('grouping',
$data->teamsubmissiongroupingid);
} else {
$data->teamsubmissiongroupingid = 0;
}
if (!isset($data->cutoffdate)) {
$data->cutoffdate = 0;
}
if (!isset($data->markingworkflow)) {
$data->markingworkflow = 0;
}
if (!isset($data->markingallocation)) {
$data->markingallocation = 0;
}
if (!isset($data->preventsubmissionnotingroup)) {
$data->preventsubmissionnotingroup = 0;
}
if (!empty($data->preventlatesubmissions)) {
$data->cutoffdate = $data->duedate;
} else {
$data->cutoffdate = $this->apply_date_offset($data->cutoffdate);
}
if ($data->grade < 0) { // Scale found, get mapping.
$data->grade = -($this->get_mappingid('scale', abs($data->grade)));
}
$newitemid = $DB->insert_record('assign', $data);
$this->apply_activity_instance($newitemid);
}
/**
* Process a submission restore
* @param object $data The data in object form
* @return void
*/
protected function process_assign_submission($data) {
global $DB;
if (!$this->includesubmission) {
return;
}
$data = (object)$data;
$oldid = $data->id;
$data->assignment = $this->get_new_parentid('assign');
$data->timemodified = $this->apply_date_offset($data->timemodified);
$data->timecreated = $this->apply_date_offset($data->timecreated);
if ($data->userid > 0) {
$data->userid = $this->get_mappingid('user', $data->userid);
}
if (!empty($data->groupid)) {
$data->groupid = $this->get_mappingid('group', $data->groupid);
} else {
$data->groupid = 0;
}
// We will correct this in set_latest_submission_field() once all submissions are restored.
$data->latest = 0;
$newitemid = $DB->insert_record('assign_submission', $data);
// Note - the old contextid is required in order to be able to restore files stored in
// sub plugin file areas attached to the submissionid.
$this->set_mapping('submission', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
}
/**
* Process a user_flags restore
* @param object $data The data in object form
* @return void
*/
protected function process_assign_userflag($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->assignment = $this->get_new_parentid('assign');
$data->userid = $this->get_mappingid('user', $data->userid);
if (!empty($data->allocatedmarker)) {
$data->allocatedmarker = $this->get_mappingid('user', $data->allocatedmarker);
}
if (!empty($data->extensionduedate)) {
$data->extensionduedate = $this->apply_date_offset($data->extensionduedate);
} else {
$data->extensionduedate = 0;
}
// Flags mailed and locked need no translation on restore.
$newitemid = $DB->insert_record('assign_user_flags', $data);
}
/**
* Process a grade restore
* @param object $data The data in object form
* @return void
*/
protected function process_assign_grade($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->assignment = $this->get_new_parentid('assign');
$data->timemodified = $this->apply_date_offset($data->timemodified);
$data->timecreated = $this->apply_date_offset($data->timecreated);
$data->userid = $this->get_mappingid('user', $data->userid);
$data->grader = $this->get_mappingid('user', $data->grader);
// Handle flags restore to a different table (for upgrade from old backups).
if (!empty($data->extensionduedate) ||
!empty($data->mailed) ||
!empty($data->locked)) {
$flags = new stdClass();
$flags->assignment = $this->get_new_parentid('assign');
if (!empty($data->extensionduedate)) {
$flags->extensionduedate = $this->apply_date_offset($data->extensionduedate);
}
if (!empty($data->mailed)) {
$flags->mailed = $data->mailed;
}
if (!empty($data->locked)) {
$flags->locked = $data->locked;
}
$flags->userid = $this->get_mappingid('user', $data->userid);
$DB->insert_record('assign_user_flags', $flags);
}
$newitemid = $DB->insert_record('assign_grades', $data);
// Note - the old contextid is required in order to be able to restore files stored in
// sub plugin file areas attached to the gradeid.
$this->set_mapping('grade', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
$this->set_mapping(restore_gradingform_plugin::itemid_mapping('submissions'), $oldid, $newitemid);
}
/**
* Process a plugin-config restore
* @param object $data The data in object form
* @return void
*/
protected function process_assign_plugin_config($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->assignment = $this->get_new_parentid('assign');
$newitemid = $DB->insert_record('assign_plugin_config', $data);
}
/**
* For all submissions in this assignment, either set the
* submission->latest field to 1 for the latest attempts
* or create a new submission record for grades with no submission.
*
* @return void
*/
protected function set_latest_submission_field() {
global $DB, $CFG;
// Required for constants.
require_once($CFG->dirroot . '/mod/assign/locallib.php');
$assignmentid = $this->get_new_parentid('assign');
// This code could be rewritten as a monster SQL - but the point of adding this "latest" field
// to the submissions table in the first place was to get away from those hard to maintain SQL queries.
// First user submissions.
$sql = 'SELECT DISTINCT userid FROM {assign_submission} WHERE assignment = ? AND groupid = ?';
$params = array($assignmentid, 0);
$users = $DB->get_records_sql($sql, $params);
foreach ($users as $userid => $unused) {
$params = array('assignment'=>$assignmentid, 'groupid'=>0, 'userid'=>$userid);
// Only return the row with the highest attemptnumber.
$submission = null;
$submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
if ($submissions) {
$submission = reset($submissions);
$submission->latest = 1;
$DB->update_record('assign_submission', $submission);
}
}
// Then group submissions (if any).
$sql = 'SELECT DISTINCT groupid FROM {assign_submission} WHERE assignment = ? AND userid = ?';
$params = array($assignmentid, 0);
$groups = $DB->get_records_sql($sql, $params);
foreach ($groups as $groupid => $unused) {
$params = array('assignment'=>$assignmentid, 'userid'=>0, 'groupid'=>$groupid);
// Only return the row with the highest attemptnumber.
$submission = null;
$submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
if ($submissions) {
$submission = reset($submissions);
$submission->latest = 1;
$DB->update_record('assign_submission', $submission);
}
}
// Now check for records with a grade, but no submission record.
// This happens when a teacher marks a student before they have submitted anything.
$records = $DB->get_recordset_sql('SELECT g.id, g.userid
FROM {assign_grades} g
LEFT JOIN {assign_submission} s
ON s.assignment = g.assignment
AND s.userid = g.userid
WHERE s.id IS NULL AND g.assignment = ?', array($assignmentid));
$submissions = array();
foreach ($records as $record) {
$submission = new stdClass();
$submission->assignment = $assignmentid;
$submission->userid = $record->userid;
$submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
$submission->groupid = 0;
$submission->latest = 1;
$submission->timecreated = time();
$submission->timemodified = time();
array_push($submissions, $submission);
}
$records->close();
$DB->insert_records('assign_submission', $submissions);
}
/**
* Once the database tables have been fully restored, restore the files
* @return void
*/
protected function after_execute() {
$this->add_related_files('mod_assign', 'intro', null);
$this->add_related_files('mod_assign', 'introattachment', null);
$this->set_latest_submission_field();
}
}