1

I have created a plugin at this location moodle/local/redirectafterlogin with the following structure :

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

version.php:

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

$plugin->version = 20170333;
$plugin->requires = 2015111000;
$plugin->component = 'local_redirectafterlogin';

events.php:

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

    $observers = array(
        array(
            'eventname' => 'core\event\user_loggedin',
            'callback' => 'local_redirectafterlogin_observer::user_loggedin',
        ),
        array(
            'eventname' => 'core\event\user_loggedout',
            'callback' => 'local_redirectafterlogin_observer::user_loggedin',
        ),
    );

observer.php:

class local_redirectafterlogin_observer
{
    public static function user_loggedin(core\event\base $event)
    {
        $event_data = $event->get_data();
        var_dump($event_data);
        die();
    }
}

Cached has been cleared a lot of time and version number has been bumped too but the callback is never called!

  1. What's wrong, why does the callback is not triggered?
  2. How can I debug events (is there a way in Moodle to see dispatched events)?
ben.IT
  • 1,490
  • 2
  • 18
  • 37

3 Answers3

4

It was a structure mistake! I put the classes folder containing observer.php in the db folder! After moving the classes folder at the root of the plugin as follow, this is ok, observer is triggered!

Structure that is ok:

redirectafterlogin/
├── classes
│   └── observer.php
├── db
│   └── events.php
└── version.php
ben.IT
  • 1,490
  • 2
  • 18
  • 37
  • This one helped successfully trigging the user logged in and logged out event in my plugin. I am struggling with course viewed event, no matter what i try it never gets triggered :( can you help me please? events.php: https://prnt.sc/xrJBNskpMRLd observer.php: https://prnt.sc/o8EmJzkPqV1X – Ali Jan 28 '23 at 10:30
1

I don't think the automatic class loading will be able to find your observer class.

Try adding to the top of the observer class file:

namespace local_redirectafterlogin;

Then change the events.php to:

'callback' => 'local_redirectafterlogin\local_redirectafterlogin_observer'

(You could also greatly shorten the class name, now it is namespaced). Make sure you bump the version number, to reload the events.php file.

You may want to reconsider the naming of your plugin, though, as redirection is not allowed from within an event handler, as that would cause a great many problems.

davosmith
  • 6,037
  • 2
  • 14
  • 23
  • The purpose of that plugin is just to redirect to a `user/edit.php` page when some extra profile fields are not filled. Why do you say that redirection is not allowed in event handler? In my opinion, the `core\event\user_loggedin` event is dispatched to perform that kind of operation? Isn't it? – ben.IT Apr 04 '17 at 07:45
  • Events are often dispatched part way through some processing (for example, you might have a plugin that creates a new user, then enrols them on the course - that would trigger a 'user_created' event and then, later, a 'user_enrolment_created' event; alternatively a script that created multiple user accounts would generate multiple events). Events may also be handled by multiple handlers. If you allowed an event handler to redirect part way through this processing then the site could be left in a very broken state. – davosmith Apr 04 '17 at 07:51
0

I made the experience that var_dump or output do stdout gets not trigggert. As quick "debug" i use file_put_contents to a temp logfile

Bearzi
  • 538
  • 5
  • 18