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