|
| 1 | +""" |
| 2 | +Provides a UserPartition driver for user groups. |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | + |
| 7 | +from django.utils.translation import gettext_lazy as _ |
| 8 | +from lms.djangoapps.courseware.masquerade import ( |
| 9 | + get_course_masquerade, |
| 10 | + get_masquerading_user_group, |
| 11 | + is_masquerading_as_specific_student, |
| 12 | +) |
| 13 | +from xmodule.partitions.partitions import Group, UserPartition, UserPartitionError |
| 14 | + |
| 15 | +from openedx_user_groups.models import UserGroup, UserGroupMembership |
| 16 | + |
| 17 | +log = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +USER_GROUP_PARTITION_ID = 1000000000 |
| 21 | +USER_GROUP_SCHEME = "user_group" |
| 22 | + |
| 23 | + |
| 24 | +class UserGroupPartition(UserPartition): |
| 25 | + """ |
| 26 | + Extends UserPartition to support dynamic groups pulled from the new user |
| 27 | + groups system. |
| 28 | + """ |
| 29 | + |
| 30 | + @property |
| 31 | + def groups(self): |
| 32 | + """ |
| 33 | + Dynamically generate groups (based on user groups) for this partition. |
| 34 | + """ |
| 35 | + # TODO: Only get user groups for the course. |
| 36 | + user_groups = UserGroup.objects.all() |
| 37 | + return [Group(user_group.id, str(user_group.name)) for user_group in user_groups] |
| 38 | + |
| 39 | + |
| 40 | +class UserGroupPartitionScheme: |
| 41 | + """Uses user groups to map learners into partition groups. |
| 42 | +
|
| 43 | + - A user partition is created for each user group in the course with a |
| 44 | + unused partition ID generated in runtime by using generate_int_id() with |
| 45 | + min=MINIMUM_STATIC_PARTITION_ID and max=MYSQL_MAX_INT. |
| 46 | + - A (User) group is created for each user group in the course with the |
| 47 | + database user group ID as the group ID, and the user group name as the |
| 48 | + group name. |
| 49 | + - A user is assigned to a group if they are a member of the user group. |
| 50 | + """ |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def get_group_for_user(cls, course_key, user, user_partition): |
| 54 | + """Get the (User) Group from the specified user partition for the user. |
| 55 | +
|
| 56 | + A user is assigned to the group via their user group membership and any |
| 57 | + mappings from user groups to partitions / groups that might exist. |
| 58 | +
|
| 59 | + Args: |
| 60 | + course_key (CourseKey): The course key. |
| 61 | + user (User): The user. |
| 62 | + user_partition (UserPartition): The user partition. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + Group: The group in the specified user partition |
| 66 | + """ |
| 67 | + # TODO: A user could belong to multiple groups. This method assumes that |
| 68 | + # the user belongs to a single group. This should be renamed? |
| 69 | + if get_course_masquerade(user, course_key) and not is_masquerading_as_specific_student(user, course_key): |
| 70 | + return get_masquerading_user_group(course_key, user, user_partition) |
| 71 | + |
| 72 | + user_group_ids = UserGroupMembership.objects.filter(user=user).values_list("group__id", flat=True) |
| 73 | + all_user_groups: list[UserGroup] = UserGroup.objects.all() |
| 74 | + |
| 75 | + if not user_group_ids: |
| 76 | + return None |
| 77 | + |
| 78 | + user_groups = [] |
| 79 | + for user_group in all_user_groups: |
| 80 | + if user_group.id in user_group_ids: |
| 81 | + user_groups.append(Group(user_group.id, str(user_group.name))) |
| 82 | + |
| 83 | + return user_groups |
| 84 | + |
| 85 | + @classmethod |
| 86 | + def create_user_partition(cls, id, name, description, groups=None, parameters=None, active=True): # pylint: disable=redefined-builtin, invalid-name |
| 87 | + """Create a custom UserPartition to support dynamic groups based on user groups. |
| 88 | +
|
| 89 | + A Partition has an id, name, scheme, description, parameters, and a |
| 90 | + list of groups. The id is intended to be unique within the context where |
| 91 | + these are used. (e.g., for partitions of users within a course, the ids |
| 92 | + should be unique per-course). |
| 93 | +
|
| 94 | + The scheme is used to assign users into groups. The parameters field is |
| 95 | + used to save extra parameters e.g., location of the course ID for this |
| 96 | + partition scheme. |
| 97 | +
|
| 98 | + Partitions can be marked as inactive by setting the "active" flag to False. |
| 99 | + Any group access rule referencing inactive partitions will be ignored |
| 100 | + when performing access checks. |
| 101 | +
|
| 102 | + Args: |
| 103 | + id (int): The id of the partition. |
| 104 | + name (str): The name of the partition. |
| 105 | + description (str): The description of the partition. |
| 106 | + groups (list of Group): The groups in the partition. |
| 107 | + parameters (dict): The parameters for the partition. |
| 108 | + active (bool): Whether the partition is active. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + UserGroupPartition: The user partition. |
| 112 | + """ |
| 113 | + user_group_partition = UserGroupPartition( |
| 114 | + id, |
| 115 | + str(name), |
| 116 | + str(description), |
| 117 | + groups, |
| 118 | + cls, |
| 119 | + parameters, |
| 120 | + active=active, |
| 121 | + ) |
| 122 | + |
| 123 | + return user_group_partition |
| 124 | + |
| 125 | + |
| 126 | +def create_user_group_partition_with_course_id(course_id): |
| 127 | + """ |
| 128 | + Create and return the user group partition based only on course_id. |
| 129 | + If it cannot be created, None is returned. |
| 130 | + """ |
| 131 | + try: |
| 132 | + user_group_scheme = UserPartition.get_scheme(USER_GROUP_SCHEME) |
| 133 | + except UserPartitionError: |
| 134 | + log.warning(f"No {USER_GROUP_SCHEME} scheme registered, UserGroupPartition will not be created.") |
| 135 | + return None |
| 136 | + |
| 137 | + partition = user_group_scheme.create_user_partition( |
| 138 | + id=USER_GROUP_PARTITION_ID, |
| 139 | + name=_("User Groups"), |
| 140 | + description=_("Partition for segmenting users by user groups"), |
| 141 | + parameters={"course_id": str(course_id)}, |
| 142 | + ) |
| 143 | + |
| 144 | + return partition |
| 145 | + |
| 146 | + |
| 147 | +def create_user_group_partition(course): |
| 148 | + """ |
| 149 | + Get the dynamic enrollment track user partition based on the user groups of the course. |
| 150 | + """ |
| 151 | + return create_user_group_partition_with_course_id(course.id) |
0 commit comments