I am trying to observe some Moodle events but it doesn't seem to be working. Can you help?
I tried the following in Moodle 2.5.1...
In my theme's db/events.php file, I added:
$handlers = array (
'course_category_created' => array (
'handlerfile' => '/theme/mytheme/lib.php',
'handlerfunction' => 'mytheme_course_category_created',
'schedule' => 'instant',
'internal' => 1,
),
);
In my theme's lib.php file, I added:
function mytheme_course_category_created(stdClass $event) {
error_log('DEBUG MESSAGE - course_category_created event triggered.');
return true;
}
Next I created a new course category but the event was not triggered.
Then I tried the following in Moodle 2.6.1...
In my theme's db/events.php file, I added:
$observers = array (
array (
'eventname' => '\core\event\course_category_created',
'callback' => 'theme_mytheme_observer:course_:category_created',
)
);
In my theme's classes/observer.php, I added:
class theme_mytheme_observer {
public static function course_category_created(\core\event\course_category_created $event) {
error_log('DEBUG MESSAGE - \core\event\course_category_created event triggered.');
return;
}
}
Last I created a new category but again, the event was not triggered.
Any idea why the event is not being triggered? I suspect either:
* My code is wrong;
* Creating a new category doesn't trigger this particular event (observing wrong event)
* There is a bug in Moodle and the event doesn't ever get triggered.
If you have any insights, I would really appreciate hearing from you. I will also need to do this with course_category_updated and course_category_deleted. All of these events are listed in Moodle's own /lib/db/events.php.
My requirement is to get it working in Moodle 2.6 however it would be nice to have it backwards compatible with 2.5.1.
Best regards,
Michael