MDL-70480 mod_assign: Apply proper upgrade savepoint version
[moodle.git] / mod / assign / db / upgrade.php
blob6ff80fa87ca74423f34fdd18210029b17c2b2159
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 // Automatically generated Moodle v3.6.0 release upgrade line.
38 // Put any upgrade step following this.
40 if ($oldversion < 2018120500) {
41 // Define field hidegrader to be added to assign.
42 $table = new xmldb_table('assign');
43 $field = new xmldb_field('hidegrader', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'blindmarking');
45 if (!$dbman->field_exists($table, $field)) {
46 $dbman->add_field($table, $field);
49 // Assignment savepoint reached.
50 upgrade_mod_savepoint(true, 2018120500, 'assign');
53 // Automatically generated Moodle v3.7.0 release upgrade line.
54 // Put any upgrade step following this.
56 // Automatically generated Moodle v3.8.0 release upgrade line.
57 // Put any upgrade step following this.
59 // Automatically generated Moodle v3.9.0 release upgrade line.
60 // Put any upgrade step following this.
62 // Automatically generated Moodle v3.10.0 release upgrade line.
63 // Put any upgrade step following this.
65 // Automatically generated Moodle v3.11.0 release upgrade line.
66 // Put any upgrade step following this.
68 if ($oldversion < 2021051701) {
69 // The most recent assign submission should always have latest = 1, we want to find all records where this is not the case.
70 // Find the records with the maximum timecreated for each assign and user combination where latest is also 0.
71 $sqluser = "SELECT s.id
72 FROM {assign_submission} s
73 WHERE s.timecreated = (
74 SELECT MAX(timecreated) timecreated
75 FROM {assign_submission} sm
76 WHERE s.assignment = sm.assignment
77 AND s.userid = sm.userid
78 AND sm.groupid = 0)
79 AND s.groupid = 0
80 AND s.latest = 0";
81 $idstofixuser = $DB->get_records_sql($sqluser, null);
83 $sqlgroup = "SELECT s.id
84 FROM {assign_submission} s
85 WHERE s.timecreated = (
86 SELECT MAX(timecreated) timecreated
87 FROM {assign_submission} sm
88 WHERE s.assignment = sm.assignment
89 AND s.groupid = sm.groupid
90 AND sm.groupid <> 0)
91 AND s.groupid <> 0
92 AND s.latest = 0";
93 $idstofixgroup = $DB->get_records_sql($sqlgroup, null);
95 $idstofix = array_merge(array_keys($idstofixuser), array_keys($idstofixgroup));
97 if (count($idstofix)) {
98 [$insql, $inparams] = $DB->get_in_or_equal($idstofix);
99 $DB->set_field_select('assign_submission', 'latest', 1, "id $insql", $inparams);
102 // Assignment savepoint reached.
103 upgrade_mod_savepoint(true, 2021051701, 'assign');
105 return true;