<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=api.track_stack.main
Order=10
[END_COT_EXT]
==================== */

defined('COT_CODE') or die('Wrong URL');

$ONLINE_WIDGET_IDS = cot_import('ONLINE_WIDGET_IDS', 'G', 'TXT');

$return['push']['onlinewidget'] = array();

if(!empty($ONLINE_WIDGET_IDS)) {
  $ONLINE_WIDGET_IDS = explode(',', $ONLINE_WIDGET_IDS);
  if(count($ONLINE_WIDGET_IDS) > 0) {
    require_once cot_incfile('whosonline', 'plug');

    $supportonline = 0;
    $supportonline_lastlog = 0;

    if(in_array(0, $ONLINE_WIDGET_IDS)) {
      $modersql = $db->query("SELECT * FROM $db_users WHERE user_maingrp=".implode(' OR user_maingrp=', $cfg['ds']['supportinfo']['modergroups']))->fetchAll();
      foreach($modersql as $val) {
        if($supportonline_lastlog < $val['user_lastlog']) $supportonline_lastlog = $val['user_lastlog'];
        if(cot_userisonline($val['user_id'])) {
          $supportonline = 1;
          break;
        }
      }
    }

    foreach($ONLINE_WIDGET_IDS as $owuid) {
      if($owuid > 0) {
        $lastlog = $db->query("SELECT user_lastlog FROM $db_users WHERE user_id=".$owuid." LIMIT 1")->fetchColumn();
        $return['push']['onlinewidget'][] = array(
          'uid' => $owuid,
          'online' => (cot_userisonline($owuid) ? 1 : 0),
          'last' => ($lastlog > 0 ? cot_build_timeago($sys['now'] - ($sys['now'] - $lastlog)) : '')
        );
      } elseif($owuid == 0) {
        $return['push']['onlinewidget'][] = array(
          'uid' => 0,
          'online' => $supportonline,
          'last' => cot_build_timeago($sys['now'] - ($sys['now'] - $supportonline_lastlog))
        );
      }
    }
  }
}