- Модуль: main
- Путь к файлу: ~/bitrix/modules/main/classes/general/ratings.php
- Класс: CAllRatings
- Вызов: CAllRatings::GetRatingVoteResultCache
static function GetRatingVoteResultCache($entityTypeId, $entityId, $user_id = 0)
{
global $DB, $CACHE_MANAGER;
$err_mess = (CRatings::err_mess())."
Function: GetRatingVoteResultCache
Line: ";
$arResult = array();
$entityId = (int)$entityId;
$user_id = (int)$user_id;
if ($entityTypeId == '' || $entityId <= 0)
return $arResult;
if ($user_id == 0)
$user_id = $GLOBALS["USER"]->GetID();
$bucket_size = (int)CACHED_b_rating_bucket_size;
if($bucket_size <= 0)
$bucket_size = 100;
$bucket = (int)($entityId / $bucket_size);
if($CACHE_MANAGER->Read(CACHED_b_rating_vote, $cache_id="b_rvg_".$entityTypeId.$bucket, "b_rating_voting"))
{
$arResult = $CACHE_MANAGER->Get($cache_id);
}
else
{
$total = array();
$sql_str = "SELECT
RVG.ID,
RVG.ENTITY_ID,
RVG.TOTAL_VALUE,
RVG.TOTAL_VOTES,
RVG.TOTAL_POSITIVE_VOTES,
RVG.TOTAL_NEGATIVE_VOTES
FROM
b_rating_voting RVG
WHERE
RVG.ENTITY_TYPE_ID = '".$DB->ForSql($entityTypeId)."'
and RVG.ENTITY_ID between ".($bucket*$bucket_size)." AND ".(($bucket+1)*$bucket_size-1)."
and RVG.ACTIVE = 'Y'";
$res = $DB->Query($sql_str, false, $err_mess.__LINE__);
while($row = $res->Fetch())
{
$arResult[$row['ENTITY_ID']] = array(
'USER_VOTE' => 0,
'USER_REACTION' => false,
'USER_HAS_VOTED' => 'N',
'USER_VOTE_LIST' => array(),
'USER_REACTION_LIST' => array(),
'TOTAL_VALUE' => $row['TOTAL_VALUE'],
'TOTAL_VOTES' => (int)$row['TOTAL_VOTES'],
'TOTAL_POSITIVE_VOTES' => (int)$row['TOTAL_POSITIVE_VOTES'],
'TOTAL_NEGATIVE_VOTES' => (int)$row['TOTAL_NEGATIVE_VOTES'],
'REACTIONS_LIST' => array(
self::REACTION_DEFAULT => (int)$row['TOTAL_POSITIVE_VOTES']
)
);
if (!isset($total[$row['ENTITY_ID']]))
{
$total[$row['ENTITY_ID']] = (int)$row['TOTAL_POSITIVE_VOTES'];
}
}
$count = array();
$foundDefault = array();
$entityIdList = array();
$sql_str = "SELECT
RVGR.ENTITY_ID,
RVGR.REACTION,
RVGR.TOTAL_VOTES
FROM
b_rating_voting_reaction RVGR
WHERE
RVGR.ENTITY_TYPE_ID = '".$DB->ForSql($entityTypeId)."'
and RVGR.ENTITY_ID between ".($bucket*$bucket_size)." AND ".(($bucket+1)*$bucket_size-1);
$res = $DB->Query($sql_str, false, $err_mess.__LINE__);
while($row = $res->Fetch())
{
if (!in_array($row['ENTITY_ID'], $entityIdList))
{
$entityIdList[] = $row['ENTITY_ID'];
}
$arResult[$row['ENTITY_ID']]['REACTIONS_LIST'][$row['REACTION']] = (int)$row['TOTAL_VOTES'];
if (!isset($count[$row['ENTITY_ID']]))
{
$count[$row['ENTITY_ID']] = 0;
}
$count[$row['ENTITY_ID']] += ((int)$row['TOTAL_VOTES'] >= 0 ? (int)$row['TOTAL_VOTES'] : 0);
if (
!isset($foundDefault[$row['ENTITY_ID']])
&& $row['REACTION'] == self::REACTION_DEFAULT
)
{
$foundDefault[$row['ENTITY_ID']] = true;
}
}
foreach($entityIdList as $eId)
{
if (
!isset($foundDefault[$eId])
&& isset($count[$eId])
&& isset($total[$eId])
&& $count[$eId] >= $total[$eId]
)
{
$arResult[$eId]['REACTIONS_LIST'][self::REACTION_DEFAULT] = 0;
}
}
$sql = "SELECT RVG.ENTITY_ID, RVG.USER_ID, RVG.VALUE, RVG.REACTION
FROM b_rating_vote RVG
WHERE RVG.ENTITY_TYPE_ID = '".$DB->ForSql($entityTypeId)."'
and RVG.ENTITY_ID between ".($bucket*$bucket_size)." AND ".(($bucket+1)*$bucket_size-1);
$res = $DB->Query($sql, false, $err_mess.__LINE__);
while($row = $res->Fetch())
{
$arResult[$row['ENTITY_ID']]['USER_VOTE_LIST'][$row['USER_ID']] = $row['VALUE'];
$arResult[$row['ENTITY_ID']]['USER_REACTION_LIST'][$row['USER_ID']] = (!empty($row['REACTION']) ? $row['REACTION'] : self::REACTION_DEFAULT);
}
$CACHE_MANAGER->Set($cache_id, $arResult);
}
if (isset($arResult[$entityId]['USER_VOTE_LIST'][$user_id]))
{
$arResult[$entityId]['USER_VOTE'] = $arResult[$entityId]['USER_VOTE_LIST'][$user_id];
$arResult[$entityId]['USER_REACTION'] = (
!empty($arResult[$entityId]['USER_REACTION_LIST'][$user_id])
? $arResult[$entityId]['USER_REACTION_LIST'][$user_id]
: self::REACTION_DEFAULT
);
$arResult[$entityId]['USER_HAS_VOTED'] = 'Y';
}
return $arResult[$entityId] ?? array();
}