diff --git a/controllers/gamification_plugin_profile_controller.rb b/controllers/gamification_plugin_profile_controller.rb index a67669d..82e29a7 100644 --- a/controllers/gamification_plugin_profile_controller.rb +++ b/controllers/gamification_plugin_profile_controller.rb @@ -1,8 +1,8 @@ class GamificationPluginProfileController < ProfileController - def info + def dashboard @target = profile - render 'gamification/info' + render 'gamification/dashboard' end end diff --git a/lib/ext/profile.rb b/lib/ext/profile.rb index 4681046..0e783d7 100644 --- a/lib/ext/profile.rb +++ b/lib/ext/profile.rb @@ -15,4 +15,13 @@ class Profile last_level end + def gamification_plugin_level_percent + settings = GamificationPlugin.settings(environment) + rules = settings.get_setting(:rank_rules) + return 100 if rules.blank? || rules.length < level + + next_level_points = rules[level][:points] + 100*points/next_level_points.to_f + end + end diff --git a/lib/gamification_plugin.rb b/lib/gamification_plugin.rb index 5af8f51..7b25b28 100644 --- a/lib/gamification_plugin.rb +++ b/lib/gamification_plugin.rb @@ -50,7 +50,7 @@ class GamificationPlugin < Noosfero::Plugin end def js_files - ['jquery.noty.packaged.min.js', 'main.js'] + ['jquery.noty.packaged.min.js', 'jquery.easypiechart.min.js', 'main.js'] end ActionDispatch::Reloader.to_prepare do diff --git a/lib/gamification_plugin/dashboard_helper.rb b/lib/gamification_plugin/dashboard_helper.rb new file mode 100644 index 0000000..fc0485c --- /dev/null +++ b/lib/gamification_plugin/dashboard_helper.rb @@ -0,0 +1,10 @@ +module GamificationPlugin::DashboardHelper + + def level_chart(target) + + content_tag(:div, :class => "level pie-chart", 'data-percent' => @target.gamification_plugin_level_percent) do + content_tag :span, @target.level + end + end + +end diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index 2aab7ee..1b9e773 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -11,6 +11,14 @@ module Merit :description => _('Point weight for comment author'), :default_weight => 10 }, + :comment_article_author => { + :action => 'comment#create', + :undo_action => 'comment#destroy', + :to => lambda {|comment| comment.source.author}, + :value => 1, + :description => _('Point weight for article author of a comment'), + :default_weight => 10 + }, :article_author => { :action => 'article#create', :undo_action => 'article#destroy', diff --git a/public/jquery.easypiechart.min.js b/public/jquery.easypiechart.min.js new file mode 100644 index 0000000..efa45d0 --- /dev/null +++ b/public/jquery.easypiechart.min.js @@ -0,0 +1,9 @@ +/**! + * easy-pie-chart + * Lightweight plugin to render simple, animated and retina optimized pie charts + * + * @license + * @author Robert Fleischmann (http://robert-fleischmann.de) + * @version 2.1.7 + **/ +!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("jquery")):b(jQuery)}(this,function(a){var b=function(a,b){var c,d=document.createElement("canvas");a.appendChild(d),"object"==typeof G_vmlCanvasManager&&G_vmlCanvasManager.initElement(d);var e=d.getContext("2d");d.width=d.height=b.size;var f=1;window.devicePixelRatio>1&&(f=window.devicePixelRatio,d.style.width=d.style.height=[b.size,"px"].join(""),d.width=d.height=b.size*f,e.scale(f,f)),e.translate(b.size/2,b.size/2),e.rotate((-0.5+b.rotate/180)*Math.PI);var g=(b.size-b.lineWidth)/2;b.scaleColor&&b.scaleLength&&(g-=b.scaleLength+2),Date.now=Date.now||function(){return+new Date};var h=function(a,b,c){c=Math.min(Math.max(-1,c||0),1);var d=0>=c?!0:!1;e.beginPath(),e.arc(0,0,g,0,2*Math.PI*c,d),e.strokeStyle=a,e.lineWidth=b,e.stroke()},i=function(){var a,c;e.lineWidth=1,e.fillStyle=b.scaleColor,e.save();for(var d=24;d>0;--d)d%6===0?(c=b.scaleLength,a=0):(c=.6*b.scaleLength,a=b.scaleLength-c),e.fillRect(-b.size/2+a,0,c,1),e.rotate(Math.PI/12);e.restore()},j=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(a){window.setTimeout(a,1e3/60)}}(),k=function(){b.scaleColor&&i(),b.trackColor&&h(b.trackColor,b.trackWidth||b.lineWidth,1)};this.getCanvas=function(){return d},this.getCtx=function(){return e},this.clear=function(){e.clearRect(b.size/-2,b.size/-2,b.size,b.size)},this.draw=function(a){b.scaleColor||b.trackColor?e.getImageData&&e.putImageData?c?e.putImageData(c,0,0):(k(),c=e.getImageData(0,0,b.size*f,b.size*f)):(this.clear(),k()):this.clear(),e.lineCap=b.lineCap;var d;d="function"==typeof b.barColor?b.barColor(a):b.barColor,h(d,b.lineWidth,a/100)}.bind(this),this.animate=function(a,c){var d=Date.now();b.onStart(a,c);var e=function(){var f=Math.min(Date.now()-d,b.animate.duration),g=b.easing(this,f,a,c-a,b.animate.duration);this.draw(g),b.onStep(a,c,g),f>=b.animate.duration?b.onStop(a,c):j(e)}.bind(this);j(e)}.bind(this)},c=function(a,c){var d={barColor:"#ef1e25",trackColor:"#f9f9f9",scaleColor:"#dfe0e0",scaleLength:5,lineCap:"round",lineWidth:3,trackWidth:void 0,size:110,rotate:0,animate:{duration:1e3,enabled:!0},easing:function(a,b,c,d,e){return b/=e/2,1>b?d/2*b*b+c:-d/2*(--b*(b-2)-1)+c},onStart:function(a,b){},onStep:function(a,b,c){},onStop:function(a,b){}};if("undefined"!=typeof b)d.renderer=b;else{if("undefined"==typeof SVGRenderer)throw new Error("Please load either the SVG- or the CanvasRenderer");d.renderer=SVGRenderer}var e={},f=0,g=function(){this.el=a,this.options=e;for(var b in d)d.hasOwnProperty(b)&&(e[b]=c&&"undefined"!=typeof c[b]?c[b]:d[b],"function"==typeof e[b]&&(e[b]=e[b].bind(this)));"string"==typeof e.easing&&"undefined"!=typeof jQuery&&jQuery.isFunction(jQuery.easing[e.easing])?e.easing=jQuery.easing[e.easing]:e.easing=d.easing,"number"==typeof e.animate&&(e.animate={duration:e.animate,enabled:!0}),"boolean"!=typeof e.animate||e.animate||(e.animate={duration:1e3,enabled:e.animate}),this.renderer=new e.renderer(a,e),this.renderer.draw(f),a.dataset&&a.dataset.percent?this.update(parseFloat(a.dataset.percent)):a.getAttribute&&a.getAttribute("data-percent")&&this.update(parseFloat(a.getAttribute("data-percent")))}.bind(this);this.update=function(a){return a=parseFloat(a),e.animate.enabled?this.renderer.animate(f,a):this.renderer.draw(a),f=a,this}.bind(this),this.disableAnimation=function(){return e.animate.enabled=!1,this},this.enableAnimation=function(){return e.animate.enabled=!0,this},g()};a.fn.easyPieChart=function(b){return this.each(function(){var d;a.data(this,"easyPieChart")||(d=a.extend({},b,a(this).data()),a.data(this,"easyPieChart",new c(this,d)))})}}); \ No newline at end of file diff --git a/public/main.js b/public/main.js index d04d5a3..48af284 100644 --- a/public/main.js +++ b/public/main.js @@ -22,9 +22,20 @@ var gamificationPlugin = { info.find('.level .value').text(gamificationPlugin.level); info.insertAfter('#user .logged-in #homepage-link'); info.show(); + }, + displayLevelChart: function() { + var chart = $('.gamification .pie-chart'); + var size = 60; + chart.easyPieChart({lineWidth: 10, scaleColor: false, size: size, barColor: '#1EA5C5', trackColor: '#C0EEFF'}); + chart.width(size); + chart.find('span').css('line-height', size+'px'); } } jQuery(window).bind("userDataLoaded", function(event, data) { gamificationPlugin.displayUserInfo(data.gamification_plugin); }); + +jQuery(document).ready(function($) { + gamificationPlugin.displayLevelChart(); +}); diff --git a/public/style.css b/public/style.css index c765912..75396ac 100644 --- a/public/style.css +++ b/public/style.css @@ -53,3 +53,12 @@ .gamification-notification .all-badges { font-size: 10px; } + +.gamification .pie-chart { + position: relative; + text-align: center; +} +.gamification .pie-chart canvas { + position: absolute; + left: 0; +} diff --git a/test/functional/gamification_plugin_profile_controller_test.rb b/test/functional/gamification_plugin_profile_controller_test.rb index 2b4925d..cbed8d9 100644 --- a/test/functional/gamification_plugin_profile_controller_test.rb +++ b/test/functional/gamification_plugin_profile_controller_test.rb @@ -10,19 +10,19 @@ class GamificationPluginProfileControllerTest < ActionController::TestCase attr_accessor :person, :environment - should 'display points in gamification info page' do + should 'display points in gamification dashboard' do person.add_points(20, :category => :comment_author) person.add_points(30, :category => :article_author) - get :info, :profile => person.identifier + get :dashboard, :profile => person.identifier assert_tag :div, :attributes => {:class => 'score article_author'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '30'} assert_tag :div, :attributes => {:class => 'score comment_author'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '20'} assert_tag :div, :attributes => {:class => 'score total'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '50'} end - should 'display level in gamification info page' do + should 'display level in gamification dashboard' do person.update_attribute(:level, 12) - get :info, :profile => person.identifier - assert_tag :span, :attributes => {:class => 'level'}, :content => '12' + get :dashboard, :profile => person.identifier + assert_tag :div, :attributes => {:class => 'level pie-chart', 'data-percent' => '100'}, :child => {:tag => 'span', :content => '12'} end should 'display person badges' do @@ -32,7 +32,7 @@ class GamificationPluginProfileControllerTest < ActionController::TestCase person.add_badge(badge1.id) person.add_badge(badge2.id) - get :info, :profile => person.identifier + get :dashboard, :profile => person.identifier assert_select '.badges .badge-list .badge', 2 end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 53b5b12..2b925d8 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -39,4 +39,19 @@ class ProfileTest < ActiveSupport::TestCase assert_equal 3, person.reload.level end + should 'return percentage of points earned in current level with no points' do + profile.stubs(:points).returns(0) + assert_equal 0, profile.gamification_plugin_level_percent + end + + should 'return percentage of points earned in current level with full points' do + profile.stubs(:points).returns(10) + assert_equal 100, profile.gamification_plugin_level_percent + end + + should 'return percentage of points earned in current level' do + profile.stubs(:points).returns(4) + assert_equal 40, profile.gamification_plugin_level_percent + end + end diff --git a/views/gamification/dashboard.html.erb b/views/gamification/dashboard.html.erb new file mode 100644 index 0000000..f30ccf6 --- /dev/null +++ b/views/gamification/dashboard.html.erb @@ -0,0 +1,37 @@ +<% extend GamificationPlugin::DashboardHelper %> + +
+

<%= _('Gamification Dashboard for %s' % @target.identifier) %>

+ +
+

<%= _('Level') %>

+ <%= level_chart(@target) %> +
+ +
+

<%= _('Score') %>

+ <% Merit::PointRules::AVAILABLE_RULES.each do |category, setting| %> +
+ <%= _('%s: ' % category) %> + <%= @target.points(:category => category) %> +
+ <% end %> +
+ <%= _('Total: ') %> + <%= @target.points %> +
+
+
+

<%= _('Badges') %>

+
    + <% @target.badges.each do |badge| %> +
  • + <%= badge.description %> + <% if badge.level.present? %> + <%= _('Level') %> <%= badge.level %> + <% end %> +
  • + <% end %> +
+
+
diff --git a/views/gamification/display_notifications.html.erb b/views/gamification/display_notifications.html.erb index c379d50..af5aa28 100644 --- a/views/gamification/display_notifications.html.erb +++ b/views/gamification/display_notifications.html.erb @@ -1,6 +1,6 @@ <%= stylesheet_link_tag '/plugins/gamification/animate.css' %> -<%= link_to({:controller => 'gamification_plugin_profile', :action => :info, :profile => current_person.identifier }, :class => 'gamification-plugin user-info-template', :style => 'display: none') do %> +<%= link_to({:controller => 'gamification_plugin_profile', :action => :dashboard, :profile => current_person.identifier }, :class => 'gamification-plugin user-info-template', :style => 'display: none') do %>       diff --git a/views/gamification/info.html.erb b/views/gamification/info.html.erb deleted file mode 100644 index 3e8c145..0000000 --- a/views/gamification/info.html.erb +++ /dev/null @@ -1,30 +0,0 @@ -
-

<%= _('Gamification Info for %s' % @target.identifier) %>

-

<%= _('Level:') %> <%= @target.level %>

-
-

<%= _('Score') %>

- <% Merit::PointRules::AVAILABLE_RULES.each do |category, setting| %> -
- <%= _('%s: ' % category) %> - <%= @target.points(:category => category) %> -
- <% end %> -
- <%= _('Total: ') %> - <%= @target.points %> -
-
-
-

<%= _('Badges') %>

-
    - <% @target.badges.each do |badge| %> -
  • - <%= badge.description %> - <% if badge.level.present? %> - <%= _('Level') %> <%= badge.level %> - <% end %> -
  • - <% end %> -
-
-
diff --git a/views/gamification/notification_badge.html.erb b/views/gamification/notification_badge.html.erb index f02598e..ab80d26 100644 --- a/views/gamification/notification_badge.html.erb +++ b/views/gamification/notification_badge.html.erb @@ -5,5 +5,5 @@
<%= badge.description %>
<%= _('Level %s' % badge.level) %>
-
<%= link_to _('My Badges'), {:controller => 'gamification_plugin_profile', :action => :info, :profile => current_person.identifier } %>
+
<%= link_to _('My Badges'), {:controller => 'gamification_plugin_profile', :action => :dashboard, :profile => current_person.identifier } %>
-- libgit2 0.21.2