Using Open edX Events in the LMS Service

Using Open edX Events in the LMS Service#

Live Example#

For a complete and detailed example you can see the openedx-events-2-zapier plugin and its documentation. This is a fully functional plugin that connects to STUDENT_REGISTRATION_COMPLETED and COURSE_ENROLLMENT_CREATED and sends the relevant information to Zapier or any other services.

Let’s see it working!

Setup Your Environment#

This tutorial assumes you are using Tutor > 18.x and its default services are already provisioned.

Installation#

For this tutorial to work, you will need a openedx image with the following package installed:

You can use your preferred method to install extra requirements in Tutor.

Configuration#

The package we just installed is a Django plugin, which adds additional configurations to our working environment thanks to the extension mechanisms that were put in place. Now, event receivers are listening to the registration and enrollment events sent within the LMS service.

The following is the implementation for the event receivers listening for the event STUDENT_REGISTRATION_COMPLETED:

# File openedx_events_2_zapier/apps.py
class OpenedxEvents2ZapierConfig(AppConfig):
    """
    Configuration for the openedx_events_2_zapier Django application.
    """

    name = "openedx_events_2_zapier"

    plugin_app = {
        "settings_config": {
            "lms.djangoapp": {
                "common": {"relative_path": "settings.common"},
                "test": {"relative_path": "settings.test"},
                "production": {"relative_path": "settings.production"},
            },
            "cms.djangoapp": {
                "common": {"relative_path": "settings.common"},
                "test": {"relative_path": "settings.test"},
                "production": {"relative_path": "settings.production"},
            },
        },
    }

    def ready(self):
        """Perform initialization tasks required for the plugin."""
        from openedx_events_2_zapier import handlers

# File openedx_events_2_zapier/handlers.py
@receiver(STUDENT_REGISTRATION_COMPLETED)
def send_user_data_to_webhook(
    signal, sender, user, metadata, **kwargs  # pylint: disable=unused-argument
):
    zapier_payload = {
        "user": asdict(user),
        "event_metadata": asdict(metadata),
    }
    send_data_to_zapier.delay(settings.ZAPIER_REGISTRATION_WEBHOOK, zapier_payload)

Those event receivers work out of the box after the plugin installation. Now, we must set the plugin settings which indicate where to send the events data. For this, go to env/apps/openedx/settings/development.py and add your Zapier configuration:

ZAPIER_REGISTRATION_WEBHOOK = "https://hooks.zapier.com/hooks/catch/<account>/<webhook>/"
ZAPIER_ENROLLMENT_WEBHOOK = "https://hooks.zapier.com/hooks/catch/<account>/<webhook>/"

Getting data from Zapier#

Now that you have configured both event receivers, you’ll need to trigger the events so you receive the events data in Zapier. Try it out!

Warning

The event receiver function in this tutorial is designed to be lightweight, serving as a basic example of event receivers. However, it has not been tested in a production environment. When implementing an event receiver for real-world use, consider additional factors such as error handling, logging, and performance.

Maintenance chart

Review Date

Reviewer

Release

Test situation

2025-02-13

Maria Grimaldi

Sumac

Pass.