From f4a2c103d66c6699ad3fef843f3f5b4527795427 Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Tue, 18 Apr 2017 16:32:02 -0400 Subject: [PATCH] working on sibling students --- app/models/attempt.rb | 4 +- app/models/recipient.rb | 2 + app/models/recipient_schedule.rb | 23 ++++++++- app/models/student.rb | 3 ++ data/parent_pilot_questions.json | 22 ++++---- db/migrate/20170418173141_create_students.rb | 18 +++++++ db/schema.rb | 22 ++++++-- spec/lib/tasks/survey_rake_spec.rb | 54 ++++++++++++++++++++ spec/models/student_spec.rb | 5 ++ 9 files changed, 133 insertions(+), 20 deletions(-) create mode 100644 app/models/student.rb create mode 100644 db/migrate/20170418173141_create_students.rb create mode 100644 spec/models/student_spec.rb diff --git a/app/models/attempt.rb b/app/models/attempt.rb index 8e15d13d..5ddf65a9 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -18,8 +18,8 @@ class Attempt < ApplicationRecord def messages [ - question.text, - "#{question.option1}: Reply 1\n\r#{question.option2}: Reply 2\n\r#{question.option3}: Reply 3\n\r#{question.option4}: Reply 4\n\r#{question.option5}: Reply 5\n\rReply 'stop' to stop these messages." + #question.text, + "#{question.text}\r\n#{question.option1}: Reply 1\n\r#{question.option2}: Reply 2\n\r#{question.option3}: Reply 3\n\r#{question.option4}: Reply 4\n\r#{question.option5}: Reply 5\n\rReply 'stop' to stop these messages." ] end diff --git a/app/models/recipient.rb b/app/models/recipient.rb index aa3cfba6..dd39040a 100644 --- a/app/models/recipient.rb +++ b/app/models/recipient.rb @@ -7,6 +7,8 @@ class Recipient < ApplicationRecord has_many :recipient_schedules has_many :attempts + has_many :students + validates :name, presence: true scope :for_school, -> (school) { where(school: school) } diff --git a/app/models/recipient_schedule.rb b/app/models/recipient_schedule.rb index 4ad8fd1e..55b6a313 100644 --- a/app/models/recipient_schedule.rb +++ b/app/models/recipient_schedule.rb @@ -9,12 +9,18 @@ class RecipientSchedule < ApplicationRecord validates :next_attempt_at, presence: true scope :ready, -> { where('next_attempt_at <= ?', Time.new) } - scope :for, -> (recipient_or_recipient_id) { + 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? @@ -31,11 +37,24 @@ class RecipientSchedule < ApplicationRecord return if question.nil? && unanswered_attempt.nil? + student = nil + if unanswered_attempt.nil? + if question.for_recipient_students? + students = recipient.students + student = students.first + queued = queued_question_ids.try(:split, /,/) || [] + students[1...students.length].each do + queued << "#{question.id}:#{student.id}" + end + self.queued_question_ids = queued.join(',') + end + attempt = recipient.attempts.create( schedule: schedule, recipient_schedule: self, - question: question + question: question, + student: student ) end diff --git a/app/models/student.rb b/app/models/student.rb new file mode 100644 index 00000000..1ffc5e9a --- /dev/null +++ b/app/models/student.rb @@ -0,0 +1,3 @@ +class Student < ApplicationRecord + belongs_to :recipient +end diff --git a/data/parent_pilot_questions.json b/data/parent_pilot_questions.json index 08dc0193..2b3d391b 100644 --- a/data/parent_pilot_questions.json +++ b/data/parent_pilot_questions.json @@ -1,29 +1,27 @@ [ {"category": "6-A-i", "text": "How often do you meet in person with teachers or administrators at your child's school?", "answers": ["Almost never", "Once or twice a year", "Every few months", "Monthly", "Weekly or more"]}, - {"category": "6-A-i", "text": "In the past year, how often have you discussed your child's school with other parents from the school?", "answers": ["Almost never", "Once or twice a year", "Every few months", "Monthly", "Weekly or more"]}, {"category": "6-A-i", "text": "How involved have you been in activities at your child's school?", "answers": ["Not involved at all", "A little bit involved ", "Somewhat involved", "Quite involved", "Extremely involved"]}, {"category": "6-A-i", "text": "In the past year, how often have you helped out at your child's school?", "answers": ["Almost never", "Once or twice ", "Every few months", "Monthly", "Weekly or more"]}, - {"category": "6-A-i", "text": "How confident are you in your ability to connect with other parents? ", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, - {"category": "6-A-i", "text": "How confident are you in your ability to make sure your child's school meets your child's learning needs? ", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, - {"category": "6-A-i", "text": "How confident are you in your ability to make choices about your child's schooling? ", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, - {"category": "6-A-i", "text": "How confident are you that you can motivate your child to try hard in school? ", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, - {"category": "6-A-i", "text": "How confident are you in your ability to support your child's learning at home? ", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, - {"category": "6-A-i", "text": "How confident are you that you can help your child develop good friendships? ", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, + {"category": "6-A-i", "child": true, "text": "How confident are you in your ability to make sure your child's school meets your child's learning needs? ", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, + {"category": "6-A-i", "text": "How confident are you in your ability to make choices about your child's schooling?", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, + {"category": "6-A-i", "child": true, "text": "How confident are you that you can motivate your child to try hard in school?", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, + {"category": "6-A-i", "child": true, "text": "How confident are you in your ability to support your child's learning at home?", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, + {"category": "6-A-i", "child": true, "text": "How confident are you that you can help your child develop good friendships?", "answers": ["Not confident at all", "A little bit confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, {"category": "6-A-i", "text": "Overall, how good is this school at communicating with parents?", "answers": ["Not good at all", "Slightly good", "Somewhat good", "Quite good", "Extremely good"]}, - {"category": "6-A-i", "text": "How often do you visit your child's school for an event, activity, or to volunteer? ", "answers": ["Almost never", "Once or twice a year", "Every few months", "Monthly", "Weekly or more"]}, - {"category": "6-A-i", "text": "How often do you discuss your child's school with other parents from the school? ", "answers": ["Almost never", "Once or twice a year", "Every few months", "Monthly", "Weekly or more"]}, + {"category": "6-A-i", "text": "How often do you visit your child's school for an event, activity, or to volunteer?", "answers": ["Almost never", "Once or twice a year", "Every few months", "Monthly", "Weekly or more"]}, + {"category": "6-A-i", "text": "How often do you discuss your child's school with other parents from the school?", "answers": ["Almost never", "Once or twice a year", "Every few months", "Monthly", "Weekly or more"]}, {"category": "6-A-i", "text": "How confident are you in your ability to connect with other parents at the school? ", "answers": ["Not confident at all", "Slightly confident", "Somewhat confident", "Quite confident", "Extremely confident"]}, {"category": "6-A-ii", "text": "How often would you like the school to host social events for families?", "answers": ["Almost never", "Once or twice a year", "Every few months", "Monthly", "Weekly or more"]}, {"category": "6-A-ii", "text": "How often would you like to meet with school staff to talk about helping children succeed academically?", "answers": ["Almost never", "Once or twice a year", "Every few months", "Monthly", "Weekly or more"]}, - {"category": "6-A-ii", "text": "How much would you enjoy spending an hour in your child’s classroom?", "answers": ["Would not enjoy that at all", "Would enjoy that a little bit", "Would enjoy that somewhat", "Would enjoy that quite a bit", "Would enjoy that a tremendous amount"]}, + {"category": "6-A-ii", "child": true, "text": "How much would you enjoy spending an hour in your child’s classroom?", "answers": ["Would not enjoy that at all", "Would enjoy that a little bit", "Would enjoy that somewhat", "Would enjoy that quite a bit", "Would enjoy that a tremendous amount"]}, {"category": "6-A-ii", "text": "How much would you like to receive text messages from the school about upcoming events?", "answers": ["Would not like that at all", "Would like that a little bit", "Would like that somewhat", "Would like that quite a bit", "Would like that a tremendous amount"]}, {"category": "6-A-ii", "text": "How much would you like to receive messages from the school translated into another language?", "answers": ["Would not like that at all", "Would like that a little bit", "Would like that somewhat", "Would like that quite a bit", "Would like that a tremendous amount"]}, {"category": "6-A-iii", "text": "How would you describe your sense of belonging in the school community? ", "answers": ["Very weak", "Weak", "Neutral", "Strong", "Very strong"]}, {"category": "6-A-iii", "text": "Overall, how good is this school?", "answers": ["Not good at all", "Slightly good", "Somewhat good", "Quite good", "Extremely good"]}, {"category": "6-A-iii", "text": "Overall, how welcome do you feel at this school?", "answers": ["Not welcome at all", "Slightly welcome", "Somewhat welcome", "Quite welcome", "Extremely welcome"]}, - {"category": "6-A-iii", "text": "Overall, how well do you get along well with your child’s teacher(s)?", "answers": ["Do not get along well at all", "Get along a little bit", "Get along somewhat", "Get along quite well", "Get along extremely well"]}, - {"category": "6-A-iii", "text": "Overall, how much do the teachers at this school care about your child?", "answers": ["Do not care about my child at all", "Care about my child a little bit", "Care about my child somewhat", "Care about my child quite a bit", "Care about my child a tremendous amount"]}, + {"category": "6-A-iii", "child": true, "text": "Overall, how well do you get along well with your child’s teacher(s)?", "answers": ["Do not get along well at all", "Get along a little bit", "Get along somewhat", "Get along quite well", "Get along extremely well"]}, + {"category": "6-A-iii", "child": true, "text": "Overall, how much do the teachers at this school care about your child?", "answers": ["Do not care about my child at all", "Care about my child a little bit", "Care about my child somewhat", "Care about my child quite a bit", "Care about my child a tremendous amount"]}, {"category": "6-A-iii", "text": "Overall, how much do people at this school care about your family?", "answers": ["Don’t care at all", "Care a little bit", "Care about my family somewhat ", "Care about my family quite a bit", "Care about my family a tremendous amount"]} ] diff --git a/db/migrate/20170418173141_create_students.rb b/db/migrate/20170418173141_create_students.rb new file mode 100644 index 00000000..cadc1786 --- /dev/null +++ b/db/migrate/20170418173141_create_students.rb @@ -0,0 +1,18 @@ +class CreateStudents < ActiveRecord::Migration[5.0] + def change + create_table :students do |t| + t.string :name + t.string :teacher + t.date :birthdate + t.string :gender + t.string :age + t.string :ethnicity + t.integer :recipient_id + + t.timestamps + end + + add_column :questions, :for_recipient_students, :boolean, default: false + add_column :attempts, :student_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 74c1f97e..92f50695 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170417203127) do +ActiveRecord::Schema.define(version: 20170418173141) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,6 +29,7 @@ ActiveRecord::Schema.define(version: 20170417203127) do t.datetime "updated_at", null: false t.text "twilio_details" t.string "twilio_sid" + t.integer "student_id" t.index ["twilio_sid"], name: "index_attempts_on_twilio_sid", using: :btree end @@ -67,9 +68,10 @@ ActiveRecord::Schema.define(version: 20170417203127) do t.string "option4" t.string "option5" t.integer "category_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "target_group", default: 0 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "target_group", default: 0 + t.boolean "for_recipient_students", default: false end create_table "recipient_lists", force: :cascade do |t| @@ -155,6 +157,18 @@ ActiveRecord::Schema.define(version: 20170417203127) do t.index ["slug"], name: "index_schools_on_slug", unique: true, using: :btree end + create_table "students", force: :cascade do |t| + t.string "name" + t.string "teacher" + t.date "birthdate" + t.string "gender" + t.string "age" + t.string "ethnicity" + t.integer "recipient_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "user_schools", force: :cascade do |t| t.integer "user_id" t.integer "school_id" diff --git a/spec/lib/tasks/survey_rake_spec.rb b/spec/lib/tasks/survey_rake_spec.rb index 7eae2c07..0c2c25aa 100644 --- a/spec/lib/tasks/survey_rake_spec.rb +++ b/spec/lib/tasks/survey_rake_spec.rb @@ -192,6 +192,60 @@ describe "survey:attempt_questions" do end end + describe 'Multiple Students In A Family' do + + before :each do + 3.times do |i| + recipients[1].students.create(name: "Student#{i}") + end + end + + describe 'With A FOR_CHILD Question Is Asked' do + before :each do + questions.first.update_attributes(for_recipient_students: true) + date = ActiveSupport::TimeZone["UTC"].parse(now.strftime("%Y-%m-%dT20:00:00%z")) + Timecop.freeze(date) { subject.invoke } + end + + it 'should create one attempt per recipient regardless of students' do + expect(FakeSMS.messages.length).to eq(3) + recipients.each do |recipient| + expect(recipient.attempts.count).to eq(1) + end + end + + it 'should store queued questions when an attempt is made on first student' do + recipients.each do |recipient| + recipient_schedule = recipient.recipient_schedules.for_schedule(schedule).first + expect(recipient_schedule.queued_question_ids).to be_present + queued_question_ids = recipient_schedule.queued_question_ids.split(/,/) + expect(queued_question_ids.length).to eq(2) + expect(queued_question_ids.first).to eq("#{questions[0].id}:#{students[1].id}") + expect(queued_question_ids.last).to eq("#{questions[0].id}:#{students[2].id}") + end + end + + it 'should set the next_attempt_at to now when attempt is made on first student' + + it 'should set the next_attempt_at to now when attempt is made on second student' + + it 'should set the next_attempt_at in the future when an attempt is made on last student' + + it 'should mention the students name in the text' + end + + describe 'With A General Question Is Asked' do + before :each do + subject.invoke + end + + it 'should not queue up an questions regardless of how many students there are' + + it 'should not mention the students name in the text' + + end + end + describe 'Opted Out Recipient' do before :each do diff --git a/spec/models/student_spec.rb b/spec/models/student_spec.rb new file mode 100644 index 00000000..6928c63e --- /dev/null +++ b/spec/models/student_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Student, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end