2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the upgrade code to upgrade from mod_assignment to 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
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once($CFG->dirroot
.'/mod/assign/locallib.php');
28 require_once($CFG->libdir
.'/accesslib.php');
29 require_once($CFG->dirroot
.'/course/lib.php');
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)
36 define('ASSIGN_MAX_UPGRADE_TIME_SECS', 300);
39 * Class to manage upgrades from mod_assignment to 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
45 class assign_upgrade_manager
{
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
55 public function upgrade_assignment($oldassignmentid, & $log) {
56 global $DB, $CFG, $USER;
57 // Steps to upgrade an assignment.
59 core_php_time_limit
::raise(ASSIGN_MAX_UPGRADE_TIME_SECS
);
61 // Get the module details.
62 $oldmodule = $DB->get_record('modules', array('name'=>'assignment'), '*', MUST_EXIST
);
63 $params = array('module'=>$oldmodule->id
, 'instance'=>$oldassignmentid);
64 $oldcoursemodule = $DB->get_record('course_modules',
68 $oldcontext = context_module
::instance($oldcoursemodule->id
);
69 // We used to check for admin capability, but since Moodle 2.7 this is called
70 // during restore of a mod_assignment module.
71 // Also note that we do not check for any mod_assignment capabilities, because they can
72 // be removed so that users don't add new instances of the broken old thing.
73 if (!has_capability('mod/assign:addinstance', $oldcontext)) {
74 $log = get_string('couldnotcreatenewassignmentinstance', 'mod_assign');
78 // First insert an assign instance to get the id.
79 $oldassignment = $DB->get_record('assignment', array('id'=>$oldassignmentid), '*', MUST_EXIST
);
81 $oldversion = get_config('assignment_' . $oldassignment->assignmenttype
, 'version');
83 $data = new stdClass();
84 $data->course
= $oldassignment->course
;
85 $data->name
= $oldassignment->name
;
86 $data->intro
= $oldassignment->intro
;
87 $data->introformat
= $oldassignment->introformat
;
88 $data->alwaysshowdescription
= 1;
89 $data->sendnotifications
= $oldassignment->emailteachers
;
90 $data->sendlatenotifications
= $oldassignment->emailteachers
;
91 $data->duedate
= $oldassignment->timedue
;
92 $data->allowsubmissionsfromdate
= $oldassignment->timeavailable
;
93 $data->grade
= $oldassignment->grade
;
94 $data->submissiondrafts
= $oldassignment->resubmit
;
95 $data->requiresubmissionstatement
= 0;
96 $data->markingworkflow
= 0;
97 $data->markingallocation
= 0;
98 $data->cutoffdate
= 0;
99 $data->gradingduedate
= 0;
100 // New way to specify no late submissions.
101 if ($oldassignment->preventlate
) {
102 $data->cutoffdate
= $data->duedate
;
104 $data->teamsubmission
= 0;
105 $data->requireallteammemberssubmit
= 0;
106 $data->teamsubmissiongroupingid
= 0;
107 $data->blindmarking
= 0;
108 $data->attemptreopenmethod
= 'none';
109 $data->maxattempts
= ASSIGN_UNLIMITED_ATTEMPTS
;
111 $newassignment = new assign(null, null, null);
113 if (!$newassignment->add_instance($data, false)) {
114 $log = get_string('couldnotcreatenewassignmentinstance', 'mod_assign');
118 // Now create a new coursemodule from the old one.
119 $newmodule = $DB->get_record('modules', array('name'=>'assign'), '*', MUST_EXIST
);
120 $newcoursemodule = $this->duplicate_course_module($oldcoursemodule,
122 $newassignment->get_instance()->id
);
123 if (!$newcoursemodule) {
124 $log = get_string('couldnotcreatenewcoursemodule', 'mod_assign');
128 // Convert the base database tables (assignment, submission, grade).
130 // These are used to store information in case a rollback is required.
132 $gradingdefinitions = null;
133 $gradeidmap = array();
134 $completiondone = false;
137 // From this point we want to rollback on failure.
140 $newassignment->set_context(context_module
::instance($newcoursemodule->id
));
142 // The course module has now been created - time to update the core tables.
145 $newassignment->copy_area_files_for_upgrade($oldcontext->id
, 'mod_assignment', 'intro', 0,
146 $newassignment->get_context()->id
, 'mod_assign', 'intro', 0);
148 // Get the plugins to do their bit.
149 foreach ($newassignment->get_submission_plugins() as $plugin) {
150 if ($plugin->can_upgrade($oldassignment->assignmenttype
, $oldversion)) {
152 if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
159 foreach ($newassignment->get_feedback_plugins() as $plugin) {
160 if ($plugin->can_upgrade($oldassignment->assignmenttype
, $oldversion)) {
162 if (!$plugin->upgrade_settings($oldcontext, $oldassignment, $log)) {
170 // See if there is advanced grading upgrades required.
171 $gradingarea = $DB->get_record('grading_areas',
172 array('contextid'=>$oldcontext->id
, 'areaname'=>'submission'),
176 $params = array('id'=>$gradingarea->id
,
177 'contextid'=>$newassignment->get_context()->id
,
178 'component'=>'mod_assign',
179 'areaname'=>'submissions');
180 $DB->update_record('grading_areas', $params);
181 $gradingdefinitions = $DB->get_records('grading_definitions',
182 array('areaid'=>$gradingarea->id
));
185 // Upgrade availability data.
186 \core_availability\info
::update_dependency_id_across_course(
187 $newcoursemodule->course
, 'course_modules', $oldcoursemodule->id
, $newcoursemodule->id
);
189 // Upgrade completion data.
190 $DB->set_field('course_modules_completion',
192 $newcoursemodule->id
,
193 array('coursemoduleid'=>$oldcoursemodule->id
));
194 $allcriteria = $DB->get_records('course_completion_criteria',
195 array('moduleinstance'=>$oldcoursemodule->id
));
196 foreach ($allcriteria as $criteria) {
197 $criteria->module
= 'assign';
198 $criteria->moduleinstance
= $newcoursemodule->id
;
199 $DB->update_record('course_completion_criteria', $criteria);
201 $completiondone = true;
203 // Migrate log entries so we don't lose them.
204 $logparams = array('cmid' => $oldcoursemodule->id
, 'course' => $oldcoursemodule->course
);
205 $DB->set_field('log', 'module', 'assign', $logparams);
206 $DB->set_field('log', 'cmid', $newcoursemodule->id
, $logparams);
208 // Copy all the submission data (and get plugins to do their bit).
209 $oldsubmissions = $DB->get_records('assignment_submissions',
210 array('assignment'=>$oldassignmentid));
212 foreach ($oldsubmissions as $oldsubmission) {
213 $submission = new stdClass();
214 $submission->assignment
= $newassignment->get_instance()->id
;
215 $submission->userid
= $oldsubmission->userid
;
216 $submission->timecreated
= $oldsubmission->timecreated
;
217 $submission->timemodified
= $oldsubmission->timemodified
;
218 $submission->status
= ASSIGN_SUBMISSION_STATUS_SUBMITTED
;
219 // Because in mod_assignment there could only be one submission per student, it is always the latest.
220 $submission->latest
= 1;
221 $submission->id
= $DB->insert_record('assign_submission', $submission);
222 if (!$submission->id
) {
223 $log .= get_string('couldnotinsertsubmission', 'mod_assign', $submission->userid
);
226 foreach ($newassignment->get_submission_plugins() as $plugin) {
227 if ($plugin->can_upgrade($oldassignment->assignmenttype
, $oldversion)) {
228 if (!$plugin->upgrade($oldcontext,
237 if ($oldsubmission->timemarked
) {
238 // Submission has been graded - create a grade record.
239 $grade = new stdClass();
240 $grade->assignment
= $newassignment->get_instance()->id
;
241 $grade->userid
= $oldsubmission->userid
;
242 $grade->grader
= $oldsubmission->teacher
;
243 $grade->timemodified
= $oldsubmission->timemarked
;
244 $grade->timecreated
= $oldsubmission->timecreated
;
245 $grade->grade
= $oldsubmission->grade
;
246 if ($oldsubmission->mailed
) {
247 // The mailed flag goes in the flags table.
248 $flags = new stdClass();
249 $flags->userid
= $oldsubmission->userid
;
250 $flags->assignment
= $newassignment->get_instance()->id
;
252 $DB->insert_record('assign_user_flags', $flags);
254 $grade->id
= $DB->insert_record('assign_grades', $grade);
256 $log .= get_string('couldnotinsertgrade', 'mod_assign', $grade->userid
);
260 // Copy any grading instances.
263 $gradeidmap[$grade->id
] = $oldsubmission->id
;
265 foreach ($gradingdefinitions as $definition) {
266 $params = array('definitionid'=>$definition->id
,
267 'itemid'=>$oldsubmission->id
);
268 $DB->set_field('grading_instances', 'itemid', $grade->id
, $params);
272 foreach ($newassignment->get_feedback_plugins() as $plugin) {
273 if ($plugin->can_upgrade($oldassignment->assignmenttype
, $oldversion)) {
274 if (!$plugin->upgrade($oldcontext,
286 $newassignment->update_calendar($newcoursemodule->id
);
288 // Reassociate grade_items from the old assignment instance to the new assign instance.
289 // This includes outcome linked grade_items.
290 $params = array('assign', $newassignment->get_instance()->id
, 'assignment', $oldassignment->id
);
291 $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
292 $DB->execute($sql, $params);
294 // Create a mapping record to map urls from the old to the new assignment.
295 $mapping = new stdClass();
296 $mapping->oldcmid
= $oldcoursemodule->id
;
297 $mapping->oldinstance
= $oldassignment->id
;
298 $mapping->newcmid
= $newcoursemodule->id
;
299 $mapping->newinstance
= $newassignment->get_instance()->id
;
300 $mapping->timecreated
= time();
301 $DB->insert_record('assignment_upgrade', $mapping);
305 } catch (Exception
$exception) {
307 $log .= get_string('conversionexception', 'mod_assign', $exception->getMessage());
311 // Roll back the grades changes.
313 // Reassociate grade_items from the new assign instance to the old assignment instance.
314 $params = array('assignment', $oldassignment->id
, 'assign', $newassignment->get_instance()->id
);
315 $sql = 'UPDATE {grade_items} SET itemmodule = ?, iteminstance = ? WHERE itemmodule = ? AND iteminstance = ?';
316 $DB->execute($sql, $params);
318 // Roll back the completion changes.
319 if ($completiondone) {
320 $DB->set_field('course_modules_completion',
322 $oldcoursemodule->id
,
323 array('coursemoduleid'=>$newcoursemodule->id
));
325 $allcriteria = $DB->get_records('course_completion_criteria',
326 array('moduleinstance'=>$newcoursemodule->id
));
327 foreach ($allcriteria as $criteria) {
328 $criteria->module
= 'assignment';
329 $criteria->moduleinstance
= $oldcoursemodule->id
;
330 $DB->update_record('course_completion_criteria', $criteria);
333 // Roll back the log changes.
334 $logparams = array('cmid' => $newcoursemodule->id
, 'course' => $newcoursemodule->course
);
335 $DB->set_field('log', 'module', 'assignment', $logparams);
336 $DB->set_field('log', 'cmid', $oldcoursemodule->id
, $logparams);
337 // Roll back the advanced grading update.
339 foreach ($gradeidmap as $newgradeid => $oldsubmissionid) {
340 foreach ($gradingdefinitions as $definition) {
341 $DB->set_field('grading_instances',
344 array('definitionid'=>$definition->id
, 'itemid'=>$newgradeid));
347 $params = array('id'=>$gradingarea->id
,
348 'contextid'=>$oldcontext->id
,
349 'component'=>'mod_assignment',
350 'areaname'=>'submission');
351 $DB->update_record('grading_areas', $params);
353 $newassignment->delete_instance();
357 // Delete the old assignment (use object delete).
358 $cm = get_coursemodule_from_id('', $oldcoursemodule->id
, $oldcoursemodule->course
);
360 course_delete_module($cm->id
);
362 rebuild_course_cache($oldcoursemodule->course
);
368 * Create a duplicate course module record so we can create the upgraded
369 * assign module alongside the old assignment module.
371 * @param stdClass $cm The old course module record
372 * @param int $moduleid The id of the new assign module
373 * @param int $newinstanceid The id of the new instance of the assign module
374 * @return mixed stdClass|bool The new course module record or FALSE
376 private function duplicate_course_module(stdClass
$cm, $moduleid, $newinstanceid) {
379 $newcm = new stdClass();
380 $newcm->course
= $cm->course
;
381 $newcm->module
= $moduleid;
382 $newcm->instance
= $newinstanceid;
383 $newcm->visible
= $cm->visible
;
384 $newcm->section
= $cm->section
;
385 $newcm->score
= $cm->score
;
386 $newcm->indent
= $cm->indent
;
387 $newcm->groupmode
= $cm->groupmode
;
388 $newcm->groupingid
= $cm->groupingid
;
389 $newcm->completion
= $cm->completion
;
390 $newcm->completiongradeitemnumber
= $cm->completiongradeitemnumber
;
391 $newcm->completionview
= $cm->completionview
;
392 $newcm->completionexpected
= $cm->completionexpected
;
393 if (!empty($CFG->enableavailability
)) {
394 $newcm->availability
= $cm->availability
;
396 $newcm->showdescription
= $cm->showdescription
;
398 $newcmid = add_course_module($newcm);
399 $newcm = get_coursemodule_from_id('', $newcmid, $cm->course
);
403 $section = $DB->get_record("course_sections", array("id"=>$newcm->section
));
408 $newcm->section
= course_add_cm_to_section($newcm->course
, $newcm->id
, $section->section
, $cm->id
);
410 // Make sure visibility is set correctly (in particular in calendar).
411 // Note: Allow them to set it even without moodle/course:activityvisibility.
412 set_coursemodule_visible($newcm->id
, $newcm->visible
);