Skip to content

Commit

Permalink
Merge pull request openedx#668 from Stanford-Online/dcadams/oli_handler
Browse files Browse the repository at this point in the history
Mods to OLI event hander
  • Loading branch information
David Adams authored Aug 4, 2017
2 parents 61ed33c + af7ff50 commit c047f5a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
36 changes: 8 additions & 28 deletions common/djangoapps/track/backends/oli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
from django.contrib.auth.models import User
from requests_oauthlib import OAuth1Session

from opaque_keys.edx.keys import CourseKey, UsageKey
from student.models import anonymous_id_for_user
from track.backends import BaseBackend
from xmodule.capa_module import CapaDescriptor
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.exceptions import ItemNotFoundError


LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -82,12 +78,8 @@ def send(self, event):
LOG.info('event_data attribute missing from event for OLI service')
return None

problem_text = None
# Look where it should be for a capa prob.
problem_id = event_data.get('problem_id')
if problem_id:
problem_text = self.get_problem_text(problem_id, course_id)

if not problem_id:
# Look where it should be for an xblock.
problem_id = context.get('module').get('usage_key')
Expand All @@ -108,6 +100,12 @@ def send(self, event):
# This is supplied by the StatTutor Xblock.
problem_question_name = event_data.get('problem_question_name')

# This is the student answer in terms of semantic choices.
submission = event_data.get('submission')

# This is the student answers in terms of choice indices.
answers = event_data.get('answers')

timestamp = event.get('time')
if not timestamp:
LOG.info('time attribute missing from event for OLI service')
Expand All @@ -128,8 +126,9 @@ def send(self, event):
'grade': grade,
'max_grade': max_grade,
'timestamp': timestamp.isoformat(),
'problem_text': problem_text,
'problem_question_name': problem_question_name,
'submission': submission,
'answers': answers,
},
})

Expand All @@ -154,22 +153,3 @@ def send(self, event):
else:
LOG.info('OLI analytics service returns error status code: %s.', response.status_code)
return 'Error'

def get_problem_text(self, block, course_id):
"""
Helper method to get the problem text
"""

course_key = CourseKey.from_string(course_id)
usage_key = UsageKey.from_string(block).map_into_course(course_key)

try:
descriptor = modulestore().get_item(usage_key, depth=5)
except ItemNotFoundError:
return None

if isinstance(descriptor, CapaDescriptor):
problem_text = descriptor.data
return problem_text
else:
return None
6 changes: 2 additions & 4 deletions common/djangoapps/track/backends/tests/test_oli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ def setUp(self):
self.user = UserFactory.create()
self.user.save()

@patch('track.backends.oli.OLIAnalyticsBackend.get_problem_text')
def test_oli_backend_correct_event_type(self, mock_get_problem_text):
def test_oli_backend_correct_event_type(self):
"""
Assert that problem_check events are sent
Assert that events are sent without any errors.
"""
mock_get_problem_text.return_value = None

# pylint: disable=line-too-long
event = {
Expand Down

0 comments on commit c047f5a

Please sign in to comment.