public static function startNewSeason($clusterIdentifier)
 {
     $season = QuizHallOfFame::getCurrentSeason($clusterIdentifier);
     $db = MMDB::instance();
     $players = $db->arrayQuery( sprintf("
         SELECT uuid
         FROM quiz_player_scoring
         WHERE user_cluster = '%s'",
         $db->escapeString( $clusterIdentifier )
     ));
     $db->begin();
     foreach($players as $player)
     {
         $uuid = $player['uuid'];
         QuizReply::moveToArchive($uuid, $season);
     }
     QuizPlayerScoring::moveToArchive($clusterIdentifier, $season);
     $db->commit();
 }
    public function getHallOfFame($season)
    {
        $tpl = $this->tpl();
        $db         = eZDB::instance();
        $ust        = array();
        $playerData = null;
        $totalPlayers = 0;
        $lowestGlobalRanking         = 0;
        $uuidsInGlobalLeaderboard = array();
        $_globalLeaderboard          = array(
            1 => array(),
            2 => array(),
            3 => array(),
        );

        $userSpecialtyTranslations = FacetFilteringTool::getTaxoTranslationWithIDs( 'user_specialty' );
        foreach( $userSpecialtyTranslations as $translation )
            $ust[$translation['id']] = $translation['label'];
        $userSpecialtyTaxonomyId = $userSpecialtyTranslations[$this->user()->attribute( 'user_speciality' )]['id'];
        $clusterIdentifier = ($this->isLocalQuiz()) ? ClusterTool::clusterIdentifier() : '';
        $results = array();
        for ($i = 1; $i <= 3; $i++)
        {
            $chunks = $db->arrayQuery( sprintf("
                    SELECT
                        COUNT(id) as total, uuid, user_specialty, game_name, global_ranking, specialty_ranking, score, nb_correct, nb_wrong
                    FROM
                        %s
                    WHERE
                        cluster_identifier = '%s'
                        AND application_id = %d
                        AND global_ranking = %d
                        AND season = %d
                    GROUP BY
                        uuid
                    ORDER BY
                        global_ranking ASC,
                        uuid = '%s' DESC
                    LIMIT
                        5",
                QuizHallOfFame::SQL_TABLENAME,
                $db->escapeString( $clusterIdentifier ),
                $this->applicationObject->attribute( 'id' ),
                $i,
                $season,
                $this->user()->attribute('uuid')
            ));
            $results = array_merge($results, $chunks);
        }

        if( $results )
        {

            foreach( $results as $row )
            {
                if( $row['nb_correct'] == 0 && $row['nb_wrong'] == 0 )
                    continue;
                if( $row['global_ranking'] <= 10 && !in_array( $row['uuid'], $uuidsInGlobalLeaderboard ) && count( $uuidsInGlobalLeaderboard ) < 10 )
                {
                    $_globalLeaderboard[$row['global_ranking']][] = array(
                        'game_name'       => is_null( $row['game_name'] ) || $row['game_name'] == '' ? ezpI18n::tr( 'merck/quiz', 'ANONYMOUS' ) : $row['game_name'],
                        'specialty_label' => $ust[$row['user_specialty']],
                        'ranking'         => $row['global_ranking'],
                        'uuid'            => $row['uuid'],
                        'score'           => $row['score']
                    );
                    $uuidsInGlobalLeaderboard[] = $row['uuid'];
                }
                if( $row['uuid'] == $this->user()->attribute( 'uuid' ) && is_null( $playerData ) )
                {
                    $playerData = $row;
                }
            }
            if( !in_array( $this->user()->attribute( 'uuid' ), $uuidsInGlobalLeaderboard ) )
            {
                if( is_null( $playerData ) )
                {
                    $playerData = QuizHallOfFame::fetchUserInSeason( $this->applicationObject->attribute( 'id' ), $season, false, $this->isLocalQuiz() );
                }
                if( $playerData && ( $playerData['nb_correct'] == 0 && $playerData['nb_wrong'] == 0 ) )
                {
                    $playerData = null;
                }
            }
            $playersCount = $db->arrayQuery( sprintf("
                    SELECT
                        COUNT(*) as total,
                        user_specialty,
                        MAX(global_ranking) as lowest_global_ranking,
                        MAX(specialty_ranking) as lowest_specialty_ranking
                    FROM
                        %s
                    WHERE
                        cluster_identifier = '%s'
                        AND application_id = %d
                        AND ( nb_correct > 0 OR nb_wrong > 0 )
                        AND season = %d
                    GROUP BY
                        (user_specialty)",
                QuizHallOfFame::SQL_TABLENAME,
                $db->escapeString( $clusterIdentifier ),
                $this->applicationObject->attribute( 'id' ),
                $season
            ) );

            foreach( $playersCount as $row )
            {
                $totalPlayers += $row['total'];
                if( $row['user_specialty'] == $userSpecialtyTaxonomyId )
                {
                    $totalSpecialtyPlayers  = $row['total'];
                    $lowestSpecialtyRanking = $row['lowest_specialty_ranking'];
                }
                if( $row['lowest_global_ranking'] > $lowestGlobalRanking )
                    $lowestGlobalRanking = $row['lowest_global_ranking'];
            }
        }
        $tpl->setVariable( 'global_leaderboard', $_globalLeaderboard );
        $tpl->setVariable( 'current_player_uuid', $this->user()->attribute('uuid') );

        $response = array(
            'player_position' => ($playerData) ? $playerData['global_ranking'] : 0,
            'hall_of_fame'    => $tpl->fetch( 'design:esibuild/app_content/train-the-brain/hall_of_fame.tpl' ),
            'total_players'   => $totalPlayers,
        );

        return $response;
    }