Skip to content

Commit

Permalink
Add CourseService.get_members
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Jun 17, 2024
1 parent 6856d22 commit eee7fa4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
21 changes: 21 additions & 0 deletions lms/services/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
CourseGroupsExportedFromH,
Grouping,
GroupingMembership,
LTIRole,
Organization,
RoleScope,
RoleType,
User,
)
from lms.product import Product
Expand Down Expand Up @@ -329,6 +332,24 @@ def get_assignments(

return self._db.scalars(assignments_query).all()

def get_members(
self, course: Course, role_type: RoleType, role_scope: RoleScope
) -> list[User]:
return self._db.scalars(
select(User)
.join(AssignmentMembership, User.id == AssignmentMembership.user_id)
.join(LTIRole)
.join(
AssignmentGrouping,
AssignmentMembership.assignment_id == AssignmentGrouping.assignment_id,
)
.where(
AssignmentGrouping.grouping_id == course.id,
LTIRole.scope == role_scope,
LTIRole.type == role_type,
)
).all()

def _get_authority_provided_id(self, context_id):
return self._grouping_service.get_authority_provided_id(
lms_id=context_id, type_=Grouping.Type.COURSE
Expand Down
30 changes: 29 additions & 1 deletion tests/unit/lms/services/course_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from h_matchers import Any
from sqlalchemy.exc import NoResultFound

from lms.models import ApplicationSettings, CourseGroupsExportedFromH, Grouping
from lms.models import (
ApplicationSettings,
CourseGroupsExportedFromH,
Grouping,
RoleScope,
)
from lms.product.product import Product
from lms.services.course import CourseService, course_service_factory
from tests import factories
Expand Down Expand Up @@ -338,6 +343,29 @@ def test_is_member(self, svc, db_session):
assert svc.is_member(course, user.h_userid)
assert not svc.is_member(course, other_user.h_userid)

def test_get_members(self, svc, db_session):
factories.User() # User not in assignment
assignment = factories.Assignment()
user = factories.User()
course = factories.Course()
lti_role = factories.LTIRole(scope=RoleScope.COURSE)
factories.AssignmentMembership.create(
assignment=assignment, user=user, lti_role=lti_role
)
# User in assignment with other role
factories.AssignmentMembership.create(
assignment=assignment,
user=factories.User(),
lti_role=factories.LTIRole(scope=RoleScope.SYSTEM),
)
factories.AssignmentGrouping(grouping=course, assignment=assignment)

db_session.flush()

assert svc.get_members(
course, role_scope=lti_role.scope, role_type=lti_role.type
) == [user]

def test_get_organization_courses_deduplicates(self, db_session, svc):
org = factories.Organization()

Expand Down

0 comments on commit eee7fa4

Please sign in to comment.