|
| 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 opaque_keys.edx.keys import CourseKey |
| 14 | +from openedx.core import types |
| 15 | +from xmodule.partitions.partitions import Group, UserPartition, UserPartitionError |
| 16 | + |
| 17 | +from openedx_user_groups.models import UserGroup, UserGroupMembership |
| 18 | +from openedx_user_groups.toggles import is_user_groups_enabled |
| 19 | + |
| 20 | +log = logging.getLogger(__name__) |
| 21 | + |
| 22 | +# TODO: This is a temporary ID. We should use a more permanent ID. |
| 23 | +USER_GROUP_PARTITION_ID = 1000000000 |
| 24 | +USER_GROUP_SCHEME = "user_group" |
| 25 | + |
| 26 | + |
| 27 | +class UserGroupPartition(UserPartition): |
| 28 | + """ |
| 29 | + Extends UserPartition to support dynamic groups pulled from the new user |
| 30 | + groups system. |
| 31 | + """ |
| 32 | + |
| 33 | + @property |
| 34 | + def groups(self): |
| 35 | + """ |
| 36 | + Dynamically generate groups (based on user groups) for this partition. |
| 37 | + """ |
| 38 | + course_key = CourseKey.from_string(self.parameters["course_id"]) |
| 39 | + if not is_user_groups_enabled(course_key): |
| 40 | + return [] |
| 41 | + |
| 42 | + # TODO: Only get user groups for the course. |
| 43 | + user_groups = UserGroup.objects.filter(enabled=True) |
| 44 | + return [Group(user_group.id, str(user_group.name)) for user_group in user_groups] |
| 45 | + |
| 46 | + |
| 47 | +class UserGroupPartitionScheme: |
| 48 | + """Uses user groups to map learners into partition groups. |
| 49 | +
|
| 50 | + This scheme is only available if the ENABLE_USER_GROUPS waffle flag is enabled for the course. |
| 51 | +
|
| 52 | + This is how it works: |
| 53 | + - A only one user partition is created for each course with the `USER_GROUP_PARTITION_ID`. |
| 54 | + - A (Content) group is created for each user group in the course with the |
| 55 | + database user group ID as the group ID, and the user group name as the |
| 56 | + group name. |
| 57 | + - A user is assigned to a group if they are a member of the user group. |
| 58 | + """ |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def get_group_for_user( |
| 62 | + cls, course_key: CourseKey, user: types.User, user_partition: UserPartition |
| 63 | + ) -> list[Group] | None: |
| 64 | + """Get the (User) Group from the specified user partition for the user. |
| 65 | +
|
| 66 | + A user is assigned to the group via their user group membership and any |
| 67 | + mappings from user groups to partitions / groups that might exist. |
| 68 | +
|
| 69 | + Args: |
| 70 | + course_key (CourseKey): The course key. |
| 71 | + user (types.User): The user. |
| 72 | + user_partition (UserPartition): The user partition. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + List[Group]: The groups in the specified user partition for the user. |
| 76 | + None if the user is not a member of any group. |
| 77 | + """ |
| 78 | + if not is_user_groups_enabled(course_key): |
| 79 | + return None |
| 80 | + |
| 81 | + # TODO: A user could belong to multiple groups. This method assumes that |
| 82 | + # the user belongs to a single group. This should be renamed? |
| 83 | + if get_course_masquerade(user, course_key) and not is_masquerading_as_specific_student(user, course_key): |
| 84 | + return get_masquerading_user_group(course_key, user, user_partition) |
| 85 | + |
| 86 | + user_group_ids = UserGroupMembership.objects.filter(user=user, is_active=True).values_list( |
| 87 | + "group__id", flat=True |
| 88 | + ) |
| 89 | + all_user_groups: list[UserGroup] = UserGroup.objects.filter(enabled=True) |
| 90 | + |
| 91 | + if not user_group_ids: |
| 92 | + return None |
| 93 | + |
| 94 | + user_groups = [] |
| 95 | + for user_group in all_user_groups: |
| 96 | + if user_group.id in user_group_ids: |
| 97 | + user_groups.append(Group(user_group.id, str(user_group.name))) |
| 98 | + |
| 99 | + return user_groups |
| 100 | + |
| 101 | + # pylint: disable=redefined-builtin, invalid-name |
| 102 | + @classmethod |
| 103 | + def create_user_partition( |
| 104 | + cls, |
| 105 | + id: int, |
| 106 | + name: str, |
| 107 | + description: str, |
| 108 | + groups: list[Group] | None = None, |
| 109 | + parameters: dict | None = None, |
| 110 | + active: bool = True, |
| 111 | + ) -> UserPartition: |
| 112 | + """Create a custom UserPartition to support dynamic groups based on user groups. |
| 113 | +
|
| 114 | + A Partition has an id, name, scheme, description, parameters, and a |
| 115 | + list of groups. The id is intended to be unique within the context where |
| 116 | + these are used. (e.g., for partitions of users within a course, the ids |
| 117 | + should be unique per-course). |
| 118 | +
|
| 119 | + The scheme is used to assign users into groups. The parameters field is |
| 120 | + used to save extra parameters e.g., location of the course ID for this |
| 121 | + partition scheme. |
| 122 | +
|
| 123 | + Partitions can be marked as inactive by setting the "active" flag to False. |
| 124 | + Any group access rule referencing inactive partitions will be ignored |
| 125 | + when performing access checks. |
| 126 | +
|
| 127 | + Args: |
| 128 | + id (int): The id of the partition. |
| 129 | + name (str): The name of the partition. |
| 130 | + description (str): The description of the partition. |
| 131 | + groups (list of Group): The groups in the partition. |
| 132 | + parameters (dict): The parameters for the partition. |
| 133 | + active (bool): Whether the partition is active. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + UserGroupPartition: The user partition. |
| 137 | + """ |
| 138 | + course_key = CourseKey.from_string(parameters["course_id"]) |
| 139 | + if not is_user_groups_enabled(course_key): |
| 140 | + return None |
| 141 | + |
| 142 | + user_group_partition = UserGroupPartition( |
| 143 | + id, |
| 144 | + str(name), |
| 145 | + str(description), |
| 146 | + groups, |
| 147 | + cls, |
| 148 | + parameters, |
| 149 | + active=active, |
| 150 | + ) |
| 151 | + |
| 152 | + return user_group_partition |
| 153 | + |
| 154 | + |
| 155 | +def create_user_group_partition_with_course_id(course_id): |
| 156 | + """ |
| 157 | + Create and return the user group partition based only on course_id. |
| 158 | + If it cannot be created, None is returned. |
| 159 | + """ |
| 160 | + try: |
| 161 | + user_group_scheme = UserPartition.get_scheme(USER_GROUP_SCHEME) |
| 162 | + except UserPartitionError: |
| 163 | + log.warning(f"No {USER_GROUP_SCHEME} scheme registered, UserGroupPartition will not be created.") |
| 164 | + return None |
| 165 | + |
| 166 | + partition = user_group_scheme.create_user_partition( |
| 167 | + id=USER_GROUP_PARTITION_ID, |
| 168 | + name=_("User Groups"), |
| 169 | + description=_("Partition for segmenting users by user groups"), |
| 170 | + parameters={"course_id": str(course_id)}, |
| 171 | + ) |
| 172 | + |
| 173 | + return partition |
| 174 | + |
| 175 | + |
| 176 | +def create_user_group_partition(course): |
| 177 | + """ |
| 178 | + Get the dynamic user group user partition based on the user groups of the course. |
| 179 | + """ |
| 180 | + if not is_user_groups_enabled(course.id): |
| 181 | + return [] |
| 182 | + |
| 183 | + return create_user_group_partition_with_course_id(course.id) |
0 commit comments