Esempio n. 1
0
 /**
  * Get rank by problems solved logic. It has its own func so
  * it can be accesed internally without authentication
  *
  * @param Request $r
  * @throws InvalidDatabaseOperationException
  */
 public static function getRankByProblemsSolved(Request $r)
 {
     if (is_null($r['user'])) {
         $rankCacheName = $r['offset'] . '-' . $r['rowcount'];
         $cacheUsed = Cache::getFromCacheOrSet(Cache::PROBLEMS_SOLVED_RANK, $rankCacheName, $r, function (Request $r) {
             $response = array();
             $response['rank'] = array();
             try {
                 $db_results = UsersDAO::GetRankByProblemsSolved2($r['rowcount'], $r['offset']);
             } catch (Exception $e) {
                 throw new InvalidDatabaseOperationException($e);
             }
             if (!is_null($db_results)) {
                 foreach ($db_results as $userEntry) {
                     $user = $userEntry['user'];
                     array_push($response['rank'], array('username' => $user->getUsername(), 'name' => $user->getName(), 'problems_solved' => $userEntry['problems_solved'], 'rank' => $userEntry['rank'], 'score' => $userEntry['score'], 'country_id' => $user->getCountryId()));
                 }
             }
             return $response;
         }, $response);
         // If cache was set, we need to maintain a list of different ranks in the cache
         // (A different rankCacheName means different offset and rowcount params
         if ($cacheUsed === false) {
             self::setProblemsSolvedRankCacheList($rankCacheName);
         }
     } else {
         $response = array();
         try {
             $db_results = UsersDAO::GetRankByProblemsSolved2($r['rowcount'], $r['offset'], $r['user']);
         } catch (Exception $e) {
             throw new InvalidDatabaseOperationException($e);
         }
         if (!is_null($db_results)) {
             if (count($db_results) > 0) {
                 $entry = $db_results[0];
                 $response['rank'] = $entry['rank'];
                 $response['name'] = $r['user']->getName();
                 $response['problems_solved'] = $entry['problems_solved'];
             } else {
                 $response['rank'] = 0;
                 $response['name'] = $r['user']->getName();
                 $response['problems_solved'] = 0;
             }
         }
     }
     $response['status'] = 'ok';
     return $response;
 }