Esempio n. 1
0
 /**
  * <p>Метод добавляет голос к рейтинговому голосованию. Динамичный метод.</p>
  *
  *
  * @param array $Param  Массив значений параметров. В качестве ключей данного массива
  * допустимо использовать: <ul> <li> <b>ENTITY_TYPE_ID</b> – объект голосования.
  * Например: FORUM_TOPIC для тем форума, FORUM_POST для сообщений форума, BLOG_POST
  * для постов в блоге, BLOG_COMMENT для комментариев в блоге. Сам список
  * кодов не регламентирован, можно использовать любой удобный.</li> <li>
  * <b>ENTITY_ID</b> – идентификатор объекта голосования</li> <li> <b>VALUE</b> –
  * выставленная оценка</li> <li> <b>USER_ID</b> – идентификатор
  * пользователя</li> <li> <b>USER_IP</b> – IP-адрес пользователя</li> </ul> Все поля
  * являются обязательными.
  *
  * @return bool 
  *
  * <h4>Example</h4> 
  * <pre>
  * &lt;?
  * // добавляем теме отрицательную оценку.
  * $arAddVote = array(
  * 	"ENTITY_TYPE_ID"  =&gt;  "FORUM_TOPIC",
  * 	"ENTITY_ID"       =&gt;  38,
  * 	"VALUE"           =&gt;  -1,
  * 	"USER_ID"         =&gt;  1, 
  *  	"USER_IP"         =&gt;  '127.0.0.1',
  * );
  * CRatings::AddRatingVote($arAddVote);
  * ?&gt;
  * </pre>
  *
  *
  * @static
  * @link http://dev.1c-bitrix.ru/api_help/main/reference/cratings/addratingvote.php
  * @author Bitrix
  */
 public static function AddRatingVote($arParam)
 {
     global $DB, $CACHE_MANAGER;
     if (isset($_SESSION['RATING_VOTE_COUNT']) && $arParam['ENTITY_TYPE_ID'] == 'USER') {
         if ($_SESSION['RATING_VOTE_COUNT'] >= $_SESSION['RATING_USER_VOTE_COUNT']) {
             return false;
         } else {
             $_SESSION['RATING_VOTE_COUNT']++;
         }
     }
     $arParam['ENTITY_TYPE_ID'] = substr($arParam['ENTITY_TYPE_ID'], 0, 50);
     CRatings::CancelRatingVote($arParam);
     $err_mess = CRatings::err_mess() . "<br>Function: AddRatingVote<br>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 ? '+' : '') . floatval($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'] = IntVal($result);
         }
     }
     $rowAffected = $DB->Update("b_rating_voting", $arFields, "WHERE ENTITY_TYPE_ID='" . $DB->ForSql($arParam['ENTITY_TYPE_ID']) . "' AND ENTITY_ID='" . intval($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='" . intval($arParam['ENTITY_ID']) . "'", false, $err_mess . __LINE__);
         $arRV = $rsRV->Fetch();
         $arParam['RATING_VOTING_ID'] = $arRV['ID'];
         $arParam['TOTAL_POSITIVE_VOTES'] = $arRV['TOTAL_POSITIVE_VOTES'];
     } else {
         $arFields = array("ENTITY_TYPE_ID" => "'" . $DB->ForSql($arParam["ENTITY_TYPE_ID"]) . "'", "ENTITY_ID" => intval($arParam['ENTITY_ID']), "OWNER_ID" => intval($arParam['OWNER_ID']), "ACTIVE" => "'Y'", "CREATED" => $DB->GetNowFunction(), "LAST_CALCULATED" => $DB->GetNowFunction(), "TOTAL_VOTES" => 1, "TOTAL_VALUE" => floatval($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;
     }
     $arFields = array("RATING_VOTING_ID" => intval($arParam['RATING_VOTING_ID']), "ENTITY_TYPE_ID" => "'" . $DB->ForSql($arParam["ENTITY_TYPE_ID"]) . "'", "ENTITY_ID" => intval($arParam['ENTITY_ID']), "VALUE" => floatval($arParam['VALUE']), "ACTIVE" => "'Y'", "CREATED" => $DB->GetNowFunction(), "USER_ID" => intval($arParam['USER_ID']), "USER_IP" => "'" . $DB->ForSql($arParam["USER_IP"]) . "'", "OWNER_ID" => intval($arParam['OWNER_ID']));
     $ID = $DB->Insert("b_rating_vote", $arFields, $err_mess . __LINE__);
     foreach (GetModuleEvents("main", "OnAddRatingVote", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array(intval($ID), $arParam));
     }
     if (CModule::IncludeModule('pull')) {
         CPullStack::AddShared(array('module_id' => 'main', 'command' => 'rating_vote', 'params' => array("TYPE" => "ADD", "USER_ID" => intval($arParam['USER_ID']), "ENTITY_TYPE_ID" => $arParam["ENTITY_TYPE_ID"], "ENTITY_ID" => intval($arParam['ENTITY_ID']), "TOTAL_POSITIVE_VOTES" => $arParam['TOTAL_POSITIVE_VOTES'], "RESULT" => $votePlus ? 'PLUS' : 'MINUS')));
     }
     if (CACHED_b_rating_vote !== false) {
         $bucket_size = intval(CACHED_b_rating_bucket_size);
         if ($bucket_size <= 0) {
             $bucket_size = 100;
         }
         $bucket = intval(intval($arParam['ENTITY_ID']) / $bucket_size);
         $CACHE_MANAGER->Clean("b_rvg_" . $DB->ForSql($arParam["ENTITY_TYPE_ID"]) . $bucket, "b_rating_voting");
     }
     return true;
 }
Esempio n. 2
0
	function CheckAllowVote($arVoteParam)
	{
		global $USER;

		$userId = $USER->GetId();
		$bUserAuth = $USER->IsAuthorized();
		$bAllGroups = false;

		$arInfo = array(
			'RESULT' => true,
			'ERROR_TYPE' => '',
			'ERROR_MSG' => '',
		);

		$bSelfVote = COption::GetOptionString("main", "rating_self_vote", 'N');
		if ($bSelfVote == 'N' && IntVal($arVoteParam['OWNER_ID']) == $userId)
		{
			$arInfo = array(
				'RESULT' => false,
				'ERROR_TYPE' => 'SELF',
				'ERROR_MSG' => GetMessage('RATING_ALLOW_VOTE_SELF'),
			);
		}
		else if (!$bUserAuth)
		{
			$arInfo = array(
				'RESULT' => false,
				'ERROR_TYPE' => 'GUEST',
				'ERROR_MSG' => GetMessage('RATING_ALLOW_VOTE_GUEST'),
			);
		}
		else
		{
			static $cacheAllowVote = array();
			static $cacheUserVote = array();
			static $cacheVoteAccess = null;
			static $cacheVoteGroup = array();
			static $cacheVoteSize = 0;
			if(!array_key_exists($userId, $cacheAllowVote))
			{
				global $DB;
				$arGroups = array();
				$bAllGroups = false;
				$sVoteType = $arVoteParam['ENTITY_TYPE_ID'] == 'USER'? 'A': 'R';
				if (!isset($cacheVoteGroup[$sVoteType]))
				{
					$cacheVoteGroup[$sVoteType] = Array();
					$rsGroups = CRatings::GetVoteGroup($sVoteType);
					while ($arVoteGroup = $rsGroups->Fetch())
						$cacheVoteGroup[$sVoteType][] = $arVoteGroup;
				}
				foreach($cacheVoteGroup[$sVoteType] as $arVoteGroup)
				{
					if ($arVoteGroup['GROUP_ID'] == 2)
					{
						$bAllGroups = true;
						break;
					}
					$arGroups[] = $arVoteGroup['GROUP_ID'];
				}
				if (!$bAllGroups && !empty($arGroups) && is_null($cacheVoteAccess))
				{
					$strSql = '
						SELECT * FROM b_user_group UG
						WHERE UG.GROUP_ID IN ('.implode(',', $arGroups).')
						AND UG.USER_ID = '.$userId.'
						AND ((UG.DATE_ACTIVE_FROM IS NULL) OR (UG.DATE_ACTIVE_FROM <= '.$DB->CurrentTimeFunction().'))
						AND ((UG.DATE_ACTIVE_TO IS NULL) OR (UG.DATE_ACTIVE_TO >= '.$DB->CurrentTimeFunction().'))';
					$res = $DB->Query($strSql, false, $err_mess.__LINE__);
					if ($row = $res->Fetch())
						$cacheVoteAccess = true;
					else
					{
						$cacheVoteAccess = false;
						$arInfo = $cacheAllowVote[$userId] = array(
							'RESULT' => false,
							'ERROR_TYPE' => 'ACCESS',
							'ERROR_MSG' => GetMessage('RATING_ALLOW_VOTE_ACCESS'),
						);
					}
				}

				$authorityRatingId	 = CRatings::GetAuthorityRating();
				$arAuthorityUserProp = CRatings::GetRatingUserProp($authorityRatingId, $userId);
				if ($arAuthorityUserProp['VOTE_WEIGHT'] <= 0)
				{
					$arInfo = $cacheAllowVote[$userId] = array(
						'RESULT' => false,
						'ERROR_TYPE' => 'ACCESS',
						'ERROR_MSG' => GetMessage('RATING_ALLOW_VOTE_LOW_WEIGHT'),
					);
				}

				if ($arInfo['RESULT'] && $sVoteType == 'A')
				{

					$strSql = '
						SELECT COUNT(*) as VOTE
						FROM b_rating_vote RV
						WHERE RV.USER_ID = '.$userId.'
						AND RV.CREATED > DATE_SUB(NOW(), INTERVAL 1 DAY)';
					$res = $DB->Query($strSql, false, $err_mess.__LINE__);
					$countVote = $res->Fetch();
					$cacheVoteSize = $_SESSION['RATING_VOTE_COUNT'] = $countVote['VOTE'];

					$cacheUserVote[$userId] = $_SESSION['RATING_USER_VOTE_COUNT'] = $arAuthorityUserProp['VOTE_COUNT'];

					if ($cacheVoteSize >= $cacheUserVote[$userId])
					{
						$arInfo = $cacheAllowVote[$userId] = array(
							'RESULT' => false,
							'ERROR_TYPE' => 'COUNT_VOTE',
							'ERROR_MSG' => GetMessage('RATING_ALLOW_VOTE_COUNT_VOTE'),
						);
					}
				}
			}
			else
			{
				if ($cacheAllowVote[$userId]['RESULT'])
				{
					if ($cacheVoteSize >= $cacheUserVote[$userId])
					{
						$arInfo = $cacheAllowVote[$userId] = array(
							'RESULT' => false,
							'ERROR_TYPE' => 'COUNT_VOTE',
							'ERROR_MSG' => GetMessage('RATING_ALLOW_VOTE_COUNT_VOTE'),
						);
					}
				}
				$arInfo = $cacheAllowVote[$userId];
			}
		}

		$db_events = GetModuleEvents("main", "OnAfterCheckAllowVote");
		while ($arEvent = $db_events->Fetch())
		{
			$arEventResult = ExecuteModuleEventEx($arEvent, array($arVoteParam));
			if (is_array($arEventResult) && isset($arEventResult['RESULT']) && $arEventResult['RESULT'] === false
				&& isset($arEventResult['ERROR_TYPE']) && strlen($arEventResult['ERROR_MSG']) > 0
				&& isset($arEventResult['ERROR_MSG']) && strlen($arEventResult['ERROR_MSG']) > 0)
			{
				$arInfo = array(
					'RESULT' => false,
					'ERROR_TYPE' => $arEventResult['ERROR_TYPE'],
					'ERROR_MSG' => $arEventResult['ERROR_MSG'],
				);
			}
		}
		return $arInfo;
	}
Esempio n. 3
0
    if ($arRatingsTmp['AUTHORITY'] == 'Y') {
        $arRatingsTmp['NAME'] = '<span class="required">[A]</span> ' . $arRatingsTmp['NAME'];
    }
    $aTabs2[] = array("DIV" => "rating_" . $i, "TAB" => $arRatingsTmp['NAME'], "TITLE" => GetMessage('RATING_TAB_INFO'));
    $arRatings[$arRatingsTmp['ID']] = $arRatingsTmp;
    $i++;
}
if (is_array($arRatings) && !empty($arRatings)) {
    $ratingWeightType = COption::GetOptionString("main", "rating_weight_type", "auto");
    $authorityRatingId = CRatings::GetAuthorityRating();
    $arAuthorityUserProp = CRatings::GetRatingUserProp($authorityRatingId, $ID);
    $viewTabControl = new CAdminViewTabControl("tabControlRating", $aTabs2);
    $viewTabControl->Begin();
    foreach ($arRatings as $ratingId => $arRating) {
        $arRatingResult = CRatings::GetRatingResult($ratingId, $ID);
        $arRatingUserProp = CRatings::GetRatingUserProp($ratingId, $ID);
        if ($ratingId == $authorityRatingId && $arRatingUserProp['BONUS'] == 0) {
            $arRatingUserProp['BONUS'] = COption::GetOptionString("main", "rating_start_authority", 3);
        }
        $viewTabControl->BeginNextTab();
        ?>
					<table cellspacing="7" cellpadding="0" border="0" width="100%" class="edit-table">
				<?php 
        if ($USER->CanDoOperation('edit_ratings') && ($selfEdit || $ID != $uid)) {
            ?>
					<tr>
						<td class="field-name" width="40%"><?php 
            echo GetMessage('RATING_BONUS');
            ?>
:<sup><span class="required">2</span></sup></td>
						<td><?php 
Esempio n. 4
0
 function AddRatingVote($arParam)
 {
     global $DB, $CACHE_MANAGER;
     if (isset($_SESSION['RATING_VOTE_COUNT']) && $arParam['ENTITY_TYPE_ID'] == 'USER') {
         if ($_SESSION['RATING_VOTE_COUNT'] >= $_SESSION['RATING_USER_VOTE_COUNT']) {
             return false;
         } else {
             $_SESSION['RATING_VOTE_COUNT']++;
         }
     }
     CRatings::CancelRatingVote($arParam);
     $err_mess = CRatings::err_mess() . "<br>Function: AddRatingVote<br>Line: ";
     $votePlus = $arParam['VALUE'] < 0 ? false : true;
     $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 ? '+' : '') . floatval($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;
     $db_events = GetModuleEvents("main", "OnGetRatingContentOwner");
     while ($arEvent = $db_events->Fetch()) {
         $result = ExecuteModuleEventEx($arEvent, array($arParam));
         if ($result !== false) {
             $arParam['OWNER_ID'] = IntVal($result);
         }
     }
     $rowAffected = $DB->Update("b_rating_voting", $arFields, "WHERE ENTITY_TYPE_ID='" . $DB->ForSql($arParam['ENTITY_TYPE_ID']) . "' AND ENTITY_ID='" . intval($arParam['ENTITY_ID']) . "'", $err_mess . __LINE__);
     if ($rowAffected > 0) {
         $rsRV = $DB->Query("SELECT ID FROM b_rating_voting WHERE ENTITY_TYPE_ID='" . $DB->ForSql($arParam['ENTITY_TYPE_ID']) . "' AND ENTITY_ID='" . intval($arParam['ENTITY_ID']) . "'", false, $err_mess . __LINE__);
         $arRV = $rsRV->Fetch();
         $arParam['RATING_VOTING_ID'] = $arRV['ID'];
     } else {
         $arFields = array("ENTITY_TYPE_ID" => "'" . $DB->ForSql($arParam["ENTITY_TYPE_ID"]) . "'", "ENTITY_ID" => intval($arParam['ENTITY_ID']), "OWNER_ID" => intval($arParam['OWNER_ID']), "ACTIVE" => "'Y'", "CREATED" => $DB->GetNowFunction(), "LAST_CALCULATED" => $DB->GetNowFunction(), "TOTAL_VOTES" => 1, "TOTAL_VALUE" => floatval($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__);
     }
     $arFields = array("RATING_VOTING_ID" => intval($arParam['RATING_VOTING_ID']), "ENTITY_TYPE_ID" => "'" . $DB->ForSql($arParam["ENTITY_TYPE_ID"]) . "'", "ENTITY_ID" => intval($arParam['ENTITY_ID']), "VALUE" => floatval($arParam['VALUE']), "ACTIVE" => "'Y'", "CREATED" => $DB->GetNowFunction(), "USER_ID" => intval($arParam['USER_ID']), "USER_IP" => "'" . $DB->ForSql($arParam["USER_IP"]) . "'", "OWNER_ID" => intval($arParam['OWNER_ID']));
     $ID = $DB->Insert("b_rating_vote", $arFields, $err_mess . __LINE__);
     $db_events = GetModuleEvents("main", "OnAddRatingVote");
     while ($arEvent = $db_events->Fetch()) {
         ExecuteModuleEventEx($arEvent, array(intval($ID), $arParam));
     }
     $CACHE_MANAGER->ClearByTag('RV_' . $arParam["ENTITY_TYPE_ID"] . "_" . intval($arParam['ENTITY_ID']));
     return true;
 }