MODX 2.8.4: Get User online active within the last xx minutes?

Hello,

I am thinking about how it is possitble te determine which users are online active within the last xxx minutes.

I found some from Bob here:
https://forums.modx.com/thread/?thread=69928&page=1

but I think this is very old and the table _active_users don’t filled with data.

Is there any way?

Thank you in advance.

bye
Chris

There is an extra UsersOnline that maybe does what you need.
But I have never used it and can’t tell you what it does exactly and if it is any good.


Also, the Login extra has a snippet ActiveUsers that seems to join the modx_session table (that has a timestamp column).

And there is also a “thislogin” column in the modx_user_attributes table (modUserProfile) that contains a timestamp and could maybe be used for this purpose.

thank you.

I tried it.
Now there is a additional table and filled with data.
But the Snippet doesnt run. blank screen.

Is it incompatibility with php 8?

<?php

/** @var array $scriptProperties */
    /** @var UsersOnline $UsersOnline */
    if (!$UsersOnline = $modx->getService('usersonline', 'UsersOnline', $modx->getOption('usersonline_core_path', null,
            $modx->getOption('core_path') . 'components/usersonline/') . 'model/usersonline/', $scriptProperties)
    ) {
        return 'Could not load UsersOnline class!';
    }
    if (!$pdo = $modx->getService('pdoTools')) {
        return $modx->lexicon('no_pdo');
    }   
    $interval = $modx->getOption('timeInterval', $scriptProperties, -1);
    if ($interval == -1) {
        $interval = $modx->getOption('usersonline_time_span');
    }
    $contexts = $modx->getOption('contexts', $scriptProperties, null);
    $innerJoin = $modx->getOption('innerJoin', $scriptProperties, '');
    $innerJoin = $modx->fromJSON($innerJoin);
    $innerJoin['UsersOnline'] = array(
        'class' => 'userOnline',
        'on'    => 'modUser.id = UsersOnline.user_id',
    );
        echo  $innerJoin['UsersOnline'];
    $select = $modx->getOption('select', $scriptProperties, '');
    $select = $modx->fromJSON($select);
    $select['UsersOnline'] = '*';
    $time = time();
    $startTime = $time - $interval;
    $where = $modx->getOption('where', $scriptProperties, '');
    $where = $modx->fromJSON($where);
    $where[] = array(
        'UsersOnline.lastvisit:>=' => $startTime,
        'UsersOnline.lastvisit:<=' => $time,
    );
    $contextsArray = array();
    if($contexts != null){
        $contextsArray = explode(',', $contexts);
        echo "haoo";
    }
    if (!empty($contextsArray)) {
        $where[] = array(
            'UsersOnline.context_key:IN' => $contextsArray,
        );
    }
    $scriptProperties['where'] = $modx->toJSON($where);
    $scriptProperties['innerJoin'] = $modx->toJSON($innerJoin);
    $scriptProperties['select'] = $modx->toJSON($select);

    $output = $modx->runSnippet('pdoUsers', $scriptProperties);
    return $output;

Do you have pdoTools installed?

yes. I have.
2.13.2-pl

Try adding a default value of '[]' (e.g. $innerJoin = $modx->getOption('innerJoin', $scriptProperties, '[]');) to the lines 18, 24 and 29.

yes, that’s it.
now, it runs.

thank you so much. :pray:

additional question:

how can I access the current resource id?
I wish to except specific resource from beeing tracked.

thank you in advance

The plugin from the UsersOnline extra runs on the event “OnMODXInit”. At that point in time the requested resource is still unknown.

You’d have to try running the plugin on a different event. Maybe OnLoadWebDocument works.

thank you very much. :pray:

Is there a information about hierarchy about system events?

Thank you for link. :+1: