function OnLoadPageData() { require_once "data/process-manager.class.php"; $this->process = new ProcessManager(); if ($this->process->ReadyToDeleteAll()) { $stats = $this->GetSettings()->GetTable("PlayerMatch"); $sql = "TRUNCATE TABLE {$stats}"; $this->GetDataConnection()->query($sql); } $matches = $this->GetSettings()->GetTable("MatchTeam"); $mt = $this->GetSettings()->GetTable('MatchTeam'); $sql = "SELECT match_id, match_team_id FROM {$matches} ORDER BY match_team_id " . $this->process->GetQueryLimit(); $result = $this->GetDataConnection()->query($sql); require_once "stoolball/player-manager.class.php"; $player_manager = new PlayerManager($this->GetSettings(), $this->GetDataConnection()); require_once 'stoolball/statistics/statistics-manager.class.php'; $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection()); while ($row = $result->fetch()) { $affected_players = $player_manager->ReadPlayersInMatch(array($row->match_id)); # generate player statistics from the data entered if (count($affected_players)) { $statistics_manager->UpdateBattingStatistics($affected_players, array($row->match_team_id)); # get the match_team_id for the bowling that goes with this batting $sql = "SELECT match_team_id FROM {$mt}\n\t\t\t\t\t\tWHERE match_id = (SELECT match_id FROM {$mt} WHERE match_team_id = {$row->match_team_id})\n\t\t\t\t\t\tAND match_team_id != {$row->match_team_id}\n\t\t\t\t\t\tAND team_role IN (" . TeamRole::Home() . ", " . TeamRole::Away() . ")"; $result2 = $this->GetDataConnection()->query($sql); $row2 = $result2->fetch(); $bowling_match_team_id = $row2->match_team_id; $statistics_manager->UpdateFieldingStatistics($affected_players, array($bowling_match_team_id)); $statistics_manager->UpdateBowlingStatistics($affected_players, array($bowling_match_team_id)); $statistics_manager->UpdatePlayerOfTheMatchStatistics($row->match_id); $statistics_manager->DeleteObsoleteStatistics($row->match_id); } $this->process->OneMoreDone(); } }
/** * @access public * @return void * @param int[] $a_ids * @desc Delete from the db the Matches matching the supplied ids. */ public function DeleteMatch($a_ids) { # check parameter if (!is_array($a_ids)) { die('No matches to delete'); } # build query $delete_sql = array(); $s_match = $this->GetSettings()->GetTable('Match'); $s_season_match = $this->GetSettings()->GetTable('SeasonMatch'); $s_mt = $this->GetSettings()->GetTable('MatchTeam'); $batting = $this->GetSettings()->GetTable('Batting'); $bowling = $this->GetSettings()->GetTable('Bowling'); $stats = $this->GetSettings()->GetTable('PlayerMatch'); $s_ids = join(', ', $a_ids); # delete batting and bowling $match_team_ids = array(); $result = $this->GetDataConnection()->query("SELECT match_team_id FROM {$s_mt} WHERE match_id IN ({$s_ids})"); while ($row = $result->fetch()) { $match_team_ids[] = $row->match_team_id; } $result->closeCursor(); if (count($match_team_ids)) { $match_team_ids = join(",", $match_team_ids); $delete_sql[] = "DELETE FROM {$batting} WHERE match_team_id IN ({$match_team_ids})"; $delete_sql[] = "DELETE FROM {$bowling} WHERE match_team_id IN ({$match_team_ids})"; } $this->GetDataConnection()->query("DELETE FROM {$stats} WHERE match_id IN ({$s_ids})"); # delete teams $delete_sql[] = "DELETE FROM {$s_mt} WHERE match_id IN ({$s_ids})"; # delete seasons $delete_sql[] = "DELETE FROM {$s_season_match} WHERE match_id IN ({$s_ids})"; # if this is a tournament, delete the matches $tournament_match_ids = array(); $s_sql = 'SELECT match_id FROM ' . $s_match . ' WHERE tournament_match_id IN (' . $s_ids . ') '; $result = $this->GetDataConnection()->query($s_sql); while ($row = $result->fetch()) { $tournament_match_ids[] = $row->match_id; } $result->closeCursor(); if (count($tournament_match_ids)) { $this->DeleteMatch($tournament_match_ids); } # delete comments thread $delete_sql[] = "DELETE FROM nsa_forum_message WHERE item_id IN ({$s_ids}) AND item_type = " . ContentType::STOOLBALL_MATCH; # delete match(es) $delete_sql[] = "DELETE FROM {$s_match} WHERE match_id IN ({$s_ids});"; # delete from short URL cache require_once 'http/short-url-manager.class.php'; $o_url_manager = new ShortUrlManager($this->GetSettings(), $this->GetDataConnection()); $s_sql = "SELECT short_url FROM {$s_match} WHERE match_id IN ({$s_ids})"; $result = $this->GetDataConnection()->query($s_sql); while ($row = $result->fetch()) { $o_url_manager->Delete($row->short_url); } $result->closeCursor(); unset($o_url_manager); # get players involved in the match before it's deleted, so that player statistics can be updated require_once 'stoolball/player-manager.class.php'; $player_manager = new PlayerManager($this->GetSettings(), $this->GetDataConnection()); $player_ids = $player_manager->ReadPlayersInMatch($a_ids); unset($player_manager); # Run the collected delete commands foreach ($delete_sql as $sql) { $this->LoggedQuery($sql); } # update player stats, removing this match and any players who featured only in this match require_once 'stoolball/statistics/statistics-manager.class.php'; $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection()); if (count($player_ids)) { $statistics_manager->UpdatePlayerStatistics($player_ids); } unset($statistics_manager); return $this->GetDataConnection()->GetAffectedRows(); }