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();
     }
 }
 /**
  * Creates a dropdown list to select either "don't know" or one of the two playing teams
  */
 private function SelectOneOfTwoTeams($list_id, Match $match, $select_home_if, $select_away_if)
 {
     $list = new XhtmlSelect($list_id);
     $list->AddControl(new XhtmlOption("Don't know", ''));
     $list->AddControl(new XhtmlOption(!is_null($match->GetHomeTeam()) ? $match->GetHomeTeam()->GetName() : 'Home team', TeamRole::Home(), $select_home_if));
     $list->AddControl(new XhtmlOption(!is_null($match->GetAwayTeam()) ? $match->GetAwayTeam()->GetName() : 'Away team', TeamRole::Away(), $select_away_if));
     return $list;
 }
 /**
  * @return void
  * @param Match $match
  * @param bool $is_first_innings
  * @desc Saves the batting and bowling scorecards for one innings
  */
 public function SaveScorecard(Match $match, $is_first_innings, ISearchIndexProvider $search)
 {
     # To add a scorecard there must always already be a match to update
     if (!$match->GetId()) {
         return;
     }
     # This isn't for tournaments
     if ($match->GetMatchType() == MatchType::TOURNAMENT) {
         return;
     }
     # Get tables
     $batting_table = $this->GetSettings()->GetTable("Batting");
     $bowling_table = $this->GetSettings()->GetTable("Bowling");
     $match_table = $this->GetSettings()->GetTable('Match');
     $mt = $this->GetSettings()->GetTable('MatchTeam');
     # Is this scorecard for the home or the away innings?
     $sql = "SELECT home_bat_first FROM {$match_table} WHERE match_id = " . Sql::ProtectNumeric($match->GetId());
     $result = $this->GetDataConnection()->query($sql);
     $row = $result->fetch();
     if (is_null($row->home_bat_first) or (bool) $row->home_bat_first === true) {
         $is_home_innings = $is_first_innings;
     } else {
         $is_home_innings = !$is_first_innings;
     }
     $result->closeCursor();
     # Prepare data for query
     if ($is_home_innings) {
         $bowling_team_id = $match->GetAwayTeamId();
         $batting_team_id = $match->GetHomeTeamId();
         $team_bowling = $match->Result()->AwayOvers();
         $team_batting = $match->Result()->HomeBatting();
     } else {
         $bowling_team_id = $match->GetHomeTeamId();
         $batting_team_id = $match->GetAwayTeamId();
         $team_bowling = $match->Result()->HomeOvers();
         $team_batting = $match->Result()->AwayBatting();
     }
     # Find the match_team_id for the bowling
     $sql = "SELECT match_team_id FROM {$mt}\r\n\t\t\t\tWHERE match_id " . Sql::ProtectNumeric($match->GetId(), false, true) . "\r\n\t\t\t\tAND team_id " . Sql::ProtectNumeric($bowling_team_id, false, true) . "\r\n\t\t\t\tAND team_role = " . ($is_home_innings ? TeamRole::Away() : TeamRole::Home());
     $result = $this->GetDataConnection()->query($sql);
     $row = $result->fetch();
     $bowling_match_team_id = $row->match_team_id;
     $result->closeCursor();
     # Find the match_team_id for the batting
     $sql = "SELECT match_team_id FROM {$mt}\r\n\t\t\t\tWHERE match_id " . Sql::ProtectNumeric($match->GetId(), false, true) . "\r\n\t\t\t\tAND team_id " . Sql::ProtectNumeric($batting_team_id, false, true) . "\r\n\t\t\t\tAND team_role = " . ($is_home_innings ? TeamRole::Home() : TeamRole::Away());
     $result = $this->GetDataConnection()->query($sql);
     $row = $result->fetch();
     $batting_match_team_id = $row->match_team_id;
     $result->closeCursor();
     $affected_players = $this->SaveBattingScorecard($match, $is_home_innings, $batting_match_team_id, $team_batting);
     $affected_bowlers = $this->SaveBowlingScorecard($match, $is_home_innings, $batting_match_team_id, $bowling_match_team_id, $team_bowling);
     $affected_players = array_merge($affected_players, $affected_bowlers);
     $affected_players = array_unique($affected_players);
     if (count($affected_players)) {
         require_once 'stoolball/statistics/statistics-manager.class.php';
         $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
         # generate player statistics from the data entered
         $statistics_manager->UpdateBattingStatistics($affected_players, array($batting_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->DeleteObsoleteStatistics($match->GetId());
         # update overall stats for players
         $statistics_manager->UpdatePlayerStatistics($affected_players);
         unset($statistics_manager);
         # update search engine
         require_once "stoolball/player-manager.class.php";
         require_once "search/player-search-adapter.class.php";
         $player_manager = new PlayerManager($this->GetSettings(), $this->GetDataConnection());
         foreach ($affected_players as $player_id) {
             $player_manager->ReadPlayerById($player_id);
             $player = $player_manager->GetFirst();
             $search->DeleteFromIndexById("player" . $player_id);
             if ($player instanceof Player) {
                 $adapter = new PlayerSearchAdapter($player);
                 $search->Index($adapter->GetSearchableItem());
             }
         }
         $search->CommitChanges();
         unset($player_manager);
     }
 }
 /**
  * @return 1 for a win, 0 for a tie, -1 for a loss and null for any other result
  */
 private function DidThePlayerWinTheMatch($match_result, $team_role)
 {
     $won_match = null;
     if ($match_result == MatchResult::AWAY_WIN) {
         $won_match = $team_role == TeamRole::Away() ? 1 : -1;
     } else {
         if ($match_result == MatchResult::TIE) {
             $won_match = 0;
         } else {
             if ($match_result == MatchResult::HOME_WIN) {
                 $won_match = $team_role == TeamRole::Home() ? 1 : -1;
             }
         }
     }
     return $won_match;
 }