Skip to content

Commit

Permalink
Merge pull request #530 from edly-io/alisalman/add-user-leaderboard-s…
Browse files Browse the repository at this point in the history
…core

update: add user-score on the leaderbaord
  • Loading branch information
Ali-Salman29 authored Apr 16, 2024
2 parents 71fad04 + 90213f4 commit 9bd5b06
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
12 changes: 7 additions & 5 deletions lms/djangoapps/badges/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""
URLs for badges API
"""


from django.conf import settings
from django.urls import re_path
from django.urls import re_path, path, include

from .views import UserBadgeAssertions, LeaderboardView, VerfyTokenView

from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'leaderboard', LeaderboardView, basename='leaderboard')

urlpatterns = [
path('', include(router.urls)),
re_path('^assertions/user/' + settings.USERNAME_PATTERN + '/$',
UserBadgeAssertions.as_view(), name='user_assertions'),

re_path('leaderboard/', LeaderboardView.as_view(), name='leaderboard'),
re_path('verify-lms-token/', VerfyTokenView.as_view(), name='verify-lms-token'),
]
7 changes: 4 additions & 3 deletions lms/djangoapps/badges/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging

from django.conf import settings
from django.http import Http404
from django.db.models import Case, Count, IntegerField, Sum, Value, When
from django.utils.translation import gettext as _
from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser
Expand All @@ -15,7 +16,7 @@
from opaque_keys import InvalidKeyError
from opaque_keys.edx.django.models import CourseKeyField
from opaque_keys.edx.keys import CourseKey
from rest_framework import generics
from rest_framework import generics, viewsets
from rest_framework.exceptions import APIException
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
Expand Down Expand Up @@ -151,13 +152,13 @@ def get_queryset(self):
return queryset


class LeaderboardView(generics.ListAPIView):
class LeaderboardView(viewsets.ModelViewSet):
"""
Leaderboard List API View
"""
serializer_class = UserLeaderboardSerializer
queryset = LeaderboardEntry.objects.all().order_by('-score')

lookup_field = "user__username"

class VerfyTokenView(APIView):
"""
Expand Down
22 changes: 21 additions & 1 deletion lms/static/js/dashboard/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ var edx = edx || {};
return listItem;
};

edx.dashboard.leaderboard.renderUserScore = function () {
var userScoreElement = $("#leaderboard-user-score");
var usernmae = $("#leaderboard-username").html();
var userScoreURL = '/api/badges/v1/leaderboard/'.concat(usernmae);
var fetchingScoreData = false;

edx.dashboard.leaderboard.fetchData(userScoreURL)
.then(function (userScoreData) {
if (userScoreData && userScoreData.score){
userScoreElement.text(userScoreData.score);
}
})
.catch(function (error) {
console.error('Error fetching and rendering data:', error);
})
.finally(function () {
fetchingScoreData = false;
});
};

edx.dashboard.leaderboard.renderUserList = function () {
var userListElement = $('#userList');
var nextPageUrl = '/api/badges/v1/leaderboard/';
Expand Down Expand Up @@ -80,7 +100,7 @@ var edx = edx || {};
};

edx.dashboard.leaderboard.init = function() {
edx.dashboard.leaderboard.renderUserScore();
edx.dashboard.leaderboard.renderUserList();
}

}(jQuery));
8 changes: 7 additions & 1 deletion lms/templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,13 @@ <h3>${course_dir}</h3>

<div class="leaderboard">
<div class="leaderboard-header">
<div class="title">${_('Leaderboard')}</div>
<div>
<div class="title">${_('Leaderboard')}</div>
<div>
<div id="leaderboard-username" hidden>${user.username}</div>
<div>${_('score')}: <span id="leaderboard-user-score">0</span></div>
</div>
</div>
<!-- <span class="icon fa fa-remove" aria-hidden="true"></span> -->
<img class="star-icon" src="${static.url('images/stars.svg')}" alt="Leaderboard">
</div>
Expand Down

0 comments on commit 9bd5b06

Please sign in to comment.