diff --git a/app/assets/javascripts/dashboard.js b/app/assets/javascripts/dashboard.js new file mode 100644 index 00000000..40b3a8cc --- /dev/null +++ b/app/assets/javascripts/dashboard.js @@ -0,0 +1,34 @@ +document.addEventListener("DOMContentLoaded", function() { + const selectSchoolElement = document.querySelector('#select-school'); + + selectSchoolElement.addEventListener('change', (event) => { + change_school(event); + }); + + const selectDistrictElement = document.querySelector('#select-district'); + + selectDistrictElement.addEventListener('change', (event) => { + change_district(event); + }); +}); + +function change_school(event) { + const school_slug = event.target.value; + + const district_regex = /districts\/(.+)\/schools/; + const district_slug = window.location.pathname.match(district_regex)[1]; + + const year_range_regex = /year=(.+)/; + const year_range = window.location.search.match(year_range_regex)[1]; + + window.location = `/districts/${district_slug}/schools/${school_slug}/dashboard?year=${year_range}`; +}; + +function change_district(event) { + const district_slug = event.target.value; + + const year_range_regex = /year=(.+)/; + const year_range = window.location.search.match(year_range_regex)[1]; + + window.location = `/districts/${district_slug}/schools/first/dashboard?year=${year_range}`; +}; diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 978510b4..3b141629 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -1,5 +1,7 @@ class DashboardController < ApplicationController def index + schools + districts authenticate(district.name.downcase, "#{district.name.downcase}!") @measure_graph_row_presenters = measure_ids .map { |measure_id| Measure.find_by_measure_id measure_id } @@ -23,13 +25,22 @@ class DashboardController < ApplicationController end def school + @school = schools.first if params[:school_id] == "first" @school ||= School.find_by_slug school_slug end + def schools + @schools = School.where(district: district).sort_by { | school| school.name } + end + def district @district ||= District.find_by_slug district_slug end + def districts + @districts = District.all.sort_by {|district| district.name} + end + def district_slug params[:district_id] end diff --git a/app/services/survey_response_aggregator.rb b/app/services/survey_response_aggregator.rb index c7e8a016..66e2064b 100644 --- a/app/services/survey_response_aggregator.rb +++ b/app/services/survey_response_aggregator.rb @@ -1,4 +1,5 @@ class SurveyResponseAggregator + # Returns an average score for all SurveyItemResponses for the given AcademicYear, School, and Measure def self.score(academic_year:, school:, measure:) SurveyItemResponse .where(academic_year: academic_year, school: school) @@ -6,4 +7,11 @@ class SurveyResponseAggregator .map { |survey_item_response| survey_item_response.likert_score } .average end + + # Returns an array of SurveyItemResponses for the given AcademicYear, School, and Measure + def self.find_responses_by_measure(academic_year:, school:, measure:) + SurveyItemResponse + .where(academic_year: academic_year, school: school) + .joins(:survey_item).where('survey_items.measure_id': measure.id) + end end diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index 8881effa..31c59a99 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -7,12 +7,20 @@ - + <% @districts.each do |district| %> + + <% end %> - + <% @schools.each do |school| %> + + <% end %> diff --git a/spec/features/school_dashboard_feature_spec.rb b/spec/features/school_dashboard_feature_spec.rb index 82c225af..2c0c9f3b 100644 --- a/spec/features/school_dashboard_feature_spec.rb +++ b/spec/features/school_dashboard_feature_spec.rb @@ -3,6 +3,7 @@ require 'rails_helper' feature 'School dashboard', type: feature do let(:district) { District.find_by_slug 'winchester' } let(:school) { School.find_by_slug 'winchester-high-school' } + let(:school_in_same_district) { School.find_by_slug 'muraco-elementary-school' } let(:measure_1A_i) { Measure.find_by_measure_id('1A-i') } let(:measure_2A_i) { Measure.find_by_measure_id('2A-i') } @@ -19,6 +20,9 @@ feature 'School dashboard', type: feature do let(:ay_2020_21) { AcademicYear.find_by_range '2020-21' } + let(:username) { 'winchester' } + let(:password) { 'winchester!' } + before :each do SurveyItemResponse.create response_id: rand.to_s, academic_year: ay_2020_21, school: school, survey_item: survey_item_1_for_measure_1A_i, likert_score: 4 @@ -76,6 +80,43 @@ feature 'School dashboard', type: feature do expect(professional_qualifications_row_index).to be < problem_solving_emphasis_row_index end - let(:username) { 'winchester' } - let(:password) { 'winchester!' } + # visit photos_path + # assert_selector 'h1', text: 'Photos' + # assert_equal 1, all('.photo-deletable').count + # click_on 'Delete' + # page.driver.browser.switch_to.alert.accept + scenario 'user sees schools in the same district' do + page.driver.browser.basic_authorize(username, password) + visit "/districts/#{district.slug}/schools/#{school.slug}/dashboard?year=#{ay_2020_21.range}" + + assert_selector "h1", text: school.name + + expected_num_of_schools = district.schools.count + expect(page.all('.school-options').count).to eq expected_num_of_schools + expect(page.all('.school-options[selected]').count).to eq 1 + expect(page.all('.school-options[selected]')[0].text).to eq 'Winchester High School' + + school_options = page.all('.school-options') + school_options.each_with_index do |school , index| + break if index == school_options.length-1 + expect(school.text).to be < school_options[index+1].text + end + end + + scenario 'user sees all districts in dropdown menu' do + page.driver.browser.basic_authorize(username, password) + visit "/districts/#{district.slug}/schools/#{school.slug}/dashboard?year=#{ay_2020_21.range}" + + expected_num_of_districts = District.count + expect(page.all('.district-options').count).to eq expected_num_of_districts + expect(page.all('.district-options[selected]').count).to eq 1 + expect(page.all('.district-options[selected]')[0].text).to eq 'Winchester' + + district_options = page.all('.district-options') + district_options.each_with_index do |district , index| + break if index == district_options.length-1 + expect(district.text).to be < district_options[index+1].text + end + end + end diff --git a/spec/services/survey_response_aggregator_spec.rb b/spec/services/survey_response_aggregator_spec.rb index 3e6b2cf3..51c3fd8e 100644 --- a/spec/services/survey_response_aggregator_spec.rb +++ b/spec/services/survey_response_aggregator_spec.rb @@ -60,4 +60,11 @@ describe SurveyResponseAggregator do expect(SurveyResponseAggregator.score(academic_year: ay_2021_22, school: school_b, measure: measure_b)).to eq 4.0 end end + + describe '.find_responses_by_measure' do + it 'returns all survey item responses corresponding to a year school and measure' do + expect(SurveyResponseAggregator.find_responses_by_measure(academic_year: ay_2020_21, school: school_a, measure: measure_a).count).to eq 2 + end + + end end