Triggering Events / Hooks

Re: Triggering Events / Hooks

by Juho Jaakkola -
Number of replies: 0

I know this is a pretty old thread but I just decided to open up the subject again.

I've just started working as Moodle plugin developer (with Moodle 2.3.1) and I'm disappointed that I haven't found the kind of hooks system and modularity as I was expecting from Moodle.

 

Some examples

1. When user requests a new password, instead of just sending an email Moodle core would ask if plugins have anything to say for this. There could for example be a plugin that would want to send an SMS message instead of an email.

2. I would like to dynamically add any new menu items from my plugin. For example: My Courses > Some course > Myplugin stuff.

3. I go to page My courses > Some course > Participants. On this page it is possible to select users and then choose an action from the dropdown box with the options "Send message", "Add a new note" and "Add a common note". I would like to add a new option to the list, but this is impossible since the options list has been hard coded intsead of being dynamically generated.

At the moment I can't do any of these without messing with the core code.

For some people the solution is to just hack the core. However if you are a Moodle hosting company with n hundred customers, it is not an option. It is a nightmare if you have to manually reapply and test your hacks in the core every time you want to upgrade your Moodle version.

 

Performance

It' been said in this thread that firing events for minor things would hurt performance. Well it would if there were 100 plugins and each of them wanted to run their own functionalities every time an event is triggered. In practise however there really wouldn't be that many events per one action. And if the event handlers would be registered in advance the calling event trigger wouldn't have to ask each of the 100 plugins whether they want to do something but instead it would just trigger the event handlers in the 1-3 plugins that have registered for that event.

 

Implementation in Elgg

Lately I have been working a lot with Elgg (which is a open source social networking engine) so I use it as an example. Elgg has both an events system and a plugin hook system. 

Events are triggered when anything is created, updated or deleted or when the Elgg framework is loading. http://docs.elgg.org/wiki/Elgg_Events

Plugin hooks are more general purpose than events but basically they are triggered when an action has occurred and parts of that action can be added to or overridden by a plugin. http://docs.elgg.org/wiki/Plugin_Hooks

Here's a list of all available Elgg events: http://docs.elgg.org/wiki/What_events_are_available_in_the_Elgg_core%3F
And plugin hooks: http://docs.elgg.org/wiki/What_plugin_hooks_are_available_in_the_Elgg_core%3F

Similar kind of system is something I would like Moodle to also have.

(Drupal also has a very flexible hooks system but I haven't worked with Drupal for a long time so I won't pick any examples from there.)

 

Plans for Moodle?

Are there currently any long terms plans for adding more flexible and extensive hooks system(s)?