annotate app/helpers/activities_helper.rb @ 1466:834828a14f2b bug_598

Close obsolete branch bug_598
author Chris Cannam
date Fri, 21 Jun 2013 14:51:55 +0100
parents 441b66f148b6
children 51364c0cd58f 1c7af51e9409
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@1227 48 # Return up to count of the user's project ids 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@1226 70 my_inst = ""
Chris@1226 71 if ! User.current.ssamr_user_detail.nil?
Chris@1226 72 my_inst = User.current.ssamr_user_detail.institution_name
Chris@1226 73 end
Chris@1226 74
Chris@1227 75 actives = Hash.new
chris@1217 76 for c in colleagues
chris@1217 77 u = User.find_by_id(c)
chris@1217 78 active_projects = projects_by_activity(u, 3)
chris@1217 79 if !active_projects.empty?
Chris@1227 80 actives[c] = active_projects
Chris@1227 81 end
Chris@1227 82 end
Chris@1227 83
Chris@1227 84 if actives.empty?
Chris@1227 85 l(:label_no_active_colleagues)
Chris@1227 86 else
Chris@1227 87
Chris@1227 88 s << "<dl>"
Chris@1227 89 for c in actives.keys.sample(10)
Chris@1227 90 u = User.find_by_id(c)
chris@1224 91 s << "<dt>"
Chris@1219 92 s << avatar(u, :size => '24')
Chris@1219 93 s << "<span class='user'>"
Chris@1219 94 s << h(u.name)
chris@1217 95 s << "</span>"
Chris@1219 96 if !u.ssamr_user_detail.nil?
Chris@1226 97 inst = u.ssamr_user_detail.institution_name
Chris@1226 98 if inst != "" and inst != my_inst
Chris@1226 99 s << " - <span class='institution'>"
Chris@1226 100 s << h(u.ssamr_user_detail.institution_name)
Chris@1226 101 s << "</span>"
Chris@1226 102 end
Chris@1219 103 end
chris@1224 104 s << "</dt>"
chris@1224 105 s << "<dd>"
Chris@1219 106 s << "<span class='active'>"
Chris@1227 107 s << (actives[c].map { |p| link_to_project(p) }.join ", ")
Chris@1219 108 s << "</span>"
chris@1217 109 end
Chris@1227 110 s << "</dl>"
chris@1222 111
Chris@1227 112 finish = Time.now
Chris@1227 113 logger.info "render_active_colleagues: took #{finish-start}"
chris@1217 114
Chris@1330 115 s.html_safe
chris@1217 116 end
chris@1217 117 end
chris@1217 118
chris@1217 119 def busy_projects(events, count)
chris@1217 120
chris@1217 121 # Return a list of count projects randomly selected from amongst
chris@1217 122 # the busiest projects represented by the given activity events
chris@1217 123
chris@1217 124 projhash = project_activity_on_events(events)
chris@1217 125
chris@1186 126 # pick N highest values and use cutoff value as selection threshold
chris@1186 127 threshold = projhash.values.sort.last(count).first
chris@1186 128
chris@1186 129 # select projects above threshold and pick N from them randomly
chris@1186 130 busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count)
chris@1186 131
chris@1186 132 # return projects rather than just ids
chris@1011 133 busy.map { |pid| Project.find(pid) }
chris@1011 134 end
chris@1011 135
chris@1011 136 def busy_institutions(events, count)
Chris@1013 137 authors = events.map do |e|
Chris@1013 138 e.event_author unless !e.respond_to?(:event_author)
Chris@1013 139 end.compact
Chris@1013 140 institutions = authors.map do |a|
Chris@1013 141 if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil?
Chris@1013 142 a.ssamr_user_detail.institution_name
Chris@1013 143 end
Chris@1013 144 end
chris@1011 145 insthash = institutions.compact.sort.group_by { |i| i }
chris@1011 146 insthash = insthash.merge(insthash) { |k,v| v.length }
chris@1011 147 threshold = insthash.values.sort.last(count).first
chris@1011 148 insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
chris@1011 149 end
chris@1011 150
chris@1011 151 end