Files
edx-platform/common/djangoapps/user_api/user_service.py
Victor Shnayder 281ad63d2b split testing support in the LMS.
Adds a split_test_module XModule, that can choose one of its children
to display, based on a get_condition_for_user API added to the runtime.

To test, add something like this to an xml course, or make equivalent
 tweaks in mongo.

 <vertical url_name="split_test_vert">
    <split_test url_name="split1" experiment_id="0" condition_id_to_child='{"0": "i4x://MITx/6.00x/html/split_test_cond0", "1": "i4x://MITx/6.00x/html/split_test_cond1"}'>
       <html url_name="split_test_cond0">condition 0</html>
       <html url_name="split_test_cond1">condition 1</html>
    </split_test>
  </vertical>

Also needs an experiment configured in the course policy json: e.g.

        "user_partitions": [{"id": 0,
                                      "name": "Experiment 0",
                                      "description": "Unicorns?",
                                      "version": 1,
                                      "groups": [{"id": 0,
                                                        "name": "group 0",
                                                        "version": 1},
                                                        {"id": 1,
                                                        "name": "group 1",
                                                        "version": 1}]}]

(This particular snippet will work inside a course with org MITx
 and course name 6.00x)

Co-Author: Sarina Canelake <sarina@edx.org>
Co-Author: Julia Hansbrough <julia@edx.org>
Co-Author: Diana Huang <diana@edx.org>
Co-Author: Calen Pennington <cale@edx.org>

[LMS-2095]
2014-03-05 13:45:08 -05:00

62 lines
1.7 KiB
Python

"""
A service-like user_info interface. Could be made into an http API later, but for now
just in-process. Exposes global and per-course key-value pairs for users.
Implementation note:
Stores global metadata using the UserPreference model, and per-course metadata using the
UserCourseTag model.
"""
from user_api.models import UserCourseTag
def get_course_tag(user, course_id, key):
"""
Gets the value of the user's course tag for the specified key in the specified
course_id.
Args:
user: the User object for the course tag
course_id: course identifier (string)
key: arbitrary (<=255 char string)
Returns:
string value, or None if there is no value saved
"""
try:
record = UserCourseTag.objects.get(
user=user,
course_id=course_id,
key=key)
return record.value
except UserCourseTag.DoesNotExist:
return None
def set_course_tag(user, course_id, key, value):
"""
Sets the value of the user's course tag for the specified key in the specified
course_id. Overwrites any previous value.
The intention is that the values are fairly short, as they will be included in all
analytics events about this user.
Args:
user: the User object
course_id: course identifier (string)
key: arbitrary (<=255 char string)
value: arbitrary string
"""
record, _ = UserCourseTag.objects.get_or_create(
user=user,
course_id=course_id,
key=key)
record.value = value
record.save()
# TODO: There is a risk of IntegrityErrors being thrown here given
# simultaneous calls from many processes. Handle by retrying after a short delay?