• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/ratings.php
  • Класс: CAllRatings
  • Вызов: CAllRatings::getEntityRatingData
static function getEntityRatingData($params = array())
{
	global $USER;
	$connection = BitrixMainApplication::getConnection();
	$helper = $connection->getSqlHelper();

	$result = array();

	$entityTypeId = (
		!empty($params['entityTypeId'])
			? $params['entityTypeId']
			: ''
	);

	$entityIdList = (
		!empty($params['entityId'])
			? $params['entityId']
			: array()
	);

	if (!is_array($entityIdList))
	{
		$entityIdList = array($entityIdList);
	}

	if (empty($entityIdList))
	{
		return $result;
	}

	$ratingId = CRatings::getAuthorityRating();
	if ((int)$ratingId <= 0)
	{
		return $result;
	}

	$topCount = (
		isset($params['topCount'])
			? (int)$params['topCount']
			: 0
	);

	if ($topCount <= 0)
	{
		$topCount = 2;
	}

	if ($topCount > 5)
	{
		$topCount = 5;
	}

	$avatarSize = (
		isset($params['avatarSize'])
			? (int)$params['avatarSize']
			: 100
	);

	if (BitrixMainModuleManager::isModuleInstalled('intranet'))
	{
		$res = $connection->query("
			SELECT
				RS1.ENTITY_ID as USER_ID,
				RV1.ENTITY_ID as ENTITY_ID,
				MAX(RS1.VOTES) as WEIGHT
			FROM
				b_rating_subordinate RS1,
				b_rating_vote RV1
			WHERE
				RS1.ENTITY_ID = RV1.USER_ID
				AND RS1.RATING_ID = ".(int)$ratingId."
				AND RV1.ENTITY_TYPE_ID = '".$helper->forSQL($entityTypeId)."'
				AND RV1.ENTITY_ID IN (".implode(',', $entityIdList).")
			GROUP BY
				RV1.ENTITY_ID, RS1.ENTITY_ID
			ORDER BY
				RV1.ENTITY_ID,
				WEIGHT DESC
		");
	}
	else
	{
		$res = $connection->query("
			SELECT
				RV1.USER_ID as USER_ID,
				RV1.ENTITY_ID as ENTITY_ID,
				RV1.VALUE as WEIGHT
			FROM
				b_rating_vote RV1
			WHERE
				RV1.ENTITY_TYPE_ID = '".$helper->forSQL($entityTypeId)."'
				AND RV1.ENTITY_ID IN (".implode(',', $entityIdList).")
			ORDER BY
				RV1.ENTITY_ID,
				WEIGHT DESC
		");
	}

	$userWeightData = $entityUserData = array();

	$currentEntityId = false;
	$hasMine = false;

	while ($voteFields = $res->fetch())
	{
		if (
			!$hasMine
			&& $voteFields['USER_ID'] == $USER->getId()
		)
		{
			$hasMine = true;
		}

		if ($voteFields['ENTITY_ID'] != $currentEntityId)
		{
			$cnt = 0;
			$hasMine = false;
			$entityUserData[$voteFields['ENTITY_ID']] = array();
		}

		$currentEntityId = $voteFields['ENTITY_ID'];
		$cnt++;

		if ($cnt > ($hasMine ? $topCount+1 : $topCount))
		{
			continue;
		}

		$entityUserData[$voteFields['ENTITY_ID']][] = $voteFields['USER_ID'];
		if (!isset($userWeightData[$voteFields['USER_ID']]))
		{
			$userWeightData[$voteFields['USER_ID']] = (float)$voteFields['WEIGHT'];
		}
	}

	$userData = array();

	if (!empty($userWeightData))
	{
		$res = BitrixMainUserTable::getList(array(
			'filter' => array(
				'@ID' => array_keys($userWeightData)
			),
			'select' => array('ID', 'NAME', 'LAST_NAME', 'SECOND_NAME', 'LOGIN', 'PERSONAL_PHOTO', 'PERSONAL_GENDER')
		));

		while ($userFields = $res->fetch())
		{
			$userData[$userFields["ID"]] = array(
				'NAME_FORMATTED' => CUser::formatName(
					CSite::getNameFormat(false),
					$userFields,
					true
				),
				'PERSONAL_PHOTO' => array(
					'ID' => $userFields['PERSONAL_PHOTO'],
					'SRC' => false
				),
				'PERSONAL_GENDER' => $userFields['PERSONAL_GENDER']
			);

			if ((int)$userFields['PERSONAL_PHOTO'] > 0)
			{
				$imageFile = CFile::getFileArray($userFields["PERSONAL_PHOTO"]);
				if ($imageFile !== false)
				{
					$file = CFile::resizeImageGet(
						$imageFile,
						array("width" => $avatarSize, "height" => $avatarSize),
						BX_RESIZE_IMAGE_EXACT,
						false
					);
					$userData[$userFields["ID"]]['PERSONAL_PHOTO']['SRC'] = $file['src'];
				}
			}
		}
	}

	foreach($entityUserData as $entityId => $userIdList)
	{
		$result[$entityId] = array();

		foreach($userIdList as $userId)
		{
			$result[$entityId][] = array(
				'ID' => $userId,
				'NAME_FORMATTED' => $userData[$userId]['NAME_FORMATTED'],
				'PERSONAL_PHOTO' => $userData[$userId]['PERSONAL_PHOTO']['ID'],
				'PERSONAL_PHOTO_SRC' => $userData[$userId]['PERSONAL_PHOTO']['SRC'],
				'PERSONAL_GENDER' => $userData[$userId]['PERSONAL_GENDER'],
				'WEIGHT' => $userWeightData[$userId]
			);
		}
	}

	foreach($result as $entityId => $data)
	{
		usort(
			$data,
			function($a, $b)
			{
				if ($a['WEIGHT'] == $b['WEIGHT'])
				{
					return 0;
				}
				return ($a['WEIGHT'] > $b['WEIGHT']) ? -1 : 1;
			}
		);
		$result[$entityId] = $data;
	}

	return $result;
}