Skip to content

Commit

Permalink
feat: add learners enrollment list api
Browse files Browse the repository at this point in the history
  • Loading branch information
tehreem-sadat committed Nov 22, 2024
1 parent 2cc9465 commit 8c13c84
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 87 deletions.
62 changes: 62 additions & 0 deletions futurex_openedx_extensions/dashboard/details/learners.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from datetime import timedelta

from common.djangoapps.student.models import CourseEnrollment
from django.contrib.auth import get_user_model
from django.db.models import BooleanField, Case, Count, Exists, OuterRef, Q, Subquery, Value, When
from django.db.models.query import QuerySet
Expand Down Expand Up @@ -250,3 +251,64 @@ def get_learner_info_queryset(
).select_related('profile')

return queryset


def get_learners_enrollments_queryset(
user_ids: list = None, course_ids: list = None, include_staff: bool = False
) -> QuerySet:
"""
Get the enrollment details. If no course_ids or user_ids are provided,
all relevant data will be processed.
:param course_ids: List of course IDs to filter by (optional).
:param user_ids: List of user IDs to filter by (optional).
:param search_text: Text to filter users by (optional).
:param include_staff: Flag to include staff users (default: False).
:return: List of dictionaries containing user and course details.
"""

course_filter = Q(is_active=True)
if course_ids:
course_filter &= Q(course_id__in=course_ids)
if user_ids:
course_filter &= Q(user_id__in=user_ids)

queryset = CourseEnrollment.objects.filter(course_filter)

if not include_staff:
queryset = queryset.filter(
~check_staff_exist_queryset(ref_user_id='user_id', ref_org='course__org', ref_course_id='course_id'),
)

queryset = queryset.annotate(
certificate_available=Exists(
GeneratedCertificate.objects.filter(
user_id=OuterRef('user_id'),
course_id=OuterRef('course_id'),
status='downloadable'
)
)
).annotate(
course_score=Subquery(
PersistentCourseGrade.objects.filter(
user_id=OuterRef('user_id'),
course_id=OuterRef('course_id')
).values('percent_grade')[:1]
)
).annotate(
active_in_course=Case(
When(
Exists(
StudentModule.objects.filter(
student_id=OuterRef('user_id'),
course_id=OuterRef('course_id'),
modified__gte=timezone.now() - timedelta(days=30)
)
),
then=Value(True),
),
default=Value(False),
output_field=BooleanField(),
)
).select_related('user', 'user__profile')
return queryset
Loading

0 comments on commit 8c13c84

Please sign in to comment.