Merge branch 'MDL-37966' of git://github.com/jonof/moodle
[moodle.git] / mod / assign / upgradelib.php
CommitLineData
bbd0e548
DW
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 * This file contains the upgrade code to upgrade from mod_assignment to mod_assign
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
25defined('MOODLE_INTERNAL') || die();
26
bbd0e548 27require_once($CFG->dirroot.'/mod/assign/locallib.php');
bbd0e548 28require_once($CFG->libdir.'/accesslib.php');
a347aee3 29require_once($CFG->dirroot.'/course/lib.php');
bbd0e548 30
e5403f8c 31/*
5b121144
DW
32 * The maximum amount of time to spend upgrading a single assignment.
33 * This is intentionally generous (5 mins) as the effect of a timeout
34 * for a legitimate upgrade would be quite harsh (roll back code will not run)
35 */
36define('ASSIGN_MAX_UPGRADE_TIME_SECS', 300);
bbd0e548
DW
37
38/**
39 * Class to manage upgrades from mod_assignment to mod_assign
40 *
41 * @package mod_assign
42 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class assign_upgrade_manager {
46
47 /**
48 * This function converts all of the base settings for an instance of
49 * the old assignment to the new format. Then it calls each of the plugins
50 * to see if they can help upgrade this assignment.
51 * @param int $oldassignmentid (don't rely on the old assignment type even being installed)
52 * @param string $log This string gets appended to during the conversion process
53 * @return bool true or false
54 */
55 public function upgrade_assignment($oldassignmentid, & $log) {
bbd0e548 56 global $DB, $CFG, $USER;
e5403f8c 57 // Steps to upgrade an assignment.
bbd0e548 58
e5403f8c 59 // Is the user the admin? admin check goes here.
bbd0e548
DW
60 if (!is_siteadmin($USER->id)) {
61 return false;
62 }
63
5b121144
DW
64 @set_time_limit(ASSIGN_MAX_UPGRADE_TIME_SECS);
65
e5403f8c 66 // Get the module details.
bbd0e548 67 $oldmodule = $DB->get_record('modules', array('name'=>'assignment'), '*', MUST_EXIST);
e5403f8c
DW
68 $params = array('module'=>$oldmodule->id, 'instance'=>$oldassignmentid);
69 $oldcoursemodule = $DB->get_record('course_modules',
70 $params,
71 '*',
72 MUST_EXIST);
bbd0e548
DW
73 $oldcontext = context_module::instance($oldcoursemodule->id);
74
e5403f8c 75 // First insert an assign instance to get the id.
bbd0e548
DW
76 $oldassignment = $DB->get_record('assignment', array('id'=>$oldassignmentid), '*', MUST_EXIST);
77
78 $oldversion = get_config('assignment_' . $oldassignment->assignmenttype, 'version');
79
80 $data = new stdClass();
81 $data->course = $oldassignment->course;
82 $data->name = $oldassignment->name;
83 $data->intro = $oldassignment->intro;
84 $data->introformat = $oldassignment->introformat;
85 $data->alwaysshowdescription = 1;
86 $data->sendnotifications = $oldassignment->emailteachers;
56ff1300 87 $data->sendlatenotifications = $oldassignment->emailteachers;
bbd0e548
DW
88 $data->duedate = $oldassignment->timedue;
89 $data->allowsubmissionsfromdate = $oldassignment->timeavailable;
90 $data->grade = $oldassignment->grade;
91 $data->submissiondrafts = $oldassignment->resubmit;
94f26900 92 $data->requiresubmissionstatement = 0;
9e795179
DW
93 $data->cutoffdate = 0;
94 // New way to specify no late submissions.
95 if ($oldassignment->preventlate) {
96 $data->cutoffdate = $data->duedate;
97 }
7c7bc2ee
DW
98 $data->teamsubmission = 0;
99 $data->requireallteammemberssubmit = 0;
100 $data->teamsubmissiongroupingid = 0;
101 $data->blindmarking = 0;
df211804
DW
102 $data->attemptreopenmethod = 'none';
103 $data->maxattempts = ASSIGN_UNLIMITED_ATTEMPTS;
bbd0e548
DW
104
105 $newassignment = new assign(null, null, null);
106
107 if (!$newassignment->add_instance($data, false)) {
108 $log = get_string('couldnotcreatenewassignmentinstance', 'mod_assign');
109 return false;
110 }
111
e5403f8c 112 // Now create a new coursemodule from the old one.
bbd0e548 113 $newmodule = $DB->get_record('modules', array('name'=>'assign'), '*', MUST_EXIST);
e5403f8c
DW
114 $newcoursemodule = $this->duplicate_course_module($oldcoursemodule,
115 $newmodule->id,
116 $newassignment->get_instance()->id);
bbd0e548
DW
117 if (!$newcoursemodule) {
118 $log = get_string('couldnotcreatenewcoursemodule', 'mod_assign');
119 return false;
120 }
121
e5403f8c 122 // Convert the base database tables (assignment, submission, grade).
bbd0e548 123
e5403f8c 124 // These are used to store information in case a rollback is required.
bbd0e548
DW
125 $gradingarea = null;
126 $gradingdefinitions = null;
127 $gradeidmap = array();
128 $completiondone = false;
c45261df 129 $gradesdone = false;
bbd0e548 130
e5403f8c 131 // From this point we want to rollback on failure.
bbd0e548
DW
132 $rollback = false;
133 try {
134 $newassignment->set_context(context_module::instance($newcoursemodule->id));
135
e5403f8c 136 // The course module has now been created - time to update the core tables.
bbd0e548 137
e5403f8c 138 // Copy intro files.
bbd0e548
DW
139 $newassignment->copy_area_files_for_upgrade($oldcontext->id, 'mod_assignment', 'intro', 0,
140 $newassignment->get_context()->id, 'mod_assign', 'intro', 0);
141
e5403f8c 142 // Get the plugins to do their bit.
bbd0e548
DW
143 foreach ($newassignment->get_submission_plugins() as $plugin) {
144 if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
145 $plugin->enable();
146 if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
147 $rollback = true;
148 }
fb11a702
LT
149 } else {
150 $plugin->disable();
bbd0e548
DW
151 }
152 }
153 foreach ($newassignment->get_feedback_plugins() as $plugin) {
154 if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
155 $plugin->enable();
156 if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
157 $rollback = true;
158 }
fb11a702
LT
159 } else {
160 $plugin->disable();
bbd0e548
DW
161 }
162 }
163
e5403f8c
DW
164 // See if there is advanced grading upgrades required.
165 $gradingarea = $DB->get_record('grading_areas',
166 array('contextid'=>$oldcontext->id, 'areaname'=>'submission'),
167 '*',
168 IGNORE_MISSING);
bbd0e548 169 if ($gradingarea) {
e5403f8c
DW
170 $params = array('id'=>$gradingarea->id,
171 'contextid'=>$newassignment->get_context()->id,
172 'component'=>'mod_assign',
173 'areaname'=>'submissions');
174 $DB->update_record('grading_areas', $params);
175 $gradingdefinitions = $DB->get_records('grading_definitions',
176 array('areaid'=>$gradingarea->id));
bbd0e548
DW
177 }
178
7720f485
DW
179 // Upgrade availability data.
180 $DB->set_field('course_modules_avail_fields',
181 'coursemoduleid',
182 $newcoursemodule->id,
183 array('coursemoduleid'=>$oldcoursemodule->id));
184 $DB->set_field('course_modules_availability',
185 'coursemoduleid',
186 $newcoursemodule->id,
187 array('coursemoduleid'=>$oldcoursemodule->id));
188 $DB->set_field('course_modules_availability',
189 'sourcecmid',
190 $newcoursemodule->id,
191 array('sourcecmid'=>$oldcoursemodule->id));
192 $DB->set_field('course_sections_availability',
193 'sourcecmid',
194 $newcoursemodule->id,
195 array('sourcecmid'=>$oldcoursemodule->id));
196
e5403f8c
DW
197 // Upgrade completion data.
198 $DB->set_field('course_modules_completion',
199 'coursemoduleid',
200 $newcoursemodule->id,
201 array('coursemoduleid'=>$oldcoursemodule->id));
202 $allcriteria = $DB->get_records('course_completion_criteria',
203 array('moduleinstance'=>$oldcoursemodule->id));
1f417de8
DW
204 foreach ($allcriteria as $criteria) {
205 $criteria->module = 'assign';
206 $criteria->moduleinstance = $newcoursemodule->id;
207 $DB->update_record('course_completion_criteria', $criteria);
208 }
bbd0e548
DW
209 $completiondone = true;
210
ac9ac1fe
AO
211 // Migrate log entries so we don't lose them.
212 $logparams = array('cmid' => $oldcoursemodule->id, 'course' => $oldcoursemodule->course);
213 $DB->set_field('log', 'module', 'assign', $logparams);
214 $DB->set_field('log', 'cmid', $newcoursemodule->id, $logparams);
215
e5403f8c
DW
216 // Copy all the submission data (and get plugins to do their bit).
217 $oldsubmissions = $DB->get_records('assignment_submissions',
218 array('assignment'=>$oldassignmentid));
bbd0e548
DW
219
220 foreach ($oldsubmissions as $oldsubmission) {
221 $submission = new stdClass();
222 $submission->assignment = $newassignment->get_instance()->id;
223 $submission->userid = $oldsubmission->userid;
224 $submission->timecreated = $oldsubmission->timecreated;
225 $submission->timemodified = $oldsubmission->timemodified;
226 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
227 $submission->id = $DB->insert_record('assign_submission', $submission);
228 if (!$submission->id) {
229 $log .= get_string('couldnotinsertsubmission', 'mod_assign', $submission->userid);
230 $rollback = true;
231 }
232 foreach ($newassignment->get_submission_plugins() as $plugin) {
233 if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
e5403f8c
DW
234 if (!$plugin->upgrade($oldcontext,
235 $oldassignment,
236 $oldsubmission,
237 $submission,
238 $log)) {
bbd0e548
DW
239 $rollback = true;
240 }
241 }
242 }
243 if ($oldsubmission->timemarked) {
e5403f8c 244 // Submission has been graded - create a grade record.
bbd0e548
DW
245 $grade = new stdClass();
246 $grade->assignment = $newassignment->get_instance()->id;
247 $grade->userid = $oldsubmission->userid;
248 $grade->grader = $oldsubmission->teacher;
249 $grade->timemodified = $oldsubmission->timemarked;
250 $grade->timecreated = $oldsubmission->timecreated;
bbd0e548 251 $grade->grade = $oldsubmission->grade;
df211804
DW
252 if ($oldsubmission->mailed) {
253 // The mailed flag goes in the flags table.
254 $flags = new stdClass();
255 $flags->userid = $oldsubmission->userid;
256 $flags->assignment = $newassignment->get_instance()->id;
257 $flags->mailed = 1;
258 $DB->insert_record('assign_user_flags', $flags);
259 }
bbd0e548
DW
260 $grade->id = $DB->insert_record('assign_grades', $grade);
261 if (!$grade->id) {
262 $log .= get_string('couldnotinsertgrade', 'mod_assign', $grade->userid);
263 $rollback = true;
264 }
265
e5403f8c 266 // Copy any grading instances.
bbd0e548
DW
267 if ($gradingarea) {
268
269 $gradeidmap[$grade->id] = $oldsubmission->id;
270
271 foreach ($gradingdefinitions as $definition) {
e5403f8c
DW
272 $params = array('definitionid'=>$definition->id,
273 'itemid'=>$oldsubmission->id);
274 $DB->set_field('grading_instances', 'itemid', $grade->id, $params);
bbd0e548
DW
275 }
276
277 }
278 foreach ($newassignment->get_feedback_plugins() as $plugin) {
279 if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
e5403f8c
DW
280 if (!$plugin->upgrade($oldcontext,
281 $oldassignment,
282 $oldsubmission,
283 $grade,
284 $log)) {
bbd0e548
DW
285 $rollback = true;
286 }
287 }
288 }
289 }
290 }
291
292 $newassignment->update_calendar($newcoursemodule->id);
bbd0e548 293
f264499f
DW
294 // Reassociate grade_items from the old assignment instance to the new assign instance.
295 // This includes outcome linked grade_items.
296 $params = array('assign', $newassignment->get_instance()->id, 'assignment', $oldassignment->id);
297 $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
298 $DB->execute($sql, $params);
1f417de8 299
c45261df 300 $gradesdone = true;
bbd0e548
DW
301
302 } catch (Exception $exception) {
303 $rollback = true;
c6d7956e 304 $log .= get_string('conversionexception', 'mod_assign', $exception->getMessage());
bbd0e548
DW
305 }
306
307 if ($rollback) {
e5403f8c 308 // Roll back the grades changes.
c45261df 309 if ($gradesdone) {
f264499f
DW
310 // Reassociate grade_items from the new assign instance to the old assignment instance.
311 $params = array('assignment', $oldassignment->id, 'assign', $newassignment->get_instance()->id);
312 $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
313 $DB->execute($sql, $params);
c45261df 314 }
e5403f8c 315 // Roll back the completion changes.
bbd0e548 316 if ($completiondone) {
e5403f8c
DW
317 $DB->set_field('course_modules_completion',
318 'coursemoduleid',
319 $oldcoursemodule->id,
320 array('coursemoduleid'=>$newcoursemodule->id));
321
322 $allcriteria = $DB->get_records('course_completion_criteria',
323 array('moduleinstance'=>$newcoursemodule->id));
1f417de8
DW
324 foreach ($allcriteria as $criteria) {
325 $criteria->module = 'assignment';
326 $criteria->moduleinstance = $oldcoursemodule->id;
327 $DB->update_record('course_completion_criteria', $criteria);
328 }
bbd0e548 329 }
e5403f8c 330 // Roll back the log changes.
ac9ac1fe
AO
331 $logparams = array('cmid' => $newcoursemodule->id, 'course' => $newcoursemodule->course);
332 $DB->set_field('log', 'module', 'assignment', $logparams);
333 $DB->set_field('log', 'cmid', $oldcoursemodule->id, $logparams);
e5403f8c 334 // Roll back the advanced grading update.
bbd0e548
DW
335 if ($gradingarea) {
336 foreach ($gradeidmap as $newgradeid => $oldsubmissionid) {
337 foreach ($gradingdefinitions as $definition) {
e5403f8c
DW
338 $DB->set_field('grading_instances',
339 'itemid',
340 $oldsubmissionid,
341 array('definitionid'=>$definition->id, 'itemid'=>$newgradeid));
bbd0e548
DW
342 }
343 }
e5403f8c
DW
344 $params = array('id'=>$gradingarea->id,
345 'contextid'=>$oldcontext->id,
346 'component'=>'mod_assignment',
347 'areaname'=>'submission');
348 $DB->update_record('grading_areas', $params);
bbd0e548
DW
349 }
350 $newassignment->delete_instance();
351
352 return false;
353 }
e5403f8c 354 // Delete the old assignment (use object delete).
bbd0e548
DW
355 $cm = get_coursemodule_from_id('', $oldcoursemodule->id, $oldcoursemodule->course);
356 if ($cm) {
a347aee3 357 course_delete_module($cm->id);
bbd0e548
DW
358 }
359 rebuild_course_cache($oldcoursemodule->course);
360 return true;
361 }
362
363
364 /**
365 * Create a duplicate course module record so we can create the upgraded
366 * assign module alongside the old assignment module.
367 *
368 * @param stdClass $cm The old course module record
369 * @param int $moduleid The id of the new assign module
370 * @param int $newinstanceid The id of the new instance of the assign module
371 * @return mixed stdClass|bool The new course module record or FALSE
372 */
373 private function duplicate_course_module(stdClass $cm, $moduleid, $newinstanceid) {
374 global $DB, $CFG;
375
376 $newcm = new stdClass();
377 $newcm->course = $cm->course;
378 $newcm->module = $moduleid;
379 $newcm->instance = $newinstanceid;
380 $newcm->visible = $cm->visible;
381 $newcm->section = $cm->section;
382 $newcm->score = $cm->score;
383 $newcm->indent = $cm->indent;
384 $newcm->groupmode = $cm->groupmode;
385 $newcm->groupingid = $cm->groupingid;
386 $newcm->groupmembersonly = $cm->groupmembersonly;
387 $newcm->completion = $cm->completion;
388 $newcm->completiongradeitemnumber = $cm->completiongradeitemnumber;
389 $newcm->completionview = $cm->completionview;
390 $newcm->completionexpected = $cm->completionexpected;
e5403f8c 391 if (!empty($CFG->enableavailability)) {
bbd0e548
DW
392 $newcm->availablefrom = $cm->availablefrom;
393 $newcm->availableuntil = $cm->availableuntil;
394 $newcm->showavailability = $cm->showavailability;
395 }
396 $newcm->showdescription = $cm->showdescription;
397
398 $newcmid = add_course_module($newcm);
399 $newcm = get_coursemodule_from_id('', $newcmid, $cm->course);
400 if (!$newcm) {
401 return false;
402 }
403 $section = $DB->get_record("course_sections", array("id"=>$newcm->section));
404 if (!$section) {
405 return false;
406 }
407
bdc4cb9e 408 $newcm->section = course_add_cm_to_section($newcm->course, $newcm->id, $section->section, $cm->id);
bbd0e548 409
e5403f8c
DW
410 // Make sure visibility is set correctly (in particular in calendar).
411 // Note: Allow them to set it even without moodle/course:activityvisibility.
bbd0e548
DW
412 set_coursemodule_visible($newcm->id, $newcm->visible);
413
414 return $newcm;
415 }
bbd0e548 416}