Module: Decidim::Gamification

Defined in:
decidim-core/app/models/decidim/gamification/badge_score.rb,
decidim-core/app/controllers/decidim/gamification/badges_controller.rb,
decidim-core/lib/decidim/gamification.rb,
decidim-core/lib/decidim/gamification/badge.rb,
decidim-core/lib/decidim/gamification/base_event.rb,
decidim-core/lib/decidim/gamification/badge_scorer.rb,
decidim-core/lib/decidim/gamification/badge_status.rb,
decidim-core/lib/decidim/gamification/badge_registry.rb,
decidim-core/lib/decidim/gamification/level_up_event.rb,
decidim-core/lib/decidim/gamification/badge_earned_event.rb

Defined Under Namespace

Classes: Badge, BadgeEarnedEvent, BadgeRegistry, BadgeScore, BadgeScorer, BadgeStatus, BadgesController, BaseEvent, LevelUpEvent

Class Method Summary collapse

Class Method Details

.badge_registryObject

Semi-private: The BadgeRegistry to register global badges to.


64
65
66
# File 'decidim-core/lib/decidim/gamification.rb', line 64

def self.badge_registry
  @badge_registry ||= Decidim::Gamification::BadgeRegistry.new
end

.badgesObject

Public: Returns all available badges.

Returns an Array<Badge>


71
72
73
# File 'decidim-core/lib/decidim/gamification.rb', line 71

def self.badges
  badge_registry.all
end

.decrement_score(user, badge_name, amount = 1) ⇒ Object

Public: Decrement the score of a user for a badge.

user - A User for whom to increase the score. badge_name - The name of the badge for which to increase the score. amount - (Optional) The amount to decrease. Defaults to 1.

Returns nothing.


43
44
45
46
47
48
# File 'decidim-core/lib/decidim/gamification.rb', line 43

def self.decrement_score(user, badge_name, amount = 1)
  return unless amount.positive?
  return unless user.is_a?(Decidim::UserBaseEntity)

  BadgeScorer.new(user, find_badge(badge_name)).decrement(amount)
end

.find_badge(name) ⇒ Object

Public: Finds a Badge given a name.

Returns a Badge if found, nil otherwise.


78
79
80
# File 'decidim-core/lib/decidim/gamification.rb', line 78

def self.find_badge(name)
  badge_registry.find(name)
end

.increment_score(user, badge_name, amount = 1) ⇒ Object

Public: Increments the score of a user for a badge.

user - A User for whom to increase the score. badge_name - The name of the badge for which to increase the score. amount - (Optional) The amount to increase. Defaults to 1.

Returns nothing.


29
30
31
32
33
34
# File 'decidim-core/lib/decidim/gamification.rb', line 29

def self.increment_score(user, badge_name, amount = 1)
  return unless amount.positive?
  return unless user.is_a?(Decidim::UserBaseEntity)

  BadgeScorer.new(user, find_badge(badge_name)).increment(amount)
end

.register_badge(name, &block) ⇒ Object

Public: Registers a new Badge.

Example:

Decidim.register_badge(:foo) do |badge|
  badge.levels = [1, 10, 50]
end

Returns nothing if registered successfully, raises an exception otherwise.


92
93
94
# File 'decidim-core/lib/decidim/gamification.rb', line 92

def self.register_badge(name, &block)
  badge_registry.register(name, &block)
end

.reset_badges(users = nil) ⇒ Object

Public: Resets all the badge scores using each of the badges' reset methods (if available). This is useful if the badges ever get inconsistent.

users - The Array of Users to reset the score.

Returns nothing.


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'decidim-core/lib/decidim/gamification.rb', line 103

def self.reset_badges(users = nil)
  return reset_badges(User.all) && reset_badges(UserGroup.all) unless users

  badges.each do |badge|
    Rails.logger.info "Resetting #{badge.name}..."

    if badge.reset
      users.find_each do |user|
        set_score(user, badge.name, badge.reset.call(user)) if badge.valid_for?(user)
      end
    else
      Rails.logger.info "Badge can't be reset since it doesn't have a reset method."
    end
  end
end

.set_score(user, badge_name, score) ⇒ Object

Public: Sets the score of a user for a badge.

user - A User for whom to set the score. badge_name - The name of the badge for which to increase the score. score - The score to set.

Returns nothing.


57
58
59
60
61
# File 'decidim-core/lib/decidim/gamification.rb', line 57

def self.set_score(user, badge_name, score)
  return unless user.is_a?(Decidim::UserBaseEntity)

  BadgeScorer.new(user, find_badge(badge_name)).set(score)
end

.status_for(user, badge_name) ⇒ Object

Public: Returns a the status of a badge given a user and a badge name.

Returns a `BadgeStatus` instance.


16
17
18
19
20
# File 'decidim-core/lib/decidim/gamification.rb', line 16

def self.status_for(user, badge_name)
  return unless user.is_a?(Decidim::UserBaseEntity)

  BadgeStatus.new(user, find_badge(badge_name))
end