Commit 503e088a authored by Tim Honcoop's avatar Tim Honcoop
Browse files

Submission ratio for paying learners added

parent 0a80ff66
Pipeline #2227 passed with stage
in 2 minutes and 15 seconds
from functools import partial
from django.db.models import Count, Q
from django.db.models.functions import Coalesce
from django.db.models import Count, Q, Subquery
from django.db.models.functions import Coalesce, Cast
from django.utils.functional import cached_property
from rest_framework import serializers
from coursera.utils import CountSubquery, NullIf
from coursera.filters import ClickstreamEventFilterSet, GenericFilterSet
from coursera.models import (
......@@ -12,6 +13,9 @@ from coursera.models import (
Item,
ItemGrade,
ItemRating,
PeerSubmission,
CertificatePayment,
CourseMembership,
)
__all__ = [
......@@ -398,10 +402,32 @@ class AssignmentAnalyticsSerializer(AssignmentSerializer):
fields = AssignmentSerializer.Meta.fields + [
"next_item",
"next_assignment",
"submission_ratio_paying_learners"
]
next_item = serializers.SerializerMethodField()
next_assignment = serializers.SerializerMethodField()
submission_ratio_paying_learners = serializers.SerializerMethodField()
@cached_property
def filter(self):
"""
Return a partial that applies the GenericFilterSet to the passed
queryset.
Requires the request object to be in the context.
"""
def get_filterset(data=None, queryset=None, *, request=None, prefix=None):
"""
Apply the GenericFilterSet to the queryset, and return the
filtered queryset.
"""
return GenericFilterSet(data, queryset, request=request, prefix=prefix).qs
return partial(
get_filterset, self.context["request"].GET, request=self.context["request"]
)
def get_next_item(self, obj):
"""
......@@ -440,3 +466,33 @@ class AssignmentAnalyticsSerializer(AssignmentSerializer):
}
except IndexError:
return {"item_id": "", "type": 0, "category": ""}
def get_submission_ratio_paying_learners(self, obj):
"""
Return the submission ratio for paying learners.
"""
paying_learners_list = CertificatePayment.objects.filter(
course=obj.branch.course
).values_list("eitdigital_user_id")
number_of_submissions = self.filter(
PeerSubmission.objects.filter(
peer_assignment__items=obj.id,
eitdigital_user_id__in=paying_learners_list
)
).values_list("eitdigital_user_id").distinct().count()
number_of_payers = self.filter(
CourseMembership.objects.filter(
course=obj.branch.course,
eitdigital_user_id__in=paying_learners_list
)
).values_list("eitdigital_user_id").distinct().count()
if number_of_payers == 0:
return 0
return number_of_submissions / number_of_payers
......@@ -994,6 +994,7 @@ def test_assignment_analytics_view(
- average_grade
- next_item
- next_assignment
- submission_ratio_paying_learners
"""
response = teacher_api_client.get(
reverse(
......@@ -1001,7 +1002,7 @@ def test_assignment_analytics_view(
kwargs={"course_id": coursera_course_id, "item_id": coursera_assignment_id},
)
)
keys = ["id", "branch", "item_id", "lesson", "lesson_name", "order", "type", "name", "optional", "submissions", "submission_ratio", "average_grade", "next_item", "next_assignment"]
keys = ["id", "branch", "item_id", "lesson", "lesson_name", "order", "type", "name", "optional", "submissions", "submission_ratio", "average_grade", "next_item", "next_assignment", "submission_ratio_paying_learners"]
assert response.status_code == 200, str(response.content)
assert list(response.data.keys()) == keys
......@@ -1019,7 +1020,7 @@ def test_assignment_analytics_view_next_assignment(
kwargs={"course_id": "V4m7Xf5qEeS9ISIACxWDhA", "item_id": "PoQSi"},
)
)
keys = ["id", "branch", "item_id", "lesson", "lesson_name", "order", "type", "name", "optional", "submissions", "submission_ratio", "average_grade", "next_item", "next_assignment"]
keys = ["id", "branch", "item_id", "lesson", "lesson_name", "order", "type", "name", "optional", "submissions", "submission_ratio", "average_grade", "next_item", "next_assignment", "submission_ratio_paying_learners"]
assert response.status_code == 200, str(response.content)
assert list(response.data.keys()) == keys
......@@ -1105,7 +1106,8 @@ def test_assignment_analytics_view_invalid_date_filter(
"submission_ratio",
"average_grade",
"next_item",
"next_assignment"
"next_assignment",
"submission_ratio_paying_learners"
]
assert response.status_code == 200, str(response.content)
assert list(response.data.keys()) == keys
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment