Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add learners enrolment list api #143

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions futurex_openedx_extensions/dashboard/details/learners.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,64 @@ def get_learner_info_queryset(
update_removable_annotations(queryset, removable=['courses_count', 'certificates_count'])

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