socialforge/app/models/user_levels.rb

68 lines
1.9 KiB
Ruby
Raw Normal View History

#用户等级类 - by zjc
class UserLevels < ActiveRecord::Base
attr_accessible :user_id, :level
belongs_to :user
#获取user的等级 -by zjc
def self.get_level(user)
unless user.nil?
if user.level.nil?
UserLevels.update_user_level(user)
end
user.level.level
end
end
#更新用户的等级 -by zjc
def self.update_user_level(user)
unless user.nil?
user_level = user.level.nil? ? UserLevels.new : user.level
user_level.user = user
#判断user的等级
pis = user.project_infos #ProjectInfo.find_all_by_user_id(self.id)
isManager = false;
pis.each do |pi|
#判断是否为项目管理员
if user.allowed_to?({:controller => "projects", :action => "edit"}, pi.project)
isManager = true;
end
end
has_effective_praise_count = false;
user.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
user.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 || user.changesets.count > 100
user_level.level = 3
elsif (user.changesets.count > 1 && user.changesets.count <= 100) || has_effective_praise_count
user_level.level = 2
else
user_level.level = 1
end
user_level.save
user.reload
end
end
end