课程讨论区踩顶帖子

会员等级计算(占位实现自动升级)
技术得分计算
This commit is contained in:
z9hang 2014-06-12 15:12:55 +08:00
parent a8670ba6fc
commit ebbd4b0642
3 changed files with 92 additions and 5 deletions

View File

@ -122,6 +122,10 @@ class User < Principal
#####
has_many :shares ,:dependent => :destroy
# add by zjc
has_one :level, :class_name => 'UserLevels', :dependent => :destroy
has_many :memos , :foreign_key => 'author_id'
#####
scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") }
scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
scope :visible, lambda {|*args|
@ -876,6 +880,63 @@ class User < Principal
end
end
#获取user的等级 -by zjc
def get_level
if self.level.nil?
update_user_level
end
self.level.level
end
#更新用户等级 - by zjc
def update_user_level
user_level = UserLevels.new
user_level.user = self
#判断user的等级
pis = self.project_infos #ProjectInfo.find_all_by_user_id(self.id)
isManager = false;
pis.each do |pi|
#判断是否为项目管理员
if self.allowed_to?({:controller => "projects", :action => "edit"}, pi.project)
isManager = true;
end
end
has_effective_praise_count = false;
self.messages.each do |message|
if message.parent_id.nil?
ptcs = PraiseTreadCache.find_all_by_object_id(message.id)
ptcs.each do |ptc|
if ptc.object_type == 'Message' && ptc.praise_num.to_i > 5
has_effective_praise_count = true
end
end
end
end
if !has_effective_praise_count
self.memos do |memo|
if memo.parent_id.nil?
ptcs = PraiseTreadCache.find_all_by_object_id(memo.id)
ptcs.each do |ptc|
if ptc.object_type == 'Memo' && ptc.praise_num > 5
has_effective_praise_count = true
end
end
end
end
end
if isManager || self.changesets.count > 100
user_level.level = 3
elsif (self.changesets.count > 1 && self.changesets.count <= 100) || has_effective_praise_count
user_level.level = 2
else
user_level.level = 1
end
user_level.save
self.reload
end
protected
def validate_password_length
@ -884,7 +945,6 @@ class User < Principal
errors.add(:password, :too_short, :count => Setting.password_min_length.to_i)
end
end
private
def act_as_activity
@ -926,6 +986,8 @@ class User < Principal
Redmine::Utils.random_hex(16)
end
end
class AnonymousUser < User

View File

@ -0,0 +1,5 @@
#用户等级类 - by zjc
class UserLevels < ActiveRecord::Base
attr_accessible :user_id, :level
belongs_to :user
end

View File

@ -150,12 +150,32 @@ class UserScore < ActiveRecord::Base
target_user.reload
target_user_score = target_user.user_score_attr
end
target_user_score.skill = target_user_score.skill.to_i - 2 #帖子被踩扣分
target_user_score.save
level = current_user.get_level
if level == 1
target_user_score.skill = target_user_score.skill.to_i - 2 #帖子被一级会员踩-2分
target_user_score.save
elsif level == 2
target_user_score.skill = target_user_score.skill.to_i - 4 #帖子被二级会员踩-4分
target_user_score.save
elsif level == 3
target_user_score.skill = target_user_score.skill.to_i - 6 #帖子被三级会员踩-6分
target_user_score.save
end
Rails.logger.info "[UserScore#skill] ===> User: #{current_user} treaded_by #{target_user}. options => (#{options.to_s})"
when :praised_by_user # current_user 顶了 target_user 的帖子
target_user_score.skill = target_user_score.skill.to_i + 2 #帖子被顶加分
target_user_score.save
level = current_user.get_level
if level == 1
target_user_score.skill = target_user_score.skill.to_i + 4 #帖子被一级会员顶+4分
target_user_score.save
elsif level == 2
target_user_score.skill = target_user_score.skill.to_i + 6 #帖子被二级会员顶+6分
target_user_score.save
elsif level == 3
target_user_score.skill = target_user_score.skill.to_i + 8 #帖子被三级会员顶+8分
target_user_score.save
end
Rails.logger.info "[UserScore#skill] ===> User: #{current_user} praised_by #{target_user}. options => (#{options.to_s})"
else
Rails.logger.error "[UserScore#skill] ===> #{operate} is not define."