48157274282355935ab7707240ac1e2a2e8b5b42
[moodle.git] / mod / assign / db / upgrade.php
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/>.
17 /**
18  * Upgrade code for install
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  */
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
31  */
32 function xmldb_assign_upgrade($oldversion) {
33     global $CFG, $DB;
35     $dbman = $DB->get_manager();
37     if ($oldversion < 2014051201) {
39         // Cleanup bad database records where assignid is missing.
41         $DB->delete_records('assign_user_mapping', array('assignment'=>0));
42         // Assign savepoint reached.
43         upgrade_mod_savepoint(true, 2014051201, 'assign');
44     }
46     if ($oldversion < 2014072400) {
48         // Add "latest" column to submissions table to mark the latest attempt.
49         $table = new xmldb_table('assign_submission');
50         $field = new xmldb_field('latest', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'attemptnumber');
52         // Conditionally launch add field latest.
53         if (!$dbman->field_exists($table, $field)) {
54             $dbman->add_field($table, $field);
55         }
57         // Assign savepoint reached.
58         upgrade_mod_savepoint(true, 2014072400, 'assign');
59     }
60     if ($oldversion < 2014072401) {
62          // Define index latestattempt (not unique) to be added to assign_submission.
63         $table = new xmldb_table('assign_submission');
64         $index = new xmldb_index('latestattempt', XMLDB_INDEX_NOTUNIQUE, array('assignment', 'userid', 'groupid', 'latest'));
66         // Conditionally launch add index latestattempt.
67         if (!$dbman->index_exists($table, $index)) {
68             $dbman->add_index($table, $index);
69         }
71         // Assign savepoint reached.
72         upgrade_mod_savepoint(true, 2014072401, 'assign');
73     }
74     if ($oldversion < 2014072405) {
76         // Prevent running this multiple times.
78         $countsql = 'SELECT COUNT(id) FROM {assign_submission} WHERE latest = ?';
80         $count = $DB->count_records_sql($countsql, array(1));
81         if ($count == 0) {
83             // Mark the latest attempt for every submission in mod_assign.
84             $maxattemptsql = 'SELECT assignment, userid, groupid, max(attemptnumber) AS maxattempt
85                                 FROM {assign_submission}
86                             GROUP BY assignment, groupid, userid';
88             $maxattemptidssql = 'SELECT souter.id
89                                    FROM {assign_submission} souter
90                                    JOIN (' . $maxattemptsql . ') sinner
91                                      ON souter.assignment = sinner.assignment
92                                     AND souter.userid = sinner.userid
93                                     AND souter.groupid = sinner.groupid
94                                     AND souter.attemptnumber = sinner.maxattempt';
96             // We need to avoid using "WHERE ... IN(SELECT ...)" clause with MySQL for performance reason.
97             // TODO MDL-29589 Remove this dbfamily exception when implemented.
98             if ($DB->get_dbfamily() === 'mysql') {
99                 $params = array('latest' => 1);
100                 $sql = 'UPDATE {assign_submission}
101                     INNER JOIN (' . $maxattemptidssql . ') souterouter ON souterouter.id = {assign_submission}.id
102                            SET latest = :latest';
103                 $DB->execute($sql, $params);
104             } else {
105                 $select = 'id IN(' . $maxattemptidssql . ')';
106                 $DB->set_field_select('assign_submission', 'latest', 1, $select);
107             }
109             // Look for grade records with no submission record.
110             // This is when a teacher has marked a student before they submitted anything.
111             $records = $DB->get_records_sql('SELECT g.id, g.assignment, g.userid
112                                                FROM {assign_grades} g
113                                           LEFT JOIN {assign_submission} s
114                                                  ON s.assignment = g.assignment
115                                                 AND s.userid = g.userid
116                                               WHERE s.id IS NULL');
117             $submissions = array();
118             foreach ($records as $record) {
119                 $submission = new stdClass();
120                 $submission->assignment = $record->assignment;
121                 $submission->userid = $record->userid;
122                 $submission->status = 'new';
123                 $submission->groupid = 0;
124                 $submission->latest = 1;
125                 $submission->timecreated = time();
126                 $submission->timemodified = time();
127                 array_push($submissions, $submission);
128             }
130             $DB->insert_records('assign_submission', $submissions);
131         }
133         // Assign savepoint reached.
134         upgrade_mod_savepoint(true, 2014072405, 'assign');
135     }
137     // Moodle v2.8.0 release upgrade line.
138     // Put any upgrade step following this.
140     if ($oldversion < 2014122600) {
141         // Delete any entries from the assign_user_flags and assign_user_mapping that are no longer required.
142         if ($DB->get_dbfamily() === 'mysql') {
143             $sql1 = "DELETE {assign_user_flags}
144                        FROM {assign_user_flags}
145                   LEFT JOIN {assign}
146                          ON {assign_user_flags}.assignment = {assign}.id
147                       WHERE {assign}.id IS NULL";
149             $sql2 = "DELETE {assign_user_mapping}
150                        FROM {assign_user_mapping}
151                   LEFT JOIN {assign}
152                          ON {assign_user_mapping}.assignment = {assign}.id
153                       WHERE {assign}.id IS NULL";
154         } else {
155             $sql1 = "DELETE FROM {assign_user_flags}
156                 WHERE NOT EXISTS (
157                           SELECT 'x' FROM {assign}
158                            WHERE {assign_user_flags}.assignment = {assign}.id)";
160             $sql2 = "DELETE FROM {assign_user_mapping}
161                 WHERE NOT EXISTS (
162                           SELECT 'x' FROM {assign}
163                            WHERE {assign_user_mapping}.assignment = {assign}.id)";
164         }
166         $DB->execute($sql1);
167         $DB->execute($sql2);
169         upgrade_mod_savepoint(true, 2014122600, 'assign');
170     }
172     if ($oldversion < 2015022300) {
174         // Define field preventsubmissionnotingroup to be added to assign.
175         $table = new xmldb_table('assign');
176         $field = new xmldb_field('preventsubmissionnotingroup',
177             XMLDB_TYPE_INTEGER,
178             '2',
179             null,
180             XMLDB_NOTNULL,
181             null,
182             '0',
183             'sendstudentnotifications');
185         // Conditionally launch add field preventsubmissionnotingroup.
186         if (!$dbman->field_exists($table, $field)) {
187             $dbman->add_field($table, $field);
188         }
190         // Assign savepoint reached.
191         upgrade_mod_savepoint(true, 2015022300, 'assign');
192     }
194     // Moodle v2.9.0 release upgrade line.
195     // Put any upgrade step following this.
197     // Moodle v3.0.0 release upgrade line.
198     // Put any upgrade step following this.
200     // Moodle v3.1.0 release upgrade line.
201     // Put any upgrade step following this.
203     if ($oldversion < 2016100301) {
205         // Define table assign_overrides to be created.
206         $table = new xmldb_table('assign_overrides');
208         // Adding fields to table assign_overrides.
209         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
210         $table->add_field('assignid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
211         $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
212         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
213         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
214         $table->add_field('allowsubmissionsfromdate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
215         $table->add_field('duedate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
216         $table->add_field('cutoffdate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
218         // Adding keys to table assign_overrides.
219         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
220         $table->add_key('assignid', XMLDB_KEY_FOREIGN, array('assignid'), 'assign', array('id'));
221         $table->add_key('groupid', XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
222         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
224         // Conditionally launch create table for assign_overrides.
225         if (!$dbman->table_exists($table)) {
226             $dbman->create_table($table);
227         }
229         // Assign savepoint reached.
230         upgrade_mod_savepoint(true, 2016100301, 'assign');
231     }
233     // Automatically generated Moodle v3.2.0 release upgrade line.
234     // Put any upgrade step following this.
236     if ($oldversion < 2017021500) {
237         // Fix event types of assign events.
238         $params = [
239             'modulename' => 'assign',
240             'eventtype' => 'close'
241         ];
242         $select = "modulename = :modulename AND eventtype = :eventtype";
243         $DB->set_field_select('event', 'eventtype', 'due', $select, $params);
245         // Assign savepoint reached.
246         upgrade_mod_savepoint(true, 2017021500, 'assign');
247     }
249     return true;