⚈ Kuopassa.net

Lisää kertomuksia netistä. 摆烂

Couple usage examples for that Textpattern plugin

A few days ago I released a small and simple Textpattern plugin with perhaps needlessly complex name: kuo_user_last_action. It records to a database table what action and currently logged in used did for the last time. So the plugin is simple loaded when a page is loaded, then a database query is made. It doesn’t do anything else, it just logs data. Here are couple PHP code examples that uses that data.

Timestamp of last activity

If for some reason it’s wanted to show date and time to user when that user’s activity was logged for the last time, then this code could be used:

if (is_logged_in()) {
    $current_user_last_activity = safe_row("timestamp",
    "`username` = '".is_logged_in()['name']."'",0);
    if ($current_user_last_activity) {
        echo date('d.m.Y \a\t H:i',strtotime($current_user_last_activity['timestamp']));

Detect inactive users

A bit more useful code is this below. It lists all those users whose activity has been logged with kuo_user_last_action, but who haven’t had any activity in the previous six months. With this block of code an administrator of website could, for example, send a notification email or a newsletter to those users inviting them to visit the site.

$passive_users = safe_rows("username","kuo_user_last_action","`username` != '' AND
`username` IS NOT NULL AND `timestamp` < ('".date('Y-m-d H:i:s')."' - INTERVAL 6 MONTH)",0);
if ($passive_users) {
    foreach ($passive_users as $passive_user) {
        echo $passive_user['username'].'<br />';

Tagged as:

About the author