Skip to content

Commit

Permalink
Merge pull request #178 from edx/michaelroytman/MST-2114-exam-complet…
Browse files Browse the repository at this point in the history
…ion-event

Emit the ExamAttemptSubmitted Open edX event when an exam is submitted.
  • Loading branch information
MichaelRoytman authored Oct 2, 2023
2 parents 38adf3e + 980ba38 commit 671e8eb
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 117 deletions.
10 changes: 10 additions & 0 deletions edx_exams/apps/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ExamIllegalStatusTransition
)
from edx_exams.apps.core.models import Exam, ExamAttempt
from edx_exams.apps.core.signals.signals import emit_exam_attempt_submitted_event
from edx_exams.apps.core.statuses import ExamAttemptStatus

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -96,6 +97,15 @@ def update_attempt_status(attempt_id, to_status):
if to_status == ExamAttemptStatus.submitted:
attempt_obj.end_time = datetime.now(pytz.UTC)

course_key = CourseKey.from_string(attempt_obj.exam.course_id)
usage_key = UsageKey.from_string(attempt_obj.exam.content_id)
emit_exam_attempt_submitted_event(
attempt_obj.user,
course_key,
usage_key,
attempt_obj.exam.exam_type
)

attempt_obj.status = to_status
attempt_obj.save()

Expand Down
18 changes: 18 additions & 0 deletions edx_exams/apps/core/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Core Application Configuration
"""

from django.apps import AppConfig


class CoreConfig(AppConfig):
"""
Application configuration for core application.
"""
name = 'edx_exams.apps.core'

def ready(self):
"""
Connect handlers to signals.
"""
from .signals import handlers # pylint: disable=unused-import,import-outside-toplevel
Empty file.
20 changes: 20 additions & 0 deletions edx_exams/apps/core/signals/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Signal handlers for the edx-exams application.
"""
from django.dispatch import receiver
from openedx_events.event_bus import get_producer
from openedx_events.learning.signals import EXAM_ATTEMPT_SUBMITTED


@receiver(EXAM_ATTEMPT_SUBMITTED)
def listen_for_exam_attempt_submitted(sender, signal, **kwargs): # pylint: disable=unused-argument
"""
Publish EXAM_ATTEMPT_SUBMITTED signals onto the event bus.
"""
get_producer().send(
signal=EXAM_ATTEMPT_SUBMITTED,
topic='exam-attempt-submitted',
event_key_field='exam_attempt.course_key',
event_data={'exam_attempt': kwargs['exam_attempt']},
event_metadata=kwargs['metadata'],
)
32 changes: 32 additions & 0 deletions edx_exams/apps/core/signals/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Signal definitions and functions to send those signals for the edx-exams application.
"""

from openedx_events.learning.data import ExamAttemptData, UserData, UserPersonalData
from openedx_events.learning.signals import EXAM_ATTEMPT_SUBMITTED


def emit_exam_attempt_submitted_event(user, course_key, usage_key, exam_type):
"""
Emit the EXAM_ATTEMPT_SUBMITTED Open edX event.
"""
user_data = UserData(
id=user.id,
is_active=user.is_active,
pii=UserPersonalData(
username=user.username,
email=user.email,
name=user.full_name
)
)

# .. event_implemented_name: EXAM_ATTEMPT_SUBMITTED
EXAM_ATTEMPT_SUBMITTED.send_event(
exam_attempt=ExamAttemptData(
student_user=user_data,
course_key=course_key,
usage_key=usage_key,
exam_type=exam_type,
requesting_user=user_data
)
)
1 change: 1 addition & 0 deletions edx_exams/apps/core/test_utils/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Meta:
password = factory.PostGenerationMethodCall('set_password', _DEFAULT_PASSWORD)
first_name = factory.Sequence('User{}'.format)
last_name = 'Test'
full_name = "{} {}".format(first_name, last_name)
is_superuser = False
is_staff = False

Expand Down
32 changes: 31 additions & 1 deletion edx_exams/apps/core/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.utils import timezone
from freezegun import freeze_time
from opaque_keys.edx.keys import CourseKey, UsageKey
from openedx_events.learning.data import ExamAttemptData, UserData, UserPersonalData

from edx_exams.apps.api.test_utils import ExamsAPITestCase
from edx_exams.apps.core.api import (
Expand Down Expand Up @@ -212,7 +213,7 @@ def setUp(self):
resource_id=str(uuid.uuid4()),
course_id=self.course_id,
provider=self.test_provider,
content_id='abcd1234',
content_id='block-v1:edX+test+2023+type@sequential+block@1111111111',
exam_name='test_exam',
exam_type='proctored',
time_limit_mins=30,
Expand Down Expand Up @@ -298,6 +299,35 @@ def test_submit_attempt(self):
self.assertEqual(updated_attempt.status, ExamAttemptStatus.submitted)
self.assertEqual(updated_attempt.end_time, timezone.now())

@patch('edx_exams.apps.core.signals.signals.EXAM_ATTEMPT_SUBMITTED.send_event')
def test_submit_attempt_event_emitted(self, mock_event_send):
"""
Test that when an exam is submitted, the EXAM_ATTEMPT_SUBMITED Open edX event is emitted.
"""
update_attempt_status(self.exam_attempt.id, ExamAttemptStatus.submitted)
self.assertEqual(mock_event_send.call_count, 1)

user_data = UserData(
id=self.user.id,
is_active=self.user.is_active,
pii=UserPersonalData(
username=self.user.username,
email=self.user.email,
name=self.user.full_name
)
)
course_key = CourseKey.from_string(self.exam.course_id)
usage_key = UsageKey.from_string(self.exam.content_id)

expected_data = ExamAttemptData(
student_user=user_data,
course_key=course_key,
usage_key=usage_key,
exam_type=self.exam.exam_type,
requesting_user=user_data,
)
mock_event_send.assert_called_with(exam_attempt=expected_data)

def test_illegal_start(self):
"""
Test that an already started exam cannot be started
Expand Down
24 changes: 12 additions & 12 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ attrs==23.1.0
# via
# lti-consumer-xblock
# openedx-events
avro==1.11.2
avro==1.11.3
# via confluent-kafka
bleach==6.0.0
# via lti-consumer-xblock
boto3==1.28.52
boto3==1.28.57
# via fs-s3fs
botocore==1.31.52
botocore==1.31.57
# via
# boto3
# s3transfer
certifi==2023.7.22
# via requests
cffi==1.15.1
cffi==1.16.0
# via
# cryptography
# pynacl
charset-normalizer==3.2.0
charset-normalizer==3.3.0
# via requests
click==8.1.7
# via
Expand All @@ -48,7 +48,7 @@ cryptography==41.0.4
# via
# pyjwt
# social-auth-core
defusedxml==0.7.1
defusedxml==0.8.0rc2
# via
# python3-openid
# social-auth-core
Expand Down Expand Up @@ -208,7 +208,7 @@ markupsafe==2.1.3
# xblock
mysqlclient==2.2.0
# via -r requirements/base.in
newrelic==9.0.0
newrelic==9.1.0
# via edx-django-utils
oauthlib==3.2.2
# via
Expand All @@ -221,13 +221,13 @@ openedx-django-pyfs==3.4.0
# via
# lti-consumer-xblock
# xblock
openedx-events==8.6.0
openedx-events==8.8.0
# via
# -r requirements/base.in
# edx-event-bus-kafka
openedx-filters==1.6.0
# via lti-consumer-xblock
packaging==23.1
packaging==23.2
# via drf-yasg
pbr==5.11.1
# via stevedore
Expand Down Expand Up @@ -287,7 +287,7 @@ requests==2.31.0
# social-auth-core
requests-oauthlib==1.3.1
# via social-auth-core
s3transfer==0.6.2
s3transfer==0.7.0
# via boto3
semantic-version==2.10.0
# via edx-drf-extensions
Expand Down Expand Up @@ -330,7 +330,7 @@ uritemplate==4.1.1
# via
# coreapi
# drf-yasg
urllib3==1.26.16
urllib3==1.26.17
# via
# botocore
# requests
Expand All @@ -342,7 +342,7 @@ webencodings==0.5.1
# via bleach
webob==1.8.7
# via xblock
xblock[django]==1.7.0
xblock[django]==1.8.0
# via
# lti-consumer-xblock
# xblock-utils
Expand Down
4 changes: 2 additions & 2 deletions requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ filelock==3.12.4
# via
# tox
# virtualenv
packaging==23.1
packaging==23.2
# via tox
platformdirs==3.10.0
platformdirs==3.11.0
# via virtualenv
pluggy==1.3.0
# via tox
Expand Down
Loading

0 comments on commit 671e8eb

Please sign in to comment.