mirror of
https://github.com/edcommonwealth/sqm-dashboards.git
synced 2026-03-07 21:48:16 -08:00
ECP-30 Delete legacy code and database tables
This commit is contained in:
parent
5bc9d27b64
commit
94c5b1acba
131 changed files with 224 additions and 17561 deletions
|
|
@ -1,7 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Legacy
|
||||
def self.table_name_prefix
|
||||
'legacy_'
|
||||
end
|
||||
end
|
||||
|
|
@ -1,91 +0,0 @@
|
|||
module Legacy
|
||||
class Attempt < ApplicationRecord
|
||||
belongs_to :schedule
|
||||
belongs_to :recipient
|
||||
belongs_to :recipient_schedule
|
||||
belongs_to :question
|
||||
belongs_to :student
|
||||
|
||||
after_save :update_school_categories
|
||||
after_commit :update_counts
|
||||
|
||||
scope :for_question, ->(question) { where(question_id: question.id) }
|
||||
scope :for_recipient, ->(recipient) { where(recipient_id: recipient.id) }
|
||||
scope :for_student, ->(student) { where(student_id: student.id) }
|
||||
scope :for_category, ->(category) { joins(:question).merge(Question.for_category(category)) }
|
||||
scope :for_school, ->(school) { joins(:recipient).merge(Legacy::Recipient.for_school(school)) }
|
||||
scope :with_answer, -> { where("answer_index is not null or open_response_id is not null") }
|
||||
scope :with_no_answer, -> { where("answer_index is null and open_response_id is null") }
|
||||
scope :not_yet_responded, -> { where(responded_at: nil) }
|
||||
scope :last_sent, -> { order(sent_at: :desc) }
|
||||
scope :created_in, ->(year) { where("extract(year from legacy_attempts.created_at) = ?", year) }
|
||||
|
||||
def messages
|
||||
child_specific = student.present? ? " (for #{student.name})" : ""
|
||||
|
||||
cancel_text = "\nSkip question: skip\nStop all questions: stop"
|
||||
|
||||
[
|
||||
"#{question.text}#{child_specific}",
|
||||
"#{question.option1}: Reply 1\n#{question.option2}: 2\n#{question.option3}: 3\n#{question.option4}: 4\n#{question.option5}: 5"
|
||||
]
|
||||
end
|
||||
|
||||
def send_message
|
||||
twilio_number = ENV["TWILIO_NUMBER"]
|
||||
client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]
|
||||
|
||||
sids = []
|
||||
messages.each do |message|
|
||||
sids << client.messages.create(
|
||||
from: twilio_number,
|
||||
to: recipient.phone,
|
||||
body: message
|
||||
).sid
|
||||
end
|
||||
|
||||
update(sent_at: Time.new, twilio_sid: sids.join(","))
|
||||
recipient.update(phone: client.messages.get(sids.last).to)
|
||||
end
|
||||
|
||||
def response
|
||||
return "No Answer Yet" if answer_index.blank?
|
||||
|
||||
question.options[answer_index_with_reverse - 1]
|
||||
end
|
||||
|
||||
def save_response(answer_index: nil, twilio_details: nil, responded_at: Time.new)
|
||||
update(
|
||||
answer_index:,
|
||||
twilio_details:,
|
||||
responded_at:
|
||||
)
|
||||
|
||||
recipient_schedule.update(next_attempt_at: Time.new) if recipient_schedule.queued_question_ids.present?
|
||||
end
|
||||
|
||||
def answer_index_with_reverse
|
||||
return 6 - answer_index if question.reverse?
|
||||
|
||||
answer_index
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_school_categories
|
||||
return if ENV["BULK_PROCESS"]
|
||||
|
||||
school_category = SchoolCategory.for(recipient.school, question.category).first
|
||||
if school_category.nil?
|
||||
school_category = SchoolCategory.create(school: recipient.school, category: question.category)
|
||||
end
|
||||
school_category.sync_aggregated_responses
|
||||
end
|
||||
|
||||
def update_counts
|
||||
return if ENV["BULK_PROCESS"]
|
||||
|
||||
recipient.update_counts
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
module Legacy
|
||||
class Category < ApplicationRecord
|
||||
has_many :questions
|
||||
belongs_to :parent_category, class_name: 'Legacy::Category', foreign_key: :parent_category_id
|
||||
has_many :child_categories, class_name: 'Legacy::Category', foreign_key: :parent_category_id
|
||||
has_many :school_categories
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
scope :for_parent, ->(category = nil) { where(parent_category_id: category.try(:id)) }
|
||||
scope :likert, -> { where('benchmark is null') }
|
||||
|
||||
include FriendlyId
|
||||
friendly_id :name, use: [:slugged]
|
||||
|
||||
def path
|
||||
p = self
|
||||
items = [p]
|
||||
items << p while (p = p.try(:parent_category))
|
||||
items.uniq.compact.reverse
|
||||
end
|
||||
|
||||
def root_identifier
|
||||
path.first.name.downcase.gsub(/\s/, '-')
|
||||
end
|
||||
|
||||
def root_index
|
||||
Category.root_identifiers.index(root_identifier) || 0
|
||||
end
|
||||
|
||||
def self.root_identifiers
|
||||
%w[
|
||||
teachers-and-the-teaching-environment
|
||||
school-culture
|
||||
resources
|
||||
academic-learning
|
||||
community-and-wellbeing
|
||||
pilot-family-questions
|
||||
]
|
||||
end
|
||||
|
||||
def self.root
|
||||
Category.where(parent_category: nil).select { |c| root_identifiers.index(c.slug) }
|
||||
end
|
||||
|
||||
def custom_zones
|
||||
return [] if zones.nil?
|
||||
|
||||
zones.split(',').map(&:to_f)
|
||||
end
|
||||
|
||||
def zone_widths
|
||||
return nil if zones.nil?
|
||||
|
||||
widths = custom_zones.each_with_index.map do |zone, index|
|
||||
(zone - (index == 0 ? 0 : custom_zones[index - 1])).round(2)
|
||||
end
|
||||
|
||||
widths[4] = widths[4] + (5 - widths.sum)
|
||||
widths
|
||||
end
|
||||
|
||||
def sync_child_zones
|
||||
likert_child_categories = child_categories.likert
|
||||
return unless likert_child_categories.present?
|
||||
|
||||
total_zones = [0, 0, 0, 0, 0]
|
||||
valid_child_categories = 0
|
||||
likert_child_categories.each do |cc|
|
||||
cc.sync_child_zones if cc.zones.nil?
|
||||
|
||||
if cc.zones.nil?
|
||||
puts "NO ZONES: #{name} -> #{cc.name}"
|
||||
else
|
||||
valid_child_categories += 1
|
||||
|
||||
puts "ZONES: #{name} | #{cc.name} | #{cc.zones}"
|
||||
|
||||
cc.custom_zones.each_with_index do |zone, index|
|
||||
puts "ZONE: #{name} | #{zone} | #{index}"
|
||||
total_zones[index] += zone
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if valid_child_categories > 0
|
||||
average_zones = total_zones.map { |zone| zone / valid_child_categories }
|
||||
puts "TOTAL: #{name} | #{total_zones} | #{valid_child_categories} | #{average_zones} | #{zone_widths}"
|
||||
update(zones: average_zones.join(','))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
module Legacy
|
||||
class District < ApplicationRecord
|
||||
has_many :schools
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
scope :alphabetic, -> { order(name: :asc) }
|
||||
|
||||
include FriendlyId
|
||||
|
||||
friendly_id :name, use: [:slugged]
|
||||
|
||||
before_save do
|
||||
self.slug ||= name.parameterize
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
AggregatedResponses = Struct.new(
|
||||
:question,
|
||||
:category,
|
||||
:responses,
|
||||
:count,
|
||||
:answer_index_total,
|
||||
:answer_index_average,
|
||||
:most_popular_answer,
|
||||
:zscore
|
||||
)
|
||||
|
||||
module Legacy
|
||||
class Question < ApplicationRecord
|
||||
belongs_to :category
|
||||
|
||||
has_many :attempts
|
||||
|
||||
validates :text, presence: true
|
||||
validates :option1, presence: true
|
||||
validates :option2, presence: true
|
||||
validates :option3, presence: true
|
||||
validates :option4, presence: true
|
||||
validates :option5, presence: true
|
||||
|
||||
scope :for_category, ->(category) { where(category:) }
|
||||
scope :created_in, ->(year) { where("extract(year from #{table_name}.created_at) = ?", year) }
|
||||
|
||||
enum :target_group, %i[unknown for_students for_teachers for_parents]
|
||||
|
||||
def source
|
||||
target_group.gsub("for_", "")
|
||||
end
|
||||
|
||||
def options
|
||||
[option1, option2, option3, option4, option5]
|
||||
end
|
||||
|
||||
def options_with_reverse
|
||||
return options.reverse if reverse?
|
||||
|
||||
options
|
||||
end
|
||||
|
||||
def option_index(answer)
|
||||
options_with_reverse.map(&:downcase).map(&:strip).index(answer.downcase.strip)
|
||||
end
|
||||
|
||||
def aggregated_responses_for_school(school)
|
||||
school_responses = attempts.for_school(school).with_answer.order(id: :asc)
|
||||
return unless school_responses.present?
|
||||
|
||||
response_answer_total = school_responses.inject(0) do |total, response|
|
||||
total + response.answer_index_with_reverse
|
||||
end
|
||||
histogram = school_responses.group_by(&:answer_index_with_reverse)
|
||||
|
||||
most_popular_answer_index = histogram.to_a.sort_by { |info| info[1].length }.last[0]
|
||||
most_popular_answer = options_with_reverse[most_popular_answer_index - 1]
|
||||
|
||||
AggregatedResponses.new(
|
||||
self,
|
||||
category,
|
||||
school_responses,
|
||||
school_responses.length,
|
||||
response_answer_total,
|
||||
response_answer_total.to_f / school_responses.length.to_f,
|
||||
most_popular_answer
|
||||
)
|
||||
end
|
||||
|
||||
def normalized_text
|
||||
text.gsub("[science/math/English/social studies]", "")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
module Legacy
|
||||
class QuestionList < ApplicationRecord
|
||||
validates :name, presence: true
|
||||
validates :question_ids, presence: true
|
||||
|
||||
attr_accessor :question_id_array
|
||||
|
||||
before_validation :convert_question_id_array
|
||||
after_initialize :set_question_id_array
|
||||
|
||||
def questions
|
||||
question_id_array.collect { |id| Question.where(id:).first }.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def convert_question_id_array
|
||||
return if question_id_array.blank?
|
||||
|
||||
self.question_ids = question_id_array.reject { |id| id.to_s.empty? }.join(',')
|
||||
end
|
||||
|
||||
def set_question_id_array
|
||||
return if question_ids.blank?
|
||||
|
||||
self.question_id_array = question_ids.split(',').map(&:to_i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
module Legacy
|
||||
class Recipient < ApplicationRecord
|
||||
belongs_to :school
|
||||
validates_associated :school
|
||||
|
||||
has_many :recipient_schedules
|
||||
has_many :attempts
|
||||
|
||||
has_many :students
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
scope :for_school, ->(school) { where(school:) }
|
||||
scope :created_in, ->(year) { where('extract(year from recipients.created_at) = ?', year) }
|
||||
|
||||
before_destroy :sync_lists
|
||||
|
||||
def self.import(school, file)
|
||||
CSV.foreach(file.path, headers: true) do |row|
|
||||
school.recipients.create!(row.to_hash)
|
||||
# recipient_hash = row.to_hash
|
||||
# recipient = school.recipients.where(phone: recipient_hash["phone"])
|
||||
#
|
||||
# if recipient.count == 1
|
||||
# recipient.first.update(recipient_hash)
|
||||
# else
|
||||
# school.recipients.create!(recipient_hash)
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
||||
def update_counts
|
||||
update(
|
||||
attempts_count: attempts.count,
|
||||
responses_count: attempts.with_answer.count
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sync_lists
|
||||
school.recipient_lists.each do |recipient_list|
|
||||
next if recipient_list.recipient_id_array.index(id).nil?
|
||||
|
||||
updated_ids = recipient_list.recipient_id_array - [id]
|
||||
recipient_list.update(recipient_id_array: updated_ids)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
module Legacy
|
||||
class RecipientList < ApplicationRecord
|
||||
belongs_to :school
|
||||
has_many :schedules
|
||||
|
||||
validates_associated :school
|
||||
validates :name, presence: true
|
||||
|
||||
attr_accessor :recipient_id_array
|
||||
|
||||
before_validation :convert_recipient_id_array
|
||||
after_initialize :set_recipient_id_array
|
||||
|
||||
after_save :sync_recipient_schedules
|
||||
|
||||
def recipients
|
||||
recipient_id_array.collect { |id| school.recipients.where(id:).first }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def convert_recipient_id_array
|
||||
return if recipient_id_array.blank? || (recipient_ids_was != recipient_ids)
|
||||
|
||||
self.recipient_ids = recipient_id_array.reject { |id| id.to_s.empty? }.join(',')
|
||||
end
|
||||
|
||||
def set_recipient_id_array
|
||||
return if recipient_id_array.present?
|
||||
|
||||
self.recipient_id_array = (recipient_ids || '').split(',').map(&:to_i)
|
||||
end
|
||||
|
||||
def sync_recipient_schedules
|
||||
return unless recipient_ids_before_last_save.present? && recipient_ids_before_last_save != recipient_ids
|
||||
|
||||
old_ids = recipient_ids_before_last_save.split(/,/)
|
||||
new_ids = recipient_ids.split(/,/)
|
||||
(old_ids - new_ids).each do |deleted_recipient|
|
||||
schedules.each do |schedule|
|
||||
schedule.recipient_schedules.for_recipient(deleted_recipient).first.destroy
|
||||
end
|
||||
end
|
||||
|
||||
(new_ids - old_ids).each do |new_recipient|
|
||||
schedules.each do |schedule|
|
||||
RecipientSchedule.create_for_recipient(new_recipient, schedule)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,168 +0,0 @@
|
|||
module Legacy
|
||||
class RecipientSchedule < ApplicationRecord
|
||||
belongs_to :recipient
|
||||
belongs_to :schedule
|
||||
has_many :attempts
|
||||
|
||||
validates_associated :recipient
|
||||
validates_associated :schedule
|
||||
validates :next_attempt_at, presence: true
|
||||
|
||||
scope :ready, -> { where('next_attempt_at <= ?', Time.new) }
|
||||
scope :for_recipient, lambda { |recipient_or_recipient_id|
|
||||
id = if recipient_or_recipient_id.is_a?(Recipient)
|
||||
recipient_or_recipient_id.id
|
||||
else
|
||||
recipient_or_recipient_id
|
||||
end
|
||||
where(recipient_id: id)
|
||||
}
|
||||
scope :for_schedule, lambda { |schedule_or_schedule_id|
|
||||
id = if schedule_or_schedule_id.is_a?(Schedule)
|
||||
schedule_or_schedule_id.id
|
||||
else
|
||||
schedule_or_schedule_id
|
||||
end
|
||||
where(schedule_id: id)
|
||||
}
|
||||
|
||||
def next_question
|
||||
next_question_id = if queued_question_ids.present?
|
||||
queued_question_ids.split(/,/).first
|
||||
else
|
||||
upcoming_question_ids.split(/,/).first
|
||||
end
|
||||
Question.where(id: next_question_id).first
|
||||
end
|
||||
|
||||
def upcoming_question_id_array
|
||||
upcoming_question_ids.try(:split, /,/) || []
|
||||
end
|
||||
|
||||
def attempted_question_id_array
|
||||
attempted_question_ids.try(:split, /,/) || []
|
||||
end
|
||||
|
||||
def queued_question_id_array
|
||||
queued_question_ids.try(:split, /,/) || []
|
||||
end
|
||||
|
||||
def attempt_question_for_recipient_students(send_message: true, question: next_question)
|
||||
return if recipient.opted_out?
|
||||
return if question.nil?
|
||||
|
||||
return attempt_question(question:) unless question.for_recipient_students?
|
||||
|
||||
missing_students = []
|
||||
recipient_attempts = attempts.for_recipient(recipient).for_question(question)
|
||||
recipient.students.each do |student|
|
||||
missing_students << student if recipient_attempts.for_student(student).empty?
|
||||
end
|
||||
|
||||
attempt = recipient.attempts.create(
|
||||
schedule:,
|
||||
recipient_schedule: self,
|
||||
question:,
|
||||
student: missing_students.first
|
||||
)
|
||||
|
||||
if send_message && attempt.send_message
|
||||
upcoming = upcoming_question_id_array
|
||||
queued = queued_question_id_array
|
||||
attempted = attempted_question_id_array
|
||||
|
||||
if question.present?
|
||||
question_id = [question.id.to_s]
|
||||
upcoming -= question_id
|
||||
if missing_students.length > 1
|
||||
queued += question_id
|
||||
else
|
||||
attempted += question_id
|
||||
queued -= question_id
|
||||
end
|
||||
end
|
||||
|
||||
update(
|
||||
upcoming_question_ids: upcoming.empty? ? nil : upcoming.join(','),
|
||||
attempted_question_ids: attempted.empty? ? nil : attempted.join(','),
|
||||
queued_question_ids: queued.empty? ? nil : queued.join(','),
|
||||
last_attempt_at: attempt.sent_at,
|
||||
next_attempt_at: next_valid_attempt_time
|
||||
)
|
||||
end
|
||||
attempt
|
||||
end
|
||||
|
||||
def attempt_question(send_message: true, question: next_question)
|
||||
return if recipient.opted_out?
|
||||
|
||||
unanswered_attempt = recipient.attempts.not_yet_responded.last
|
||||
|
||||
return if question.nil? && unanswered_attempt.nil?
|
||||
|
||||
if unanswered_attempt.nil?
|
||||
return attempt_question_for_recipient_students(question:) if question.for_recipient_students?
|
||||
|
||||
attempt = recipient.attempts.create(
|
||||
schedule:,
|
||||
recipient_schedule: self,
|
||||
question:
|
||||
)
|
||||
end
|
||||
|
||||
if send_message && (unanswered_attempt || attempt).send_message
|
||||
upcoming = upcoming_question_id_array
|
||||
queued = queued_question_id_array
|
||||
attempted = attempted_question_id_array
|
||||
|
||||
if question.present?
|
||||
question_id = [question.id.to_s]
|
||||
upcoming -= question_id
|
||||
if unanswered_attempt.nil?
|
||||
attempted += question_id
|
||||
queued -= question_id
|
||||
else
|
||||
queued += question_id
|
||||
end
|
||||
end
|
||||
|
||||
update(
|
||||
upcoming_question_ids: upcoming.empty? ? nil : upcoming.join(','),
|
||||
attempted_question_ids: attempted.empty? ? nil : attempted.join(','),
|
||||
queued_question_ids: queued.empty? ? nil : queued.join(','),
|
||||
last_attempt_at: (unanswered_attempt || attempt).sent_at,
|
||||
next_attempt_at: next_valid_attempt_time
|
||||
)
|
||||
end
|
||||
(unanswered_attempt || attempt)
|
||||
end
|
||||
|
||||
def next_valid_attempt_time
|
||||
local_time = (next_attempt_at + (60 * 60 * schedule.frequency_hours)).in_time_zone('Eastern Time (US & Canada)')
|
||||
local_time += 1.day while local_time.on_weekend?
|
||||
local_time
|
||||
end
|
||||
|
||||
def self.create_for_recipient(recipient_or_recipient_id, schedule, next_attempt_at = nil)
|
||||
if next_attempt_at.nil?
|
||||
next_attempt_at = Time.at(schedule.start_date.to_time.to_i + (60 * schedule.time))
|
||||
next_attempt_at += 1.day while next_attempt_at.on_weekend?
|
||||
end
|
||||
|
||||
question_ids = schedule.question_list.question_ids.split(/,/)
|
||||
question_ids = question_ids.shuffle if schedule.random?
|
||||
|
||||
recipient_id = if recipient_or_recipient_id.is_a?(Recipient)
|
||||
recipient_or_recipient_id.id
|
||||
else
|
||||
recipient_or_recipient_id
|
||||
end
|
||||
|
||||
schedule.recipient_schedules.create(
|
||||
recipient_id:,
|
||||
upcoming_question_ids: question_ids.join(','),
|
||||
next_attempt_at:
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
module Legacy
|
||||
class Schedule < ApplicationRecord
|
||||
belongs_to :school
|
||||
belongs_to :recipient_list
|
||||
belongs_to :question_list
|
||||
has_many :recipient_schedules
|
||||
|
||||
validates :name, presence: true
|
||||
validates :recipient_list, presence: true
|
||||
validates :question_list, presence: true
|
||||
|
||||
before_validation :set_start_date
|
||||
after_create :create_recipient_schedules
|
||||
|
||||
scope :active, lambda {
|
||||
where(active: true).where('start_date <= ? and end_date > ?', Date.today, Date.today)
|
||||
}
|
||||
|
||||
private
|
||||
|
||||
def set_start_date
|
||||
return if start_date.present?
|
||||
|
||||
self.start_date = Date.today
|
||||
end
|
||||
|
||||
def create_recipient_schedules
|
||||
recipient_list.recipients.each do |recipient|
|
||||
RecipientSchedule.create_for_recipient(recipient, self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,89 +0,0 @@
|
|||
module Legacy
|
||||
class School < ApplicationRecord
|
||||
has_many :schedules, dependent: :destroy
|
||||
has_many :recipient_lists, dependent: :destroy
|
||||
belongs_to :district
|
||||
has_many :recipients, dependent: :destroy
|
||||
has_many :school_categories, dependent: :destroy
|
||||
has_many :user_schools, dependent: :destroy
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
scope :alphabetic, -> { order(name: :asc) }
|
||||
|
||||
include FriendlyId
|
||||
friendly_id :name, use: [:slugged]
|
||||
|
||||
def self.find_by_district_code_and_school_code(district_code, school_code)
|
||||
School
|
||||
.joins(:district)
|
||||
.where(districts: { qualtrics_code: district_code })
|
||||
.find_by_qualtrics_code(school_code)
|
||||
end
|
||||
|
||||
def available_responders_for(question)
|
||||
if question.for_students?
|
||||
student_count || 1
|
||||
elsif question.for_teachers?
|
||||
teacher_count || 1
|
||||
else
|
||||
1
|
||||
end
|
||||
end
|
||||
|
||||
def merge_into(school_name)
|
||||
school = district.schools.where(name: school_name).first
|
||||
if school.nil?
|
||||
puts "Unable to find school named #{school_name} in district (#{district.name})"
|
||||
return
|
||||
end
|
||||
puts "Merging #{name} (#{id}) in to #{school.name} (#{school.id})"
|
||||
schedules.update_all(school_id: school.id)
|
||||
recipient_lists.update_all(school_id: school.id)
|
||||
recipients.update_all(school_id: school.id)
|
||||
school_categories.each do |school_category|
|
||||
school_category.update(school_id: school.id)
|
||||
existing_school_category = school.school_categories.for(school_category.school,
|
||||
school_category.category).in(school_category.year)
|
||||
if existing_school_category.present?
|
||||
if existing_school_category.attempt_count == 0 && existing_school_category.zscore.nil?
|
||||
existing_school_category.destroy
|
||||
else
|
||||
school_category.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
reload
|
||||
|
||||
user_schools.update_all(school_id: school.id)
|
||||
|
||||
school.school_categories.map(&:sync_aggregated_responses)
|
||||
|
||||
base_categories = Category.joins(:questions).to_a.flatten.uniq
|
||||
base_categories.each do |category|
|
||||
SchoolCategory.for(school, category).each do |school_category|
|
||||
year = school_category.year
|
||||
dup_school_categories = SchoolCategory.for(school, category).in(year)
|
||||
next unless dup_school_categories.count > 1
|
||||
|
||||
dup_school_categories.each { |dsc| dsc.destroy unless dsc.id == school_category.id }
|
||||
school_category.sync_aggregated_responses
|
||||
parent = category.parent_category
|
||||
until parent.nil?
|
||||
SchoolCategory.for(school, parent).in(year).valid.each do |parent_school_category|
|
||||
parent_dup_school_categories = SchoolCategory.for(school, parent).in(year)
|
||||
if parent_dup_school_categories.count > 1
|
||||
parent_dup_school_categories.each { |pdsc| pdsc.destroy unless pdsc.id == parent_school_category.id }
|
||||
parent_school_category.sync_aggregated_responses
|
||||
end
|
||||
end
|
||||
parent = parent.parent_category
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,106 +0,0 @@
|
|||
module Legacy
|
||||
class SchoolCategory < ApplicationRecord
|
||||
|
||||
MIN_RESPONSE_COUNT = 10
|
||||
|
||||
belongs_to :school
|
||||
belongs_to :category
|
||||
|
||||
has_many :school_questions
|
||||
|
||||
validates_associated :school
|
||||
validates_associated :category
|
||||
|
||||
scope :for, -> (school, category) { where(school: school).where(category: category) }
|
||||
scope :for_parent_category, -> (school, category = nil) { where(school: school).joins(:category).merge(Category.for_parent(category)) }
|
||||
scope :in, -> (year) { where(year: year) }
|
||||
|
||||
scope :valid, -> { where("zscore is not null or valid_child_count is not null") }
|
||||
|
||||
def admin?
|
||||
child_categories = category.child_categories
|
||||
return false if child_categories.blank?
|
||||
child_categories.each { |cc| return false if cc.benchmark.blank? }
|
||||
return true
|
||||
end
|
||||
|
||||
def root_index
|
||||
category.root_index
|
||||
end
|
||||
|
||||
def answer_index_average
|
||||
answer_index_total.to_f / response_count.to_f
|
||||
end
|
||||
|
||||
def aggregated_responses
|
||||
attempt_data = Legacy::Attempt.
|
||||
created_in(year).
|
||||
for_category(category).
|
||||
for_school(school).
|
||||
select('count(legacy_attempts.id) as attempt_count').
|
||||
select('count(legacy_attempts.answer_index) as response_count').
|
||||
select('sum(case when legacy_questions.reverse then 6 - legacy_attempts.answer_index else legacy_attempts.answer_index end) as answer_index_total')[0]
|
||||
|
||||
return {
|
||||
attempt_count: attempt_data.attempt_count || 0,
|
||||
response_count: attempt_data.response_count || 0,
|
||||
answer_index_total: attempt_data.answer_index_total || 0,
|
||||
zscore: attempt_data.answer_index_total.nil? ?
|
||||
(attempt_data.response_count > MIN_RESPONSE_COUNT ? zscore : nil) :
|
||||
(attempt_data.response_count > MIN_RESPONSE_COUNT ?
|
||||
(attempt_data.answer_index_total.to_f / attempt_data.response_count.to_f - 3.to_f) :
|
||||
nil)
|
||||
}
|
||||
end
|
||||
|
||||
def chained_aggregated_responses
|
||||
return {} if nonlikert.present?
|
||||
|
||||
_aggregated_responses = aggregated_responses
|
||||
|
||||
child_school_categories = []
|
||||
if category.child_categories.length > 0
|
||||
child_school_categories = category.child_categories.collect do |cc|
|
||||
SchoolCategory.for(school, cc).in(year).valid
|
||||
end.flatten.compact
|
||||
|
||||
return {} if child_school_categories.blank?
|
||||
end
|
||||
|
||||
average_zscore = nil
|
||||
zscore_categories = child_school_categories.select { |csc| csc.zscore.present? && !csc.zscore.nan? }
|
||||
if zscore_categories.length > 0
|
||||
total_zscore = zscore_categories.inject(0) { |total, zc| total + zc.zscore }
|
||||
average_zscore = total_zscore / zscore_categories.length
|
||||
end
|
||||
|
||||
return {
|
||||
attempt_count:
|
||||
_aggregated_responses[:attempt_count] +
|
||||
child_school_categories.inject(0) { |total, csc| total + (csc.attempt_count || 0) },
|
||||
response_count:
|
||||
_aggregated_responses[:response_count] +
|
||||
child_school_categories.inject(0) { |total, csc| total + (csc.response_count || 0) },
|
||||
answer_index_total:
|
||||
_aggregated_responses[:answer_index_total] +
|
||||
child_school_categories.inject(0) { |total, csc| total + (csc.answer_index_total || 0) },
|
||||
zscore: average_zscore.present? ? average_zscore : _aggregated_responses[:zscore]
|
||||
}
|
||||
end
|
||||
|
||||
def sync_aggregated_responses
|
||||
# This doesn't seem to be taking into account valid_child_count or Boston's "Community and Wellbeing" category which should be suppressed if the "Health" category is the only child category visible.
|
||||
|
||||
return if ENV['BULK_PROCESS']
|
||||
update(chained_aggregated_responses)
|
||||
return if response_count == 0 && zscore.nil?
|
||||
if category.parent_category.present?
|
||||
parent_school_category = SchoolCategory.for(school, category.parent_category).in(year).first
|
||||
if parent_school_category.nil?
|
||||
parent_school_category = SchoolCategory.create(school: school, category: category.parent_category, year: year)
|
||||
end
|
||||
parent_school_category.sync_aggregated_responses
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
module Legacy
|
||||
class SchoolQuestion < ApplicationRecord
|
||||
belongs_to :school
|
||||
belongs_to :question
|
||||
belongs_to :school_category
|
||||
|
||||
validates_associated :school
|
||||
validates_associated :question
|
||||
validates_associated :school_category
|
||||
|
||||
scope :for, ->(school, question) { where(school_id: school.id, question_id: question.id) }
|
||||
scope :in, ->(year) { where(year:) }
|
||||
|
||||
def sync_attempts
|
||||
attempt_data = Attempt
|
||||
.joins(:question)
|
||||
.created_in(school_category.year)
|
||||
.for_question(question)
|
||||
.for_school(school)
|
||||
.select('count(attempts.answer_index) as response_count')
|
||||
.select('sum(case when questions.reverse then 6 - attempts.answer_index else attempts.answer_index end) as answer_index_total')[0]
|
||||
|
||||
available_responders = school.available_responders_for(question)
|
||||
|
||||
update(
|
||||
attempt_count: available_responders,
|
||||
response_count: attempt_data.response_count,
|
||||
response_rate: attempt_data.response_count.to_f / available_responders.to_f,
|
||||
response_total: attempt_data.answer_index_total
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
module Legacy
|
||||
class Student < ApplicationRecord
|
||||
belongs_to :recipient
|
||||
end
|
||||
end
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
module Legacy
|
||||
class User < ApplicationRecord
|
||||
# Include default devise modules. Others available are:
|
||||
# :confirmable, :lockable, :timeoutable and :omniauthable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :trackable, :validatable
|
||||
|
||||
has_many :user_schools
|
||||
|
||||
def schools
|
||||
districts = user_schools.map(&:district).compact.uniq
|
||||
(user_schools.map(&:school) + districts.map(&:schools)).flatten.compact.uniq
|
||||
end
|
||||
|
||||
def admin?(school)
|
||||
schools.index(school).present?
|
||||
end
|
||||
|
||||
def super_admin?
|
||||
[1].index(id).present?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
module Legacy
|
||||
class UserSchool < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :school
|
||||
belongs_to :district
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue