Skip to content

Commit

Permalink
Added a bunch of badges for answering questions with cutesy names
Browse files Browse the repository at this point in the history
  • Loading branch information
kisonecat committed Jan 4, 2013
1 parent 398188d commit aa2a100
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ end

# badges for our users
gem 'merit'

# I like ambry for storing some static data
gem 'ambry'
53 changes: 42 additions & 11 deletions app/models/merit/badge_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,54 @@ class BadgeRules
include Merit::BadgeRulesMethods

def initialize
grant_on ['users#create', 'users#update'], :badge => 'autobiographer', :temporary => true do |user|
user.name.present?
grant_on ['score#attempt'], :badge => 'Hourglass', :to => :user do |score|
score.user.total_thinking_time >= 3600
end

grant_on ['score#attempt'], :badge => 'answerer', :level => 5, :to => :user do |score|
score.user.scores.count > 5
grant_on ['score#attempt'], :badge => 'Dayfly', :to => :user do |score|
score.user.total_thinking_time >= 86400
end

grant_on ['score#attempt'], :badge => 'answerer', :level => 5, :to => :user do |score|
score.user.scores.count > 5
grant_on ['score#attempt'], :badge => 'Perfect Ten', :to => :user do |score|
score.user.total_correct_answers >= 10
end

grant_on ['score#attempt'], :badge => 'Hundreder', :to => :user do |score|
score.user.total_correct_answers >= 100
end

grant_on ['score#attempt'], :badge => 'Grand Slam', :to => :user do |score|
score.user.total_correct_answers >= 1000
end

grant_on ['score#attempt'], :badge => 'Myriad', :to => :user do |score|
score.user.total_correct_answers >= 10000
end

grant_on ['score#attempt'], :badge => 'Hole in One', :to => :user do |score|
score.user.total_correct_answers_without_hints >= 1
end

grant_on ['score#attempt'], :badge => 'Four Aces', :to => :user do |score|
score.user.total_correct_answers_without_hints >= 4
end

grant_on ['score#attempt'], :badge => 'C Note', :to => :user do |score|
score.user.total_correct_answers_without_hints >= 100
end

grant_on ['score#hint'], :badge => 'Secret Agent', :to => :user do |score|
score.user.total_hints_requested >= 1000
end

grant_on ['score#hint'], :badge => 'Setec Astronomer', :to => :user do |score|
score.user.total_hints_requested >= 10000
end

#grant_on ['users#create', 'users#update'], :badge => 'autobiographer', :temporary => true do |user|
# user.name.present?
#end

# If it creates user, grant badge
# Should be "current_user" after registration for badge to be granted.
# grant_on 'users#create', :badge => 'just-registered', :to => :itself
Expand All @@ -42,11 +78,6 @@ def initialize
# comment.user.comments.count == 10
# end

# If it has 5 votes, grant relevant-commenter badge
# grant_on 'comments#vote', :badge => 'relevant-commenter', :to => :user do |comment|
# comment.votes.count == 5
# end

# Changes his name by one wider than 4 chars (arbitrary ruby code case)
# grant_on 'registrations#update', :badge => 'autobiographer', :temporary => true, :model_name => 'User' do |user|
# user.name.length > 4
Expand Down
19 changes: 19 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,23 @@ def self.find_for_coursera(access_token, signed_in_resource=nil)
end
user
end

#################################################################
# some methods for computing simple statistics for badges
def total_hints_requested
self.scores.where( :attempt_content => "hint" ).count
end

def total_correct_answers
self.scores.where( :complete => true ).count
end

def total_correct_answers_without_hints
self.scores.where( :complete => true, :count_hints => 0 ).count
end

def total_thinking_time
self.scores.sum(:time_taken)
end

end
64 changes: 61 additions & 3 deletions config/initializers/merit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,66 @@
# Create application badges (uses https://github.com/norman/ambry)
Badge.create!({
:id => 1,
:name => 'answerer',
:level => 5,
:description => 'Submitted answers'
:name => 'Hourglass',
:description => 'Spent one hour doing exercises'
})

Badge.create!({
:id => 2,
:name => 'Dayfly',
:description => 'Spent one day doing exercises'
})

Badge.create!({
:id => 3,
:name => 'Perfect Ten',
:description => 'Answered ten exercises correctly'
})

Badge.create!({
:id => 4,
:name => 'Hundreder',
:description => 'Answered 100 exercises correctly'
})

Badge.create!({
:id => 5,
:name => 'Grand Slam',
:description => 'Answered 1000 exercises correctly'
})

Badge.create!({
:id => 6,
:name => 'Myriad',
:description => 'Answered 10000 exercises correctly'
})

Badge.create!({
:id => 7,
:name => 'Hole in One',
:description => 'Answered an exercise correctly without using a hint'
})

Badge.create!({
:id => 8,
:name => 'Four Aces',
:description => 'Answered four exercises correctly without using a hint'
})

Badge.create!({
:id => 9,
:name => 'C Note',
:description => 'Answered 100 exercises correctly without using a hint'
})

Badge.create!({
:id => 10,
:name => 'Secret Agent',
:description => 'Examined one thousand hints'
})

Badge.create!({
:id => 11,
:name => 'Setec Astronomer',
:description => 'Examined ten thousand hints'
})

0 comments on commit aa2a100

Please sign in to comment.