diff --git a/app/models/measure.rb b/app/models/measure.rb index 81102656..f24392c9 100644 --- a/app/models/measure.rb +++ b/app/models/measure.rb @@ -25,8 +25,8 @@ class Measure < ActiveRecord::Base end def student_survey_items_with_sufficient_responses(school:, academic_year:) - @student_survey_items_with_sufficient_responses ||= - SurveyItem.where(id: SurveyItem.joins('inner join survey_item_responses on survey_item_responses.survey_item_id = survey_items.id') + @student_survey_items_with_sufficient_responses ||= Hash.new do |memo, (school, academic_year)| + memo[[school, academic_year]] = SurveyItem.where(id: SurveyItem.joins("inner join survey_item_responses on survey_item_responses.survey_item_id = survey_items.id") .student_survey_items .where("survey_item_responses.school": school, "survey_item_responses.academic_year": academic_year, @@ -34,6 +34,8 @@ class Measure < ActiveRecord::Base .group('survey_items.id') .having('count(*) >= 10') .count.keys) + end + @student_survey_items_with_sufficient_responses[[school, academic_year]] end def teacher_scales diff --git a/app/presenters/analyze/graph/column/all_data.rb b/app/presenters/analyze/graph/column/all_data.rb index 3bd7fe00..aba0ac86 100644 --- a/app/presenters/analyze/graph/column/all_data.rb +++ b/app/presenters/analyze/graph/column/all_data.rb @@ -31,6 +31,11 @@ module Analyze def basis "student surveys" end + + def n_size(academic_year) + SurveyItemResponse.where(survey_item: measure.student_survey_items, school:, grade: grades, + academic_year:).select(:response_id).distinct.count + end end end end diff --git a/app/presenters/analyze/graph/column/all_student.rb b/app/presenters/analyze/graph/column/all_student.rb index e51f201c..358ad321 100644 --- a/app/presenters/analyze/graph/column/all_student.rb +++ b/app/presenters/analyze/graph/column/all_student.rb @@ -13,8 +13,8 @@ module Analyze end def show_insufficient_data_message? - scores = academic_years.map do |year| - measure.score(school:, academic_year: year) + scores = academic_years.map do |academic_year| + measure.student_score(school:, academic_year:) end scores.none? { |score| score.meets_student_threshold? } @@ -27,6 +27,11 @@ module Analyze def type :student end + + def n_size(academic_year) + SurveyItemResponse.where(survey_item: measure.student_survey_items, school:, grade: grades, + academic_year:).select(:response_id).distinct.count + end end end end diff --git a/app/presenters/analyze/graph/column/grouped_bar_column_presenter.rb b/app/presenters/analyze/graph/column/grouped_bar_column_presenter.rb index c08da3c2..08480c57 100644 --- a/app/presenters/analyze/graph/column/grouped_bar_column_presenter.rb +++ b/app/presenters/analyze/graph/column/grouped_bar_column_presenter.rb @@ -21,13 +21,13 @@ module Analyze end def bars - @bars ||= yearly_scores.map.each_with_index do |yearly_score, index| - year = yearly_score.year - Analyze::BarPresenter.new(measure:, academic_year: year, - score: yearly_score.score, + @bars ||= academic_years.map.with_index do |academic_year, index| + Analyze::BarPresenter.new(measure:, academic_year:, + score: score(academic_year), x_position: bar_x(index), - color: bar_color(year)) + color: bar_color(academic_year)) end.reject(&:blank?).select { |bar| bar.score.average&.positive? } + @bars end def label @@ -46,6 +46,10 @@ module Analyze raise NotImplementedError end + def n_size(academic_year) + raise NotImplementedError + end + def basis "student surveys" end @@ -107,11 +111,6 @@ module Analyze %i[student teacher].include? type end - def n_size(academic_year) - SurveyItemResponse.where(survey_item: measure.student_survey_items, school:, grade: grades, - academic_year:).select(:response_id).distinct.count - end - def popover_content(academic_year) "#{n_size(academic_year)} #{type.to_s.capitalize}s" end @@ -126,12 +125,12 @@ module Analyze private - YearlyScore = Struct.new(:year, :score) - def yearly_scores - yearly_scores = academic_years.each.map do |year| - YearlyScore.new(year, score(year)) - end.reject { |year| year.score.nil? || year.score.blank? } - end + # YearlyScore = Struct.new(:year, :score) + # def yearly_scores + # @yearly_scores ||= academic_years.each.map do |year| + # YearlyScore.new(year, score(year)) + # end.reject { |year| year.score.nil? || year.score.blank? } + # end def bar_color(year) @available_academic_years ||= AcademicYear.order(:range).all @@ -140,7 +139,7 @@ module Analyze def bar_x(index) column_start_x + (index * bar_width * 1.2) + - ((column_end_x - column_start_x) - (yearly_scores.size * bar_width * 1.2)) / 2 + ((column_end_x - column_start_x) - (academic_years.size * bar_width * 1.2)) / 2 end end end