Issues getting event listeners working

Issues getting event listeners working

by Erik Fox -
Number of replies: 6

I am new to moodle development as well as php. I am trying to get some event listeners working in moodle 3.5. At this point I am just trying to get some output in the console when an event is triggered. My plugin is in the form of a block. Here is my file structure:

Here is the code in events.php:

defined('MOODLE_INTERNAL') || die();
$observers = array (
array (
'eventname' => '\core\event\user_loggedin',
'callback' => 'block_simplehtml_observer::update',
),
array (
'eventname' => '\core\event\user_loggedout',
'callback' => 'block_simplehtml_observer::update',
),
array (
'eventname' => '\core\event\course_module_created',
'callback' => 'block_simplehtml_observer::update',
),
);

Here is the code in observer.php

defined('MOODLE_INTERNAL') || die();
class block_simplehtml_observer {
public static function update(\core\event\base $event) {
// This code works when put inside the corresponding 'store' function in blocks/recent_activity/classes/observer.php
$json_data = json_encode($event->get_data());
echo("<script>console.log('EVENT: ".$json_data."');</script>");
}
}

The rest of the files directly follow the simplehtml block tutorial on moodles development site. I am not sure whether there is something that I need to do in another file to get this working or I am doing something incorrectly in these two files. I have been trying to debug this for a couple of hours by looking at existing events/observer files with no luck. Any help would be much appreciated.

Average of ratings: -
In reply to Erik Fox

Re: Issues getting event listeners working

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Have you bumped the version number in version.php? Until you do that, Moodle will not parse your db/events.php file to find any new observers.

You do not appear to be using automatic class loading to load the class that handles the events (I'd expect the observer function to be called '\block_simplehtml\observer::update' in the events.php, and the file to be stored in classes/observer.php, with: "namespace \block_simplehtml;" (without quotes) at the top of it). If you're not using automatic class loading, you'll need to specify the name of the file that contains the handler code, via an 'includefile' entry in the capabilities array.

I'd also strongly recommend against adding echo statements to observers - the event is usually triggered before any page output is started, so adding echo statements will almost certainly break the HTML of the page and will also break any redirect() statements found after the event is triggered.


Average of ratings: Useful (2)
In reply to Davo Smith

Re: Issues getting event listeners working

by Erik Fox -

Bumping the version number worked. I guess I wasn't aware that things in the db folder are stored in the cache. I am actually using automatic class loading and that is working perfectly fine. The echo statements are temporary and just a way for me to easily see that my code is working and what information comes with the events until I start displaying data in the actual block. Thank you for the help!

In reply to Davo Smith

Re: Issues getting event listeners working

by Alfonso Moscato -

Hi Davo, 

quite interesting. I found different instrutions, and indeed it's not working.

I am subscribing to the event course_completed. 

I read I have to install my plugin in local. It is called mqcc.

So I put in local/mqcc/classes observer.php that is:

<?php
class local_mqcc_observer {
public static function salva(\core\event\course_completed $event) {
global $DB, $CFG;
//require_once('../../../mqlogin/corso_completato.php'); //include file that contains custom function
//SalvaUtente($event);
$fp=fopen("pippo.txt","a+");
fwrite($fp,"evento lanciato\r\n");
fwrite($fp,print_r($event,true));
fclose($fp);
}
}
?>

and in local/mqcc/db events.php, that is:

<?php
$observers = array(
array(
'eventname' => '\core\event\course_completed',
'callback' => 'local_mqcc_observer::salva',
'internal' => false
)
);
?>

Finally, in local/mqcc I have version.php, that is:

defined('MOODLE_INTERNAL') || die();
$plugin->version = '1.1';
//$plugin->requires = TODO;
$plugin->component = 'local_mqcc';
$plugin->maturity = MATURITY_STABLE;

Am I doing something wrong?
Thanks for your help, I am getting crazy!
Alfonso


In reply to Alfonso Moscato

Re: Issues getting event listeners working

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

First, drop the "?>" tags from the end of the PHP files - having them there can lead to all sorts of obscure problems.

Your plugin version number looks wrong - it should be something like: 2019051000 (10th May 2019, version 00), this will allow it to work well with the version numbers on the rest of the site.

I've never tried to use automatic class loading in the way you are doing, so I don't know if it works. The usual practice is to write:


namespace local_mqcc;

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

class observer {

Then in db/events.php

$observers = array(
 array(
 'eventname' => '\core\event\course_completed',
 'callback' => '\local_mqcc\observer::salva',
 )
 );

In reply to Alfonso Moscato

Re: Issues getting event listeners working

by Alfonso Moscato -

I have modified files like you suggested:

events.php


$observers = array(
array(
'eventname' => '\core\event\course_completed',
'callback' => 'local_mqcc\observer::salva',
'internal' => false
)
);

observer.php

namespace local_mqcc;
defined('MOODLE_INTERNAL') || die();
class observer {
public static function salva(\core\event\course_completed $event) {
global $DB, $CFG;
$fp=fopen("pippo.txt","a+");
fwrite($fp,"evento lanciato\r\n");
fwrite($fp,print_r($event,true));
fclose($fp);
}
}
and finally version.php now is:

defined('MOODLE_INTERNAL') || die();
$plugin->version = '20190510';
$plugin->component = 'local_mqcc';
$plugin->maturity = MATURITY_STABLE;


I have  cleared the cache, but still no way.
I have enabled notifications and every time an user completes a course I receive an email, but the event is not fired.
Is there a way to debug this, i.e. to undestand why Moodle is not firing the event or is not launching the plugin?
Thanks
Alfonso



In reply to Alfonso Moscato

Re: Issues getting event listeners working

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The easiest way to debug this would be to use xdebug + and IDE that can work with it (e.g. PHPStorm, the IDE I use). That way you can put a breakpoint in the course completion code, then trace through to the event handlers and hopefully see why it is not triggering your code.