Commit 9cb5a0920ac2b322c468825a0b27c320f9d84c04

Authored by Victor Costa
1 parent 3520fc1a

Add chart in dashboard to represent level

controllers/gamification_plugin_profile_controller.rb
1 1 class GamificationPluginProfileController < ProfileController
2 2  
3   - def info
  3 + def dashboard
4 4 @target = profile
5   - render 'gamification/info'
  5 + render 'gamification/dashboard'
6 6 end
7 7  
8 8 end
... ...
lib/ext/profile.rb
... ... @@ -15,4 +15,13 @@ class Profile
15 15 last_level
16 16 end
17 17  
  18 + def gamification_plugin_level_percent
  19 + settings = GamificationPlugin.settings(environment)
  20 + rules = settings.get_setting(:rank_rules)
  21 + return 100 if rules.blank? || rules.length < level
  22 +
  23 + next_level_points = rules[level][:points]
  24 + 100*points/next_level_points.to_f
  25 + end
  26 +
18 27 end
... ...
lib/gamification_plugin.rb
... ... @@ -50,7 +50,7 @@ class GamificationPlugin &lt; Noosfero::Plugin
50 50 end
51 51  
52 52 def js_files
53   - ['jquery.noty.packaged.min.js', 'main.js']
  53 + ['jquery.noty.packaged.min.js', 'jquery.easypiechart.min.js', 'main.js']
54 54 end
55 55  
56 56 ActionDispatch::Reloader.to_prepare do
... ...
lib/gamification_plugin/dashboard_helper.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +module GamificationPlugin::DashboardHelper
  2 +
  3 + def level_chart(target)
  4 +
  5 + content_tag(:div, :class => "level pie-chart", 'data-percent' => @target.gamification_plugin_level_percent) do
  6 + content_tag :span, @target.level
  7 + end
  8 + end
  9 +
  10 +end
... ...
lib/merit/point_rules.rb
... ... @@ -11,6 +11,14 @@ module Merit
11 11 :description => _('Point weight for comment author'),
12 12 :default_weight => 10
13 13 },
  14 + :comment_article_author => {
  15 + :action => 'comment#create',
  16 + :undo_action => 'comment#destroy',
  17 + :to => lambda {|comment| comment.source.author},
  18 + :value => 1,
  19 + :description => _('Point weight for article author of a comment'),
  20 + :default_weight => 10
  21 + },
14 22 :article_author => {
15 23 :action => 'article#create',
16 24 :undo_action => 'article#destroy',
... ...
public/jquery.easypiechart.min.js 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +/**!
  2 + * easy-pie-chart
  3 + * Lightweight plugin to render simple, animated and retina optimized pie charts
  4 + *
  5 + * @license
  6 + * @author Robert Fleischmann <rendro87@gmail.com> (http://robert-fleischmann.de)
  7 + * @version 2.1.7
  8 + **/
  9 +!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)))})}});
0 10 \ No newline at end of file
... ...
public/main.js
... ... @@ -22,9 +22,20 @@ var gamificationPlugin = {
22 22 info.find('.level .value').text(gamificationPlugin.level);
23 23 info.insertAfter('#user .logged-in #homepage-link');
24 24 info.show();
  25 + },
  26 + displayLevelChart: function() {
  27 + var chart = $('.gamification .pie-chart');
  28 + var size = 60;
  29 + chart.easyPieChart({lineWidth: 10, scaleColor: false, size: size, barColor: '#1EA5C5', trackColor: '#C0EEFF'});
  30 + chart.width(size);
  31 + chart.find('span').css('line-height', size+'px');
25 32 }
26 33 }
27 34  
28 35 jQuery(window).bind("userDataLoaded", function(event, data) {
29 36 gamificationPlugin.displayUserInfo(data.gamification_plugin);
30 37 });
  38 +
  39 +jQuery(document).ready(function($) {
  40 + gamificationPlugin.displayLevelChart();
  41 +});
... ...
public/style.css
... ... @@ -53,3 +53,12 @@
53 53 .gamification-notification .all-badges {
54 54 font-size: 10px;
55 55 }
  56 +
  57 +.gamification .pie-chart {
  58 + position: relative;
  59 + text-align: center;
  60 +}
  61 +.gamification .pie-chart canvas {
  62 + position: absolute;
  63 + left: 0;
  64 +}
... ...
test/functional/gamification_plugin_profile_controller_test.rb
... ... @@ -10,19 +10,19 @@ class GamificationPluginProfileControllerTest &lt; ActionController::TestCase
10 10  
11 11 attr_accessor :person, :environment
12 12  
13   - should 'display points in gamification info page' do
  13 + should 'display points in gamification dashboard' do
14 14 person.add_points(20, :category => :comment_author)
15 15 person.add_points(30, :category => :article_author)
16   - get :info, :profile => person.identifier
  16 + get :dashboard, :profile => person.identifier
17 17 assert_tag :div, :attributes => {:class => 'score article_author'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '30'}
18 18 assert_tag :div, :attributes => {:class => 'score comment_author'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '20'}
19 19 assert_tag :div, :attributes => {:class => 'score total'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '50'}
20 20 end
21 21  
22   - should 'display level in gamification info page' do
  22 + should 'display level in gamification dashboard' do
23 23 person.update_attribute(:level, 12)
24   - get :info, :profile => person.identifier
25   - assert_tag :span, :attributes => {:class => 'level'}, :content => '12'
  24 + get :dashboard, :profile => person.identifier
  25 + assert_tag :div, :attributes => {:class => 'level pie-chart', 'data-percent' => '100'}, :child => {:tag => 'span', :content => '12'}
26 26 end
27 27  
28 28 should 'display person badges' do
... ... @@ -32,7 +32,7 @@ class GamificationPluginProfileControllerTest &lt; ActionController::TestCase
32 32  
33 33 person.add_badge(badge1.id)
34 34 person.add_badge(badge2.id)
35   - get :info, :profile => person.identifier
  35 + get :dashboard, :profile => person.identifier
36 36 assert_select '.badges .badge-list .badge', 2
37 37 end
38 38  
... ...
test/unit/profile_test.rb
... ... @@ -39,4 +39,19 @@ class ProfileTest &lt; ActiveSupport::TestCase
39 39 assert_equal 3, person.reload.level
40 40 end
41 41  
  42 + should 'return percentage of points earned in current level with no points' do
  43 + profile.stubs(:points).returns(0)
  44 + assert_equal 0, profile.gamification_plugin_level_percent
  45 + end
  46 +
  47 + should 'return percentage of points earned in current level with full points' do
  48 + profile.stubs(:points).returns(10)
  49 + assert_equal 100, profile.gamification_plugin_level_percent
  50 + end
  51 +
  52 + should 'return percentage of points earned in current level' do
  53 + profile.stubs(:points).returns(4)
  54 + assert_equal 40, profile.gamification_plugin_level_percent
  55 + end
  56 +
42 57 end
... ...
views/gamification/dashboard.html.erb 0 → 100644
... ... @@ -0,0 +1,37 @@
  1 +<% extend GamificationPlugin::DashboardHelper %>
  2 +
  3 +<div class="gamification">
  4 + <h1><%= _('Gamification Dashboard for %s' % @target.identifier) %></h1>
  5 +
  6 + <div>
  7 + <h3> <%= _('Level') %></h3>
  8 + <%= level_chart(@target) %>
  9 + </div>
  10 +
  11 + <div class="scores">
  12 + <h3><%= _('Score') %></h3>
  13 + <% Merit::PointRules::AVAILABLE_RULES.each do |category, setting| %>
  14 + <div class="score <%= category %>">
  15 + <span class="label"><%= _('%s: ' % category) %></span>
  16 + <span class="value"><%= @target.points(:category => category) %></span>
  17 + </div>
  18 + <% end %>
  19 + <div class="score total">
  20 + <span class="label"><%= _('Total: ') %></span>
  21 + <span class="value"><%= @target.points %></span>
  22 + </div>
  23 + </div>
  24 + <div class="badges">
  25 + <h3><%= _('Badges') %></h3>
  26 + <ul class="badge-list">
  27 + <% @target.badges.each do |badge| %>
  28 + <li class="badge <%= badge.name %>">
  29 + <span class="description"><%= badge.description %></span>
  30 + <% if badge.level.present? %>
  31 + <span class="label"><%= _('Level') %></span>&nbsp;<span class="level"><%= badge.level %></span>
  32 + <% end %>
  33 + </li>
  34 + <% end %>
  35 + </ul>
  36 + </div>
  37 +</div>
... ...
views/gamification/display_notifications.html.erb
1 1 <%= stylesheet_link_tag '/plugins/gamification/animate.css' %>
2 2  
3   -<%= link_to({:controller => 'gamification_plugin_profile', :action => :info, :profile => current_person.identifier }, :class => 'gamification-plugin user-info-template', :style => 'display: none') do %>
  3 +<%= link_to({:controller => 'gamification_plugin_profile', :action => :dashboard, :profile => current_person.identifier }, :class => 'gamification-plugin user-info-template', :style => 'display: none') do %>
4 4 <span class="level"><span class="icon">&nbsp;</span><span class="value"></span></span>
5 5 <span class="points"><span class="icon">&nbsp;</span><span class="value"></span></span>
6 6 <span class="badges"><span class="icon">&nbsp;</span><span class="value"></span></span>
... ...
views/gamification/info.html.erb
... ... @@ -1,30 +0,0 @@
1   -<div class="gamification">
2   - <h1><%= _('Gamification Info for %s' % @target.identifier) %></h1>
3   - <h3> <%= _('Level:') %>&nbsp;<span class="level"><%= @target.level %></span></h3>
4   - <div class="scores">
5   - <h3><%= _('Score') %></h3>
6   - <% Merit::PointRules::AVAILABLE_RULES.each do |category, setting| %>
7   - <div class="score <%= category %>">
8   - <span class="label"><%= _('%s: ' % category) %></span>
9   - <span class="value"><%= @target.points(:category => category) %></span>
10   - </div>
11   - <% end %>
12   - <div class="score total">
13   - <span class="label"><%= _('Total: ') %></span>
14   - <span class="value"><%= @target.points %></span>
15   - </div>
16   - </div>
17   - <div class="badges">
18   - <h3><%= _('Badges') %></h3>
19   - <ul class="badge-list">
20   - <% @target.badges.each do |badge| %>
21   - <li class="badge <%= badge.name %>">
22   - <span class="description"><%= badge.description %></span>
23   - <% if badge.level.present? %>
24   - <span class="label"><%= _('Level') %></span>&nbsp;<span class="level"><%= badge.level %></span>
25   - <% end %>
26   - </li>
27   - <% end %>
28   - </ul>
29   - </div>
30   -</div>
views/gamification/notification_badge.html.erb
... ... @@ -5,5 +5,5 @@
5 5 <div class="description"><%= badge.description %></div>
6 6 <div class="level"><%= _('Level %s' % badge.level) %></div>
7 7 </div>
8   - <div class="all-badges"><%= link_to _('My Badges'), {:controller => 'gamification_plugin_profile', :action => :info, :profile => current_person.identifier } %></div>
  8 + <div class="all-badges"><%= link_to _('My Badges'), {:controller => 'gamification_plugin_profile', :action => :dashboard, :profile => current_person.identifier } %></div>
9 9 </div>
... ...