comparison app/helpers/activities_helper.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents 441b66f148b6
children 51364c0cd58f 1c7af51e9409
comparison
equal deleted inserted replaced
1209:1b1138f6f55e 1338:25603efa57b5
13 else 13 else
14 nil 14 nil
15 end 15 end
16 end 16 end
17 17
18 def busy_projects(events, count) 18 def project_activity_on_events(events)
19 19
20 # Score each project for which there are any events, by giving 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 21 # each event a score based on how long ago it was (the more recent
22 # the better). 22 # the better). Return a hash mapping project id to score.
23 23
24 projhash = Hash.new 24 projhash = Hash.new
25 25
26 events.each do |e| 26 events.each do |e|
27 if e.respond_to?(:project) 27 if e.respond_to?(:project)
37 projhash[p] = score 37 projhash[p] = score
38 end 38 end
39 end 39 end
40 end 40 end
41 end 41 end
42
43 projhash
44 end
45
46 def projects_by_activity(user, count)
47
48 # Return up to count of the user's project ids ordered by that user's
49 # recent activity, omitting any projects for which no activity
50 # occurred in the recent past and any projects not visible to
51 # the current user
52
53 activity = Redmine::Activity::Fetcher.new(User.current, :author => user)
54
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 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 start = Time.now
69
70 my_inst = ""
71 if ! User.current.ssamr_user_detail.nil?
72 my_inst = User.current.ssamr_user_detail.institution_name
73 end
74
75 actives = Hash.new
76 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 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 s << "<dt>"
92 s << avatar(u, :size => '24')
93 s << "<span class='user'>"
94 s << h(u.name)
95 s << "</span>"
96 if !u.ssamr_user_detail.nil?
97 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 end
104 s << "</dt>"
105 s << "<dd>"
106 s << "<span class='active'>"
107 s << (actives[c].map { |p| link_to_project(p) }.join ", ")
108 s << "</span>"
109 end
110 s << "</dl>"
111
112 finish = Time.now
113 logger.info "render_active_colleagues: took #{finish-start}"
114
115 s.html_safe
116 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)
42 125
43 # pick N highest values and use cutoff value as selection threshold 126 # pick N highest values and use cutoff value as selection threshold
44 threshold = projhash.values.sort.last(count).first 127 threshold = projhash.values.sort.last(count).first
45 128
46 # select projects above threshold and pick N from them randomly 129 # select projects above threshold and pick N from them randomly