socialforge/app/helpers/user_score_helper.rb

539 lines
18 KiB
Ruby
Raw Normal View History

2014-04-21 11:13:45 +08:00
# encoding: utf-8
#
# 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.
module UserScoreHelper
def calculate_collaboration_count(user)
issue_c = 0
issues = Issue.where('author_id = ?', user.id)
issues.each do |issue|
issue_c = issue_c + issue.journals.where("user_id <> ?", user.id).count
end
2014-04-22 09:54:56 +08:00
issue_c = issue_c + Journal.where("user_id = ?", user.id).count
############################
memos = Memo.where('author_id = ? AND parent_id IS NOT NULL', user.id)
memos.each do |m|
if Memo.find(m.parent_id).author.id != user.id
issue_c = issue_c + 1
else
issue_c = issue_c - 1
end
end
pmemos = Memo.where('author_id = ? AND parent_id IS NULL', user.id)
pmemos.each do |pm|
issue_c = issue_c + pm.replies_count
end
############################
issue_c = issue_c + JournalsForMessage.where('user_id = ? AND reply_id IS NOT NULL AND reply_id <> ?', user.id, user.id).count + JournalsForMessage.where('reply_id = ? AND user_id <> ?', user.id, user.id).count
2014-04-21 11:13:45 +08:00
return issue_c
end
def calculate_influence_count(user)
2014-04-22 14:53:25 +08:00
watcher_count = Watcher.where("watchable_type = 'principal' AND watchable_id = ?", user.id).count
2014-04-21 11:13:45 +08:00
end
2014-04-30 13:51:19 +08:00
2014-04-21 11:13:45 +08:00
def calculate_skill_count(user)
2014-04-30 13:51:19 +08:00
praise_count_l0 = 0
praise_count_l1 = 0
praise_count_l2 = 0
tread_count_l0 = 0
tread_count_l1 = 0
tread_count_l2 = 0
issues = Issue.where('author_id = ?', user.id)
issues.each do |i|
2014-04-30 13:51:19 +08:00
pts = PraiseTread.where('praise_tread_object_id = ?', i.id)
pts.each do |p|
templevel = calculate_level(User.find(p.user_id))
if templevel.to_i == 0
if p.praise_or_tread == 1
praise_count_l0 = praise_count_l0 + 1
else
tread_count_l0 = tread_count_l0 + 1
end
end
if templevel.to_i == 1
if p.praise_or_tread == 1
praise_count_l1 = praise_count_l1 + 1
else
tread_count_l1 = tread_count_l1 + 1
end
end
if templevel.to_i == 2
if p.praise_or_tread == 1
praise_count_l2 = praise_count_l2 + 1
else
tread_count_l2 + tread_count_l2 + 1
end
end
end
end
2014-04-30 13:51:19 +08:00
bids = Bid.where('author_id = ?', user.id)
bids.each do |b|
2014-04-30 13:51:19 +08:00
ptcs = PraiseTread.where('praise_tread_object_id = ?', b.id)
ptcs.each do |p|
2014-04-30 13:51:19 +08:00
templevel = calculate_level(User.find(p.user_id))
if templevel.to_i == 0
if p.praise_or_tread == 1
praise_count_l0 = praise_count_l0 + 1
else
tread_count_l0 = tread_count_l0 + 1
end
end
if templevel.to_i == 1
if p.praise_or_tread == 1
praise_count_l1 = praise_count_l1 + 1
else
tread_count_l1 = tread_count_l1 + 1
end
end
if templevel.to_i == 2
if p.praise_or_tread == 1
praise_count_l2 = praise_count_l2 + 1
else
tread_count_l2 + tread_count_l2 + 1
end
end
end
end
contests = Contest.where('author_id = ?', user.id)
contests.each do |c|
2014-04-30 13:51:19 +08:00
ptcs = PraiseTread.where('praise_tread_object_id = ?', c.id)
ptcs.each do |p|
2014-04-30 13:51:19 +08:00
templevel = calculate_level(User.find(p.user_id))
if templevel.to_i == 0
if p.praise_or_tread == 1
praise_count_l0 = praise_count_l0 + 1
else
tread_count_l0 = tread_count_l0 + 1
end
end
if templevel.to_i == 1
if p.praise_or_tread == 1
praise_count_l1 = praise_count_l1 + 1
else
tread_count_l1 = tread_count_l1 + 1
end
end
if templevel.to_i == 2
if p.praise_or_tread == 1
praise_count_l2 = praise_count_l2 + 1
else
tread_count_l2 + tread_count_l2 + 1
end
end
end
end
# case level
# when 0 skill_score = praise_count - 0.5 * tread_count
# when 1 skill_score = 2 * praise_count - 1.5 * tread_count
# when 2 skill_socre = 3 * praise_count - 2.5 * tread_count
# when 3 skill_socre = 4 * praise_count - 3.5 * tread_count
# when 4 skill_socre = 5 * praise_count - 4.5 * tread_count
# end
2014-04-30 13:51:19 +08:00
skill_score = 2 * praise_count_l0.to_f + 3 * praise_count_l1.to_f + 4 * praise_count_l2.to_f
- 1 * tread_count_l0.to_f - 1.5 * tread_count_l1.to_f - 2 * tread_count_l2.to_f
2014-04-22 09:54:56 +08:00
tread_user_count = PraiseTread.where('praise_or_tread = ? AND user_id = ?', 0, user.id).count
2014-04-30 13:51:19 +08:00
skill_score = skill_score.to_f - tread_user_count.to_f
return skill_score
2014-04-21 11:13:45 +08:00
end
def calculate_level(user)
commit_count = user.changesets.count
max_praise_num = 0
issues = Issue.where('author_id = ?', user.id)
issues.each do |i|
ptcs = PraiseTreadCache.where('object_id = ?', i.id)
ptcs.each do |p|
2014-04-30 09:13:05 +08:00
if p.praise_num.to_i > max_praise_num.to_i
max_praise_num = p.praise_num
end
end
end
bids = Bid.where('author_id = ?', user.id)
bids.each do |b|
ptcs = PraiseTreadCache.where('object_id = ?', b.id)
ptcs.each do |p|
2014-04-30 09:13:05 +08:00
if p.praise_num.to_i > max_praise_num.to_i
max_praise_num = p.praise_num
end
end
end
contests = Contest.where('author_id = ?', user.id)
contests.each do |c|
ptcs = PraiseTreadCache.where('object_id = ?', c.id)
ptcs.each do |p|
2014-04-30 09:13:05 +08:00
if p.praise_num.to_i > max_praise_num.to_i
max_praise_num = p.praise_num
end
end
end
best_answer_num = 0
2014-04-22 09:54:56 +08:00
isManager = 0
members = Member.where('user_id = ?', user.id)
members.each do |m|
@membership = m.memberships.all(:conditions => Project.visible_condition(User.current))
@membership.each do |membership|
#拥有编辑项目权限的可操作该项目
2014-07-08 18:02:10 +08:00
if m.allowed_to?(:is_manager, membership.project, :global => false)
2014-04-22 09:54:56 +08:00
isManager = 1
end
end
2014-04-22 09:54:56 +08:00
end
level = 0
if max_praise_num > 4
level = 1
2014-04-22 14:53:25 +08:00
end
if commit_count > 0 and commit_count < 101
level = 1
2014-04-22 14:53:25 +08:00
end
if commit_count > 100 or isManager == 1
level = 2
end
return level
2014-04-21 11:13:45 +08:00
end
def calculate_activity_count(user)
# commit_count = user.changesets.count
# issue_details_count = 0
# issues = Issue.where('assigned_to_id = ?', user.id)
# issues.each do |issue|
# change_count = issue.journals.where("prop_key = ?", "done_ratio").count
# issue_details_count = change_count + issue_details_count
# end
# file_count = user.file_commit.count
# issue_count = Issue.where('author_id = ?', user.id).count
2014-04-22 14:53:25 +08:00
#f = user.user_score.file
#i = user.user_score.issue
#f_max = UserScore.find_max_file
#f_min = UserScore.find_min_file
#i_max = UserScore.find_max_issue
#i_min = UserScore.find_min_issue
#score = 100 * ((f - f_min)/(f_max - f_min) + (i - i_min)/(i_max - i_min))
2014-04-21 11:13:45 +08:00
end
def calculate_file(user)
file_count = user.file_commit.count
issue_count = Issue.where('author_id = ?', user.id).count
return (file_count + issue_count)
end
def calculate_issue(user)
commit_count = user.changesets.count
issue_details_count = 0
issues = Issue.where('assigned_to_id = ?', user.id)
2014-04-22 14:53:25 +08:00
change_count = 0
2014-04-21 11:13:45 +08:00
issues.each do |issue|
2014-04-22 14:53:25 +08:00
js = issue.journals
js.each do |j|
change_count = change_count + j.details.where("prop_key = ?", "done_ratio").count
end
2014-04-21 11:13:45 +08:00
issue_details_count = change_count + issue_details_count
end
return (commit_count + issue_details_count)
end
2014-05-05 10:03:13 +08:00
def calculate_attachments(user)
attachments = Attachment.where("container_type IS NOT NULL AND container_type <> 'Issue' AND author_id = ?", user.id).count
return attachments
end
2014-04-21 11:13:45 +08:00
def calculate_user_score(user)
collaboration = calculate_collaboration_count(user)
influence = calculate_influence_count(user)
skill = calculate_skill_count(user)
activity = calculate_activity_count(user)
file = calculate_file(user)
issue = calculate_issue(user)
level = calculate_level(user)
2014-04-22 14:53:25 +08:00
#user.user_score << UserScore.new(:collaboration => collaboration, :influence => influence, :skill => skill,
# :activity => activity, :file => file, :issue => issue, :level => level)
UserScore.new(:collaboration => collaboration, :influence => influence, :skill => skill,
2014-04-21 11:13:45 +08:00
:activity => activity, :file => file, :issue => issue, :level => level)
end
def update_user_score(user)
collaboration = calculate_collaboration_count(user)
influence = calculate_influence_count(user)
skill = calculate_skill_count(user)
file = calculate_file(user)
issue = calculate_issue(user)
2014-04-22 14:53:25 +08:00
##activity = calculate_activity_count(user)
2014-04-21 11:13:45 +08:00
level = calculate_level(user)
user.user_score.update_attributes(:collaboration => collaboration, :influence => influence, :skill => skill,
:activity => activity, :file => file, :issue => issue, :level => level)
end
2014-04-22 14:53:25 +08:00
#====================================================================================================
2014-07-24 18:05:37 +08:00
def get_option_number(user,type,project_id=nil)
if project_id.nil?
option_number = OptionNumber.where("user_id = '#{user.id}' and score_type = '#{type}'");
else
option_number = OptionNumber.where("user_id = '#{user.id}' and score_type = '#{type}' and project_id = '#{project_id}'");
end
result = nil
if option_number.nil? || option_number.count == 0
result = OptionNumber.new
result.user_id = user.id
result.memo = 0
result.messages_for_issues = 0
result.issues_status = 0
result.replay_for_message = 0
result.replay_for_memo = 0
result.follow = 0
result.tread = 0
result.praise_by_one = 0
result.praise_by_two = 0
result.praise_by_three = 0
result.tread_by_one = 0
result.tread_by_two = 0
result.tread_by_three = 0
result.changeset = 0
result.document = 0
result.attachment = 0
result.issue_done_ratio = 0
result.post_issue = 0
result.total_score = 0
result.score_type =type
unless project_id.nil?
result.project_id = project_id
end
else
result = option_number.first
end
2014-07-24 18:05:37 +08:00
result
end
#更新分数
def update_score(option_number)
option_number.total_score = collaboration(option_number) + influence(option_number) + skill(option_number) + active(option_number)
option_number.save
end
#协同得分
def collaboration(option_number)
option_number.memo * 2 + option_number.messages_for_issues + option_number.issues_status + option_number.replay_for_message + option_number.replay_for_memo
end
#影响力得分
def influence(option_number)
option_number.follow * 2
end
#技术得分
def skill(option_number)
option_number.praise_by_one * 4 + option_number.praise_by_two * 6 + option_number.praise_by_three * 8 - option_number.tread * 2 - option_number.tread_by_one * 2 - option_number.tread_by_two * 4 - option_number.tread_by_three * 6
end
#项目贡献得分
def active(option_number)
option_number.changeset * 4 + option_number.document * 4 + option_number.attachment * 4 + option_number.issue_done_ratio * 2 + option_number.post_issue * 4
end
#更新发帖数
def update_memo_number(user,type)
option_number = get_option_number(user,type)
2014-07-24 18:05:37 +08:00
option_number.memo = Message.includes(:author).where("parent_id IS NULL and author_id = '#{user.id}'").all.count + Memo.includes(:author).where("parent_id IS NULL and author_id = '#{user.id}'").all.count
update_score(option_number)
end
#更新对缺陷留言数
def update_messges_for_issue(user,type)
option_number = get_option_number(user,type)
option_number.messages_for_issues = Journal.includes(:user).where("user_id = '#{user.id}'").all.count
update_score(option_number)
end
#更新更改缺陷状态状态次数
def update_issues_status(user,type)
option_number = get_option_number(user,type)
option_number.issues_status = Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
update_score(option_number)
end
#更新对留言的回复数量
def update_replay_for_message(user,type)
option_number = get_option_number(user,type)
option_number.replay_for_message = JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id}").count
update_score(option_number)
end
#更新对帖子的回复数量
def update_replay_for_memo(user,type)
option_number = get_option_number(user,type)
2014-07-24 18:05:37 +08:00
option_number.replay_for_memo = Message.includes(:author).where("parent_id IS NOT NULL and author_id = #{user.id}").all.count + Memo.includes(:author).where("parent_id IS NOT NULL and author_id = #{user.id}").all.count
update_score(option_number)
end
#更新被关注的人数
def update_follow(user,type)
option_number = get_option_number(user,type)
option_number.follow = Watcher.includes(:watchable).where("watchable_type = 'Principal' and watchable_id = '#{user.id}'").count
update_score(option_number)
end
2014-04-21 11:13:45 +08:00
2014-07-24 18:05:37 +08:00
#更新帖子踩各项数量
def update_tread(user,type)
option_number = get_option_number(user,type)
option_number.tread = PraiseTread.where("praise_tread_object_type = 'Memo' || praise_tread_object_type = 'Message' and praise_or_tread = 0 and user_id = '#{user.id}'").all.count
pts = PraiseTread.where("praise_tread_object_type = 'Memo' || praise_tread_object_type = 'Message' and praise_or_tread = 0").all
result = []
result1 = []
result2 = []
pts.each do |pt|
obj = PraiseTread.find_object_by_type_and_id(pt.praise_tread_object_type, pt.praise_tread_object_id)
if obj.nil?
next
end
target_user = obj.author
level = UserLevels.get_level(pt.user)#pt.user.get_level
project = pt.project
if level == 1 && target_user.id = user.id
result << pt
elsif level == 2 && target_user.id = user.id
result1 << pt
elsif level == 3 && target_user.id = user.id
result2 << pt
end
end
option_number.tread_by_one = result.count
option_number.tread_by_two = result1.count
option_number.tread_by_three = result2.count
update_score(option_number)
end
#更新帖子顶数量
def update_praise(user,type)
option_number = get_option_number(user,type)
pts = PraiseTread.where("praise_tread_object_type = 'Memo' || praise_tread_object_type = 'Message' and praise_or_tread = 1").all
result = []
result1 = []
result2 = []
pts.each do |pt|
obj = PraiseTread.find_object_by_type_and_id(pt.praise_tread_object_type, pt.praise_tread_object_id)
if obj.nil?
next
end
target_user = obj.author
level = UserLevels.get_level(pt.user)#pt.user.get_level
project = pt.project
if level == 1 && target_user.id = user.id
result << pt
elsif level == 2 && target_user.id = user.id
result1 << pt
elsif level == 3 && target_user.id = user.id
result2 << pt
end
end
option_number.praise_by_one = result.count
option_number.praise_by_two = result1.count
option_number.praise_by_three = result2.count
update_score(option_number)
end
#更新提交代码次数
def update_changeset(user,type)
option_number = get_option_number(user,type)
option_number.changeset = Changeset.includes(:user).where("user_id = '#{user.id}'").all.count
update_score(option_number)
end
#更新文档提交次数
def update_document(user,type)
option_number = get_option_number(user,type)
option_number.document = Document.includes(:user).where("user_id = '#{user.id}'").all.count
update_score(option_number)
end
#更新附件提交数量
def update_attachment(user,type)
option_number = get_option_number(user,type)
option_number.attachment = Attachment.includes(:author).where("author_id = '#{user.id}'").all.count
update_score(option_number)
end
#更新缺陷完成度次数
def update_issue_done_ratio(user,type)
option_number = get_option_number(user,type)
option_number.issue_done_ratio = Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
update_score(option_number)
end
#更新发布缺陷次数
def update_post_issue(user,type)
option_number = get_option_number(user,type)
option_number.post_issue = Issue.includes(:author).where("author_id = '#{user.id}'").all.count
update_score(option_number)
end
2014-04-21 11:13:45 +08:00
end