From c8f99fa30544e4f5b77521bf06f7f0e22068eb36 Mon Sep 17 00:00:00 2001 From: bdaloukas Date: Tue, 20 Jun 2017 17:30:32 +0300 Subject: [PATCH] Change name of game_snakes_databases.cols to usedcols --- CHANGES.md | 3 +++ db/upgrade.php | 44 ++++++++++++++++++++++++++++++++------------ version.php | 2 +- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2335f98..7f0a364 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +Changes in version 2017-06-20 (20170620) +- Fix conversation of columns (cols,rows) from string to int + Changes in version 2017-06-19 (20170619) - Change field name game_snakes_database.cols => usedcols - Change field name game_snakes_database.rows => usedrows diff --git a/db/upgrade.php b/db/upgrade.php index f24bccb..244083d 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -41,6 +41,29 @@ defined('MOODLE_INTERNAL') || die(); + +/** + * Copies a string field to int field + * + * @param int $oldversion + */ +function mpgame_db_copy_string_to_int( $table, $from, $to) { + global $CFG, $DB; + + $sql = "SELECT id,$from,$to FROM {$CFG->prefix}$table"; + $recs = $DB->get_records_sql( $sql); + foreach ($recs as $rec) { + $val = intval( $rec->$from); + if ($rec->$to == $val) { + continue; + } + $updrec = new stdClass; + $updrec->id = $rec->id; + $updrec->$to = $val; + $DB->update_record( $table, $updrec); + } +} + /** * Upgrades database * @@ -1556,7 +1579,7 @@ function xmldb_game_upgrade($oldversion) { $dbman->change_field_type($table, $field); } - if ($oldversion < ($ver = 2017061604)) { + if ($oldversion < ($ver = 2017062002)) { $table = new xmldb_table('game_cross'); $field = new xmldb_field('usedrows', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'id'); if (!$dbman->field_exists($table, $field)) { @@ -1565,15 +1588,14 @@ function xmldb_game_upgrade($oldversion) { $field = new xmldb_field('rows', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'id'); if ($dbman->field_exists($table, $field)) { - $sql = "UPDATE {$CFG->prefix}game_cross SET usedrows=rows"; - $DB->execute( $sql); + mpgame_db_copy_string_to_int( 'game_cross', 'rows', 'usedrows'); $dbman->drop_field($table, $field); } else { $dbman->add_field($table, $field); } } - if ($oldversion < ($ver = 2017061604)) { + if ($oldversion < ($ver = 2017062002)) { $table = new xmldb_table('game_cross'); $field = new xmldb_field('usedcols', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'id'); if (!$dbman->field_exists($table, $field)) { @@ -1582,15 +1604,14 @@ function xmldb_game_upgrade($oldversion) { $field = new xmldb_field('cols', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'id'); if ($dbman->field_exists($table, $field)) { - $sql = "UPDATE {$CFG->prefix}game_cross SET usedcols=cols"; - $DB->execute( $sql); + mpgame_db_copy_string_to_int( 'game_cross', 'cols', 'usedcols'); $dbman->drop_field($table, $field); } else { $dbman->add_field($table, $field); } } - if ($oldversion < ($ver = 2017061901)) { + if ($oldversion < ($ver = 2017062002)) { $table = new xmldb_table('game_snakes_database'); $field = new xmldb_field('usedrows', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'id'); if (!$dbman->field_exists($table, $field)) { @@ -1599,15 +1620,14 @@ function xmldb_game_upgrade($oldversion) { $field = new xmldb_field('rows', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'id'); if ($dbman->field_exists($table, $field)) { - $sql = "UPDATE {$CFG->prefix}game_snakes_database SET usedrows=rows"; - $DB->execute( $sql); + mpgame_db_copy_string_to_int( 'game_snakes_database', 'rows', 'usedrows'); $dbman->drop_field($table, $field); } else { $dbman->add_field($table, $field); } } - if ($oldversion < ($ver = 2017061901)) { + if ($oldversion < ($ver = 2017062002)) { $table = new xmldb_table('game_snakes_database'); $field = new xmldb_field('usedcols', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'id'); if (!$dbman->field_exists($table, $field)) { @@ -1616,8 +1636,7 @@ function xmldb_game_upgrade($oldversion) { $field = new xmldb_field('cols', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0', 'id'); if ($dbman->field_exists($table, $field)) { - $sql = "UPDATE {$CFG->prefix}game_snakes_database SET usedcols=cols"; - $DB->execute( $sql); + mpgame_db_copy_string_to_int( 'game_snakes_database', 'cols', 'usedcols'); $dbman->drop_field($table, $field); } else { $dbman->add_field($table, $field); @@ -1626,3 +1645,4 @@ function xmldb_game_upgrade($oldversion) { return true; } + diff --git a/version.php b/version.php index 6d0ce40..6b37fe8 100644 --- a/version.php +++ b/version.php @@ -35,7 +35,7 @@ if (!isset( $plugin)) { } $plugin->component = 'mod_game'; // Full name of the plugin (used for diagnostics). -$plugin->version = 2017061901; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2017062002; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2010112400; // Requires Moodle 2.0. $plugin->cron = 0; // Period for cron to check this module (secs). $plugin->release = '2017-06-19';