forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgradelib.php
440 lines (389 loc) · 20.1 KB
/
upgradelib.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?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/>.
/**
* This file contains the upgrade code to upgrade from mod_assignment to mod_assign
*
* @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();
require_once($CFG->dirroot.'/mod/assign/locallib.php');
require_once($CFG->libdir.'/accesslib.php');
require_once($CFG->dirroot.'/course/lib.php');
/*
* The maximum amount of time to spend upgrading a single assignment.
* This is intentionally generous (5 mins) as the effect of a timeout
* for a legitimate upgrade would be quite harsh (roll back code will not run)
*/
define('ASSIGN_MAX_UPGRADE_TIME_SECS', 300);
/**
* Class to manage upgrades from mod_assignment to mod_assign
*
* @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 assign_upgrade_manager {
/**
* This function converts all of the base settings for an instance of
* the old assignment to the new format. Then it calls each of the plugins
* to see if they can help upgrade this assignment.
* @param int $oldassignmentid (don't rely on the old assignment type even being installed)
* @param string $log This string gets appended to during the conversion process
* @return bool true or false
*/
public function upgrade_assignment($oldassignmentid, & $log) {
global $DB, $CFG, $USER;
// Steps to upgrade an assignment.
core_php_time_limit::raise(ASSIGN_MAX_UPGRADE_TIME_SECS);
// Get the module details.
$oldmodule = $DB->get_record('modules', array('name'=>'assignment'), '*', MUST_EXIST);
$params = array('module'=>$oldmodule->id, 'instance'=>$oldassignmentid);
$oldcoursemodule = $DB->get_record('course_modules',
$params,
'*',
MUST_EXIST);
$oldcontext = context_module::instance($oldcoursemodule->id);
// We used to check for admin capability, but since Moodle 2.7 this is called
// during restore of a mod_assignment module.
// Also note that we do not check for any mod_assignment capabilities, because they can
// be removed so that users don't add new instances of the broken old thing.
if (!has_capability('mod/assign:addinstance', $oldcontext)) {
$log = get_string('couldnotcreatenewassignmentinstance', 'mod_assign');
return false;
}
// First insert an assign instance to get the id.
$oldassignment = $DB->get_record('assignment', array('id'=>$oldassignmentid), '*', MUST_EXIST);
$oldversion = get_config('assignment_' . $oldassignment->assignmenttype, 'version');
$data = new stdClass();
$data->course = $oldassignment->course;
$data->name = $oldassignment->name;
$data->intro = $oldassignment->intro;
$data->introformat = $oldassignment->introformat;
$data->alwaysshowdescription = 1;
$data->sendnotifications = $oldassignment->emailteachers;
$data->sendlatenotifications = $oldassignment->emailteachers;
$data->duedate = $oldassignment->timedue;
$data->allowsubmissionsfromdate = $oldassignment->timeavailable;
$data->grade = $oldassignment->grade;
$data->submissiondrafts = $oldassignment->resubmit;
$data->requiresubmissionstatement = 0;
$data->markingworkflow = 0;
$data->markingallocation = 0;
$data->cutoffdate = 0;
$data->gradingduedate = 0;
// New way to specify no late submissions.
if ($oldassignment->preventlate) {
$data->cutoffdate = $data->duedate;
}
$data->teamsubmission = 0;
$data->requireallteammemberssubmit = 0;
$data->teamsubmissiongroupingid = 0;
$data->blindmarking = 0;
$data->attemptreopenmethod = 'none';
$data->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS;
$newassignment = new assign(null, null, null);
if (!$newassignment->add_instance($data, false)) {
$log = get_string('couldnotcreatenewassignmentinstance', 'mod_assign');
return false;
}
// Now create a new coursemodule from the old one.
$newmodule = $DB->get_record('modules', array('name'=>'assign'), '*', MUST_EXIST);
$newcoursemodule = $this->duplicate_course_module($oldcoursemodule,
$newmodule->id,
$newassignment->get_instance()->id);
if (!$newcoursemodule) {
$log = get_string('couldnotcreatenewcoursemodule', 'mod_assign');
return false;
}
// Convert the base database tables (assignment, submission, grade).
// These are used to store information in case a rollback is required.
$gradingarea = null;
$gradingdefinitions = null;
$gradeidmap = array();
$completiondone = false;
$gradesdone = false;
// From this point we want to rollback on failure.
$rollback = false;
try {
$newassignment->set_context(context_module::instance($newcoursemodule->id));
// The course module has now been created - time to update the core tables.
// Copy intro files.
$newassignment->copy_area_files_for_upgrade($oldcontext->id, 'mod_assignment', 'intro', 0,
$newassignment->get_context()->id, 'mod_assign', 'intro', 0);
// Get the plugins to do their bit.
foreach ($newassignment->get_submission_plugins() as $plugin) {
if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
$plugin->enable();
if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
$rollback = true;
}
} else {
$plugin->disable();
}
}
foreach ($newassignment->get_feedback_plugins() as $plugin) {
if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
$plugin->enable();
if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
$rollback = true;
}
} else {
$plugin->disable();
}
}
// See if there is advanced grading upgrades required.
$gradingarea = $DB->get_record('grading_areas',
array('contextid'=>$oldcontext->id, 'areaname'=>'submission'),
'*',
IGNORE_MISSING);
if ($gradingarea) {
$params = array('id'=>$gradingarea->id,
'contextid'=>$newassignment->get_context()->id,
'component'=>'mod_assign',
'areaname'=>'submissions');
$DB->update_record('grading_areas', $params);
$gradingdefinitions = $DB->get_records('grading_definitions',
array('areaid'=>$gradingarea->id));
}
// Upgrade availability data.
\core_availability\info::update_dependency_id_across_course(
$newcoursemodule->course, 'course_modules', $oldcoursemodule->id, $newcoursemodule->id);
// Upgrade completion data.
$DB->set_field('course_modules_completion',
'coursemoduleid',
$newcoursemodule->id,
array('coursemoduleid'=>$oldcoursemodule->id));
$allcriteria = $DB->get_records('course_completion_criteria',
array('moduleinstance'=>$oldcoursemodule->id));
foreach ($allcriteria as $criteria) {
$criteria->module = 'assign';
$criteria->moduleinstance = $newcoursemodule->id;
$DB->update_record('course_completion_criteria', $criteria);
}
$completiondone = true;
// Migrate log entries so we don't lose them.
$logparams = array('cmid' => $oldcoursemodule->id, 'course' => $oldcoursemodule->course);
$DB->set_field('log', 'module', 'assign', $logparams);
$DB->set_field('log', 'cmid', $newcoursemodule->id, $logparams);
// Copy all the submission data (and get plugins to do their bit).
$oldsubmissions = $DB->get_records('assignment_submissions',
array('assignment'=>$oldassignmentid));
foreach ($oldsubmissions as $oldsubmission) {
$submission = new stdClass();
$submission->assignment = $newassignment->get_instance()->id;
$submission->userid = $oldsubmission->userid;
$submission->timecreated = $oldsubmission->timecreated;
$submission->timemodified = $oldsubmission->timemodified;
$submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
// Because in mod_assignment there could only be one submission per student, it is always the latest.
$submission->latest = 1;
$submission->id = $DB->insert_record('assign_submission', $submission);
if (!$submission->id) {
$log .= get_string('couldnotinsertsubmission', 'mod_assign', $submission->userid);
$rollback = true;
}
foreach ($newassignment->get_submission_plugins() as $plugin) {
if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
if (!$plugin->upgrade($oldcontext,
$oldassignment,
$oldsubmission,
$submission,
$log)) {
$rollback = true;
}
}
}
if ($oldsubmission->timemarked) {
// Submission has been graded - create a grade record.
$grade = new stdClass();
$grade->assignment = $newassignment->get_instance()->id;
$grade->userid = $oldsubmission->userid;
$grade->grader = $oldsubmission->teacher;
$grade->timemodified = $oldsubmission->timemarked;
$grade->timecreated = $oldsubmission->timecreated;
$grade->grade = $oldsubmission->grade;
if ($oldsubmission->mailed) {
// The mailed flag goes in the flags table.
$flags = new stdClass();
$flags->userid = $oldsubmission->userid;
$flags->assignment = $newassignment->get_instance()->id;
$flags->mailed = 1;
$DB->insert_record('assign_user_flags', $flags);
}
$grade->id = $DB->insert_record('assign_grades', $grade);
if (!$grade->id) {
$log .= get_string('couldnotinsertgrade', 'mod_assign', $grade->userid);
$rollback = true;
}
// Copy any grading instances.
if ($gradingarea) {
$gradeidmap[$grade->id] = $oldsubmission->id;
foreach ($gradingdefinitions as $definition) {
$params = array('definitionid'=>$definition->id,
'itemid'=>$oldsubmission->id);
$DB->set_field('grading_instances', 'itemid', $grade->id, $params);
}
}
foreach ($newassignment->get_feedback_plugins() as $plugin) {
if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
if (!$plugin->upgrade($oldcontext,
$oldassignment,
$oldsubmission,
$grade,
$log)) {
$rollback = true;
}
}
}
}
}
$newassignment->update_calendar($newcoursemodule->id);
// Reassociate grade_items from the old assignment instance to the new assign instance.
// This includes outcome linked grade_items.
$params = array('assign', $newassignment->get_instance()->id, 'assignment', $oldassignment->id);
$sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
$DB->execute($sql, $params);
// Create a mapping record to map urls from the old to the new assignment.
$mapping = new stdClass();
$mapping->oldcmid = $oldcoursemodule->id;
$mapping->oldinstance = $oldassignment->id;
$mapping->newcmid = $newcoursemodule->id;
$mapping->newinstance = $newassignment->get_instance()->id;
$mapping->timecreated = time();
$DB->insert_record('assignment_upgrade', $mapping);
$gradesdone = true;
} catch (Exception $exception) {
$rollback = true;
$log .= get_string('conversionexception', 'mod_assign', $exception->getMessage());
}
if ($rollback) {
// Roll back the grades changes.
if ($gradesdone) {
// Reassociate grade_items from the new assign instance to the old assignment instance.
$params = array('assignment', $oldassignment->id, 'assign', $newassignment->get_instance()->id);
$sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
$DB->execute($sql, $params);
}
// Roll back the completion changes.
if ($completiondone) {
$DB->set_field('course_modules_completion',
'coursemoduleid',
$oldcoursemodule->id,
array('coursemoduleid'=>$newcoursemodule->id));
$allcriteria = $DB->get_records('course_completion_criteria',
array('moduleinstance'=>$newcoursemodule->id));
foreach ($allcriteria as $criteria) {
$criteria->module = 'assignment';
$criteria->moduleinstance = $oldcoursemodule->id;
$DB->update_record('course_completion_criteria', $criteria);
}
}
// Roll back the log changes.
$logparams = array('cmid' => $newcoursemodule->id, 'course' => $newcoursemodule->course);
$DB->set_field('log', 'module', 'assignment', $logparams);
$DB->set_field('log', 'cmid', $oldcoursemodule->id, $logparams);
// Roll back the advanced grading update.
if ($gradingarea) {
foreach ($gradeidmap as $newgradeid => $oldsubmissionid) {
foreach ($gradingdefinitions as $definition) {
$DB->set_field('grading_instances',
'itemid',
$oldsubmissionid,
array('definitionid'=>$definition->id, 'itemid'=>$newgradeid));
}
}
$params = array('id'=>$gradingarea->id,
'contextid'=>$oldcontext->id,
'component'=>'mod_assignment',
'areaname'=>'submission');
$DB->update_record('grading_areas', $params);
}
$newassignment->delete_instance();
return false;
}
// Delete the old assignment (use object delete).
$cm = get_coursemodule_from_id('', $oldcoursemodule->id, $oldcoursemodule->course);
if ($cm) {
course_delete_module($cm->id);
}
rebuild_course_cache($oldcoursemodule->course);
return true;
}
/**
* Create a duplicate course module record so we can create the upgraded
* assign module alongside the old assignment module.
*
* @param stdClass $cm The old course module record
* @param int $moduleid The id of the new assign module
* @param int $newinstanceid The id of the new instance of the assign module
* @return mixed stdClass|bool The new course module record or FALSE
*/
private function duplicate_course_module(stdClass $cm, $moduleid, $newinstanceid) {
global $DB, $CFG;
$newcm = new stdClass();
$newcm->course = $cm->course;
$newcm->module = $moduleid;
$newcm->instance = $newinstanceid;
$newcm->visible = $cm->visible;
$newcm->section = $cm->section;
$newcm->score = $cm->score;
$newcm->indent = $cm->indent;
$newcm->groupmode = $cm->groupmode;
$newcm->groupingid = $cm->groupingid;
$newcm->completion = $cm->completion;
$newcm->completiongradeitemnumber = $cm->completiongradeitemnumber;
$newcm->completionview = $cm->completionview;
$newcm->completionexpected = $cm->completionexpected;
if (!empty($CFG->enableavailability)) {
$newcm->availability = $cm->availability;
}
$newcm->showdescription = $cm->showdescription;
$newcmid = add_course_module($newcm);
$newcm = get_coursemodule_from_id('', $newcmid, $cm->course);
if (!$newcm) {
return false;
}
$section = $DB->get_record("course_sections", array("id"=>$newcm->section));
if (!$section) {
return false;
}
$newcm->section = course_add_cm_to_section($newcm->course, $newcm->id, $section->section, $cm->id);
// Make sure visibility is set correctly (in particular in calendar).
// Note: Allow them to set it even without moodle/course:activityvisibility.
set_coursemodule_visible($newcm->id, $newcm->visible);
return $newcm;
}
}
/**
* Determines if the assignment as any null grades that were rescaled.
*
* Null grades are stored as -1 but should never be rescaled.
*
* @return int[] Array of the ids of all the assignments with rescaled null grades.
*/
function get_assignments_with_rescaled_null_grades() {
global $DB;
$query = 'SELECT id, assignment FROM {assign_grades}
WHERE grade < 0 AND grade <> -1';
$assignments = array_values($DB->get_records_sql($query));
$getassignmentid = function ($assignment) {
return $assignment->assignment;
};
$assignments = array_map($getassignmentid, $assignments);
return $assignments;
}