To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / helpers / activities_helper.rb @ 1330:441b66f148b6

History | View | Annotate | Download (4.17 KB)

1 1011:f44860e089c5 chris
2
module ActivitiesHelper
3
4 1205:adb5f38f6ab7 chris
  def date_of_event(e)
5
    if e.respond_to? :updated_at
6
      e.updated_at
7
    elsif e.respond_to? :updated_on
8
      e.updated_on
9
    elsif e.respond_to? :created_on
10
      e.created_on
11
    elsif e.respond_to? :committed_on
12
      e.committed_on
13
    else
14
      nil
15
    end
16
  end
17
18 1217:875b5b4c574d chris
  def project_activity_on_events(events)
19 1186:e19f375f9afa chris
20
    # Score each project for which there are any events, by giving
21
    # each event a score based on how long ago it was (the more recent
22 1217:875b5b4c574d chris
    # the better). Return a hash mapping project id to score.
23 1186:e19f375f9afa chris
24
    projhash = Hash.new
25
26
    events.each do |e|
27
      if e.respond_to?(:project)
28
        p = e.project
29 1205:adb5f38f6ab7 chris
        d = date_of_event e
30
        if !d.nil?
31
          dd = Date.parse d.to_s
32
          age = Date.today - dd
33
          score = (age < 14 ? 15-age : 1)
34
          if projhash.key? p
35
            projhash[p] += score
36
          else
37
            projhash[p] = score
38
          end
39 1186:e19f375f9afa chris
        end
40
      end
41
    end
42
43 1217:875b5b4c574d chris
    projhash
44
  end
45
46
  def projects_by_activity(user, count)
47
48 1227:eb168c1e9553 Chris
    # Return up to count of the user's project ids ordered by that user's
49 1217:875b5b4c574d chris
    # recent activity, omitting any projects for which no activity
50 1219:3286032e15cf Chris
    # occurred in the recent past and any projects not visible to
51
    # the current user
52 1217:875b5b4c574d chris
53 1219:3286032e15cf Chris
    activity = Redmine::Activity::Fetcher.new(User.current, :author => user)
54 1224:30c444ea1338 chris
55
    # Limit scope so as to exclude issues (which non-members can add)
56
    activity.scope = [ "changesets", "files", "documents", "news", "wiki_edits", "messages", "time_entries", "publications" ]
57
58 1217:875b5b4c574d chris
    days = Setting.activity_days_default.to_i
59
    events = activity.events(Date.today - days, Date.today + 1)
60
    projhash = project_activity_on_events(events)
61
    projhash.keys.sort_by { |k| -projhash[k] }.first(count)
62
  end
63
64
  def render_active_colleagues(colleagues)
65
66
    s = ""
67
68 1222:2c59ee348e2b chris
    start = Time.now
69
70 1226:d280360758e5 Chris
    my_inst = ""
71
    if ! User.current.ssamr_user_detail.nil?
72
      my_inst = User.current.ssamr_user_detail.institution_name
73
    end
74
75 1227:eb168c1e9553 Chris
    actives = Hash.new
76 1217:875b5b4c574d chris
    for c in colleagues
77
      u = User.find_by_id(c)
78
      active_projects = projects_by_activity(u, 3)
79
      if !active_projects.empty?
80 1227:eb168c1e9553 Chris
        actives[c] = active_projects
81
      end
82
    end
83
84
    if actives.empty?
85
      l(:label_no_active_colleagues)
86
    else
87
88
      s << "<dl>"
89
      for c in actives.keys.sample(10)
90
        u = User.find_by_id(c)
91 1224:30c444ea1338 chris
        s << "<dt>"
92 1219:3286032e15cf Chris
        s << avatar(u, :size => '24')
93
        s << "<span class='user'>"
94
        s << h(u.name)
95 1217:875b5b4c574d chris
        s << "</span>"
96 1219:3286032e15cf Chris
        if !u.ssamr_user_detail.nil?
97 1226:d280360758e5 Chris
          inst = u.ssamr_user_detail.institution_name
98
          if inst != "" and inst != my_inst
99
            s << " - <span class='institution'>"
100
            s << h(u.ssamr_user_detail.institution_name)
101
            s << "</span>"
102
          end
103 1219:3286032e15cf Chris
        end
104 1224:30c444ea1338 chris
        s << "</dt>"
105
        s << "<dd>"
106 1219:3286032e15cf Chris
        s << "<span class='active'>"
107 1227:eb168c1e9553 Chris
        s << (actives[c].map { |p| link_to_project(p) }.join ", ")
108 1219:3286032e15cf Chris
        s << "</span>"
109 1217:875b5b4c574d chris
      end
110 1227:eb168c1e9553 Chris
      s << "</dl>"
111 1222:2c59ee348e2b chris
112 1227:eb168c1e9553 Chris
      finish = Time.now
113
      logger.info "render_active_colleagues: took #{finish-start}"
114 1217:875b5b4c574d chris
115 1330:441b66f148b6 Chris
      s.html_safe
116 1217:875b5b4c574d chris
    end
117
  end
118
119
  def busy_projects(events, count)
120
121
    # Return a list of count projects randomly selected from amongst
122
    # the busiest projects represented by the given activity events
123
124
    projhash = project_activity_on_events(events)
125
126 1186:e19f375f9afa chris
    # pick N highest values and use cutoff value as selection threshold
127
    threshold = projhash.values.sort.last(count).first
128
129
    # select projects above threshold and pick N from them randomly
130
    busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count)
131
132
    # return projects rather than just ids
133 1011:f44860e089c5 chris
    busy.map { |pid| Project.find(pid) }
134
  end
135
136
  def busy_institutions(events, count)
137 1013:b98f60a6d231 Chris
    authors = events.map do |e|
138
      e.event_author unless !e.respond_to?(:event_author)
139
    end.compact
140
    institutions = authors.map do |a|
141
      if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil?
142
        a.ssamr_user_detail.institution_name
143
      end
144
    end
145 1011:f44860e089c5 chris
    insthash = institutions.compact.sort.group_by { |i| i }
146
    insthash = insthash.merge(insthash) { |k,v| v.length }
147
    threshold = insthash.values.sort.last(count).first
148
    insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
149
  end
150
151
end