Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: emit new course passing status events #34524

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,14 @@
# .. toggle_creation_date: 2024-03-22
# .. toggle_tickets: https://github.com/openedx/edx-platform/pull/33911
'ENABLE_GRADING_METHOD_IN_PROBLEMS': False,

# .. toggle_name: FEATURES['BADGES_ENABLED']
# .. toggle_implementation: DjangoSetting
# .. toggle_default: False
# .. toggle_description: Set to True to enable the Badges feature.
# .. toggle_use_cases: open_edx
# .. toggle_creation_date: 2024-04-10
'BADGES_ENABLED': False,
}

# .. toggle_name: ENABLE_COPPA_COMPLIANCE
Expand Down Expand Up @@ -2837,6 +2845,8 @@
def _should_send_xblock_events(settings):
return settings.FEATURES['ENABLE_SEND_XBLOCK_LIFECYCLE_EVENTS_OVER_BUS']

def _should_send_learning_badge_events(settings):
return settings.FEATURES['BADGES_ENABLED']

# .. setting_name: EVENT_BUS_PRODUCER_CONFIG
# .. setting_default: all events disabled
Expand Down Expand Up @@ -2887,6 +2897,18 @@ def _should_send_xblock_events(settings):
'learning-certificate-lifecycle':
{'event_key_field': 'certificate.course.course_key', 'enabled': False},
},
"org.openedx.learning.course.passing.status.updated.v1": {
"learning-badges-lifecycle": {
"event_key_field": "course_passing_status.course.course_key",
"enabled": _should_send_learning_badge_events,
},
},
"org.openedx.learning.ccx.course.passing.status.updated.v1": {
"learning-badges-lifecycle": {
"event_key_field": "course_passing_status.course.course_key",
"enabled": _should_send_learning_badge_events,
},
},
}


Expand All @@ -2897,6 +2919,18 @@ def _should_send_xblock_events(settings):
derived_collection_entry('EVENT_BUS_PRODUCER_CONFIG', 'org.openedx.content_authoring.xblock.deleted.v1',
'course-authoring-xblock-lifecycle', 'enabled')

derived_collection_entry(
"EVENT_BUS_PRODUCER_CONFIG",
"org.openedx.learning.course.passing.status.updated.v1",
"learning-badges-lifecycle",
"enabled",
)
derived_collection_entry(
"EVENT_BUS_PRODUCER_CONFIG",
"org.openedx.learning.ccx.course.passing.status.updated.v1",
"learning-badges-lifecycle",
"enabled",
)

################### Authoring API ######################

Expand Down
87 changes: 87 additions & 0 deletions lms/djangoapps/grades/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from crum import get_current_user
from django.conf import settings
from eventtracking import tracker
from openedx_events.learning.data import (
CcxCourseData,
CcxCoursePassingStatusData,
CourseData,
CoursePassingStatusData,
UserData,
UserPersonalData
)
from openedx_events.learning.signals import CCX_COURSE_PASSING_STATUS_UPDATED, COURSE_PASSING_STATUS_UPDATED

from common.djangoapps.course_modes.models import CourseMode
from common.djangoapps.student.models import CourseEnrollment
Expand Down Expand Up @@ -190,6 +199,45 @@ def course_grade_now_passed(user, course_id):
}
)

# produce to event bus
if hasattr(course_id, 'ccx'):
CCX_COURSE_PASSING_STATUS_UPDATED.send_event(
course_passing_status=CcxCoursePassingStatusData(
status=CcxCoursePassingStatusData.PASSING,
user=UserData(
pii=UserPersonalData(
username=user.username,
email=user.email,
name=user.get_full_name(),
),
id=user.id,
is_active=user.is_active,
),
course=CcxCourseData(
ccx_course_key=course_id,
master_course_key=course_id.to_course_locator(),
),
)
)
else:
COURSE_PASSING_STATUS_UPDATED.send_event(
course_passing_status=CoursePassingStatusData(
status=CoursePassingStatusData.PASSING,
user=UserData(
pii=UserPersonalData(
username=user.username,
email=user.email,
name=user.get_full_name(),
),
id=user.id,
is_active=user.is_active,
),
course=CourseData(
course_key=course_id,
),
)
)


def course_grade_now_failed(user, course_id):
"""
Expand All @@ -209,6 +257,45 @@ def course_grade_now_failed(user, course_id):
}
)

# produce to event bus
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be moved to the utility function, so the condition for checking course type isn't duplicated?

if hasattr(course_id, 'ccx'):
CCX_COURSE_PASSING_STATUS_UPDATED.send_event(
course_passing_status=CcxCoursePassingStatusData(
status=CcxCoursePassingStatusData.FAILING,
user=UserData(
pii=UserPersonalData(
username=user.username,
email=user.email,
name=user.get_full_name(),
),
id=user.id,
is_active=user.is_active,
),
course=CcxCourseData(
ccx_course_key=course_id,
master_course_key=course_id.to_course_locator(),
),
)
)
else:
COURSE_PASSING_STATUS_UPDATED.send_event(
course_passing_status=CoursePassingStatusData(
status=CoursePassingStatusData.FAILING,
user=UserData(
pii=UserPersonalData(
username=user.username,
email=user.email,
name=user.get_full_name(),
),
id=user.id,
is_active=user.is_active,
),
course=CourseData(
course_key=course_id,
),
)
)


def fire_segment_event_on_course_grade_passed_first_time(user_id, course_locator):
"""
Expand Down
152 changes: 148 additions & 4 deletions lms/djangoapps/grades/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@

from unittest import mock

from ccx_keys.locator import CCXLocator
from django.utils.timezone import now
from openedx_events.learning.data import (
CcxCourseData,
CcxCoursePassingStatusData,
CourseData,
PersistentCourseGradeData
CoursePassingStatusData,
PersistentCourseGradeData,
UserData,
UserPersonalData
)
from openedx_events.learning.signals import (
CCX_COURSE_PASSING_STATUS_UPDATED,
COURSE_PASSING_STATUS_UPDATED,
PERSISTENT_GRADE_SUMMARY_CHANGED
)
from openedx_events.learning.signals import PERSISTENT_GRADE_SUMMARY_CHANGED
from openedx_events.tests.utils import OpenEdxEventsTestMixin

from common.djangoapps.student.tests.factories import UserFactory
from common.djangoapps.student.tests.factories import AdminFactory, UserFactory
from lms.djangoapps.ccx.models import CustomCourseForEdX
from lms.djangoapps.grades.course_grade_factory import CourseGradeFactory
from lms.djangoapps.grades.models import PersistentCourseGrade
from lms.djangoapps.grades.tests.utils import mock_passing_grade
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory

Expand Down Expand Up @@ -94,5 +107,136 @@ def test_persistent_grade_event_emitted(self):
passed_timestamp=grade.passed_timestamp
)
},
event_receiver.call_args.kwargs
event_receiver.call_args.kwargs,
)


class CoursePassingStatusEventsTest(SharedModuleStoreTestCase, OpenEdxEventsTestMixin): # pylint: disable=missing-class-docstring
ENABLED_OPENEDX_EVENTS = [
"org.openedx.learning.course.passing.status.updated.v1",
]

@classmethod
def setUpClass(cls):
"""
Set up class method for the Test class.
"""
super().setUpClass()
cls.start_events_isolation()

def setUp(self): # pylint: disable=arguments-differ
super().setUp()
self.course = CourseFactory.create()
self.user = UserFactory.create()
self.receiver_called = False

def _event_receiver_side_effect(self, **kwargs): # pylint: disable=unused-argument
"""
Used show that the Open edX Event was called by the Django signal handler.
"""
self.receiver_called = True

def test_course_passing_status_updated_emitted(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docstring to this test case as well

event_receiver = mock.Mock(side_effect=self._event_receiver_side_effect)
COURSE_PASSING_STATUS_UPDATED.connect(event_receiver)
grade_factory = CourseGradeFactory()

with mock_passing_grade():
grade_factory.update(self.user, self.course)

self.assertTrue(self.receiver_called)
self.assertDictContainsSubset(
{
"signal": COURSE_PASSING_STATUS_UPDATED,
"sender": None,
"course_passing_status": CoursePassingStatusData(
status=CoursePassingStatusData.PASSING,
user=UserData(
pii=UserPersonalData(
username=self.user.username,
email=self.user.email,
name=self.user.get_full_name(),
),
id=self.user.id,
is_active=self.user.is_active,
),
course=CourseData(
course_key=self.course.id,
),
),
},
event_receiver.call_args.kwargs,
)


class CCXCoursePassingStatusEventsTest( # pylint: disable=missing-class-docstring
SharedModuleStoreTestCase, OpenEdxEventsTestMixin
):
ENABLED_OPENEDX_EVENTS = [
"org.openedx.learning.ccx.course.passing.status.updated.v1",
]

@classmethod
def setUpClass(cls):
"""
Set up class method for the Test class.
"""
super().setUpClass()
cls.start_events_isolation()

def setUp(self): # pylint: disable=arguments-differ
super().setUp()
self.course = CourseFactory.create()
self.user = UserFactory.create()
self.coach = AdminFactory.create()
self.ccx = ccx = CustomCourseForEdX(
course_id=self.course.id, display_name="Test CCX", coach=self.coach
)
ccx.save()
self.ccx_locator = CCXLocator.from_course_locator(self.course.id, ccx.id)

self.receiver_called = False

def _event_receiver_side_effect(self, **kwargs): # pylint: disable=unused-argument
"""
Used show that the Open edX Event was called by the Django signal handler.
"""
self.receiver_called = True

def test_ccx_course_passing_status_updated_emitted(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a docstring to this test case

event_receiver = mock.Mock(side_effect=self._event_receiver_side_effect)
CCX_COURSE_PASSING_STATUS_UPDATED.connect(event_receiver)
grade_factory = CourseGradeFactory()

with mock_passing_grade():
grade_factory.update(self.user, self.store.get_course(self.ccx_locator))

self.assertTrue(self.receiver_called)
self.assertDictContainsSubset(
{
"signal": CCX_COURSE_PASSING_STATUS_UPDATED,
"sender": None,
"course_passing_status": CcxCoursePassingStatusData(
status=CcxCoursePassingStatusData.PASSING,
user=UserData(
pii=UserPersonalData(
username=self.user.username,
email=self.user.email,
name=self.user.get_full_name(),
),
id=self.user.id,
is_active=self.user.is_active,
),
course=CcxCourseData(
ccx_course_key=self.ccx_locator,
master_course_key=self.course.id,
display_name="",
coach_email="",
start=None,
end=None,
max_students_allowed=self.ccx.max_student_enrollments_allowed,
),
),
},
event_receiver.call_args.kwargs,
)
Loading
Loading