Skip to content

Latest commit

 

History

History
261 lines (186 loc) · 11.4 KB

events.rst

File metadata and controls

261 lines (186 loc) · 11.4 KB

Open edX Events

How to use

Using openedx-events in your code is very straight forward. We can consider the two possible cases, sending or receiving an event.

Receiving events

This is one of the most common use cases for plugins. The edx-platform will send an event and you want to react to it in your plugin.

For this you need to:

  1. Include openedx-events in your dependencies.
  2. Connect your receiver functions to the signals being sent.

Connecting signals can be done using regular django syntax:

from openedx_events.learning.signals import SESSION_LOGIN_COMPLETED

@receiver(SESSION_LOGIN_COMPLETED)
# your receiver function here

Or at the apps.py

"signals_config": {
    "lms.djangoapp": {
        "relative_path": "your_module_name",
        "receivers": [
            {
                "receiver_func_name": "your_receiver_function",
                "signal_path": "openedx_events.learning.signals.SESSION_LOGIN_COMPLETED",
            },
        ],
    }
},

In case you are listening to an event in the edx-platform repo, you can directly use the django syntax since the apps.py method will not be available without the plugin.

Sending events

Sending events requires you to import both the event definition as well as the attr data classes that encapsulate the event data.

from openedx_events.learning.data import UserData, UserPersonalData
from openedx_events.learning.signals import STUDENT_REGISTRATION_COMPLETED

STUDENT_REGISTRATION_COMPLETED.send_event(
    user=UserData(
        pii=UserPersonalData(
            username=user.username,
            email=user.email,
            name=user.profile.name,
        ),
        id=user.id,
        is_active=user.is_active,
    ),
)

You can do this both from the edx-platform code as well as from an openedx plugin.

Testing events

Testing your code in CI, specially for plugins is now possible without having to import the complete edx-platform as a dependency.

To test your functions you need to include the openedx-events library in your testing dependencies and make the signal connection in your test case.

from openedx_events.learning.signals import STUDENT_REGISTRATION_COMPLETED

def test_your_receiver(self):
    STUDENT_REGISTRATION_COMPLETED.connect(your_function)
    STUDENT_REGISTRATION_COMPLETED.send_event(
        user=UserData(
            pii=UserPersonalData(
                username='test_username',
                email='[email protected]',
                name='test_name',
            ),
            id=1,
            is_active=True,
        ),
    )

    # run your assertions

Changes in the openedx-events library that are not compatible with your code should break this kind of test in CI and let you know you need to upgrade your code.

Live example

For a complete and detailed example you can see the openedx-events-2-zapier plugin. This is a fully functional plugin that connects to STUDENT_REGISTRATION_COMPLETED and COURSE_ENROLLMENT_CREATED and sends the relevant information to zapier.com using a webhook.

Index of Events

This list contains the events currently being sent by edx-platform. The provided links target both the definition of the event in the openedx-events library as well as the trigger location in this same repository.

Learning Events

Name Type Date added
STUDENT_REGISTRATION_COMPLETED org.openedx.learning.student.registration.completed.v1 2022-06-14
SESSION_LOGIN_COMPLETED org.openedx.learning.auth.session.login.completed.v1 2022-06-14
COURSE_ENROLLMENT_CREATED org.openedx.learning.course.enrollment.created.v1 2022-06-14
COURSE_ENROLLMENT_CHANGED org.openedx.learning.course.enrollment.changed.v1 2022-06-14
COURSE_UNENROLLMENT_COMPLETED org.openedx.learning.course.unenrollment.completed.v1 2022-06-14
CERTIFICATE_CREATED org.openedx.learning.certificate.created.v1 2022-06-14
CERTIFICATE_CHANGED org.openedx.learning.certificate.changed.v1 2022-06-14
CERTIFICATE_REVOKED org.openedx.learning.certificate.revoked.v1 2022-06-14
COHORT_MEMBERSHIP_CHANGED org.openedx.learning.cohort_membership.changed.v1 2022-06-14
COURSE_DISCUSSIONS_CHANGED org.openedx.learning.discussions.configuration.changed.v1 2022-06-14

Content Authoring Events

Name Type Date added
COURSE_CATALOG_INFO_CHANGED org.openedx.content_authoring.course.catalog_info.changed.v1 2022-08-24
XBLOCK_PUBLISHED org.openedx.content_authoring.xblock.published.v1 2022-12-06
XBLOCK_DELETED org.openedx.content_authoring.xblock.deleted.v1 2022-12-06
XBLOCK_DUPLICATED org.openedx.content_authoring.xblock.duplicated.v1 2022-12-06
XBLOCK_CREATED org.openedx.content_authoring.xblock.created.v1 2023-07-20
XBLOCK_UPDATED org.openedx.content_authoring.xblock.updated.v1 2023-07-20
COURSE_CREATED org.openedx.content_authoring.course.created.v1 2023-07-20
CONTENT_LIBRARY_CREATED org.openedx.content_authoring.content_library.created.v1 2023-07-20
CONTENT_LIBRARY_UPDATED org.openedx.content_authoring.content_library.updated.v1 2023-07-20
CONTENT_LIBRARY_DELETED org.openedx.content_authoring.content_library.deleted.v1 2023-07-20
LIBRARY_BLOCK_CREATED org.openedx.content_authoring.library_block.created.v1 2023-07-20
LIBRARY_BLOCK_UPDATED org.openedx.content_authoring.library_block.updated.v1 2023-07-20
LIBRARY_BLOCK_DELETED org.openedx.content_authoring.library_block.deleted.v1 2023-07-20
LIBRARY_COLLECTION_CREATED org.openedx.content_authoring.content_library.collection.created.v1 2024-08-23
LIBRARY_COLLECTION_UPDATED org.openedx.content_authoring.content_library.collection.updated.v1 2024-08-23
LIBRARY_COLLECTION_DELETED org.openedx.content_authoring.content_library.collection.deleted.v1 2024-08-23
CONTENT_OBJECT_ASSOCIATIONS_CHANGED org.openedx.content_authoring.content.object.associations.changed.v1 2024-09-06