<?php
/**
 * Online Block
 *
 * @package modules
 * @copyright (C) 2002-2009 The Digital Development Foundation
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage Roles module
 * @link http://xaraya.com/index.php/release/27.html
 */

/**
 * Online Block
 * @author Jim McDonald, Greg Allan, John Cox, Michael Makushev
 */
/*
 * initialise block
 */
function roles_onlineblock_init()
{
    // No parameters accepted by this block.
    return array(
        'groups' => array(),
        'max_users' => 20,
        'nocache' => 0, // cache by default
        'pageshared' => 1, // share across pages
        'usershared' => 1, // share for group members
        'cacheexpire' => null,
    );
}

/**
 * get information on block
 */
function roles_onlineblock_info()
{
    return array(
        'text_type' => 'Online',
        'module' => 'roles',
        'text_type_long' => 'Display who is online',
        'func_update' => 'roles_onlineblock_update',
        'allow_multiple' => true,
    );
}

/**
 * Display func.
 * @param $blockinfo array containing title,content
 */
function roles_onlineblock_display($blockinfo)
{
    // Security check
    if (!xarSecurityCheck('ViewRoles',0,'Block',"online:$blockinfo[title]:$blockinfo[bid]")) {return;}

    // Get variables from content block
    if (!is_array($blockinfo['content'])) {
        $vars = unserialize($blockinfo['content']);
    } else {
        $vars = $blockinfo['content'];
    }

    $args['numusers'] = xarModAPIfunc(
        'roles', 'user', 'countallactive',
        array('count_users' => true, 'include_anonymous' => false)
    );
    if (empty($args['numusers'])) {
        $args['numusers'] = 0;
    }

    // FIXME: there could be many active users, but we only want a handful of them.
    $sessions = xarModAPIFunc(
        'roles', 'user', 'getallactive',
        array(
            'order' => 'name',
            'startnum' => 0,
            'include_anonymous' => false,
            'include_myself' => true,
            'group' => (!empty($vars['groups']) ? implode(',', $vars['groups']) : NULL),
            'numitems' => (!empty($vars['max_users']) ? $vars['max_users'] : NULL),
        )
    );

    if (!empty($sessions)) {
        foreach ($sessions as $key => $session) {
            $args['test1'][$key] = array(
                'uid' => $session['uid'],
                'name' => $session['name'],
                'userurl' => xarModURL(
                    'roles', 'user', 'display',
                    array('uid' => $session['uid'])
                ),
                'total' => '',
                'unread' => '',
                'messagesurl' => '',
            );

            if ($session['name'] == xarUserGetVar('name')) {
                if (xarModIsAvailable('messages')) {
                    $args['test1'][$key]['total'] = xarModAPIFunc(
                        'messages', 'user', 'count_total',
                        array('uid'=>$session['uid'])
                    );

                    $args['test1'][$key]['unread'] = xarModAPIFunc(
                        'messages', 'user', 'count_unread',
                        array('uid'=>$session['uid'])
                    );

                    $args['test1'][$key]['messagesurl'] =xarModURL(
                        'messages', 'user', 'display',
                        array('uid'=>$session['uid'])
                    );
                }
            }
        }
    }

    $args['numguests'] = xarModAPIfunc('roles', 'user', 'countallactive', array('uid' => 2));
    if (empty($args['numguests'])) {
        $args['numguests'] = 0;
    }

    // Pluralise
    if ($args['numguests'] == 1) {
         $args['guests'] = xarML('guest');
    } else {
         $args['guests'] = xarML('guests');
    }

    if ($args['numusers'] == 1) {
         $args['users'] = xarML('user');
    } else {
         $args['users'] = xarML('users');
    }

    $uid = xarModGetVar('roles', 'lastuser');

    // Make sure we have a lastuser
    if (!empty($uid)) {
        if (!is_numeric($uid)) {
            // Remove this further down the line
            $status = xarModAPIFunc(
                'roles', 'user', 'get',
                array('uname' => $uid)
            );

        } else {
            $status = xarModAPIFunc(
                'roles', 'user', 'get',
                array('uid' => $uid)
            );

        }
         // Check return
         if ($status) {$args['lastuser'] = $status;}
    }

    $args['blockid'] = $blockinfo['bid'];
    $blockinfo['content'] = $args;
    return $blockinfo;
}

?>
