Changeset 1116 for trunk/common


Ignore:
Timestamp:
03/21/10 15:45:34 (2 years ago)
Author:
bogdan2412
Message:

Make rankings work slightly better for single rounds.

Added a round_id, score, user_id index to ia_score_user_round.
Make MySQL use that index for single round rankings by removing
the GROUP BY and SUM(score).

Moved identity_can from common/ into the macro, where it's supposed
to be.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/common/db/score.php

    r1110 r1116  
    7878    } 
    7979    if ($round != null) { 
    80         if (is_string($round)) 
     80        if (is_string($round)) { 
    8181            $round = array($round); 
    82  
    83         if (is_array($round) && count($round) > 0) { 
    84             $rounds_where = "(`id` IN (" . db_escape_array($round) . "))"; 
    85             $query = "SELECT `id`, `state`, `public_eval` FROM `ia_round` WHERE " . $rounds_where; 
    86             $round_objects = db_fetch_all($query); 
    87             $allowed_round_ids = array(); 
    88  
    89             foreach ($round_objects as $round) { 
    90                 if (identity_can('round-view-scores', $round)) { 
    91                     $allowed_round_ids[] = $round["id"]; 
    92                 } 
    93             } 
    94  
    95             if (count($allowed_round_ids) == 0) { 
     82        } 
     83 
     84        if (is_array($round)) { 
     85            if (count($round) > 0) { 
     86                $where[] = "(`round_id` IN (" . db_escape_array($round) . "))"; 
     87            } else { 
    9688                $where[] = "(TRUE = FALSE)"; 
    97             } else { 
    98                 $where[] = "(`round_id` IN (" . db_escape_array($allowed_round_ids) . "))"; 
    9989            } 
    10090        } 
     
    370360// if detail_task == true, extra columns for each task will be created 
    371361// if detail_round == true, extra columns for each round will be created 
    372 function score_get_rankings($rounds, $tasks, $start = 0, $count = 999999, $detail_task = false, $detail_round = false) 
    373 { 
     362function score_get_rankings($rounds, $tasks, $start = 0, $count = 999999, 
     363                            $detail_task = false, $detail_round = false) { 
     364    if (count($rounds) == 0) { 
     365        return array(); 
     366    } 
    374367    $where = score_build_where_clauses(null, null, $rounds); 
    375368 
    376369    // Get the total score for all rounds 
    377     $query = "SELECT SUM(score) AS score, user_id, ia_user.username AS user_name, ia_user.full_name AS user_full, 
    378                     ia_user.rating_cache AS user_rating 
    379                 FROM ia_score_user_round 
    380                 LEFT JOIN ia_user ON ia_user.id = ia_score_user_round.user_id 
    381                 WHERE ".implode('AND', $where)." 
    382                 GROUP BY `user_id` 
    383                 ORDER BY score DESC 
    384                 LIMIT ".db_escape($start).", ".db_escape($count); 
     370    $query = " 
     371        SELECT ".(count($rounds) > 1 ? "SUM(score) AS score" : "score").", 
     372                user_id, ia_user.username AS user_name, 
     373                ia_user.full_name AS user_full, 
     374                ia_user.rating_cache AS user_rating 
     375        FROM ia_score_user_round 
     376        LEFT JOIN ia_user ON ia_user.id = ia_score_user_round.user_id 
     377        WHERE".implode('AND', $where)." 
     378        ".(count($rounds) > 1 ? "GROUP BY `user_id`" : "")." 
     379        ORDER BY score DESC 
     380        LIMIT ".db_escape($start).", ".db_escape($count); 
    385381 
    386382    $rankings = db_fetch_all($query); 
Note: See TracChangeset for help on using the changeset viewer.