<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=users.details.tags
 * [END_COT_EXT]
 */

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

$t->assign(array(
  'USERS_DETAILS_DS_RATING_SHOW' => 0,
  'USERS_DETAILS_DS_RATING' => '0.0'
));

if(in_array($urr['user_maingrp'], $cfg['ds']['supportinfo']['modergroups'])) {// && in_array($usr['maingrp'], $cfg['ds']['supportinfo']['modergroups'])) {
  require_once cot_incfile('ds', 'module');

  $monthdate = cot_date2stamp('01.'.cot_date('m.Y', $sys['now']), 'd.m.Y');
  if(!($monthdate > 0)) $monthdate = 0;

  $ds_rating = array(
    'cnt' => $db->query("SELECT COUNT(*) FROM $db_ds_score WHERE s_touid=".$urr['user_id'].' AND s_date>'.$monthdate)->fetchColumn(),
    'sum' => $db->query("SELECT SUM(s_score) FROM $db_ds_score WHERE s_touid=".$urr['user_id'].' AND s_date>'.$monthdate)->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;
      }
    }
  }

  $t->assign(array(
    'USERS_DETAILS_DS_RATING_SHOW' => 1,
    'USERS_DETAILS_DS_RATING' => number_format(round($ds_rating['avg'], 1), 1, '.', '')
  ));
}