Commit bea83c2384fd561acb4cddf17454616dd91fabd7

Authored by Victor Costa
2 parents b22222a8 9ae72901

Merge branch 'fix_pontuation_check' into 'master'

Fix pontuation check and add check for badges

See merge request !5
lib/merit/point_rules.rb
@@ -159,6 +159,7 @@ module Merit @@ -159,6 +159,7 @@ module Merit
159 end 159 end
160 160
161 def profile_condition(setting, target, profile) 161 def profile_condition(setting, target, profile)
  162 + return false if target == true
162 profile.nil? || setting[:target_profile].blank? || setting[:target_profile].call(target) == profile 163 profile.nil? || setting[:target_profile].blank? || setting[:target_profile].call(target) == profile
163 end 164 end
164 165
script/check_merit_actions_vs_points.rb
@@ -36,8 +36,8 @@ def create_action(obj, index, count) @@ -36,8 +36,8 @@ def create_action(obj, index, count)
36 end 36 end
37 37
38 def recreate_actions person, objects, category 38 def recreate_actions person, objects, category
39 - puts "Recreating actions for #{person.identifier} on model #{objects.first.class.base_class.name}"  
40 - actions = Merit::Action.where(target_id: objects, target_model: objects.first.class.base_class.name.downcase, action_method: 'create') 39 + puts "Recreating actions for #{person.identifier} on model #{objects.name}"
  40 + actions = Merit::Action.where(target_id: objects, target_model: objects.name.downcase, action_method: 'create')
41 Merit::Score::Point.where(action_id: actions).destroy_all 41 Merit::Score::Point.where(action_id: actions).destroy_all
42 actions.destroy_all 42 actions.destroy_all
43 # erase remaining points if any (can be wrong on destroy cases ?) 43 # erase remaining points if any (can be wrong on destroy cases ?)
@@ -65,11 +65,20 @@ class Article < ActiveRecord::Base @@ -65,11 +65,20 @@ class Article < ActiveRecord::Base
65 end 65 end
66 end 66 end
67 67
68 -puts "Creaning up points from actions which don't exist" 68 +puts "Cleaning up points from actions which don't exist"
69 Merit::Score::Point.includes(:action).find_each(batch_size: 100) do |point| 69 Merit::Score::Point.includes(:action).find_each(batch_size: 100) do |point|
70 point.destroy if point.action.nil? 70 point.destroy if point.action.nil?
71 end 71 end
72 72
  73 +# erase the badges spreadsheet
  74 +CSV.open( "gamification_wrong_badges.csv", 'w' ) do |csv|
  75 + csv << ['identifier', 'missing badges', 'exceeding badges']
  76 +end
  77 +# erase the points spreadsheet
  78 +CSV.open( "gamification_points_out_expectation.csv", 'w' ) do |csv|
  79 + csv << ['identifier', 'name', 'action', 'profile', 'category id', 'category type', 'should have', 'have']
  80 +end
  81 +
73 Environment.all.each do |environment| 82 Environment.all.each do |environment|
74 puts "Process environment #{environment.name}" 83 puts "Process environment #{environment.name}"
75 84
@@ -82,12 +91,12 @@ Environment.all.each do |environment| @@ -82,12 +91,12 @@ Environment.all.each do |environment|
82 conditions = group_control.nil? ? {} : {:identifier => group_control.map{|k,v| v['profiles']}.flatten} 91 conditions = group_control.nil? ? {} : {:identifier => group_control.map{|k,v| v['profiles']}.flatten}
83 people_count = environment.people.where(conditions).count 92 people_count = environment.people.where(conditions).count
84 person_index = 0 93 person_index = 0
85 - remaining_wrong_points = []  
86 puts "Analising environment people" 94 puts "Analising environment people"
87 environment.people.find_each(:conditions => conditions) do |person| 95 environment.people.find_each(:conditions => conditions) do |person|
88 person_index += 1 96 person_index += 1
89 profile_ids = GamificationPlugin::PointsCategorization.uniq.pluck(:profile_id) 97 profile_ids = GamificationPlugin::PointsCategorization.uniq.pluck(:profile_id)
90 profile_ids.keep_if { |item| group_control.keys.include?(item) } unless group_control.nil? 98 profile_ids.keep_if { |item| group_control.keys.include?(item) } unless group_control.nil?
  99 + profile_ids.delete nil # avoid loosing time with generic for now
91 profile_ids.each do |profile_id| 100 profile_ids.each do |profile_id|
92 profile = Profile.where(id: profile_id).first 101 profile = Profile.where(id: profile_id).first
93 if profile.nil? 102 if profile.nil?
@@ -112,8 +121,34 @@ Environment.all.each do |environment| @@ -112,8 +121,34 @@ Environment.all.each do |environment|
112 follows_received = ArticleFollower.where(:article_id => person_articles) 121 follows_received = ArticleFollower.where(:article_id => person_articles)
113 122
114 puts "#{person_index}/#{people_count} - Analising points for #{person.identifier} on #{profile_name}" 123 puts "#{person_index}/#{people_count} - Analising points for #{person.identifier} on #{profile_name}"
115 - puts "Proposed #{person_articles.count} times, Commented #{comments.count} times, Voted #{votes.count} times, Followed #{follows.count} times"  
116 - puts "Received #{votes_received.count} votes, #{comments_received.count} comments, #{follows_received.count} follows\n" 124 + #puts "Proposed #{person_articles.count} times, Commented #{comments.count} times, Voted #{votes.count} times, Followed #{follows.count} times"
  125 + #puts "Received #{votes_received.count} votes, #{comments_received.count} comments, #{follows_received.count} follows\n"
  126 +
  127 + scope_by_badge_action = {
  128 + "articlefollower#create" => follows, "comment#create" => comments, "article#create" => person_articles, "vote#create" => votes
  129 + }
  130 +
  131 + # ignoring user badges out of environment badges
  132 + should_and_doesnt_have = []
  133 + should_not_have = []
  134 + should_have = true
  135 + environment.gamification_plugin_badges.each do |badge|
  136 + (badge.custom_fields || {}).each do |action, config|
  137 + break if scope_by_badge_action[action].nil? or config[:threshold].nil?
  138 + should_have &= scope_by_badge_action[action].count >= config[:threshold].to_i
  139 + end
  140 + have = person.badges.include? badge
  141 + if should_have && !have
  142 + should_and_doesnt_have << "#{badge.title} #{badge.level}"
  143 + elsif should_have && !have
  144 + should_not_have << "#{badge.title} #{badge.level}"
  145 + end
  146 + end
  147 + if should_and_doesnt_have.size > 0 || should_not_have.size > 0
  148 + CSV.open( "gamification_points_out_expectation.csv", 'a' ) do |csv|
  149 + [person.identifier, should_and_doesnt_have.join(' | '), should_not_have.join(' | ')]
  150 + end
  151 + end
117 152
118 scope_by_type = { 153 scope_by_type = {
119 article_author: person_articles, comment_author: comments, vote_voter: votes, follower: follows, 154 article_author: person_articles, comment_author: comments, vote_voter: votes, follower: follows,
@@ -128,8 +163,15 @@ Environment.all.each do |environment| @@ -128,8 +163,15 @@ Environment.all.each do |environment|
128 if points != person.points(category: c.id.to_s) 163 if points != person.points(category: c.id.to_s)
129 recreate_actions person, scope, c.id.to_s 164 recreate_actions person, scope, c.id.to_s
130 points = calc_points c, scope 165 points = calc_points c, scope
131 - puts "after recreating points the person has: #{person.reload.points(category: c.id.to_s)} and should have #{points}"  
132 - remaining_wrong_points << [person.identifier, person.name, scope.first.class.base_class.name, profile_name, c.id, c.point_type.name, scope.count*c.weight, person.points(category: c.id.to_s)] if points != person.points(category: c.id.to_s) 166 + if points != person.reload.points(category: c.id.to_s)
  167 + puts "after recreating points the person has: #{person.reload.points(category: c.id.to_s)} and should have #{points}"
  168 + # write to the spreadsheet the person points that couldn't regulate
  169 + CSV.open( "gamification_points_out_expectation.csv", 'a' ) do |csv|
  170 + [person.identifier, person.name, scope.first.class.base_class.name, profile_name, c.id, c.point_type.name, scope.count*c.weight, person.points(category: c.id.to_s)]
  171 + end
  172 + else
  173 + puts "points fixed for #{c.point_type.name}!"
  174 + end
133 end 175 end
134 end 176 end
135 puts 177 puts
@@ -142,20 +184,4 @@ Environment.all.each do |environment| @@ -142,20 +184,4 @@ Environment.all.each do |environment|
142 puts "Updating #{person.identifier} level\n" 184 puts "Updating #{person.identifier} level\n"
143 person.update_attribute(:level, person.gamification_plugin_calculate_level) 185 person.update_attribute(:level, person.gamification_plugin_calculate_level)
144 end 186 end
145 -  
146 - # write to the spreadsheet the person points that couldn't regulate  
147 - unless remaining_wrong_points.blank?  
148 - CSV.open( "gamification_points_out_expectation.csv", 'w' ) do |csv|  
149 - csv << ['identifier', 'name', 'action', 'profile', 'category id', 'category type', 'should have', 'have']  
150 - remaining_wrong_points.each do |line|  
151 - csv << line  
152 - end  
153 - end  
154 - end  
155 -  
156 - if remaining_wrong_points.count  
157 - puts "Finished. There was #{remaining_wrong_points.count} people/pontuation types with errors after check and fix. Please check the created spreadsheet."  
158 - else  
159 - puts "Finished. There was no errors after checking. \o/ Pontuation seems to be ok!"  
160 - end  
161 end 187 end