Skip to content
Advertisement

Moodle Events API

I’m trying to develop a plugin for Moodle, using Events API that will send out emails when new blog post is created. I created my plugin with the following structure:

blog_observer/
├── classes
│   └── observer.php
├── db
│   └── events.php
└── version.php

db/events.php:

defined('MOODLE_INTERNAL') || die();

$observers = array(
    array(
        'eventname'   => 'coreeventblog_entry_created',
        'callback'    => 'blog_observerblog_observer::blog_created',
    ),
);

I got the event name from here /blog/locallib.php.

classes/observer.php:

namespace blog_observer;

defined('MOODLE_INTERNAL') || die();

class blog_observer {

    /**
     * The observer monitoring all the events.
     *
     * @param coreeventbase $event event object.
     * @return bool
     */
    public static function blog_created(coreeventbase $event) {
        file_put_contents('php://stderr', print_r("observed an event", TRUE));

        return false;

    }
}

However I don’t see that log, as if it doesn’t even get to that class, why ?

Advertisement

Answer

I figured it out, it was the namespaces…

So the updated files are (I also renamed my plugin to newsletter):

newsletter/
├── classes
│   └── observer.php
├── db
│   └── events.php
└── version.php

db/events.php:

defined('MOODLE_INTERNAL') || die();

$observers = array(
    array(
        'eventname'   => 'coreeventblog_entry_created',
        'callback'    => 'local_newsletterobserver::blog_created',
    ),
);

classes/observer.php:

namespace local_newsletter;

defined('MOODLE_INTERNAL') || die();

class observer {

// The rest is same here

}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement