Changeset 1137 for trunk/common


Ignore:
Timestamp:
04/25/10 17:47:20 (2 years ago)
Author:
bogdan2412
Message:

Fix round_recompute_score function.

It now correctly handles the case where some user's score is present
in ia_score_user_round_task but not in ia_score_user_round. Also
does less DB queries now.

File:
1 edited

Legend:

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

    r1132 r1137  
    5757 
    5858function round_recompute_score($round_id) { 
    59     $query = "SELECT * FROM `ia_score_user_round` 
    60         WHERE `round_id` = ".db_quote($round_id); 
     59    db_query("DELETE FROM ia_score_user_round 
     60        WHERE round_id = ".db_quote($round_id)); 
     61    $query = "SELECT SUM(`score`) AS score, user_id 
     62        FROM ia_score_user_round_task 
     63        WHERE `round_id` = ".db_quote($round_id)." 
     64        GROUP BY `user_id`"; 
    6165    $rows = db_fetch_all($query); 
    62     $users = array(); 
    63     foreach ($rows as $row) { 
    64       $users[] = $row['user_id']; 
    65     } 
    66  
    67     $query = "SELECT SUM(`score`) AS score, user_id 
    68             FROM ia_score_user_round_task 
    69             WHERE `user_id` IN ('".implode("', '", $users)."') AND 
    70                   `round_id` = ".db_quote($round_id)." 
    71             GROUP BY `user_id`"; 
    72     $rows = db_fetch_all($query); 
     66    if (empty($rows)) { 
     67        return false; 
     68    } 
     69    $query = "INSERT INTO `ia_score_user_round` 
     70        (`user_id`, `round_id`, `score`) 
     71        VALUES "; 
     72    $first = true; 
    7373    foreach($rows as $row) { 
    7474        $user_id = $row['user_id']; 
    7575        $score = $row['score']; 
    7676 
    77         $query = "UPDATE `ia_score_user_round` 
    78                     SET `score` = ".db_quote($score)." 
    79                     WHERE `user_id` = ".db_quote($user_id)." && 
    80                           `round_id` = ".db_quote($round_id); 
    81         db_query($query); 
    82     } 
     77        if (!$first) { 
     78            $query .= ", "; 
     79        } else { 
     80            $first = false; 
     81        } 
     82        $query .= sprintf("(%s, %s, %s)", 
     83            db_quote($user_id), db_quote($round_id), db_quote($score)); 
     84    } 
     85    db_query($query); 
    8386} 
    8487 
Note: See TracChangeset for help on using the changeset viewer.