コード例 #1
0
ファイル: commune.php プロジェクト: Nikitian/fl-ru-damp
 /**
 * Голосование
 *
 * @param integer $commune_id ИД сообщества
 * @param integer $user_id    Ид пользователя
 * @param integer $rating       Голос
 * @return  Возвращает: -1, если голос ПРОТИВ был принят; 1, если голос ЗА был принят;
    				0, если голос не принят или ошибка.
  			Принят -- то есть, пошел в расчет, рейтинг изменился.
 */
 function TopicVote($topic_id, $user_id, $rating)
 {
     global $DB;
     $vB = (int) self::GetUserTopicVote($topic_id, $user_id);
     if ($vB == 0) {
         $rating = $rating > 0 ? 1 : -1;
     } elseif ($vB < 0 && $rating > 0) {
         $rating = 0;
     } elseif ($vB > 0 && $rating < 0) {
         $rating = 0;
     } elseif ($vB == $rating) {
         return 0;
     } elseif ($vB < 0 && $rating < 0) {
         return 0;
     } elseif ($vB > 0 && $rating > 0) {
         return 0;
     }
     $sql = "UPDATE commune_users_messages SET rating = ?i\n            WHERE message_id = ?i\n                AND user_id = ?i;";
     $res = $DB->query($sql, $rating, $topic_id, $user_id);
     if (!pg_affected_rows($res)) {
         $insert_table = self::getTableName('commune_users_messages', self::getCommuneIDByMessageID($topic_id));
         $sql = "INSERT INTO {$insert_table} (message_id, user_id, rating)\n                VALUES (?i, ?i, ?i)";
         $res = $DB->query($sql, $topic_id, $user_id, $rating);
     }
     $rate = self::GetTopicRating($topic_id);
     $actionRate = commune_carma::actionByRate($rate, commune_carma::getScale('post'));
     if ($actionRate == 'banned') {
         $themes = current(commune::getCommunePostByIds(array($topic_id)));
         if (!commune_carma::isImmunity($themes['user_id'], array(), $topic_id) && $themes['id'] > 0) {
             commune::BlockedTopic($themes['theme_id'], $topic_id, 'block');
         }
     }
     //    $vB = (int)self::GetUserTopicVote($topic_id, $user_id);
     $vA = (int) self::GetUserTopicVote($topic_id, $user_id);
     return ($vA > $vB) - ($vA < $vB);
 }