Skip to content

Commit

Permalink
Apply improvement to limit database calls in leaderboard.
Browse files Browse the repository at this point in the history
Thanks to drgrice1.
  • Loading branch information
somiaj committed Dec 16, 2024
1 parent 68f77c4 commit 46b6785
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/WeBWorK/ContentGenerator/AchievementsLeaderboard.pm
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,20 @@ sub initialize ($c) {

my $level = $globalData->level_achievement_id ? $achievementsById{ $globalData->level_achievement_id } : '';

my %userAchievements = map { $_->achievement_id => $_ } $db->getUserAchievementsWhere({
user_id => $user->user_id,
achievement_id => [ map { $_->achievement_id } grep { $_->category ne 'level' } @achievements ],
});

my @badges;
for my $achievement (@achievements) {
# Skip level achievements and only show earned achievements.
last if $achievement->category eq 'level';

my $userBadge = $db->getUserAchievement($user->user_id, $achievement->achievement_id);
push(@badges, $achievement) if $userBadge && $achievement->enabled && $userBadge->earned;
push(@badges, $achievement)
if $userAchievements{ $achievement->achievement_id }
&& $achievement->enabled
&& $userAchievements{ $achievement->achievement_id }->earned;
}

push(@rows, [ $globalData->achievement_points || 0, $level, $user, \@badges ]);
Expand Down

0 comments on commit 46b6785

Please sign in to comment.