def dismiss_welcome_message(request, course_id): """ Given the course_id in the request, disable displaying the welcome message for the user. """ course_key = CourseKey.from_string(course_id) set_course_tag(request.user, course_key, PREFERENCE_KEY, 'False') return HttpResponse()
def get_group_for_user(cls, course_key, user, user_partition, assign=True): """ Returns the group from the specified user position to which the user is assigned. If the user has not yet been assigned, a group will be randomly chosen for them if assign flag is True. """ partition_key = cls.key_for_partition(user_partition) group_id = course_tag_api.get_course_tag(user, course_key, partition_key) group = None if group_id is not None: # attempt to look up the presently assigned group try: group = user_partition.get_group(int(group_id)) except NoSuchUserPartitionGroupError: # jsa: we can turn off warnings here if this is an expected case. log.warn(u"group not found in RandomUserPartitionScheme: %r", { "requested_partition_id": user_partition.id, "requested_group_id": group_id, }, exc_info=True) except ValueError: log.error(u"Bad group_id %r for user: %r", group_id, user) if group is None and assign and not course_tag_api.BulkCourseTags.is_prefetched( course_key): if not user_partition.groups: raise UserPartitionError( 'Cannot assign user to an empty user partition') # pylint: disable=fixme # TODO: had a discussion in arch council about making randomization more # deterministic (e.g. some hash). Could do that, but need to be careful not # to introduce correlation between users or bias in generation. group = cls.RANDOM.choice(user_partition.groups) # persist the value as a course tag course_tag_api.set_course_tag(user, course_key, partition_key, group.id) # emit event for analytics # FYI - context is always user ID that is logged in, NOT the user id that is # being operated on. If instructor can move user explicitly, then we should # put in event_info the user id that is being operated on. event_name = 'xmodule.partitions.assigned_user_to_partition' event_info = { 'group_id': group.id, 'group_name': group.name, 'partition_id': user_partition.id, 'partition_name': user_partition.name } # pylint: disable=fixme # TODO: Use the XBlock publish api instead with tracker.get_tracker().context(event_name, {}): tracker.emit( event_name, event_info, ) return group
def test_legacy_ignore_all_support(self): """Storing 'False' as the dismissal ignores all updates.""" self.create_course_update('First') assert get_current_update_for_user(self.request, self.course) == 'First' set_course_tag(self.user, self.course.id, self.UPDATES_TAG, 'False') assert get_current_update_for_user(self.request, self.course) is None
def _add_dismissed_hash(user, course_key, new_hash): """ Add a new hash to the list of previously dismissed updates. Overwrites a 'False' value with the current hash. Though we likely won't end up in that situation, since a 'False' value will never show the update to the user to dismiss in the first place. """ hashes = _get_dismissed_hashes(user, course_key) or [] hashes.append(new_hash) set_course_tag(user, course_key, VIEW_WELCOME_MESSAGE_KEY, ','.join(hashes))
def test_user(self, group_id, expected_blocks): course_tag_api.set_course_tag( self.user, self.course.id, RandomUserPartitionScheme.key_for_partition(self.split_test_user_partition), group_id, ) block_structure1 = get_course_blocks(self.user, self.course.location, self.transformers) self.assertEqual( set(block_structure1.get_block_keys()), set(self.get_block_key_set(self.blocks, *expected_blocks)) )
def test_get_set_course_tag(self): # get a tag that doesn't exist tag = course_tag_api.get_course_tag(self.user, self.course_id, self.test_key) assert tag is None # test setting a new key test_value = 'value' course_tag_api.set_course_tag(self.user, self.course_id, self.test_key, test_value) tag = course_tag_api.get_course_tag(self.user, self.course_id, self.test_key) assert tag == test_value #test overwriting an existing key test_value = 'value2' course_tag_api.set_course_tag(self.user, self.course_id, self.test_key, test_value) tag = course_tag_api.get_course_tag(self.user, self.course_id, self.test_key) assert tag == test_value
def test_user(self, group_id, expected_blocks): course_tag_api.set_course_tag( self.user, self.course.id, RandomUserPartitionScheme.key_for_partition( self.split_test_user_partition), group_id, ) block_structure1 = get_course_blocks( self.user, self.course.location, self.transformers, ) assert set(block_structure1.get_block_keys()) == set( self.get_block_key_set(self.blocks, *expected_blocks))
def test_get_set_course_tag(self): # get a tag that doesn't exist tag = course_tag_api.get_course_tag(self.user, self.course_id, self.test_key) self.assertIsNone(tag) # test setting a new key test_value = 'value' course_tag_api.set_course_tag(self.user, self.course_id, self.test_key, test_value) tag = course_tag_api.get_course_tag(self.user, self.course_id, self.test_key) self.assertEqual(tag, test_value) #test overwriting an existing key test_value = 'value2' course_tag_api.set_course_tag(self.user, self.course_id, self.test_key, test_value) tag = course_tag_api.get_course_tag(self.user, self.course_id, self.test_key) self.assertEqual(tag, test_value)
def dismiss_welcome_message(request): course_id = request.data.get('course_id', None) # If body doesn't contain 'course_id', return 400 to client. if not course_id: raise ParseError(_("'course_id' is required.")) # If body contains params other than 'course_id', return 400 to client. if len(request.data) > 1: raise ParseError(_("Only 'course_id' is expected.")) try: course_key = CourseKey.from_string(course_id) set_course_tag(request.user, course_key, PREFERENCE_KEY, 'False') return Response({'message': _('Welcome message successfully dismissed.')}) except Exception: raise UnableToDismissWelcomeMessage
def set_tag(self, scope, key, value): """ Set the user tag for the current course and the current user for a given key scope: the current scope of the runtime key: the key that to the value to be set value: the value to set """ if scope != user_course_tag_api.COURSE_SCOPE: raise ValueError(f"unexpected scope {scope}") return user_course_tag_api.set_course_tag(self._user, self._course_id, key, value)
def set_tag(self, scope, key, value): """ Set the user tag for the current course and the current user for a given key scope: the current scope of the runtime key: the key that to the value to be set value: the value to set """ if scope != user_course_tag_api.COURSE_SCOPE: raise ValueError("unexpected scope {0}".format(scope)) return user_course_tag_api.set_course_tag( self._get_current_user(), self.runtime.course_id, key, value )
def test_cohort_scheme_partition(self): """ Test that cohort-schemed user partitions are ignored in the grades export. """ # Set up a course with 'cohort' and 'random' user partitions. cohort_scheme_partition = UserPartition( 0, 'Cohort-schemed Group Configuration', 'Group Configuration based on Cohorts', [Group(0, 'Group A'), Group(1, 'Group B')], scheme_id='cohort' ) experiment_group_a = Group(2, u'Expériment Group A') experiment_group_b = Group(3, u'Expériment Group B') experiment_partition = UserPartition( 1, u'Content Expériment Configuration', u'Group Configuration for Content Expériments', [experiment_group_a, experiment_group_b], scheme_id='random' ) course = CourseFactory.create( cohort_config={'cohorted': True}, user_partitions=[cohort_scheme_partition, experiment_partition] ) # Create user_a and user_b which are enrolled in the course # and assigned to experiment_group_a and experiment_group_b, # respectively. user_a = UserFactory.create(username='******') user_b = UserFactory.create(username='******') CourseEnrollment.enroll(user_a, course.id) CourseEnrollment.enroll(user_b, course.id) course_tag_api.set_course_tag( user_a, course.id, RandomUserPartitionScheme.key_for_partition(experiment_partition), experiment_group_a.id ) course_tag_api.set_course_tag( user_b, course.id, RandomUserPartitionScheme.key_for_partition(experiment_partition), experiment_group_b.id ) # Assign user_a to a group in the 'cohort'-schemed user # partition (by way of a cohort) to verify that the user # partition group does not show up in the "Experiment Group" # cell. cohort_a = CohortFactory.create(course_id=course.id, name=u'Cohørt A', users=[user_a]) CourseUserGroupPartitionGroup( course_user_group=cohort_a, partition_id=cohort_scheme_partition.id, group_id=cohort_scheme_partition.groups[0].id ).save() # Verify that we see user_a and user_b in their respective # content experiment groups, and that we do not see any # content groups. experiment_group_message = u'Experiment Group ({content_experiment})' self._verify_cell_data_for_user( user_a.username, course.id, experiment_group_message.format( content_experiment=experiment_partition.name ), experiment_group_a.name ) self._verify_cell_data_for_user( user_b.username, course.id, experiment_group_message.format( content_experiment=experiment_partition.name ), experiment_group_b.name ) # Make sure cohort info is correct. cohort_name_header = 'Cohort Name' self._verify_cell_data_for_user( user_a.username, course.id, cohort_name_header, cohort_a.name ) self._verify_cell_data_for_user( user_b.username, course.id, cohort_name_header, '' )