How to create user update event?

How to create user update event?

by Коля Куликов -
Number of replies: 6

Hello,

I developing my plugin and adding an event on user update. I created events.php in db folder:

$observers = [
    [
        'eventname' => 'core\event\user_updated',
        'callback' => 'user_observer::updated',
    ],
];

and observer.php in classes folder:

class user_observer
{
        public static function updated(\core\event\user_updated $event): void
        {
                var_dump('test');
                 die();
        }
}
For example I have to get "test" in browser after update user, but it is not working. How to fix it?

Average of ratings: -
In reply to Коля Куликов

Re: How to create user update event?

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

Firstly, did you update your plugin's version number (in version.php) after adding the observer to the events.php file (Moodle doesn't look at that file unless the version number has changed).

Secondly, if you want Moodle to find your observer class, then you need to namespace it correctly.

Start your overserver.php file with:

namespace local_myplugin;

(This assumes your plugin is a 'local' plugin within the local/ directory of your Moodle install and that it is called 'myplugin' - adjust the namespace to match your actual plugin).

Then make sure your class is called 'observer' (not user_observer) - i.e. make sure it matches the name of the file it is stored in.

Finally, update the $observers array to set the callback to '\local_myplugin\observer::updated' (i.e. to match the full namespaced name of your class) and update the version number in version.php, so that Moodle can find it.

A little bit of explanation - using the 'local_myplugin' namepace means that Moodle knows to look in 'local/myplugin/classes' to try and find the class you have referenced. Calling the class 'observer', means Moodle knows to look for a file 'observer.php' in that directory.

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

Re: How to create user update event?

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi Davo,

Thanks for your advices. I lost time but also my mind by didn't understand how moodle manage it.
And I will just add that that also make me crazy before I read it.

"NOTE: Event observers are cached. If you add or change observers you need to purge the caches or they will not be recognised. Plugin developers need to bump up the version number to guarantee that the list of observers is reloaded during upgrade."
In reply to Коля Куликов

Ri: How to create user update event?

by Nicola Vallinoto -
Hi,
Davo is right! You need to add the namespace.
It worked the same with my local plugin.
Here you find my code just to have a code to take as reference:
Nicola

-- db\events.php --
<?php
defined('MOODLE_INTERNAL') || die();
$observers = [
 [
        'eventname' => '\core\event\user_updated',
        'callback'  => '\local_coursecompletion\local_coursecompletion_observer::user_update'
      ]


-- classes\local_coursecompletion_observer.php --
<?php
namespace local_coursecompletion;
defined('MOODLE_INTERNAL') || die();
class local_coursecompletion_observer {
public static function user_update(\core\event\user_updated $event) {
//  function code
}

In reply to Nicola Vallinoto

Re: Ri: How to create user update event?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Just to add - in theory if your class is local_coursecompletion_observer, then you don't need the namespace (you'd have to remove it from both the 'callback' and the class file), as Moodle also knows how to find {plugintype}_{pluginname}_{classname} (as long as the class is called something like 'local_coursecompletion_observer' and the filename is 'observer.php').

However, I'd recommend using namespaces, but in your case, simplifying your class name - change the filename to 'observer.php' and the class name to 'observer' - there's no need to have a namespace AND include the plugin name in the class name (it doesn't break things, it's just messy).
In reply to Davo Smith

Ri: Re: Ri: How to create user update event?

by Nicola Vallinoto -
Hi Davo,
if you "include the plugin name in the class name" you should also change the file name. Right ? Or it works also if class name and file name are different ?
In reply to Nicola Vallinoto

Re: Ri: Re: Ri: How to create user update event?

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

I think the developer documentation covers it in a lot more detail than I can write here: https://docs.moodle.org/dev/Automatic_class_loading

Short version: namespace the file (with the full plugin type + name), don't include the plugin name in the class name (the old version - no namespace, include the plugin name in the class, but not the filename - is strongly discouraged).