cirandas.net

ref: master

plugins/statistics/lib/statistics_block.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
class StatisticsBlock < Block

  settings_items :community_counter, :default => false
  settings_items :user_counter, :default => true
  settings_items :enterprise_counter, :default => false
  settings_items :product_counter, :default => false
  settings_items :category_counter, :default => false
  settings_items :tag_counter, :default => true
  settings_items :comment_counter, :default => true
  settings_items :hit_counter, :default => false
  settings_items :templates_ids_counter, type: Hash, default: {}

  attr_accessible :comment_counter, :community_counter, :user_counter, :enterprise_counter, :product_counter, :category_counter, :tag_counter, :hit_counter, :templates_ids_counter

  USER_COUNTERS = [:community_counter, :user_counter, :enterprise_counter, :tag_counter, :comment_counter, :hit_counter]
  COMMUNITY_COUNTERS = [:user_counter, :tag_counter, :comment_counter, :hit_counter]
  ENTERPRISE_COUNTERS = [:user_counter, :tag_counter, :comment_counter, :hit_counter]

  def self.description
    c_('Statistics')
  end

  def default_title
    _('Statistics for %s') % owner.name
  end

  def is_visible? counter
    value = self.send(counter)
    value == '1' || value == true
  end

  def is_counter_available? counter
    if owner.kind_of?(Environment)
      true
    elsif owner.kind_of?(Person)
      USER_COUNTERS.include?(counter)
    elsif owner.kind_of?(Community)
      COMMUNITY_COUNTERS.include?(counter)
    elsif owner.kind_of?(Enterprise)
      ENTERPRISE_COUNTERS.include?(counter)
    end

  end

  def help
    _('This block presents some statistics about your context.')
  end

  def timeout
    60.minutes
  end

  def environment
    if owner.kind_of?(Environment)
      owner
    elsif owner.kind_of?(Profile)
      owner.environment
    else
      nil
    end
  end

  def templates
    self.environment.community_templates
  end

  def is_template_counter_active? template_id
    self.templates_ids_counter[template_id.to_s.to_sym].to_s == 'true'
  end

  def template_counter_count(template_id)
    owner.communities.visible.where(template_id: template_id).count
  end

  def users
    if owner.kind_of?(Environment)
      owner.people.visible.count
    elsif owner.kind_of?(Organization)
      owner.members.visible.count
    elsif owner.kind_of?(Person)
      owner.friends.visible.count
    else
      0
    end
  end

  def enterprises
    if owner.kind_of?(Environment) || owner.kind_of?(Person)
      owner.enterprises.visible.enabled.count
    else
      0
    end
  end

  def products
    return [] unless environment.plugin_enabled?('ProductsPlugin')
    if owner.kind_of?(Environment)
      owner.products.where("profiles.enabled = true and profiles.visible = true").count
    elsif owner.kind_of?(Enterprise)
      owner.products.count
    else
      0
    end
  end

  def communities
    if owner.kind_of?(Environment) || owner.kind_of?(Person)
      owner.communities.visible.count
    else
      0
    end
  end

  def categories
    if owner.kind_of?(Environment) then
      owner.categories.count
    else
      0
    end
  end

  def tags
    if owner.kind_of?(Environment) then
      owner.environment_tags.count
    elsif owner.kind_of?(Profile) then
      owner.article_tags.count
    else
      0
    end
  end

  include Noosfero::Plugin::HotSpot
  def comments
    if owner.kind_of?(Environment) then
      owner.articles.sum(:comments_count).to_i + plugins.dispatch(:more_comments_count, owner).first.to_i
    elsif owner.kind_of?(Profile) then
      owner.articles.sum(:comments_count) + plugins.dispatch(:more_comments_count, owner).first.to_i
    else
      0
    end
  end

  def hits
    if owner.kind_of?(Environment) then
      owner.profiles.joins(:articles).sum(:hits).to_i
    elsif owner.kind_of?(Profile) then
      owner.articles.sum(:hits)
    else
      0
    end
  end

end