static function getKey($key) { if (self::$cache === null) { self::$cache = Model::pluck(Setting::find('1=1'), 'value', 'key'); } return self::$cache[$key]; }
/** * @param bool|true $ispublic * @return Team[] */ function getTeams($ispublic = true) { if (!$ispublic) { $teamIds = Model::pluck(SeasonTeam::find('seasonid = ?', [$this->seasonid]), 'teamid'); } else { $teamIds = Model::pluck(SeasonTeam::find('seasonid = ? and ispublic', [$this->seasonid]), 'teamid'); } if (!$teamIds) { return []; } return Model::orderBy(Team::getMultiple($teamIds), 'name'); }
public function makeMatches() { $teams = Team::find('1=1'); $teamIdByName = Model::pluck($teams, 'teamid', 'name'); $week = 0; $matches = []; foreach (explode("\n", file_get_contents("matches.txt")) as $line) { $parts = explode("VS", $line); if (count($parts) == 1 && substr(trim($line), 0, 2) == '--') { $week++; } if (count($parts) != 2) continue; list($name1, $name2) = $parts; $name1 = trim($name1); $name2 = trim($name2); $match = Match::create(); $match->team1id = $teamIdByName[$name1]; $match->team2id = $teamIdByName[$name2]; $match->week = $week; $matches[] = $match; if (!$match->team1id) { die("Equipo desconocido: $name1"); } if (!$match->team2id) { die("Equipo desconocido: $name2"); } } if (count($matches) >= 1 && count($matches) != count(Match::find('1=1'))) { Match::truncate(true); Model::saveAll($matches); ?>Enfrentamientos actualizados.<br><? } else { ?>No hay cambios en los enfrentamientos.<br><? } }
private static function afterLogin() { $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); $json = $connection->get('users/show', ['user_id' => $_SESSION['twitter-userid']]); $_SESSION['twitter-avatar'] = isset($json->profile_image_url) ? $json->profile_image_url : ''; Avatar::setUsersAvatar($_SESSION['twitter-userid'], $_SESSION['twitter-username'], $_SESSION['twitter-avatar']); $oldAvatars = Avatar::find('1=1 order by dateline asc limit 100'); if ($oldAvatars) { $oldAvatarsIds = Model::pluck($oldAvatars, 'userid'); $json = $connection->get('users/lookup', ['user_id' => implode(',', $oldAvatarsIds)]); foreach ($json as $userdata) { $newavatars[$userdata->id_str] = $userdata->profile_image_url; } foreach ($oldAvatars as $avatar) { $avatar->url = $newavatars[$avatar->userid]; $avatar->dateline = time(); } Model::saveAll($oldAvatars); } }
private function showTeamSeasons() { $teamSeasonIds = Model::pluck(SeasonTeam::find('teamid = ?', [$this->team->teamid]), 'seasonid'); $teamSeasons = Season::getMultiple($teamSeasonIds); Model::orderBy($teamSeasons, 'seasonid'); $teamMatches = Match::find('team1id = ? or team2id = ?', [$this->team->teamid, $this->team->teamid]); // $games = []; $playedGames = []; $wins = []; $losses = []; foreach($teamMatches as $match) { if (!$match->isPublished()) continue; // $games[$match->seasonid]++; if ($match->getWinner() == $this->team->teamid) { $playedGames[$match->seasonid]++; $wins[$match->seasonid]++; } if ($match->getLooser() == $this->team->teamid) { $playedGames[$match->seasonid]++; $losses[$match->seasonid]++; } } ?> <h2>Actividad por temporadas</h2> <table style="width: 400px"> <thead><tr> <td>Nombre</td> <td>Combates</td> <td>Victorias</td> <td>Derrotas</td> </tr></thead> <? foreach($teamSeasons as $season) { if (!$season->ispublic && !Team::isSuperAdmin()) continue; ?> <tr> <td> <a href="/<?=$season->getLink()?>/equipos/<?=$this->team->getLink()?>/"> <?= htmlentities($season->name) ?> </a> </td> <td><?= $playedGames[$season->seasonid] * 1 ?></td> <td><?= $wins[$season->seasonid] * 1 ?></td> <td><?= $losses[$season->seasonid] * 1 ?></td> </tr> <? } ?></table><br><? }
static function orderBy($items, $field) { $keys = Model::pluck($items, $field); array_multisort($keys, $items); return $items; }
/** * @return void */ public function show() { if (!TwitterAuth::isLogged()) { ?> Sólo los miembros pueden ver esta página. <a href="<?=HTMLResponse::getRoute()?>?authenticate=1"> Inicia sesión. </a><br> <? return; } else { if (!Team::isMember()) { HTMLResponse::exitWithRoute('/votaciones/'); } $answer = PollVote::findOne('userid = ? and pollid = ?', [TwitterAuth::getUserId(), $this->poll->pollid]); $options = PollOption::find('pollid = ? order by polloptionid asc', [$this->poll->pollid]); if (!$answer && strlen($hash = HTMLResponse::fromGET('hash', ''))) { $optionid = HTMLResponse::fromGET('vote'); foreach($options as $index => $option) { if ($option->polloptionid == $optionid && $option->getHash() == $hash) { $answer = PollVote::create(); $answer->userid = TwitterAuth::getUserId(); $answer->dateline = time(); $answer->avatar = TwitterAuth::getAvatar(); $answer->pollid = $this->poll->pollid; $answer->polloptionid = $option->polloptionid; $answer->username = TwitterAuth::getUserName(); $answer->save(); } } } $answers = Model::groupBy(PollVote::find('pollid = ?', [$this->poll->pollid]), 'polloptionid'); $hasAnswered = !!$answer; ?><div style="text-align:left; margin: 0 auto" class="inblock"> <table style="width:640px"> <thead> <tr> <td>Lista de opciones</td> </tr> </thead> <? foreach($options as $index => $option) { ?> <tr><td class="row" style="text-align: left"> <div style="height: 6px"></div> <div class="inblock middle" style="width:320px"> <b>Opción <?=$index+1?></b>: <?= htmlentities($option->title) ?> </div> <div class="inblock middle"> <div class="moreless inblock middle" style="width: 150px"> <a href="javascript:void(0)" onclick="$(this).closest('.row').find('.moreless').toggle(); $(this).closest('.row').find('.onmore').slideDown(500);">+ Mostrar más</a> </div> <div class="moreless inblock middle" style="width: 150px; display: none"> <a href="javascript:void(0)" onclick="$(this).closest('.row').find('.moreless').toggle(); $(this).closest('.row').find('.onmore').slideUp(400);">- Mostrar menos</a> </div> </div> <div class="inblock middle"> <? if (!$hasAnswered) { ?> <a href="<?=HTMLResponse::getRoute()?>?vote=<?=$option->polloptionid?>&hash=<?=$option->getHash()?>" onclick="return confirm('¿Votas <?=htmlentities($option->title)?>?')"> Votar esta opción </a> <? } else if ($answer->polloptionid == $option->polloptionid) { ?> <i>Votaste esta opción</i> <? } ?> </div> <div class="onmore" style="display: none; padding: 12px"> <?= $option->description ?> </div> <div style="height: 6px"></div> <? if (!$hasAnswered) { ?><i>Vota primero para ver los resultados</i><? } else { $optionAnswers = $answers[$option->polloptionid]; ?> Votado por: <?= $optionAnswers ? '<b>'.implode(', ', Model::pluck($optionAnswers, 'username')).'</b> ('.count($optionAnswers).' votos)' : '<i>Nadie</i>'; ?> <? } ?> <div style="height: 6px"></div> </td></tr> <? } ?></table></div><br><br><? } }
public function getScore() { $votes = $this->getVotes(); return array_sum(Model::pluck($votes, 'vote')); }
/** * @return void */ public function show() { $matches = Match::find('seasonid = ? and result != 0', [$this->season->seasonid]); $matches = Model::indexBy($matches, 'matchid'); $teams = Model::indexBy(Team::find('1=1'), 'teamid'); $matchWinner = []; foreach($matches as $match) { if ($this->season->weekIsPublished($match->week) && !$match->isDelayed()) { $matchWinner[$match->matchid] = $match->getWinner(); } } $totalBets = []; $correctBets = []; $usernames = []; $avatars = Model::pluck(Avatar::find('1=1'), 'url', 'userid'); foreach(Bet::find('1=1') as $bet) { if (isset($matchWinner[$bet->matchid])) { if ($bet->teamid && $matchWinner[$bet->matchid] == $bet->teamid) { $correctBets[$bet->userid]++; } $totalBets[$bet->userid]++; } if ($bet->username) { $usernames[$bet->userid] = $bet->username; } } $tiebreakers = []; foreach(array_keys($correctBets) as $userid) { $tiebreakers[$userid] = [-$correctBets[$userid], $totalBets[$userid], strtolower($usernames[$userid])]; } asort($tiebreakers); if (TwitterAuth::isLogged()) { $userid = TwitterAuth::getUserId(); $userBets = Bet::find('userid = ? order by matchid desc', [$userid]); $userpos = array_search($userid, array_keys($tiebreakers)); $userpos = ($userpos === FALSE) ? 0 : $userpos+1; ?> <div class="inblock" style="text-align: left; margin-right: 20px"> <h2>Tus estadísticas</h2> <table> <thead> <tr> <td>Puesto</td> <td>Nombre</td> <td>Aciertos</td> <td>Fallos</td> </tr> </thead> <tr> <td><?= $userpos ?>º</td> <td style="text-align: left"> <div class="inblock" style="vertical-align: middle"> <a href="http://twitter.com/<?=htmlentities($usernames[$userid])?>" target="_blank"> <img src="<?= htmlentities($avatars[$userid]) ?>" style="width:40px; height:40px; border-radius: 20px"> </a> </div> <div class="inblock" style="vertical-align: middle"> <a href="http://twitter.com/<?=htmlentities($usernames[$userid])?>" target="_blank"> <?= htmlentities($usernames[$userid]) ?> <? if (!isset($usernames[$userid])) echo "<i>$userid</i>"; ?> </a> </div> </td> <td><?= $correctBets[$userid] ?></td> <td><?= $totalBets[$userid]-$correctBets[$userid] ?></td> </tr> </table> <h2>Tus apuestas</h2> <table> <thead> <tr> <td>Jornada</td> <td>Enfrentamiento</td> <td>Acierto</td> </tr> </thead> <? foreach($userBets as $bet) { if (!isset($matches[$bet->matchid])) continue; /** * @var $match Match */ $match = $matches[$bet->matchid]; if (!$match->isPublished() || $match->isDelayed()) continue; $team1 = $teams[$match->team1id]; $team2 = $teams[$match->team2id]; $success = $match->getWinner() == $bet->teamid; ?> <tr> <td><?= $match->week ?></td> <td> <div class="inblock"> <div class="teamimg64"> <img src="/<?= $team1->getImageLink(64, 64) ?>" class="<?= $match->getWinner() == $team1->teamid ? '' : 'grayscale' ?>"> </div> <? if ($bet->teamid == $team1->teamid) { ?> <br><i style="font-size:11px">Votado</i> <? } ?> </div> <div class="inblock" style="line-height: 64px; margin: 0px 4px"> VS </div> <div class="inblock"> <div class="teamimg64"> <img src="/<?= $team2->getImageLink(64, 64) ?>" class="<?= $match->getWinner() == $team2->teamid ? '' : 'grayscale' ?>"> </div> <? if ($bet->teamid == $team2->teamid) { ?> <br><i style="font-size:11px">Votado</i> <? } ?> </div> </td> <td> <?= $success ? '<div class="success-icon">✔</div>' : '<div class="fail-icon">✘</div>' ?> </td> </tr> <? } ?> </table> </div> <? } ?> <div class="inblock"> <h2>Clasificación</h2> <table> <thead> <tr> <td>Puesto</td> <td>Nombre</td> <td>Aciertos</td> <td>Fallos</td> </tr> </thead> <? $lastTiebreakers = null; $lastPos = 0; foreach(array_keys($tiebreakers) as $pos => $userid) { unset($tiebreakers[$userid][2]); if ($lastTiebreakers != $tiebreakers[$userid]) { $lastPos = $pos; } if (!isset($avatars[$userid])) { Avatar::setUsersAvatar($userid, $usernames[$userid]); } ?> <tr> <td><?= $lastPos+1 ?>º</td> <td style="text-align: left"> <div class="inblock" style="vertical-align: middle"> <a href="http://twitter.com/<?=htmlentities($usernames[$userid])?>" target="_blank"> <img src="<?= htmlentities($avatars[$userid]) ?>" style="width:40px; height:40px; border-radius: 20px"> </a> </div> <div class="inblock" style="vertical-align: middle"> <a href="http://twitter.com/<?=htmlentities($usernames[$userid])?>" target="_blank"> <?= htmlentities($usernames[$userid]) ?> <? if (!isset($usernames[$userid])) echo "<i>$userid</i>"; ?> </a> </div> </td> <td><?= $correctBets[$userid] ?></td> <td><?= $totalBets[$userid]-$correctBets[$userid] ?></td> </tr> <? $lastTiebreakers = $tiebreakers[$userid]; } ?> </table> </div> <? }