Beispiel #1
0
 function stats()
 {
     $id = $this->_arg('game');
     if (!$id) {
         $this->Session->setFlash(sprintf(__('Invalid %s', true), __('game', true)), 'default', array('class' => 'info'));
         $this->redirect('/');
     }
     $team_id = $this->_arg('team');
     if ($team_id) {
         $stat_conditions = array('Stat.team_id' => $team_id);
     } else {
         $stat_conditions = array();
     }
     $this->Game->contain(array('Division' => array('League' => array('StatType' => array('conditions' => array('StatType.type' => Configure::read('stat_types.game'))))), 'HomeTeam', 'AwayTeam', 'GameSlot' => array('Field' => 'Facility'), 'ScoreEntry', 'Stat' => array('conditions' => $stat_conditions)));
     $game = $this->Game->read(null, $id);
     if (!$game) {
         $this->Session->setFlash(sprintf(__('Invalid %s', true), __('game', true)), 'default', array('class' => 'info'));
         $this->redirect('/');
     }
     if (!League::hasStats($game['Division']['League'])) {
         $this->Session->setFlash(__('This league does not have stat tracking enabled.', true), 'default', array('class' => 'info'));
         $this->redirect(array('action' => 'view', 'game' => $id));
     }
     if ("{$game['GameSlot']['game_date']} {$game['GameSlot']['game_start']}" > date('Y-m-d H:i:s')) {
         $this->Session->setFlash(__('This game has not yet started.', true), 'default', array('class' => 'info'));
         $this->redirect(array('action' => 'view', 'game' => $id));
     }
     if (empty($game['Stat'])) {
         $this->Session->setFlash(__('No stats have been entered for this game.', true), 'default', array('class' => 'info'));
         // Redirect coordinators to the stats entry page with whatever parameters were used here
         if (in_array($game['Division']['id'], $this->UserCache->read('DivisionIDs'))) {
             $this->redirect(array('action' => 'submit_stats', 'game' => $id, 'team' => $team_id));
         }
         // If there was no team ID given, check if one of the two teams is captained by the current user
         if (!$team_id) {
             $teams = array_intersect(array($game['Game']['home_team'], $game['Game']['away_team']), $this->UserCache->read('OwnedTeamIDs'));
             $team_id = array_pop($teams);
         }
         // If we have a team ID and we're a captain of that team, go to the stats entry page
         if ($team_id && in_array($team_id, $this->UserCache->read('OwnedTeamIDs'))) {
             $this->redirect(array('action' => 'submit_stats', 'game' => $id, 'team' => $team_id));
         }
         $this->redirect(array('action' => 'view', 'game' => $id));
     }
     $this->Configuration->loadAffiliate($game['Division']['League']['affiliate_id']);
     $sport_obj = $this->_getComponent('Sport', $game['Division']['League']['sport'], $this);
     // Team rosters may have changed since the game was played, so use the list of people with stats instead
     foreach (array('HomeTeam', 'AwayTeam') as $key) {
         $people = array_unique(Set::extract("/Stat[team_id={$game[$key]['id']}]/person_id", $game));
         $game[$key]['Person'] = $this->Game->HomeTeam->Person->find('all', array('contain' => array(), 'conditions' => array('Person.id' => $people)));
         usort($game[$key]['Person'], array('Person', 'comparePerson'));
     }
     if ($game['Game']['home_team'] == $team_id || $team_id === null) {
         $team = $game['HomeTeam'];
         $opponent = $game['AwayTeam'];
     } else {
         if ($game['Game']['away_team'] == $team_id) {
             $team = $game['AwayTeam'];
             $opponent = $game['HomeTeam'];
         } else {
             $this->Session->setFlash(__('That team is not playing in this game.', true), 'default', array('class' => 'info'));
             $this->redirect(array('action' => 'view', 'game' => $id));
         }
     }
     $this->set(compact('game', 'team_id', 'team', 'opponent', 'sport_obj'));
     if ($this->params['url']['ext'] == 'csv') {
         $this->set('download_file_name', "Stats - Game {$game['Game']['id']}");
         Configure::write('debug', 0);
     }
 }
 function stats()
 {
     if (!ini_get('safe_mode')) {
         set_time_limit(180);
     }
     $id = $this->_arg('division');
     if (!$id) {
         $this->Session->setFlash(sprintf(__('Invalid %s', true), __('division', true)), 'default', array('class' => 'info'));
         $this->redirect(array('controller' => 'leagues', 'action' => 'index'));
     }
     $contain = array('League' => array('StatType' => array('conditions' => array('StatType.type' => Configure::read('stat_types.team')))), 'Day', 'Team');
     $this->Division->contain($contain);
     $division = $this->Division->read(null, $id);
     if (!$division) {
         $this->Session->setFlash(sprintf(__('Invalid %s', true), __('division', true)), 'default', array('class' => 'info'));
         $this->redirect(array('controller' => 'leagues', 'action' => 'index'));
     }
     if (!League::hasStats($division['League'])) {
         $this->Session->setFlash(__('This league does not have stat tracking enabled.', true), 'default', array('class' => 'info'));
         $this->redirect(array('action' => 'view', 'division' => $id));
     }
     $this->Configuration->loadAffiliate($division['League']['affiliate_id']);
     Configure::load("sport/{$division['League']['sport']}");
     $sport_obj = $this->_getComponent('Sport', $division['League']['sport'], $this);
     // Hopefully, everything we need is already cached
     $cache_key = 'division/' . intval($id) . '/stats';
     $cached = Cache::read($cache_key, 'long_term');
     if ($cached) {
         $stats = $cached;
     }
     if (!empty($stats)) {
         $division += $stats;
     } else {
         // Calculate some stats.
         $teams = Set::extract('/Team/id', $division);
         $stats = $this->Division->Team->Stat->find('all', array('conditions' => array('team_id' => $teams), 'contain' => array()));
         $sport_obj->_init_stats($stats);
         $division['Person'] = $this->Division->Team->TeamsPerson->find('all', array('contain' => array('Person', 'Team'), 'conditions' => array('TeamsPerson.team_id' => $teams, 'TeamsPerson.role' => Configure::read('extended_playing_roster_roles'))));
         usort($division['Person'], array('Person', 'comparePerson'));
         AppModel::_reindexOuter($division['Person'], 'Person', 'id');
         foreach ($division['League']['StatType'] as $stat_type) {
             switch ($stat_type['type']) {
                 case 'season_total':
                     $sport_obj->_season_total($stat_type, $stats);
                     break;
                 case 'season_avg':
                     $sport_obj->_season_avg($stat_type, $stats);
                     break;
                 case 'season_calc':
                     $func = "{$stat_type['handler']}_season";
                     if (method_exists($sport_obj, $func)) {
                         $sport_obj->{$func}($stat_type, $stats);
                     } else {
                         trigger_error("Season stat handler {$stat_type['handler']} was not found in the {$stat_type['sport']} component!", E_USER_ERROR);
                     }
                     break;
             }
         }
         if (!empty($stats['Calculated'])) {
             $division['Calculated'] = $stats['Calculated'];
         } else {
             $division['Calculated'] = array();
         }
         Cache::write($cache_key, array('Person' => $division['Person'], 'Calculated' => $division['Calculated']), 'long_term');
     }
     $this->set(compact('division', 'sport_obj'));
     $this->set('is_coordinator', in_array($id, $this->UserCache->read('DivisionIDs')));
     if ($this->params['url']['ext'] == 'csv') {
         $this->set('download_file_name', "Stats - {$division['Division']['name']}");
         Configure::write('debug', 0);
     }
 }
Beispiel #3
0
 function stat_sheet()
 {
     $id = $this->_arg('team');
     if (!$id) {
         $this->Session->setFlash(sprintf(__('Invalid %s', true), __('team', true)), 'default', array('class' => 'info'));
         $this->redirect(array('action' => 'index'));
     }
     $contain = array('Division' => array('League' => array('StatType' => array('conditions' => array('StatType.type' => 'entered')))), 'Person');
     $this->Team->contain($contain);
     $team = $this->Team->read(null, $id);
     if (!$team) {
         $this->Session->setFlash(sprintf(__('Invalid %s', true), __('team', true)), 'default', array('class' => 'info'));
         $this->redirect(array('action' => 'index'));
     }
     if (!League::hasStats($team['Division']['League'])) {
         $this->Session->setFlash(__('This league does not have stat tracking enabled.', true), 'default', array('class' => 'info'));
         $this->redirect(array('action' => 'view', 'team' => $id));
     }
     if (Configure::read('feature.pdfize') && isset($this->Pdf)) {
         $this->Pdf->actionsToPdf = array($this->action);
     }
     Configure::load("sport/{$team['Division']['League']['sport']}");
     $this->set(compact('team'));
 }
Beispiel #4
0
 /**
  * Add all the links for a division to the menu.
  */
 function _addDivisionMenuItems($division, $league, $id = null, $name = null)
 {
     Configure::load("sport/{$league['sport']}");
     if ($id) {
         $path = array(__('Leagues', true), $name);
     } else {
         $path = array(__('Leagues', true));
     }
     if (!array_key_exists('league_name', $division)) {
         Division::_addNames($division, $league);
     }
     $is_coordinator = in_array($division['id'], $this->UserCache->read('DivisionIDs'));
     $is_manager = $this->is_manager && in_array($league['affiliate_id'], $this->UserCache->read('ManagedAffiliateIDs'));
     if (array_key_exists('Division', $league)) {
         $division_count = count($league['Division']);
     } else {
         $division_count = $this->requestAction(array('controller' => 'leagues', 'action' => 'division_count'), array('named' => array('league' => $league['id'])));
     }
     if ($division_count == 1) {
         $this->_addMenuItem($division['league_name'], array('controller' => 'leagues', 'action' => 'view', 'league' => $league['id']), $path);
         $path[] = $division['league_name'];
     } else {
         $this->_addMenuItem($league['name'], array('controller' => 'leagues', 'action' => 'view', 'league' => $league['id']), $path);
         $path[] = $league['name'];
         if (!empty($division['name'])) {
             $this->_addMenuItem($division['name'], array('controller' => 'divisions', 'action' => 'view', 'division' => $division['id']), $path);
             $path[] = $division['name'];
         }
     }
     $this->_addMenuItem(__('Schedule', true), array('controller' => 'divisions', 'action' => 'schedule', 'division' => $division['id']), $path);
     $this->_addMenuItem(__('Standings', true), array('controller' => 'divisions', 'action' => 'standings', 'division' => $division['id']), $path);
     if ($this->is_logged_in) {
         if ($division['schedule_type'] != 'competition') {
             $this->_addMenuItem(__('Scores', true), array('controller' => 'divisions', 'action' => 'scores', 'division' => $division['id']), $path);
         }
         if (League::hasStats($league)) {
             $this->_addMenuItem(__('Stats', true), array('controller' => 'divisions', 'action' => 'stats', 'division' => $division['id']), $path);
         }
     }
     if ($this->is_admin || $is_manager || $is_coordinator) {
         $this->_addMenuItem(__('Add Games', true), array('controller' => 'schedules', 'action' => 'add', 'division' => $division['id']), array_merge($path, array('Schedule')));
         if ($division['schedule_type'] != 'competition') {
             $this->_addMenuItem(__('Approve scores', true), array('controller' => 'divisions', 'action' => 'approve_scores', 'division' => $division['id']), $path);
         }
         if ($division_count == 1) {
             $this->_addMenuItem(__('Edit', true), array('controller' => 'leagues', 'action' => 'edit', 'league' => $league['id']), $path);
         } else {
             $this->_addMenuItem(__('Edit', true), array('controller' => 'divisions', 'action' => 'edit', 'division' => $division['id']), $path);
         }
         $this->_addMenuItem(sprintf(__('%s distribution', true), __(Configure::read('sport.field_cap'), true)), array('controller' => 'divisions', 'action' => 'fields', 'division' => $division['id']), $path);
         $this->_addMenuItem(sprintf(__('%s availability', true), __(Configure::read('sport.field_cap'), true)), array('controller' => 'divisions', 'action' => 'slots', 'division' => $division['id']), $path);
         $this->_addMenuItem(__('Status report', true), array('controller' => 'divisions', 'action' => 'status', 'division' => $division['id']), $path);
         if (Configure::read('scoring.allstars') && $division['allstars'] != 'never') {
             $this->_addMenuItem(__('All stars', true), array('controller' => 'divisions', 'action' => 'allstars', 'division' => $division['id']), $path);
         }
         $this->_addMenuItem(__('Captain emails', true), array('controller' => 'divisions', 'action' => 'emails', 'division' => $division['id']), $path);
         if (League::hasSpirit($league)) {
             $this->_addMenuItem(__('Spirit Report', true), array('controller' => 'divisions', 'action' => 'spirit', 'division' => $division['id']), $path);
             $this->_addMenuItem(__('Download', true), array('controller' => 'divisions', 'action' => 'spirit', 'division' => $division['id'], 'ext' => 'csv'), array_merge($path, array('Spirit Report')));
         }
         $this->_addMenuItem(__('Adjust seeds', true), array('controller' => 'divisions', 'action' => 'seeds', 'division' => $division['id']), $path);
     }
     if ($this->is_admin) {
         $this->_addMenuItem(__('Add coordinator', true), array('controller' => 'divisions', 'action' => 'add_coordinator', 'division' => $division['id']), $path);
     }
     // Some items are only applicable depending on league configuration
     if (!empty($division['schedule_type'])) {
         $league_obj = $this->_getComponent('LeagueType', $division['schedule_type'], $this);
         $league_obj->addMenuItems($division, $path, $is_coordinator || $is_manager);
     }
 }
Beispiel #5
0
 function displayScore($game, $division, $league, $show_score_for_team = false)
 {
     // Data may come in one of two forms.
     if (array_key_exists('Game', $game)) {
         // Either all the models are at the same level in the array...
         $details = $game['Game'];
     } else {
         // ...or the Game model is at the top and others are below
         $details = $game;
     }
     $view =& ClassRegistry::getObject('view');
     $is_logged_in = $view->viewVars['is_logged_in'];
     $is_admin = $view->viewVars['is_admin'];
     $is_manager = $view->viewVars['is_manager'] && in_array($league['affiliate_id'], $this->UserCache->read('ManagedAffiliateIDs'));
     $is_volunteer = $view->viewVars['is_volunteer'];
     $is_official = $view->viewVars['is_official'];
     $is_coordinator = in_array($details['division_id'], $this->UserCache->read('DivisionIDs'));
     // Calculate the game start and end time stamps
     $start_time = strtotime("{$game['GameSlot']['game_date']} {$game['GameSlot']['game_start']}") + Configure::read('timezone.adjust') * 60;
     $end_time = strtotime("{$game['GameSlot']['game_date']} {$game['GameSlot']['display_game_end']}") + Configure::read('timezone.adjust') * 60;
     // Check if one of the teams involved in the game is a team the current user is a captain of
     $teams = array_intersect(array($details['home_team'], $details['away_team']), $this->UserCache->read('OwnedTeamIDs'));
     $team_id = array_pop($teams);
     $links = array();
     if (Game::_is_finalized($details)) {
         if (in_array($details['status'], Configure::read('unplayed_status'))) {
             __($details['status']);
         } else {
             if ($division['schedule_type'] == 'competition') {
                 echo $details['home_score'];
             } else {
                 // If scores are being shown from a particular team's perspective,
                 // we may need to swap the home and away scores.
                 if ($show_score_for_team == $details['away_team']) {
                     $first_score = $details['away_score'];
                     $second_score = $details['home_score'];
                 } else {
                     $first_score = $details['home_score'];
                     $second_score = $details['away_score'];
                 }
                 echo "{$first_score} - {$second_score}";
             }
             if (strpos($details['status'], 'default') !== false) {
                 echo ' (' . __('default', true) . ')';
             }
             if (League::hasStats($league)) {
                 if ($team_id || $is_admin || $is_coordinator) {
                     $links[] = $this->Html->link(__('Submit Stats', true), array('controller' => 'games', 'action' => 'submit_stats', 'game' => $details['id'], 'team' => $team_id));
                 }
                 if (($this->params['controller'] != 'games' || $this->params['action'] != 'stats') && ($is_logged_in || Configure::read('feature.public'))) {
                     $links[] = $this->ZuluruHtml->iconLink('stats_24.png', array('controller' => 'games', 'action' => 'stats', 'game' => $details['id'], 'team' => $show_score_for_team), array('alt' => __('Game Stats', true), 'title' => __('Game Stats', true)));
                 }
             }
         }
     } else {
         $score_entry = Game::_get_best_score_entry($game);
         if (!empty($score_entry)) {
             if (in_array($score_entry['status'], Configure::read('unplayed_status'))) {
                 __($score_entry['status']);
             } else {
                 if ($division['schedule_type'] == 'competition') {
                     echo $score_entry['score_for'];
                 } else {
                     // If scores are being shown from a particular team's perspective,
                     // we may need to swap the home and away scores.
                     if ($show_score_for_team == $score_entry['team_id'] || $show_score_for_team === false && $score_entry['team_id'] == $details['home_team']) {
                         $first_score = $score_entry['score_for'];
                         $second_score = $score_entry['score_against'];
                     } else {
                         $first_score = $score_entry['score_against'];
                         $second_score = $score_entry['score_for'];
                     }
                     echo "{$first_score} - {$second_score}";
                 }
             }
             if ($team_id) {
                 if ($score_entry['status'] == 'in_progress') {
                     $links[] = $this->Html->link(__('Live Score', true), array('controller' => 'games', 'action' => 'live_score', 'game' => $details['id'], 'team' => $team_id));
                 } else {
                     if ($score_entry['team_id'] == $team_id) {
                         $links[] = $this->Html->link(__('Edit score', true), array('controller' => 'games', 'action' => 'submit_score', 'game' => $details['id'], 'team' => $team_id));
                     } else {
                         $links[] = $this->Html->link(__('Submit', true), array('controller' => 'games', 'action' => 'submit_score', 'game' => $details['id'], 'team' => $team_id));
                     }
                 }
                 // Check if someone is a captain on both teams that played each other
                 $second_team_id = array_pop($teams);
                 if ($second_team_id) {
                     $links[] = $this->Html->link(__('Submit', true), array('controller' => 'games', 'action' => 'submit_score', 'game' => $details['id'], 'team' => $second_team_id));
                 }
             } else {
                 if ($is_volunteer || $is_official) {
                     // Allow specified individuals (referees, umpires, volunteers) to live score without a team id
                     if ($score_entry['status'] == 'in_progress') {
                         $links[] = $this->Html->link(__('Live Score', true), array('controller' => 'games', 'action' => 'live_score', 'game' => $details['id']));
                     } else {
                         $links[] = $this->Html->link(__('Edit score', true), array('controller' => 'games', 'action' => 'edit', 'game' => $details['id']));
                     }
                 }
             }
             if ($score_entry['status'] == 'in_progress') {
                 echo ' (' . __('in progress', true) . ')';
             } else {
                 echo ' (' . __('unofficial', true) . ')';
             }
         } else {
             if ($score_entry === null) {
                 __('score mismatch');
                 if ($team_id) {
                     if ($score_entry['status'] == 'in_progress') {
                         $links[] = $this->Html->link(__('Live Score', true), array('controller' => 'games', 'action' => 'live_score', 'game' => $details['id'], 'team' => $team_id));
                     } else {
                         $links[] = $this->Html->link(__('Edit score', true), array('controller' => 'games', 'action' => 'submit_score', 'game' => $details['id'], 'team' => $team_id));
                     }
                 }
             } else {
                 if (time() > ($start_time + 3 * $end_time) / 4) {
                     if ($division['schedule_type'] != 'competition') {
                         // Allow score submissions any time after 3/4 through the game.
                         // Some people like to submit via mobile phone immediately, and games can end early.
                         if ($team_id) {
                             $links[] = $this->Html->link(__('Submit', true), array('controller' => 'games', 'action' => 'submit_score', 'game' => $details['id'], 'team' => $team_id));
                         } else {
                             __('not entered');
                         }
                     } else {
                         if ($is_admin || $is_manager || $is_coordinator) {
                             $links[] = $this->Html->link(__('Submit', true), array('controller' => 'game_slots', 'action' => 'submit_score', 'slot' => $details['game_slot_id']));
                         }
                     }
                 } else {
                     if (time() > $start_time - 30 * 60) {
                         if ($details['home_team'] != null && $details['away_team'] != null) {
                             // Allow live scoring to start up to half an hour before scheduled game start time.
                             // This allows score keepers to get the page loaded and ready to go in advance.
                             if ($team_id) {
                                 $links[] = $this->Html->link(__('Live Score', true), array('controller' => 'games', 'action' => 'live_score', 'game' => $details['id'], 'team' => $team_id));
                             } else {
                                 if ($is_volunteer || $is_official) {
                                     // Allow specified individuals (referees, umpires, volunteers) to live score without a team id
                                     $links[] = $this->Html->link(__('Live Score', true), array('controller' => 'games', 'action' => 'live_score', 'game' => $details['id']));
                                 }
                             }
                         }
                     } else {
                         // Check if one of the teams involved in the game is a team the current user is on
                         $player_team_id = array_pop(array_intersect(array($details['home_team'], $details['away_team']), $this->UserCache->read('TeamIDs')));
                         if (!$player_team_id) {
                             $player_team_id = array_pop(array_intersect(array($details['home_team'], $details['away_team']), $this->UserCache->read('RelativeTeamIDs')));
                         }
                         if ($player_team_id) {
                             $links[] = $this->Html->link(__('iCal', true), array('controller' => 'games', 'action' => 'ical', $details['id'], $player_team_id, 'game.ics'));
                         }
                     }
                 }
             }
         }
     }
     // Give admins, managers and coordinators the option to edit games
     if ($is_admin || $is_manager || $is_coordinator) {
         $links[] = $this->ZuluruHtml->iconLink('edit_24.png', array('controller' => 'games', 'action' => 'edit', 'game' => $details['id'], 'return' => true), array('alt' => __('Edit', true), 'title' => __('Edit', true)));
     }
     echo $this->Html->tag('span', implode('', $links), array('class' => 'actions'));
 }