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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-07-10 22:47:21 +00:00
|
|
|
record.score = score
|
|
|
|
record.level = level
|
|
|
|
record.save
|
|
|
|
return record
|
|
|
|
end
|
2018-05-08 10:10:19 +00:00
|
|
|
Karma::User.create!(
|
2016-07-10 22:47:21 +00:00
|
|
|
user_id: user.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
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')
|
2017-10-01 12:25:52 +00:00
|
|
|
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]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-07-10 22:47:21 +00:00
|
|
|
level = local_level[:name]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-07-10 22:47:21 +00:00
|
|
|
level
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|