Skip to content

Commit

Permalink
fix: get_learning_hours_count exclude staff by default
Browse files Browse the repository at this point in the history
  • Loading branch information
shadinaif committed Jan 16, 2025
1 parent b194ac9 commit 6f06031
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
36 changes: 27 additions & 9 deletions futurex_openedx_extensions/dashboard/statistics/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ def get_certificates_count(


def get_learning_hours_count(
fx_permission_info: dict, visible_courses_filter: bool | None = True, active_courses_filter: bool | None = None
fx_permission_info: dict,
visible_courses_filter: bool | None = True,
active_courses_filter: bool | None = None,
include_staff: bool | None = None,
) -> int:
"""
Get the count of learning hours in the given tenants. The count is grouped by course_effort. Certificates
Expand All @@ -78,6 +81,8 @@ def get_learning_hours_count(
:type visible_courses_filter: bool | None
:param active_courses_filter: Value to filter courses on active status. None means no filter.
:type active_courses_filter: bool | None
:param include_staff: Include staff members in the count
:type include_staff: bool | None
:return: Count of certificates per organization
:rtype: Dict[str, int]
"""
Expand Down Expand Up @@ -117,15 +122,28 @@ def parse_course_effort(effort: str, course_id: str) -> float:
)
return settings.FX_DEFAULT_COURSE_EFFORT

queryset = GeneratedCertificate.objects.filter(
status='downloadable',
course_id__in=get_base_queryset_courses(
fx_permission_info,
visible_filter=visible_courses_filter,
active_filter=active_courses_filter,
),
)

if not include_staff:
queryset = queryset.annotate(
course_org=Subquery(
CourseOverview.objects.filter(id=OuterRef('course_id')).values('org')
)
).filter(
~check_staff_exist_queryset(
ref_user_id='user_id', ref_org='course_org', ref_course_id='course_id',
)
)

result = list(
GeneratedCertificate.objects.filter(
status='downloadable',
course_id__in=get_base_queryset_courses(
fx_permission_info,
visible_filter=visible_courses_filter,
active_filter=active_courses_filter,
),
).annotate(
queryset.annotate(
course_effort=Subquery(
CourseOverview.objects.filter(
id=OuterRef('course_id')
Expand Down
5 changes: 3 additions & 2 deletions futurex_openedx_extensions/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def _get_learners_count_data(one_tenant_permission_info: dict, include_staff: bo
return get_learners_count(one_tenant_permission_info, include_staff=include_staff)

@staticmethod
def _get_learning_hours_count_data(one_tenant_permission_info: dict) -> int:
def _get_learning_hours_count_data(one_tenant_permission_info: dict, include_staff: bool) -> int:
"""Get the count of learning_hours for the given tenant"""
return get_learning_hours_count(one_tenant_permission_info)
return get_learning_hours_count(one_tenant_permission_info, include_staff=include_staff)

def _get_stat_count(self, stat: str, tenant_id: int, include_staff: bool) -> int:
"""Get the count of the given stat for the given tenant"""
Expand All @@ -169,6 +169,7 @@ def _get_stat_count(self, stat: str, tenant_id: int, include_staff: bool) -> int
elif stat == self.STAT_LEARNING_HOURS:
result = self._get_learning_hours_count_data(
one_tenant_permission_info,
include_staff=include_staff,
)

else:
Expand Down
13 changes: 8 additions & 5 deletions tests/test_dashboard/test_statistics/test_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,23 @@ def test_get_certificates_count_not_downloadable(base_data, fx_permission_info):

@pytest.mark.django_db
@override_settings(FX_DEFAULT_COURSE_EFFORT=10)
@pytest.mark.parametrize('tenant_ids, expected_result', [
([1], 14 * 10),
([2], 9 * 10),
([3], 0 * 10),
@pytest.mark.parametrize('tenant_ids, expected_result, expected_result_with_staff', [
([1], 11 * 10, 14 * 10),
([2], 8 * 10, 9 * 10),
([3], 0 * 10, 0 * 10),
])
def test_get_learning_hours_count_for_default_course_effort(
base_data, fx_permission_info, tenant_ids, expected_result
base_data, fx_permission_info, tenant_ids, expected_result, expected_result_with_staff,
): # pylint: disable=unused-argument
"""Verify get_learning_hours_count function."""
fx_permission_info['view_allowed_full_access_orgs'] = get_tenants_orgs(tenant_ids)
fx_permission_info['view_allowed_any_access_orgs'] = get_tenants_orgs(tenant_ids)
result = certificates.get_learning_hours_count(fx_permission_info)
assert result == expected_result, \
f'Wrong learning hours count for tenant(s) {tenant_ids}. expected: {expected_result}, got: {result}'
result = certificates.get_learning_hours_count(fx_permission_info, include_staff=True)
assert result == expected_result_with_staff, \
f'Wrong learning hours count for tenant(s) {tenant_ids}. expected: {expected_result}, got: {result}'


@pytest.mark.django_db
Expand Down
8 changes: 4 additions & 4 deletions tests/test_dashboard/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,26 @@ def test_all_stats(self):
self.assertDictEqual(json.loads(response.content), {
'1': {
'certificates_count': 11, 'courses_count': 12, 'hidden_courses_count': 0,
'learners_count': 16, 'enrollments_count': 26, 'learning_hours_count': 280
'learners_count': 16, 'enrollments_count': 26, 'learning_hours_count': 220
},
'2': {
'certificates_count': 8, 'courses_count': 5, 'hidden_courses_count': 0,
'learners_count': 21, 'enrollments_count': 21, 'learning_hours_count': 180
'learners_count': 21, 'enrollments_count': 21, 'learning_hours_count': 160
},
'3': {
'certificates_count': 0, 'courses_count': 1, 'hidden_courses_count': 0,
'learners_count': 6, 'enrollments_count': 4, 'learning_hours_count': 0
},
'7': {
'certificates_count': 6, 'courses_count': 3, 'hidden_courses_count': 0,
'learners_count': 17, 'enrollments_count': 14, 'learning_hours_count': 140
'learners_count': 17, 'enrollments_count': 14, 'learning_hours_count': 120
},
'8': {
'certificates_count': 2, 'courses_count': 2, 'hidden_courses_count': 0,
'learners_count': 9, 'enrollments_count': 7, 'learning_hours_count': 40
},
'total_certificates_count': 27, 'total_courses_count': 23, 'total_hidden_courses_count': 0,
'total_learners_count': 69, 'total_enrollments_count': 72, 'total_learning_hours_count': 640,
'total_learners_count': 69, 'total_enrollments_count': 72, 'total_learning_hours_count': 540,
'total_unique_learners': 37, 'limited_access': False
})

Expand Down

0 comments on commit 6f06031

Please sign in to comment.