Get students experiments, groups, and cohorts

TNL-498
This commit is contained in:
muhammad-ammar
2014-12-10 21:00:08 +05:00
parent 58fd7a92c3
commit 5ba492ecf3
11 changed files with 215 additions and 27 deletions

View File

@@ -196,7 +196,7 @@ def get_cohorted_commentables(course_key):
return ans
def get_cohort(user, course_key):
def get_cohort(user, course_key, assign=True):
"""
Given a Django user and a CourseKey, return the user's cohort in that
cohort.
@@ -204,6 +204,7 @@ def get_cohort(user, course_key):
Arguments:
user: a Django User object.
course_key: CourseKey
assign (bool): if False then we don't assign a group to user
Returns:
A CourseUserGroup object if the course is cohorted and the User has a
@@ -230,7 +231,8 @@ def get_cohort(user, course_key):
)
except CourseUserGroup.DoesNotExist:
# Didn't find the group. We'll go on to create one if needed.
pass
if not assign:
return None
choices = course.auto_cohort_groups
if len(choices) > 0:

View File

@@ -207,6 +207,31 @@ class TestCohorts(TestCase):
"other_user should be assigned to the default cohort"
)
def test_get_cohort_with_assign(self):
"""
Make sure cohorts.get_cohort() returns None if no group is already
assigned to a user instead of assigning/creating a group automatically
"""
course = modulestore().get_course(self.toy_course_key)
self.assertFalse(course.is_cohorted)
user = UserFactory(username="test", email="a@b.com")
# Add an auto_cohort_group to the course...
config_course_cohorts(
course,
discussions=[],
cohorted=True,
auto_cohort_groups=["AutoGroup"]
)
# get_cohort should return None as no group is assigned to user
self.assertIsNone(cohorts.get_cohort(user, course.id, assign=False))
# get_cohort should return a group for user
self.assertEquals(cohorts.get_cohort(user, course.id).name, "AutoGroup")
def test_auto_cohorting(self):
"""
Make sure cohorts.get_cohort() does the right thing with auto_cohort_groups

View File

@@ -15,15 +15,16 @@ class RandomUserPartitionScheme(object):
RANDOM = random.Random()
@classmethod
def get_group_for_user(cls, course_id, user, user_partition, track_function=None):
def get_group_for_user(cls, course_id, user, user_partition, assign=True, track_function=None):
"""
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 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_id, partition_key)
group = user_partition.get_group(int(group_id)) if not group_id is None else None
if group is None:
if group is None and assign:
if not user_partition.groups:
raise UserPartitionError('Cannot assign user to an empty user partition')

View File

@@ -58,6 +58,24 @@ class TestRandomUserPartitionScheme(PartitionTestCase):
group2_id = RandomUserPartitionScheme.get_group_for_user(self.MOCK_COURSE_ID, self.user, self.user_partition)
self.assertEqual(group1_id, group2_id)
def test_get_group_for_user_with_assign(self):
"""
Make sure get_group_for_user returns None if no group is already
assigned to a user instead of assigning/creating a group automatically
"""
# We should not get any group because assign is False which will
# protect us from automatically creating a group for user
group = RandomUserPartitionScheme.get_group_for_user(
self.MOCK_COURSE_ID, self.user, self.user_partition, assign=False
)
self.assertIsNone(group)
# We should get a group automatically assigned to user
group = RandomUserPartitionScheme.get_group_for_user(self.MOCK_COURSE_ID, self.user, self.user_partition)
self.assertIsNotNone(group)
def test_empty_partition(self):
empty_partition = UserPartition(
self.TEST_ID,