Event handling in plugin...

Event handling in plugin...

by Peter Eliyahu Kornfeld -
Number of replies: 14

Hi,

I'm in the process of adding an event handler to my plugin, but it seems that the event does not reach the handler...

The code part:

<?php
// in db/events.php
$observers = array(
	array(
		'eventname'	=> '\core\event\user_graded',
		'callback'	=> 'ws_rashim_event_handlers::grades_export_handler',
	)
);
?>
<?php
// in classes/observer.php
class ws_rashim_event_handlers {
	public static function grades_export_handler($eventdata) {
		global $CFG;
		global $DB;
	
		file_put_contents('grade_event.txt', print_r($eventdata, true));
		// more code
	}
}
?>

Now, when I'm updating any grade of any course I would excpect a 'grade_event.txt' file appears, but nothing happening...

Any hint what wrong with this?

Thank you,

Peter

Average of ratings: -
In reply to Peter Eliyahu Kornfeld

Re: Event handling in plugin...

by Michael Aherne -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
It seems most likely that your observer class isn't being autoloaded. You could test this by adding something like this to your observer definition array:

'includefile' => 'path/to/classes/observer.php'
If that makes it work, you'd be best to remove it again and get the class autoloading working.
In reply to Michael Aherne

Re: Event handling in plugin...

by Frédéric Massart ⭐ -
Picture of Core developers Picture of Plugin developers Picture of Testers

Good thinking. It may also be that the caches haven't been purged. Purging the cache will ensure the autoloaded classes list is up to date.

More importantly, when adding a new observer it is important to change the version of your plugin. That forces Moodle to update another cache containing the list of existing observers.

Edit: To make your class autoloaded, keep the same file name but rename it to: nameofplugin_observer. Example: mod_forum_observer.
Average of ratings: Useful (1)
In reply to Frédéric Massart ⭐

Re: Event handling in plugin...

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

Does purging the caches rebuild the class map? That's good to know! For some reason I thought it didn't, but can't remember why now.

In reply to Frédéric Massart ⭐

Re: Event handling in plugin...

by Peter Eliyahu Kornfeld -

Purging all caches doesn't seems to help... (uninstalling of the plugin, shouldn't clean all traces of it?)

Also changing the version in version.php does not help...

Nut sure about your last bit, but changed the class name in observer.php (and references) to local_ws_rashim_observer... No good...

In reply to Michael Aherne

Re: Event handling in plugin...

by Peter Eliyahu Kornfeld -

Added this line to the definition in events.php

'includefile' => __DIR__.'/local/ws_rashim/classes/observer.php'

No changes at all...

In reply to Peter Eliyahu Kornfeld

Re: Event handling in plugin...

by Michael Aherne -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Did you try:

'includefile' => $CFG->dirroot.'/local/ws_rashim/classes/observer.php'
In reply to Michael Aherne

Re: Event handling in plugin...

by Peter Eliyahu Kornfeld -

Now that you suggested I did... No good...

I also checked the source code for mod_forum to see how it built and can't spot the difference... (checked version.php, db/events.php and classes/observer.php)

In reply to Michael Aherne

Re: Event handling in plugin...

by Peter Eliyahu Kornfeld -

Cloud it be that '\core\event\user_graded' is not the right event?

I'm trying to catch any grade changes for any user...

In reply to Peter Eliyahu Kornfeld

Re: Event handling in plugin...

by Dimitar Ivanov -

1. Is the event you are interested in logged in the logs?

2. Is cron running? You have to either set it up, or run it manually from CLI to test your observer.


In reply to Dimitar Ivanov

Re: Event handling in plugin...

by Peter Eliyahu Kornfeld -

1. Yes. I have the 'User graded' event in the log

2. I'm not sure what do you suggest here... No cron running at all (actually it is a Windows machine)... Can you explain (or point me to some) why it is important (I'm in a process of updating old code from 1.9 and the grade update was running there without cron.)

In reply to Peter Eliyahu Kornfeld

Re: Event handling in plugin...

by Víctor Déniz Falcón -

Hi Peter.

In this URL (Administration|Site administration|Reports|Events list):

http://yourmoodlesite/report/eventlist/eventdetail.php?eventname=\core\event\user_graded

is the list of plugins observing the event. Is it in this list your plugin?

In reply to Víctor Déniz Falcón

Re: Event handling in plugin...

by Peter Eliyahu Kornfeld -

Plugins observing this event

Log store manager (tool_log)

Event monitor (tool_monitor)

Synchronizer for Michlol (local_ws_rashim)


The last one is mine...

In reply to Peter Eliyahu Kornfeld

Re: Event handling in plugin...

by Dimitar Ivanov -

Peter, I suggested to check Cron, because in my experience some events get triggered by it. But since you see the event you are interested in the log, then this is not the case here.

You can read more about Cron and why you need it here: https://docs.moodle.org/31/en/Cron, this covers the Windows side of things: https://docs.moodle.org/31/en/Cron_with_Windows_OS


About your event, my best guess is that you have some PHP error in your observer and that's why it is failing. You should enable debug level logging and look at the error logs of the web server to see what happens when the event is triggered.

Average of ratings: Useful (1)
In reply to Dimitar Ivanov

Re: Event handling in plugin...

by Olumuyiwa Taiwo -
Picture of Plugin developers

Could this be a permissions issue? You may want to check your web server logs for errors related to that.