diff --git a/Gemfile b/Gemfile index 2ca1739..1f0a01b 100644 --- a/Gemfile +++ b/Gemfile @@ -50,3 +50,6 @@ end # badges for our users gem 'merit' + +# I like ambry for storing some static data +gem 'ambry' diff --git a/app/models/merit/badge_rules.rb b/app/models/merit/badge_rules.rb index 6e04326..a9a0ebb 100644 --- a/app/models/merit/badge_rules.rb +++ b/app/models/merit/badge_rules.rb @@ -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 @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 19c4f87..cf8348b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/config/initializers/merit.rb b/config/initializers/merit.rb index 8f39b9e..2d3f107 100644 --- a/config/initializers/merit.rb +++ b/config/initializers/merit.rb @@ -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' +})