29 " .
$DB->DateToCharFunction(
"R.CREATED") .
" as CREATED,
30 " .
$DB->DateToCharFunction(
"R.LAST_MODIFIED") .
" as LAST_MODIFIED,
31 " .
$DB->DateToCharFunction(
"R.LAST_CALCULATED") .
" as LAST_CALCULATED
45 if (CACHED_b_rating ===
false)
50 " .
$DB->DateToCharFunction(
"R.CREATED") .
" as CREATED,
51 " .
$DB->DateToCharFunction(
"R.LAST_MODIFIED") .
" as LAST_MODIFIED,
52 " .
$DB->DateToCharFunction(
"R.LAST_CALCULATED") .
" as LAST_CALCULATED
62 global $stackCacheManager;
63 $stackCacheManager->SetLength(
"b_rating", 100);
64 $stackCacheManager->SetTTL(
"b_rating", CACHED_b_rating);
65 if ($stackCacheManager->Exist(
"b_rating", $strID))
67 $arResult = $stackCacheManager->Get(
"b_rating", $strID);
74 " .
$DB->DateToCharFunction(
"R.CREATED") .
" as CREATED,
75 " .
$DB->DateToCharFunction(
"R.LAST_MODIFIED") .
" as LAST_MODIFIED,
76 " .
$DB->DateToCharFunction(
"R.LAST_CALCULATED") .
" as LAST_CALCULATED
85 $stackCacheManager->Set(
"b_rating", $strID,
$arResult);
104 if ((
string)
$val ==
'' ||
$val ===
"NOT_REF")
108 switch (mb_strtoupper(
$key))
114 if (in_array(
$val, [
'Y',
'N']))
116 $arSqlSearch[] =
"R.ACTIVE = '" .
$val .
"'";
120 if (in_array(
$val, [
'Y',
'N']))
122 $arSqlSearch[] =
"R.AUTHORITY = '" .
$val .
"'";
126 if (in_array(
$val, [
'Y',
'N']))
128 $arSqlSearch[] =
"R.POSITION = '" .
$val .
"'";
132 if (in_array(
$val, [
'Y',
'N',
'C']))
134 $arSqlSearch[] =
"R.CALCULATED = '" .
$val .
"'";
150 $ord = (mb_strtoupper(
$val) <>
"ASC" ?
"DESC" :
"ASC");
151 switch (mb_strtoupper(
$key))
154 $sOrder .=
", R.ID " . $ord;
157 $sOrder .=
", R.NAME " . $ord;
160 $sOrder .=
", R.CREATED " . $ord;
162 case "LAST_MODIFIED":
163 $sOrder .=
", R.LAST_MODIFIED " . $ord;
165 case "LAST_CALCULATED":
166 $sOrder .=
", R.LAST_CALCULATED " . $ord;
169 $sOrder .=
", R.ACTIVE " . $ord;
172 $sOrder .=
", R.AUTHORITY " . $ord;
175 $sOrder .=
", R.POSITION " . $ord;
178 $sOrder .=
", R.CALCULATED " . $ord;
181 $sOrder .=
", R.CALCULATED " . $ord;
183 case "CALCULATION_METHOD":
184 $sOrder .=
", R.CALCULATION_METHOD " . $ord;
187 $sOrder .=
", R.ENTITY_ID " . $ord;
194 $sOrder =
"R.ID DESC";
197 $strSqlOrder =
" ORDER BY " . trim($sOrder,
", ");
202 R.ID, R.NAME, R.ACTIVE, R.CALCULATED, R.AUTHORITY, R.POSITION, R.ENTITY_ID, R.CALCULATION_METHOD,
203 " .
$DB->DateToCharFunction(
"R.CREATED") .
" CREATED,
204 " .
$DB->DateToCharFunction(
"R.LAST_MODIFIED") .
" LAST_MODIFIED,
205 " .
$DB->DateToCharFunction(
"R.LAST_CALCULATED") .
" LAST_CALCULATED
209 " . $strSqlSearch .
"
211 return $DB->Query($strSql);
218 $ratingId = (int)$ratingId;
222 MAX(CURRENT_VALUE) as MAX,
223 MIN(CURRENT_VALUE) as MIN,
224 AVG(CURRENT_VALUE) as AVG,
226 FROM b_rating_results
227 WHERE RATING_ID = " . $ratingId;
228 return $DB->Query($strSql);
234 global
$DB, $stackCacheManager;
243 "ACTIVE" =>
$arFields[
"ACTIVE"] ===
'Y' ?
'Y' :
'N',
244 "POSITION" =>
$arFields[
"POSITION"] ===
'Y' ?
'Y' :
'N',
245 "AUTHORITY" =>
$arFields[
"AUTHORITY"] ===
'Y' ?
'Y' :
'N',
248 "CALCULATION_METHOD" =>
$arFields[
"CALCULATION_METHOD"],
249 "~CREATED" =>
$DB->GetNowFunction(),
250 "~LAST_MODIFIED" =>
$DB->GetNowFunction(),
252 $ID =
$DB->Add(
"b_rating", $arFields_i);
255 foreach (
GetModuleEvents(
"main",
"OnAfterAddRating",
true) as $arEvent)
263 "CONFIGS" =>
"'" .
$DB->ForSQL(serialize(
$arFields[
"CONFIGS"])) .
"'",
266 $DB->Update(
"b_rating", $arFields_u,
"WHERE ID = " .
$ID);
273 CAgent::AddAgent(
"CRatings::Calculate($ID);",
"main",
"N", 3600,
"",
"Y",
"");
275 $stackCacheManager->Clear(
"b_rating");
283 global
$DB, $stackCacheManager;
294 "ACTIVE" =>
$arFields[
'ACTIVE'] ===
'Y' ?
'Y' :
'N',
297 "CALCULATION_METHOD" =>
$arFields[
"CALCULATION_METHOD"],
298 "~LAST_MODIFIED" =>
$DB->GetNowFunction(),
300 $strUpdate =
$DB->PrepareUpdate(
"b_rating", $arFields_u);
301 if (!
$DB->Query(
"UPDATE b_rating SET " . $strUpdate .
" WHERE ID=" .
$ID))
308 $stackCacheManager->Clear(
"b_rating");
312 foreach (
GetModuleEvents(
"main",
"OnAfterUpdateRating",
true) as $arEvent)
320 "POSITION" =>
"'" . (
$arFields[
'POSITION'] ===
'Y' ?
'Y' :
'N') .
"'",
321 "AUTHORITY" =>
"'" . (
$arFields[
'AUTHORITY'] ===
'Y' ?
'Y' :
'N') .
"'",
322 "CONFIGS" =>
"'" .
$DB->ForSQL(serialize(
$arFields[
"CONFIGS"])) .
"'",
324 $DB->Update(
"b_rating", $arFields_u,
"WHERE ID = " .
$ID);
333 $DB->Query(
"UPDATE b_rating_results SET PREVIOUS_VALUE = 0 WHERE RATING_ID=" .
$ID .
" and ENTITY_TYPE_ID='" .
$DB->ForSql(
$arFields[
"ENTITY_ID"]) .
"'");
336 $strSql =
"SELECT COMPLEX_NAME FROM b_rating_component WHERE RATING_ID = $ID and ACTIVE = 'N'";
338 $arrRatingComponentId = [];
341 $arrRatingComponentId[] =
$arRes[
'COMPLEX_NAME'];
344 if (!empty($arrRatingComponentId))
346 $DB->Query(
"DELETE FROM b_rating_component_results WHERE RATING_ID = $ID AND COMPLEX_NAME IN ('" . implode(
"','", $arrRatingComponentId) .
"')");
351 CAgent::RemoveAgent(
"CRatings::Calculate($ID);",
"main");
352 $AID = CAgent::AddAgent(
"CRatings::Calculate($ID);",
"main",
"N", 3600,
"",
"Y",
"");
354 $stackCacheManager->Clear(
"b_rating");
362 global
$DB, $stackCacheManager;
366 foreach (
GetModuleEvents(
"main",
"OnBeforeDeleteRating",
true) as $arEvent)
371 $DB->Query(
"DELETE FROM b_rating WHERE ID=$ID");
372 $DB->Query(
"DELETE FROM b_rating_user WHERE RATING_ID=$ID");
373 $DB->Query(
"DELETE FROM b_rating_component WHERE RATING_ID=$ID");
374 $DB->Query(
"DELETE FROM b_rating_component_results WHERE RATING_ID=$ID");
375 $DB->Query(
"DELETE FROM b_rating_results WHERE RATING_ID=$ID");
377 CAgent::RemoveAgent(
"CRatings::Calculate($ID);",
"main");
379 $stackCacheManager->Clear(
"b_rating");
393 " .
$DB->DateToCharFunction(
"RC.LAST_MODIFIED") .
" LAST_MODIFIED,
394 " .
$DB->DateToCharFunction(
"RC.LAST_CALCULATED") .
" LAST_CALCULATED,
395 " .
$DB->DateToCharFunction(
"RC.NEXT_CALCULATION") .
" NEXT_CALCULATION
397 b_rating_component RC
400 and ACTIVE = 'Y' " . ($bForceRecalc ?
'' :
'AND NEXT_CALCULATION <= ' .
$DB->GetNowFunction());
404 if (CModule::IncludeModule(mb_strtolower(
$arRes[
'MODULE_ID'])))
406 $arRes[
'CONFIG'] = unserialize(
$arRes[
'CONFIG'], [
'allowed_classes' =>
false]);
408 $sRatingWeightType = COption::GetOptionString(
"main",
"rating_weight_type",
"auto");
409 if ($sRatingWeightType ===
'auto')
411 $voteWeight = COption::GetOptionString(
"main",
"rating_vote_weight", 1);
412 $arRes[
'CONFIG'][
'COEFFICIENT'] = (
$arRes[
'CONFIG'][
'COEFFICIENT'] ?? 1) * $voteWeight;
414 if (
$arRes[
'EXCEPTION_METHOD'] <>
'')
416 if (method_exists(
$arRes[
'CLASS'],
$arRes[
'EXCEPTION_METHOD']))
418 $exceptionText = call_user_func([
$arRes[
'CLASS'],
$arRes[
'EXCEPTION_METHOD']]);
419 if ($exceptionText ===
false)
421 if (method_exists(
$arRes[
'CLASS'],
$arRes[
'CALC_METHOD']))
430 if (method_exists(
$arRes[
'CLASS'],
$arRes[
'CALC_METHOD']))
440 return "CRatings::Calculate($ID);";
448 foreach (
GetModuleEvents(
"main",
"OnGetRatingsObjects",
true) as $arEvent)
451 foreach ($arConfig as $OBJ_TYPE)
453 if (!in_array($OBJ_TYPE, $arObjects))
455 $arObjects[] = $OBJ_TYPE;
467 foreach (
GetModuleEvents(
"main",
"OnGetRatingsConfigs",
true) as $arEvent)
470 if (is_null($objectType))
472 foreach ($arConfig as $OBJ_TYPE => $OBJ_VALUE)
474 foreach ($OBJ_VALUE[
'VOTE'] as $VOTE_VALUE)
476 $EntityTypeId = $VOTE_VALUE[
'MODULE_ID'] .
'_' . $VOTE_VALUE[
'ID'];
477 if (!in_array($arEntityTypes[$OBJ_TYPE], $EntityTypeId))
479 $arEntityTypes[$OBJ_TYPE][] = $EntityTypeId;
486 foreach ($arConfig[$objectType][
'VOTE'] as $VOTE_VALUE)
488 $EntityTypeId = $VOTE_VALUE[
'MODULE_ID'] .
'_' . $VOTE_VALUE[
'ID'];
489 $arEntityTypes[$EntityTypeId] = $EntityTypeId;
494 return $arEntityTypes;
502 foreach (
GetModuleEvents(
"main",
"OnGetRatingsConfigs",
true) as $arEvent)
505 if (is_null($objectType))
507 foreach ($arConfig[
"COMPONENT"] as $OBJ_TYPE => $TYPE_VALUE)
509 foreach ($TYPE_VALUE as $RAT_TYPE => $RAT_VALUE)
511 foreach ($RAT_VALUE as $VALUE)
515 $arConfigs[$OBJ_TYPE][$arConfig[
'MODULE_ID']][$RAT_TYPE][$arConfig[
'MODULE_ID'] .
"_" . $RAT_TYPE .
"_" . $VALUE[
'ID']] = $VALUE;
519 $arConfigs[$OBJ_TYPE][$arConfig[
'MODULE_ID']][$arConfig[
'MODULE_ID'] .
"_" . $RAT_TYPE .
"_" . $VALUE[
'ID']] = $VALUE;
527 foreach ($arConfig[
"COMPONENT"][$objectType] as $RAT_TYPE => $RAT_VALUE)
529 $arConfigs[$arConfig[
'MODULE_ID']][
'MODULE_ID'] = $arConfig[
'MODULE_ID'];
530 $arConfigs[$arConfig[
'MODULE_ID']][
'MODULE_NAME'] = $arConfig[
'MODULE_NAME'];
531 foreach ($RAT_VALUE as $VALUE)
535 $arConfigs[$arConfig[
'MODULE_ID']][$RAT_TYPE][$arConfig[
'MODULE_ID'] .
"_" . $RAT_TYPE .
"_" . $VALUE[
'ID']] = $VALUE;
539 $arConfigs[$arConfig[
'MODULE_ID']][$arConfig[
'MODULE_ID'] .
"_" . $RAT_TYPE .
"_" . $VALUE[
'ID']] = $VALUE;
554 $user_id = (int)$user_id;
558 if (!(
$USER instanceof CUser))
562 $user_id =
$USER->GetID();
586 $user_id = (int)$user_id;
588 if ($entityTypeId ==
'' ||
$entityId <= 0)
595 $user_id =
$GLOBALS[
"USER"]->GetID();
598 $bucket_size = (int)CACHED_b_rating_bucket_size;
599 if ($bucket_size <= 0)
604 $bucket = (int)(
$entityId / $bucket_size);
605 if (
$CACHE_MANAGER->Read(CACHED_b_rating_vote, $cache_id =
"b_rvg_" . $entityTypeId . $bucket,
"b_rating_voting"))
618 RVG.TOTAL_POSITIVE_VOTES,
619 RVG.TOTAL_NEGATIVE_VOTES
623 RVG.ENTITY_TYPE_ID = '" .
$DB->ForSql($entityTypeId) .
"'
624 and RVG.ENTITY_ID between " . ($bucket * $bucket_size) .
" AND " . (($bucket + 1) * $bucket_size - 1) .
"
625 and RVG.ACTIVE = 'Y'";
627 while ($row =
$res->Fetch())
631 'USER_REACTION' =>
false,
632 'USER_HAS_VOTED' =>
'N',
633 'USER_VOTE_LIST' => [],
634 'USER_REACTION_LIST' => [],
635 'TOTAL_VALUE' => $row[
'TOTAL_VALUE'],
636 'TOTAL_VOTES' => (int)$row[
'TOTAL_VOTES'],
637 'TOTAL_POSITIVE_VOTES' => (
int)$row[
'TOTAL_POSITIVE_VOTES'],
638 'TOTAL_NEGATIVE_VOTES' => (int)$row[
'TOTAL_NEGATIVE_VOTES'],
639 'REACTIONS_LIST' => [
640 self::REACTION_DEFAULT => (
int)$row[
'TOTAL_POSITIVE_VOTES'],
644 if (!isset($total[$row[
'ENTITY_ID']]))
646 $total[$row[
'ENTITY_ID']] = (int)$row[
'TOTAL_POSITIVE_VOTES'];
659 b_rating_voting_reaction RVGR
661 RVGR.ENTITY_TYPE_ID = '" .
$DB->ForSql($entityTypeId) .
"'
662 and RVGR.ENTITY_ID between " . ($bucket * $bucket_size) .
" AND " . (($bucket + 1) * $bucket_size - 1);
664 while ($row =
$res->Fetch())
666 if (!in_array($row[
'ENTITY_ID'], $entityIdList))
668 $entityIdList[] = $row[
'ENTITY_ID'];
671 $arResult[$row[
'ENTITY_ID']][
'REACTIONS_LIST'][$row[
'REACTION']] = (int)$row[
'TOTAL_VOTES'];
673 if (!isset(
$count[$row[
'ENTITY_ID']]))
675 $count[$row[
'ENTITY_ID']] = 0;
677 $count[$row[
'ENTITY_ID']] += ((int)$row[
'TOTAL_VOTES'] >= 0 ? (int)$row[
'TOTAL_VOTES'] : 0);
680 !isset($foundDefault[$row[
'ENTITY_ID']])
681 && $row[
'REACTION'] == self::REACTION_DEFAULT
684 $foundDefault[$row[
'ENTITY_ID']] =
true;
688 foreach ($entityIdList as $eId)
691 !isset($foundDefault[$eId])
693 && isset($total[$eId])
694 &&
$count[$eId] >= $total[$eId]
697 $arResult[$eId][
'REACTIONS_LIST'][self::REACTION_DEFAULT] = 0;
701 $sql =
"SELECT RVG.ENTITY_ID, RVG.USER_ID, RVG.VALUE, RVG.REACTION
702 FROM b_rating_vote RVG
703 WHERE RVG.ENTITY_TYPE_ID = '" .
$DB->ForSql($entityTypeId) .
"'
704 and RVG.ENTITY_ID between " . ($bucket * $bucket_size) .
" AND " . (($bucket + 1) * $bucket_size - 1);
706 while ($row =
$res->Fetch())
708 $arResult[$row[
'ENTITY_ID']][
'USER_VOTE_LIST'][$row[
'USER_ID']] = $row[
'VALUE'];
709 $arResult[$row[
'ENTITY_ID']][
'USER_REACTION_LIST'][$row[
'USER_ID']] = (!empty($row[
'REACTION']) ? $row[
'REACTION'] : self::REACTION_DEFAULT);
721 : self::REACTION_DEFAULT
735 static $cacheRatingResult = [];
736 if (!array_key_exists(
$ID, $cacheRatingResult))
738 $cacheRatingResult[
$ID] = [];
747 $value = (int)$value;
750 if (array_key_exists($value, $cacheRatingResult[
$ID]))
756 $arResult[$value] = $cacheRatingResult[
$ID][$value] = [];
757 $arToSelect[$value] = $value;
767 if (isset($cacheRatingResult[
$ID][$value]))
773 $arResult[$value] = $cacheRatingResult[
$ID][$value] = [];
774 $arToSelect[$value] = $value;
779 if (!empty($arToSelect))
782 SELECT ENTITY_TYPE_ID, ENTITY_ID, PREVIOUS_VALUE, CURRENT_VALUE, PREVIOUS_POSITION, CURRENT_POSITION
783 FROM b_rating_results
784 WHERE RATING_ID = '" .
$ID .
"' AND ENTITY_ID IN (" . implode(
',', $arToSelect) .
")
790 $arRes[
'PROGRESS_VALUE'] = round(
$arRes[
'PROGRESS_VALUE'], 2);
791 $arRes[
'PROGRESS_VALUE'] =
$arRes[
'PROGRESS_VALUE'] > 0 ?
"+" .
$arRes[
'PROGRESS_VALUE'] :
$arRes[
'PROGRESS_VALUE'];
792 $arRes[
'ROUND_CURRENT_VALUE'] = round(
$arRes[
'CURRENT_VALUE']) == 0 ? 0 : round(
$arRes[
'CURRENT_VALUE']);
793 $arRes[
'ROUND_PREVIOUS_VALUE'] = round(
$arRes[
'PREVIOUS_VALUE']) == 0 ? 0 : round(
$arRes[
'CURRENT_VALUE']);
795 if (
$arRes[
'PREVIOUS_POSITION'] > 0)
797 $arRes[
'PROGRESS_POSITION'] =
$arRes[
'PREVIOUS_POSITION'] -
$arRes[
'CURRENT_POSITION'];
798 $arRes[
'PROGRESS_POSITION'] =
$arRes[
'PROGRESS_POSITION'] > 0 ?
"+" .
$arRes[
'PROGRESS_POSITION'] :
$arRes[
'PROGRESS_POSITION'];
802 $arRes[
'PREVIOUS_POSITION'] = 0;
803 $arRes[
'PROGRESS_POSITION'] = 0;
823 if ($arParam[
'ENTITY_TYPE_ID'] ===
'USER' && isset(\
Bitrix\Main\Application::getInstance()->getSession()[
'RATING_VOTE_COUNT']))
825 if (\
Bitrix\Main\Application::getInstance()->getSession()[
'RATING_VOTE_COUNT'] >= \
Bitrix\Main\Application::getInstance()->getSession()[
'RATING_USER_VOTE_COUNT'])
833 $arParam[
'ENTITY_TYPE_ID'] = mb_substr($arParam[
'ENTITY_TYPE_ID'], 0, 50);
834 $arParam[
'REACTION'] = ($arParam[
'REACTION'] <>
'' ? $arParam[
'REACTION'] : self::REACTION_DEFAULT);
838 $votePlus = $arParam[
'VALUE'] >= 0 ? true :
false;
843 $voteUserWeight = $arRatingUserProp[
'VOTE_WEIGHT'];
845 $sRatingWeightType = COption::GetOptionString(
"main",
"rating_weight_type",
"auto");
846 if ($sRatingWeightType ===
'auto')
848 if ($arParam[
'ENTITY_TYPE_ID'] ===
'USER')
850 $sRatingAuthrorityWeight = COption::GetOptionString(
"main",
"rating_authority_weight_formula",
'Y');
851 if ($sRatingAuthrorityWeight ===
'Y')
853 $communitySize = COption::GetOptionString(
"main",
"rating_community_size", 1);
854 $communityAuthority = COption::GetOptionString(
"main",
"rating_community_authority", 1);
855 $voteWeight = COption::GetOptionString(
"main",
"rating_vote_weight", 1);
856 $arParam[
'VALUE'] = $arParam[
'VALUE'] * ($communitySize * ($voteUserWeight / $voteWeight) / $communityAuthority);
861 $arParam[
'VALUE'] = $arParam[
'VALUE'] * $voteUserWeight;
866 $arParam[
'VALUE'] = $arParam[
'VALUE'] * $voteUserWeight;
870 'TOTAL_VOTES' =>
"TOTAL_VOTES+1",
871 'TOTAL_VALUE' =>
"TOTAL_VALUE" . ($votePlus ?
'+' :
'') . (
float)$arParam[
'VALUE'],
872 'LAST_CALCULATED' =>
$DB->GetNowFunction(),
874 $arFields[($votePlus ?
'TOTAL_POSITIVE_VOTES' :
'TOTAL_NEGATIVE_VOTES')] = ($votePlus ?
'TOTAL_POSITIVE_VOTES+1' :
'TOTAL_NEGATIVE_VOTES+1');
877 $arParam[
'OWNER_ID'] = 0;
878 foreach (
GetModuleEvents(
"main",
"OnGetRatingContentOwner",
true) as $arEvent)
883 $arParam[
'OWNER_ID'] = (int)
$result;
887 $rowAffected =
$DB->Update(
"b_rating_voting",
$arFields,
"WHERE ENTITY_TYPE_ID='" .
$DB->ForSql($arParam[
'ENTITY_TYPE_ID']) .
"' AND ENTITY_ID='" . (
int)$arParam[
'ENTITY_ID'] .
"'");
888 if ($rowAffected > 0)
890 $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'] .
"'");
891 $arRV = $rsRV->Fetch();
892 $arParam[
'RATING_VOTING_ID'] = $arRV[
'ID'];
893 $arParam[
'TOTAL_POSITIVE_VOTES'] = $arRV[
'TOTAL_POSITIVE_VOTES'];
894 $arParam[
'REACTIONS_LIST'] = [
895 self::REACTION_DEFAULT => (int)$arParam[
'TOTAL_POSITIVE_VOTES'],
900 $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'] .
"'");
901 if (!($arRVR = $rsRVR->fetch()))
903 $merge = $helper->prepareMerge(
'b_rating_voting_reaction', [
'ENTITY_TYPE_ID',
'ENTITY_ID',
'REACTION'], [
904 'ENTITY_TYPE_ID' => $arParam[
'ENTITY_TYPE_ID'],
905 'ENTITY_ID' => $arParam[
'ENTITY_ID'],
906 'REACTION' => self::REACTION_DEFAULT,
907 'TOTAL_VOTES' => $arRV[
'TOTAL_POSITIVE_VOTES'],
909 'TOTAL_VOTES' => $arRV[
'TOTAL_POSITIVE_VOTES'],
913 $DB->query($merge[0]);
917 $merge = $helper->prepareMerge(
'b_rating_voting_reaction', [
'ENTITY_TYPE_ID',
'ENTITY_ID',
'REACTION'], [
918 'ENTITY_TYPE_ID' => $arParam[
'ENTITY_TYPE_ID'],
919 'ENTITY_ID' => $arParam[
'ENTITY_ID'],
920 'REACTION' => $arParam[
'REACTION'],
923 'TOTAL_VOTES' =>
new \
Bitrix\Main\DB\SqlExpression(
'b_rating_voting_reaction.TOTAL_VOTES + 1'),
927 $DB->query($merge[0]);
934 "ENTITY_TYPE_ID" =>
"'" .
$DB->ForSql($arParam[
"ENTITY_TYPE_ID"]) .
"'",
935 "ENTITY_ID" => (int)$arParam[
'ENTITY_ID'],
936 "OWNER_ID" => (
int)$arParam[
'OWNER_ID'],
938 "CREATED" =>
$DB->GetNowFunction(),
939 "LAST_CALCULATED" =>
$DB->GetNowFunction(),
941 "TOTAL_VALUE" => (float)$arParam[
'VALUE'],
942 "TOTAL_POSITIVE_VOTES" => ($votePlus ? 1 : 0),
943 "TOTAL_NEGATIVE_VOTES" => ($votePlus ? 0 : 1),
945 $arParam[
'RATING_VOTING_ID'] =
$DB->Insert(
"b_rating_voting",
$arFields);
946 $arParam[
'TOTAL_POSITIVE_VOTES'] = ($votePlus ? 1 : 0);
948 $arParam[
'REACTIONS_LIST'] = [
949 self::REACTION_DEFAULT => (int)$arParam[
'TOTAL_POSITIVE_VOTES'],
954 $merge = $helper->prepareMerge(
'b_rating_voting_reaction', [
'ENTITY_TYPE_ID',
'ENTITY_ID',
'REACTION'], [
955 'ENTITY_TYPE_ID' => $arParam[
'ENTITY_TYPE_ID'],
956 'ENTITY_ID' => $arParam[
'ENTITY_ID'],
957 'REACTION' => $arParam[
'REACTION'],
964 $DB->query($merge[0]);
969 $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'] .
"'");
970 while ($arRVR = $rsRVR->fetch())
972 $arParam[
'REACTIONS_LIST'][$arRVR[
'REACTION']] = $arRVR[
'TOTAL_VOTES'];
976 "RATING_VOTING_ID" => (int)$arParam[
'RATING_VOTING_ID'],
977 "ENTITY_TYPE_ID" =>
"'" .
$DB->ForSql($arParam[
"ENTITY_TYPE_ID"]) .
"'",
978 "ENTITY_ID" => (int)$arParam[
'ENTITY_ID'],
979 "VALUE" => (
float)$arParam[
'VALUE'],
981 "CREATED" =>
$DB->GetNowFunction(),
982 "USER_ID" => (int)$arParam[
'USER_ID'],
983 "USER_IP" =>
"'" .
$DB->ForSql($arParam[
"USER_IP"]) .
"'",
984 "OWNER_ID" => (int)$arParam[
'OWNER_ID'],
985 "REACTION" =>
"'" .
$DB->ForSql($arParam[
"REACTION"]) .
"'",
994 $userData = static::getUserData((
int)$arParam[
'USER_ID'], (
float)$arParam[
'VALUE']);
995 if (CModule::IncludeModule(
'pull'))
997 CPullStack::AddShared([
998 'module_id' =>
'main',
999 'command' =>
'rating_vote',
1002 "USER_ID" => (
int)$arParam[
'USER_ID'],
1003 "ENTITY_TYPE_ID" => $arParam[
"ENTITY_TYPE_ID"],
1004 "ENTITY_ID" => (
int)$arParam[
'ENTITY_ID'],
1005 "TOTAL_POSITIVE_VOTES" => $arParam[
'TOTAL_POSITIVE_VOTES'],
1006 "RESULT" => $votePlus ?
'PLUS' :
'MINUS',
1007 "USER_DATA" => $userData,
1008 "REACTION" => $arParam[
'REACTION'],
1009 "REACTIONS_LIST" => $arParam[
'REACTIONS_LIST'],
1014 if (CACHED_b_rating_vote !==
false)
1016 $bucket_size = (int)CACHED_b_rating_bucket_size;
1017 if ($bucket_size <= 0)
1021 $bucket = (int)((
int)$arParam[
'ENTITY_ID'] / $bucket_size);
1022 $CACHE_MANAGER->Clean(
"b_rvg_" .
$DB->ForSql($arParam[
"ENTITY_TYPE_ID"]) . $bucket,
"b_rating_voting");
1034 $arParam[
'ENTITY_TYPE_ID'] = mb_substr($arParam[
'ENTITY_TYPE_ID'], 0, 50);
1035 $arParam[
'REACTION'] = ($arParam[
'REACTION'] <>
'' ? $arParam[
'REACTION'] : self::REACTION_DEFAULT);
1036 $userData = static::getUserData((
int)$arParam[
'USER_ID'], (
float)$arParam[
'VALUE']);
1042 RV.REACTION AS REACTION,
1043 RV.VALUE AS VOTE_VALUE,
1044 RVG.TOTAL_POSITIVE_VOTES
1046 b_rating_voting RVG,
1049 RVG.ENTITY_TYPE_ID = '" .
$DB->ForSql($arParam[
'ENTITY_TYPE_ID']) .
"'
1050 and RVG.ENTITY_ID = " . (int)$arParam[
'ENTITY_ID'] .
"
1051 and RVG.ID = RV.RATING_VOTING_ID
1052 and RV.USER_ID = " . (
int)$arParam[
'USER_ID'];
1055 if ($arVote =
$res->Fetch())
1058 $arParam[
'OWNER_ID'] = 0;
1059 foreach (
GetModuleEvents(
"main",
"OnGetRatingContentOwner",
true) as $arEvent)
1064 $arParam[
'OWNER_ID'] = (int)
$result;
1068 $votePlus = $arVote[
'VOTE_VALUE'] >= 0 ? true :
false;
1069 $arVote[
'REACTION_OLD'] = ($arVote[
'REACTION'] <>
'' ? $arVote[
'REACTION'] : self::REACTION_DEFAULT);
1076 $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'] .
"'");
1077 if ($arRV = $rsRV->Fetch())
1079 $arParam[
'RATING_VOTING_ID'] = $arRV[
'ID'];
1080 $arParam[
'TOTAL_POSITIVE_VOTES'] = $arRV[
'TOTAL_POSITIVE_VOTES'];
1081 $arParam[
'REACTIONS_LIST'] = [
1082 self::REACTION_DEFAULT => (int)$arParam[
'TOTAL_POSITIVE_VOTES'],
1090 $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'] .
"'");
1091 if (!($arRVR = $rsRVR->fetch()))
1093 $merge = $helper->prepareMerge(
'b_rating_voting_reaction', [
'ENTITY_TYPE_ID',
'ENTITY_ID',
'REACTION'], [
1094 'ENTITY_TYPE_ID' => $arParam[
'ENTITY_TYPE_ID'],
1095 'ENTITY_ID' => $arParam[
'ENTITY_ID'],
1096 'REACTION' => self::REACTION_DEFAULT,
1097 'TOTAL_VOTES' => $arRV[
'TOTAL_POSITIVE_VOTES'],
1099 'TOTAL_VOTES' => $arRV[
'TOTAL_POSITIVE_VOTES'],
1103 $DB->query($merge[0]);
1107 $merge = $helper->prepareMerge(
'b_rating_voting_reaction', [
'ENTITY_TYPE_ID',
'ENTITY_ID',
'REACTION'], [
1108 'ENTITY_TYPE_ID' => $arParam[
'ENTITY_TYPE_ID'],
1109 'ENTITY_ID' => $arParam[
'ENTITY_ID'],
1110 'REACTION' => $arParam[
'REACTION'],
1113 'TOTAL_VOTES' =>
new \
Bitrix\Main\DB\SqlExpression(
'b_rating_voting_reaction.TOTAL_VOTES + 1'),
1117 $DB->query($merge[0]);
1120 if (!empty($arVote[
'REACTION_OLD']))
1122 $DB->Query(
"UPDATE b_rating_voting_reaction SET TOTAL_VOTES = TOTAL_VOTES - 1 WHERE ENTITY_TYPE_ID = '" .
$DB->ForSql($arParam[
'ENTITY_TYPE_ID']) .
"' AND ENTITY_ID = '" . (
int)$arParam[
'ENTITY_ID'] .
"' AND REACTION = '" .
$DB->ForSql($arVote[
'REACTION_OLD']) .
"'");
1125 $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'] .
"'");
1126 while ($arRVR = $rsRVR->fetch())
1128 $arParam[
'REACTIONS_LIST'][$arRVR[
'REACTION']] = $arRVR[
'TOTAL_VOTES'];
1132 "CREATED" =>
$DB->GetNowFunction(),
1133 "USER_IP" =>
"'" .
$DB->ForSql($arParam[
"USER_IP"]) .
"'",
1134 "REACTION" =>
"'" .
$DB->ForSql($arParam[
"REACTION"]) .
"'",
1137 $ID =
$DB->Update(
"b_rating_vote",
$arFields,
"WHERE RATING_VOTING_ID=" . (
int)$arParam[
'RATING_VOTING_ID'] .
" AND USER_ID=" . (
int)$arParam[
'USER_ID']);
1143 foreach (
GetModuleEvents(
"main",
"OnChangeRatingVote",
true) as $arEvent)
1148 if (CModule::IncludeModule(
'pull'))
1150 CPullStack::AddShared([
1151 'module_id' =>
'main',
1152 'command' =>
'rating_vote',
1155 "USER_ID" => (
int)$arParam[
'USER_ID'],
1156 "ENTITY_TYPE_ID" => $arParam[
"ENTITY_TYPE_ID"],
1157 "ENTITY_ID" => (
int)$arParam[
'ENTITY_ID'],
1158 "TOTAL_POSITIVE_VOTES" => $arParam[
'TOTAL_POSITIVE_VOTES'],
1159 "RESULT" =>
'CHANGE',
1160 "USER_DATA" => $userData,
1161 "REACTION" => $arParam[
'REACTION'],
1162 "REACTION_OLD" => $arVote[
'REACTION'],
1163 "REACTIONS_LIST" => $arParam[
'REACTIONS_LIST'],
1168 if (CACHED_b_rating_vote !==
false)
1170 $bucket_size = (int)CACHED_b_rating_bucket_size;
1171 if ($bucket_size <= 0)
1175 $bucket = (int)((
int)$arParam[
'ENTITY_ID'] / $bucket_size);
1176 $CACHE_MANAGER->Clean(
"b_rvg_" .
$DB->ForSql($arParam[
"ENTITY_TYPE_ID"]) . $bucket,
"b_rating_voting");
1193 RV.REACTION AS REACTION,
1194 RV.VALUE AS VOTE_VALUE,
1195 RVG.TOTAL_POSITIVE_VOTES
1197 b_rating_voting RVG,
1200 RVG.ENTITY_TYPE_ID = '" .
$DB->ForSql($arParam[
'ENTITY_TYPE_ID']) .
"'
1201 and RVG.ENTITY_ID = " . (int)$arParam[
'ENTITY_ID'] .
"
1202 and RVG.ID = RV.RATING_VOTING_ID
1203 and RV.USER_ID = " . (
int)$arParam[
'USER_ID'];
1206 if ($arVote =
$res->Fetch())
1208 $votePlus = $arVote[
'VOTE_VALUE'] >= 0 ? true :
false;
1209 $arVote[
'REACTION'] = ($arVote[
'REACTION'] <>
'' ? $arVote[
'REACTION'] : self::REACTION_DEFAULT);
1213 $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'] .
"'");
1214 if (!($arRVR = $rsRVR->fetch()))
1216 $merge = $helper->prepareMerge(
'b_rating_voting_reaction', [
'ENTITY_TYPE_ID',
'ENTITY_ID',
'REACTION'], [
1217 'ENTITY_TYPE_ID' => $arParam[
'ENTITY_TYPE_ID'],
1218 'ENTITY_ID' => $arParam[
'ENTITY_ID'],
1219 'REACTION' => self::REACTION_DEFAULT,
1220 'TOTAL_VOTES' => $arVote[
'TOTAL_POSITIVE_VOTES'],
1222 'TOTAL_VOTES' => $arVote[
'TOTAL_POSITIVE_VOTES'],
1226 $DB->query($merge[0]);
1232 'TOTAL_VOTES' =>
"TOTAL_VOTES-1",
1233 'TOTAL_VALUE' =>
"TOTAL_VALUE" . ($votePlus ?
'-' . (float)$arVote[
'VOTE_VALUE'] :
'+' . (float)(-1 * $arVote[
'VOTE_VALUE'])),
1234 'LAST_CALCULATED' =>
$DB->GetNowFunction(),
1236 $arFields[($votePlus ?
'TOTAL_POSITIVE_VOTES' :
'TOTAL_NEGATIVE_VOTES')] = ($votePlus ?
'TOTAL_POSITIVE_VOTES-1' :
'TOTAL_NEGATIVE_VOTES-1');
1237 $DB->Update(
"b_rating_voting",
$arFields,
"WHERE ID=" . (
int)$arVote[
'ID']);
1240 $merge = $helper->prepareMerge(
'b_rating_voting_reaction', [
'ENTITY_TYPE_ID',
'ENTITY_ID',
'REACTION'], [
1241 'ENTITY_TYPE_ID' => $arParam[
'ENTITY_TYPE_ID'],
1242 'ENTITY_ID' => $arParam[
'ENTITY_ID'],
1243 'REACTION' => $arVote[
'REACTION'],
1246 'TOTAL_VOTES' =>
new \
Bitrix\Main\DB\SqlExpression(
'b_rating_voting_reaction.TOTAL_VOTES - 1'),
1250 $DB->query($merge[0]);
1253 $DB->Query(
"DELETE FROM b_rating_vote WHERE ID=" . (
int)$arVote[
'VOTE_ID']);
1255 $arParam[
'REACTIONS_LIST'] = [];
1256 $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'] .
"'");
1257 while ($arRVR = $rsRVR->fetch())
1259 $arParam[
'REACTIONS_LIST'][$arRVR[
'REACTION']] = $arRVR[
'TOTAL_VOTES'];
1262 foreach (
GetModuleEvents(
"main",
"OnCancelRatingVote",
true) as $arEvent)
1267 $userData = static::getUserData((
int)$arParam[
'USER_ID'], (
float)$arVote[
'VOTE_VALUE']);
1269 if (CModule::IncludeModule(
'pull'))
1271 CPullStack::AddShared([
1272 'module_id' =>
'main',
1273 'command' =>
'rating_vote',
1276 "USER_ID" => (
int)$arParam[
'USER_ID'],
1277 "ENTITY_TYPE_ID" => $arParam[
"ENTITY_TYPE_ID"],
1278 "ENTITY_ID" => (
int)$arParam[
'ENTITY_ID'],
1279 "TOTAL_POSITIVE_VOTES" => (
int)($arVote[
'TOTAL_POSITIVE_VOTES'] + ($votePlus ? -1 : 1)),
1280 "RESULT" => $votePlus ?
'PLUS' :
'MINUS',
1281 "USER_DATA" => $userData,
1282 "REACTION" => $arVote[
'REACTION'],
1283 "REACTIONS_LIST" => $arParam[
'REACTIONS_LIST'],
1288 if (CACHED_b_rating_vote !==
false)
1290 $bucket_size = (int)CACHED_b_rating_bucket_size;
1291 if ($bucket_size <= 0)
1295 $bucket = (int)((
int)$arParam[
'ENTITY_ID'] / $bucket_size);
1296 $CACHE_MANAGER->Clean(
"b_rvg_" .
$DB->ForSql($arParam[
"ENTITY_TYPE_ID"]) . $bucket,
"b_rating_voting");
1309 $arParam[
'RATING_ID'] = (int)$arParam[
'RATING_ID'];
1310 $arParam[
'ENTITY_ID'] = (int)$arParam[
'ENTITY_ID'];
1311 $arParam[
'BONUS'] = (float)$arParam[
'BONUS'];
1314 'RATING_ID' => $arParam[
'RATING_ID'],
1315 'ENTITY_ID' => $arParam[
'ENTITY_ID'],
1316 'BONUS' => $arParam[
'BONUS'],
1319 if (isset($arParam[
'VOTE_WEIGHT']))
1321 $arFields[
'VOTE_WEIGHT'] = (float)$arParam[
'VOTE_WEIGHT'];
1324 if (isset($arParam[
'VOTE_COUNT']))
1326 $arFields[
'VOTE_COUNT'] = (int)$arParam[
'VOTE_COUNT'];
1329 $rows =
$DB->Update(
"b_rating_user",
$arFields,
"WHERE RATING_ID = " . $arParam[
'RATING_ID'] .
" AND ENTITY_ID = " . $arParam[
'ENTITY_ID']);
1332 $rsRB =
$DB->Query(
"SELECT * FROM b_rating_user WHERE RATING_ID = " . $arParam[
'RATING_ID'] .
" AND ENTITY_ID = " . $arParam[
'ENTITY_ID']);
1333 if (!$rsRB->SelectedRowsCount())
1338 if (CACHED_b_rating_vote !==
false)
1341 $bucket_size = (int)CACHED_b_rating_bucket_size;
1342 if ($bucket_size <= 0)
1347 $CACHE_MANAGER->Clean(
"b_rvu_" . $arParam[
'RATING_ID'] . (
int)($arParam[
'ENTITY_ID'] / $bucket_size),
"b_rating_user");
1356 $ratingId = (int)$ratingId;
1359 if (!array_key_exists($ratingId, $cache))
1361 $cache[$ratingId] = [];
1370 $value = (int)$value;
1373 if (array_key_exists($value, $cache[$ratingId]))
1375 $arResult[$value] = $cache[$ratingId][$value];
1379 $arResult[$value] = $cache[$ratingId][$value] = [];
1380 $arToSelect[$value] = $value;
1390 if (isset($cache[$ratingId][$value]))
1392 $arResult[$value] = $cache[$ratingId][$value];
1396 $arResult[$value] = $cache[$ratingId][$value] = [];
1397 $arToSelect[$value] = $value;
1402 if (!empty($arToSelect))
1405 SELECT RATING_ID, ENTITY_ID, BONUS, VOTE_WEIGHT, VOTE_COUNT
1407 WHERE RATING_ID = '" . $ratingId .
"' AND ENTITY_ID IN (" . implode(
',', $arToSelect) .
")
1428 $ratingId = (int)$ratingId;
1431 $arDefaultResult = [
1432 "RATING_ID" => $ratingId,
1440 return $arDefaultResult;
1443 $bucket_size = (int)CACHED_b_rating_bucket_size;
1444 if ($bucket_size <= 0)
1449 $bucket = (int)(
$entityId / $bucket_size);
1450 $arResult =
$CACHE_MANAGER->Read(CACHED_b_rating, $cache_id =
"b_rvu_" . $ratingId . $bucket,
"b_rating_user");
1458 SELECT RATING_ID, ENTITY_ID, BONUS, VOTE_WEIGHT, VOTE_COUNT
1460 WHERE RATING_ID = '" . $ratingId .
"'
1461 and ENTITY_ID between " . ($bucket * $bucket_size) .
" AND " . (($bucket + 1) * $bucket_size - 1) .
"
1479 $authorityRatingId = COption::GetOptionString(
"main",
"rating_authority_rating",
null);
1480 if (is_null($authorityRatingId))
1485 $authorityRatingId = (int)
$res[
'ID'];
1486 COption::SetOptionString(
"main",
"rating_authority_rating", $authorityRatingId);
1489 return $authorityRatingId;
1503 if ((
string)
$val ==
'' ||
$val ===
"NOT_REF")
1507 switch (strtoupper(
$key))
1525 if (in_array(
$val, [
'Y',
'N']))
1527 $arSqlSearch[] =
"R.MAX = '" .
$val .
"'";
1537 $ord = (mb_strtoupper(
$val) <>
"ASC" ?
"DESC" :
"ASC");
1538 switch (mb_strtoupper(
$key))
1541 $sOrder .=
", RW.ID " . $ord;
1544 $sOrder .=
", RW.RATING_FROM " . $ord;
1547 $sOrder .=
", RW.RATING_TO " . $ord;
1550 $sOrder .=
", RW.WEIGHT " . $ord;
1553 $sOrder .=
", RW.COUNT " . $ord;
1560 $sOrder =
"RW.ID DESC";
1563 $strSqlOrder =
" ORDER BY " . trim($sOrder,
", ");
1568 RW.ID, RW.RATING_FROM, RW.RATING_TO, RW.WEIGHT, RW.COUNT
1572 " . $strSqlSearch .
"
1574 return $DB->Query($strSql);
1581 usort($arConfigs, [
'CRatings',
'__SortWeight']);
1584 foreach ($arConfigs as
$key => $arConfig)
1589 $arConfig[
'RATING_FROM'] = -1000000;
1593 $arConfig[
'RATING_FROM'] = (float)$arConfigs[
$key - 1][
'RATING_TO'] + 0.0001;
1596 if (!array_key_exists(
'RATING_TO', $arConfig))
1598 $arConfig[
'RATING_TO'] = 1000000;
1600 elseif ($arConfig[
'RATING_TO'] > 1000000)
1602 $arConfig[
'RATING_TO'] = 1000000;
1605 $arAdd[
$key][
'RATING_FROM'] = (float)$arConfig[
'RATING_FROM'];
1606 $arAdd[
$key][
'RATING_TO'] = (float)$arConfig[
'RATING_TO'];
1607 $arAdd[
$key][
'WEIGHT'] = (float)$arConfig[
'WEIGHT'];
1608 $arAdd[
$key][
'COUNT'] = (int)$arConfig[
'COUNT'];
1612 $DB->Query(
"DELETE FROM b_rating_weight");
1625 if (!in_array(
$type, [
'R',
'A']))
1630 if (!is_array($arGroupId))
1637 foreach ($arGroupId as
$key => $value)
1640 $arField[
'GROUP_ID'] = (int)$value;
1641 $arField[
'TYPE'] =
"'" .
$type .
"'";
1645 $DB->Query(
"DELETE FROM b_rating_vote_group WHERE TYPE = '" .
$type .
"'");
1648 $DB->Insert(
"b_rating_vote_group", $arField);
1661 if (!in_array(
$type, [
'R',
'A']))
1666 $strSql =
"SELECT ID, GROUP_ID, TYPE FROM b_rating_vote_group RVG";
1670 $strSql .=
" WHERE TYPE = '" .
$type .
"'";
1673 return $DB->Query($strSql);
1687 $strSql =
"SELECT GROUP_ID, TYPE FROM b_rating_vote_group RVG";
1699 if ($value[
'TYPE'] !=
$type)
1712 $DB->Query(
"TRUNCATE TABLE b_rating_prepare");
1713 $DB->Query(
"TRUNCATE TABLE b_rating_voting_prepare");
1715 $DB->Query(
"TRUNCATE TABLE b_rating_results");
1716 $DB->Query(
"TRUNCATE TABLE b_rating_component_results");
1718 $DB->Query(
"TRUNCATE TABLE b_rating_vote");
1719 $DB->Query(
"TRUNCATE TABLE b_rating_voting");
1720 $DB->Query(
"TRUNCATE TABLE b_rating_voting_reaction");
1722 $DB->Query(
"UPDATE b_rating_user SET VOTE_WEIGHT = 0, VOTE_COUNT = 0");
1740 if (isset(
$arFields[
'EXTERNAL_AUTH_ID']) && in_array(
$arFields[
'EXTERNAL_AUTH_ID'], \
Bitrix\Main\UserTable::getExternalUserTypes(),
true))
1749 $ratingStartValue = COption::GetOptionString(
"main",
"rating_start_authority", 3);
1750 $ratingCountVote = COption::GetOptionString(
"main",
"rating_count_vote", 10);
1753 'RATING_ID' => $authorityRatingId,
1755 'BONUS' => (int)$ratingStartValue,
1756 'VOTE_WEIGHT' => (
int)$ratingStartValue * COption::GetOptionString(
"main",
"rating_vote_weight", 1),
1757 'VOTE_COUNT' => (int)$ratingCountVote + (
int)$ratingStartValue,
1763 $strSql =
"INSERT INTO b_rating_subordinate (RATING_ID, ENTITY_ID, VOTES) VALUES ('" . $authorityRatingId .
"', '" .
$userId .
"', '" . ((int)$ratingCountVote + (int)$ratingStartValue) .
"')";
1764 $DB->Query($strSql);
1767 $sRatingAssignType = COption::GetOptionString(
"main",
"rating_assign_type",
'manual');
1768 if ($sRatingAssignType ===
'auto')
1770 $assignRatingGroup = COption::GetOptionString(
"main",
"rating_assign_rating_group", 0);
1771 $assignAuthorityGroup = COption::GetOptionString(
"main",
"rating_assign_authority_group", 0);
1772 if ($assignRatingGroup == 0 && $assignAuthorityGroup == 0)
1778 if ($assignRatingGroup > 0)
1782 if ($assignAuthorityGroup > 0 && $assignRatingGroup != $assignAuthorityGroup)
1784 $arGroups[] = (int)$assignAuthorityGroup;
1792 if (CACHED_b_rating_vote !==
false)
1795 $bucket_size = (int)CACHED_b_rating_bucket_size;
1796 if ($bucket_size <= 0)
1801 $bucket = (int)(
$userId / $bucket_size);
1802 $CACHE_MANAGER->Clean(
"b_rvu_" . $authorityRatingId . $bucket,
"b_rating_user");
1809 if (isset(
$a[
'RATING_FROM']) || isset($b[
'RATING_FROM']))
1814 return (
float)
$a[
'RATING_TO'] < (float)$b[
'RATING_TO'] ? -1 : 1;
1824 $aMsg[] = [
"id" =>
"NAME",
"text" =>
GetMessage(
"RATING_GENERAL_ERR_NAME")];
1828 $aMsg[] = [
"id" =>
"ACTIVE",
"text" =>
GetMessage(
"RATING_GENERAL_ERR_ACTIVE")];
1833 if (!in_array(
$arFields[
'ENTITY_ID'], $arObjects))
1835 $aMsg[] = [
"id" =>
"ENTITY_ID",
"text" =>
GetMessage(
"RATING_GENERAL_ERR_ENTITY_ID")];
1840 $aMsg[] = [
"id" =>
"CALCULATION_METHOD",
"text" =>
GetMessage(
"RATING_GENERAL_ERR_CAL_METHOD")];
1846 $GLOBALS[
"APPLICATION"]->ThrowException($e);
1862 foreach (
$arFields[
'CONFIGS'] as $MODULE_ID => $RAT_ARRAY)
1864 if (!is_array($RAT_ARRAY))
1869 foreach ($RAT_ARRAY as $RAT_TYPE => $COMPONENT)
1871 if (!is_array($COMPONENT))
1876 foreach ($COMPONENT as $COMPONENT_NAME => $COMPONENT_VALUE)
1878 if (!isset($arRatingConfigs[$MODULE_ID][$MODULE_ID .
"_" . $RAT_TYPE .
"_" . $COMPONENT_NAME]))
1885 "ACTIVE" => isset($COMPONENT_VALUE[
"ACTIVE"]) && $COMPONENT_VALUE[
"ACTIVE"] ===
'Y' ?
'Y' :
'N',
1887 "MODULE_ID" => $MODULE_ID,
1888 "RATING_TYPE" => $RAT_TYPE,
1889 "NAME" => $COMPONENT_NAME,
1890 "COMPLEX_NAME" =>
$arFields[
"ENTITY_ID"] .
'_' . $MODULE_ID .
'_' . $RAT_TYPE .
'_' . $COMPONENT_NAME,
1891 "CLASS" => $arRatingConfigs[$MODULE_ID][$MODULE_ID .
"_" . $RAT_TYPE .
"_" . $COMPONENT_NAME][
"CLASS"],
1892 "CALC_METHOD" => $arRatingConfigs[$MODULE_ID][$MODULE_ID .
"_" . $RAT_TYPE .
"_" . $COMPONENT_NAME][
"CALC_METHOD"],
1893 "EXCEPTION_METHOD" => $arRatingConfigs[$MODULE_ID][$MODULE_ID .
"_" . $RAT_TYPE .
"_" . $COMPONENT_NAME][
"EXCEPTION_METHOD"],
1894 "REFRESH_INTERVAL" => $arRatingConfigs[$MODULE_ID][$MODULE_ID .
"_" . $RAT_TYPE .
"_" . $COMPONENT_NAME][
"REFRESH_TIME"],
1895 "~LAST_MODIFIED" =>
$DB->GetNowFunction(),
1896 "~NEXT_CALCULATION" =>
$DB->GetNowFunction(),
1897 "IS_CALCULATED" =>
"N",
1898 "~CONFIG" =>
"'" . serialize($COMPONENT_VALUE) .
"'",
1901 $DB->Add(
"b_rating_component", $arFields_i);
1915 $DB->Query(
"DELETE FROM b_rating_component WHERE RATING_ID=$ID");
1936 $bplus = (mb_strtoupper($arParam[
'LIST_TYPE']) !==
'MINUS');
1937 $key = $arParam[
'ENTITY_TYPE_ID'] .
'_' . (int)$arParam[
'ENTITY_ID'] .
'_' . ($bplus ?
'1' :
'0');
1940 isset($arParam[
'USE_REACTIONS_CACHE'])
1941 && $arParam[
'USE_REACTIONS_CACHE'] ===
'Y'
1942 && isset($cache[
$key])
1956 RV.ENTITY_TYPE_ID = '" .
$DB->ForSql($arParam[
'ENTITY_TYPE_ID']) .
"'
1957 AND RV.ENTITY_ID = " . (int)$arParam[
'ENTITY_ID'] .
" " .
1959 "GROUP BY REACTION";
1960 $res_cnt =
$DB->Query($sqlStr);
1964 while ($ar_cnt = $res_cnt->fetch())
1966 $key = (!empty($ar_cnt[
"REACTION"]) ? $ar_cnt[
"REACTION"] : self::REACTION_DEFAULT);
1967 if (!isset($cntReactions[
$key]))
1969 $cntReactions[
$key] = 0;
1971 $cntReactions[
$key] += $ar_cnt[
"CNT"];
1972 $cnt += $ar_cnt[
"CNT"];
1976 'items_all' => $cnt,
1977 'reactions' => $cntReactions,
1988 $reactionResult = self::GetRatingVoteReaction($arParam);
1989 $cnt = $reactionResult[
'items_all'];
1990 $cntReactions = $reactionResult[
'reactions'];
1992 $bplus = (mb_strtoupper($arParam[
'LIST_TYPE']) !==
'MINUS');
1998 $includeReaction = ($arParam[
'INCLUDE_REACTION'] ??
'N') ===
'Y';
2002 array_key_exists(
"USER_FIELDS", $arParam)
2003 && is_array($arParam[
"USER_FIELDS"])
2006 array_key_exists(
"USER_SELECT", $arParam)
2007 && is_array($arParam[
"USER_SELECT"])
2021 if ($arParam[
'LIST_LIMIT'] != 0 && ceil($cnt / (
int)$arParam[
'LIST_LIMIT']) >= (
int)$arParam[
'LIST_PAGE'])
2023 $res =
new CDBResult();
2024 $res->NavQuery($sqlStr, $cnt, [
'iNumPage' => (
int)$arParam[
'LIST_PAGE'],
'nPageSize' => (
int)$arParam[
'LIST_LIMIT']]);
2026 while ($row =
$res->Fetch())
2032 $ar[
"PHOTO"] =
$ar[
"PHOTO_SRC"] =
'';
2033 if (!empty(
$ar[
"PERSONAL_PHOTO"]))
2035 $arFileTmp = CFile::ResizeImageGet(
2036 $row[
"PERSONAL_PHOTO"],
2037 [
'width' => 58,
'height' => 58],
2041 $ar[
'PHOTO'] = CFile::ShowImage($arFileTmp[
'src'], 21, 21,
'border=0');
2042 $ar[
'PHOTO_SRC'] = $arFileTmp[
'src'];
2044 $ar[
'FULL_NAME'] = CUser::FormatName(CSite::GetNameFormat(
false), $row,
true);
2048 $arUserID[] = $row[
"ID"];
2053 $arList[
$ar[
'ID']] =
$ar;
2057 $arVoteList[
$ar[
'ID']] =
$ar;
2060 foreach ($arList as
$ar)
2062 $arVoteList[
$ar[
'ID']] =
$ar;
2067 && !empty($arUserID)
2070 $arUserListParams = [];
2073 $arUserListParams[
"FIELDS"] = (
2074 array_key_exists(
"USER_FIELDS", $arParam)
2075 && is_array($arParam[
"USER_FIELDS"])
2076 ? $arParam[
"USER_FIELDS"]
2077 : [
"NAME",
"LAST_NAME",
"SECOND_NAME",
"LOGIN",
"PERSONAL_PHOTO"]
2080 $arUserListParams[
"FIELDS"] = array_unique(array_merge([
"ID"], $arUserListParams[
"FIELDS"]));
2083 array_key_exists(
"USER_SELECT", $arParam)
2084 && is_array($arParam[
"USER_SELECT"])
2087 $arUserListParams[
"SELECT"] = $arParam[
"USER_SELECT"];
2090 $rsUser = CUser::GetList(
2093 [
"ID" => implode(
"|", $arUserID)],
2097 while ($arUser = $rsUser->Fetch())
2099 $arUser[
"PHOTO"] = $arUser[
"PHOTO_SRC"] =
'';
2100 if (array_key_exists(
"PERSONAL_PHOTO", $arUser))
2102 $arFileTmp = CFile::ResizeImageGet(
2103 $arUser[
"PERSONAL_PHOTO"],
2104 [
"width" => 58,
"height" => 58],
2108 $arUser[
"PHOTO_SRC"] = $arFileTmp[
"src"];
2109 $arUser[
"PHOTO"] = CFile::ShowImage($arFileTmp[
"src"], 21, 21,
"border=0");
2111 $arUser[
"FULL_NAME"] = CUser::FormatName(CSite::GetNameFormat(
false), $arUser,
true);
2112 $arUsers[$arUser[
"ID"]] = $arUser;
2115 foreach ($arVoteList as
$i => $arVoteUser)
2117 if (array_key_exists($arVoteUser[
"ID"], $arUsers))
2119 foreach ($arUsers[$arVoteUser[
"ID"]] as
$key => $value)
2121 $arVoteList[
$i][
$key] = $value;
2129 'items_all' => $cnt,
2130 'items_page' =>
count($arVoteList),
2131 'items' => $arVoteList,
2132 'reactions' => $cntReactions,
2133 'list_page' => isset($arParam[
'LIST_PAGE']) ? (int)$arParam[
'LIST_PAGE'] : 0,
2141 if (!\
Bitrix\Main\ModuleManager::isModuleInstalled(
'intranet'))
2157 $ratingId = \CRatings::getAuthorityRating();
2158 if ((
int)$ratingId <= 0)
2164 if ($record =
$res->fetch())
2166 $result = (float)$record[
'VOTES'];
2188 if ($userFields =
$res->fetch())
2191 'NAME_FORMATTED' => \CUser::formatName(
2192 \CSite::getNameFormat(
false),
2194 'NAME' => $userFields[
"NAME"],
2195 'LAST_NAME' => $userFields[
"LAST_NAME"],
2196 'SECOND_NAME' => $userFields[
"SECOND_NAME"],
2197 'LOGIN' => $userFields[
"LOGIN"],
2201 'PERSONAL_PHOTO' => [
2202 'ID' => $userFields[
"PERSONAL_PHOTO"],
2212 if ((
int)$userFields[
'PERSONAL_PHOTO'] > 0)
2214 $imageFile = \CFile::getFileArray($userFields[
"PERSONAL_PHOTO"]);
2215 if ($imageFile !==
false)
2217 $file = \CFile::resizeImageGet(
2219 [
"width" => 100,
"height" => 100],
2223 $result[
'PERSONAL_PHOTO'][
'SRC'] = $file[
'src'];
2240 !empty(
$params[
'entityTypeId'])
2251 if (!is_array($entityIdList))
2253 $entityIdList = [$entityIdList];
2256 if (empty($entityIdList))
2261 $ratingId = \CRatings::getAuthorityRating();
2262 if ((
int)$ratingId <= 0)
2289 if (\
Bitrix\Main\ModuleManager::isModuleInstalled(
'intranet'))
2293 RS1.ENTITY_ID as USER_ID,
2294 RV1.ENTITY_ID as ENTITY_ID,
2295 MAX(RS1.VOTES) as WEIGHT
2297 b_rating_subordinate RS1,
2300 RS1.ENTITY_ID = RV1.USER_ID
2301 AND RS1.RATING_ID = " . (
int)$ratingId .
"
2302 AND RV1.ENTITY_TYPE_ID = '" . $helper->forSQL($entityTypeId) .
"'
2303 AND RV1.ENTITY_ID IN (" . implode(
',', $entityIdList) .
")
2305 RV1.ENTITY_ID, RS1.ENTITY_ID
2315 RV1.USER_ID as USER_ID,
2316 RV1.ENTITY_ID as ENTITY_ID,
2321 RV1.ENTITY_TYPE_ID = '" . $helper->forSQL($entityTypeId) .
"'
2322 AND RV1.ENTITY_ID IN (" . implode(
',', $entityIdList) .
")
2329 $userWeightData = $entityUserData = [];
2331 $currentEntityId =
false;
2334 while ($voteFields =
$res->fetch())
2338 && $voteFields[
'USER_ID'] ==
$USER->getId()
2344 if ($voteFields[
'ENTITY_ID'] != $currentEntityId)
2348 $entityUserData[$voteFields[
'ENTITY_ID']] = [];
2351 $currentEntityId = $voteFields[
'ENTITY_ID'];
2354 if ($cnt > ($hasMine ? $topCount + 1 : $topCount))
2359 $entityUserData[$voteFields[
'ENTITY_ID']][] = $voteFields[
'USER_ID'];
2360 if (!isset($userWeightData[$voteFields[
'USER_ID']]))
2362 $userWeightData[$voteFields[
'USER_ID']] = (float)$voteFields[
'WEIGHT'];
2368 if (!empty($userWeightData))
2372 '@ID' => array_keys($userWeightData),
2374 'select' => [
'ID',
'NAME',
'LAST_NAME',
'SECOND_NAME',
'LOGIN',
'PERSONAL_PHOTO',
'PERSONAL_GENDER'],
2377 while ($userFields =
$res->fetch())
2379 $userData[$userFields[
"ID"]] = [
2380 'NAME_FORMATTED' => \CUser::formatName(
2381 \CSite::getNameFormat(
false),
2385 'PERSONAL_PHOTO' => [
2386 'ID' => $userFields[
'PERSONAL_PHOTO'],
2389 'PERSONAL_GENDER' => $userFields[
'PERSONAL_GENDER'],
2392 if ((
int)$userFields[
'PERSONAL_PHOTO'] > 0)
2394 $imageFile = \CFile::getFileArray($userFields[
"PERSONAL_PHOTO"]);
2395 if ($imageFile !==
false)
2397 $file = \CFile::resizeImageGet(
2399 [
"width" => $avatarSize,
"height" => $avatarSize],
2403 $userData[$userFields[
"ID"]][
'PERSONAL_PHOTO'][
'SRC'] = $file[
'src'];
2409 foreach ($entityUserData as
$entityId => $userIdList)
2413 foreach ($userIdList as
$userId)
2417 'NAME_FORMATTED' => $userData[
$userId][
'NAME_FORMATTED'],
2418 'PERSONAL_PHOTO' => $userData[
$userId][
'PERSONAL_PHOTO'][
'ID'],
2419 'PERSONAL_PHOTO_SRC' => $userData[
$userId][
'PERSONAL_PHOTO'][
'SRC'],
2420 'PERSONAL_GENDER' => $userData[
$userId][
'PERSONAL_GENDER'],
2421 'WEIGHT' => $userWeightData[
$userId],
2431 if (
$a[
'WEIGHT'] == $b[
'WEIGHT'])
2435 return (
$a[
'WEIGHT'] > $b[
'WEIGHT']) ? -1 : 1;
2449 isset(
$params[
'ENTITY_TYPE_ID'])
2450 &&
$params[
'ENTITY_TYPE_ID'] <>
''
2456 && (int)
$params[
'ENTITY_ID'] > 0
2468 $DB->query(
"DELETE FROM b_rating_vote WHERE ENTITY_TYPE_ID='" .
$DB->forSql($entityTypeId) .
"' AND ENTITY_ID=" .
$entityId,
true);
2469 $DB->query(
"DELETE FROM b_rating_voting WHERE ENTITY_TYPE_ID='" .
$DB->forSql($entityTypeId) .
"' AND ENTITY_ID=" .
$entityId,
true);
2470 $DB->query(
"DELETE FROM b_rating_voting_reaction WHERE ENTITY_TYPE_ID='" .
$DB->forSql($entityTypeId) .
"' AND ENTITY_ID=" .
$entityId,
true);
2483 $arRating = $resRating->Fetch();
2484 if ($arRating && $arRating[
'ACTIVE'] ==
'Y')
2486 $DB->Query(
"UPDATE b_rating SET CALCULATED = 'C' WHERE id = " .
$ID);
2489 $sqlFunc = ($arRating[
'CALCULATION_METHOD'] ==
'SUM') ?
'SUM' :
'AVG';
2491 INSERT INTO b_rating_results
2492 (RATING_ID, ENTITY_TYPE_ID, ENTITY_ID, CURRENT_VALUE, PREVIOUS_VALUE)
2494 " .
$ID .
" RATING_ID,
2495 '" . $arRating[
'ENTITY_ID'] .
"' ENTITY_TYPE_ID,
2497 " . $sqlFunc .
"(RC.CURRENT_VALUE) CURRENT_VALUE,
2500 b_rating_component_results RC LEFT JOIN b_rating_results RR ON RR.RATING_ID = RC.RATING_ID and RR.ENTITY_ID = RC.ENTITY_ID
2502 RC.RATING_ID = " .
$ID .
" and RR.ID IS NULL
2503 GROUP BY RC.ENTITY_ID";
2504 $DB->Query($strSql);
2507 $strSql = $helper->prepareCorrelatedUpdate(
"b_rating_results",
"RR", [
2508 'PREVIOUS_VALUE' =>
'case when RR.CURRENT_VALUE = RCR.CURRENT_VALUE then RR.PREVIOUS_VALUE else RR.CURRENT_VALUE end',
2509 'CURRENT_VALUE' =>
'RCR.CURRENT_VALUE',
2511 (SELECT '" . $arRating[
'ENTITY_ID'] .
"' ENTITY_TYPE_ID, RC.ENTITY_ID, " . $sqlFunc .
"(RC.CURRENT_VALUE) CURRENT_VALUE
2512 FROM b_rating_component_results RC INNER JOIN b_rating_results RR on RR.RATING_ID = RC.RATING_ID and RR.ENTITY_ID = RC.ENTITY_ID
2513 WHERE RC.RATING_ID = " .
$ID .
"
2514 GROUP BY RC.ENTITY_ID
2517 RR.RATING_ID=" .
$ID .
"
2518 and RR.ENTITY_TYPE_ID = RCR.ENTITY_TYPE_ID
2519 and RR.ENTITY_ID = RCR.ENTITY_ID
2522 $DB->Query($strSql);
2525 if ($arRating[
'POSITION'] ==
'Y')
2527 $strSql = $helper->initRowNumber(
'nPos');
2530 $DB->Query($strSql);
2532 $strSql = $helper->prepareCorrelatedUpdate(
"b_rating_results",
"RR", [
2533 'PREVIOUS_POSITION' =>
'case when RR.CURRENT_POSITION = RP.POSITION then RR.PREVIOUS_POSITION else RR.CURRENT_POSITION end',
2534 'CURRENT_POSITION' =>
'RP.POSITION',
2536 (SELECT ENTITY_TYPE_ID, ENTITY_ID, CURRENT_VALUE, " . $helper->getRowNumber(
'nPos') .
" as POSITION
2537 FROM b_rating_results
2538 WHERE RATING_ID = " .
$ID .
"
2539 ORDER BY CURRENT_VALUE DESC
2542 RR.RATING_ID=" .
$ID .
"
2543 and RR.ENTITY_TYPE_ID = RP.ENTITY_TYPE_ID
2544 and RR.ENTITY_ID = RP.ENTITY_ID
2547 $DB->Query($strSql);
2552 INSERT INTO b_rating_user
2553 (RATING_ID, ENTITY_ID)
2555 " .
$ID .
" RATING_ID,
2559 LEFT JOIN b_rating_user RU ON RU.RATING_ID = " .
$ID .
" and RU.ENTITY_ID = U.ID
2562 AND (CASE WHEN U.EXTERNAL_AUTH_ID IN ('" . join(
"', '", \
Bitrix\Main\UserTable::getExternalUserTypes()) .
"') THEN 'Y' ELSE 'N' END) = 'N'
2563 AND RU.ID IS NULL ";
2564 $DB->Query($strSql);
2566 if ($arRating[
'AUTHORITY'] ==
'Y')
2568 $sRatingAssignType = COption::GetOptionString(
"main",
"rating_assign_type",
"manual");
2569 if ($sRatingAssignType ==
'auto')
2572 $assignRatingGroup = COption::GetOptionString(
"main",
"rating_assign_rating_group", 0);
2573 $assignRatingValueAdd = COption::GetOptionString(
"main",
"rating_assign_rating_group_add", 1);
2574 $assignRatingValueDelete = COption::GetOptionString(
"main",
"rating_assign_rating_group_delete", 1);
2579 $assignAuthorityGroup = COption::GetOptionString(
"main",
"rating_assign_authority_group", 0);
2580 $assignAuthorityValueAdd = COption::GetOptionString(
"main",
"rating_assign_authority_group_add", 2);
2581 $assignAuthorityValueDelete = COption::GetOptionString(
"main",
"rating_assign_authority_group_delete", 2);
2586 $sRatingWeightType = COption::GetOptionString(
"main",
"rating_weight_type",
"auto");
2587 if ($sRatingWeightType ==
'auto')
2590 $communitySize = $arCI[
'COMMUNITY_SIZE'];
2591 $communityAuthority = $arCI[
'COMMUNITY_AUTHORITY'];
2593 $sRatingNormalizationType = COption::GetOptionString(
"main",
"rating_normalization_type",
"auto");
2594 if ($sRatingNormalizationType ==
'manual')
2596 $ratingNormalization = COption::GetOptionString(
"main",
"rating_normalization", 1000);
2600 if ($communitySize <= 10)
2602 $ratingNormalization = 10;
2604 elseif ($communitySize <= 1000)
2606 $ratingNormalization = 100;
2610 $ratingNormalization = 1000;
2612 COption::SetOptionString(
"main",
"rating_normalization", $ratingNormalization);
2616 if ($communitySize > 0)
2618 $voteWeight = $ratingNormalization / $communitySize;
2621 COption::SetOptionString(
"main",
"rating_community_size", $communitySize);
2622 COption::SetOptionString(
"main",
"rating_community_authority", $communityAuthority);
2623 COption::SetOptionString(
"main",
"rating_vote_weight", $voteWeight);
2625 $ratingCountVote = COption::GetOptionString(
"main",
"rating_count_vote", 10);
2626 $strSql =
"UPDATE b_rating_user SET VOTE_COUNT = 0, VOTE_WEIGHT =0 WHERE RATING_ID=" .
$ID;
2627 $DB->Query($strSql);
2629 $strSql = $helper->prepareCorrelatedUpdate(
"b_rating_user",
"RU", [
2630 'VOTE_COUNT' => intval($ratingCountVote) .
" + RP.CURRENT_VALUE",
2631 'VOTE_WEIGHT' =>
"RP.CURRENT_VALUE * " . $voteWeight,
2633 (SELECT ENTITY_ID, CURRENT_VALUE
2634 FROM b_rating_results
2635 WHERE RATING_ID = " .
$ID .
"
2638 RU.RATING_ID=" .
$ID .
"
2639 and RU.ENTITY_ID = RP.ENTITY_ID
2642 $DB->Query($strSql);
2648 $strSql =
"UPDATE b_rating_user SET VOTE_COUNT = 0, VOTE_WEIGHT =0 WHERE RATING_ID=" .
$ID;
2649 $DB->Query($strSql);
2651 $strSql = $helper->prepareCorrelatedUpdate(
"b_rating_user",
"RU", [
2652 'VOTE_COUNT' =>
'RP.COUNT',
2653 'VOTE_WEIGHT' =>
'RP.WEIGHT',
2656 RW.RATING_FROM, RW.RATING_TO, RW.WEIGHT, RW.COUNT, RR.ENTITY_ID
2661 RR.RATING_ID = " .
$ID .
"
2662 and RR.CURRENT_VALUE BETWEEN RW.RATING_FROM AND RW.RATING_TO
2665 RU.RATING_ID=" .
$ID .
"
2666 and RU.ENTITY_ID = RP.ENTITY_ID
2669 $DB->Query($strSql);
2675 $DB->Query(
"UPDATE b_rating SET CALCULATED = 'Y', LAST_CALCULATED = " .
$DB->GetNowFunction() .
" WHERE id = " .
$ID);
2688 $strSql = $helper->prepareCorrelatedUpdate(
"b_rating_voting_reaction",
"RVR", [
2689 'TOTAL_VOTES' =>
'RP.TOTAL_POSITIVE_VOTES',
2694 SUM(case when VALUE > 0 AND USER_ID <> $ID then 1 else 0 end) as TOTAL_POSITIVE_VOTES
2696 WHERE RATING_VOTING_ID IN (
2697 SELECT DISTINCT RV0.RATING_VOTING_ID FROM b_rating_vote RV0 WHERE RV0.USER_ID=$ID
2699 GROUP BY RATING_VOTING_ID, ENTITY_TYPE_ID, ENTITY_ID
2702 RVR.ENTITY_TYPE_ID = RP.ENTITY_TYPE_ID
2703 AND RVR.ENTITY_ID = RP.ENTITY_ID
2708 $strSql = $helper->prepareCorrelatedUpdate(
"b_rating_voting",
"RV", [
2709 'TOTAL_VALUE' =>
'RP.TOTAL_VALUE',
2710 'TOTAL_VOTES' =>
'RP.TOTAL_VOTES',
2711 'TOTAL_POSITIVE_VOTES' =>
'RP.TOTAL_POSITIVE_VOTES',
2712 'TOTAL_NEGATIVE_VOTES' =>
'RP.TOTAL_NEGATIVE_VOTES',
2716 SUM(case when USER_ID <> $ID then VALUE else 0 end) as TOTAL_VALUE,
2717 SUM(case when USER_ID <> $ID then 1 else 0 end) as TOTAL_VOTES,
2718 SUM(case when VALUE > 0 AND USER_ID <> $ID then 1 else 0 end) as TOTAL_POSITIVE_VOTES,
2719 SUM(case when VALUE < 0 AND USER_ID <> $ID then 1 else 0 end) as TOTAL_NEGATIVE_VOTES
2721 WHERE RATING_VOTING_ID IN (
2722 SELECT DISTINCT RV0.RATING_VOTING_ID FROM b_rating_vote RV0 WHERE RV0.USER_ID=$ID
2724 GROUP BY RATING_VOTING_ID
2727 RV.ID = RP.RATING_VOTING_ID
2733 $connection->query(
"DELETE FROM b_rating_vote WHERE USER_ID = $ID");
2734 $connection->query(
"DELETE FROM b_rating_user WHERE ENTITY_ID = $ID");
2747 INSERT INTO b_rating_results
2748 (RATING_ID, ENTITY_TYPE_ID, ENTITY_ID, CURRENT_VALUE, PREVIOUS_VALUE)
2751 $maxValuesLen = 2048;
2757 if (mb_strlen($strSqlValues) > $maxValuesLen)
2759 $DB->Query($strSqlPrefix . mb_substr($strSqlValues, 2));
2763 if ($strSqlValues <>
'')
2765 $DB->Query($strSqlPrefix . mb_substr($strSqlValues, 2));
2776 if (!is_array($arComponentConfigs))
2782 UPDATE b_rating_component
2783 SET LAST_CALCULATED = " .
$DB->GetNowFunction() .
",
2784 NEXT_CALCULATION = '" . date(
'Y-m-d H:i:s', time() + $arComponentConfigs[
'REFRESH_INTERVAL']) .
"'
2785 WHERE RATING_ID = " . intval($arComponentConfigs[
'RATING_ID']) .
" AND COMPLEX_NAME = '" .
$DB->ForSql($arComponentConfigs[
'COMPLEX_NAME']) .
"'";
2786 $DB->Query($strSql);
2793 global
$DB, $stackCacheManager;
2795 $ratingId = intval($ratingId);
2797 $DB->Query(
"UPDATE b_rating SET AUTHORITY = CASE WHEN ID <> $ratingId THEN 'N' ELSE 'Y' END");
2799 COption::SetOptionString(
"main",
"rating_authority_rating", $ratingId);
2801 $stackCacheManager->Clear(
"b_rating");
2812 $bAllGroups =
false;
2814 $communityLastVisit = COption::GetOptionString(
"main",
"rating_community_last_visit",
'90');
2816 while ($arVoteGroup =
$res->Fetch())
2818 if ($arVoteGroup[
'GROUP_ID'] == 2)
2826 $strModulesSql =
'';
2830 SELECT USER_START_ID as ENTITY_ID
2832 WHERE START_DATE > " . $helper->addDaysToDateTime(-intval($communityLastVisit)) .
"
2833 GROUP BY USER_START_ID
2835 SELECT AUTHOR_ID as ENTITY_ID
2836 FROM b_forum_message
2837 WHERE POST_DATE > " . $helper->addDaysToDateTime(-intval($communityLastVisit)) .
"
2845 SELECT AUTHOR_ID as ENTITY_ID
2847 WHERE DATE_PUBLISH > " . $helper->addDaysToDateTime(-intval($communityLastVisit)) .
"
2850 SELECT AUTHOR_ID as ENTITY_ID
2852 WHERE DATE_CREATE > " . $helper->addDaysToDateTime(-intval($communityLastVisit)) .
"
2858 $ratingId = COption::GetOptionString(
"main",
"rating_authority_rating", 0);
2861 FROM b_rating_subordinate
2862 WHERE RATING_ID = $ratingId
2865 if (!empty($strModulesSql))
2869 " . $strModulesSql .
"
2870 SELECT USER_ID as ENTITY_ID
2872 WHERE CREATED > " . $helper->addDaysToDateTime(-intval($communityLastVisit)) .
"
2879 $DB->Query(
"DELETE FROM b_rating_prepare");
2885 INSERT INTO b_rating_prepare (ID)
2886 SELECT DISTINCT U.ID
2887 FROM " . $strModulesSql .
"
2889 WHERE " . (!empty($strModulesSql) ?
"U.ID = MS.ENTITY_ID AND" :
"") .
"
2891 AND (CASE WHEN U.EXTERNAL_AUTH_ID IN ('" . join(
"', '", \
Bitrix\Main\UserTable::getExternalUserTypes()) .
"') THEN 'Y' ELSE 'N' END) = 'N'
2892 AND U.LAST_LOGIN >" . $helper->addDaysToDateTime(-intval($communityLastVisit)) .
"
2898 INSERT INTO b_rating_prepare (ID)
2899 SELECT DISTINCT U.ID
2900 FROM " . $strModulesSql .
"
2902 WHERE " . (!empty($strModulesSql) ?
"U.ID = MS.ENTITY_ID AND" :
"") .
"
2904 AND (CASE WHEN U.EXTERNAL_AUTH_ID IN ('" . join(
"', '", \
Bitrix\Main\UserTable::getExternalUserTypes()) .
"') THEN 'Y' ELSE 'N' END) = 'N'
2905 AND U.LAST_LOGIN > " . $helper->addDaysToDateTime(-intval($communityLastVisit)) .
"
2908 $DB->Query($strSql);
2910 $strSql =
'SELECT COUNT(*) as COMMUNITY_SIZE, SUM(CURRENT_VALUE) COMMUNITY_AUTHORITY
2911 FROM b_rating_results RC LEFT JOIN b_rating_prepare TT ON RC.ENTITY_ID = TT.ID
2912 WHERE RATING_ID = ' . intval($ratingId) .
' AND TT.ID IS NOT NULL';
2915 return $res->Fetch();
2923 isset($arVoteParam[
'CURRENT_USER_ID'])
2924 && (
int)$arVoteParam[
'CURRENT_USER_ID'] > 0
2927 $userId = (int)$arVoteParam[
'CURRENT_USER_ID'];
2933 $bUserAuth = (bool)
$USER?->IsAuthorized();
2942 $bSelfVote = COption::GetOptionString(
"main",
"rating_self_vote",
'N');
2943 if ($bSelfVote ==
'N' && intval($arVoteParam[
'OWNER_ID']) ==
$userId)
2947 'ERROR_TYPE' =>
'SELF',
2948 'ERROR_MSG' =>
GetMessage(
'RATING_ALLOW_VOTE_SELF'),
2955 'ERROR_TYPE' =>
'GUEST',
2956 'ERROR_MSG' =>
GetMessage(
'RATING_ALLOW_VOTE_GUEST'),
2961 static $cacheAllowVote = [];
2962 static $cacheUserVote = [];
2963 static $cacheVoteSize = 0;
2964 if (!array_key_exists(
$userId, $cacheAllowVote))
2970 $sVoteType = $arVoteParam[
'ENTITY_TYPE_ID'] ==
'USER' ?
'A' :
'R';
2972 $userVoteGroup = [];
2974 foreach (
$ar as $group)
2976 if ($sVoteType == $group[
'TYPE'])
2978 $userVoteGroup[] = $group[
'GROUP_ID'];
2982 $userGroup =
$USER->GetUserGroupArray();
2984 $result = array_intersect($userGroup, $userVoteGroup);
2987 $arInfo = $cacheAllowVote[
$userId] = [
2989 'ERROR_TYPE' =>
'ACCESS',
2990 'ERROR_MSG' =>
GetMessage(
'RATING_ALLOW_VOTE_ACCESS'),
2997 $arAuthorityUserProp[
'VOTE_WEIGHT'] < 0
2999 $arAuthorityUserProp[
'VOTE_WEIGHT'] == 0
3004 $arInfo = $cacheAllowVote[
$userId] = [
3006 'ERROR_TYPE' =>
'ACCESS',
3007 'ERROR_MSG' =>
GetMessage(
'RATING_ALLOW_VOTE_LOW_WEIGHT'),
3011 if ($arInfo[
'RESULT'] && $sVoteType ==
'A')
3014 SELECT COUNT(*) as VOTE
3015 FROM b_rating_vote RV
3016 WHERE RV.USER_ID = ' .
$userId .
'
3017 AND RV.CREATED > ' . $helper->addDaysToDateTime(-1);
3019 $countVote =
$res->Fetch();
3023 if ($cacheVoteSize >= $cacheUserVote[
$userId])
3025 $arInfo = $cacheAllowVote[
$userId] = [
3027 'ERROR_TYPE' =>
'COUNT_VOTE',
3028 'ERROR_MSG' =>
GetMessage(
'RATING_ALLOW_VOTE_COUNT_VOTE'),
3035 if ($cacheAllowVote[
$userId][
'RESULT'])
3037 if ($cacheVoteSize >= $cacheUserVote[
$userId])
3041 'ERROR_TYPE' =>
'COUNT_VOTE',
3042 'ERROR_MSG' =>
GetMessage(
'RATING_ALLOW_VOTE_COUNT_VOTE'),
3046 $arInfo = $cacheAllowVote[
$userId];
3051 if (!isset($handlers))
3056 foreach ($handlers as $arEvent)
3059 if (is_array($arEventResult) && isset($arEventResult[
'RESULT']) && $arEventResult[
'RESULT'] ===
false
3060 && isset($arEventResult[
'ERROR_TYPE']) && $arEventResult[
'ERROR_TYPE'] <>
''
3061 && isset($arEventResult[
'ERROR_MSG']) && $arEventResult[
'ERROR_MSG'] <>
'')
3065 'ERROR_TYPE' => $arEventResult[
'ERROR_TYPE'],
3066 'ERROR_MSG' => $arEventResult[
'ERROR_MSG'],
3079 while ($arRatingsTmp = $rsRatings->GetNext())
3081 $arRatingList[] = $arRatingsTmp[
'ID'];
3086 $ratingStartValue = 0;
3087 if (isset(
$arParams[
'DEFAULT_CONFIG_NEW_USER']) &&
$arParams[
'DEFAULT_CONFIG_NEW_USER'] ==
'Y')
3089 $ratingStartValue = COption::GetOptionString(
"main",
"rating_start_authority", 3);
3092 $strSql =
"UPDATE b_rating_user SET BONUS = $ratingStartValue WHERE RATING_ID IN (" . implode(
',', $arRatingList) .
")";
3093 $DB->Query($strSql);
3098 TO_USER_ID as ENTITY_ID, COUNT(*) as CNT
3100 b_forum_user_points FUP
3104 RU.BONUS = " .
$DB->IsNull(
'RP.CNT',
'0') .
"+" . $ratingStartValue .
"
3106 RU.RATING_ID IN (" . implode(
',', $arRatingList) .
")
3107 and RU.ENTITY_ID = RP.ENTITY_ID
3109 $DB->Query($strSql);
3113 if (isset(
$arParams[
'DEFAULT_CONFIG_NEW_USER']) &&
$arParams[
'DEFAULT_CONFIG_NEW_USER'] ==
'Y' && !empty($arRatingList))
3115 $ratingStartValue = COption::GetOptionString(
"main",
"rating_start_authority", 3);
3116 $strSql =
"UPDATE b_rating_user SET BONUS = " . $ratingStartValue .
" WHERE RATING_ID IN (" . implode(
',', $arRatingList) .
")";
3117 $DB->Query($strSql);
3124 public static function AutoAssignGroup($groupId, $authorityValueAdd, $authorityValueDelete)
3128 $groupId = intval($groupId);
3135 $ratingValueAdd = intval($authorityValueAdd);
3136 $ratingValueDelete = intval($authorityValueDelete);
3137 $sRatingWeightType = COption::GetOptionString(
"main",
"rating_weight_type",
"auto");
3138 if ($sRatingWeightType ==
'auto')
3140 $ratingValueAdd = $ratingValueAdd * COption::GetOptionString(
"main",
"rating_vote_weight", 1);
3141 $ratingValueDelete = $ratingValueDelete * COption::GetOptionString(
"main",
"rating_vote_weight", 1);
3147 FROM b_rating_results rr
3148 WHERE rr.RATING_ID = $ratingId
3149 AND rr.CURRENT_VALUE < $ratingValueDelete
3150 GROUP BY rr.ENTITY_ID
3153 while ($row =
$res->Fetch())
3155 CUser::RemoveUserGroup($row[
'ENTITY_ID'], [$groupId]);
3161 FROM b_rating_results rr
3162 LEFT JOIN b_user_group ug ON ug.GROUP_ID = $groupId AND ug.USER_ID = rr.ENTITY_ID
3163 WHERE rr.RATING_ID = $ratingId
3164 and rr.CURRENT_VALUE >= $ratingValueAdd
3165 and ug.USER_ID IS NULL
3166 GROUP BY rr.ENTITY_ID
3169 while ($row =
$res->Fetch())
3171 CUser::AppendUserGroup($row[
'ENTITY_ID'], [$groupId]);
3181 $externalAuthTypes = array_diff(\
Bitrix\Main\UserTable::getExternalUserTypes(), [
'email',
'replica']);
3183 $reactionField = $includeReaction ?
"RV.REACTION," :
"";
3185 $groupBy =
"U.ID, U.NAME, U.LAST_NAME, U.SECOND_NAME, U.LOGIN, U.PERSONAL_PHOTO, RV.VALUE, RV.USER_ID";
3186 if ($includeReaction)
3188 $groupBy .=
", RV.REACTION";
3200 RV.VALUE AS VOTE_VALUE,
3202 SUM(case when RV0.ID is not null then 1 else 0 end) " .
$DB->quote(
"RANK") .
",
3205 b_rating_vote RV LEFT JOIN b_rating_vote RV0 ON RV0.USER_ID = " . intval(
$USER->GetId()) .
" and RV0.OWNER_ID = RV.USER_ID
3206 INNER JOIN b_user U ON RV.USER_ID = U.ID
3208 (CASE WHEN U.EXTERNAL_AUTH_ID IN ('" . join(
"', '", $externalAuthTypes) .
"') THEN 'Y' ELSE 'N' END) = 'N'
3209 AND RV.ENTITY_TYPE_ID = '" .
$DB->ForSql($arParam[
'ENTITY_TYPE_ID']) .
"'
3210 and RV.ENTITY_ID = " . intval($arParam[
'ENTITY_ID']) .
"
3211 " . self::getReactionFilterSQL($arParam, $bplus) .
"
3213 ORDER BY " . ($bIntranetInstalled ?
"RV.VALUE DESC, " .
$DB->quote(
"RANK") .
" DESC, RV_ID DESC" :
$DB->quote(
"RANK") .
" DESC, RV.VALUE DESC, RV_ID DESC");
3220 $externalAuthTypes = array_diff(\
Bitrix\Main\UserTable::getExternalUserTypes(), [
'email',
'replica']);
3222 $reactionField = $includeReaction ?
"RV.REACTION," :
"";
3224 $groupBy =
"U.ID, RV.VALUE, RV.USER_ID";
3225 if ($includeReaction)
3227 $groupBy .=
", RV.REACTION";
3233 RV.VALUE AS VOTE_VALUE,
3236 SUM(case when RV0.ID is not null then 1 else 0 end) " .
$DB->quote(
"RANK") .
",
3240 LEFT JOIN b_rating_vote RV0 ON RV0.USER_ID = " . intval(
$USER->GetId()) .
" and RV0.OWNER_ID = RV.USER_ID
3241 INNER JOIN b_user U ON RV.USER_ID = U.ID
3243 (CASE WHEN U.EXTERNAL_AUTH_ID IN ('" . join(
"', '", $externalAuthTypes) .
"') THEN 'Y' ELSE 'N' END) = 'N'
3244 AND RV.ENTITY_TYPE_ID = '" .
$DB->ForSql($arParam[
'ENTITY_TYPE_ID']) .
"'
3245 and RV.ENTITY_ID = " . intval($arParam[
'ENTITY_ID']) .
"
3246 " . self::getReactionFilterSQL($arParam, $bplus) .
"
3248 ORDER BY " . ($bIntranetInstalled ?
"RV.VALUE DESC, " .
$DB->quote(
"RANK") .
" DESC, RV_ID DESC" :
$DB->quote(
"RANK") .
" DESC, RV.VALUE DESC, RV_ID DESC");
3251 private static function getReactionFilterSQL($arParam, $bplus)
3257 && !empty($arParam[
"REACTION"])
3259 $arParam[
"REACTION"] == self::REACTION_DEFAULT
3260 ?
" and (RV.REACTION IS NULL OR RV.REACTION = '" .
$DB->ForSql($arParam[
"REACTION"]) .
"') "
3261 :
" and RV.REACTION = '" .
$DB->ForSql($arParam[
"REACTION"]) .
"' "
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
static getConnection($name="")
static isModuleInstalled($moduleName)
static getList(array $parameters=array())
static GetRatingVoteListSQL($arParam, $bplus, $bIntranetInstalled, $includeReaction=false)
static SetWeight($arConfigs)
static __UpdateComponents($ID, $arFields)
static SetAuthorityDefaultValue($arParams)
static OnAfterUserRegister($arFields)
static AddResults($arResults)
static CheckAllowVote($arVoteParam)
static SetVoteGroup($arGroupId, $type)
static AddRatingVote($arParam)
static GetRatingConfigs($objectType=null, $withRatingType=true)
static getEntityRatingData($params=[])
static GetRatingEntityTypes($objectType=null)
static GetAuthorityRating()
static SetAuthorityRating($ratingId)
static GetList($arSort=[], $arFilter=[])
static GetRatingObjects()
static getRatingVoteList($arParam)
static getRatingVoteReaction($arParam)
static GetRatingUserProp($ratingId, $entityId)
static UpdateRatingUserBonus($arParam)
static GetWeightList($arSort=[], $arFilter=[])
static __AddComponents($ID, $arFields)
static GetVoteGroupEx($type='')
static GetRatingVoteListSQLExtended($arParam, $bplus, $bIntranetInstalled, $includeReaction=false)
static getUserWeight($userId=0)
static ChangeRatingVote($arParam)
static getUserData($userId=0, $value=0)
static GetRatingVoteResultCache($entityTypeId, $entityId, $user_id=0)
static __CheckFields($arFields)
static GetRatingResult($ID, $entityId)
static GetVoteGroup($type='')
static AddComponentResults($arComponentConfigs)
static deleteRatingVoting(array $params=[])
static Calculate($ID, $bForceRecalc=false)
static GetRatingVoteResult($entityTypeId, $entityId, $user_id=0)
static GetRatingUserPropEx($ratingId, $entityId)
static Update($ID, $arFields)
static __SortWeight($a, $b)
static GetCommunityInfo($ratingId)
static GetRatingValueInfo($ratingId)
static CancelRatingVote($arParam)
static AutoAssignGroup($groupId, $authorityValueAdd, $authorityValueDelete)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
const BX_RESIZE_IMAGE_EXACT
ExecuteModuleEventEx($arEvent, $arParams=[])
IsModuleInstalled($module_id)
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
GetMessage($name, $aReplace=null)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
$GLOBALS['_____370096793']