chris@1011: chris@1011: module ActivitiesHelper chris@1011: chris@1011: def busy_projects(events, count) chris@1011: # Transform events list into hash from project id to number of chris@1011: # occurrences of project in list (there is surely a tidier way chris@1011: # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8) chris@1168: phash = events.map do |e| chris@1168: e.project unless !e.respond_to?(:project) chris@1168: end.select { |p| !p.nil? }.sort.group_by { |p| p.id } chris@1011: phash = phash.merge(phash) { |k,v| v.length } chris@1011: threshold = phash.values.sort.last(count).first chris@1011: busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count) chris@1011: busy.map { |pid| Project.find(pid) } chris@1011: end chris@1011: chris@1011: def busy_institutions(events, count) Chris@1013: authors = events.map do |e| Chris@1013: e.event_author unless !e.respond_to?(:event_author) Chris@1013: end.compact Chris@1013: institutions = authors.map do |a| Chris@1013: if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil? Chris@1013: a.ssamr_user_detail.institution_name Chris@1013: end Chris@1013: end chris@1011: insthash = institutions.compact.sort.group_by { |i| i } chris@1011: insthash = insthash.merge(insthash) { |k,v| v.length } chris@1011: threshold = insthash.values.sort.last(count).first chris@1011: insthash.keys.select { |k| insthash[k] >= threshold }.sample(count) chris@1011: end chris@1011: chris@1011: end