annotate app/helpers/activities_helper.rb @ 1219:3286032e15cf live

Some layout and fixes
author Chris Cannam
date Tue, 12 Mar 2013 13:24:05 +0000
parents 875b5b4c574d
children 2c59ee348e2b
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@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@1217 64 for c in colleagues
chris@1217 65 u = User.find_by_id(c)
chris@1217 66 active_projects = projects_by_activity(u, 3)
chris@1217 67 if !active_projects.empty?
Chris@1219 68 s << "<div class='active-person'>"
Chris@1219 69 s << avatar(u, :size => '24')
Chris@1219 70 s << "<span class='user'>"
Chris@1219 71 s << h(u.name)
chris@1217 72 s << "</span>"
Chris@1219 73 if !u.ssamr_user_detail.nil?
Chris@1219 74 s << " - <span class='institution'>"
Chris@1219 75 s << h(u.ssamr_user_detail.institution_name)
Chris@1219 76 s << "</span>"
Chris@1219 77 end
Chris@1219 78 s << "<br>"
Chris@1219 79 s << "<span class='active'>"
chris@1217 80 s << (active_projects.map { |p| link_to_project(p) }.join ", ")
Chris@1219 81 s << "</span>"
chris@1217 82 s << "</div>"
chris@1217 83 end
chris@1217 84 end
chris@1217 85
chris@1217 86 if s != ""
chris@1217 87 s
chris@1217 88 else
chris@1217 89 l(:label_no_active_colleagues)
chris@1217 90 end
chris@1217 91 end
chris@1217 92
chris@1217 93 def busy_projects(events, count)
chris@1217 94
chris@1217 95 # Return a list of count projects randomly selected from amongst
chris@1217 96 # the busiest projects represented by the given activity events
chris@1217 97
chris@1217 98 projhash = project_activity_on_events(events)
chris@1217 99
chris@1186 100 # pick N highest values and use cutoff value as selection threshold
chris@1186 101 threshold = projhash.values.sort.last(count).first
chris@1186 102
chris@1186 103 # select projects above threshold and pick N from them randomly
chris@1186 104 busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count)
chris@1186 105
chris@1186 106 # return projects rather than just ids
chris@1011 107 busy.map { |pid| Project.find(pid) }
chris@1011 108 end
chris@1011 109
chris@1011 110 def busy_institutions(events, count)
Chris@1013 111 authors = events.map do |e|
Chris@1013 112 e.event_author unless !e.respond_to?(:event_author)
Chris@1013 113 end.compact
Chris@1013 114 institutions = authors.map do |a|
Chris@1013 115 if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil?
Chris@1013 116 a.ssamr_user_detail.institution_name
Chris@1013 117 end
Chris@1013 118 end
chris@1011 119 insthash = institutions.compact.sort.group_by { |i| i }
chris@1011 120 insthash = insthash.merge(insthash) { |k,v| v.length }
chris@1011 121 threshold = insthash.values.sort.last(count).first
chris@1011 122 insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
chris@1011 123 end
chris@1011 124
chris@1011 125 end