fix: Overall response rate was incorrectly using the updated_at date instead of the recorded date. Also, it was just using the last date for all academic years instead of the last date the survey was taken per academic year.

mciea-main
rebuilt 2 years ago
parent bf5b9e88f3
commit aafaeeaf41

@ -5,12 +5,17 @@ class ResponseRatePresenter
@focus = focus @focus = focus
@academic_year = academic_year @academic_year = academic_year
@school = school @school = school
@survey_items = SurveyItem.student_survey_items if focus == :student if focus == :student
@survey_items = Measure.all.flat_map do |measure|
measure.student_survey_items_with_sufficient_responses(school:, academic_year:)
end
end
@survey_items = SurveyItem.teacher_survey_items if focus == :teacher @survey_items = SurveyItem.teacher_survey_items if focus == :teacher
end end
def date def date
SurveyItemResponse.where(survey_item: survey_items, school:).order(updated_at: :DESC).first&.updated_at || Date.new SurveyItemResponse.where(survey_item: survey_items, school:,
academic_year:).order(recorded_date: :DESC).first&.recorded_date || Date.today
end end
def percentage def percentage
@ -20,11 +25,11 @@ class ResponseRatePresenter
end end
def color def color
percentage > 75 ? 'purple' : 'gold' percentage > 75 ? "purple" : "gold"
end end
def hover_message def hover_message
"Percentages based on #{ actual_count } out of #{ respondents_count.round } #{ focus }s completing at least 25% of the survey." "Percentages based on #{actual_count} out of #{respondents_count.round} #{focus}s completing at least 25% of the survey."
end end
private private

@ -1,55 +1,67 @@
require 'rails_helper' require "rails_helper"
describe ResponseRatePresenter do describe ResponseRatePresenter do
let(:academic_year) { create(:academic_year, range: '2022-23') } let(:academic_year) { create(:academic_year, range: "2022-23") }
let(:school) { create(:school, name: 'A school') } let(:school) { create(:school, name: "A school") }
let(:respondents) { create(:respondent, school:, academic_year:, total_students: 40, total_teachers: 40) } let(:respondents) { create(:respondent, school:, academic_year:, total_students: 40, total_teachers: 40) }
let(:wrong_school) { create(:school, name: 'Wrong school') } let(:wrong_school) { create(:school, name: "Wrong school") }
let(:wrong_academic_year) { create(:academic_year) } let(:wrong_academic_year) { create(:academic_year) }
let(:wrong_respondents) do let(:wrong_respondents) do
create(:respondent, school: wrong_school, academic_year: wrong_academic_year, total_students: 40, create(:respondent, school: wrong_school, academic_year: wrong_academic_year, total_students: 40,
total_teachers: 40) total_teachers: 40)
end end
let(:today) { Date.today }
let(:yesterday) { Date.today - 1 }
let(:two_days_ago) { Date.today - 2 }
let(:three_days_ago) { Date.today - 3 }
let(:student_survey_item) { create(:student_survey_item) } let(:student_survey_item) { create(:student_survey_item) }
let(:teacher_survey_item) { create(:teacher_survey_item) } let(:teacher_survey_item) { create(:teacher_survey_item) }
let(:oldest_student_survey_response) do let(:oldest_student_survey_response) do
create(:survey_item_response, school:, academic_year:, survey_item: student_survey_item) create(:survey_item_response, school:, academic_year:, survey_item: student_survey_item,
recorded_date: three_days_ago)
end end
let(:newest_student_survey_response) do let(:newest_student_survey_response) do
create(:survey_item_response, school:, academic_year:, survey_item: student_survey_item) create(:survey_item_response, school:, academic_year:, survey_item: student_survey_item, recorded_date: yesterday)
end end
let(:oldest_teacher_survey_response) do let(:oldest_teacher_survey_response) do
create(:survey_item_response, school:, academic_year:, survey_item: teacher_survey_item) create(:survey_item_response, school:, academic_year:, survey_item: teacher_survey_item,
recorded_date: three_days_ago)
end end
let(:newest_teacher_survey_response) do let(:newest_teacher_survey_response) do
create(:survey_item_response, school:, academic_year:, survey_item: teacher_survey_item) create(:survey_item_response, school:, academic_year:, survey_item: teacher_survey_item, recorded_date: yesterday)
end end
let(:wrong_student_survey_response) do let(:wrong_student_survey_response) do
create(:survey_item_response, school: wrong_school, academic_year: wrong_academic_year, create(:survey_item_response, school: wrong_school, academic_year: wrong_academic_year,
survey_item: student_survey_item) survey_item: student_survey_item, recorded_date: two_days_ago)
end end
let(:wrong_teacher_survey_response) do let(:wrong_teacher_survey_response) do
create(:survey_item_response, school: wrong_school, academic_year: wrong_academic_year, create(:survey_item_response, school: wrong_school, academic_year: wrong_academic_year,
survey_item: teacher_survey_item) survey_item: teacher_survey_item, recorded_date: two_days_ago)
end end
context '.date' do let(:filler_survey_item_responses_to_meet_sufficiency) do
context 'when focus is student' do create_list(:survey_item_response, 10, school:, academic_year:,
survey_item: student_survey_item, recorded_date: two_days_ago)
end
context ".date" do
context "when focus is student" do
before :each do before :each do
oldest_student_survey_response oldest_student_survey_response
newest_student_survey_response newest_student_survey_response
wrong_student_survey_response wrong_student_survey_response
wrong_teacher_survey_response wrong_teacher_survey_response
filler_survey_item_responses_to_meet_sufficiency
end end
it 'ignores all teacher items and only gets the modified date of the last student item' do it "ignores all teacher items and only gets the modified date of the last student item" do
percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).date date = ResponseRatePresenter.new(focus: :student, academic_year:, school:).date
expect(percentage).to eq(newest_student_survey_response.updated_at) expect(date).to eq(newest_student_survey_response.recorded_date)
end end
end end
context 'when focus is teacher' do context "when focus is teacher" do
before :each do before :each do
oldest_teacher_survey_response oldest_teacher_survey_response
newest_teacher_survey_response newest_teacher_survey_response
@ -57,107 +69,107 @@ describe ResponseRatePresenter do
wrong_teacher_survey_response wrong_teacher_survey_response
end end
it 'ignores all student responses and only gets the modified date of the last teacher item' do it "ignores all student responses and only gets the modified date of the last teacher item" do
percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).date date = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).date
expect(percentage).to eq(newest_teacher_survey_response.updated_at) expect(date).to eq(newest_teacher_survey_response.recorded_date)
end end
end end
end end
context '.percentage' do context ".percentage" do
before :each do before :each do
respondents respondents
wrong_respondents wrong_respondents
end end
context 'when no survey responses are found for a school' do context "when no survey responses are found for a school" do
it 'returns a response rate of 0' do it "returns a response rate of 0" do
percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage
expect(percentage).to eq(0) expect(percentage).to eq(0)
end end
end end
context 'when there all possible teacher respondents answered questions' do context "when there all possible teacher respondents answered questions" do
before :each do before :each do
create_list(:survey_item_response, 40, school:, academic_year:, create_list(:survey_item_response, 40, school:, academic_year:,
survey_item: teacher_survey_item) survey_item: teacher_survey_item)
end end
it 'returns a response rate of 100' do it "returns a response rate of 100" do
percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage
expect(percentage).to eq(100) expect(percentage).to eq(100)
end end
end end
context 'when more teachers responded than staff the school' do context "when more teachers responded than staff the school" do
before :each do before :each do
create_list(:survey_item_response, 80, school:, academic_year:, create_list(:survey_item_response, 80, school:, academic_year:,
survey_item: teacher_survey_item) survey_item: teacher_survey_item)
end end
it 'returns a response rate of 100' do it "returns a response rate of 100" do
percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage
expect(percentage).to eq(100) expect(percentage).to eq(100)
end end
end end
context 'when three quarters of the teachers responded to the survey' do context "when three quarters of the teachers responded to the survey" do
before :each do before :each do
create_list(:survey_item_response, 30, school:, academic_year:, create_list(:survey_item_response, 30, school:, academic_year:,
survey_item: teacher_survey_item) survey_item: teacher_survey_item)
end end
it 'returns a response rate of 75' do it "returns a response rate of 75" do
percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage
expect(percentage).to eq(75) expect(percentage).to eq(75)
end end
end end
context 'when one quarter of the teachers responded to the survey' do context "when one quarter of the teachers responded to the survey" do
before :each do before :each do
create_list(:survey_item_response, 10, school:, academic_year:, create_list(:survey_item_response, 10, school:, academic_year:,
survey_item: teacher_survey_item) survey_item: teacher_survey_item)
end end
it 'returns a response rate of 25' do it "returns a response rate of 25" do
percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage
expect(percentage).to eq(25) expect(percentage).to eq(25)
end end
end end
context 'When the percentage is not a round number' do context "When the percentage is not a round number" do
before :each do before :each do
create_list(:survey_item_response, 9, school:, academic_year:, create_list(:survey_item_response, 9, school:, academic_year:,
survey_item: teacher_survey_item) survey_item: teacher_survey_item)
end end
it 'its rounded to the nearest integer' do it "its rounded to the nearest integer" do
percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :teacher, academic_year:, school:).percentage
expect(percentage).to eq(23) expect(percentage).to eq(23)
end end
end end
context 'when there all possible student respondents answered questions' do context "when there all possible student respondents answered questions" do
before :each do before :each do
create_list(:survey_item_response, 40, school:, academic_year:, create_list(:survey_item_response, 40, school:, academic_year:,
survey_item: student_survey_item) survey_item: student_survey_item)
end end
it 'returns a response rate of 100' do it "returns a response rate of 100" do
percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage
expect(percentage).to eq(100) expect(percentage).to eq(100)
end end
end end
context 'when half of all students responded' do context "when half of all students responded" do
before :each do before :each do
create_list(:survey_item_response, 20, school:, academic_year:, create_list(:survey_item_response, 20, school:, academic_year:,
survey_item: student_survey_item) survey_item: student_survey_item)
end end
it 'returns a response rate of 50' do it "returns a response rate of 50" do
percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage
expect(percentage).to eq(50) expect(percentage).to eq(50)
end end
end end
context 'when only a subset of grades was given the survey' do context "when only a subset of grades was given the survey" do
before :each do before :each do
respondents.one = 20 respondents.one = 20
respondents.two = 20 respondents.two = 20
@ -166,71 +178,71 @@ describe ResponseRatePresenter do
respondents.five = 20 respondents.five = 20
respondents.save respondents.save
end end
context 'and only first grade was given the survey' do context "and only first grade was given the survey" do
context 'and all the first grade responded' do context "and all the first grade responded" do
before :each do before :each do
create_list(:survey_item_response, 20, school:, academic_year:, create_list(:survey_item_response, 20, school:, academic_year:,
survey_item: student_survey_item, grade: 1) survey_item: student_survey_item, grade: 1)
end end
it 'returns a response rate of 100' do it "returns a response rate of 100" do
percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage
expect(percentage).to eq(100) expect(percentage).to eq(100)
end end
end end
context 'and half of first grade responded' do context "and half of first grade responded" do
before :each do before :each do
create_list(:survey_item_response, 10, school:, academic_year:, create_list(:survey_item_response, 10, school:, academic_year:,
survey_item: student_survey_item, grade: 1) survey_item: student_survey_item, grade: 1)
end end
it 'returns a response rate of 50' do it "returns a response rate of 50" do
percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage
expect(percentage).to eq(50) expect(percentage).to eq(50)
end end
end end
end end
context 'and two grades responded' do context "and two grades responded" do
context 'and both grades responded fully' do context "and both grades responded fully" do
before :each do before :each do
create_list(:survey_item_response, 20, school:, academic_year:, create_list(:survey_item_response, 20, school:, academic_year:,
survey_item: student_survey_item, grade: 1) survey_item: student_survey_item, grade: 1)
create_list(:survey_item_response, 20, school:, academic_year:, create_list(:survey_item_response, 20, school:, academic_year:,
survey_item: student_survey_item, grade: 2) survey_item: student_survey_item, grade: 2)
end end
it 'returns a response rate of 100' do it "returns a response rate of 100" do
percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage
expect(percentage).to eq(100) expect(percentage).to eq(100)
end end
end end
context 'and half of first grade responded' do context "and half of first grade responded" do
before :each do before :each do
create_list(:survey_item_response, 10, school:, academic_year:, create_list(:survey_item_response, 10, school:, academic_year:,
survey_item: student_survey_item, grade: 1) survey_item: student_survey_item, grade: 1)
create_list(:survey_item_response, 20, school:, academic_year:, create_list(:survey_item_response, 20, school:, academic_year:,
survey_item: student_survey_item, grade: 2) survey_item: student_survey_item, grade: 2)
end end
it 'returns a response rate of 75' do it "returns a response rate of 75" do
percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage
expect(percentage).to eq(75) expect(percentage).to eq(75)
end end
end end
context 'and a quarter of first grade responded' do context "and a quarter of first grade responded" do
before :each do before :each do
create_list(:survey_item_response, 5, school:, academic_year:, create_list(:survey_item_response, 5, school:, academic_year:,
survey_item: student_survey_item, grade: 1) survey_item: student_survey_item, grade: 1)
create_list(:survey_item_response, 20, school:, academic_year:, create_list(:survey_item_response, 20, school:, academic_year:,
survey_item: student_survey_item, grade: 2) survey_item: student_survey_item, grade: 2)
end end
it 'returns a response rate of 63 (rounded up from 62.5)' do it "returns a response rate of 63 (rounded up from 62.5)" do
percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage
expect(percentage).to eq(63) expect(percentage).to eq(63)
end end
end end
end end
context 'and three grades responded' do context "and three grades responded" do
context 'and all three grades responded fully' do context "and all three grades responded fully" do
before :each do before :each do
create_list(:survey_item_response, 20, school:, academic_year:, create_list(:survey_item_response, 20, school:, academic_year:,
survey_item: student_survey_item, grade: 1) survey_item: student_survey_item, grade: 1)
@ -239,7 +251,7 @@ describe ResponseRatePresenter do
create_list(:survey_item_response, 20, school:, academic_year:, create_list(:survey_item_response, 20, school:, academic_year:,
survey_item: student_survey_item, grade: 3) survey_item: student_survey_item, grade: 3)
end end
it 'returns a response rate of 100' do it "returns a response rate of 100" do
percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage percentage = ResponseRatePresenter.new(focus: :student, academic_year:, school:).percentage
expect(percentage).to eq(100) expect(percentage).to eq(100)
end end

Loading…
Cancel
Save