Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
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
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
require_once($CFG->dirroot . '/mod/assign/locallib.php');
28
 
29
/**
30
 * Define the complete assignment structure for restore, with file and id annotations
31
 *
32
 * @package   mod_assign
33
 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class restore_assign_activity_structure_step extends restore_activity_structure_step {
37
 
38
    /**
39
     * Store whether submission details should be included. Details may not be included if the
40
     * this is a team submission, but groups/grouping information was not included in the backup.
41
     */
42
    protected $includesubmission = true;
43
 
44
    /**
45
     * Define the structure of the restore workflow.
46
     *
47
     * @return restore_path_element $structure
48
     */
49
    protected function define_structure() {
50
 
51
        $paths = array();
52
        // To know if we are including userinfo.
53
        $userinfo = $this->get_setting_value('userinfo');
54
 
55
        // Define each element separated.
56
        $paths[] = new restore_path_element('assign', '/activity/assign');
57
        if ($userinfo) {
58
            $submission = new restore_path_element('assign_submission',
59
                                                   '/activity/assign/submissions/submission');
60
            $paths[] = $submission;
61
            $this->add_subplugin_structure('assignsubmission', $submission);
62
            $grade = new restore_path_element('assign_grade', '/activity/assign/grades/grade');
63
            $paths[] = $grade;
64
            $this->add_subplugin_structure('assignfeedback', $grade);
65
            $userflag = new restore_path_element('assign_userflag',
66
                                                   '/activity/assign/userflags/userflag');
67
            $paths[] = $userflag;
68
        }
69
 
70
        $paths[] = new restore_path_element('assign_override', '/activity/assign/overrides/override');
71
        $paths[] = new restore_path_element('assign_plugin_config',
72
                                            '/activity/assign/plugin_configs/plugin_config');
73
 
74
        return $this->prepare_activity_structure($paths);
75
    }
76
 
77
    /**
78
     * Process an assign restore.
79
     *
80
     * @param object $data The data in object form
81
     * @return void
82
     */
83
    protected function process_assign($data) {
84
        global $DB;
85
 
86
        $data = (object)$data;
87
        $oldid = $data->id;
88
        $data->course = $this->get_courseid();
89
 
90
        // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
91
        // See MDL-9367.
92
        $data->allowsubmissionsfromdate = $this->apply_date_offset($data->allowsubmissionsfromdate);
93
        $data->duedate = $this->apply_date_offset($data->duedate);
94
 
95
        // If this is a team submission, but there is no group info we need to flag that the submission
96
        // information should not be included. It should not be restored.
97
        $groupinfo = $this->task->get_setting_value('groups');
98
        if ($data->teamsubmission && !$groupinfo) {
99
            $this->includesubmission = false;
100
        }
101
 
102
        // Reset revealidentities if blindmarking with no user data (MDL-43796).
103
        $userinfo = $this->get_setting_value('userinfo');
104
        if (!$userinfo && $data->blindmarking) {
105
            $data->revealidentities = 0;
106
        }
107
 
108
        if (!empty($data->teamsubmissiongroupingid)) {
109
            $data->teamsubmissiongroupingid = $this->get_mappingid('grouping',
110
                                                                   $data->teamsubmissiongroupingid);
111
        } else {
112
            $data->teamsubmissiongroupingid = 0;
113
        }
114
        if (!isset($data->cutoffdate)) {
115
            $data->cutoffdate = 0;
116
        }
117
        if (!isset($data->gradingduedate)) {
118
            $data->gradingduedate = 0;
119
        } else {
120
            $data->gradingduedate = $this->apply_date_offset($data->gradingduedate);
121
        }
122
        if (!isset($data->markingworkflow)) {
123
            $data->markingworkflow = 0;
124
        }
125
        if (!isset($data->markingallocation)) {
126
            $data->markingallocation = 0;
127
        }
128
        if (!isset($data->markinganonymous)) {
129
            $data->markinganonymous = 0;
130
        }
131
        if (!isset($data->preventsubmissionnotingroup)) {
132
            $data->preventsubmissionnotingroup = 0;
133
        }
134
 
135
        if (!empty($data->preventlatesubmissions)) {
136
            $data->cutoffdate = $data->duedate;
137
        } else {
138
            $data->cutoffdate = $this->apply_date_offset($data->cutoffdate);
139
        }
140
 
141
        if ($data->grade < 0) { // Scale found, get mapping.
142
            $data->grade = -($this->get_mappingid('scale', abs($data->grade)));
143
        }
144
 
145
        $newitemid = $DB->insert_record('assign', $data);
146
 
147
        $this->apply_activity_instance($newitemid);
148
    }
149
 
150
    /**
151
     * Process a submission restore
152
     * @param object $data The data in object form
153
     * @return void
154
     */
155
    protected function process_assign_submission($data) {
156
        global $DB;
157
 
158
        if (!$this->includesubmission) {
159
            return;
160
        }
161
 
162
        $data = (object)$data;
163
        $oldid = $data->id;
164
 
165
        $data->assignment = $this->get_new_parentid('assign');
166
 
167
        if ($data->userid > 0) {
168
            $data->userid = $this->get_mappingid('user', $data->userid);
169
        }
170
        if (!empty($data->groupid)) {
171
            $data->groupid = $this->get_mappingid('group', $data->groupid);
172
            if (!$data->groupid) {
173
                // If the group does not exist, then the submission cannot be viewed and restoring can
174
                // violate the unique index on the submission table.
175
                return;
176
            }
177
        } else {
178
            $data->groupid = 0;
179
        }
180
 
181
        // We will correct this in set_latest_submission_field() once all submissions are restored.
182
        $data->latest = 0;
183
 
184
        $newitemid = $DB->insert_record('assign_submission', $data);
185
 
186
        // Note - the old contextid is required in order to be able to restore files stored in
187
        // sub plugin file areas attached to the submissionid.
188
        $this->set_mapping('submission', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
189
    }
190
 
191
    /**
192
     * Process a user_flags restore
193
     * @param object $data The data in object form
194
     * @return void
195
     */
196
    protected function process_assign_userflag($data) {
197
        global $DB;
198
 
199
        $data = (object)$data;
200
        $oldid = $data->id;
201
 
202
        $data->assignment = $this->get_new_parentid('assign');
203
 
204
        $data->userid = $this->get_mappingid('user', $data->userid);
205
        if (!empty($data->allocatedmarker)) {
206
            $data->allocatedmarker = $this->get_mappingid('user', $data->allocatedmarker);
207
        }
208
        if (!empty($data->extensionduedate)) {
209
            $data->extensionduedate = $this->apply_date_offset($data->extensionduedate);
210
        } else {
211
            $data->extensionduedate = 0;
212
        }
213
        // Flags mailed and locked need no translation on restore.
214
 
215
        $newitemid = $DB->insert_record('assign_user_flags', $data);
216
    }
217
 
218
    /**
219
     * Process a grade restore
220
     * @param object $data The data in object form
221
     * @return void
222
     */
223
    protected function process_assign_grade($data) {
224
        global $DB;
225
 
226
        $data = (object)$data;
227
        $oldid = $data->id;
228
 
229
        $data->assignment = $this->get_new_parentid('assign');
230
 
231
        $data->userid = $this->get_mappingid('user', $data->userid);
232
        $data->grader = $this->get_mappingid('user', $data->grader);
233
 
234
        // Handle flags restore to a different table (for upgrade from old backups).
235
        if (!empty($data->extensionduedate) ||
236
                !empty($data->mailed) ||
237
                !empty($data->locked)) {
238
            $flags = new stdClass();
239
            $flags->assignment = $this->get_new_parentid('assign');
240
            if (!empty($data->extensionduedate)) {
241
                $flags->extensionduedate = $this->apply_date_offset($data->extensionduedate);
242
            }
243
            if (!empty($data->mailed)) {
244
                $flags->mailed = $data->mailed;
245
            }
246
            if (!empty($data->locked)) {
247
                $flags->locked = $data->locked;
248
            }
249
            $flags->userid = $this->get_mappingid('user', $data->userid);
250
            $DB->insert_record('assign_user_flags', $flags);
251
        }
252
        // Fix null grades that were rescaled.
253
        if ($data->grade < 0 && $data->grade != ASSIGN_GRADE_NOT_SET) {
254
            $data->grade = ASSIGN_GRADE_NOT_SET;
255
        }
256
        $newitemid = $DB->insert_record('assign_grades', $data);
257
 
258
        // Note - the old contextid is required in order to be able to restore files stored in
259
        // sub plugin file areas attached to the gradeid.
260
        $this->set_mapping('grade', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
261
        $this->set_mapping(restore_gradingform_plugin::itemid_mapping('submissions'), $oldid, $newitemid);
262
    }
263
 
264
    /**
265
     * Process a plugin-config restore
266
     * @param object $data The data in object form
267
     * @return void
268
     */
269
    protected function process_assign_plugin_config($data) {
270
        global $DB;
271
 
272
        $data = (object)$data;
273
        $oldid = $data->id;
274
 
275
        $data->assignment = $this->get_new_parentid('assign');
276
 
277
        $newitemid = $DB->insert_record('assign_plugin_config', $data);
278
    }
279
 
280
    /**
281
     * For all submissions in this assignment, either set the
282
     * submission->latest field to 1 for the latest attempts
283
     * or create a new submission record for grades with no submission.
284
     *
285
     * @return void
286
     */
287
    protected function set_latest_submission_field() {
288
        global $DB, $CFG;
289
 
290
        // Required for constants.
291
        require_once($CFG->dirroot . '/mod/assign/locallib.php');
292
 
293
        $assignmentid = $this->get_new_parentid('assign');
294
 
295
        // First check for records with a grade, but no submission record.
296
        // This happens when a teacher marks a student before they have submitted anything.
297
        $records = $DB->get_recordset_sql('SELECT g.id, g.userid, g.attemptnumber
298
                                           FROM {assign_grades} g
299
                                      LEFT JOIN {assign_submission} s
300
                                             ON s.assignment = g.assignment
301
                                            AND s.userid = g.userid
302
                                          WHERE s.id IS NULL AND g.assignment = ?', array($assignmentid));
303
 
304
        $submissions = array();
305
        foreach ($records as $record) {
306
            $submission = new stdClass();
307
            $submission->assignment = $assignmentid;
308
            $submission->userid = $record->userid;
309
            $submission->attemptnumber = $record->attemptnumber;
310
            $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
311
            $submission->groupid = 0;
312
            $submission->latest = 0;
313
            $submission->timecreated = time();
314
            $submission->timemodified = time();
315
            array_push($submissions, $submission);
316
        }
317
 
318
        $records->close();
319
 
320
        $DB->insert_records('assign_submission', $submissions);
321
 
322
        // This code could be rewritten as a monster SQL - but the point of adding this "latest" field
323
        // to the submissions table in the first place was to get away from those hard to maintain SQL queries.
324
 
325
        // First user submissions.
326
        $sql = 'SELECT DISTINCT userid FROM {assign_submission} WHERE assignment = ? AND groupid = ?';
327
        $params = array($assignmentid, 0);
328
        $users = $DB->get_records_sql($sql, $params);
329
 
330
        foreach ($users as $userid => $unused) {
331
            $params = array('assignment'=>$assignmentid, 'groupid'=>0, 'userid'=>$userid);
332
 
333
            // Only return the row with the highest attemptnumber.
334
            $submission = null;
335
            $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
336
            if ($submissions) {
337
                $submission = reset($submissions);
338
                $submission->latest = 1;
339
                $DB->update_record('assign_submission', $submission);
340
            }
341
        }
342
        // Then group submissions (if any).
343
        $sql = 'SELECT DISTINCT groupid FROM {assign_submission} WHERE assignment = ? AND userid = ?';
344
        $params = array($assignmentid, 0);
345
        $groups = $DB->get_records_sql($sql, $params);
346
 
347
        foreach ($groups as $groupid => $unused) {
348
            $params = array('assignment'=>$assignmentid, 'userid'=>0, 'groupid'=>$groupid);
349
 
350
            // Only return the row with the highest attemptnumber.
351
            $submission = null;
352
            $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
353
            if ($submissions) {
354
                $submission = reset($submissions);
355
                $submission->latest = 1;
356
                $DB->update_record('assign_submission', $submission);
357
            }
358
        }
359
    }
360
 
361
    /**
362
     * Restore files from plugin configuration
363
     * @param string $subtype the plugin type to handle
364
     * @return void
365
     */
366
    protected function add_plugin_config_files($subtype) {
367
        $dummyassign = new assign(null, null, null);
368
        $plugins = $dummyassign->load_plugins($subtype);
369
        foreach ($plugins as $plugin) {
370
            $component = $plugin->get_subtype() . '_' . $plugin->get_type();
371
            $areas = $plugin->get_config_file_areas();
372
            foreach ($areas as $area) {
373
                $this->add_related_files($component, $area, null);
374
            }
375
        }
376
    }
377
 
378
    /**
379
     * Process a assign override restore
380
     * @param object $data The data in object form
381
     * @return void
382
     */
383
    protected function process_assign_override($data) {
384
        global $DB;
385
 
386
        $data = (object)$data;
387
        $oldid = $data->id;
388
 
389
        // Based on userinfo, we'll restore user overides or no.
390
        $userinfo = $this->get_setting_value('userinfo');
391
 
392
        // Skip user overrides if we are not restoring userinfo.
393
        if (!$userinfo && !is_null($data->userid)) {
394
            return;
395
        }
396
 
397
        // Skip group overrides if we are not restoring groupinfo.
398
        $groupinfo = $this->get_setting_value('groups');
399
        if (!$groupinfo && !is_null($data->groupid)) {
400
            return;
401
        }
402
 
403
        $data->assignid = $this->get_new_parentid('assign');
404
 
405
        if (!is_null($data->userid)) {
406
            $data->userid = $this->get_mappingid('user', $data->userid);
407
        }
408
        if (!is_null($data->groupid)) {
409
            $data->groupid = $this->get_mappingid('group', $data->groupid);
410
        }
411
 
412
        $data->allowsubmissionsfromdate = $this->apply_date_offset($data->allowsubmissionsfromdate);
413
        $data->duedate = $this->apply_date_offset($data->duedate);
414
        $data->cutoffdate = $this->apply_date_offset($data->cutoffdate);
415
 
416
        $newitemid = $DB->insert_record('assign_overrides', $data);
417
 
418
        // Add mapping, restore of logs needs it.
419
        $this->set_mapping('assign_override', $oldid, $newitemid);
420
    }
421
 
422
    /**
423
     * Once the database tables have been fully restored, restore the files
424
     */
425
    protected function after_execute() {
426
        $this->add_related_files('mod_assign', 'intro', null);
427
        $this->add_related_files('mod_assign', 'introattachment', null);
428
        $this->add_related_files('mod_assign', 'activityattachment', null);
429
 
430
        $this->add_plugin_config_files('assignsubmission');
431
        $this->add_plugin_config_files('assignfeedback');
432
 
433
        $this->set_latest_submission_field();
434
    }
435
}