Commit f54c5d4426c06106e3d77d38068bbf02caa9f04a

Authored by Victor Costa
1 parent 689bc940
Exists in master

Add an option to disable gamification for a profile

controllers/gamification_plugin_myprofile_controller.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +class GamificationPluginMyprofileController < MyProfileController
  2 +
  3 + def enable_gamification
  4 + toggle_gamification(false)
  5 + end
  6 +
  7 + def disable_gamification
  8 + toggle_gamification(true)
  9 + end
  10 +
  11 + protected
  12 +
  13 + def toggle_gamification(disabled = false)
  14 + current_person.update_attribute(:gamification_plugin_disabled, disabled)
  15 + redirect_to :controller => 'profile_editor', :action => 'edit'
  16 + end
  17 +
  18 +end
... ...
controllers/gamification_plugin_profile_controller.rb
1 1 class GamificationPluginProfileController < ProfileController
2 2  
  3 + before_filter :verify_gamification_enabled
  4 +
3 5 def dashboard
4 6 @target = profile
5 7 render 'gamification/dashboard'
6 8 end
7 9  
  10 + protected
  11 +
  12 + def verify_gamification_enabled
  13 + render_access_denied if profile.gamification_plugin_disabled
  14 + end
  15 +
8 16 end
... ...
db/migrate/20160222091930_add_gamification_disabled_field_to_profile.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class AddGamificationDisabledFieldToProfile < ActiveRecord::Migration
  2 + def change
  3 + add_column :profiles, :gamification_plugin_disabled, :boolean, :default => false
  4 + end
  5 +end
... ...
lib/gamification_plugin.rb
... ... @@ -14,7 +14,7 @@ class GamificationPlugin &lt; Noosfero::Plugin
14 14  
15 15 def user_data_extras
16 16 proc do
17   - current_person.present? ? {:gamification_plugin => {:points => current_person.points, :badges => current_person.badges, :level => current_person.level}} : {}
  17 + current_person.present? && !current_person.gamification_plugin_disabled ? {:gamification_plugin => {:points => current_person.points, :badges => current_person.badges, :level => current_person.level}} : {}
18 18 end
19 19 end
20 20  
... ... @@ -45,6 +45,12 @@ class GamificationPlugin &lt; Noosfero::Plugin
45 45 end
46 46 end
47 47  
  48 + def profile_info_extra_contents
  49 + proc do
  50 + render :partial => 'gamification/gamification_profile_settings'
  51 + end
  52 + end
  53 +
48 54 def stylesheet?
49 55 true
50 56 end
... ...
lib/gamification_plugin/dashboard_helper.rb
... ... @@ -25,10 +25,15 @@ module GamificationPlugin::DashboardHelper
25 25 point.undo_rule? ? 'undo_action':'do_action'
26 26 end
27 27  
28   - def ranking(target, from_date=nil, limit=10)
  28 + def calc_ranking(target, from_date=nil, limit=10)
29 29 # FIXME move these queries to profile model
30 30 ranking = Profile.select('profiles.*, sum(num_points) as gamification_points, ROW_NUMBER() OVER(order by sum(num_points) DESC) as gamification_position').joins(:sash => {:scores => :score_points}).where(:type => target.class).reorder('sum(num_points) DESC').group('profiles.id')
31 31 ranking = ranking.where("merit_score_points.created_at >= ?", from_date) if from_date.present?
  32 + ranking.where(gamification_plugin_disabled: false)
  33 + end
  34 +
  35 + def ranking(target, from_date=nil, limit=10)
  36 + ranking = calc_ranking(target, from_date, limit)
32 37 target_ranking = Profile.from("(#{ranking.to_sql}) profiles").where('profiles.id' => target.id).first
33 38  
34 39 context_ranking = []
... ...
test/functional/gamification_plugin_myprofile_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +require_relative '../test_helper'
  2 +
  3 +class GamificationPluginMyprofileControllerTest < ActionController::TestCase
  4 +
  5 + def setup
  6 + @person = create_user('person').person
  7 + @environment = Environment.default
  8 + login_as(@person.identifier)
  9 + end
  10 +
  11 + attr_accessor :person
  12 +
  13 + should 'disable gamification for profile' do
  14 + person.update_attribute(:gamification_plugin_disabled, false)
  15 + post :disable_gamification, profile: person.identifier
  16 + assert person.reload.gamification_plugin_disabled
  17 + end
  18 +
  19 + should 'enable gamification for profile' do
  20 + person.update_attribute(:gamification_plugin_disabled, true)
  21 + post :enable_gamification, profile: person.identifier
  22 + assert !person.reload.gamification_plugin_disabled
  23 + end
  24 +
  25 +end
... ...
test/functional/gamification_plugin_profile_controller_test.rb
... ... @@ -37,4 +37,10 @@ class GamificationPluginProfileControllerTest &lt; ActionController::TestCase
37 37 assert_select '.badges .badge-list li.badge', 1
38 38 end
39 39  
  40 + should 'not display gamification dashboard when an user disabled the gamification for his profile' do
  41 + person.update_attribute(:gamification_plugin_disabled, true)
  42 + get :dashboard, :profile => person.identifier
  43 + assert_response :forbidden
  44 + end
  45 +
40 46 end
... ...
test/functional/profile_editor_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,26 @@
  1 +require_relative '../test_helper'
  2 +
  3 +class ProfileEditorControllerTest < ActionController::TestCase
  4 +
  5 + def setup
  6 + @person = create_user('person').person
  7 + @environment = Environment.default
  8 + @environment.enable_plugin(GamificationPlugin)
  9 + login_as(@person.identifier)
  10 + end
  11 +
  12 + attr_accessor :person
  13 +
  14 + should 'display button to enable gamification when it is disabled' do
  15 + person.update_attribute(:gamification_plugin_disabled, true)
  16 + get :edit, profile: person.identifier
  17 + assert_select '#gamification_enable'
  18 + end
  19 +
  20 + should 'display button to disable gamification when it is enabled' do
  21 + person.update_attribute(:gamification_plugin_disabled, false)
  22 + get :edit, profile: person.identifier
  23 + assert_select '#gamification_disable'
  24 + end
  25 +
  26 +end
... ...
test/unit/dashboard_helper_test.rb
1 1 require_relative "../test_helper"
2 2  
3   -class DashboardHelperTest < ActiveSupport::TestCase
  3 +class DashboardHelperTest < ActionView::TestCase
4 4  
5 5 include GamificationPlugin::DashboardHelper
6 6  
  7 + def setup
  8 + @environment = Environment.default
  9 + end
  10 +
7 11 should 'return title for global badges' do
8 12 owner = Environment.new
9 13 assert_equal 'Badges', badges_title(owner)
... ... @@ -32,4 +36,16 @@ class DashboardHelperTest &lt; ActiveSupport::TestCase
32 36 assert_equal "Voter", score_point_category(point)
33 37 end
34 38  
  39 + should 'not include a profile with disabled gamification in ranking' do
  40 + author = create_user('testuserauthor').person
  41 + person1 = create_user('testuser1').person
  42 + person2 = create_user('testuser2').person
  43 + create_point_rule_definition('comment_author')
  44 + article = create(TextileArticle, :profile_id => author.id, :author_id => author.id)
  45 + create(Comment, :source => article, :author_id => person1.id)
  46 + 3.times { create(Comment, :source => article, :author_id => person2.id) }
  47 + person1.update_attribute(:gamification_plugin_disabled, true)
  48 + assert_not_includes calc_ranking(person1, Time.zone.now.at_beginning_of_week), person1
  49 + end
  50 +
35 51 end
... ...
test/unit/gamification_plugin_test.rb
... ... @@ -13,4 +13,9 @@ class GamificationPluginTest &lt; ActiveSupport::TestCase
13 13 assert_equal({:gamification_plugin => {:points => 0, :badges => [], :level => 0}}, instance_eval(&plugin.user_data_extras))
14 14 end
15 15  
  16 + should 'not return gamification data in user_data_extras when disabled' do
  17 + current_person.update_attribute(:gamification_plugin_disabled, true)
  18 + assert_equal({}, instance_eval(&plugin.user_data_extras))
  19 + end
  20 +
16 21 end
... ...
views/gamification/_gamification_profile_settings.html.erb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +<h2><%= _('Gamification Settings') %></h2>
  2 +
  3 +<% if profile.gamification_plugin_disabled %>
  4 + <%= button(:remove, _('Enable gamification'),
  5 + {:action => :enable_gamification, profile: profile.identifier, controller: :gamification_plugin_myprofile},
  6 + :id => 'gamification_enable',
  7 + :data => {:confirm=>_("Are you sure you want to activate gamification for this profile?")})
  8 + %>
  9 +<% else %>
  10 + <%= button(:remove, _('Disable gamification'),
  11 + {:action => :disable_gamification, profile: profile.identifier, controller: :gamification_plugin_myprofile},
  12 + :id => 'gamification_disable',
  13 + :data => {:confirm=>_("Are you sure you want to deactivate gamification for this profile?")})
  14 + %>
  15 +<% end %>
... ...