diff --git a/db/install.xml b/db/install.xml
index b310deb..482b8e0 100644
--- a/db/install.xml
+++ b/db/install.xml
@@ -127,7 +127,7 @@
-
+
diff --git a/db/upgrade.php b/db/upgrade.php
index 9454dcc..0917366 100644
--- a/db/upgrade.php
+++ b/db/upgrade.php
@@ -341,35 +341,6 @@ function xmldb_attendance_upgrade($oldversion=0) {
upgrade_mod_savepoint(true, 2017060900, 'attendance');
}
- // Add new warning table.
- if ($oldversion < 2017061600) {
-
- // Define table attendance_warning to be created.
- $table = new xmldb_table('attendance_warning');
-
- // Adding fields to table attendance_warning.
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('idnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
- $table->add_field('warningpercent', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
- $table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
- $table->add_field('emailuser', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
- $table->add_field('emailsubject', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
- $table->add_field('emailcontent', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
- $table->add_field('emailcontentformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
- $table->add_field('thirdpartyemails', XMLDB_TYPE_TEXT, null, null, null, null, null);
-
- // Adding keys to table attendance_warning.
- $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
-
- // Conditionally launch create table for attendance_warning.
- if (!$dbman->table_exists($table)) {
- $dbman->create_table($table);
- }
-
- // Attendance savepoint reached.
- upgrade_mod_savepoint(true, 2017061600, 'attendance');
- }
-
if ($oldversion < 2017062000) {
// Define table attendance_warning_done to be created.
@@ -396,17 +367,45 @@ function xmldb_attendance_upgrade($oldversion=0) {
upgrade_mod_savepoint(true, 2017062000, 'attendance');
}
- if ($oldversion < 2017071301) {
- // Fix key.
+ if ($oldversion < 2017071305) {
+
+ // Define table attendance_warning to be created.
$table = new xmldb_table('attendance_warning');
- if ($table->getkey('level_id')) {
- $table->deleteKey('level_id');
- }
- $key = new xmldb_key('level_id', XMLDB_KEY_UNIQUE, array('idnumber, warningpercent, warnafter'));
- $dbman->add_key($table, $key);
+ if (!$dbman->table_exists($table)) {
+ // Adding fields to table attendance_warning.
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('idnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('warningpercent', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('emailuser', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('emailsubject', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('emailcontent', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
+ $table->add_field('emailcontentformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('thirdpartyemails', XMLDB_TYPE_TEXT, null, null, null, null, null);
+
+ // Adding keys to table attendance_warning.
+ $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+ $table->add_key('level_id', XMLDB_KEY_UNIQUE, array('idnumber, warningpercent, warnafter'));
+
+ // Conditionally launch create table for attendance_warning.
+ $dbman->create_table($table);
+
+ } else {
+ // Key definition is probably incorrect so fix it - drop_key dml function doesn't seem to work.
+ $indexes = $DB->get_indexes('attendance_warning');
+ foreach ($indexes as $name => $index) {
+ if ($DB->get_dbfamily() === 'mysql') {
+ $DB->execute("ALTER TABLE {attendance_warning} DROP INDEX ". $name);
+ } else {
+ $DB->execute("DROP INDEX ". $name);
+ }
+ }
+ $index = new xmldb_key('level_id', XMLDB_KEY_UNIQUE, array('idnumber, warningpercent', 'warnafter'));
+ $dbman->add_key($table, $index);
+ }
// Attendance savepoint reached.
- upgrade_mod_savepoint(true, 2017071301, 'attendance');
+ upgrade_mod_savepoint(true, 2017071305, 'attendance');
}
return $result;
diff --git a/version.php b/version.php
index 83f3064..cc73377 100644
--- a/version.php
+++ b/version.php
@@ -23,7 +23,7 @@
*/
defined('MOODLE_INTERNAL') || die();
-$plugin->version = 2017071301;
+$plugin->version = 2017071305;
$plugin->requires = 2017042100;
$plugin->release = '3.3.9';
$plugin->maturity = MATURITY_ALPHA;