MDL-62478 upgrade: add 3.5.0 separation line to all upgrade scripts
[moodle.git] / mod / assign / db / upgrade.php
blob9908d9982b7d856e05ad1f202494b58341062c9d
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.
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/>.
17 /**
18 * Upgrade code for install
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
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * upgrade this assignment instance - this function could be skipped but it will be needed later
29 * @param int $oldversion The old version of the assign module
30 * @return bool
32 function xmldb_assign_upgrade($oldversion) {
33 global $CFG, $DB;
35 $dbman = $DB->get_manager();
37 if ($oldversion < 2016100301) {
39 // Define table assign_overrides to be created.
40 $table = new xmldb_table('assign_overrides');
42 // Adding fields to table assign_overrides.
43 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
44 $table->add_field('assignid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
45 $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
46 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
47 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
48 $table->add_field('allowsubmissionsfromdate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
49 $table->add_field('duedate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
50 $table->add_field('cutoffdate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
52 // Adding keys to table assign_overrides.
53 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
54 $table->add_key('assignid', XMLDB_KEY_FOREIGN, array('assignid'), 'assign', array('id'));
55 $table->add_key('groupid', XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
56 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
58 // Conditionally launch create table for assign_overrides.
59 if (!$dbman->table_exists($table)) {
60 $dbman->create_table($table);
63 // Assign savepoint reached.
64 upgrade_mod_savepoint(true, 2016100301, 'assign');
67 // Automatically generated Moodle v3.2.0 release upgrade line.
68 // Put any upgrade step following this.
70 if ($oldversion < 2017021500) {
71 // Fix event types of assign events.
72 $params = [
73 'modulename' => 'assign',
74 'eventtype' => 'close'
76 $select = "modulename = :modulename AND eventtype = :eventtype";
77 $DB->set_field_select('event', 'eventtype', 'due', $select, $params);
79 // Delete 'open' events.
80 $params = [
81 'modulename' => 'assign',
82 'eventtype' => 'open'
84 $DB->delete_records('event', $params);
86 // Assign savepoint reached.
87 upgrade_mod_savepoint(true, 2017021500, 'assign');
90 if ($oldversion < 2017031300) {
91 // Add a 'gradingduedate' field to the 'assign' table.
92 $table = new xmldb_table('assign');
93 $field = new xmldb_field('gradingduedate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'cutoffdate');
95 // Conditionally launch add field.
96 if (!$dbman->field_exists($table, $field)) {
97 $dbman->add_field($table, $field);
100 // Assign savepoint reached.
101 upgrade_mod_savepoint(true, 2017031300, 'assign');
104 if ($oldversion < 2017042800) {
105 // Update query to set the grading due date one week after the due date.
106 // Only assign instances with grading due date not set and with a due date of not older than 3 weeks will be updated.
107 $sql = "UPDATE {assign}
108 SET gradingduedate = duedate + :weeksecs
109 WHERE gradingduedate = 0
110 AND duedate > :timelimit";
112 // Calculate the time limit, which is 3 weeks before the current date.
113 $interval = new DateInterval('P3W');
114 $timelimit = new DateTime();
115 $timelimit->sub($interval);
117 // Update query params.
118 $params = [
119 'weeksecs' => WEEKSECS,
120 'timelimit' => $timelimit->getTimestamp()
123 // Execute DB update for assign instances.
124 $DB->execute($sql, $params);
126 // Assign savepoint reached.
127 upgrade_mod_savepoint(true, 2017042800, 'assign');
130 // Automatically generated Moodle v3.3.0 release upgrade line.
131 // Put any upgrade step following this.
132 if ($oldversion < 2017061200) {
133 // Data fix any assign group override event priorities which may have been accidentally nulled due to a bug on the group
134 // overrides edit form.
136 // First, find all assign group override events having null priority (and join their corresponding assign_overrides entry).
137 $sql = "SELECT e.id AS id, o.sortorder AS priority
138 FROM {assign_overrides} o
139 JOIN {event} e ON (e.modulename = 'assign' AND o.assignid = e.instance AND e.groupid = o.groupid)
140 WHERE o.groupid IS NOT NULL AND e.priority IS NULL
141 ORDER BY o.id";
142 $affectedrs = $DB->get_recordset_sql($sql);
144 // Now update the event's priority based on the assign_overrides sortorder we found. This uses similar logic to
145 // assign_refresh_events(), except we've restricted the set of assignments and overrides we're dealing with here.
146 foreach ($affectedrs as $record) {
147 $DB->set_field('event', 'priority', $record->priority, ['id' => $record->id]);
149 $affectedrs->close();
151 // Main savepoint reached.
152 upgrade_mod_savepoint(true, 2017061200, 'assign');
155 if ($oldversion < 2017061205) {
156 require_once($CFG->dirroot.'/mod/assign/upgradelib.php');
157 $brokenassigns = get_assignments_with_rescaled_null_grades();
159 // Set config value.
160 foreach ($brokenassigns as $assign) {
161 set_config('has_rescaled_null_grades_' . $assign, 1, 'assign');
164 // Main savepoint reached.
165 upgrade_mod_savepoint(true, 2017061205, 'assign');
168 // Automatically generated Moodle v3.4.0 release upgrade line.
169 // Put any upgrade step following this.
171 // Automatically generated Moodle v3.5.0 release upgrade line.
172 // Put any upgrade step following this.
174 return true;