Beispiel #1
0
 /**
  * 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;
 }