Extract legacy parts of the codebase into its own module

pull/1/head
Liam Morley 4 years ago committed by rebuilt
parent cf6e80ce6b
commit 413096dfe2

@ -1,6 +0,0 @@
class AdminController < ApplicationController
def index
end
end

@ -1,94 +0,0 @@
class AttemptsController < ApplicationController
# before_action :set_attempt, only: [:edit, :update]
protect_from_forgery :except => [:twilio]
def twilio
recipient = Recipient.where(phone: twilio_params['From']).first
attempt = recipient.attempts.last_sent.first
all_twilio_details = (attempt.twilio_details || '').split('~!~')
all_twilio_details << twilio_params.to_h.to_yaml
attempt.save_response(
answer_index: twilio_params[:Body].to_i > 0 ? twilio_params[:Body].to_i : nil,
twilio_details: all_twilio_details.join('~!~')
)
unless (['start', 'resume', 'restart', 'yes', 'go'].index(twilio_params[:Body].downcase).nil?)
recipient.update(opted_out: false)
render plain: 'Thank you, you will now begin receiving messages again.'
return
end
unless (['stop', 'cancel', 'quit', 'no'].index(twilio_params[:Body].downcase).nil?)
recipient.update(opted_out: true)
render plain: 'Thank you, you have been opted out of these messages and will no longer receive them.'
return
end
unless (['skip', 'i dont know', "i don't know", 'next'].index(twilio_params[:Body].downcase).nil?)
render plain: 'Thank you, this question has been skipped.'
return
end
response_message = ["We've registered your response of \"#{attempt.response}\"."]
answer_count = Attempt.for_question(attempt.question).for_school(recipient.school).with_answer.count
if answer_count > 1
response_message << "#{answer_count} people have responded to this question so far. To see all responses visit:"
else
response_message << 'You are the first person to respond to this question. Once more people have responded you will be able to see all responses at:'
end
response_message << school_category_url(attempt.recipient.school, attempt.question.category)
render plain: response_message.join(' ')
end
# # GET /attempts/1/edit
# def edit
# end
#
# # PATCH/PUT /attempts/1
# # PATCH/PUT /attempts/1.json
# def update
# attempt_params = {}
# if twilio_params.present?
# attempt_params.merge!(
# answer_index: twilio_params[:Body].to_i,
# twilio_details: twilio_params.to_h.to_yaml
# )
# end
#
# respond_to do |format|
# if @attempt.update(attempt_params)
# format.html { render plain: 'Thank you!' }
# format.json { render :show, status: :ok, location: @attempt }
# else
# format.html { render :edit }
# format.json { render json: @attempt.errors, status: :unprocessable_entity }
# end
# end
# end
# # DELETE /attempts/1
# # DELETE /attempts/1.json
# def destroy
# @attempt.destroy
# respond_to do |format|
# format.html { redirect_to attempts_url, notice: 'attempt was successfully destroyed.' }
# format.json { head :no_content }
# end
# end
private
# Use callbacks to share common setup or constraints between actions.
def set_attempt
@attempt = Attempt.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def twilio_params
{"Body"=>"5", ""=>"US", "To"=>"+16172023890", "ToZip"=>"02135", "NumSegments"=>"1", "MessageSid"=>"SMe37977e625b7f0b429339e752dddefef", "AccountSid"=>"AC57dc8a5a6d75addb9528e730e92f66b2", "From"=>"+16502693205", "ApiVersion"=>"2010-04-01"}
params.permit(:FromCountry, :FromState, :FromZip, :FromCity, :ToCountry, :ToState, :SmsStatus, :SmsSid, :SmsMessageSid, :MessageSid, :AccountSid, :MessagingServiceSid, :From, :To, :Body, :NumMedia)
end
end

@ -1,104 +1,9 @@
class CategoriesController < ApplicationController class CategoriesController < SqmApplicationController
before_action :set_school, only: [:show]
before_action :set_category, only: [:show, :edit, :update, :destroy]
# GET /categories
# GET /categories.json
def index
@categories = Category.all
end
# GET /categories/1
# GET /categories/1.json
def show def show
district = @school.district @categories = Category.sorted.map { |category| CategoryPresenter.new(category: category) }
authenticate(district.name.downcase, "#{district.name.downcase}!")
school_categories = SchoolCategory.for(@school, @category)
@years = school_categories.map(&:year).map(&:to_i).sort
@year = (params[:year] || @years.last || "2019").to_i
if school_categories.empty?
school_categories = [SchoolCategory.new(school: @school, category: @category, year: @year)]
end
@school_category = school_categories.select { |sc| sc.year.to_s == @year.to_s }.first
@child_school_categories = SchoolCategory.for_parent_category(@school, @category).in(@year).valid.to_a
missing_categories = Category.for_parent(@category) - @child_school_categories.map(&:category)
missing_categories.each do |category|
next if category.benchmark.present?
@child_school_categories << category.school_categories.new(school: @school)
end
if district.name == "Boston"
@child_school_categories = @child_school_categories.reject { |csc| csc.admin? }
end
@questions = @category.questions.created_in(@year)
end
# GET /categories/new
def new
@category = Category.new
end
# GET /categories/1/edit
def edit
end
# POST /categories @category = CategoryPresenter.new(category: Category.find_by_slug(params[:id]))
# POST /categories.json
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /categories/1
# PATCH/PUT /categories/1.json
def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end end
# DELETE /categories/1
# DELETE /categories/1.json
def destroy
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_school
redirect_to root_path and return false unless params.include?(:school_id)
@school = School.friendly.find(params[:school_id])
redirect_to root_path and return false if @school.nil?
end
def set_category
@category = Category.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def category_params
params.require(:category).permit(:name, :blurb, :description, :external_id, :parent_category_id)
end
end end

@ -2,7 +2,7 @@ class DashboardController < SqmApplicationController
def index def index
@variance_chart_row_presenters = Measure.all.map(&method(:presenter_for_measure)) @variance_chart_row_presenters = Measure.all.map(&method(:presenter_for_measure))
@category_presenters = SqmCategory.sorted.map { |sqm_category| CategoryPresenter.new(category: sqm_category) } @category_presenters = Category.sorted.map { |category| CategoryPresenter.new(category: category) }
end end
private private

@ -1,76 +0,0 @@
class DistrictsController < ApplicationController
before_action :set_district, only: [:show, :edit, :update, :destroy]
# GET /districts
# GET /districts.json
def index
@districts = District.all.alphabetic
end
# GET /districts/1
# GET /districts/1.json
def show
authenticate(@district.name.downcase, "#{@district.name.downcase}!")
@schools = @district.schools.alphabetic
end
# GET /districts/new
def new
@district = District.new
end
# GET /districts/1/edit
def edit
end
# POST /districts
# POST /districts.json
def create
@district = District.new(district_params)
respond_to do |format|
if @district.save
format.html { redirect_to @district, notice: 'District was successfully created.' }
format.json { render :show, status: :created, location: @district }
else
format.html { render :new }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /districts/1
# PATCH/PUT /districts/1.json
def update
respond_to do |format|
if @district.update(district_params)
format.html { redirect_to @district, notice: 'District was successfully updated.' }
format.json { render :show, status: :ok, location: @district }
else
format.html { render :edit }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
# DELETE /districts/1
# DELETE /districts/1.json
def destroy
@district.destroy
respond_to do |format|
format.html { redirect_to districts_url, notice: 'District was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_district
@district = District.find_by_slug(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def district_params
params.require(:district).permit(:name, :state_id)
end
end

@ -3,6 +3,6 @@ class HomeController < ActionController::Base
@districts = District.all.order(:name) @districts = District.all.order(:name)
@schools = School.all @schools = School.all
@categories = SqmCategory.sorted.map { |category| CategoryPresenter.new(category: category) } @categories = Category.sorted.map { |category| CategoryPresenter.new(category: category) }
end end
end end

@ -0,0 +1,8 @@
module Legacy
class AdminController < ApplicationController
def index
end
end
end

@ -0,0 +1,97 @@
module Legacy
class AttemptsController < ApplicationController
# before_action :set_attempt, only: [:edit, :update]
protect_from_forgery :except => [:twilio]
def twilio
recipient = Recipient.where(phone: twilio_params['From']).first
attempt = recipient.attempts.last_sent.first
all_twilio_details = (attempt.twilio_details || '').split('~!~')
all_twilio_details << twilio_params.to_h.to_yaml
attempt.save_response(
answer_index: twilio_params[:Body].to_i > 0 ? twilio_params[:Body].to_i : nil,
twilio_details: all_twilio_details.join('~!~')
)
unless (['start', 'resume', 'restart', 'yes', 'go'].index(twilio_params[:Body].downcase).nil?)
recipient.update(opted_out: false)
render plain: 'Thank you, you will now begin receiving messages again.'
return
end
unless (['stop', 'cancel', 'quit', 'no'].index(twilio_params[:Body].downcase).nil?)
recipient.update(opted_out: true)
render plain: 'Thank you, you have been opted out of these messages and will no longer receive them.'
return
end
unless (['skip', 'i dont know', "i don't know", 'next'].index(twilio_params[:Body].downcase).nil?)
render plain: 'Thank you, this question has been skipped.'
return
end
response_message = ["We've registered your response of \"#{attempt.response}\"."]
answer_count = Attempt.for_question(attempt.question).for_school(recipient.school).with_answer.count
if answer_count > 1
response_message << "#{answer_count} people have responded to this question so far. To see all responses visit:"
else
response_message << 'You are the first person to respond to this question. Once more people have responded you will be able to see all responses at:'
end
response_message << legacy_school_legacy_category_url(attempt.recipient.school, attempt.question.category)
render plain: response_message.join(' ')
end
# # GET /attempts/1/edit
# def edit
# end
#
# # PATCH/PUT /attempts/1
# # PATCH/PUT /attempts/1.json
# def update
# attempt_params = {}
# if twilio_params.present?
# attempt_params.merge!(
# answer_index: twilio_params[:Body].to_i,
# twilio_details: twilio_params.to_h.to_yaml
# )
# end
#
# respond_to do |format|
# if @attempt.update(attempt_params)
# format.html { render plain: 'Thank you!' }
# format.json { render :show, status: :ok, location: @attempt }
# else
# format.html { render :edit }
# format.json { render json: @attempt.errors, status: :unprocessable_entity }
# end
# end
# end
# # DELETE /attempts/1
# # DELETE /attempts/1.json
# def destroy
# @attempt.destroy
# respond_to do |format|
# format.html { redirect_to attempts_url, notice: 'attempt was successfully destroyed.' }
# format.json { head :no_content }
# end
# end
private
# Use callbacks to share common setup or constraints between actions.
def set_attempt
@attempt = Attempt.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def twilio_params
{ "Body" => "5", "" => "US", "To" => "+16172023890", "ToZip" => "02135", "NumSegments" => "1", "MessageSid" => "SMe37977e625b7f0b429339e752dddefef", "AccountSid" => "AC57dc8a5a6d75addb9528e730e92f66b2", "From" => "+16502693205", "ApiVersion" => "2010-04-01" }
params.permit(:FromCountry, :FromState, :FromZip, :FromCity, :ToCountry, :ToState, :SmsStatus, :SmsSid, :SmsMessageSid, :MessageSid, :AccountSid, :MessagingServiceSid, :From, :To, :Body, :NumMedia)
end
end
end

@ -0,0 +1,107 @@
module Legacy
class CategoriesController < ApplicationController
before_action :set_school, only: [:show]
before_action :set_category, only: [:show, :edit, :update, :destroy]
# GET /categories
# GET /categories.json
def index
@categories = Category.all
end
# GET /categories/1
# GET /categories/1.json
def show
district = @school.district
authenticate(district.name.downcase, "#{district.name.downcase}!")
school_categories = SchoolCategory.for(@school, @category)
@years = school_categories.map(&:year).map(&:to_i).sort
@year = (params[:year] || @years.last || "2019").to_i
if school_categories.empty?
school_categories = [SchoolCategory.new(school: @school, category: @category, year: @year)]
end
@school_category = school_categories.select { |sc| sc.year.to_s == @year.to_s }.first
@child_school_categories = SchoolCategory.for_parent_category(@school, @category).in(@year).valid.to_a
missing_categories = Category.for_parent(@category) - @child_school_categories.map(&:category)
missing_categories.each do |category|
next if category.benchmark.present?
@child_school_categories << category.school_categories.new(school: @school)
end
if district.name == "Boston"
@child_school_categories = @child_school_categories.reject { |csc| csc.admin? }
end
@questions = @category.questions.created_in(@year)
end
# GET /categories/new
def new
@category = Category.new
end
# GET /categories/1/edit
def edit
end
# POST /categories
# POST /categories.json
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /categories/1
# PATCH/PUT /categories/1.json
def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
# DELETE /categories/1
# DELETE /categories/1.json
def destroy
@category.destroy
respond_to do |format|
format.html { redirect_to legacy_categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_school
redirect_to root_path and return false unless params.include?(:school_id)
@school = School.friendly.find(params[:school_id])
redirect_to root_path and return false if @school.nil?
end
def set_category
@category = Category.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def category_params
params.require(:category).permit(:name, :blurb, :description, :external_id, :parent_category_id)
end
end
end

@ -0,0 +1,79 @@
module Legacy
class DistrictsController < ApplicationController
before_action :set_district, only: [:show, :edit, :update, :destroy]
# GET /districts
# GET /districts.json
def index
@districts = District.all.alphabetic
end
# GET /districts/1
# GET /districts/1.json
def show
authenticate(@district.name.downcase, "#{@district.name.downcase}!")
@schools = @district.schools.alphabetic
end
# GET /districts/new
def new
@district = District.new
end
# GET /districts/1/edit
def edit
end
# POST /districts
# POST /districts.json
def create
@district = District.new(district_params)
respond_to do |format|
if @district.save
format.html { redirect_to @district, notice: 'District was successfully created.' }
format.json { render :show, status: :created, location: @district }
else
format.html { render :new }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /districts/1
# PATCH/PUT /districts/1.json
def update
respond_to do |format|
if @district.update(district_params)
format.html { redirect_to @district, notice: 'District was successfully updated.' }
format.json { render :show, status: :ok, location: @district }
else
format.html { render :edit }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
# DELETE /districts/1
# DELETE /districts/1.json
def destroy
@district.destroy
respond_to do |format|
format.html { redirect_to districts_url, notice: 'District was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_district
@district = District.find_by_slug(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def district_params
params.require(:district).permit(:name, :state_id)
end
end
end

@ -0,0 +1,77 @@
module Legacy
class QuestionListsController < ApplicationController
before_action :set_question_list, only: [:show, :edit, :update, :destroy]
# GET /question_lists
# GET /question_lists.json
def index
@question_lists = QuestionList.all
end
# GET /question_lists/1
# GET /question_lists/1.json
def show
end
# GET /question_lists/new
def new
@question_list = QuestionList.new
end
# GET /question_lists/1/edit
def edit
end
# POST /question_lists
# POST /question_lists.json
def create
@question_list = QuestionList.new(question_list_params)
respond_to do |format|
if @question_list.save
format.html { redirect_to @question_list, notice: 'Question list was successfully created.' }
format.json { render :show, status: :created, location: @question_list }
else
format.html { render :new }
format.json { render json: @question_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /question_lists/1
# PATCH/PUT /question_lists/1.json
def update
respond_to do |format|
if @question_list.update(question_list_params)
format.html { redirect_to @question_list, notice: 'Question list was successfully updated.' }
format.json { render :show, status: :ok, location: @question_list }
else
format.html { render :edit }
format.json { render json: @question_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /question_lists/1
# DELETE /question_lists/1.json
def destroy
@question_list.destroy
respond_to do |format|
format.html { redirect_to legacy_question_lists_url, notice: 'Question list was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_question_list
@question_list = QuestionList.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def question_list_params
params.require(:question_list).permit(:name, :description, question_id_array: [])
end
end
end

@ -0,0 +1,90 @@
module Legacy
class QuestionsController < ApplicationController
before_action :authenticate_user!, except: [:show]
before_action :verify_super_admin, except: [:show]
before_action :set_school, only: [:show]
before_action :set_question, only: [:show, :edit, :update, :destroy]
# GET /questions
# GET /questions.json
def index
@questions = Question.all
end
# GET /questions/1
# GET /questions/1.json
def show
end
# GET /questions/new
def new
@question = Question.new
end
# GET /questions/1/edit
def edit
end
# POST /questions
# POST /questions.json
def create
@question = Question.new(question_params)
respond_to do |format|
if @question.save
format.html { redirect_to @question, notice: 'Question was successfully created.' }
format.json { render :show, status: :created, location: @question }
else
format.html { render :new }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /questions/1
# PATCH/PUT /questions/1.json
def update
respond_to do |format|
if @question.update(question_params)
format.html { redirect_to @question, notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: @question }
else
format.html { render :edit }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
# DELETE /questions/1
# DELETE /questions/1.json
def destroy
@question.destroy
respond_to do |format|
format.html { redirect_to legacy_questions_url, notice: 'Question was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_school
redirect_to root_path and return false unless params.include?(:school_id)
@school = School.friendly.find(params[:school_id])
redirect_to root_path and return false if @school.nil?
end
# Use callbacks to share common setup or constraints between actions.
def set_question
@question = Question.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def question_params
params.require(:question).permit(:text, :option1, :option2, :option3, :option4, :option5, :category_id)
end
def verify_super_admin
user_signed_in? && current_user.super_admin?
end
end
end

@ -0,0 +1,69 @@
module Legacy
class RecipientListsController < ApplicationController
before_action :authenticate_user!
before_action :set_school
before_action :verify_admin
before_action :set_recipient_list, only: [:show, :edit, :update, :destroy]
# GET schools/1/recipient_lists
def index
@recipient_lists = @school.recipient_lists
end
# GET schools/1/recipient_lists/1
def show
end
# GET schools/1/recipient_lists/new
def new
@recipient_list = @school.recipient_lists.build
end
# GET schools/1/recipient_lists/1/edit
def edit
end
# POST schools/1/recipient_lists
def create
@recipient_list = @school.recipient_lists.build(recipient_list_params)
if @recipient_list.save
redirect_to([@recipient_list.school, @recipient_list], notice: 'Recipient list was successfully created.')
else
render action: 'new'
end
end
# PUT schools/1/recipient_lists/1
def update
if @recipient_list.update(recipient_list_params)
redirect_to([@recipient_list.school, @recipient_list], notice: 'Recipient list was successfully updated.')
else
render action: 'edit'
end
end
# DELETE schools/1/recipient_lists/1
def destroy
@recipient_list.destroy
redirect_to @school
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.friendly.find(params[:school_id])
end
def set_recipient_list
@recipient_list = @school.recipient_lists.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def recipient_list_params
params.require(:recipient_list).permit(:name, :description, recipient_id_array: [])
end
end
end

@ -0,0 +1,92 @@
module Legacy
class RecipientsController < ApplicationController
before_action :authenticate_user!
before_action :set_school
before_action :verify_admin
before_action :set_recipient, only: [:show, :edit, :update, :destroy]
# GET /recipients
# GET /recipients.json
def index
@recipients = @school.recipients.all
end
# GET /recipients/1
# GET /recipients/1.json
def show
end
# GET /recipients/new
def new
@recipient = @school.recipients.new
end
# GET /recipients/1/edit
def edit
end
# POST /recipients
# POST /recipients.json
def create
@recipient = @school.recipients.new(recipient_params)
respond_to do |format|
if @recipient.save
format.html { redirect_to legacy_school_legacy_recipient_path(@school, @recipient), notice: 'Recipient was successfully created.' }
format.json { render :show, status: :created, location: @recipient }
else
format.html { render :new }
format.json { render json: @recipient.errors, status: :unprocessable_entity }
end
end
end
def import
render and return if request.get?
Recipient.import(@school, params[:file])
redirect_to @school, notice: "Recipients imported."
end
# PATCH/PUT /recipients/1
# PATCH/PUT /recipients/1.json
def update
respond_to do |format|
if @recipient.update(recipient_params)
format.html { redirect_to legacy_school_legacy_recipient_path(@school, @recipient), notice: 'Recipient was successfully updated.' }
format.json { render :show, status: :ok, location: @recipient }
else
format.html { render :edit }
format.json { render json: @recipient.errors, status: :unprocessable_entity }
end
end
end
# DELETE /recipients/1
# DELETE /recipients/1.json
def destroy
@recipient.destroy
respond_to do |format|
format.html { redirect_to @school, notice: 'Recipient was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.friendly.find(params[:school_id])
end
# Use callbacks to share common setup or constraints between actions.
def set_recipient
@recipient = @school.recipients.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def recipient_params
params.require(:recipient).permit(:name, :phone, :birth_date, :gender, :race, :ethnicity, :home_language_id, :income, :opted_out, :school_id)
end
end
end

@ -0,0 +1,71 @@
module Legacy
class SchedulesController < ApplicationController
before_action :authenticate_user!, except: [:show]
before_action :set_school
before_action :verify_admin
before_action :set_schedule, only: [:show, :edit, :update, :destroy]
before_action :set_time, only: [:create, :update]
# GET schools/1/schedules/1
def show
end
# GET schools/1/schedules/new
def new
@schedule = @school.schedules.build
end
# GET schools/1/schedules/1/edit
def edit
end
# POST schools/1/schedules
def create
@schedule = @school.schedules.build(schedule_params)
if @schedule.save
redirect_to([@schedule.school, @schedule], notice: 'Schedule was successfully created.')
else
render action: 'new'
end
end
# PUT schools/1/schedules/1
def update
if @schedule.update(schedule_params)
redirect_to([@schedule.school, @schedule], notice: 'Schedule was successfully updated.')
else
render action: 'edit'
end
end
# DELETE schools/1/schedules/1
def destroy
@schedule.destroy
redirect_to @school
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.friendly.find(params[:school_id])
end
def set_schedule
@schedule = @school.schedules.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def schedule_params
params.require(:schedule).permit(:name, :description, :school_id, :frequency_hours, :start_date, :end_date, :time, :active, :random, :recipient_list_id, :question_list_id)
end
def set_time
return unless schedule_params.include?(:time)
params[:schedule][:time] = schedule_params[:time].to_i + (4 * 60) # Go from EST to UTC (NEEDS TO BETTER)
end
end
end

@ -0,0 +1,97 @@
module Legacy
class SchoolsController < ApplicationController
before_action :authenticate_user!, except: [:show]
before_action :set_school, only: [:admin, :show, :edit, :update, :destroy]
before_action :verify_admin, except: [:show, :create, :new]
# GET /schools/1
# GET /schools/1.json
def show
@district = @school.district
authenticate(@district.name.downcase, "#{@district.name.downcase}!")
@years = [2017, 2018, 2019]
@year = (params[:year] || @years.last).to_i
if @district.name == "Boston"
@categories = Category.joins(:questions)
@school_categories = SchoolCategory.where(school: @school).where(category: @categories).in(@year).to_a
else
@categories = Category.root
@school_categories = @school.school_categories.for_parent_category(@school, nil).valid.in(@year).sort
end
missing_categories = @categories - @school_categories.map(&:category)
missing_categories.each do |category|
@school_categories << category.school_categories.new(school: @school, year: @year)
end
@school_categories = @school_categories.select { |sc| sc.year.to_i == @year }
end
def admin
end
# GET /schools/new
def new
@school = School.new
end
# GET /schools/1/edit
def edit
end
# POST /schools
# POST /schools.json
def create
@school = School.new(school_params)
respond_to do |format|
if @school.save
format.html { redirect_to @school, notice: 'School was successfully created.' }
format.json { render :show, status: :created, location: @school }
else
format.html { render :new }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schools/1
# PATCH/PUT /schools/1.json
def update
respond_to do |format|
if @school.update(school_params)
format.html { redirect_to @school, notice: 'School was successfully updated.' }
format.json { render :show, status: :ok, location: @school }
else
format.html { render :edit }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schools/1
# DELETE /schools/1.json
def destroy
@school.destroy
respond_to do |format|
format.html { redirect_to legacy_schools_url, notice: 'School was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.friendly.find(params[:id] || params[:school_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def school_params
params.require(:school).permit(:name, :district_id)
end
end
end

@ -0,0 +1,16 @@
module Legacy
class UsersController < ApplicationController
def show; end
# private
# # Use callbacks to share common setup or constraints between actions.
# def set_district
# @district = District.find(params[:id])
# end
#
# # Never trust parameters from the scary internet, only allow the white list through.
# def district_params
# params.require(:district).permit(:name, :state_id)
# end
end
end

@ -0,0 +1,10 @@
module Legacy
class WelcomeController < ApplicationController
def index
@districts = District.all.alphabetic
@schools = School.all.alphabetic
end
end
end

@ -1,74 +0,0 @@
class QuestionListsController < ApplicationController
before_action :set_question_list, only: [:show, :edit, :update, :destroy]
# GET /question_lists
# GET /question_lists.json
def index
@question_lists = QuestionList.all
end
# GET /question_lists/1
# GET /question_lists/1.json
def show
end
# GET /question_lists/new
def new
@question_list = QuestionList.new
end
# GET /question_lists/1/edit
def edit
end
# POST /question_lists
# POST /question_lists.json
def create
@question_list = QuestionList.new(question_list_params)
respond_to do |format|
if @question_list.save
format.html { redirect_to @question_list, notice: 'Question list was successfully created.' }
format.json { render :show, status: :created, location: @question_list }
else
format.html { render :new }
format.json { render json: @question_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /question_lists/1
# PATCH/PUT /question_lists/1.json
def update
respond_to do |format|
if @question_list.update(question_list_params)
format.html { redirect_to @question_list, notice: 'Question list was successfully updated.' }
format.json { render :show, status: :ok, location: @question_list }
else
format.html { render :edit }
format.json { render json: @question_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /question_lists/1
# DELETE /question_lists/1.json
def destroy
@question_list.destroy
respond_to do |format|
format.html { redirect_to question_lists_url, notice: 'Question list was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_question_list
@question_list = QuestionList.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def question_list_params
params.require(:question_list).permit(:name, :description, question_id_array: [])
end
end

@ -1,87 +0,0 @@
class QuestionsController < ApplicationController
before_action :authenticate_user!, except: [:show]
before_action :verify_super_admin, except: [:show]
before_action :set_school, only: [:show]
before_action :set_question, only: [:show, :edit, :update, :destroy]
# GET /questions
# GET /questions.json
def index
@questions = Question.all
end
# GET /questions/1
# GET /questions/1.json
def show
end
# GET /questions/new
def new
@question = Question.new
end
# GET /questions/1/edit
def edit
end
# POST /questions
# POST /questions.json
def create
@question = Question.new(question_params)
respond_to do |format|
if @question.save
format.html { redirect_to @question, notice: 'Question was successfully created.' }
format.json { render :show, status: :created, location: @question }
else
format.html { render :new }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /questions/1
# PATCH/PUT /questions/1.json
def update
respond_to do |format|
if @question.update(question_params)
format.html { redirect_to @question, notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: @question }
else
format.html { render :edit }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
# DELETE /questions/1
# DELETE /questions/1.json
def destroy
@question.destroy
respond_to do |format|
format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_school
redirect_to root_path and return false unless params.include?(:school_id)
@school = School.friendly.find(params[:school_id])
redirect_to root_path and return false if @school.nil?
end
# Use callbacks to share common setup or constraints between actions.
def set_question
@question = Question.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def question_params
params.require(:question).permit(:text, :option1, :option2, :option3, :option4, :option5, :category_id)
end
def verify_super_admin
user_signed_in? && current_user.super_admin?
end
end

@ -1,66 +0,0 @@
class RecipientListsController < ApplicationController
before_action :authenticate_user!
before_action :set_school
before_action :verify_admin
before_action :set_recipient_list, only: [:show, :edit, :update, :destroy]
# GET schools/1/recipient_lists
def index
@recipient_lists = @school.recipient_lists
end
# GET schools/1/recipient_lists/1
def show
end
# GET schools/1/recipient_lists/new
def new
@recipient_list = @school.recipient_lists.build
end
# GET schools/1/recipient_lists/1/edit
def edit
end
# POST schools/1/recipient_lists
def create
@recipient_list = @school.recipient_lists.build(recipient_list_params)
if @recipient_list.save
redirect_to([@recipient_list.school, @recipient_list], notice: 'Recipient list was successfully created.')
else
render action: 'new'
end
end
# PUT schools/1/recipient_lists/1
def update
if @recipient_list.update(recipient_list_params)
redirect_to([@recipient_list.school, @recipient_list], notice: 'Recipient list was successfully updated.')
else
render action: 'edit'
end
end
# DELETE schools/1/recipient_lists/1
def destroy
@recipient_list.destroy
redirect_to @school
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.friendly.find(params[:school_id])
end
def set_recipient_list
@recipient_list = @school.recipient_lists.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def recipient_list_params
params.require(:recipient_list).permit(:name, :description, recipient_id_array: [])
end
end

@ -1,89 +0,0 @@
class RecipientsController < ApplicationController
before_action :authenticate_user!
before_action :set_school
before_action :verify_admin
before_action :set_recipient, only: [:show, :edit, :update, :destroy]
# GET /recipients
# GET /recipients.json
def index
@recipients = @school.recipients.all
end
# GET /recipients/1
# GET /recipients/1.json
def show
end
# GET /recipients/new
def new
@recipient = @school.recipients.new
end
# GET /recipients/1/edit
def edit
end
# POST /recipients
# POST /recipients.json
def create
@recipient = @school.recipients.new(recipient_params)
respond_to do |format|
if @recipient.save
format.html { redirect_to school_recipient_path(@school, @recipient), notice: 'Recipient was successfully created.' }
format.json { render :show, status: :created, location: @recipient }
else
format.html { render :new }
format.json { render json: @recipient.errors, status: :unprocessable_entity }
end
end
end
def import
render and return if request.get?
Recipient.import(@school, params[:file])
redirect_to @school, notice: "Recipients imported."
end
# PATCH/PUT /recipients/1
# PATCH/PUT /recipients/1.json
def update
respond_to do |format|
if @recipient.update(recipient_params)
format.html { redirect_to school_recipient_path(@school, @recipient), notice: 'Recipient was successfully updated.' }
format.json { render :show, status: :ok, location: @recipient }
else
format.html { render :edit }
format.json { render json: @recipient.errors, status: :unprocessable_entity }
end
end
end
# DELETE /recipients/1
# DELETE /recipients/1.json
def destroy
@recipient.destroy
respond_to do |format|
format.html { redirect_to @school, notice: 'Recipient was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.friendly.find(params[:school_id])
end
# Use callbacks to share common setup or constraints between actions.
def set_recipient
@recipient = @school.recipients.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def recipient_params
params.require(:recipient).permit(:name, :phone, :birth_date, :gender, :race, :ethnicity, :home_language_id, :income, :opted_out, :school_id)
end
end

@ -1,68 +0,0 @@
class SchedulesController < ApplicationController
before_action :authenticate_user!, except: [:show]
before_action :set_school
before_action :verify_admin
before_action :set_schedule, only: [:show, :edit, :update, :destroy]
before_action :set_time, only: [:create, :update]
# GET schools/1/schedules/1
def show
end
# GET schools/1/schedules/new
def new
@schedule = @school.schedules.build
end
# GET schools/1/schedules/1/edit
def edit
end
# POST schools/1/schedules
def create
@schedule = @school.schedules.build(schedule_params)
if @schedule.save
redirect_to([@schedule.school, @schedule], notice: 'Schedule was successfully created.')
else
render action: 'new'
end
end
# PUT schools/1/schedules/1
def update
if @schedule.update(schedule_params)
redirect_to([@schedule.school, @schedule], notice: 'Schedule was successfully updated.')
else
render action: 'edit'
end
end
# DELETE schools/1/schedules/1
def destroy
@schedule.destroy
redirect_to @school
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.friendly.find(params[:school_id])
end
def set_schedule
@schedule = @school.schedules.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def schedule_params
params.require(:schedule).permit(:name, :description, :school_id, :frequency_hours, :start_date, :end_date, :time, :active, :random, :recipient_list_id, :question_list_id)
end
def set_time
return unless schedule_params.include?(:time)
params[:schedule][:time] = schedule_params[:time].to_i + (4 * 60) # Go from EST to UTC (NEEDS TO BETTER)
end
end

@ -1,95 +0,0 @@
class SchoolsController < ApplicationController
before_action :authenticate_user!, except: [:show]
before_action :set_school, only: [:admin, :show, :edit, :update, :destroy]
before_action :verify_admin, except: [:show, :create, :new]
# GET /schools/1
# GET /schools/1.json
def show
@district = @school.district
authenticate(@district.name.downcase, "#{@district.name.downcase}!")
@years = [2017, 2018, 2019]
@year = (params[:year] || @years.last).to_i
if @district.name == "Boston"
@categories = Category.joins(:questions)
@school_categories = SchoolCategory.where(school: @school).where(category: @categories).in(@year).to_a
else
@categories = Category.root
@school_categories = @school.school_categories.for_parent_category(@school, nil).valid.in(@year).sort
end
missing_categories = @categories - @school_categories.map(&:category)
missing_categories.each do |category|
@school_categories << category.school_categories.new(school: @school, year: @year)
end
@school_categories = @school_categories.select { |sc| sc.year.to_i == @year }
end
def admin
end
# GET /schools/new
def new
@school = School.new
end
# GET /schools/1/edit
def edit
end
# POST /schools
# POST /schools.json
def create
@school = School.new(school_params)
respond_to do |format|
if @school.save
format.html { redirect_to @school, notice: 'School was successfully created.' }
format.json { render :show, status: :created, location: @school }
else
format.html { render :new }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schools/1
# PATCH/PUT /schools/1.json
def update
respond_to do |format|
if @school.update(school_params)
format.html { redirect_to @school, notice: 'School was successfully updated.' }
format.json { render :show, status: :ok, location: @school }
else
format.html { render :edit }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schools/1
# DELETE /schools/1.json
def destroy
@school.destroy
respond_to do |format|
format.html { redirect_to schools_url, notice: 'School was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.friendly.find(params[:id] || params[:school_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def school_params
params.require(:school).permit(:name, :district_id)
end
end

@ -1,9 +0,0 @@
class SqmCategoriesController < SqmApplicationController
def show
@categories = SqmCategory.sorted.map { |category| CategoryPresenter.new(category: category) }
@category = CategoryPresenter.new(category: SqmCategory.find_by_slug(params[:id]))
end
end

@ -1,17 +0,0 @@
class UsersController < ApplicationController
def show
end
# private
# # Use callbacks to share common setup or constraints between actions.
# def set_district
# @district = District.find(params[:id])
# end
#
# # Never trust parameters from the scary internet, only allow the white list through.
# def district_params
# params.require(:district).permit(:name, :state_id)
# end
end

@ -1,8 +0,0 @@
class WelcomeController < ApplicationController
def index
@districts = District.all.alphabetic
@schools = School.all.alphabetic
end
end

@ -25,7 +25,7 @@ class Seeder
def seed_sqm_framework csv_file def seed_sqm_framework csv_file
CSV.parse(File.read(csv_file), headers: true) do |row| CSV.parse(File.read(csv_file), headers: true) do |row|
category_id = row['Category ID'].strip category_id = row['Category ID'].strip
category = SqmCategory.find_or_create_by!(category_id: category_id) category = Category.find_or_create_by!(category_id: category_id)
category_slugs = { category_slugs = {
'1' => 'teachers-and-leadership', '1' => 'teachers-and-leadership',
'2' => 'school-culture', '2' => 'school-culture',
@ -36,7 +36,7 @@ class Seeder
category.update! name: row['Category'].strip, description: row['Category Description'].strip, slug: category_slugs[category_id], sort_index: category_slugs.keys.index(category_id) category.update! name: row['Category'].strip, description: row['Category Description'].strip, slug: category_slugs[category_id], sort_index: category_slugs.keys.index(category_id)
subcategory_id = row['Subcategory ID'].strip subcategory_id = row['Subcategory ID'].strip
subcategory = Subcategory.find_or_create_by! subcategory_id: subcategory_id, sqm_category: category subcategory = Subcategory.find_or_create_by! subcategory_id: subcategory_id, category: category
subcategory.update! name: row['Subcategory'].strip, description: row['Subcategory Description'].strip subcategory.update! name: row['Subcategory'].strip, description: row['Subcategory Description'].strip
measure_id = row['Measure ID'].strip measure_id = row['Measure ID'].strip

@ -1,3 +1,4 @@
# TODO move this to legacy, probably?
class ApplicationRecord < ActiveRecord::Base class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true self.abstract_class = true
end end

@ -1,91 +0,0 @@
require 'twilio-ruby'
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(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 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: answer_index,
twilio_details: twilio_details,
responded_at: responded_at
)
if recipient_schedule.queued_question_ids.present?
recipient_schedule.update(next_attempt_at: Time.new)
end
end
def answer_index_with_reverse
return 6 - answer_index if question.reverse?
return 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

@ -1,93 +1,9 @@
class Category < ApplicationRecord class Category < ActiveRecord::Base
has_many :questions
belongs_to :parent_category, class_name: 'Category', foreign_key: :parent_category_id
has_many :child_categories, class_name: '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 include FriendlyId
friendly_id :name, :use => [:slugged] 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
[
"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| self.root_identifiers.index(c.slug) }
end
def custom_zones
return [] if zones.nil?
zones.split(",").map(&:to_f)
end
def zone_widths scope :sorted, ->() { order(:sort_index) }
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)
return 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|
if cc.zones.nil?
cc.sync_child_zones
end
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
has_many :subcategories
has_many :measures, through: :subcategories
end end

@ -12,5 +12,4 @@ class District < ApplicationRecord
before_save do before_save do
self.slug ||= name.parameterize self.slug ||= name.parameterize
end end
end end

@ -0,0 +1,5 @@
module Legacy
def self.table_name_prefix
'legacy_'
end
end

@ -0,0 +1,93 @@
require 'twilio-ruby'
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: answer_index,
twilio_details: twilio_details,
responded_at: responded_at
)
if recipient_schedule.queued_question_ids.present?
recipient_schedule.update(next_attempt_at: Time.new)
end
end
def answer_index_with_reverse
return 6 - answer_index if question.reverse?
return 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

@ -0,0 +1,95 @@
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
[
"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| self.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)
return 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|
if cc.zones.nil?
cc.sync_child_zones
end
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

@ -0,0 +1,17 @@
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

@ -0,0 +1,72 @@
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: category) }
scope :created_in, -> (year) { where("extract(year from #{self.table_name}.created_at) = ?", year) }
enum target_group: [: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) { |total, response| total + response.answer_index_with_reverse }
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

@ -0,0 +1,28 @@
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: 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

@ -0,0 +1,52 @@
require 'csv'
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: 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,3 +1,4 @@
module Legacy
class RecipientList < ApplicationRecord class RecipientList < ApplicationRecord
belongs_to :school belongs_to :school
has_many :schedules has_many :schedules
@ -45,3 +46,4 @@ class RecipientList < ApplicationRecord
end end
end end
end

@ -0,0 +1,169 @@
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, -> (recipient_or_recipient_id) {
id = recipient_or_recipient_id.is_a?(Recipient) ?
recipient_or_recipient_id.id :
recipient_or_recipient_id
where(recipient_id: id)
}
scope :for_schedule, -> (schedule_or_schedule_id) {
id = schedule_or_schedule_id.is_a?(Schedule) ?
schedule_or_schedule_id.id :
schedule_or_schedule_id
where(schedule_id: id)
}
def next_question
if queued_question_ids.present?
next_question_id = queued_question_ids.split(/,/).first
else
next_question_id = 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?
if !question.for_recipient_students?
return attempt_question(question: question)
end
missing_students = []
recipient_attempts = attempts.for_recipient(recipient).for_question(question)
recipient.students.each do |student|
if recipient_attempts.for_student(student).empty?
missing_students << student
end
end
attempt = recipient.attempts.create(
schedule: schedule,
recipient_schedule: self,
question: 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 = 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
return 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?
if question.for_recipient_students?
return attempt_question_for_recipient_students(question: question)
end
attempt = recipient.attempts.create(
schedule: schedule,
recipient_schedule: self,
question: 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 = 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
return (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?
return 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 = recipient_or_recipient_id.is_a?(Recipient) ?
recipient_or_recipient_id.id :
recipient_or_recipient_id
schedule.recipient_schedules.create(
recipient_id: recipient_id,
upcoming_question_ids: question_ids.join(','),
next_attempt_at: next_attempt_at
)
end
end
end

@ -0,0 +1,33 @@
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, -> {
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

@ -0,0 +1,89 @@
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

@ -0,0 +1,106 @@
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

@ -0,0 +1,35 @@
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: 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

@ -0,0 +1,5 @@
module Legacy
class Student < ApplicationRecord
belongs_to :recipient
end
end

@ -0,0 +1,23 @@
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

@ -0,0 +1,9 @@
module Legacy
class UserSchool < ApplicationRecord
belongs_to :user
belongs_to :school
belongs_to :district
end
end

@ -1,70 +0,0 @@
AggregatedResponses = Struct.new(
:question,
:category,
:responses,
:count,
:answer_index_total,
:answer_index_average,
:most_popular_answer,
:zscore
)
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: category) }
scope :created_in, -> (year) { where('extract(year from questions.created_at) = ?', year) }
enum target_group: [: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) { |total, response| total + response.answer_index_with_reverse }
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

@ -1,26 +0,0 @@
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: 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

@ -1,50 +0,0 @@
require 'csv'
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: 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

@ -1,168 +0,0 @@
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, -> (recipient_or_recipient_id) {
id = recipient_or_recipient_id.is_a?(Recipient) ?
recipient_or_recipient_id.id :
recipient_or_recipient_id
where(recipient_id: id)
}
scope :for_schedule, -> (schedule_or_schedule_id) {
id = schedule_or_schedule_id.is_a?(Schedule) ?
schedule_or_schedule_id.id :
schedule_or_schedule_id
where(schedule_id: id)
}
def next_question
if queued_question_ids.present?
next_question_id = queued_question_ids.split(/,/).first
else
next_question_id = 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?
if !question.for_recipient_students?
return attempt_question(question: question)
end
missing_students = []
recipient_attempts = attempts.for_recipient(recipient).for_question(question)
recipient.students.each do |student|
if recipient_attempts.for_student(student).empty?
missing_students << student
end
end
attempt = recipient.attempts.create(
schedule: schedule,
recipient_schedule: self,
question: 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 = 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
return 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?
if question.for_recipient_students?
return attempt_question_for_recipient_students(question: question)
end
attempt = recipient.attempts.create(
schedule: schedule,
recipient_schedule: self,
question: 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 = 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
return (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?
return 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 = recipient_or_recipient_id.is_a?(Recipient) ?
recipient_or_recipient_id.id :
recipient_or_recipient_id
schedule.recipient_schedules.create(
recipient_id: recipient_id,
upcoming_question_ids: question_ids.join(','),
next_attempt_at: next_attempt_at
)
end
end

@ -1,31 +0,0 @@
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, -> {
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

@ -1,17 +1,12 @@
class School < ApplicationRecord class School < ApplicationRecord
has_many :schedules, dependent: :destroy
has_many :recipient_lists, dependent: :destroy
belongs_to :district belongs_to :district
has_many :recipients, dependent: :destroy
has_many :school_categories, dependent: :destroy
has_many :user_schools, dependent: :destroy
validates :name, presence: true validates :name, presence: true
scope :alphabetic, -> { order(name: :asc) } scope :alphabetic, -> { order(name: :asc) }
include FriendlyId include FriendlyId
friendly_id :name, :use => [:slugged] friendly_id :name, use: [:slugged]
def self.find_by_district_code_and_school_code(district_code, school_code) def self.find_by_district_code_and_school_code(district_code, school_code)
School School
@ -19,69 +14,4 @@ class School < ApplicationRecord
.where(districts: { qualtrics_code: district_code }) .where(districts: { qualtrics_code: district_code })
.find_by_qualtrics_code(school_code) .find_by_qualtrics_code(school_code)
end end
def available_responders_for(question)
if question.for_students?
return student_count || 1
elsif question.for_teachers?
return teacher_count || 1
else
return 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)
if 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
while 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
end
destroy
end
end end

@ -1,104 +0,0 @@
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 = Attempt.
created_in(year).
for_category(category).
for_school(school).
select('count(attempts.id) as attempt_count').
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]
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

@ -1,33 +0,0 @@
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: 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

@ -1,9 +0,0 @@
class SqmCategory < ActiveRecord::Base
include FriendlyId
friendly_id :name, use: [:slugged]
scope :sorted, ->() { order(:sort_index) }
has_many :subcategories
has_many :measures, through: :subcategories
end

@ -1,3 +0,0 @@
class Student < ApplicationRecord
belongs_to :recipient
end

@ -1,5 +1,5 @@
class Subcategory < ActiveRecord::Base class Subcategory < ActiveRecord::Base
belongs_to :sqm_category belongs_to :category
has_many :measures has_many :measures
end end

@ -1,21 +0,0 @@
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

@ -1,7 +0,0 @@
class UserSchool < ApplicationRecord
belongs_to :user
belongs_to :school
belongs_to :district
end

@ -1,3 +0,0 @@
%p= link_to 'Categories', categories_path
%p= link_to 'Questions', questions_path

@ -1,2 +0,0 @@
%p
= school_category.category.path.map { |c| link_to(c.name, school_category_path(school_category.school, c, year: school_category.year)) }.join(' > ').html_safe

@ -1,7 +1,7 @@
.row .row
.col .col
=# link_to(image_tag(@district.logo), @district) if @district.present? =# link_to(image_tag(@district.logo), @district) if @district.present?
%h2= link_to @school.name, school_path(@school, year: @year) %h2= link_to @school.name, legacy_school_path(@school, year: @year)
= simple_format(@school.description) = simple_format(@school.description)
- if @year.present? - if @year.present?
%p %p
@ -12,9 +12,9 @@
- @years.sort.each do |year| - @years.sort.each do |year|
&nbsp; &nbsp; &nbsp; &nbsp;
- if @school_category.present? - if @school_category.present?
= link_to_unless(year == @year, "#{year - 1}-#{year}", school_category_path(@school_category.school, @school_category.category, year: year)) = link_to_unless(year == @year, "#{year - 1}-#{year}", legacy_school_legacy_category_path(@school_category.school, @school_category.category, year: year))
- else - else
= link_to_unless(year == @year, "#{year - 1}-#{year}", school_path(@school, year: year)) = link_to_unless(year == @year, "#{year - 1}-#{year}", legacy_school_path(@school, year: year))
- if @school.district.present? - if @school.district.present?
%p %p
%strong District: %strong District:

@ -0,0 +1,3 @@
%p= link_to 'Categories', legacy_categories_path
%p= link_to 'Questions', legacy_questions_path

@ -0,0 +1,2 @@
%p
= school_category.category.path.map { |c| link_to(c.name, legacy_school_legacy_category_path(school_category.school, c, year: school_category.year)) }.join(' > ').html_safe

@ -1,4 +1,4 @@
= form_for(category) do |f| = form_for(category, as: 'category') do |f|
- if category.errors.any? - if category.errors.any?
#error_explanation #error_explanation
%h2 %h2

@ -2,4 +2,4 @@
= render 'form', category: @category = render 'form', category: @category
= link_to 'Show', @category = link_to 'Show', @category
| |
= link_to 'Back', categories_path = link_to 'Back', legacy_categories_path

@ -18,7 +18,7 @@
%td= category.external_id %td= category.external_id
%td= category.parent_category_id %td= category.parent_category_id
%td= link_to 'Show', category %td= link_to 'Show', category
%td= link_to 'Edit', edit_category_path(category) %td= link_to 'Edit', edit_legacy_category_path(category)
%td= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %td= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' }
%br/ %br/
= link_to 'New Category', new_category_path = link_to 'New Category', new_legacy_category_path

@ -1,3 +1,3 @@
%h1 New Category %h1 New Category
= render 'form', category: @category = render 'form', category: @category
= link_to 'Back', categories_path = link_to 'Back', legacy_categories_path

@ -6,7 +6,7 @@
.row .row
.col-12 .col-12
%p %p
= render 'categories/breadcrumbs', school_category: @school_category = render 'legacy/categories/breadcrumbs', school_category: @school_category
.row .row
.col-12 .col-12
@ -29,7 +29,7 @@
.indicator-container.py-3 .indicator-container.py-3
= render 'school_categories/indicator', info: @school_category = render 'legacy/school_categories/indicator', info: @school_category
- else - else
.row.py-4 .row.py-4
.col-12.text-center .col-12.text-center
@ -46,4 +46,4 @@
.row .row
= render @questions = render @questions
= render "shared/performance_spectrum" = render "legacy/shared/performance_spectrum"

@ -1,4 +1,4 @@
= form_for(district) do |f| = form_for(district, as: 'district') do |f|
- if district.errors.any? - if district.errors.any?
#error_explanation #error_explanation
%h2 %h2

@ -1,4 +1,4 @@
= form_for(question_list) do |f| = form_for(question_list, as: 'question_list') do |f|
- if question_list.errors.any? - if question_list.errors.any?
#error_explanation #error_explanation
%h2 %h2
@ -17,6 +17,6 @@
= f.label :question_id_array, 'Questions' = f.label :question_id_array, 'Questions'
%br/ %br/
.form-check.form-check-collection .form-check.form-check-collection
= f.collection_check_boxes(:question_id_array, Question.all, :id, :text) { |c| c.label(class: 'form-check-label') { c.check_box(class: 'form-check-input') + " #{c.text}" } } = f.collection_check_boxes(:question_id_array, Legacy::Question.all, :id, :text) { |c| c.label(class: 'form-check-label') { c.check_box(class: 'form-check-input') + " #{c.text}" } }
.form-group .form-group
= f.submit 'Save List', class: 'btn btn-primary' = f.submit 'Save List', class: 'btn btn-primary'

@ -2,4 +2,4 @@
= render 'form', question_list: @question_list = render 'form', question_list: @question_list
= link_to 'Show', @question_list = link_to 'Show', @question_list
| |
= link_to 'Back', question_lists_path = link_to 'Back', legacy_question_lists_path

@ -14,7 +14,7 @@
%td= question_list.description %td= question_list.description
%td= question_list.question_ids %td= question_list.question_ids
%td= link_to 'Show', question_list %td= link_to 'Show', question_list
%td= link_to 'Edit', edit_question_list_path(question_list) %td= link_to 'Edit', edit_legacy_question_list_path(question_list)
%td= link_to 'Destroy', question_list, method: :delete, data: { confirm: 'Are you sure?' } %td= link_to 'Destroy', question_list, method: :delete, data: { confirm: 'Are you sure?' }
%br/ %br/
= link_to 'New Question List', new_question_list_path = link_to 'New Question List', new_legacy_question_list_path

@ -7,9 +7,9 @@
= @question_list.description = @question_list.description
%p %p
= link_to 'Edit', edit_question_list_path(@question_list) = link_to 'Edit', edit_legacy_question_list_path(@question_list)
| |
= link_to 'Back', question_lists_path = link_to 'Back', legacy_question_lists_path
%br %br
%br %br

@ -1,4 +1,4 @@
= form_for(question) do |f| = form_for(question, as: 'question') do |f|
- if question.errors.any? - if question.errors.any?
#error_explanation #error_explanation
%h2 %h2

@ -1,6 +1,6 @@
- aggregated_responses = question.aggregated_responses_for_school(@school) - aggregated_responses = question.aggregated_responses_for_school(@school)
- return if aggregated_responses.nil? - return if aggregated_responses.nil?
- school_question = SchoolQuestion.for(@school, question).first - school_question = Legacy::SchoolQuestion.for(@school, question).first
- valid_responses = school_question.nil? ? true : school_question.response_rate > 0.3 # || (aggregated_responses.count.to_f / @school.available_responders_for(question).to_f)) >= 0.3 - valid_responses = school_question.nil? ? true : school_question.response_rate > 0.3 # || (aggregated_responses.count.to_f / @school.available_responders_for(question).to_f)) >= 0.3
.col-12.col-md-6.py-3 .col-12.col-md-6.py-3
@ -11,7 +11,7 @@
- if valid_responses - if valid_responses
.row.pt-2 .row.pt-2
.col-4.indicator-container.centered .col-4.indicator-container.centered
= render 'school_categories/indicator', info: aggregated_responses, small: true = render 'legacy/school_categories/indicator', info: aggregated_responses, small: true
.col-8 .col-8
%p %p
@ -48,7 +48,7 @@
Raw Data Raw Data
.collapse{id: "histogram#{question.id}"} .collapse{id: "histogram#{question.id}"}
= render 'shared/histogram', data: aggregated_responses = render 'legacy/shared/histogram', data: aggregated_responses
%p.collapse{id: "raw-data#{question.id}"} %p.collapse{id: "raw-data#{question.id}"}
= aggregated_responses.responses.map(&:answer_index_with_reverse).join(', ') = aggregated_responses.responses.map(&:answer_index_with_reverse).join(', ')

@ -2,4 +2,4 @@
= render 'form', question: @question = render 'form', question: @question
= link_to 'Show', @question = link_to 'Show', @question
| |
= link_to 'Back', questions_path = link_to 'Back', legacy_questions_path

@ -22,7 +22,7 @@
%td= question.option5 %td= question.option5
%td= link_to(question.category.name, question.category) %td= link_to(question.category.name, question.category)
%td= link_to 'Show', question %td= link_to 'Show', question
%td= link_to 'Edit', edit_question_path(question) %td= link_to 'Edit', edit_legacy_question_path(question)
%td= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %td= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' }
%br/ %br/
= link_to 'New Question', new_question_path = link_to 'New Question', new_legacy_question_path

@ -1,3 +1,3 @@
%h1 New Question %h1 New Question
= render 'form', question: @question = render 'form', question: @question
= link_to 'Back', questions_path = link_to 'Back', legacy_questions_path

@ -21,13 +21,13 @@
%strong Category: %strong Category:
&nbsp; &nbsp;
- if @question.category.present? - if @question.category.present?
= render partial: 'categories/full_path', locals: {category: @question.category} = render partial: 'legacy/categories/full_path', locals: {category: @question.category}
- else - else
No Category No Category
= link_to 'Edit', edit_question_path(@question) = link_to 'Edit', edit_legacy_question_path(@question)
| |
= link_to 'Back', questions_path = link_to 'Back', legacy_questions_path
%br %br

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save