app/models/activities/accountability_report.rb
# Calculates the totals for all records in the data hash.
Calculates the totals for all records in the data hash.
Calculates the totals for all records in the data hash .
def final_statistics(force = false)
    @log_step = nil
    load_results(:with_departments, force) if force || @results.empty?
    update_status("Generated on #{generated_at.to_s(:date_at_time12)}") if generated_at
    puts "Calculating final statistics."
    stats = {:department => {}}
    default_quarter_hash = {}
    stats[:total] = {
      :number_of_students => @results.size,
      :student_quarters => @results.values.collect(&:size).sum.to_i,
      :average_quarters_per_student => @results.values.collect(&:size).average.to_f,
      :quarters => {},
      :courses => []
    }
    for quarter in @quarters
      default_quarter_hash[quarter.abbrev] = {
        :number_of_students => 0,
        :number_of_hours => 0,
        :courses => []
      }
    end
    stats[:total][:quarters] = default_quarter_hash.clone
    for system_key, quarter_hash in @results
      for quarter_abbrev, activities in quarter_hash
        stats[:total][:quarters][quarter_abbrev][:number_of_students] += 1
        hours_to_add = activities.collect{|a| a[:number_of_hours] unless a[:duplicate]==true}.compact.sum.to_i
        stats[:total][:quarters][quarter_abbrev][:number_of_hours] += hours_to_add
        for activity in activities
          unless stats[:department][activity[:department]]
            stats[:department][activity[:department]] = { :quarters => {} }
            for quarter in @quarters
              stats[:department][activity[:department]][:quarters][quarter.abbrev] = {
                :number_of_students => 0,
                :number_of_hours => 0,
                :courses => []
              }
            end
          end
          unless activity[:course].blank?
            unless stats[:department][activity[:department]][:quarters][quarter_abbrev][:courses].include?(activity[:course])
              stats[:department][activity[:department]][:quarters][quarter_abbrev][:courses] << activity[:course] 
            end 
          end
          stats[:department][activity[:department]][:quarters][quarter_abbrev][:number_of_students] += 1
          stats[:department][activity[:department]][:quarters][quarter_abbrev][:number_of_hours] += activity[:number_of_hours].to_f
          stats[:department][activity[:department]][:total] ||= { :students => [] }
          stats[:department][activity[:department]][:total][:students] << system_key
        end
        for activity in activities
          unless activity[:reporting_department].blank?
            unless stats[:department][activity[:reporting_department]]
              stats[:department][activity[:reporting_department]] = { :quarters => {} }
              for quarter in @quarters
                stats[:department][activity[:reporting_department]][:quarters][quarter.abbrev] = {
                  :number_of_students => 0,
                  :number_of_hours => 0,
                  :courses => []
                }
              end
            end
            stats[:department][activity[:reporting_department]][:quarters][quarter_abbrev][:number_of_students] += 1
            stats[:department][activity[:reporting_department]][:quarters][quarter_abbrev][:number_of_hours] += activity[:number_of_hours].to_f
            stats[:department][activity[:reporting_department]][:total] ||= { :students => [] }
            stats[:department][activity[:reporting_department]][:total][:students] << system_key
          end
        end
      end
    end
    stats[:total][:number_of_hours] = stats[:total][:quarters].collect{|k,v| v[:number_of_hours]}.sum.to_i
    stats[:total][:average_hours_per_student] = stats[:total][:number_of_hours] / stats[:total][:number_of_students].to_f
    stats
  end
def final_statistics ( force = false ) @log_step = nil load_results ( :with_departments , force ) if force || @results . empty? update_status ( "Generated on #{generated_at.to_s(:date_at_time12)}" ) if generated_at puts "Calculating final statistics." stats = { :department => { } } default_quarter_hash = { } stats [ :total ] = { :number_of_students => @results . size , :student_quarters => @results . values . collect ( & :size ) . sum . to_i , :average_quarters_per_student => @results . values . collect ( & :size ) . average . to_f , :quarters => { } , :courses => [ ] } for quarter in @quarters default_quarter_hash [ quarter . abbrev ] = { :number_of_students => 0 , :number_of_hours => 0 , :courses => [ ] } end stats [ :total ] [ :quarters ] = default_quarter_hash . clone for system_key , quarter_hash in @results for quarter_abbrev , activities in quarter_hash stats [ :total ] [ :quarters ] [ quarter_abbrev ] [ :number_of_students ] += 1 hours_to_add = activities . collect { | a | a [ :number_of_hours ] unless a [ :duplicate ] == true } . compact . sum . to_i stats [ :total ] [ :quarters ] [ quarter_abbrev ] [ :number_of_hours ] += hours_to_add for activity in activities unless stats [ :department ] [ activity [ :department ] ] stats [ :department ] [ activity [ :department ] ] = { :quarters => { } } for quarter in @quarters stats [ :department ] [ activity [ :department ] ] [ :quarters ] [ quarter . abbrev ] = { :number_of_students => 0 , :number_of_hours => 0 , :courses => [ ] } end end unless activity [ :course ] . blank? unless stats [ :department ] [ activity [ :department ] ] [ :quarters ] [ quarter_abbrev ] [ :courses ] . include? ( activity [ :course ] ) stats [ :department ] [ activity [ :department ] ] [ :quarters ] [ quarter_abbrev ] [ :courses ] << activity [ :course ] end end stats [ :department ] [ activity [ :department ] ] [ :quarters ] [ quarter_abbrev ] [ :number_of_students ] += 1 stats [ :department ] [ activity [ :department ] ] [ :quarters ] [ quarter_abbrev ] [ :number_of_hours ] += activity [ :number_of_hours ] . to_f stats [ :department ] [ activity [ :department ] ] [ :total ] ||= { :students => [ ] } stats [ :department ] [ activity [ :department ] ] [ :total ] [ :students ] << system_key end for activity in activities unless activity [ :reporting_department ] . blank? unless stats [ :department ] [ activity [ :reporting_department ] ] stats [ :department ] [ activity [ :reporting_department ] ] = { :quarters => { } } for quarter in @quarters stats [ :department ] [ activity [ :reporting_department ] ] [ :quarters ] [ quarter . abbrev ] = { :number_of_students => 0 , :number_of_hours => 0 , :courses => [ ] } end end stats [ :department ] [ activity [ :reporting_department ] ] [ :quarters ] [ quarter_abbrev ] [ :number_of_students ] += 1 stats [ :department ] [ activity [ :reporting_department ] ] [ :quarters ] [ quarter_abbrev ] [ :number_of_hours ] += activity [ :number_of_hours ] . to_f stats [ :department ] [ activity [ :reporting_department ] ] [ :total ] ||= { :students => [ ] } stats [ :department ] [ activity [ :reporting_department ] ] [ :total ] [ :students ] << system_key end end end end stats [ :total ] [ :number_of_hours ] = stats [ :total ] [ :quarters ] . collect { | k , v | v [ :number_of_hours ] } . sum . to_i stats [ :total ] [ :average_hours_per_student ] = stats [ :total ] [ :number_of_hours ] / stats [ :total ] [ :number_of_students ] . to_f stats end
Calculates the totals for all records in the data hash.
Calculates the totals for all records in the data hash .
# Add in department info # if activity.is_a?(ActivityCourse) # stats[:total][:quarters][quarter_abbrev][:courses] <<  # Add in reporting_department info
force