forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgradelib.php
382 lines (326 loc) · 17.2 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
<?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();
/** Include locallib.php */
require_once($CFG->dirroot.'/mod/assign/locallib.php');
/** Include accesslib.php */
require_once($CFG->libdir.'/accesslib.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) {
// steps to upgrade an assignment
global $DB, $CFG, $USER;
// steps to upgrade an assignment
// is the user the admin? admin check goes here
if (!is_siteadmin($USER->id)) {
return false;
}
// should we use a shutdown handler to rollback on timeout?
@set_time_limit(ASSIGN_MAX_UPGRADE_TIME_SECS);
// get the module details
$oldmodule = $DB->get_record('modules', array('name'=>'assignment'), '*', MUST_EXIST);
$oldcoursemodule = $DB->get_record('course_modules', array('module'=>$oldmodule->id, 'instance'=>$oldassignmentid), '*', MUST_EXIST);
$oldcontext = context_module::instance($oldcoursemodule->id);
// 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->preventlatesubmissions = $oldassignment->preventlate;
$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;
}
}
}
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;
}
}
}
// see if there is advanced grading upgrades required
$gradingarea = $DB->get_record('grading_areas', array('contextid'=>$oldcontext->id, 'areaname'=>'submission'), '*', IGNORE_MISSING);
if ($gradingarea) {
$DB->update_record('grading_areas', array('id'=>$gradingarea->id, 'contextid'=>$newassignment->get_context()->id, 'component'=>'mod_assign', 'areaname'=>'submissions'));
$gradingdefinitions = $DB->get_records('grading_definitions', array('areaid'=>$gradingarea->id));
}
// upgrade completion data
$DB->set_field('course_modules_completion', 'coursemoduleid', $newcoursemodule->id, array('coursemoduleid'=>$oldcoursemodule->id));
$DB->set_field('course_completion_criteria', 'module', 'assign', array('moduleinstance'=>$oldcoursemodule->id));
$DB->set_field('course_completion_criteria', 'moduleinstance', $newcoursemodule->id, array('moduleinstance'=>$oldcoursemodule->id));
$completiondone = true;
// 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;
$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->locked = $oldsubmission->locked;
$grade->grade = $oldsubmission->grade;
$grade->mailed = $oldsubmission->mailed;
$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) {
$DB->set_field('grading_instances', 'itemid', $grade->id, array('definitionid'=>$definition->id, 'itemid'=>$oldsubmission->id));
}
}
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);
// copy the grades from the old assignment to the new one
$DB->set_field('grade_items', 'itemmodule', 'assign', array('iteminstance'=>$oldassignment->id, 'itemmodule'=>'assignment'));
$DB->set_field('grade_items', 'iteminstance', $newassignment->get_instance()->id, array('iteminstance'=>$oldassignment->id, 'itemmodule'=>'assign'));
$gradesdone = true;
} catch (Exception $exception) {
$rollback = true;
$log .= get_string('conversionexception', 'mod_assign', $exception->error);
}
if ($rollback) {
// roll back the grades changes
if ($gradesdone) {
// copy the grades from the old assignment to the new one
$DB->set_field('grade_items', 'itemmodule', 'assignment', array('iteminstance'=>$newassignment->get_instance()->id, 'itemmodule'=>'assign'));
$DB->set_field('grade_items', 'iteminstance', $oldassignment->id, array('iteminstance'=>$newassignment->get_instance()->id, 'itemmodule'=>'assignment'));
}
// roll back the completion changes
if ($completiondone) {
$DB->set_field('course_modules_completion', 'coursemoduleid', $oldcoursemodule->id, array('coursemoduleid'=>$newcoursemodule->id));
$DB->set_field('course_completion_criteria', 'module', 'assignment', array('moduleinstance'=>$newcoursemodule->id));
$DB->set_field('course_completion_criteria', 'moduleinstance', $oldcoursemodule->id, array('moduleinstance'=>$newcoursemodule->id));
}
// 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));
}
}
$DB->update_record('grading_areas', array('id'=>$gradingarea->id, 'contextid'=>$oldcontext->id, 'component'=>'mod_assignment', 'areaname'=>'submission'));
}
$newassignment->delete_instance();
return false;
}
// all is well,
// delete the old assignment (use object delete)
$cm = get_coursemodule_from_id('', $oldcoursemodule->id, $oldcoursemodule->course);
if ($cm) {
$this->delete_course_module($cm);
}
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->groupmembersonly = $cm->groupmembersonly;
$newcm->completion = $cm->completion;
$newcm->completiongradeitemnumber = $cm->completiongradeitemnumber;
$newcm->completionview = $cm->completionview;
$newcm->completionexpected = $cm->completionexpected;
if(!empty($CFG->enableavailability)) {
$newcm->availablefrom = $cm->availablefrom;
$newcm->availableuntil = $cm->availableuntil;
$newcm->showavailability = $cm->showavailability;
}
$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;
}
$mod = new stdClass();
$mod->course = $newcm->course;
$mod->section = $section->section;
$mod->coursemodule = $newcm->id;
$mod->id = $newcm->id;
$newcm->section = add_mod_to_section($mod, $cm);
// 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;
}
/**
* This function deletes the old assignment course module after
* it has been upgraded. This code is adapted from "course/mod.php".
*
* @param stdClass $cm The course module to delete.
* @return bool
*/
private function delete_course_module($cm) {
global $CFG, $USER, $DB, $OUTPUT;
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$coursecontext = context_course::instance($course->id);
$modcontext = context_module::instance($cm->id);
$modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
if (file_exists($modlib)) {
require_once($modlib);
} else {
print_error('modulemissingcode', '', '', $modlib);
}
$deleteinstancefunction = $cm->modname."_delete_instance";
if (!$deleteinstancefunction($cm->instance)) {
echo $OUTPUT->notification("Could not delete the $cm->modname (instance)");
}
// remove all module files in case modules forget to do that
$fs = get_file_storage();
$fs->delete_area_files($modcontext->id);
if (!delete_course_module($cm->id)) {
echo $OUTPUT->notification("Could not delete the $cm->modname (coursemodule)");
}
if (!delete_mod_from_section($cm->id, $cm->section)) {
echo $OUTPUT->notification("Could not delete the $cm->modname from that section");
}
// Trigger a mod_deleted event with information about this module.
$eventdata = new stdClass();
$eventdata->modulename = $cm->modname;
$eventdata->cmid = $cm->id;
$eventdata->courseid = $course->id;
$eventdata->userid = $USER->id;
events_trigger('mod_deleted', $eventdata);
add_to_log($course->id, 'course', "delete mod",
"view.php?id=$cm->course",
"$cm->modname $cm->instance", $cm->id);
return true;
}
}