简体   繁体   中英

Moodle: Getting user id when a user is logged in

In order to keep login statistics for my moodle site I need to keep the time that a user logins and logouts. In order to do that I utilize the moodle's observer:

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

And the following event listener class:

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

class observer 
{
    public static function recordloginTime()
    {
        global $DB;
        //Do stuff here
    }

    public static function recordLogoutTime()
    {
        global $DB;
        //Do stuff here
    }
}

So now I want to get access to the current session id and current user id, so I can record the time that the user is logged in and logged out. How I can access this information I guess it will be some sort of a global variable but I do not know what is it.

You could use the $USER global to get the current user record. Alternatively, every event observer function is passed a copy of the event that triggered it - of the same class that you listed in event.php (eg \\core\\event\\user_loggedin). So you should be able to just use $event->userid (assuming you've called the function param $event).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM