Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 
1441 ariadna 145
        // Grade penalty.
146
        if (!isset($data->gradepenalty)) {
147
            $data->gradepenalty = 0;
148
        }
149
 
1 efrain 150
        $newitemid = $DB->insert_record('assign', $data);
151
 
152
        $this->apply_activity_instance($newitemid);
153
    }
154
 
155
    /**
156
     * Process a submission restore
157
     * @param object $data The data in object form
158
     * @return void
159
     */
160
    protected function process_assign_submission($data) {
161
        global $DB;
162
 
163
        if (!$this->includesubmission) {
164
            return;
165
        }
166
 
167
        $data = (object)$data;
168
        $oldid = $data->id;
169
 
170
        $data->assignment = $this->get_new_parentid('assign');
171
 
172
        if ($data->userid > 0) {
173
            $data->userid = $this->get_mappingid('user', $data->userid);
174
        }
175
        if (!empty($data->groupid)) {
176
            $data->groupid = $this->get_mappingid('group', $data->groupid);
177
            if (!$data->groupid) {
178
                // If the group does not exist, then the submission cannot be viewed and restoring can
179
                // violate the unique index on the submission table.
180
                return;
181
            }
182
        } else {
183
            $data->groupid = 0;
184
        }
185
 
186
        // We will correct this in set_latest_submission_field() once all submissions are restored.
187
        $data->latest = 0;
188
 
189
        $newitemid = $DB->insert_record('assign_submission', $data);
190
 
191
        // Note - the old contextid is required in order to be able to restore files stored in
192
        // sub plugin file areas attached to the submissionid.
193
        $this->set_mapping('submission', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
194
    }
195
 
196
    /**
197
     * Process a user_flags restore
198
     * @param object $data The data in object form
199
     * @return void
200
     */
201
    protected function process_assign_userflag($data) {
202
        global $DB;
203
 
204
        $data = (object)$data;
205
        $oldid = $data->id;
206
 
207
        $data->assignment = $this->get_new_parentid('assign');
208
 
209
        $data->userid = $this->get_mappingid('user', $data->userid);
210
        if (!empty($data->allocatedmarker)) {
211
            $data->allocatedmarker = $this->get_mappingid('user', $data->allocatedmarker);
212
        }
213
        if (!empty($data->extensionduedate)) {
214
            $data->extensionduedate = $this->apply_date_offset($data->extensionduedate);
215
        } else {
216
            $data->extensionduedate = 0;
217
        }
218
        // Flags mailed and locked need no translation on restore.
219
 
220
        $newitemid = $DB->insert_record('assign_user_flags', $data);
221
    }
222
 
223
    /**
224
     * Process a grade restore
225
     * @param object $data The data in object form
226
     * @return void
227
     */
228
    protected function process_assign_grade($data) {
229
        global $DB;
230
 
231
        $data = (object)$data;
232
        $oldid = $data->id;
233
 
234
        $data->assignment = $this->get_new_parentid('assign');
235
 
236
        $data->userid = $this->get_mappingid('user', $data->userid);
237
        $data->grader = $this->get_mappingid('user', $data->grader);
238
 
239
        // Handle flags restore to a different table (for upgrade from old backups).
240
        if (!empty($data->extensionduedate) ||
241
                !empty($data->mailed) ||
242
                !empty($data->locked)) {
243
            $flags = new stdClass();
244
            $flags->assignment = $this->get_new_parentid('assign');
245
            if (!empty($data->extensionduedate)) {
246
                $flags->extensionduedate = $this->apply_date_offset($data->extensionduedate);
247
            }
248
            if (!empty($data->mailed)) {
249
                $flags->mailed = $data->mailed;
250
            }
251
            if (!empty($data->locked)) {
252
                $flags->locked = $data->locked;
253
            }
254
            $flags->userid = $this->get_mappingid('user', $data->userid);
255
            $DB->insert_record('assign_user_flags', $flags);
256
        }
257
        // Fix null grades that were rescaled.
258
        if ($data->grade < 0 && $data->grade != ASSIGN_GRADE_NOT_SET) {
259
            $data->grade = ASSIGN_GRADE_NOT_SET;
260
        }
261
        $newitemid = $DB->insert_record('assign_grades', $data);
262
 
263
        // Note - the old contextid is required in order to be able to restore files stored in
264
        // sub plugin file areas attached to the gradeid.
265
        $this->set_mapping('grade', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
266
        $this->set_mapping(restore_gradingform_plugin::itemid_mapping('submissions'), $oldid, $newitemid);
267
    }
268
 
269
    /**
270
     * Process a plugin-config restore
271
     * @param object $data The data in object form
272
     * @return void
273
     */
274
    protected function process_assign_plugin_config($data) {
275
        global $DB;
276
 
277
        $data = (object)$data;
278
        $oldid = $data->id;
279
 
280
        $data->assignment = $this->get_new_parentid('assign');
281
 
282
        $newitemid = $DB->insert_record('assign_plugin_config', $data);
283
    }
284
 
285
    /**
286
     * For all submissions in this assignment, either set the
287
     * submission->latest field to 1 for the latest attempts
288
     * or create a new submission record for grades with no submission.
289
     *
290
     * @return void
291
     */
292
    protected function set_latest_submission_field() {
293
        global $DB, $CFG;
294
 
295
        // Required for constants.
296
        require_once($CFG->dirroot . '/mod/assign/locallib.php');
297
 
298
        $assignmentid = $this->get_new_parentid('assign');
299
 
300
        // First check for records with a grade, but no submission record.
301
        // This happens when a teacher marks a student before they have submitted anything.
302
        $records = $DB->get_recordset_sql('SELECT g.id, g.userid, g.attemptnumber
303
                                           FROM {assign_grades} g
304
                                      LEFT JOIN {assign_submission} s
305
                                             ON s.assignment = g.assignment
306
                                            AND s.userid = g.userid
307
                                          WHERE s.id IS NULL AND g.assignment = ?', array($assignmentid));
308
 
309
        $submissions = array();
310
        foreach ($records as $record) {
311
            $submission = new stdClass();
312
            $submission->assignment = $assignmentid;
313
            $submission->userid = $record->userid;
314
            $submission->attemptnumber = $record->attemptnumber;
315
            $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
316
            $submission->groupid = 0;
317
            $submission->latest = 0;
318
            $submission->timecreated = time();
319
            $submission->timemodified = time();
320
            array_push($submissions, $submission);
321
        }
322
 
323
        $records->close();
324
 
325
        $DB->insert_records('assign_submission', $submissions);
326
 
327
        // This code could be rewritten as a monster SQL - but the point of adding this "latest" field
328
        // to the submissions table in the first place was to get away from those hard to maintain SQL queries.
329
 
330
        // First user submissions.
331
        $sql = 'SELECT DISTINCT userid FROM {assign_submission} WHERE assignment = ? AND groupid = ?';
332
        $params = array($assignmentid, 0);
333
        $users = $DB->get_records_sql($sql, $params);
334
 
335
        foreach ($users as $userid => $unused) {
336
            $params = array('assignment'=>$assignmentid, 'groupid'=>0, 'userid'=>$userid);
337
 
338
            // Only return the row with the highest attemptnumber.
339
            $submission = null;
340
            $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
341
            if ($submissions) {
342
                $submission = reset($submissions);
343
                $submission->latest = 1;
344
                $DB->update_record('assign_submission', $submission);
345
            }
346
        }
347
        // Then group submissions (if any).
348
        $sql = 'SELECT DISTINCT groupid FROM {assign_submission} WHERE assignment = ? AND userid = ?';
349
        $params = array($assignmentid, 0);
350
        $groups = $DB->get_records_sql($sql, $params);
351
 
352
        foreach ($groups as $groupid => $unused) {
353
            $params = array('assignment'=>$assignmentid, 'userid'=>0, 'groupid'=>$groupid);
354
 
355
            // Only return the row with the highest attemptnumber.
356
            $submission = null;
357
            $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
358
            if ($submissions) {
359
                $submission = reset($submissions);
360
                $submission->latest = 1;
361
                $DB->update_record('assign_submission', $submission);
362
            }
363
        }
364
    }
365
 
366
    /**
367
     * Restore files from plugin configuration
368
     * @param string $subtype the plugin type to handle
369
     * @return void
370
     */
371
    protected function add_plugin_config_files($subtype) {
372
        $dummyassign = new assign(null, null, null);
373
        $plugins = $dummyassign->load_plugins($subtype);
374
        foreach ($plugins as $plugin) {
375
            $component = $plugin->get_subtype() . '_' . $plugin->get_type();
376
            $areas = $plugin->get_config_file_areas();
377
            foreach ($areas as $area) {
378
                $this->add_related_files($component, $area, null);
379
            }
380
        }
381
    }
382
 
383
    /**
384
     * Process a assign override restore
385
     * @param object $data The data in object form
386
     * @return void
387
     */
388
    protected function process_assign_override($data) {
389
        global $DB;
390
 
391
        $data = (object)$data;
392
        $oldid = $data->id;
393
 
394
        // Based on userinfo, we'll restore user overides or no.
395
        $userinfo = $this->get_setting_value('userinfo');
396
 
397
        // Skip user overrides if we are not restoring userinfo.
398
        if (!$userinfo && !is_null($data->userid)) {
399
            return;
400
        }
401
 
402
        // Skip group overrides if we are not restoring groupinfo.
403
        $groupinfo = $this->get_setting_value('groups');
404
        if (!$groupinfo && !is_null($data->groupid)) {
405
            return;
406
        }
407
 
408
        $data->assignid = $this->get_new_parentid('assign');
409
 
410
        if (!is_null($data->userid)) {
411
            $data->userid = $this->get_mappingid('user', $data->userid);
412
        }
413
        if (!is_null($data->groupid)) {
414
            $data->groupid = $this->get_mappingid('group', $data->groupid);
415
        }
416
 
417
        $data->allowsubmissionsfromdate = $this->apply_date_offset($data->allowsubmissionsfromdate);
418
        $data->duedate = $this->apply_date_offset($data->duedate);
419
        $data->cutoffdate = $this->apply_date_offset($data->cutoffdate);
420
 
421
        $newitemid = $DB->insert_record('assign_overrides', $data);
422
 
423
        // Add mapping, restore of logs needs it.
424
        $this->set_mapping('assign_override', $oldid, $newitemid);
425
    }
426
 
427
    /**
428
     * Once the database tables have been fully restored, restore the files
429
     */
430
    protected function after_execute() {
431
        $this->add_related_files('mod_assign', 'intro', null);
432
        $this->add_related_files('mod_assign', 'introattachment', null);
433
        $this->add_related_files('mod_assign', 'activityattachment', null);
434
 
435
        $this->add_plugin_config_files('assignsubmission');
436
        $this->add_plugin_config_files('assignfeedback');
437
 
438
        $this->set_latest_submission_field();
439
    }
440
}