public function getLeague($entity) { if (is_null($entity->tempDataGet('league'))) { $conditions = array('_id' => $entity->league_id); $entity->tempDataSet('league', Leagues::first(compact('conditions'))); } return $entity->tempDataGet('league'); }
protected function edit() { $league = Leagues::find((string) $this->game->league_id); if (!$this->CURRENT_USER or !$league->isManager($this->CURRENT_USER)) { $this->flashMessage('You don\'t have permission to view that page.', array('alertType' => 'error')); return $this->redirect('/'); } if (!isset($this->game)) { $this->flashMessage('Game not found.', array('alertType' => 'error')); $this->redirect('/'); return; } if ($this->game->game_time->sec < time()) { $this->flashMessage('This game is in the past! Any reported scores will be erased if this game is edited.', array('alertType' => 'warning')); } if ($this->request->data) { $gameStartTime = $this->request->data['game_date'] . ' ' . $this->request->data['game_time']; unset($this->request->data['game_date']); unset($this->request->data['game_time']); $this->request->data['game_time'] = strtotime($gameStartTime); $this->game->set($this->request->data); if ($this->game->save()) { // Handle game with scores already if ($this->game->scores) { $old_score_report = $this->game->scores->export(); $old_score_report = $old_score_report['data']; if (!empty($old_score_report)) { // Log the previously-reported score $conditions = array('_id' => $this->game->_id); $query = array('$push' => array('old_scores' => $old_score_report)); Games::update($query, $conditions); // Remove the scores and winner $query = array('$unset' => array('scores' => 1, 'winner' => 1)); Games::update($query, $conditions); } } $this->flashMessage('The game has been updated successfully.', array('alertType' => 'success')); return $this->redirect(array('Leagues::view', 'id' => $league->_id)); } } return compact('league'); }
<?php if (isset($CURRENT_USER) and $CURRENT_USER->can('leagues.edit')) { echo '<p>' . $this->html->link('<i class="icon-plus icon-white"></i> Create a new league', 'Leagues::create', array('class' => 'btn btn-primary', 'escape' => false)) . '</p>'; } $league_types = array('future' => 'Upcoming Leagues', 'present' => 'Current Leagues', 'past' => 'Past Leagues'); foreach ($league_types as $key => $label) { if (count($leagues[$key]) <= 0) { continue; } echo "<h2>{$label}</h2>\n"; echo '<table class="table table-striped tablesorter">'; echo '<thead><tr><th width="7%">Action</th><th>League Name</th><th width="15%">Season</th><th width="15%">Sport</th><th width="15%">Commissioners</th><th width="15%">Start</th><th width="15%">End</th></tr></thead>'; echo '<tbody>'; foreach ($leagues[$key] as $l) { $this_league = Leagues::find($l['_id']); $league_link = $this->html->link($l['name'], array('Leagues::view', 'id' => $l['_id'])); $actionLinks = array(); if (isset($CURRENT_USER)) { $actionLinks[] = $this->html->link('<i class="icon-list"></i>', "/leagues/{$l['_id']}/registrations", array('escape' => false, 'class' => 'hasTooltip', 'title' => 'Participant List')); } else { $actionLinks[] = '<i class="icon-list hasTooltip" title="Please log in to view the participant list."></i>'; } if (isset($CURRENT_USER) and $this_league->isManager($CURRENT_USER)) { $actionLinks[] = $this->html->link('<i class="icon-pencil"></i>', array('Leagues::edit', 'id' => $l['_id']), array('escape' => false, 'class' => 'hasTooltip', 'title' => 'Edit League')); } if ($l['meta']['registration_open'] and isset($CURRENT_USER) and $CURRENT_USER->can('leagues.register')) { $actionLinks[] = $this->html->link('<i class="icon-shopping-cart"></i>', "/leagues/" . $l['_id'] . "/register/", array('escape' => false, 'class' => 'hasTooltip', 'title' => 'Register')); } echo '<tr>'; echo '<td>' . implode(' ', $actionLinks) . '</td>';
public function create() { if (isset($this->CURRENT_USER) and !$this->CURRENT_USER->can('leagues.create')) { $this->flashMessage('You don\'t have permission to view that page.', array('alertType' => 'error')); return $this->redirect('Leagues::index'); } $league = Leagues::create(); if ($this->request->data and $league->save($this->request->data)) { return $this->redirect('Leagues::index'); } return compact('league'); }
public function getLeague($entity) { $conditions = array('_id' => $entity->league_id); return Leagues::first(compact('conditions')); }
protected function updateLeagueStandings() { // Compile a list of all teams needing an update across all leagues $conditions = array('needs_standings_update' => true); $fields = array('_id'); $leagues_needing_update = Leagues::all(compact('conditions', 'fields')); foreach ($leagues_needing_update as $l) { $team_list = array(); $conditions = array('league_id' => $l->_id); $leage_teams = Teams::all(compact('conditions')); foreach ($leage_teams as $t) { $team_list[] = $t; } usort($team_list, function ($a, $b) { // One of the team has no reported scores, other team wins by default. if (is_object($a->stats) and !is_object($b->stats)) { return 1; } else { if (is_object($a->stats) and !is_object($b->stats)) { return -1; } else { if (!is_object($a->stats) and !is_object($b->stats)) { return 0; } } } // Criteria #1: winning percentage $a_pct = $a->stats->wins / ($a->stats->wins + $a->stats->losses); $b_pct = $b->stats->wins / ($b->stats->wins + $b->stats->losses); if ($a_pct != $b_pct) { // Needs to return an int, these percentages won't be ints. return intval(($a_pct - $b_pct) * 100); } // Criteria #2: Number of wins if ($a->stats->wins != $b->stats->wins) { return $a->stats->wins - $b->stats->wins; } // Criteria #3: Head-to-head wins / Criteria #4: Head-to-head point-diff $conditions = array('teams' => array($a->_id, $b->_id), 'scores' => array('$exists' => true)); $joint_games = Games::all(compact('conditions')); $head_to_head_wins = 0; $head_to_head_pnts = 0; foreach ($joint_games as $g) { // Note: Lithium does not support $and yet, so we can't do this optimally if ($a->_id != $g->teams[0] and $a->_id != $g->teams[1] or $b->_id != $g->teams[0] and $b->_id != $g->teams[1]) { // This game isn't head-to-head, doesn't count; continue; } if ($g->winner == $a->_id) { $head_to_head_wins++; $head_to_head_pnts += $g->getScoreDiff(); } else { if ($g->winner == $b->_id) { $head_to_head_wins--; $head_to_head_pnts -= $g->getScoreDiff(); } } } if ($head_to_head_wins != 0) { return $head_to_head_wins; } if ($head_to_head_pnts != 0) { return $head_to_head_pnts; } // Criteria #5: Head-to-head point-diff if ($a->stats->point_differential != $b->stats->point_differential) { return $a->stats->point_differential - $b->stats->point_differential; } // Still tied? $a->stats->tied = true; $b->stats->tied = true; return 0; }); // Assign their ranks, handle ties correctly: $rank = 0; $tied_rank = null; foreach (array_reverse($team_list) as $t) { $rank++; if (!isset($tied_rank) and $t->stats->tied == true) { $tied_rank = $rank; } else { if ($t->stats->tied == false) { $tied_rank = null; } } $display_rank = $tied_rank ?: $rank; $t->save(array('stats.rank' => $display_rank)); } } }