Automatically detect the survey type by grade

pull/1/head
rebuilt 3 years ago
parent cfece47993
commit 5e0ba556bf

@ -24,6 +24,26 @@ class SurveyItem < ActiveRecord::Base
scope :short_form_items, lambda {
where(on_short_form: true)
}
scope :early_education_surveys, lambda {
where("survey_item_id LIKE '%-%-es%'")
}
scope :survey_items_for_grade, lambda { |school, academic_year, grade|
includes(:survey_item_responses)
.where("survey_item_responses.grade": grade,
"survey_item_responses.school": school,
"survey_item_responses.academic_year": academic_year)
.pluck(:survey_item_id).to_set
}
scope :survey_type_for_grade, lambda { |school, academic_year, grade|
survey_items_set_by_grade = survey_items_for_grade(school, academic_year, grade)
if survey_items_set_by_grade.size > 0 && survey_items_set_by_grade.subset?(early_education_surveys.pluck(:survey_item_id).to_set)
return :early_education
end
:regular
}
def description
DataAvailability.new(survey_item_id, prompt, true)

@ -26,10 +26,4 @@ class SurveyItemResponse < ActiveRecord::Base
SurveyItemResponse.where(survey_item: survey_items, school:,
academic_year:, gender:).group(:survey_item).average(:likert_score)
}
# grouped_responses = SurveyItemResponse.where(academic_year:, school:, survey_item: student_survey_items).group(["survey_item_id, grade"]).count
# grouped_responses = SurveyItemResponse.select("survey_item_id, grade, count(survey_item_id) as count").group("survey_item_id, grade")
# grade_zeroitems = SurveyItemResponse.where(grade: 0, school:, academic_year:).select(:survey_item_id).distinct.to_set
# grade_zeroitems.subset? all_student_survey_items
# SurveyItem.includes(:survey_item_responses).where("survey_item_responses.grade": 0,"survey_item_responses.school": school , "survey_item_responses.academic_year": academic_year).distinct(:survey_item_id).count
end

@ -147,6 +147,14 @@ FactoryBot.define do
approval_low_benchmark { 4.0 }
ideal_low_benchmark { 4.5 }
end
factory :early_education_survey_item do
survey_item_id { "s-#{rand}-es#{rand}" }
watch_low_benchmark { 2.0 }
growth_low_benchmark { 3.0 }
approval_low_benchmark { 4.0 }
ideal_low_benchmark { 4.5 }
end
end
factory :survey_item_response do

@ -38,4 +38,34 @@ RSpec.describe SurveyItem, type: :model do
end
end
end
describe '.survey_type_for_grade' do
let(:early_education_survey_item1) { create(:early_education_survey_item, scale:) }
context 'when no responses exist' do
it 'it returns back a regular survey' do
expect(SurveyItem.survey_type_for_grade(school, academic_year, 0)).to eq :regular
end
end
context 'when some responses exist' do
context 'and the responses are only within the set of early education survey items' do
before :each do
create(:survey_item_response, survey_item: early_education_survey_item1, school:, academic_year:, grade: 0)
end
it 'reports the survey type as early education' do
expect(SurveyItem.survey_type_for_grade(school, academic_year, 0)).to eq :early_education
end
end
context 'when there are responses for both early education and regular survey items' do
before :each do
create(:survey_item_response, school:, academic_year:, grade: 0)
end
it 'reports the survey type as regular' do
expect(SurveyItem.survey_type_for_grade(school, academic_year, 0)).to eq :regular
end
end
end
end
end

Loading…
Cancel
Save