BathHacked/energy-sparks

View on GitHub
app/controllers/admin/reports/engaged_schools_controller.rb

Summary

Maintainability
A
3 hrs
Test Coverage
# frozen_string_literal: true

module Admin
  module Reports
    class EngagedSchoolsController < AdminController
      def index
        @previous_year = params.key?(:previous_year)
        @engaged_schools = ::Schools::EngagedSchoolService
                           .list_engaged_schools(previous_year: @previous_year)

        respond_to do |format|
          format.html do
            @visible_schools = School.visible.count
            @percentage = percentage_engaged
          end
          format.csv do
            send_data csv_report(@engaged_schools),
                      filename: "engaged-schools-report-#{Time.zone.now.iso8601.parameterize}" \
                                "#{@previous_year ? '-previous-year' : ''}.csv"
          end
        end
      end

      private

      def percentage_engaged
        format('%.2f', @engaged_schools.size / @visible_schools.to_f * 100)
      end

      def csv_report(engaged_schools)
        CSV.generate(headers: true) do |csv|
          csv << ['School Group', 'School', 'Funder', 'Country', 'Activities', 'Actions',
                  'Programmes', 'Target?', 'Transport survey?', 'Temperatures?', 'Audit?',
                  'Active users', 'Last visit']
          engaged_schools.each do |service|
            csv << [
              service.school_group.name,
              service.school.name,
              service.school.funder.present? ? service.school.funder.name : nil,
              service.school.country.humanize,
              service.recent_activity_count,
              service.recent_action_count,
              service.recently_enrolled_programme_count,
              service.active_target? ? 'Y' : 'N',
              service.transport_surveys? ? 'Y' : 'N',
              service.temperature_recordings? ? 'Y' : 'N',
              service.audits? ? 'Y' : 'N',
              service.recently_logged_in_user_count,
              service.most_recent_login.present? ? service.most_recent_login.iso8601 : nil
            ]
          end
        end
      end
    end
  end
end