Skip to main content

get_games

Retrieves games (dilemmas) with optional filtering, including aggregated session statistics.

Signature

get_games($filters = [], $limit = null, $offset = 0)

Parameters

filters
array
default:"[]"
Associative array of filter criteria:
limit
integer
default:"null"
Maximum number of results. If null, returns all matching games.
offset
integer
default:"0"
Number of results to skip for pagination.

Returns

games
array
Array of game objects with the following properties:

Example Usage

// Get all published games
$games = get_games();

Implementation Details

This function performs a complex SQL query with multiple JOINs to aggregate session statistics. It groups results by game ID to provide summary metrics.
The function uses a LEFT JOIN with de_app_sessions to include games even if they have no sessions. Games without sessions will have sessions_count, total_score, total_duration, and users_count set to 0.

Query Structure

$sql = "SELECT 
    p.ID as id,
    p.post_title as name,
    p.post_name as slug,
    p.post_date as created_at,
    (SELECT meta_value FROM de_postmeta WHERE post_id = p.ID AND meta_key = '_dilema_fecha_activacion' LIMIT 1) as activation_date,
    p.post_status as status,
    p.guid as guid,
    MAX(c.post_title) as client,
    (SELECT gp.guid FROM de_posts gp WHERE gp.ID = (SELECT CAST(gpm.meta_value AS UNSIGNED) FROM de_postmeta gpm WHERE gpm.post_id = MAX(c.ID) AND gpm.meta_key = '_thumbnail_id' LIMIT 1)) as client_logo,
    (SELECT meta_value FROM de_postmeta WHERE post_id = p.ID AND (meta_key = 'dilemma_type' OR meta_key = '_tipo_dilema' OR meta_key = 'tipo') LIMIT 1) as type,
    COUNT(s.id) as sessions_count,
    SUM(s.score) as total_score,
    SUM(s.duration) as total_duration,
    COUNT(DISTINCT s.userId) as users_count
FROM de_posts p
LEFT JOIN de_postmeta pm ON p.ID = pm.post_id AND pm.meta_key = '_cliente_asociado'
LEFT JOIN de_posts c ON pm.meta_value = c.ID
LEFT JOIN de_app_sessions s ON p.ID = s.gameId
WHERE p.post_type = 'dilemas' AND p.post_status = 'publish'
GROUP BY p.ID
ORDER BY p.post_date DESC";

get_games_count

Returns the total count of games matching the specified filters.

Signature

get_games_count($filters = [])

Parameters

filters
array
default:"[]"
Same filter options as get_games()

Returns

count
integer
Total number of games matching the filters

Example Usage

$totalGames = get_games_count(['client' => 'Acme Corporation']);
echo "Total games: " . $totalGames;
When date filters (start_date or end_date) are provided, the function counts games that have sessions within that date range, not games created in that range.

get_game_details

Retrieves detailed information about a specific game, including aggregated statistics.

Signature

get_game_details($gameId)

Parameters

gameId
integer
required
Game ID

Returns

game
object
Game details object or null if not found:

Example Usage

$game = get_game_details(42);
if ($game) {
    echo "Game: " . $game['name'];
    echo "Started: " . $game['started'];
    echo "Finished: " . $game['finished'];
    echo "Completion Rate: " . round(($game['finished'] / $game['started']) * 100, 1) . "%";
}

get_game_users

Retrieves users who have played a specific game with session details.

Signature

get_game_users($gameId, $limit = 10, $offset = 0, $search = '', $status = '')

Parameters

gameId
integer
required
Game ID
limit
integer
default:"10"
Maximum number of results
offset
integer
default:"0"
Number of results to skip
search
string
default:"''"
Search by user name, email, or user ID
status
string
default:"''"
Filter by session status: ‘Finished’ or ‘In Progress’

Returns

users
array
Array of user session objects

Example Usage

// Get finished sessions for a game
$users = get_game_users(42, 20, 0, '', 'Finished');

// Search for specific user
$users = get_game_users(42, 10, 0, '[email protected]', '');

Sessions

Manage game session data

Clients

Client-related functions

Build docs developers (and LLMs) love