socialforge/app/models/user_score.rb

211 lines
8.5 KiB
Ruby
Raw Normal View History

2014-06-04 10:08:54 +08:00
# Redmine - project management software
# Copyright (C) 2006-2013 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class UserScore < ActiveRecord::Base
belongs_to :user
before_save :correct_score
2014-06-04 10:08:54 +08:00
def self.find_max_file
self.maximum(:file)
end
def self.find_min_file
self.minimum(:file)
end
def self.find_max_issue
self.maximum(:issue)
end
def self.find_min_issue
self.minimum(:file)
end
2014-06-06 17:33:42 +08:00
# 以上类方法目测没用,反正也报错
# ===============================================================================
# 前略·协同得分
# operate - 更新操作 类型 symbol
# user - 计分用户
#
# Examples
#
# UserScore.joint(:post_message, User.current, nil)
# # => true #当前用户发帖计分操作成功
#
# Returns boolean. 返回积分保存结果
def self.joint(operate, current_user, target_user, options={})
2014-06-09 17:37:48 +08:00
current_user, target_user = get_users(current_user, target_user)
user_score = current_user.user_score_attr
2014-06-06 17:33:42 +08:00
case operate
when :post_message # current_user 发帖了 Add Message
2014-06-09 17:37:48 +08:00
user_score.collaboration = user_score.collaboration.to_i + 2
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} posting a message. options => (#{options.to_s})"
when :post_issue # current_user 对 target_user 的缺陷留言了 Add Journal
2014-06-09 17:37:48 +08:00
user_score.collaboration = user_score.collaboration.to_i + 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} posting a issue. options => (#{options.to_s})"
when :change_issue_status # current_user 更改了缺陷的状态 Changed Issue
2014-06-09 17:37:48 +08:00
user_score.collaboration = user_score.collaboration.to_i + 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} change issue status. options => (#{options.to_s})"
when :reply_message # current_user 对 target_user 留言的回复 Add Journals_for_messages
2014-06-09 17:37:48 +08:00
user_score.collaboration = user_score.collaboration.to_i + 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} reply message. options => (#{options.to_s})"
when :reply_posting # current_user 对 target_user 帖子的回复 Add Message
2014-06-09 17:37:48 +08:00
user_score.collaboration = user_score.collaboration.to_i + 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} reply posting. options => (#{options.to_s})"
2014-06-06 17:33:42 +08:00
else
Rails.logger.error "[UserScore#joint] ===> #{operate} is not define."
return false
end
rescue => e
Rails.logger.error "[UserScore#joint] ===> Exception: #{e}."
2014-06-06 17:33:42 +08:00
end
# 前略·影响力得分
# operate - 更新操作 类型 symbol
# user - 计分用户
#
# Examples
#
# UserScore.influence(:followed_by, user)
# # => true #当前被关注用户记分成功
#
# Returns boolean. 返回积分保存结果
def self.influence(operate, current_user, target_user, options={})
2014-06-09 17:37:48 +08:00
current_user, target_user = get_users(current_user, target_user)
user_score = target_user.user_score_attr
2014-06-06 17:33:42 +08:00
case operate
2014-06-09 17:37:48 +08:00
when :followed_by # current_user 关注了target_user
user_score.active = user_score.influence.to_i + 2
user_score.save
Rails.logger.info "[UserScore#influence] ===> User: #{current_user} be followed. options => (#{options.to_s})"
when :cancel_followed # current_uer 取消了对 target_user的关注
user_score.active = user_score.influence.to_i - 2
user_score.save
Rails.logger.info "[UserScore#influence] ===> User: #{current_user} canceled followed. options => (#{options.to_s})"
2014-06-06 17:33:42 +08:00
else
Rails.logger.error "[UserScore#influence] ===> #{operate} is not define."
return false
end
rescue => e
Rails.logger.error "[UserScore#influence] ===> Exception: #{e}."
2014-06-06 17:33:42 +08:00
end
# 前略·技术得分
# operate - 更新操作 类型 symbol
# user - 计分用户
#
# Examples
#
# UserScore.skill(:followed_by, current_user, target_user)
# # => true #当前current_user对target_user用户帖子的记分成功
#
# Returns boolean. 返回积分保存结果
def self.skill(operate, current_user, target_user, options={})
2014-06-09 17:37:48 +08:00
current_user, target_user = get_users(current_user, target_user)
user_score = current_user.user_score_attr
2014-06-06 17:33:42 +08:00
case operate
# when :treading # current_user 踩了 target_user 的帖子
# Rails.logger.info "[UserScore#skill] ===> User: #{current_user} treading #{target_user}'s posting. options => (#{options.to_s})"
2014-06-06 17:33:42 +08:00
when :treaded_by_user # current_user 踩了 target_user 的帖子
Rails.logger.info "[UserScore#skill] ===> User: #{current_user} treaded_by #{target_user}. options => (#{options.to_s})"
when :praised_by_user # current_user 顶了 target_user 的帖子
Rails.logger.info "[UserScore#skill] ===> User: #{current_user} praised_by #{target_user}. options => (#{options.to_s})"
2014-06-06 17:33:42 +08:00
else
Rails.logger.error "[UserScore#skill] ===> #{operate} is not define."
return false
end
rescue => e
Rails.logger.error "[UserScore#skill] ===> Exception: #{e}."
2014-06-06 17:33:42 +08:00
end
# 前略·项目得分
# operate - 更新操作 类型 symbol
# user - 计分用户
#
# Examples
#
# UserScore.project(:pull_code, current_user)
# # => true #当前current_user对项目贡献积分成功
#
# Returns boolean. 返回积分保存结果
def self.project(operate, current_user, options={})
current_user, target_user = get_users(current_user, nil)
2014-06-09 13:52:21 +08:00
user_score = current_user.try(:user_score_attr)
return false if current_user.nil?
2014-06-06 17:33:42 +08:00
case operate
when :push_code # current_user 提交了代码
2014-06-09 13:52:21 +08:00
user_score = user_score.active.to_i + 4
user_score.save
Rails.logger.debug "[UserScore#project] ===> User: [#{current_user.id},#{current_user.name}] pushed code one time. options => (#{options.to_s})"
2014-06-06 17:33:42 +08:00
when :push_document # current_user
user_score.active = user_score.active.to_i + 4
user_score.save
Rails.logger.debug "[UserScore#project] ===> User: [#{current_user.id},#{current_user.name}] pushed a document. options => (#{options.to_s})"
2014-06-06 17:33:42 +08:00
when :push_file # current_user
user_score.active = user_score.active.to_i + 4
user_score.save
Rails.logger.debug "[UserScore#project] ===> User: [#{current_user.id},#{current_user.name}] pushed a file. options => (#{options.to_s})"
when :update_issue_ratio # current_user
user_score.active = user_score.active.to_i + 2
user_score.save
Rails.logger.debug "[UserScore#project] ===> User: [#{current_user.id},#{current_user.name}] updated issue ratio. options => (#{options.to_s})"
2014-06-06 17:33:42 +08:00
when :post_issue # current_user
user_score.active = user_score.active.to_i + 4
user_score.save
Rails.logger.debug "[UserScore#project] ===> User: [#{current_user.id},#{current_user.name}] posting issue. options => (#{options.to_s})"
2014-06-06 17:33:42 +08:00
else
2014-06-06 17:41:16 +08:00
Rails.logger.error "[UserScore#project] ===> #{operate} is not define."
2014-06-06 17:33:42 +08:00
return false
end
rescue => e
Rails.logger.error "[UserScore#project] ===> Exception: #{e}."
2014-06-06 17:33:42 +08:00
end
private
def self.get_users(current_user, target_user)
cUser = (current_user.kind_of?User) ? current_user : User.find_by_id(current_user)
tUser = (target_user.kind_of?User) ? target_user : User.find_by_id(target_user)
2014-06-06 17:41:16 +08:00
[cUser, tUser]
2014-06-06 17:33:42 +08:00
end
#修正分数
#分数小于0时修正为0
#分数大于等于0时不修正 -by zjc
def correct_score
if self.collaboration < 0
self.collaboration = 0
end
if self.active < 0
self.active = 0
end
if self.influence < 0
self.influence = 0
end
if self.skill < 0
self.skill = 0
end
end
2014-06-04 10:08:54 +08:00
end