reversed questions

pull/1/head
Jared Cosulich 8 years ago
parent aa5f1ad157
commit e73351d0f5

@ -51,7 +51,7 @@ class Attempt < ApplicationRecord
def response
return 'No Answer Yet' if answer_index.blank?
question.options[answer_index - 1]
question.options[answer_index_with_reverse - 1]
end
def save_response(answer_index: nil, twilio_details: nil, responded_at: Time.new)
@ -66,6 +66,11 @@ class Attempt < ApplicationRecord
end
end
def answer_index_with_reverse
return 5 - answer_index if question.reverse?
return answer_index
end
private
def update_school_categories

@ -40,9 +40,9 @@ class Question < ApplicationRecord
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 }
response_answer_total = school_responses.inject(0) { |total, response| total + response.answer_index_with_reverse }
histogram = school_responses.group_by(&:answer_index_with_reverse)
histogram = school_responses.group_by(&:answer_index)
most_popular_answer_index = histogram.to_a.sort_by { |info| info[1].length }.last[0]
most_popular_answer = send("option#{most_popular_answer_index}")

@ -19,7 +19,7 @@ class SchoolCategory < ApplicationRecord
for_school(school).
select('count(attempts.id) as attempt_count').
select('count(attempts.answer_index) as response_count').
select('sum(attempts.answer_index) as answer_index_total')[0]
select('sum(case when questions.reverse then 5 - attempts.answer_index else attempts.answer_index end) as answer_index_total')[0]
return {
attempt_count: attempt_data.attempt_count || 0,

@ -35,4 +35,4 @@
= render 'shared/histogram', data: aggregated_responses
%p.collapse{id: "raw-data#{question.id}"}
= aggregated_responses.responses.map(&:answer_index).join(', ')
= aggregated_responses.responses.map(&:answer_index_with_reverse).join(', ')

@ -76,4 +76,4 @@
%td
= attempt.sent_at.strftime('%x') if attempt.sent_at.present?
%td= link_to truncate(attempt.question.text, length: 60), [@school, attempt.question], title: attempt.question.text
%td= "(#{attempt.answer_index}) #{attempt.response}"
%td= "(#{attempt.answer_index_with_reverse}) #{attempt.response}"

@ -1,4 +1,4 @@
- histogram = data.responses.map(&:answer_index).group_by { |a| a.to_s }
- histogram = data.responses.map(&:answer_index_with_reverse).group_by { |a| a.to_s }
%table.answers
%tbody
- data.question.options.each_with_index do |option, index|

@ -0,0 +1,5 @@
class AddReverseToQuestions < ActiveRecord::Migration[5.0]
def change
add_column :questions, :reverse, :boolean, default: false
end
end

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170418173141) do
ActiveRecord::Schema.define(version: 20171028181758) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -72,6 +72,7 @@ ActiveRecord::Schema.define(version: 20170418173141) do
t.datetime "updated_at", null: false
t.integer "target_group", default: 0
t.boolean "for_recipient_students", default: false
t.boolean "reverse", default: false
end
create_table "recipient_lists", force: :cascade do |t|

Loading…
Cancel
Save