view app/helpers/activities_helper.rb @ 1193:d67ed1030451 feature_564

Add new-project link to Explore page
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 22 Jan 2013 16:54:14 +0000
parents e19f375f9afa
children adb5f38f6ab7
line wrap: on
line source

module ActivitiesHelper

  def busy_projects(events, count)

    # Score each project for which there are any events, by giving
    # each event a score based on how long ago it was (the more recent
    # the better).

    projhash = Hash.new
    
    events.each do |e|
      if e.respond_to?(:project)
        p = e.project
        d = if e.respond_to? :updated_at then e.updated_at else e.updated_on end
        dd = Date.parse d.to_s
        age = Date.today - dd
        score = (age < 14 ? 15-age : 1)
        if projhash.key? p
          projhash[p] += score
        else
          projhash[p] = score
        end
      end
    end

    # pick N highest values and use cutoff value as selection threshold
    threshold = projhash.values.sort.last(count).first

    # select projects above threshold and pick N from them randomly
    busy = projhash.keys.select { |k| projhash[k] >= threshold }.sample(count)

    # return projects rather than just ids
    busy.map { |pid| Project.find(pid) }
  end

  def busy_institutions(events, count)
    authors = events.map do |e|
      e.event_author unless !e.respond_to?(:event_author) 
    end.compact
    institutions = authors.map do |a|
      if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil?
        a.ssamr_user_detail.institution_name
      end
    end
    insthash = institutions.compact.sort.group_by { |i| i }
    insthash = insthash.merge(insthash) { |k,v| v.length }
    threshold = insthash.values.sort.last(count).first
    insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
  end

end