Simple question about Moodle Events

Simple question about Moodle Events

by Paul M -
Number of replies: 2

After reading the documentation on Moodle Events (https://docs.moodle.org/dev/Event_2) I have a simple question about them which I'm guessing somebody could quickly confirm that my understand is correct (before I go ahead and test it).

I have an existing plugin which through an overnight cron job, takes all the data in mdl_user_info_data and normalises it into a new table, where every additional profile field is a column in the table (called userdata)

I'm now looking to improve this plugin to make the updates to my plugin table instant, rather than wait for the overnight cron job. From my understanding of the Events this should be possible by:

  • adding a file db/events.php to my plugin
  • listen for core/event/user_updated within the $observers array and call my custom function (defined within the callback param). My custom function would update my userdata table when a user profile is updated.
  • I could also 'observe' other events such as user_created and user_deleted to do other things on my custom table.

Is my understanding correct and do other authentication methods trigger the same events, for example if a user is created via LDAP authentication would it trigger the same user_created event?

Thanks in advance

Average of ratings: -
In reply to Paul M

Re: Simple question about Moodle Events

by Michael Aherne -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

Yes, that sounds about right. I've generally found that core Moodle plugins "do the right thing" with events (e.g. auth plugins will call user_create_user() when creating a user, triggering an event), but if you need 100% accuracy the only way to be sure is to check the code!

If you're using Moodle 2.7 or higher, it might also be worth considering creating ad-hoc tasks rather than updating your database directly in the event handler. It's pretty easy to introduce performance problems with event handlers that are doing anything but the simplest things, so it's often preferable just to create an ad-hoc task that will be run next time the cron job runs. It's not instant, but it sounds like that might not matter for your use case.

In reply to Michael Aherne

Re: Simple question about Moodle Events

by Paul M -

Great - thanks for your help.