Technology used:
- Moodle 4.3
- XAMPP
- PHP
- MySQL
Moodle is a Learning Management System (LMS) that is used by academic organizations to provide instructors with the ability to manage students’ progresses, organize teaching materials, and customize their own teaching methods.
Moodle’s functionalities benefit from its rich plugin (official and custom) library. This project’s purpose is to enhance and customize the notifications feature within Moodle.
My work
I developed a local plugin for Moodle which allows each course to send notifications webhooks to its custom endpoint.
Moodle Webhook plugin works as follows: an instructor “Course A” creates an announcement for the final exam which takes place next week. The data of this announcement will then be sent to the endpoint URL assigned to “Course A”. Similarly, each announcement of each course will be sent to that course’s endpoint.
This is achieved using Moodle’s Events API and Custom fields.
Firstly, I created a course custom field to store the endpoint to which we want the webhooks to be sent. Doing this allows us to sent notifications to different endpoints correlated to each course.
Furthermore, data in course custom field can be modified anytime via course settings.
Secondly, I needed to create an event observer and handler for each type of event that we want to be notified of. For example, if we want to be notified when a new discussion is created we need to:
- Add the event we want to catch (mod_forum\event\discussion_created)
events.php
- Create the handler for the event. Noted that the logic for the handler has to be queued using adhoc task so that it can be run in the background without impacting the performance of the site.
observer.php
- Create the logic for the handler (extract event data->get course’s endpoint->add relevant info->send data)
discussion_create_noti_task.php
Now, whenever a discussion is created in any course, the event handler will fetch the event’s data, query necessary information such as course name, instructor name, etc., attach them to a JSON payload, and finally send it to the endpoint correlated to that course in which the discussion was created.
From here, we can use the information within the payload and display the webhook such as:
‘{instructor}’ has created an announcement ‘{message}’
with {instructor} being the instructor name and {message} being the newly created discussion’s content.
Acknowledgement
Moodle Webhook Plugin is inspired by Valentino’s plugin. Valentino’s exceptional work on enabling Moodle users to generate webhooks for various event types, each linked to a specific endpoint, deserves commendation. The key difference between Valentino’s project and mine lies in the scope: Valentino’s webhook operates on a site-wide level, whereas my plugin operates within the confinements of individual courses.
Consequently, when an event is activated, Moodle dispatches a webhook to the designated endpoint irrespective of the course. Simply put, my plugin caters to a more specialized and targeted scenario that Valentino’s solution does not encompass.