trabajo-afectivo/app/models/karma/user.rb

48 lines
1.1 KiB
Ruby
Raw Permalink Normal View History

2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
2016-07-10 22:47:21 +00:00
class Karma::User < ApplicationModel
self.table_name = 'karma_users'
def self.sync(user)
score = Karma.score_by_user(user)
level = level_by_score(score)
record = Karma::User.find_by(user_id: user.id)
if record
return record if record.score == score && record.level == level
2016-07-10 22:47:21 +00:00
record.score = score
record.level = level
record.save
return record
end
Karma::User.create!(
2016-07-10 22:47:21 +00:00
user_id: user.id,
level: level,
score: score,
2016-07-10 22:47:21 +00:00
)
end
def self.by_user(user)
record = Karma::User.find_by(user_id: user.id)
return record if record
2016-07-10 22:47:21 +00:00
sync(user)
end
def self.level_by_score(score)
level = nil
karma_levels = Setting.get('karma_levels')
karma_levels.each do |local_level|
2016-07-10 22:47:21 +00:00
if !level
level = local_level[:name]
end
next if local_level[:start] && score < local_level[:start]
next if local_level[:end] && score > local_level[:end]
2016-07-10 22:47:21 +00:00
level = local_level[:name]
end
2016-07-10 22:47:21 +00:00
level
end
end