Skip to content

Commit

Permalink
Merge pull request openedx#180 from edx-solutions/ziafazal/api-fix-bu…
Browse files Browse the repository at this point in the history
…g-progress-tab

ziafazal/api-fix-bug-progress-tab: Made no of completions as percentage
  • Loading branch information
mattdrayer committed Aug 13, 2014
2 parents ba5370f + 4890db5 commit 2652679
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
15 changes: 13 additions & 2 deletions lms/djangoapps/api_manager/courses/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_points_scored(self, obj):
formats points_scored to two decimal points
"""
points_scored = obj['points_scored'] or 0
return round(points_scored, 2)
return int(round(points_scored))


class CourseCompletionsLeadersSerializer(serializers.Serializer):
Expand All @@ -43,7 +43,18 @@ class CourseCompletionsLeadersSerializer(serializers.Serializer):
username = serializers.CharField(source='user__username')
title = serializers.CharField(source='user__profile__title')
avatar_url = serializers.CharField(source='user__profile__avatar_url')
completions = serializers.IntegerField()
completions = serializers.SerializerMethodField('get_completion_percentage')

def get_completion_percentage(self, obj):
"""
formats get completions as percentage
"""
total_completions = self.context['total_completions'] or 0
completions = obj['completions'] or 0
completion_percentage = 0
if total_completions > 0:
completion_percentage = int(round(100 * completions / total_completions))
return completion_percentage


class CourseSerializer(serializers.Serializer):
Expand Down
4 changes: 2 additions & 2 deletions lms/djangoapps/api_manager/courses/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ def test_courses_leaders_list_get(self):
self.assertEqual(len(response.data['leaders']), 3)
self.assertEqual(response.data['course_avg'], 3.4)
self.assertEqual(response.data['position'], 2)
self.assertEqual(response.data['points'], 4.5)
self.assertEqual(response.data['points'], 5)

# Filter by user who has never accessed a course module
test_user = UserFactory.create(username="testusernocoursemod")
Expand Down Expand Up @@ -1656,7 +1656,7 @@ def test_courses_completions_leaders_list_get(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['leaders']), 3)
self.assertEqual(response.data['position'], 1)
self.assertEqual(response.data['completions'], 10)
self.assertEqual(response.data['completions'], 40)

# test with bogus course
test_uri = '{}/{}/metrics/completions/leaders/'.format(self.base_courses_uri, self.test_bogus_course_id)
Expand Down
14 changes: 9 additions & 5 deletions lms/djangoapps/api_manager/courses/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ def get(self, request, course_id): # pylint: disable=W0613,W0221
filter(points__gt=user_points).exclude(student__id=user_id).count() # excluding user to overcome
# float comparison bug
data['position'] = users_above + 1
data['points'] = user_points
data['points'] = int(round(user_points))

points = queryset.aggregate(total=Sum('grade'))
if points and points['total'] is not None:
Expand Down Expand Up @@ -1607,24 +1607,28 @@ def get(self, request, course_id): # pylint: disable=W0613
exclude_users = _get_aggregate_exclusion_user_ids(course_key)
queryset = CourseModuleCompletion.objects.filter(course_id=course_key)\
.exclude(user__in=exclude_users)
total_completions = queryset.filter(user__is_active=True).count()

if user_id:
user_completions = queryset.filter(user__id=user_id).count()
completions_above_user = queryset.filter(user__is_active=True).values('user__id')\
.annotate(completions=Count('content_id')).filter(completions__gt=user_completions).count()
data['position'] = completions_above_user + 1
data['completions'] = user_completions
completion_percentage = 0
if total_completions > 0:
completion_percentage = int(round(100 * user_completions/total_completions))
data['completions'] = completion_percentage

total_completions = queryset.filter(user__is_active=True).count()
total_users = CourseEnrollment.users_enrolled_in(course_key).exclude(id__in=exclude_users).count()
if total_users:
course_avg = round(total_completions / float(total_users), 1)
data['course_avg'] = course_avg

queryset = queryset.filter(user__is_active=True).values('user__id', 'user__username', 'user__profile__title',
'user__profile__avatar_url')\
.annotate(completions=Count('content_id')).order_by('-completions')[:count]
serializer = CourseCompletionsLeadersSerializer(queryset, many=True)
.annotate(completions=Count('content_id')).order_by('-completions')[:count]
serializer = CourseCompletionsLeadersSerializer(queryset, many=True,
context={'total_completions': total_completions})
data['leaders'] = serializer.data # pylint: disable=E1101
return Response(data, status=status.HTTP_200_OK)

Expand Down

0 comments on commit 2652679

Please sign in to comment.