<?php

/**
 * Dialog System
 * @version 2.2
 * @package DS
 * @copyright (c) Alexeev Vlad
 */

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

require_once cot_incfile('ds', 'module');
require_once cot_incfile('forms');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('ds', 'a');
cot_block($usr['auth_read']);

$datefilter = array(
  'start' => array(
    'int' => 0,
    'title' => ''
  ),
  'end' => array(
    'int' => 0,
    'title' => ''
  ),
);

$datestart = cot_import('datestart', 'G', 'TXT');
$dateend = cot_import('dateend', 'G', 'TXT');

if(!empty($datestart)) {
  $datefilter['start']['title'] = $datestart;
  $datefilter['start']['int'] = cot_date2stamp($datestart, 'd.m.Y');
}
if(!empty($dateend)) {
  $datefilter['end']['title'] = $dateend;
  $datefilter['end']['int'] = cot_date2stamp($dateend, 'd.m.Y');
}

if(!$datefilter['start']['int']) {
  $datefilter['start']['title'] = '01.'.cot_date('m.Y', $sys['now']);
  $datefilter['start']['int'] = cot_date2stamp($datefilter['start']['title'], 'd.m.Y');
}
if(!$datefilter['end']['int']) {
  $datefilter['end']['title'] = cot_date('d.m.Y', $sys['now']);
  $datefilter['end']['int'] = $sys['now'];
}
elseif($datefilter['end']['title'] == cot_date('d.m.Y', $sys['now'])) {
  $datefilter['end']['int'] = $sys['now'];
}

$t = new XTemplate(cot_tplfile(array('ds', 'admin.supportrating')));

$usersrating = array();

if(is_array($cfg['ds']['supportinfo']['modergroups']) && count($cfg['ds']['supportinfo']['modergroups']) > 0) {
  $urrsql = $db->query("SELECT * FROM $db_users WHERE user_maingrp=".implode(' OR user_maingrp=', $cfg['ds']['supportinfo']['modergroups']))->fetchAll();

  require_once cot_incfile('ds', 'module');

  foreach($urrsql as $urr) {
    $ds_rating = array(
      'cnt' => $db->query("SELECT COUNT(*) FROM $db_ds_score WHERE s_touid=".$urr['user_id'].' AND s_date>'.$datefilter['start']['int'].' AND s_date<'.$datefilter['end']['int'])->fetchColumn(),
      'sum' => $db->query("SELECT SUM(s_score) FROM $db_ds_score WHERE s_touid=".$urr['user_id'].' AND s_date>'.$datefilter['start']['int'].' AND s_date<'.$datefilter['end']['int'])->fetchColumn(),
      'avg' => 0
    );

    $ds_rating['avg'] = ($ds_rating['cnt'] > 0 ? round(($ds_rating['sum']/$ds_rating['cnt']), 1) : 0);

    if(is_array($cfg['rating_gradation']) && count($cfg['rating_gradation']) > 0) {
      foreach($cfg['rating_gradation'] as $ratgrad) {
        if($ratgrad['cnt'] >= $ds_rating['cnt']) {
          $ds_rating['avg'] = $ratgrad['max']*(($ds_rating['avg']/(5/100))/100);
          if($ds_rating['avg'] > $ratgrad['max']) $ds_rating['avg'] = $ratgrad['max'];
          break;
        }
      }
    }
    $usersrating[] = array(
      'user_url' => cot_url('users', 'm=details&u='.$urr['user_name'].'&id='.$urr['user_id']),
      'user_name' => $urr['user_name'].(!empty($urr['user_MODERNAME']) ? ' ('.$urr['user_MODERNAME'].')' : ''),
      'cnt' => $ds_rating['cnt'],
      'sum' => $ds_rating['sum'],
      'avg' => $ds_rating['avg']
    );
  }

  $urrsort = cot_import('urrsort', 'G', 'TXT');
  if(!empty($urrsort) && count($usersrating) > 0) {
    if($urrsort == 'avg_asc') {
      usort($usersrating, function($a,$b){
        return ($a['avg']-$b['avg']);
      });
    }
    elseif($urrsort == 'avg_desc') {
      usort($usersrating, function($a,$b){
        return ($b['avg']-$a['avg']);
      });
    }
    elseif($urrsort == 'cnt_asc') {
      usort($usersrating, function($a,$b){
        return ($a['cnt']-$b['cnt']);
      });
    }
    elseif($urrsort == 'cnt_desc') {
      usort($usersrating, function($a,$b){
        return ($b['cnt']-$a['cnt']);
      });
    }
  }

  foreach($usersrating as $urr) {
    $urr['avg'] = number_format(round($urr['avg'], 1), 1, '.', '');
    $t->assign('USER_DS_DATA', $urr);

    $t->parse('MAIN.DS_ROW');
  }
}

$t->parse('MAIN');
$adminmain = $t->text("MAIN");
