diff --git a/controllers/gamification_plugin_myprofile_controller.rb b/controllers/gamification_plugin_myprofile_controller.rb new file mode 100644 index 0000000..b87466b --- /dev/null +++ b/controllers/gamification_plugin_myprofile_controller.rb @@ -0,0 +1,18 @@ +class GamificationPluginMyprofileController < MyProfileController + + def enable_gamification + toggle_gamification(false) + end + + def disable_gamification + toggle_gamification(true) + end + + protected + + def toggle_gamification(disabled = false) + current_person.update_attribute(:gamification_plugin_disabled, disabled) + redirect_to :controller => 'profile_editor', :action => 'edit' + end + +end diff --git a/controllers/gamification_plugin_profile_controller.rb b/controllers/gamification_plugin_profile_controller.rb index 82e29a7..56b86de 100644 --- a/controllers/gamification_plugin_profile_controller.rb +++ b/controllers/gamification_plugin_profile_controller.rb @@ -1,8 +1,16 @@ class GamificationPluginProfileController < ProfileController + before_filter :verify_gamification_enabled + def dashboard @target = profile render 'gamification/dashboard' end + protected + + def verify_gamification_enabled + render_access_denied if profile.gamification_plugin_disabled + end + end diff --git a/db/migrate/20160222091930_add_gamification_disabled_field_to_profile.rb b/db/migrate/20160222091930_add_gamification_disabled_field_to_profile.rb new file mode 100644 index 0000000..86bea62 --- /dev/null +++ b/db/migrate/20160222091930_add_gamification_disabled_field_to_profile.rb @@ -0,0 +1,5 @@ +class AddGamificationDisabledFieldToProfile < ActiveRecord::Migration + def change + add_column :profiles, :gamification_plugin_disabled, :boolean, :default => false + end +end diff --git a/lib/gamification_plugin.rb b/lib/gamification_plugin.rb index 6ce3237..b354729 100644 --- a/lib/gamification_plugin.rb +++ b/lib/gamification_plugin.rb @@ -14,7 +14,7 @@ class GamificationPlugin < Noosfero::Plugin def user_data_extras proc do - current_person.present? ? {:gamification_plugin => {:points => current_person.points, :badges => current_person.badges, :level => current_person.level}} : {} + current_person.present? && !current_person.gamification_plugin_disabled ? {:gamification_plugin => {:points => current_person.points, :badges => current_person.badges, :level => current_person.level}} : {} end end @@ -45,6 +45,12 @@ class GamificationPlugin < Noosfero::Plugin end end + def profile_info_extra_contents + proc do + render :partial => 'gamification/gamification_profile_settings' + end + end + def stylesheet? true end diff --git a/lib/gamification_plugin/dashboard_helper.rb b/lib/gamification_plugin/dashboard_helper.rb index 7ee1ca3..5a6ca1b 100644 --- a/lib/gamification_plugin/dashboard_helper.rb +++ b/lib/gamification_plugin/dashboard_helper.rb @@ -25,10 +25,15 @@ module GamificationPlugin::DashboardHelper point.undo_rule? ? 'undo_action':'do_action' end - def ranking(target, from_date=nil, limit=10) + def calc_ranking(target, from_date=nil, limit=10) # FIXME move these queries to profile model 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') ranking = ranking.where("merit_score_points.created_at >= ?", from_date) if from_date.present? + ranking.where(gamification_plugin_disabled: false) + end + + def ranking(target, from_date=nil, limit=10) + ranking = calc_ranking(target, from_date, limit) target_ranking = Profile.from("(#{ranking.to_sql}) profiles").where('profiles.id' => target.id).first context_ranking = [] diff --git a/test/functional/gamification_plugin_myprofile_controller_test.rb b/test/functional/gamification_plugin_myprofile_controller_test.rb new file mode 100644 index 0000000..96e174b --- /dev/null +++ b/test/functional/gamification_plugin_myprofile_controller_test.rb @@ -0,0 +1,25 @@ +require_relative '../test_helper' + +class GamificationPluginMyprofileControllerTest < ActionController::TestCase + + def setup + @person = create_user('person').person + @environment = Environment.default + login_as(@person.identifier) + end + + attr_accessor :person + + should 'disable gamification for profile' do + person.update_attribute(:gamification_plugin_disabled, false) + post :disable_gamification, profile: person.identifier + assert person.reload.gamification_plugin_disabled + end + + should 'enable gamification for profile' do + person.update_attribute(:gamification_plugin_disabled, true) + post :enable_gamification, profile: person.identifier + assert !person.reload.gamification_plugin_disabled + end + +end diff --git a/test/functional/gamification_plugin_profile_controller_test.rb b/test/functional/gamification_plugin_profile_controller_test.rb index 2f2f737..f2a97b3 100644 --- a/test/functional/gamification_plugin_profile_controller_test.rb +++ b/test/functional/gamification_plugin_profile_controller_test.rb @@ -37,4 +37,10 @@ class GamificationPluginProfileControllerTest < ActionController::TestCase assert_select '.badges .badge-list li.badge', 1 end + should 'not display gamification dashboard when an user disabled the gamification for his profile' do + person.update_attribute(:gamification_plugin_disabled, true) + get :dashboard, :profile => person.identifier + assert_response :forbidden + end + end diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb new file mode 100644 index 0000000..8899cc1 --- /dev/null +++ b/test/functional/profile_editor_controller_test.rb @@ -0,0 +1,26 @@ +require_relative '../test_helper' + +class ProfileEditorControllerTest < ActionController::TestCase + + def setup + @person = create_user('person').person + @environment = Environment.default + @environment.enable_plugin(GamificationPlugin) + login_as(@person.identifier) + end + + attr_accessor :person + + should 'display button to enable gamification when it is disabled' do + person.update_attribute(:gamification_plugin_disabled, true) + get :edit, profile: person.identifier + assert_select '#gamification_enable' + end + + should 'display button to disable gamification when it is enabled' do + person.update_attribute(:gamification_plugin_disabled, false) + get :edit, profile: person.identifier + assert_select '#gamification_disable' + end + +end diff --git a/test/unit/dashboard_helper_test.rb b/test/unit/dashboard_helper_test.rb index 6c508e5..f2832a1 100644 --- a/test/unit/dashboard_helper_test.rb +++ b/test/unit/dashboard_helper_test.rb @@ -1,9 +1,13 @@ require_relative "../test_helper" -class DashboardHelperTest < ActiveSupport::TestCase +class DashboardHelperTest < ActionView::TestCase include GamificationPlugin::DashboardHelper + def setup + @environment = Environment.default + end + should 'return title for global badges' do owner = Environment.new assert_equal 'Badges', badges_title(owner) @@ -32,4 +36,16 @@ class DashboardHelperTest < ActiveSupport::TestCase assert_equal "Voter", score_point_category(point) end + should 'not include a profile with disabled gamification in ranking' do + author = create_user('testuserauthor').person + person1 = create_user('testuser1').person + person2 = create_user('testuser2').person + create_point_rule_definition('comment_author') + article = create(TextileArticle, :profile_id => author.id, :author_id => author.id) + create(Comment, :source => article, :author_id => person1.id) + 3.times { create(Comment, :source => article, :author_id => person2.id) } + person1.update_attribute(:gamification_plugin_disabled, true) + assert_not_includes calc_ranking(person1, Time.zone.now.at_beginning_of_week), person1 + end + end diff --git a/test/unit/gamification_plugin_test.rb b/test/unit/gamification_plugin_test.rb index c59fc28..32da134 100644 --- a/test/unit/gamification_plugin_test.rb +++ b/test/unit/gamification_plugin_test.rb @@ -13,4 +13,9 @@ class GamificationPluginTest < ActiveSupport::TestCase assert_equal({:gamification_plugin => {:points => 0, :badges => [], :level => 0}}, instance_eval(&plugin.user_data_extras)) end + should 'not return gamification data in user_data_extras when disabled' do + current_person.update_attribute(:gamification_plugin_disabled, true) + assert_equal({}, instance_eval(&plugin.user_data_extras)) + end + end diff --git a/views/gamification/_gamification_profile_settings.html.erb b/views/gamification/_gamification_profile_settings.html.erb new file mode 100644 index 0000000..0c71e31 --- /dev/null +++ b/views/gamification/_gamification_profile_settings.html.erb @@ -0,0 +1,15 @@ +