} } $bTypeChange = isset($_POST["action"]) && $_POST["action"] == 'type_changed' ? true : false; $str_NAME = isset($_REQUEST["NAME"]) ? htmlspecialcharsbx($_REQUEST["NAME"]) : GetMessage("RATING_DEF_NAME"); $str_ENTITY_ID = isset($_REQUEST["ENTITY_ID"]) ? htmlspecialcharsbx($_REQUEST["ENTITY_ID"]) : 'USER'; $str_CALCULATION_METHOD = isset($_REQUEST["CALCULATION_METHOD"]) ? IntVal($_REQUEST["CALCULATION_METHOD"]) : '1'; $str_ACTIVE = isset($_REQUEST["ACTIVE"]) && $_REQUEST["ACTIVE"] == 'Y' ? 'Y' : 'N'; $str_POSITION = isset($_REQUEST["POSITION"]) && $_REQUEST["POSITION"] == 'Y' ? 'Y' : 'N'; $str_AUTHORITY = isset($_REQUEST["AUTHORITY"]) && $_REQUEST["AUTHORITY"] == 'Y' ? 'Y' : 'N'; $str_CONFIGS = null; if ($ID == 0 && empty($_POST)) { $str_ACTIVE = 'Y'; $str_POSITION = 'Y'; } if ($ID > 0 && !$bTypeChange) { $raging = CRatings::GetByID($ID); if (!($raging_arr = $raging->ExtractFields("str_"))) { $ID = 0; } $str_CONFIGS = unserialize(htmlspecialcharsback($str_CONFIGS)); } $sDocTitle = $ID > 0 ? GetMessage("MAIN_RATING_EDIT_RECORD", array("#ID#" => $ID)) : GetMessage("MAIN_RATING_NEW_RECORD"); $APPLICATION->SetTitle($sDocTitle); $APPLICATION->SetAdditionalCSS("/bitrix/themes/.default/ratings.css"); require $_SERVER["DOCUMENT_ROOT"] . BX_ROOT . "/modules/main/include/prolog_admin_after.php"; $aMenu = array(array("TEXT" => GetMessage("RATING_LIST"), "TITLE" => GetMessage("RATING_LIST_TITLE"), "LINK" => "rating_list.php?lang=" . LANG, "ICON" => "btn_list")); if ($ID > 0) { $aMenu[] = array("SEPARATOR" => "Y"); $aMenu[] = array("TEXT" => GetMessage("RATING_EDIT_ADD"), "TITLE" => GetMessage("RATING_EDIT_ADD_TITLE"), "LINK" => "rating_edit.php?lang=" . LANG, "ICON" => "btn_new"); $aMenu[] = array("TEXT" => GetMessage("RATING_EDIT_DEL"), "TITLE" => GetMessage("RATING_EDIT_DEL_TITLE"), "LINK" => "javascript:if(confirm('" . GetMessage("RATING_EDIT_DEL_CONF") . "')) window.location='rating_list.php?ID=" . $ID . "&action=delete&lang=" . LANG . "&" . bitrix_sessid_get() . "';", "ICON" => "btn_delete"); }
if ($arParams["SHOW_RATING"] == 'Y' && array_key_exists("RATING_ID", $arParams)) { if (is_array($arParams["RATING_ID"]) && count($arParams["RATING_ID"]) > 0) { $arParams["RATING_ID_ARR"] = $arParams["RATING_ID"]; $arParams["RATING_ID"] = $arParams["RATING_ID_ARR"][0]; foreach ($arParams["RATING_ID_ARR"] as $rating_id) { if (intval($rating_id) > 0) { $db_rating = CRatings::GetByID($rating_id); if ($arRating = $db_rating->GetNext()) { $arResult["RatingMultiple"][$rating_id] = array("NAME" => $arRating["NAME"]); } $arListParams["SELECT"][] = "RATING_" . $rating_id; } } $arResult["Rating"]["NAME"] = $arResult["RatingMultiple"][$arParams["RATING_ID"]]["NAME"]; } elseif (intval($arParams["RATING_ID"]) > 0) { $db_rating = CRatings::GetByID($arParams["RATING_ID"]); if ($arRating = $db_rating->GetNext()) { $arResult["Rating"]["NAME"] = $arRating["NAME"]; } $arListParams["SELECT"][] = "RATING_" . $arParams["RATING_ID"]; } $dbUser = CUser::GetList($by = "id", $order = "asc", array("ID_EQUAL_EXACT" => $arParams["ID"]), $arListParams); $arResult["User"] = $dbUser->GetNext(); } else { $dbUser = CUser::GetByID($arParams["ID"]); $arResult["User"] = $dbUser->GetNext(); } if (!is_array($arResult["User"])) { $arResult["FatalError"] = GetMessage("SONET_P_USER_NO_USER") . ". "; } else { if (CModule::IncludeModule('extranet') && !CExtranet::IsProfileViewable($arResult["User"]) && $arResult["User"]["ID"] != $USER->GetID()) {
public static function BuildRating($ID) { global $DB; $ID = intval($ID); $err_mess = (CRatings::err_mess())."<br>Function: BuildRating<br>Line: "; $resRating = CRatings::GetByID($ID); $arRating = $resRating->Fetch(); if ($arRating && $arRating['ACTIVE'] == 'Y') { $DB->Query("UPDATE b_rating SET CALCULATED = 'C' WHERE id = ".$ID, false, $err_mess.__LINE__); // Insert new results $sqlFunc = ($arRating['CALCULATION_METHOD'] == 'SUM') ? 'SUM' : 'AVG'; $strSql = " INSERT INTO b_rating_results (RATING_ID, ENTITY_TYPE_ID, ENTITY_ID, CURRENT_VALUE, PREVIOUS_VALUE) SELECT ".$ID." RATING_ID, '".$arRating['ENTITY_ID']."' ENTITY_TYPE_ID, RC.ENTITY_ID, ".$sqlFunc."(RC.CURRENT_VALUE) CURRENT_VALUE, 0 PREVIOUS_VALUE FROM 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 WHERE RC.RATING_ID = ".$ID." and RR.ID IS NULL GROUP BY RC.ENTITY_ID"; $res = $DB->Query($strSql, false, $err_mess.__LINE__); // Update current results $strSql = " UPDATE b_rating_results RR, ( SELECT '".$arRating['ENTITY_ID']."' ENTITY_TYPE_ID, RC.ENTITY_ID, ".$sqlFunc."(RC.CURRENT_VALUE) CURRENT_VALUE 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 WHERE RC.RATING_ID = ".$ID." GROUP BY RC.ENTITY_ID ) as RCR SET RR.PREVIOUS_VALUE = IF(RR.CURRENT_VALUE = RCR.CURRENT_VALUE, RR.PREVIOUS_VALUE, RR.CURRENT_VALUE), RR.CURRENT_VALUE = RCR.CURRENT_VALUE WHERE RR.RATING_ID=".$ID." and RR.ENTITY_TYPE_ID = RCR.ENTITY_TYPE_ID and RR.ENTITY_ID = RCR.ENTITY_ID "; $res = $DB->Query($strSql, false, $err_mess.__LINE__); // Calculation position in rating if ($arRating['POSITION'] == 'Y') { $strSql = " UPDATE b_rating_results RR, ( SELECT ENTITY_TYPE_ID, ENTITY_ID, CURRENT_VALUE, @nPos:=@nPos+1 as POSITION FROM b_rating_results, (select @nPos:=0) tmp WHERE RATING_ID = ".$ID." ORDER BY CURRENT_VALUE DESC ) as RP SET RR.PREVIOUS_POSITION = IF(RR.CURRENT_POSITION = RP.POSITION, RR.PREVIOUS_POSITION, RR.CURRENT_POSITION), RR.CURRENT_POSITION = RP.POSITION WHERE RR.RATING_ID=".$ID." and RR.ENTITY_TYPE_ID = RP.ENTITY_TYPE_ID and RR.ENTITY_ID = RP.ENTITY_ID "; $res = $DB->Query($strSql, false, $err_mess.__LINE__); } // Insert new user rating prop $strSql = " INSERT INTO b_rating_user (RATING_ID, ENTITY_ID) SELECT ".$ID." RATING_ID, U.ID as ENTITY_ID FROM b_user U LEFT JOIN b_rating_user RU ON RU.RATING_ID = ".$ID." and RU.ENTITY_ID = U.ID WHERE RU.ID IS NULL "; $res = $DB->Query($strSql, false, $err_mess.__LINE__); // authority calc if ($arRating['AUTHORITY'] == 'Y') { $sRatingAssignType = COption::GetOptionString("main", "rating_assign_type", "manual"); if ($sRatingAssignType == 'auto') { // auto assign for rating group $assignRatingGroup = COption::GetOptionString("main", "rating_assign_rating_group", 0); $assignRatingValueAdd = COption::GetOptionString("main", "rating_assign_rating_group_add", 1); $assignRatingValueDelete = COption::GetOptionString("main", "rating_assign_rating_group_delete", 1); CRatings::AutoAssignGroup($assignRatingGroup, $assignRatingValueAdd, $assignRatingValueDelete); // auto assign for authority group $assignAuthorityGroup = COption::GetOptionString("main", "rating_assign_authority_group", 0); $assignAuthorityValueAdd = COption::GetOptionString("main", "rating_assign_authority_group_add", 2); $assignAuthorityValueDelete = COption::GetOptionString("main", "rating_assign_authority_group_delete", 2); CRatings::AutoAssignGroup($assignAuthorityGroup, $assignAuthorityValueAdd, $assignAuthorityValueDelete); } $sRatingWeightType = COption::GetOptionString("main", "rating_weight_type", "auto"); if ($sRatingWeightType == 'auto') { $arCI = CRatings::GetCommunityInfo($ID); $communitySize = $arCI['COMMUNITY_SIZE']; $communityAuthority = $arCI['COMMUNITY_AUTHORITY']; $sRatingNormalizationType = COption::GetOptionString("main", "rating_normalization_type", "auto"); if ($sRatingNormalizationType == 'manual') $ratingNormalization = COption::GetOptionString("main", "rating_normalization", 1000); else { if ($communitySize <= 10) $ratingNormalization = 10; else if ($communitySize > 10 && $communitySize <= 1000) $ratingNormalization = 100; else if ($communitySize > 1000) $ratingNormalization = 1000; COption::SetOptionString("main", "rating_normalization", $ratingNormalization); } $voteWeight = 1; if ($communitySize > 0) $voteWeight = $ratingNormalization/$communitySize; COption::SetOptionString("main", "rating_community_size", $communitySize); COption::SetOptionString("main", "rating_community_authority", $communityAuthority); COption::SetOptionString("main", "rating_vote_weight", $voteWeight); $ratingCountVote = COption::GetOptionString("main", "rating_count_vote", 10); $strSql = "UPDATE b_rating_user SET VOTE_COUNT = 0, VOTE_WEIGHT =0 WHERE RATING_ID=".$ID; $res = $DB->Query($strSql, false, $err_mess.__LINE__); // default vote count + user authority $strSql = " UPDATE b_rating_user RU, ( SELECT ENTITY_ID, CURRENT_VALUE FROM b_rating_results WHERE RATING_ID = ".$ID." ) as RP SET RU.VOTE_COUNT = ".intval($ratingCountVote)."+RP.CURRENT_VALUE, RU.VOTE_WEIGHT = RP.CURRENT_VALUE*".$voteWeight." WHERE RU.RATING_ID=".$ID." and RU.ENTITY_ID = RP.ENTITY_ID "; $res = $DB->Query($strSql, false, $err_mess.__LINE__); } else { // Depending on current authority set correct weight votes // Depending on current authority set correct vote count $strSql = "UPDATE b_rating_user SET VOTE_COUNT = 0, VOTE_WEIGHT =0 WHERE RATING_ID=".$ID; $res = $DB->Query($strSql, false, $err_mess.__LINE__); $strSql = " UPDATE b_rating_user RU, ( SELECT RW.RATING_FROM, RW.RATING_TO, RW.WEIGHT, RW.COUNT, RR.ENTITY_ID FROM b_rating_weight RW, b_rating_results RR WHERE RR.RATING_ID = ".$ID." and RR.CURRENT_VALUE BETWEEN RW.RATING_FROM AND RW.RATING_TO ) as RP SET RU.VOTE_COUNT = RP.COUNT, RU.VOTE_WEIGHT = RP.WEIGHT WHERE RU.RATING_ID=".$ID." and RU.ENTITY_ID = RP.ENTITY_ID "; $res = $DB->Query($strSql, false, $err_mess.__LINE__); } } global $CACHE_MANAGER; $CACHE_MANAGER->CleanDir("b_rating_user"); $DB->Query("UPDATE b_rating SET CALCULATED = 'Y', LAST_CALCULATED = ".$DB->GetNowFunction()." WHERE id = ".$ID, false, $err_mess.__LINE__); } return true; }