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