annotate app/helpers/activities_helper.rb @ 1224:30c444ea1338 cannam

Various fixes to colleagues box
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 12 Mar 2013 15:15:18 +0000
parents 2c59ee348e2b
children d280360758e5
rev   line source
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@1224 54
chris@1224 55 # Limit scope so as to exclude issues (which non-members can add)
chris@1224 56 activity.scope = [ "changesets", "files", "documents", "news", "wiki_edits", "messages", "time_entries", "publications" ]
chris@1224 57
chris@1217 58 days = Setting.activity_days_default.to_i
chris@1217 59 events = activity.events(Date.today - days, Date.today + 1)
chris@1217 60 projhash = project_activity_on_events(events)
chris@1217 61 projhash.keys.sort_by { |k| -projhash[k] }.first(count)
chris@1217 62 end
chris@1217 63
chris@1217 64 def render_active_colleagues(colleagues)
chris@1217 65
chris@1217 66 s = ""
chris@1217 67
chris@1222 68 start = Time.now
chris@1222 69
chris@1217 70 for c in colleagues
chris@1217 71 u = User.find_by_id(c)
chris@1217 72 active_projects = projects_by_activity(u, 3)
chris@1217 73 if !active_projects.empty?
chris@1224 74 s << "<dl>"
chris@1224 75 s << "<dt>"
Chris@1219 76 s << avatar(u, :size => '24')
Chris@1219 77 s << "<span class='user'>"
Chris@1219 78 s << h(u.name)
chris@1217 79 s << "</span>"
Chris@1219 80 if !u.ssamr_user_detail.nil?
Chris@1219 81 s << " - <span class='institution'>"
Chris@1219 82 s << h(u.ssamr_user_detail.institution_name)
Chris@1219 83 s << "</span>"
Chris@1219 84 end
chris@1224 85 s << "</dt>"
chris@1224 86 s << "<dd>"
Chris@1219 87 s << "<span class='active'>"
chris@1217 88 s << (active_projects.map { |p| link_to_project(p) }.join ", ")
Chris@1219 89 s << "</span>"
chris@1224 90 s << "</dl>"
chris@1217 91 end
chris@1217 92 end
chris@1222 93
chris@1222 94 finish = Time.now
chris@1222 95 logger.info "render_active_colleagues: took #{finish-start}"
chris@1217 96
chris@1217 97 if s != ""
chris@1217 98 s
chris@1217 99 else
chris@1217 100 l(:label_no_active_colleagues)
chris@1217 101 end
chris@1217 102 end
chris@1217 103
chris@1217 104 def busy_projects(events, count)
chris@1217 105
chris@1217 106 # Return a list of count projects randomly selected from amongst
chris@1217 107 # the busiest projects represented by the given activity events
chris@1217 108
chris@1217 109 projhash = project_activity_on_events(events)
chris@1217 110
chris@1186 111 # pick N highest values and use cutoff value as selection threshold
chris@1186 112 threshold = projhash.values.sort.last(count).first
chris@1186 113
chris@1186 114 # select projects above threshold and pick N from them randomly
chris@1186 115 busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count)
chris@1186 116
chris@1186 117 # return projects rather than just ids
chris@1011 118 busy.map { |pid| Project.find(pid) }
chris@1011 119 end
chris@1011 120
chris@1011 121 def busy_institutions(events, count)
Chris@1013 122 authors = events.map do |e|
Chris@1013 123 e.event_author unless !e.respond_to?(:event_author)
Chris@1013 124 end.compact
Chris@1013 125 institutions = authors.map do |a|
Chris@1013 126 if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil?
Chris@1013 127 a.ssamr_user_detail.institution_name
Chris@1013 128 end
Chris@1013 129 end
chris@1011 130 insthash = institutions.compact.sort.group_by { |i| i }
chris@1011 131 insthash = insthash.merge(insthash) { |k,v| v.length }
chris@1011 132 threshold = insthash.values.sort.last(count).first
chris@1011 133 insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
chris@1011 134 end
chris@1011 135
chris@1011 136 end