annotate app/helpers/activities_helper.rb @ 1217:875b5b4c574d cannam

Add "People I'm working with" as My Page box -- currently off by default
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 12 Mar 2013 13:01:36 +0000
parents adb5f38f6ab7
children 3286032e15cf
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@1217 50 # occurred in the recent past
chris@1217 51
chris@1217 52 activity = Redmine::Activity::Fetcher.new(user, :author => user)
chris@1217 53 days = Setting.activity_days_default.to_i
chris@1217 54 events = activity.events(Date.today - days, Date.today + 1)
chris@1217 55 projhash = project_activity_on_events(events)
chris@1217 56 projhash.keys.sort_by { |k| -projhash[k] }.first(count)
chris@1217 57 end
chris@1217 58
chris@1217 59 def render_active_colleagues(colleagues)
chris@1217 60
chris@1217 61 s = ""
chris@1217 62
chris@1217 63 for c in colleagues
chris@1217 64 u = User.find_by_id(c)
chris@1217 65 active_projects = projects_by_activity(u, 3)
chris@1217 66 if !active_projects.empty?
chris@1217 67 s << "<div class='user'>"
chris@1217 68 s << link_to_user(u)
chris@1217 69 s << "<span class='institution'>"
chris@1217 70 s << h(u.ssamr_user_detail.institution_name)
chris@1217 71 s << "</span>"
chris@1217 72 s << "</div>"
chris@1217 73 s << "<div class='active'>"
chris@1217 74 s << l(:label_working_in) << " "
chris@1217 75 s << (active_projects.map { |p| link_to_project(p) }.join ", ")
chris@1217 76 s << "</div>"
chris@1217 77 end
chris@1217 78 end
chris@1217 79
chris@1217 80 if s != ""
chris@1217 81 s
chris@1217 82 else
chris@1217 83 l(:label_no_active_colleagues)
chris@1217 84 end
chris@1217 85 end
chris@1217 86
chris@1217 87 def busy_projects(events, count)
chris@1217 88
chris@1217 89 # Return a list of count projects randomly selected from amongst
chris@1217 90 # the busiest projects represented by the given activity events
chris@1217 91
chris@1217 92 projhash = project_activity_on_events(events)
chris@1217 93
chris@1186 94 # pick N highest values and use cutoff value as selection threshold
chris@1186 95 threshold = projhash.values.sort.last(count).first
chris@1186 96
chris@1186 97 # select projects above threshold and pick N from them randomly
chris@1186 98 busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count)
chris@1186 99
chris@1186 100 # return projects rather than just ids
chris@1011 101 busy.map { |pid| Project.find(pid) }
chris@1011 102 end
chris@1011 103
chris@1011 104 def busy_institutions(events, count)
Chris@1013 105 authors = events.map do |e|
Chris@1013 106 e.event_author unless !e.respond_to?(:event_author)
Chris@1013 107 end.compact
Chris@1013 108 institutions = authors.map do |a|
Chris@1013 109 if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil?
Chris@1013 110 a.ssamr_user_detail.institution_name
Chris@1013 111 end
Chris@1013 112 end
chris@1011 113 insthash = institutions.compact.sort.group_by { |i| i }
chris@1011 114 insthash = insthash.merge(insthash) { |k,v| v.length }
chris@1011 115 threshold = insthash.values.sort.last(count).first
chris@1011 116 insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
chris@1011 117 end
chris@1011 118
chris@1011 119 end