Merge branch 'MDL-35109-available-updates' of git://github.com/mudrd8mz/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
27/** Include locallib.php */
28require_once($CFG->dirroot.'/mod/assign/locallib.php');
29/** Include accesslib.php */
30require_once($CFG->libdir.'/accesslib.php');
31
5b121144
DW
32/**
33 * The maximum amount of time to spend upgrading a single assignment.
34 * This is intentionally generous (5 mins) as the effect of a timeout
35 * for a legitimate upgrade would be quite harsh (roll back code will not run)
36 */
37define('ASSIGN_MAX_UPGRADE_TIME_SECS', 300);
bbd0e548
DW
38
39/**
40 * Class to manage upgrades from mod_assignment to mod_assign
41 *
42 * @package mod_assign
43 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
46class assign_upgrade_manager {
47
48 /**
49 * This function converts all of the base settings for an instance of
50 * the old assignment to the new format. Then it calls each of the plugins
51 * to see if they can help upgrade this assignment.
52 * @param int $oldassignmentid (don't rely on the old assignment type even being installed)
53 * @param string $log This string gets appended to during the conversion process
54 * @return bool true or false
55 */
56 public function upgrade_assignment($oldassignmentid, & $log) {
bbd0e548 57 // steps to upgrade an assignment
bbd0e548
DW
58 global $DB, $CFG, $USER;
59 // steps to upgrade an assignment
60
61 // is the user the admin? admin check goes here
62 if (!is_siteadmin($USER->id)) {
63 return false;
64 }
65
5b121144
DW
66 // should we use a shutdown handler to rollback on timeout?
67 @set_time_limit(ASSIGN_MAX_UPGRADE_TIME_SECS);
68
bbd0e548
DW
69
70 // get the module details
71 $oldmodule = $DB->get_record('modules', array('name'=>'assignment'), '*', MUST_EXIST);
72 $oldcoursemodule = $DB->get_record('course_modules', array('module'=>$oldmodule->id, 'instance'=>$oldassignmentid), '*', MUST_EXIST);
73 $oldcontext = context_module::instance($oldcoursemodule->id);
74
75 // first insert an assign instance to get the id
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;
92 $data->preventlatesubmissions = $oldassignment->preventlate;
94f26900 93 $data->requiresubmissionstatement = 0;
bbd0e548
DW
94
95 $newassignment = new assign(null, null, null);
96
97 if (!$newassignment->add_instance($data, false)) {
98 $log = get_string('couldnotcreatenewassignmentinstance', 'mod_assign');
99 return false;
100 }
101
102 // now create a new coursemodule from the old one
103 $newmodule = $DB->get_record('modules', array('name'=>'assign'), '*', MUST_EXIST);
104 $newcoursemodule = $this->duplicate_course_module($oldcoursemodule, $newmodule->id, $newassignment->get_instance()->id);
105 if (!$newcoursemodule) {
106 $log = get_string('couldnotcreatenewcoursemodule', 'mod_assign');
107 return false;
108 }
109
110 // convert the base database tables (assignment, submission, grade)
111
112 // these are used to store information in case a rollback is required
113 $gradingarea = null;
114 $gradingdefinitions = null;
115 $gradeidmap = array();
116 $completiondone = false;
c45261df 117 $gradesdone = false;
bbd0e548
DW
118
119 // from this point we want to rollback on failure
120 $rollback = false;
121 try {
122 $newassignment->set_context(context_module::instance($newcoursemodule->id));
123
124 // the course module has now been created - time to update the core tables
125
126 // copy intro files
127 $newassignment->copy_area_files_for_upgrade($oldcontext->id, 'mod_assignment', 'intro', 0,
128 $newassignment->get_context()->id, 'mod_assign', 'intro', 0);
129
130
131 // get the plugins to do their bit
132 foreach ($newassignment->get_submission_plugins() as $plugin) {
133 if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
134 $plugin->enable();
135 if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
136 $rollback = true;
137 }
138 }
139 }
140 foreach ($newassignment->get_feedback_plugins() as $plugin) {
141 if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
142 $plugin->enable();
143 if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
144 $rollback = true;
145 }
146 }
147 }
148
149 // see if there is advanced grading upgrades required
150 $gradingarea = $DB->get_record('grading_areas', array('contextid'=>$oldcontext->id, 'areaname'=>'submission'), '*', IGNORE_MISSING);
151 if ($gradingarea) {
152 $DB->update_record('grading_areas', array('id'=>$gradingarea->id, 'contextid'=>$newassignment->get_context()->id, 'component'=>'mod_assign', 'areaname'=>'submissions'));
153 $gradingdefinitions = $DB->get_records('grading_definitions', array('areaid'=>$gradingarea->id));
154 }
155
156 // upgrade completion data
157 $DB->set_field('course_modules_completion', 'coursemoduleid', $newcoursemodule->id, array('coursemoduleid'=>$oldcoursemodule->id));
1f417de8
DW
158 $allcriteria = $DB->get_records('course_completion_criteria', array('moduleinstance'=>$oldcoursemodule->id));
159 foreach ($allcriteria as $criteria) {
160 $criteria->module = 'assign';
161 $criteria->moduleinstance = $newcoursemodule->id;
162 $DB->update_record('course_completion_criteria', $criteria);
163 }
bbd0e548
DW
164 $completiondone = true;
165
166
167 // copy all the submission data (and get plugins to do their bit)
168 $oldsubmissions = $DB->get_records('assignment_submissions', array('assignment'=>$oldassignmentid));
169
170 foreach ($oldsubmissions as $oldsubmission) {
171 $submission = new stdClass();
172 $submission->assignment = $newassignment->get_instance()->id;
173 $submission->userid = $oldsubmission->userid;
174 $submission->timecreated = $oldsubmission->timecreated;
175 $submission->timemodified = $oldsubmission->timemodified;
176 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
177 $submission->id = $DB->insert_record('assign_submission', $submission);
178 if (!$submission->id) {
179 $log .= get_string('couldnotinsertsubmission', 'mod_assign', $submission->userid);
180 $rollback = true;
181 }
182 foreach ($newassignment->get_submission_plugins() as $plugin) {
183 if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
184 if (!$plugin->upgrade($oldcontext, $oldassignment, $oldsubmission, $submission, $log)) {
185 $rollback = true;
186 }
187 }
188 }
189 if ($oldsubmission->timemarked) {
190 // submission has been graded - create a grade record
191 $grade = new stdClass();
192 $grade->assignment = $newassignment->get_instance()->id;
193 $grade->userid = $oldsubmission->userid;
194 $grade->grader = $oldsubmission->teacher;
195 $grade->timemodified = $oldsubmission->timemarked;
196 $grade->timecreated = $oldsubmission->timecreated;
197 // $grade->locked = $oldsubmission->locked;
198 $grade->grade = $oldsubmission->grade;
199 $grade->mailed = $oldsubmission->mailed;
200 $grade->id = $DB->insert_record('assign_grades', $grade);
201 if (!$grade->id) {
202 $log .= get_string('couldnotinsertgrade', 'mod_assign', $grade->userid);
203 $rollback = true;
204 }
205
206 // copy any grading instances
207 if ($gradingarea) {
208
209 $gradeidmap[$grade->id] = $oldsubmission->id;
210
211 foreach ($gradingdefinitions as $definition) {
212 $DB->set_field('grading_instances', 'itemid', $grade->id, array('definitionid'=>$definition->id, 'itemid'=>$oldsubmission->id));
213 }
214
215 }
216 foreach ($newassignment->get_feedback_plugins() as $plugin) {
217 if ($plugin->can_upgrade($oldassignment->assignmenttype, $oldversion)) {
218 if (!$plugin->upgrade($oldcontext, $oldassignment, $oldsubmission, $grade, $log)) {
219 $rollback = true;
220 }
221 }
222 }
223 }
224 }
225
226 $newassignment->update_calendar($newcoursemodule->id);
bbd0e548
DW
227
228 // copy the grades from the old assignment to the new one
1f417de8
DW
229
230 $gradeitem = $DB->get_record('grade_items', array('iteminstance'=>$oldassignment->id, 'itemmodule'=>'assignment'), 'id', IGNORE_MISSING);
231 if ($gradeitem) {
232 $gradeitem->iteminstance = $newassignment->get_instance()->id;
233 $gradeitem->itemmodule = 'assign';
234 $DB->update_record('grade_items', $gradeitem);
235 }
c45261df 236 $gradesdone = true;
bbd0e548
DW
237
238 } catch (Exception $exception) {
239 $rollback = true;
5b121144 240 $log .= get_string('conversionexception', 'mod_assign', $exception->error);
bbd0e548
DW
241 }
242
243 if ($rollback) {
c45261df
DW
244 // roll back the grades changes
245 if ($gradesdone) {
246 // copy the grades from the old assignment to the new one
1f417de8
DW
247 $gradeitem = $DB->get_record('grade_items', array('iteminstance'=>$newassignment->get_instance()->id, 'itemmodule'=>'assign'), 'id', IGNORE_MISSING);
248 if ($gradeitem) {
249 $gradeitem->iteminstance = $oldassignment->id;
250 $gradeitem->itemmodule = 'assignment';
251 $DB->update_record('grade_items', $gradeitem);
252 }
c45261df 253 }
bbd0e548
DW
254 // roll back the completion changes
255 if ($completiondone) {
256 $DB->set_field('course_modules_completion', 'coursemoduleid', $oldcoursemodule->id, array('coursemoduleid'=>$newcoursemodule->id));
1f417de8
DW
257 $allcriteria = $DB->get_records('course_completion_criteria', array('moduleinstance'=>$newcoursemodule->id));
258 foreach ($allcriteria as $criteria) {
259 $criteria->module = 'assignment';
260 $criteria->moduleinstance = $oldcoursemodule->id;
261 $DB->update_record('course_completion_criteria', $criteria);
262 }
bbd0e548
DW
263 }
264 // roll back the advanced grading update
265 if ($gradingarea) {
266 foreach ($gradeidmap as $newgradeid => $oldsubmissionid) {
267 foreach ($gradingdefinitions as $definition) {
268 $DB->set_field('grading_instances', 'itemid', $oldsubmissionid, array('definitionid'=>$definition->id, 'itemid'=>$newgradeid));
269 }
270 }
271 $DB->update_record('grading_areas', array('id'=>$gradingarea->id, 'contextid'=>$oldcontext->id, 'component'=>'mod_assignment', 'areaname'=>'submission'));
272 }
273 $newassignment->delete_instance();
274
275 return false;
276 }
277 // all is well,
278 // delete the old assignment (use object delete)
279 $cm = get_coursemodule_from_id('', $oldcoursemodule->id, $oldcoursemodule->course);
280 if ($cm) {
281 $this->delete_course_module($cm);
282 }
283 rebuild_course_cache($oldcoursemodule->course);
284 return true;
285 }
286
287
288 /**
289 * Create a duplicate course module record so we can create the upgraded
290 * assign module alongside the old assignment module.
291 *
292 * @param stdClass $cm The old course module record
293 * @param int $moduleid The id of the new assign module
294 * @param int $newinstanceid The id of the new instance of the assign module
295 * @return mixed stdClass|bool The new course module record or FALSE
296 */
297 private function duplicate_course_module(stdClass $cm, $moduleid, $newinstanceid) {
298 global $DB, $CFG;
299
300 $newcm = new stdClass();
301 $newcm->course = $cm->course;
302 $newcm->module = $moduleid;
303 $newcm->instance = $newinstanceid;
304 $newcm->visible = $cm->visible;
305 $newcm->section = $cm->section;
306 $newcm->score = $cm->score;
307 $newcm->indent = $cm->indent;
308 $newcm->groupmode = $cm->groupmode;
309 $newcm->groupingid = $cm->groupingid;
310 $newcm->groupmembersonly = $cm->groupmembersonly;
311 $newcm->completion = $cm->completion;
312 $newcm->completiongradeitemnumber = $cm->completiongradeitemnumber;
313 $newcm->completionview = $cm->completionview;
314 $newcm->completionexpected = $cm->completionexpected;
315 if(!empty($CFG->enableavailability)) {
316 $newcm->availablefrom = $cm->availablefrom;
317 $newcm->availableuntil = $cm->availableuntil;
318 $newcm->showavailability = $cm->showavailability;
319 }
320 $newcm->showdescription = $cm->showdescription;
321
322 $newcmid = add_course_module($newcm);
323 $newcm = get_coursemodule_from_id('', $newcmid, $cm->course);
324 if (!$newcm) {
325 return false;
326 }
327 $section = $DB->get_record("course_sections", array("id"=>$newcm->section));
328 if (!$section) {
329 return false;
330 }
331
332 $mod = new stdClass();
333 $mod->course = $newcm->course;
334 $mod->section = $section->section;
335 $mod->coursemodule = $newcm->id;
336 $mod->id = $newcm->id;
337 $newcm->section = add_mod_to_section($mod, $cm);
338
339 // make sure visibility is set correctly (in particular in calendar)
340 // note: allow them to set it even without moodle/course:activityvisibility
341 set_coursemodule_visible($newcm->id, $newcm->visible);
342
343 return $newcm;
344 }
345
346 /**
347 * This function deletes the old assignment course module after
348 * it has been upgraded. This code is adapted from "course/mod.php".
349 *
350 * @param stdClass $cm The course module to delete.
351 * @return bool
352 */
353 private function delete_course_module($cm) {
354 global $CFG, $USER, $DB, $OUTPUT;
355 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
356
357 $coursecontext = context_course::instance($course->id);
358 $modcontext = context_module::instance($cm->id);
359
360 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
361
362 if (file_exists($modlib)) {
363 require_once($modlib);
364 } else {
365 print_error('modulemissingcode', '', '', $modlib);
366 }
367
368 $deleteinstancefunction = $cm->modname."_delete_instance";
369
370 if (!$deleteinstancefunction($cm->instance)) {
371 echo $OUTPUT->notification("Could not delete the $cm->modname (instance)");
372 }
373
374 // remove all module files in case modules forget to do that
375 $fs = get_file_storage();
376 $fs->delete_area_files($modcontext->id);
377
378 if (!delete_course_module($cm->id)) {
379 echo $OUTPUT->notification("Could not delete the $cm->modname (coursemodule)");
380 }
381 if (!delete_mod_from_section($cm->id, $cm->section)) {
382 echo $OUTPUT->notification("Could not delete the $cm->modname from that section");
383 }
384
385 // Trigger a mod_deleted event with information about this module.
386 $eventdata = new stdClass();
387 $eventdata->modulename = $cm->modname;
388 $eventdata->cmid = $cm->id;
389 $eventdata->courseid = $course->id;
390 $eventdata->userid = $USER->id;
391 events_trigger('mod_deleted', $eventdata);
392
393 add_to_log($course->id, 'course', "delete mod",
394 "view.php?id=$cm->course",
395 "$cm->modname $cm->instance", $cm->id);
396
397 return true;
398 }
399
bbd0e548 400}