comparison app/helpers/activities_helper.rb @ 1339:c03a6c3c4db9 luisf

Merge
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Thu, 20 Jun 2013 14:34:42 +0100
parents 441b66f148b6
children 51364c0cd58f 1c7af51e9409
comparison
equal deleted inserted replaced
1079:413d1d9c3efa 1339:c03a6c3c4db9
1 1
2 module ActivitiesHelper 2 module ActivitiesHelper
3 3
4 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 def project_activity_on_events(events)
19
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 # the better). Return a hash mapping project id to score.
23
24 projhash = Hash.new
25
26 events.each do |e|
27 if e.respond_to?(:project)
28 p = e.project
29 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 end
40 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
4 def busy_projects(events, count) 119 def busy_projects(events, count)
5 # Transform events list into hash from project id to number of 120
6 # occurrences of project in list (there is surely a tidier way 121 # Return a list of count projects randomly selected from amongst
7 # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8) 122 # the busiest projects represented by the given activity events
8 phash = events.map { |e| e.project unless !e.respond_to?(:project) }.sort.group_by { |p| p.id } 123
9 phash = phash.merge(phash) { |k,v| v.length } 124 projhash = project_activity_on_events(events)
10 threshold = phash.values.sort.last(count).first 125
11 busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count) 126 # 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
12 busy.map { |pid| Project.find(pid) } 133 busy.map { |pid| Project.find(pid) }
13 end 134 end
14 135
15 def busy_institutions(events, count) 136 def busy_institutions(events, count)
16 authors = events.map do |e| 137 authors = events.map do |e|