mixin.py 7.34 KB
Newer Older
Calen Pennington committed
1 2 3
"""
Namespace that defines fields common to all blocks used in the LMS
"""
4 5
from lazy import lazy

6 7 8
from xblock.fields import Boolean, Scope, String, XBlockMixin, Dict
from xblock.validation import ValidationMessage
from xmodule.modulestore.inheritance import UserPartitionList
9
from xmodule.partitions.partitions import NoSuchUserPartitionError, NoSuchUserPartitionGroupError
Calen Pennington committed
10

11 12 13
# Make '_' a no-op so we can scrape strings
_ = lambda text: text

Calen Pennington committed
14

15 16 17 18 19 20 21 22 23 24 25
class GroupAccessDict(Dict):
    """Special Dict class for serializing the group_access field"""
    def from_json(self, access_dict):
        if access_dict is not None:
            return {int(k): access_dict[k] for k in access_dict}

    def to_json(self, access_dict):
        if access_dict is not None:
            return {unicode(k): access_dict[k] for k in access_dict}


Calen Pennington committed
26 27 28 29 30
class LmsBlockMixin(XBlockMixin):
    """
    Mixin that defines fields common to all blocks used in the LMS
    """
    hide_from_toc = Boolean(
31
        help=_("Whether to display this module in the table of contents"),
Calen Pennington committed
32 33 34 35
        default=False,
        scope=Scope.settings
    )
    format = String(
36 37 38
        # Translators: "TOC" stands for "Table of Contents"
        help=_("What format this module is in (used for deciding which "
               "grader to apply, and what to show in the TOC)"),
Calen Pennington committed
39 40
        scope=Scope.settings,
    )
41
    chrome = String(
42 43
        display_name=_("Courseware Chrome"),
        help=_("Enter the chrome, or navigation tools, to use for the XBlock in the LMS. Valid values are: \n"
44 45 46 47
               "\"chromeless\" -- to not use tabs or the accordion; \n"
               "\"tabs\" -- to use tabs only; \n"
               "\"accordion\" -- to use the accordion only; or \n"
               "\"tabs,accordion\" -- to use tabs and the accordion."),
48
        scope=Scope.settings,
Piotr Mitros committed
49
        default=None,
50 51
    )
    default_tab = String(
52 53
        display_name=_("Default Tab"),
        help=_("Enter the tab that is selected in the XBlock. If not set, the Courseware tab is selected."),
54
        scope=Scope.settings,
Piotr Mitros committed
55
        default=None,
56
    )
57 58 59 60 61 62 63 64 65
    source_file = String(
        display_name=_("LaTeX Source File Name"),
        help=_("Enter the source file name for LaTeX."),
        scope=Scope.settings,
        deprecated=True
    )
    ispublic = Boolean(
        display_name=_("Course Is Public"),
        help=_("Enter true or false. If true, the course is open to the public. If false, the course is open only to admins."),
66
        scope=Scope.settings
67
    )
68 69 70 71 72
    visible_to_staff_only = Boolean(
        help=_("If true, can be seen only by course staff, regardless of start date."),
        default=False,
        scope=Scope.settings,
    )
73 74 75 76 77 78 79 80
    group_access = GroupAccessDict(
        help=_(
            "A dictionary that maps which groups can be shown this block. The keys "
            "are group configuration ids and the values are a list of group IDs. "
            "If there is no key for a group configuration or if the set of group IDs "
            "is empty then the block is considered visible to all. Note that this "
            "field is ignored if the block is visible_to_staff_only."
        ),
81 82 83 84
        default={},
        scope=Scope.settings,
    )

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    @lazy
    def merged_group_access(self):
        """
        This computes access to a block's group_access rules in the context of its position
        within the courseware structure, in the form of a lazily-computed attribute.
        Each block's group_access rule is merged recursively with its parent's, guaranteeing
        that any rule in a parent block will be enforced on descendants, even if a descendant
        also defined its own access rules.  The return value is always a dict, with the same
        structure as that of the group_access field.

        When merging access rules results in a case where all groups are denied access in a
        user partition (which effectively denies access to that block for all students),
        the special value False will be returned for that user partition key.
        """
        parent = self.get_parent()
        if not parent:
            return self.group_access or {}

        merged_access = parent.merged_group_access.copy()
        if self.group_access is not None:
            for partition_id, group_ids in self.group_access.items():
                if group_ids:  # skip if the "local" group_access for this partition is None or empty.
                    if partition_id in merged_access:
                        if merged_access[partition_id] is False:
                            # special case - means somewhere up the hierarchy, merged access rules have eliminated
                            # all group_ids from this partition, so there's no possible intersection.
                            continue
                        # otherwise, if the parent defines group access rules for this partition,
                        # intersect with the local ones.
                        merged_access[partition_id] = list(
                            set(merged_access[partition_id]).intersection(group_ids)
                        ) or False
                    else:
                        # add the group access rules for this partition to the merged set of rules.
                        merged_access[partition_id] = group_ids
        return merged_access

122 123 124 125 126 127 128 129 130
    # Specified here so we can see what the value set at the course-level is.
    user_partitions = UserPartitionList(
        help=_("The list of group configurations for partitioning students in content experiments."),
        default=[],
        scope=Scope.settings
    )

    def _get_user_partition(self, user_partition_id):
        """
131 132
        Returns the user partition with the specified id.  Raises
        `NoSuchUserPartitionError` if the lookup fails.
133 134 135 136 137
        """
        for user_partition in self.user_partitions:
            if user_partition.id == user_partition_id:
                return user_partition

138
        raise NoSuchUserPartitionError("could not find a UserPartition with ID [{}]".format(user_partition_id))
139 140 141 142 143 144 145

    def validate(self):
        """
        Validates the state of this xblock instance.
        """
        _ = self.runtime.service(self, "i18n").ugettext  # pylint: disable=redefined-outer-name
        validation = super(LmsBlockMixin, self).validate()
146 147
        has_invalid_user_partitions = False
        has_invalid_groups = False
148
        for user_partition_id, group_ids in self.group_access.iteritems():
149 150 151
            try:
                user_partition = self._get_user_partition(user_partition_id)
            except NoSuchUserPartitionError:
152
                has_invalid_user_partitions = True
153 154
            else:
                for group_id in group_ids:
155 156 157
                    try:
                        user_partition.get_group(group_id)
                    except NoSuchUserPartitionGroupError:
158
                        has_invalid_groups = True
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173
        if has_invalid_user_partitions:
            validation.add(
                ValidationMessage(
                    ValidationMessage.ERROR,
                    _(u"This component refers to deleted or invalid content group configurations.")
                )
            )
        if has_invalid_groups:
            validation.add(
                ValidationMessage(
                    ValidationMessage.ERROR,
                    _(u"This component refers to deleted or invalid content groups.")
                )
            )
174
        return validation