From ebbd4b0642de83afa29bd9de04c0aa37df89229c Mon Sep 17 00:00:00 2001 From: z9hang Date: Thu, 12 Jun 2014 15:12:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E8=AE=A8=E8=AE=BA=E5=8C=BA?= =?UTF-8?q?=E8=B8=A9=E9=A1=B6=E5=B8=96=E5=AD=90=20=E4=BC=9A=E5=91=98?= =?UTF-8?q?=E7=AD=89=E7=BA=A7=E8=AE=A1=E7=AE=97=EF=BC=88=E5=8D=A0=E4=BD=8D?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=87=AA=E5=8A=A8=E5=8D=87=E7=BA=A7=EF=BC=89?= =?UTF-8?q?=20=E6=8A=80=E6=9C=AF=E5=BE=97=E5=88=86=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/user.rb | 64 ++++++++++++++++++++++++++++++++++++++- app/models/user_levels.rb | 5 +++ app/models/user_score.rb | 28 ++++++++++++++--- 3 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 app/models/user_levels.rb diff --git a/app/models/user.rb b/app/models/user.rb index e8587661..b4aaea49 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user_levels.rb b/app/models/user_levels.rb new file mode 100644 index 00000000..eb8dd37e --- /dev/null +++ b/app/models/user_levels.rb @@ -0,0 +1,5 @@ +#用户等级类 - by zjc +class UserLevels < ActiveRecord::Base + attr_accessible :user_id, :level + belongs_to :user +end \ No newline at end of file diff --git a/app/models/user_score.rb b/app/models/user_score.rb index 202a5307..28837084 100644 --- a/app/models/user_score.rb +++ b/app/models/user_score.rb @@ -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."