From 2ae9e83f7cdca98afc273a103ef674f594860431 Mon Sep 17 00:00:00 2001 From: Vasilis Daloukas Date: Mon, 29 Dec 2014 23:52:11 +0200 Subject: [PATCH] Fix:Problem on deletion of a game and restore a course --- backup/moodle2/restore_game_stepslib.php | 4 ++ lib.php | 87 +++++++++++------------- version.php | 4 +- 3 files changed, 46 insertions(+), 49 deletions(-) diff --git a/backup/moodle2/restore_game_stepslib.php b/backup/moodle2/restore_game_stepslib.php index 08d8f7d..fa976cb 100755 --- a/backup/moodle2/restore_game_stepslib.php +++ b/backup/moodle2/restore_game_stepslib.php @@ -80,6 +80,8 @@ class restore_game_activity_structure_step extends restore_activity_structure_st $data = (object)$data; $data->id = $this->get_new_parentid('game'); + if( $data->id == 0) + return; $DB->insert_record('game_export_html', $data); } @@ -90,6 +92,8 @@ class restore_game_activity_structure_step extends restore_activity_structure_st $data = (object)$data; $data->id = $this->get_new_parentid('game'); + if( $data->id == 0) + return; $DB->insert_record('game_export_javame', $data); } diff --git a/lib.php b/lib.php index 4ad79d2..6a36b61 100755 --- a/lib.php +++ b/lib.php @@ -189,60 +189,53 @@ function game_before_add_or_update(&$game) { function game_delete_instance($gameid) { global $DB; - $result = true; - # Delete any dependent records here # - if( ($recs = $DB->get_records( 'game_attempts', array( 'gameid' => $gameid))) != false){ - $ids = ''; - $count = 0; - $aids = array(); - foreach( $recs as $rec){ - $ids .= ','.$rec->id; - if( ++$count > 10){ - $count = 0; - $aids[] = $ids; - $ids = ''; - } - } - if( $ids != ''){ - $aids[] = $ids; - } + $aids = array(); + if( ($recs = $DB->get_records( 'game_attempts', array( 'gameid' => $gameid))) != false) + { + $ids = ''; + $count = 0; + foreach( $recs as $rec) + { + $ids .= ( $ids == '' ? $rec->id : ','.$rec->id); + if( ++$count > 10) + { + $aids[] = $ids; + $count = 0; + $ids = ''; + } + } + if( $ids != '') + $aids[] = $ids; + } - foreach( $aids as $ids){ - if( $result == false){ - break; - } - $tables = array( 'game_hangman', 'game_cross', 'game_cryptex', 'game_millionaire', 'game_bookquiz', 'game_sudoku', 'game_snakes'); - foreach( $tables as $t){ - $sql = "DELETE FROM {".$t."} WHERE id IN (".substr( $ids, 1).')'; - if (! $DB->execute( $sql)) { - $result = false; - break; - } - } - } + foreach( $aids as $ids) + { + $tables = array( 'game_hangman', 'game_cross', 'game_cryptex', 'game_millionaire', 'game_bookquiz', 'game_sudoku', 'game_snakes'); + foreach( $tables as $t) + { + $sql = "DELETE FROM {".$t."} WHERE id IN (".$ids.')'; + if (! $DB->execute( $sql)) + return false; } - + } + $tables = array( 'game_attempts', 'game_grades', 'game_bookquiz_questions', 'game_queries', 'game_repetitions'); - foreach( $tables as $t){ - if( $result == false){ - break; - } - - if (! $DB->delete_records( $t, array( 'gameid' => $gameid))) { - $result = false; - } - } - - if( $result){ - $tables = array( 'game_export_javame', 'game_export_html', 'game'); - if (!$DB->delete_records( 'game', array( 'id' => $gameid))) { - $result = false; - } + foreach( $tables as $t) + { + if (! $DB->delete_records( $t, array( 'gameid' => $gameid))) + return false; + } + + $tables = array( 'game_export_javame', 'game_export_html', 'game'); + foreach( $tables as $table) + { + if (!$DB->delete_records( $table, array( 'id' => $gameid))) + return false; } - return $result; + return true; } /** diff --git a/version.php b/version.php index 4e8ae9b..1c8d2a3 100755 --- a/version.php +++ b/version.php @@ -22,10 +22,10 @@ if( !isset( $plugin)) $useplugin = 2; $plugin->component = 'mod_game'; // Full name of the plugin (used for diagnostics) -$plugin->version = 2014122801; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2014122901; // 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 = '3.24.28'; +$plugin->release = '3.24.29'; if( $useplugin != 2) $module = $plugin;