<?php

/**
 * Reviews plugin
 *
 * @package reviews
 * @version 2.2.0
 * @author CMSWorks Team
 * @copyright Copyright (c) CMSWorks.ru, littledev.ru
 * @license BSD
 */
defined('COT_CODE') or die('Wrong URL.');

global $cot_extrafields;
// Tables
cot::$db->registerTable('reviews');

function cot_reviews_get_rstar($area = '', $type = '', $code = '', $mcode = '', $uid = 0)
{
	global $db, $db_reviews;

  $where = array();
  if(!empty($area)) $where['item_area'] = "item_area='".$area."'";
  if(!empty($type)) $where['item_type'] = "item_type='".$type."'";
  if(!empty($code)) $where['item_code'] = "item_code='".$code."'";
  if(!empty($mcode)) $where['item_code'] = "item_mcode='".$mcode."'";
  if($uid > 0) $where['item_touserid'] = "item_touserid=".$uid;

  if(count($where) == 0) return 0;

  $cnt = $db->query("SELECT COUNT(*)
    FROM $db_reviews WHERE ".implode(' AND ', $where))->fetchColumn();

  $where['item_score'] = "item_score=1";
  $pos = $db->query("SELECT SUM(item_score) as score
    FROM $db_reviews WHERE ".implode(' AND ', $where))->fetchColumn();
  return (($cnt > 0 && $pos > 0) ? ($pos/$cnt) : ($cnt > 0 ? 0.20 : 0));
}