/** * Get the ranking for a competition * * @param Competition $competition * @param integer $maxResults * * @return array */ public function getRankingForCompetition(Competition $competition, $maxResults = 10) { $ranking = $this->repository->createQueryBuilder('p')->select('p as picture')->addSelect('COUNT(NULLIF(IDENTITY(votes.votedAgainstPicture), p.id)) as votes_up')->addSelect('COUNT(NULLIF(IDENTITY(votes.votedForPicture), p.id)) as votes_down')->addSelect('COUNT(NULLIF(IDENTITY(votes.votedAgainstPicture), p.id)) - COUNT(NULLIF(IDENTITY(votes.votedForPicture), p.id)) as total_votes')->where('p.competition = :competitionId')->andWhere('p.status = :status')->setParameter('competitionId', $competition->getId())->setParameter('status', 'ACTIVE')->leftJoin('HotOrNotBundle\\Entity\\Vote', 'votes', \Doctrine\ORM\Query\Expr\Join::WITH, 'p.id = votes.votedForPicture or p.id = votes.votedAgainstPicture')->groupBy('p.id')->addOrderBy('total_votes', 'DESC')->addOrderBy('votes_up', 'DESC')->setMaxResults($maxResults)->getQuery()->getResult(); $currentRank = 1; $previousRank = 1; $previousTotal = null; // set the rank for each picture foreach ($ranking as &$item) { $item['rank'] = $currentRank; if ($previousTotal === $item['total_votes']) { $item['rank'] = $previousRank; } $previousRank = $item['rank']; $previousTotal = $item['total_votes']; $currentRank++; } return $ranking; }
/** * Get a random combination by competition * which the currently logged in competitor has not rated on * * @param Competition $competition * @param Session $session * @param Competitor $competitor * * @return Combination */ public function getRandomCombinationByCompetition(Competition $competition, Session $session, $competitor) { $combination = null; $amount = 1; $competitionId = $competition->getId(); $combinationSession = $session->get('hotornot-combinations'); if (empty($combinationSession)) { $combinationSession = []; } $competitorId = 0; if ($competitor instanceof Competitor && $competitor->hasRole('ROLE_OAUTH_USER')) { $competitorId = $competitor->getId(); } // get all the combinations the current user has not voted on $possibleCombinations = $this->getUnratedCombinationIds($competitionId, $competitorId); if (!empty($possibleCombinations)) { // check if the session contains an entry // with the last combination and display it if (array_key_exists($competitionId, $combinationSession)) { $combinationData = $combinationSession[$competitionId]; if (in_array($combinationData['id'], $possibleCombinations)) { $combination = $this->getCombinationById($combinationData['id']); return $combination; } } // randomly select one entry from possible combinations $combinationId = $possibleCombinations[array_rand($possibleCombinations)]; // get the combination $combination = $this->getCombinationById($combinationId); } if (empty($combination)) { // unset last displayed combination unset($combinationSession[$competitionId]); } else { // set the last displayed combination $combinationSession[$competitionId] = ['id' => $combination->getId(), 'reversed' => 1 === rand(0, 1)]; } // update the session $session->set('hotornot-combinations', $combinationSession); return $combination; }