Skip to content

Commit

Permalink
feat: emit the <TODO> Open edX event when an exam is submitted
Browse files Browse the repository at this point in the history
TODO: add a description here once the exam name and exam data name are finalized
  • Loading branch information
MichaelRoytman committed Sep 20, 2023
1 parent bf28d14 commit 7938bba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
28 changes: 28 additions & 0 deletions edx_exams/apps/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
from django.utils import dateparse, timezone
from opaque_keys.edx.keys import CourseKey, UsageKey

# TODO: Change the signal to the appropriate signal type.
from openedx_events.learning.signals import STUDENT_REGISTRATION_COMPLETED
from openedx_events.learning.data import UserData, UserPersonalData


from edx_exams.apps.core.exam_types import OnboardingExamType, PracticeExamType, get_exam_type
from edx_exams.apps.core.exceptions import (
ExamAttemptAlreadyExists,
Expand Down Expand Up @@ -96,6 +101,11 @@ def update_attempt_status(attempt_id, to_status):
if to_status == ExamAttemptStatus.submitted:
attempt_obj.end_time = datetime.now(pytz.UTC)

_emit_TODO_NAME_event(
attempt_obj.user.username,
attempt_obj.exam.content_id
)

attempt_obj.status = to_status
attempt_obj.save()

Expand Down Expand Up @@ -160,6 +170,24 @@ def _calculate_allowed_mins(exam):

return allowed_time_limit_mins

def _emit_TODO_NAME_event(username, content_id):
# .. event_implemented_name: TODO
STUDENT_REGISTRATION_COMPLETED.send_event(
user=UserData(
id=1,
is_active=True,
pii=UserPersonalData(
username=username,
email='test',
name='test',
)
# data=InstructorCompletionData(
# username=username,
# content_id=content_id,
# )
)
)


def get_exam_attempt_time_remaining(exam_attempt, now=None):
"""
Expand Down
20 changes: 20 additions & 0 deletions 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 UserData, UserPersonalData

from edx_exams.apps.api.test_utils import ExamsAPITestCase
from edx_exams.apps.core.api import (
Expand Down Expand Up @@ -298,6 +299,25 @@ 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.api.STUDENT_REGISTRATION_COMPLETED.send_event')
def test_submit_attempt_event_emitted(self, mock_event_send):
"""
Test that when an exam is submitted, the appropriate Open edX event is emitted.
"""
update_attempt_status(self.exam_attempt.id, ExamAttemptStatus.submitted)
self.assertEqual(mock_event_send.call_count, 1)

expected_data = UserData(
id=1,
is_active=True,
pii=UserPersonalData(
username=self.user.username,
email='test',
name='test',
)
)
mock_event_send.assert_called_with(user=expected_data)

def test_illegal_start(self):
"""
Test that an already started exam cannot be started
Expand Down

0 comments on commit 7938bba

Please sign in to comment.