socialforge/app/helpers/user_score_helper.rb

955 lines
39 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
#========================================================================================================
#个人得分统计
#========================================================================================================
#type 1个人得分、2个人在项目project中的得分
def get_option_number(user,type,project=nil)
if project.nil?
2014-07-24 18:05:37 +08:00
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}'");
2014-07-24 18:05:37 +08:00
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
2014-08-08 13:47:02 +08:00
unless project.nil?
result.project_id = project.id
2014-07-24 18:05:37 +08:00
end
else
result = option_number.first
end
2014-07-24 18:05:37 +08:00
result
end
def get_option_num_by_id(user_id,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
result
end
#更新分数
def update_score(option_number)
option_number.total_score = collaboration(option_number) + influence(option_number) + skill(option_number) + active(option_number)
2014-08-16 15:08:14 +08:00
if option_number.total_score < 0
option_number.total_score = 0
end
option_number.save
option_number.total_score
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,project=nil)
option_number = get_option_number(user,type,project)
option_number.memo = memo_num(user,project)#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
2014-07-29 10:06:48 +08:00
#发帖数
def memo_num(user,project=nil)
2014-07-29 18:04:35 +08:00
if project.nil?
2014-08-19 17:03:26 +08:00
#Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id != -1").all.count #+ Memo.includes(:author).where("parent_id IS NULL and author_id = '#{user.id}'").all.count
users = Message.find_by_sql("SELECT COUNT(*) as m_count FROM `messages` JOIN `boards` ON boards.project_id != -1 AND messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = #{user.id}")
2014-08-19 17:03:26 +08:00
result = 0
users.each do |user|
result = user.m_count
end
result
2014-08-08 13:47:02 +08:00
#user.messages.where("parent_id IS NULL").count
2014-07-29 18:04:35 +08:00
else
2014-08-19 17:03:26 +08:00
#Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id = #{project.id}").all.count
users = Message.find_by_sql("SELECT COUNT(*) as m_count FROM `messages` JOIN `boards` ON boards.project_id = '#{project.id}' AND messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = #{user.id}")
2014-08-19 17:03:26 +08:00
result = 0
users.each do |user|
result = user.m_count
end
result
2014-07-29 18:04:35 +08:00
end
2014-07-29 10:06:48 +08:00
end
2014-08-08 13:47:02 +08:00
def memo_score(user,project=nil)
if project.nil?
User.find_by_sql("SELECT `users`.id,(SELECT COUNT(*) * 2 FROM `messages` JOIN `boards` ON boards.project_id != -1 AND messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = `users`.id) AS m_count
FROM `users` where id = #{user.id}")
else
User.find_by_sql("SELECT `users`.id,(SELECT COUNT(*) * 2 FROM `messages` JOIN `boards` ON boards.project_id = '#{project.id}' AND messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = `users`.id) AS m_count
FROM `users` where id = #{user.id}")
end
end
#更新对缺陷留言数
def update_messges_for_issue(user,type,project=nil)
option_number = get_option_number(user,type,project)
option_number.messages_for_issues = messges_for_issue_num(user,project)#Journal.includes(:user).where("user_id = '#{user.id}' and notes != '' and notes is not null").all.count
update_score(option_number)
end
2014-07-29 10:06:48 +08:00
def messges_for_issue_num(user,project=nil)
2014-07-29 18:04:35 +08:00
if project.nil?
2014-08-19 17:03:26 +08:00
#Journal.includes(:user).where("user_id = '#{user.id}' and notes is not null and notes != ''").all.count
users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM journals WHERE journals.user_id = #{user.id} AND journals.notes IS NOT NULL AND journals.notes != ''")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
else
2014-08-19 17:03:26 +08:00
#Journal.includes(:user).joins(:issue).where("#{Journal.table_name}.user_id = '#{user.id}' and #{Issue.table_name}.project_id = '#{project.id}' and #{Journal.table_name}.notes != '' and #{Journal.table_name}.notes is not null").all.count
users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM journals join issues on #{Journal.table_name}.journalized_type = 'Issue' and #{Journal.table_name}.journalized_id = #{Issue.table_name}.id WHERE journals.user_id = #{user.id} AND journals.notes IS NOT NULL AND journals.notes != ''and #{Issue.table_name}.project_id = '#{project.id}'")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
end
2014-07-29 10:06:48 +08:00
end
2014-08-08 13:47:02 +08:00
def messges_for_issue_score(user,project=nil)
if project.nil?
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals WHERE journals.user_id = users.id AND journals.notes IS NOT NULL AND journals.notes != '') AS m_score FROM users where id = #{user.id}")
else
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals join issues on #{Journal.table_name}.journalized_type = 'Issue' and #{Journal.table_name}.journalized_id = #{Issue.table_name}.id WHERE journals.user_id = users.id AND journals.notes IS NOT NULL AND journals.notes != ''and #{Issue.table_name}.project_id = '#{project.id}') AS m_score FROM users where id = #{user.id}")
end
end
#更新更改缺陷状态状态次数
def update_issues_status(user,type,project=nil)
option_number = get_option_number(user,type,project)
option_number.issues_status = issues_status_num(user,project)#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
2014-07-29 10:06:48 +08:00
def issues_status_num(user,project=nil)
2014-07-29 18:04:35 +08:00
if project.nil?
2014-08-19 17:03:26 +08:00
#Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM journals JOIN journal_details on journals.id = journal_details.journal_id WHERE journal_details.prop_key = 'status_id' and journals.user_id = #{user.id}")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
else
2014-08-19 17:03:26 +08:00
#Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM journals JOIN journal_details on journals.id = journal_details.journal_id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE journal_details.prop_key = 'status_id' and journals.user_id = #{user.id} and issues.project_id = '#{project.id}'")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
end
2014-07-29 10:06:48 +08:00
end
2014-08-13 11:17:22 +08:00
def issues_status_score(user,project=nil)
if project.nil?
#Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
2014-08-19 17:03:26 +08:00
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id) AS m_count FROM users WHERE users.id = '#{user.id}'")
2014-08-13 11:17:22 +08:00
else
#Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
2014-08-19 17:03:26 +08:00
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id and issues.project_id = '#{project.id}') AS m_count FROM users WHERE users.id = '#{user.id}'")
2014-08-13 11:17:22 +08:00
end
end
#更新对留言的回复数量
def update_replay_for_message(user,type,project=nil)
option_number = get_option_number(user,type,project)
option_number.replay_for_message = replay_for_message_num(user,project)#JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id}").count
update_score(option_number)
end
def replay_for_message_num(user,project=nil)
if project.nil?
2014-08-19 17:03:26 +08:00
#JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project'").count
users = JournalsForMessage.find_by_sql("SELECT COUNT(*) as m_count From #{JournalsForMessage.table_name} WHERE m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project'")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
2014-08-19 17:03:26 +08:00
#JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project' and jour_id = '#{project.id}'").count
users = JournalsForMessage.find_by_sql("SELECT COUNT(*) as m_count From #{JournalsForMessage.table_name} WHERE m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project' and jour_id = '#{project.id}'")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
end
2014-07-29 10:06:48 +08:00
end
2014-08-13 11:17:22 +08:00
def replay_for_message_score(user,project=nil)
if project.nil?
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) From journals_for_messages WHERE m_parent_id IS NOT NULL and user_id = users.id and jour_type = 'Project') as m_score FROM users WHERE users.id = '#{user.id}'")
else
2014-08-19 17:03:26 +08:00
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) From journals_for_messages WHERE m_parent_id IS NOT NULL and user_id = users.id and jour_type = 'Project' and jour_id = '#{project.id}') as m_score FROM users WHERE users.id = '#{user.id}'")
2014-08-13 11:17:22 +08:00
end
end
#更新对帖子的回复数量
def update_replay_for_memo(user,type,project=nil)
option_number = get_option_number(user,type,project)
option_number.replay_for_memo = replay_for_memo_num(user,project)#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
2014-07-29 10:06:48 +08:00
def replay_for_memo_num(user,project=nil)
2014-07-29 18:04:35 +08:00
if project.nil?
2014-08-19 17:03:26 +08:00
#Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NOT NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id != -1").all.count
users = Message.find_by_sql("SELECT COUNT(*) as m_count FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = #{user.id} AND boards.project_id != -1")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
else
2014-08-19 17:03:26 +08:00
#Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NOT NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id = #{project.id}").all.count
users = Message.find_by_sql("SELECT COUNT(*) as m_count FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = #{user.id} AND boards.project_id = #{project.id}")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
end
2014-07-29 10:06:48 +08:00
end
2014-08-13 11:17:22 +08:00
def replay_for_memo_score(user,project=nil)
if project.nil?
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = users.id AND boards.project_id != -1) FROM users WHERE users.id = #{user.id}")
else
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = users.id AND boards.project_id = #{project.id}) FROM users WHERE users.id = #{user.id}")
end
end
#更新被关注的人数
def update_follow(user,type)
option_number = get_option_number(user,type)
2014-08-19 17:03:26 +08:00
option_number.follow = follow_num(user)#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-29 18:04:35 +08:00
def follow_num(user)
2014-08-19 17:03:26 +08:00
#Watcher.includes(:watchable).where("watchable_type = 'Principal' and watchable_id = '#{user.id}'").count
users = Watcher.find_by_sql("SELECT COUNT(*) as m_count FROM #{Watcher.table_name} WHERE watchable_type = 'Principal' and watchable_id = #{user.id}")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 10:06:48 +08:00
end
2014-08-13 11:17:22 +08:00
def follow_score(user)
2014-08-19 17:03:26 +08:00
User.find_by_sql("SELECT users.id, (SELECT COUNT(*) * 2 FROM #{Watcher.table_name} WHERE watchable_type = 'Principal' and watchable_id = users.id) FROM users WHERE users.id = '#{user.id}'")
2014-08-13 11:17:22 +08:00
end
2014-07-24 18:05:37 +08:00
#更新帖子踩各项数量
def update_tread(user,type,project=nil)
option_number = get_option_number(user,type,project)
tread_nums = tread_num(user,project)
option_number.tread = tread_nums[:tread]
option_number.tread_by_one = tread_nums[:tead_by_one]
option_number.tread_by_two = tread_nums[:tread_by_two]
option_number.tread_by_three = tread_nums[:tread_by_three]
2014-07-24 18:05:37 +08:00
update_score(option_number)
end
2014-07-29 10:06:48 +08:00
def tread_num(user,project=nil)
2014-07-29 18:04:35 +08:00
if project.nil?
2014-08-08 13:47:02 +08:00
result0 = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 0 and user_id = '#{user.id}'").all.count
pts = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 0").all
2014-07-29 18:04:35 +08:00
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
2014-08-13 11:17:22 +08:00
#project = pt.project
2014-08-08 13:47:02 +08:00
if level == 1 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result << pt
2014-08-08 13:47:02 +08:00
elsif level == 2 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result1 << pt
2014-08-08 13:47:02 +08:00
elsif level == 3 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result2 << pt
end
2014-07-29 10:06:48 +08:00
end
2014-08-08 13:47:02 +08:00
{:tread=>result0,:tead_by_one => result.count,:tread_by_two => result1.count,:tread_by_three =>result2.count}
2014-07-29 18:04:35 +08:00
else
2014-08-08 13:47:02 +08:00
result0 = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 0 and user_id = '#{user.id}'").all.count
pts = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 0").all
2014-07-29 18:04:35 +08:00
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)
2014-08-13 11:17:22 +08:00
if obj.nil? || (pt.praise_tread_object_type == "Issue" && obj.project.nil?) || (pt.praise_tread_object_type == "Message" && obj.board.nil?) || (pt.praise_tread_object_type == "Message" && obj.board.project.nil?)
2014-07-29 18:04:35 +08:00
next
end
if !(pt.praise_tread_object_type == "Issue" && obj.project.id == project.id) && !(pt.praise_tread_object_type == "Message" && obj.board.project.id == project.id)
next
end
target_user = obj.author
level = UserLevels.get_level(pt.user)#pt.user.get_level
2014-08-13 11:17:22 +08:00
#project = pt.project
2014-08-08 13:47:02 +08:00
if level == 1 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result << pt
2014-08-08 13:47:02 +08:00
elsif level == 2 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result1 << pt
2014-08-08 13:47:02 +08:00
elsif level == 3 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result2 << pt
end
2014-07-29 10:06:48 +08:00
end
2014-08-08 13:47:02 +08:00
{:tread=>result0,:tead_by_one => result.count,:tread_by_two => result1.count,:tread_by_three =>result2.count}
2014-07-29 10:06:48 +08:00
end
2014-07-29 18:04:35 +08:00
2014-07-29 10:06:48 +08:00
end
2014-07-24 18:05:37 +08:00
#更新帖子顶数量
def update_praise(user,type,project=nil)
option_number = get_option_number(user,type,project)
praise_nums = praise_num(user,project)
option_number.praise_by_one = praise_nums[:praise_by_one]
option_number.praise_by_two = praise_nums[:praise_by_two]
option_number.praise_by_three = praise_nums[:praise_by_three]
2014-07-24 18:05:37 +08:00
update_score(option_number)
end
2014-07-29 10:06:48 +08:00
def praise_num(user,project=nil)
2014-08-01 14:08:38 +08:00
if !project.nil?
2014-08-08 13:47:02 +08:00
pts = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 1").all
2014-07-29 18:04:35 +08:00
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)
2014-08-13 11:17:22 +08:00
if obj.nil? || (pt.praise_tread_object_type == "Issue" && obj.project.nil?) || (pt.praise_tread_object_type == "Message" && obj.board.nil?) || (pt.praise_tread_object_type == "Message" && obj.board.project.nil?)
2014-07-29 18:04:35 +08:00
next
end
if !(pt.praise_tread_object_type == "Issue" && obj.project.id == project.id) && !(pt.praise_tread_object_type == "Message" && obj.board.project.id == project.id)
next
end
target_user = obj.author
level = UserLevels.get_level(pt.user)#pt.user.get_level
2014-08-13 11:17:22 +08:00
#project = pt.project
2014-08-08 13:47:02 +08:00
if level == 1 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result << pt
2014-08-08 13:47:02 +08:00
elsif level == 2 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result1 << pt
2014-08-08 13:47:02 +08:00
elsif level == 3 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result2 << pt
end
2014-07-29 10:06:48 +08:00
end
2014-08-08 13:47:02 +08:00
{:praise_by_one => result.count,:praise_by_two => result1.count,:praise_by_three => result2.count}
2014-07-29 18:04:35 +08:00
else
2014-08-08 13:47:02 +08:00
pts = PraiseTread.where("praise_tread_object_type IN ( 'Issue','Message') and praise_or_tread = 1").all
2014-07-29 18:04:35 +08:00
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
#if obj.project.id == -1
# next
#end
2014-07-29 18:04:35 +08:00
target_user = obj.author
level = UserLevels.get_level(pt.user)#pt.user.get_level
project = pt.project
2014-08-08 13:47:02 +08:00
if level == 1 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result << pt
2014-08-08 13:47:02 +08:00
elsif level == 2 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result1 << pt
2014-08-08 13:47:02 +08:00
elsif level == 3 && target_user.id == user.id
2014-07-29 18:04:35 +08:00
result2 << pt
end
2014-07-29 10:06:48 +08:00
end
2014-08-08 13:47:02 +08:00
{:praise_by_one => result.count,:praise_by_two => result1.count,:praise_by_three => result2.count}
2014-07-29 10:06:48 +08:00
end
2014-07-29 18:04:35 +08:00
2014-07-29 10:06:48 +08:00
end
2014-07-24 18:05:37 +08:00
#更新提交代码次数
def update_changeset(user,type,project=nil)
option_number = get_option_number(user,type,project)
option_number.changeset = changeset_num(user,project)#Changeset.includes(:user).where("user_id = '#{user.id}'").all.count
2014-07-24 18:05:37 +08:00
update_score(option_number)
end
2014-07-29 10:06:48 +08:00
def changeset_num(user,project=nil)
2014-07-29 18:04:35 +08:00
if project.nil?
2014-08-19 17:03:26 +08:00
#Changeset.includes(:user).where("user_id = '#{user.id}'").all.count
users = Changeset.find_by_sql("SELECT COUNT(*) as m_count FROM #{Changeset.table_name} WHERE user_id = #{user.id}")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
else
2014-08-19 17:03:26 +08:00
#Changeset.includes(:user).joins(:repository).where("#{Changeset.table_name}.user_id = '#{user.id}' and #{Repository.table_name}.project_id = #{project.id}").all.count
users = Changeset.find_by_sql("SELECT COUNT(*) as m_count FROM #{Changeset.table_name} JOIN #{Repository.table_name} ON #{Changeset.table_name}.repository_id = #{Repository.table_name}.id WHERE #{Changeset.table_name}.user_id = #{user.id} and #{Repository.table_name}.project_id = #{project.id}")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
end
2014-07-29 10:06:48 +08:00
end
2014-07-24 18:05:37 +08:00
#更新文档提交次数
def update_document(user,type,project=nil)
option_number = get_option_number(user,type,project)
option_number.document = document_num(user,project)#Document.includes(:user).where("user_id = '#{user.id}'").all.count
2014-07-24 18:05:37 +08:00
update_score(option_number)
end
2014-08-01 14:08:38 +08:00
def document_num(user,project=nil)
2014-07-29 18:04:35 +08:00
if project.nil?
2014-08-19 17:03:26 +08:00
#Document.includes(:user).where("user_id = '#{user.id}'").all.count
users = Document.find_by_sql("SELECT COUNT(*) as m_count FROM #{Document.table_name} WHERE #{Document.table_name}.user_id = #{user.id}")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
else
2014-08-19 17:03:26 +08:00
#Document.includes(:user,:project).where("user_id = '#{user.id}' and project_id = '#{project.id}'").all.count
users = Document.find_by_sql("SELECT COUNT(*) as m_count FROM #{Document.table_name} WHERE #{Document.table_name}.user_id = #{user.id} and #{Document.table_name}.project_id = '#{project.id}'")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
end
2014-07-29 10:06:48 +08:00
end
2014-07-24 18:05:37 +08:00
#更新附件提交数量
def update_attachment(user,type,project=nil)
option_number = get_option_number(user,type,project)
option_number.attachment = attachment_num(user,project)#Attachment.includes(:author).where("author_id = '#{user.id}'").all.count
2014-07-24 18:05:37 +08:00
update_score(option_number)
end
2014-08-01 14:08:38 +08:00
2014-07-29 10:06:48 +08:00
def attachment_num(user,project=nil)
2014-07-29 18:04:35 +08:00
if project.nil?
2014-08-19 17:03:26 +08:00
#Attachment.includes(:author,:container).where("author_id = '#{user.id}' and container_type = 'Project'").all.count
users = Attachment.find_by_sql("SELECT COUNT(*) as m_count FROM #{Attachment.table_name} WHERE author_id = '#{user.id}' and container_type = 'Project'")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-07-29 18:04:35 +08:00
else
2014-08-19 17:03:26 +08:00
#Attachment.includes(:author,:container).where("author_id = '#{user.id}' and container_type = 'Project' and container_id = #{project.id}").all.count
users = Attachment.find_by_sql("SELECT COUNT(*) as m_count FROM #{Attachment.table_name} WHERE author_id = '#{user.id}' and container_type = 'Project' and container_id = #{project.id}")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
2014-08-22 11:58:12 +08:00
result = users.first.m_count
2014-08-19 17:03:26 +08:00
end
result
2014-07-29 18:04:35 +08:00
end
2014-07-29 10:06:48 +08:00
end
2014-07-24 18:05:37 +08:00
#更新缺陷完成度次数
def update_issue_done_ratio(user,type,project=nil)
option_number = get_option_number(user,type,project)
option_number.issue_done_ratio = issue_done_ratio_num(user,project) #Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
2014-07-24 18:05:37 +08:00
update_score(option_number)
end
2014-08-01 14:08:38 +08:00
def issue_done_ratio_num(user,project=nil)
if project.nil?
2014-08-19 17:03:26 +08:00
#Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM #{Journal.table_name} JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id WHERE #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{Journal.table_name}.user_id = #{user.id}")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-08-01 14:08:38 +08:00
else
2014-08-19 17:03:26 +08:00
#Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
users = Journal.find_by_sql("SELECT COUNT(*) as m_count FROM #{Journal.table_name} JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{Journal.table_name}.user_id = #{user.id} and #{Issue.table_name}.project_id = '#{project.id}'")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-08-01 14:08:38 +08:00
end
2014-07-29 10:06:48 +08:00
end
2014-07-24 18:05:37 +08:00
#更新发布缺陷次数
def update_post_issue(user,type,project=nil)
option_number = get_option_number(user,type,project)
option_number.post_issue = post_issue_num(user,project) #Issue.includes(:author).where("author_id = '#{user.id}'").all.count
2014-07-24 18:05:37 +08:00
update_score(option_number)
end
2014-07-29 18:04:35 +08:00
2014-07-29 10:06:48 +08:00
def post_issue_num(user,project=nil)
2014-08-01 14:08:38 +08:00
if project.nil?
2014-08-19 17:03:26 +08:00
#Issue.includes(:author).where("author_id = '#{user.id}'").all.count
users = Issue.find_by_sql("SELECT COUNT(*) as m_count FROM #{Issue.table_name} WHERE author_id = #{user.id}")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-08-01 14:08:38 +08:00
else
2014-08-19 17:03:26 +08:00
#Issue.includes(:author).where("author_id = '#{user.id}' and project_id = '#{project.id}'").all.count
users = Issue.find_by_sql("SELECT COUNT(*) as m_count FROM #{Issue.table_name} WHERE author_id = #{user.id} and project_id = '#{project.id}'")
2014-08-19 17:03:26 +08:00
result = 0
if users.count > 0
result = users.first.m_count
end
result
2014-08-01 14:08:38 +08:00
end
end
#读取项目成员得分
def read_user_project_scores(user,project)
option_num = get_option_number(user,2,project)
option_num.total_score
end
def user_scores(user,type,project=nil)
ooption_num = get_option_number(user,type,project)
update_memo_number(user,type,project)
update_messges_for_issue(user,type,project)
update_issues_status(user,type,project)
update_replay_for_message(user,type,project)
update_replay_for_memo(user,type,project)
update_tread(user,type,project)
update_praise(user,type,project)
update_changeset(user,type,project)
update_document(user,type,project)
update_attachment(user,type,project)
update_issue_done_ratio(user,type,project)
update_post_issue(user,type,project)
2014-08-19 17:03:26 +08:00
if project.nil?
update_follow(user,type)
end
update_score(ooption_num)
ooption_num
2014-07-29 10:06:48 +08:00
end
2014-04-21 11:13:45 +08:00
end