app\models\Contest::defaultJson PHP Method

defaultJson() public method

public defaultJson ( $user = null )
    public function defaultJson($user = null)
    {
        $includes = ['entries'];
        if ($this->type === 'art') {
            $includes[] = 'entries.artMeta';
        }
        if ($this->show_votes) {
            $includes[] = 'entries.results';
        }
        $contestJson = json_item($this, new ContestTransformer(), $includes);
        if (!empty($contestJson['entries'])) {
            if ($this->show_votes) {
                // Sort results by number of votes desc
                usort($contestJson['entries'], function ($a, $b) {
                    if ($a['results']['votes'] === $b['results']['votes']) {
                        return 0;
                    }
                    return $a['results']['votes'] > $b['results']['votes'] ? -1 : 1;
                });
            } else {
                // We want the results to appear randomized to the user but be
                // deterministic (i.e. we don't want the rows shuffling each time
                // the user votes), so we seed based on user_id
                $seed = $user ? $user->user_id : time();
                seeded_shuffle($contestJson['entries'], $seed);
            }
        }
        return json_encode(['contest' => $contestJson, 'userVotes' => $this->votesForUser($user)], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
    }