Actions

Developer Area/Events API: Difference between revisions

From Mahara Wiki

< Developer Area
(Created page with "Mahara has a simple '''Event Subscription API''' that lets plugins respond to actions performed throughout the system. 1. Core code triggers an event by calling <tt>handle_event…")
 
No edit summary
Line 7: Line 7:
=== Subscribing to an Event ===
=== Subscribing to an Event ===


To subscribe your Plugin to an event type, override the public static function "get_event_subscription()" in your Plugin subclass. Your function should return an object containing three fields:
To subscribe your Plugin to an event type, override the public static function "get_event_subscription()" in your Plugin subclass. Your function should return an array of event subscription objects. Each event subscription object should be a generic object containing three fields:


* '''plugin''': The name of the plugin
* '''plugin''': The name of the plugin
Line 20: Line 20:
   
   
     public static function get_event_subscriptions() {
     public static function get_event_subscriptions() {
         return array(
         $sub = new stdClass();
            (object)array(
        $sub->plugin = 'blog';
                'plugin'      => 'blog',
        $sub->event = 'createuser';
                'event'        => 'createuser',
        $sub->callfunction = 'create_default_blog';
                'callfunction' => 'create_default_blog',
         return array($sub);
            ),
     }
         );
     }  
   
   
     public static function create_default_blog($event, $user) {
     public static function create_default_blog($event, $user) {

Revision as of 15:26, 15 July 2013

Mahara has a simple Event Subscription API that lets plugins respond to actions performed throughout the system.

1. Core code triggers an event by calling handle_event('eventtype', $data); 2. A table of event subscriptions is called. Each subscription specifies a plugin, and a function to be called should that event occur. 3. Each event subscription function is called. 'eventtype', and $data are passed to it.

Subscribing to an Event

To subscribe your Plugin to an event type, override the public static function "get_event_subscription()" in your Plugin subclass. Your function should return an array of event subscription objects. Each event subscription object should be a generic object containing three fields:

  • plugin: The name of the plugin
  • event: The name of the event you want to subscribe to
  • callfunction: The (public static) function in the plugin's Plugin subclass, which should be called when the event occurs.

Example:

The blog core plugin subscribes to the createuser event, in order to create a default Journal for each newly created user. Here's what the subscription-related code looks like in artefact/blog/lib.php:

class PluginArtefactBlog extends PluginArtefact { 

    public static function get_event_subscriptions() {
        $sub = new stdClass();
        $sub->plugin = 'blog';
        $sub->event = 'createuser';
        $sub->callfunction = 'create_default_blog';
        return array($sub);
    }

    public static function create_default_blog($event, $user) {
        $name = display_name($user, null, true);
        $blog = new ArtefactTypeBlog(0, (object) array(
            'title'       => get_string('defaultblogtitle', 'artefact.blog', $name),
            'owner'       => $user['id'],
        ));
        $blog->commit();
    }
}