<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=usertags.main
 * [END_COT_EXT]
 */
/**
 * Reviews plugin
 *
 * @package reviews
 * @version 2.0.0
 * @author CMSWorks Team
 * @copyright Copyright (c) CMSWorks.ru, littledev.ru
 * @license BSD
 */
defined('COT_CODE') or die('Wrong URL.');

if(is_array($user_data) && $user_data['user_id'] > 0) {
  require_once cot_incfile('reviews', 'plug');
  global $db_reviews, $cfg;

	$temp_array['RSTAR'] = round($user_data['user_rstar']*5, 1);
  $temp_array['RSTAR_CNT'] = $db->query("SELECT COUNT(*) FROM $db_reviews WHERE item_type='review' AND item_touserid=".$user_data['user_id'])->fetchColumn();
  if(!$temp_array['RSTAR_CNT']) $temp_array['RSTAR_CNT'] = 0;

  if(is_array($cfg['rating_gradation']) && count($cfg['rating_gradation']) > 0) {
    foreach($cfg['rating_gradation'] as $ratgrad) {
      if($ratgrad['cnt'] >= $temp_array['RSTAR_CNT']) {
        $temp_array['RSTAR'] = $ratgrad['max']*(($temp_array['RSTAR']/(5/100))/100);
        if($temp_array['RSTAR'] > $ratgrad['max']) $temp_array['RSTAR'] = $ratgrad['max'];
        break;
      }
    }
  }

  $temp_array['RSTAR'] = number_format(round($temp_array['RSTAR'], 1), 1, '.', '');
  $temp_array['RSTAR_CNT_TEXT'] = cot_declension($temp_array['RSTAR_CNT'], 'оценка,оценки,оценок');
}