Skip to content

Instantly share code, notes, and snippets.

@ztraboo
Last active March 31, 2021 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ztraboo/ef23c82721f1bad59c412e1ebad7ffd1 to your computer and use it in GitHub Desktop.
Save ztraboo/ef23c82721f1bad59c412e1ebad7ffd1 to your computer and use it in GitHub Desktop.
Store Subscription ID from Qualtrics API for given Course ID and XBlock Location to prevent multiple callbacks from Qualtrics to same XBlock event handler.
from django.db import models
LOGGER = logging.getLogger(__name__)
class QualtricsSurveyModelMixin(ScorableXBlockMixin, CourseDetailsXBlockMixin):
def __init__(self):
"""
Check to see if Qualtrics Subscription ID exists for given CourseID and XBlock location.
"""
# Store Qualtrics API subscription ID for given CourseID and XBlock location. Used to prevent multiple callbacks
# to XBlock callback handler from Qualtrics after a survey response complete is triggered.
try:
qualtrics_subscription = QualtricsSubscriptions.objects.get(course_id='course-v1:DEMO+AT-BR101+2020_Fall', usage_key='block-v1:DEMO+AT-BR101+2020_Fall+type@qualtricssurvey+block@617b566bb8134d8a9a2e1a7ace1002c8')
except QualtricsSubscriptions.DoesNotExist:
# Make a request call to the Qualtrics `Event Subscriptions: Create` endpoint https://clemson.qualtrics.com/API/v3/eventsubscriptions to create a new subscription callback to the Xblock endpoint.
#
# Sample Request POST Body Payload (raw)
# {
# "topics": "surveyengine.completedResponse.SV_0GOuI59dZl8YdkW",
# "publicationUrl": "http://localhost:18000/courses/course-v1:DEMO+AT-BR101+2020_Fall/xblock/block-v1:DEMO+AT-BR101+2020_Fall+type@qualtricssurvey+block@2deb2d20bb914423997b750c78fa0bf7/handler_noauth/end_survey"
# }
#
# Example POST Return from Qualtrics:
# {
# "result": {
# "id": "SUB_9MLhUW4oN3kx5bM"
# },
# "meta": {
# "httpStatus": "200 - OK",
# "requestId": "a7840953-9260-412c-a4eb-ad7d67942031"
# }
# }
# Todo: Use Postman request call to this endpoint.
# Store value in 'response' variable.
self._log_if_raised(response, data)
subscription_id = response.json()['result']['id']
# self.location should be this XBlock location id
# (e.g 'block-v1:DEMO+AT-BR101+2020_Fall+type@qualtricssurvey+block@617b566bb8134d8a9a2e1a7ace1002c8')
# Check to see if Qualtrics returned a subscription id from the API
# for SurveyID and XBlock endpoint then store that in the database.
# This database entry is to prevent from calling the Qualtrics
# create subscription multiple times for the same XBlock handler
# callback for the given survey.
if subscription_id:
qualtrics_subscription = QualtricsSubscriptions(course_id='course-v1:DEMO+AT-BR101+2020_Fall', usage_key=self.location, subscription_id='SUB_9MLhUW4oN3kx5bM')
qualtrics_subscription.save()
else:
LOGGER.error(u"Could not locate a subscription id from Qualtrics API for course {} - XBlock location {}".format(
course_id, self.location
))
raise
...
class QualtricsSubscriptions(model.Model):
"""
Defines a way to see if a given Qualtrics subscription_id is tied to a course_id, XBlock location id
"""
course_id = CourseKeyField(max_length=255, db_index=True)
usage_key = UsageKeyField(max_length=255, db_index=True, help_text=_(u'The course block identifier.'))
subscription_id = models.CharField(max_length=50, db_index=True, help_text=_(u'The subscription id from Qualtrics.'))
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment