static function AddRatingVote($arParam)
{
global $DB, $CACHE_MANAGER;
$connection = BitrixMainApplication::getConnection();
$helper = $connection->getSqlHelper();
if ($arParam['ENTITY_TYPE_ID'] === 'USER' && isset(BitrixMainApplication::getInstance()->getSession()['RATING_VOTE_COUNT']))
{
if (BitrixMainApplication::getInstance()->getSession()['RATING_VOTE_COUNT'] >= BitrixMainApplication::getInstance()->getSession()['RATING_USER_VOTE_COUNT'])
{
return false;
}
BitrixMainApplication::getInstance()->getSession()['RATING_VOTE_COUNT']++;
}
$arParam['ENTITY_TYPE_ID'] = mb_substr($arParam['ENTITY_TYPE_ID'], 0, 50);
$arParam['REACTION'] = ($arParam['REACTION'] <> '' ? $arParam['REACTION'] : self::REACTION_DEFAULT);
CRatings::CancelRatingVote($arParam);
$err_mess = (CRatings::err_mess())."
Function: AddRatingVote
Line: ";
$votePlus = $arParam['VALUE'] >= 0 ? true : false;
$ratingId = CRatings::GetAuthorityRating();
$arRatingUserProp = CRatings::GetRatingUserProp($ratingId, $arParam['USER_ID']);
$voteUserWeight = $arRatingUserProp['VOTE_WEIGHT'];
$sRatingWeightType = COption::GetOptionString("main", "rating_weight_type", "auto");
if ($sRatingWeightType === 'auto')
{
if ($arParam['ENTITY_TYPE_ID'] === 'USER')
{
$sRatingAuthrorityWeight = COption::GetOptionString("main", "rating_authority_weight_formula", 'Y');
if ($sRatingAuthrorityWeight === 'Y')
{
$communitySize = COption::GetOptionString("main", "rating_community_size", 1);
$communityAuthority = COption::GetOptionString("main", "rating_community_authority", 1);
$voteWeight = COption::GetOptionString("main", "rating_vote_weight", 1);
$arParam['VALUE'] = $arParam['VALUE']*($communitySize*($voteUserWeight/$voteWeight)/$communityAuthority);
}
}
else
{
$arParam['VALUE'] = $arParam['VALUE']*$voteUserWeight;
}
}
else
{
$arParam['VALUE'] = $arParam['VALUE']*$voteUserWeight;
}
$arFields = array(
'ACTIVE' => "'Y'",
'TOTAL_VOTES' => "TOTAL_VOTES+1",
'TOTAL_VALUE' => "TOTAL_VALUE".($votePlus ? '+' : '').(float)$arParam['VALUE'],
'LAST_CALCULATED' => $DB->GetNowFunction(),
);
$arFields[($votePlus ? 'TOTAL_POSITIVE_VOTES' : 'TOTAL_NEGATIVE_VOTES')] = ($votePlus ? 'TOTAL_POSITIVE_VOTES+1' : 'TOTAL_NEGATIVE_VOTES+1');
// GetOwnerDocument
$arParam['OWNER_ID'] = 0;
foreach(GetModuleEvents("main", "OnGetRatingContentOwner", true) as $arEvent)
{
$result = ExecuteModuleEventEx($arEvent, array($arParam));
if ($result !== false)
$arParam['OWNER_ID'] = (int)$result;
}
$rowAffected = $DB->Update("b_rating_voting", $arFields, "WHERE ENTITY_TYPE_ID='".$DB->ForSql($arParam['ENTITY_TYPE_ID'])."' AND ENTITY_ID='".(int)$arParam['ENTITY_ID']."'" , $err_mess.__LINE__);
if ($rowAffected > 0)
{
$rsRV = $DB->Query("SELECT ID, TOTAL_POSITIVE_VOTES FROM b_rating_voting WHERE ENTITY_TYPE_ID='".$DB->ForSql($arParam['ENTITY_TYPE_ID'])."' AND ENTITY_ID='".(int)$arParam['ENTITY_ID']."'", false, $err_mess.__LINE__);
$arRV = $rsRV->Fetch();
$arParam['RATING_VOTING_ID'] = $arRV['ID'];
$arParam['TOTAL_POSITIVE_VOTES'] = $arRV['TOTAL_POSITIVE_VOTES'];
$arParam['REACTIONS_LIST'] = array(
self::REACTION_DEFAULT => (int)$arParam['TOTAL_POSITIVE_VOTES']
);
if ($votePlus)
{
$rsRVR = $DB->Query("SELECT TOTAL_VOTES FROM b_rating_voting_reaction WHERE ENTITY_TYPE_ID='".$DB->ForSql($arParam['ENTITY_TYPE_ID'])."' AND ENTITY_ID='".(int)$arParam['ENTITY_ID']."'", false, $err_mess.__LINE__);
if (!($arRVR = $rsRVR->fetch())) // reactions not initialized
{
$merge = $helper->prepareMerge('b_rating_voting_reaction', ['ENTITY_TYPE_ID', 'ENTITY_ID', 'REACTION'], [
'ENTITY_TYPE_ID' => $arParam['ENTITY_TYPE_ID'],
'ENTITY_ID' => $arParam['ENTITY_ID'],
'REACTION' => self::REACTION_DEFAULT,
'TOTAL_VOTES' => $arRV['TOTAL_POSITIVE_VOTES'],
], [
'TOTAL_VOTES' => $arRV['TOTAL_POSITIVE_VOTES'],
]);
if ($merge[0])
{
$DB->query($merge[0]);
}
}
$merge = $helper->prepareMerge('b_rating_voting_reaction', ['ENTITY_TYPE_ID', 'ENTITY_ID', 'REACTION'], [
'ENTITY_TYPE_ID' => $arParam['ENTITY_TYPE_ID'],
'ENTITY_ID' => $arParam['ENTITY_ID'],
'REACTION' => $arParam['REACTION'],
'TOTAL_VOTES' => 1,
], [
'TOTAL_VOTES' => new BitrixMainDBSqlExpression('b_rating_voting_reaction.TOTAL_VOTES + 1'),
]);
if ($merge[0])
{
$DB->query($merge[0]);
}
}
}
else
{
$arFields = array(
"ENTITY_TYPE_ID" => "'".$DB->ForSql($arParam["ENTITY_TYPE_ID"])."'",
"ENTITY_ID" => (int)$arParam['ENTITY_ID'],
"OWNER_ID" => (int)$arParam['OWNER_ID'],
"ACTIVE" => "'Y'",
"CREATED" => $DB->GetNowFunction(),
"LAST_CALCULATED" => $DB->GetNowFunction(),
"TOTAL_VOTES" => 1,
"TOTAL_VALUE" => (float)$arParam['VALUE'],
"TOTAL_POSITIVE_VOTES" => ($votePlus ? 1 : 0),
"TOTAL_NEGATIVE_VOTES" => ($votePlus ? 0 : 1)
);
$arParam['RATING_VOTING_ID'] = $DB->Insert("b_rating_voting", $arFields, $err_mess.__LINE__);
$arParam['TOTAL_POSITIVE_VOTES'] = ($votePlus ? 1 : 0);
$arParam['REACTIONS_LIST'] = array(
self::REACTION_DEFAULT => (int)$arParam['TOTAL_POSITIVE_VOTES']
);
if ($votePlus)
{
$merge = $helper->prepareMerge('b_rating_voting_reaction', ['ENTITY_TYPE_ID', 'ENTITY_ID', 'REACTION'], [
'ENTITY_TYPE_ID' => $arParam['ENTITY_TYPE_ID'],
'ENTITY_ID' => $arParam['ENTITY_ID'],
'REACTION' => $arParam['REACTION'],
'TOTAL_VOTES' => 1,
], [
'TOTAL_VOTES' => 1,
]);
if ($merge[0])
{
$DB->query($merge[0]);
}
}
}
$rsRVR = $DB->Query("SELECT REACTION, TOTAL_VOTES FROM b_rating_voting_reaction WHERE ENTITY_TYPE_ID='".$DB->ForSql($arParam['ENTITY_TYPE_ID'])."' AND ENTITY_ID='".(int)$arParam['ENTITY_ID']."'", false, $err_mess.__LINE__);
while($arRVR = $rsRVR->fetch())
{
$arParam['REACTIONS_LIST'][$arRVR['REACTION']] = $arRVR['TOTAL_VOTES'];
}
$arFields = array(
"RATING_VOTING_ID" => (int)$arParam['RATING_VOTING_ID'],
"ENTITY_TYPE_ID" => "'".$DB->ForSql($arParam["ENTITY_TYPE_ID"])."'",
"ENTITY_ID" => (int)$arParam['ENTITY_ID'],
"VALUE" => (float)$arParam['VALUE'],
"ACTIVE" => "'Y'",
"CREATED" => $DB->GetNowFunction(),
"USER_ID" => (int)$arParam['USER_ID'],
"USER_IP" => "'".$DB->ForSql($arParam["USER_IP"])."'",
"OWNER_ID" => (int)$arParam['OWNER_ID'],
"REACTION" => "'".$DB->ForSql($arParam["REACTION"])."'"
);
$ID = $DB->Insert("b_rating_vote", $arFields, $err_mess.__LINE__);
foreach(GetModuleEvents("main", "OnAddRatingVote", true) as $arEvent)
ExecuteModuleEventEx($arEvent, array((int)$ID, $arParam));
$userData = CAllRatings::getUserData((int)$arParam['USER_ID'], (float)$arParam['VALUE']);
if (CModule::IncludeModule('pull'))
{
CPullStack::AddShared(Array(
'module_id' => 'main',
'command' => 'rating_vote',
'params' => Array(
"TYPE" => "ADD",
"USER_ID" => (int)$arParam['USER_ID'],
"ENTITY_TYPE_ID" => $arParam["ENTITY_TYPE_ID"],
"ENTITY_ID" => (int)$arParam['ENTITY_ID'],
"TOTAL_POSITIVE_VOTES" => $arParam['TOTAL_POSITIVE_VOTES'],
"RESULT" => $votePlus? 'PLUS': 'MINUS',
"USER_DATA" => $userData,
"REACTION" => $arParam['REACTION'],
"REACTIONS_LIST" => $arParam['REACTIONS_LIST']
)
));
}
if (CACHED_b_rating_vote!==false)
{
$bucket_size = (int)CACHED_b_rating_bucket_size;
if($bucket_size <= 0)
$bucket_size = 100;
$bucket = (int)((int)$arParam['ENTITY_ID'] / $bucket_size);
$CACHE_MANAGER->Clean("b_rvg_".$DB->ForSql($arParam["ENTITY_TYPE_ID"]).$bucket, "b_rating_voting");
}
return $userData;
}