Permalink
Browse files

Make MCQuestionOption -> MCQuestionResponse a many-to-many relationship

  • Loading branch information...
1 parent 038fb13 commit 3e1a20d39d80241753b028ff1e86329f230c253b @benweissmann benweissmann committed Nov 28, 2012
@@ -62,7 +62,8 @@ def responses_csv(sep = ',')
# MC Questions
real_mc_questions.each do |mc_q|
- option_id = @data[:responses][task_response.id][:mcQuestions][mc_q.id]
+ # TODO: make this aware of checkbox MCs
+ option_id = @data[:responses][task_response.id][:mcQuestions][mc_q.id][0]
mc_q_resp = option_id.nil? ? nil : @data[:mcQuestionOptions][option_id][:label]
row.push(mc_q_resp.nil? ? nil : mc_q_resp)
end
@@ -88,9 +89,9 @@ def responses_csv(sep = ',')
# GET /evaluations/1/task_responses.json
def index
@eval = Evaluation.includes({
- :task_responses => {:mc_question_responses => [:mc_question_option], :fr_question_responses => [], :task => [], :m_turk_user => []},
+ :task_responses => {:mc_question_responses => [:mc_question_options], :fr_question_responses => [], :task => [], :m_turk_user => []},
:fr_questions => [:fr_question_responses],
- :mc_questions => {:mc_question_options => [:mc_question_responses => [:mc_question_option]]}
+ :mc_questions => {:mc_question_options => [:mc_question_responses => [:mc_question_options]]}
}).find(params[:evaluation_id])
@task_responses = @eval.task_responses
@@ -165,7 +166,7 @@ def index
mc_question_responses = {}
resp.mc_question_responses.each do |mc_resp|
- mc_question_responses[mc_resp.mc_question_option.mc_question_id] = mc_resp.mc_question_option_id
+ mc_question_responses[mc_resp.mc_question_options.first.mc_question_id] = mc_resp.mc_question_options.map(&:id)
end
responses[resp.id] = {
@@ -21,7 +21,7 @@ class MCQuestionOption < ActiveRecord::Base
before_save :clean_value
belongs_to :mc_question
- has_many :mc_question_responses, :dependent => :destroy
+ has_and_belongs_to_many :mc_question_responses
validates :label, :presence => true
@@ -16,7 +16,14 @@
# Associated with the MCQuestionOption the judge chose, as well as the
# TaskResponse, which represents the judges complete response to a task.
class MCQuestionResponse < ActiveRecord::Base
- belongs_to :mc_question_option
+ has_and_belongs_to_many :mc_question_options
belongs_to :task_response
- has_one :mc_question, :through => :mc_question_option
+
+ def mc_question
+ if self.mc_question_options.blank?
+ nil
+ else
+ self.mc_question_options.first.mc_question
+ end
+ end
end
View
@@ -45,7 +45,7 @@ def add_metadata_as_questions
# create a response that's linked to this task's TaskResponse and
# the appropriate MCQuestionOption
question_response = self.task_response.mc_question_responses.build
- question_response.mc_question_option = opt
+ question_response.mc_question_options = [opt]
question_response.save!
end
end
@@ -176,7 +176,8 @@ Effective pay rate, based on median time: #{format_cents @eval.median_pay_rate}
-# We need the answers to be in the same order as the headers, so we
-# have to look up the response by the question, becuase the questions
-# have a stable order.
- - option_id = @data[:responses][task_response.id][:mcQuestions][mc_q.id]
+ -# TODO: modify this logic to be aware of checkbox MC questions
+ - option_id = @data[:responses][task_response.id][:mcQuestions][mc_q.id].first
-# check for nil in case the worker didn't submit a response to the
-# question
- mc_q_resp = option_id.nil? ? nil : @data[:mcQuestionOptions][option_id][:label]
@@ -0,0 +1,10 @@
+class CreateMCQuestionOptionToResponseJoinTable < ActiveRecord::Migration
+ def change
+ create_table :mc_question_options_mc_question_responses, :id => false do |t|
+ t.references :mc_question_option, :null => false
+ t.references :mc_question_response, :null => false
+ end
+
+ add_index(:mc_question_options_mc_question_responses, [:mc_question_option_id, :mc_question_response_id], :unique => true, :name => :mc_q_opt_resp_idx)
+ end
+end
View
@@ -77,6 +77,8 @@ CREATE TABLE `clockwork_raven_m_turk_users` (
`trusted` tinyint(1) NOT NULL DEFAULT '0',
`banned` tinyint(1) NOT NULL DEFAULT '0',
`prod` int(11) NOT NULL DEFAULT '0',
+ `name` varchar(255) DEFAULT NULL,
+ `notes` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@@ -93,6 +95,12 @@ CREATE TABLE `clockwork_raven_mc_question_options` (
CONSTRAINT `clockwork_raven_mc_question_options_ibfk_1` FOREIGN KEY (`mc_question_id`) REFERENCES `clockwork_raven_mc_questions` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+CREATE TABLE `clockwork_raven_mc_question_options_mc_question_responses` (
+ `mc_question_option_id` int(11) NOT NULL,
+ `mc_question_response_id` int(11) NOT NULL,
+ UNIQUE KEY `mc_q_opt_resp_idx` (`mc_question_option_id`,`mc_question_response_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+
CREATE TABLE `clockwork_raven_mc_question_responses` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`mc_question_option_id` int(11) unsigned DEFAULT NULL,
@@ -180,4 +188,8 @@ INSERT INTO clockwork_raven_schema_migrations (version) VALUES ('20120911001453'
INSERT INTO clockwork_raven_schema_migrations (version) VALUES ('20121010052749');
-INSERT INTO clockwork_raven_schema_migrations (version) VALUES ('20121020214517');
+INSERT INTO clockwork_raven_schema_migrations (version) VALUES ('20121020214517');
+
+INSERT INTO clockwork_raven_schema_migrations (version) VALUES ('20121107205842');
+
+INSERT INTO clockwork_raven_schema_migrations (version) VALUES ('20121120222505');
View
@@ -164,7 +164,7 @@ def register_hit_type eval
props[:QualificationRequirement] = [{
:QualificationTypeId => eval.mturk_qualification_type,
:Comparator => 'Exists',
- :RequiredToPreview => true
+ :RequiredToPreview => true
}]
end
@@ -252,7 +252,7 @@ def fetch_results task
next if option.nil?
question_response = response.mc_question_responses.build
- question_response.mc_question_option = option
+ question_response.mc_question_options = [option]
end
end
@@ -288,7 +288,7 @@ def assignment_results_to_hash assignment
next if mc_question_option.nil? or mc_question_option.mc_question.nil?
answer_key, answer_value = mc_question_option.mc_question.label, mc_question_option.label
end
-
+
next if answer_key.nil?
answer_value = "No response given" if answer_value.nil?
curr_answers_hash[answer_key] = answer_value
@@ -14,7 +14,7 @@
FactoryGirl.define do
factory :mc_question_response do
- mc_question_option
+ mc_question_options { [create(:mc_question_option)] }
task_response
end
end
@@ -47,10 +47,10 @@ class TaskResponsesControllerTest < ActionController::TestCase
r1_mc1 = create :mc_question_response,
:task_response => r1,
- :mc_question_option => mc1_opt1
+ :mc_question_options => [mc1_opt1]
r1_mc2 = create :mc_question_response,
:task_response => r1,
- :mc_question_option => mc2_opt2
+ :mc_question_options => [mc2_opt2]
r1_fr1 = create :fr_question_response,
:task_response => r1,
:fr_question => fr1,
@@ -67,10 +67,10 @@ class TaskResponsesControllerTest < ActionController::TestCase
r2_mc1 = create :mc_question_response,
:task_response => r2,
- :mc_question_option => mc1_opt2
+ :mc_question_options => [mc1_opt2]
r2_mc2 = create :mc_question_response,
:task_response => r2,
- :mc_question_option => mc2_opt3
+ :mc_question_options => [mc2_opt3]
r2_fr1 = create :fr_question_response,
:task_response => r2,
:fr_question => fr1,
@@ -87,10 +87,10 @@ class TaskResponsesControllerTest < ActionController::TestCase
r3_mc1 = create :mc_question_response,
:task_response => r3,
- :mc_question_option => mc1_opt3
+ :mc_question_options => [mc1_opt3]
r3_mc2 = create :mc_question_response,
:task_response => r3,
- :mc_question_option => mc2_opt1
+ :mc_question_options => [mc2_opt1]
r3_fr1 = create :fr_question_response,
:task_response => r3,
:fr_question => fr1,
@@ -158,17 +158,17 @@ class TaskResponsesControllerTest < ActionController::TestCase
},
:responses => {
r1.id => {
- :mcQuestions => {mc1.id => mc1_opt1.id, mc2.id => mc2_opt2.id},
+ :mcQuestions => {mc1.id => [mc1_opt1.id], mc2.id => [mc2_opt2.id]},
:frQuestions => {fr1.id => 'response 1', fr2.id => 'response 2'},
:approved => true
},
r2.id => {
- :mcQuestions => {mc1.id => mc1_opt2.id, mc2.id => mc2_opt3.id},
+ :mcQuestions => {mc1.id => [mc1_opt2.id], mc2.id => [mc2_opt3.id]},
:frQuestions => {fr1.id => 'response 3', fr2.id => 'response 4'},
:approved => false
},
r3.id => {
- :mcQuestions => {mc1.id => mc1_opt3.id, mc2.id => mc2_opt1.id},
+ :mcQuestions => {mc1.id => [mc1_opt3.id], mc2.id => [mc2_opt1.id]},
:frQuestions => {fr1.id => 'response 5', fr2.id => 'response 6'},
:approved => true
}
@@ -192,35 +192,35 @@ def setup_data
@mc2_opt3.save!
@r1 = create :task_response, :task => @e.tasks.first, :approved => nil, :work_duration => 10
- create :mc_question_response, :task_response => @r1,
- :mc_question_option => @mc1_opt1
+ create :mc_question_response, :task_response => @r1,
+ :mc_question_options => [@mc1_opt1]
- create :mc_question_response, :task_response => @r1,
- :mc_question_option => @mc2_opt2
+ create :mc_question_response, :task_response => @r1,
+ :mc_question_options => [@mc2_opt2]
- create :fr_question_response, :task_response => @r1,
- :fr_question => @fr1,
- :response => "response 1"
+ create :fr_question_response, :task_response => @r1,
+ :fr_question => @fr1,
+ :response => "response 1"
- create :fr_question_response, :task_response => @r1,
- :fr_question => @fr2,
- :response => "response 2"
+ create :fr_question_response, :task_response => @r1,
+ :fr_question => @fr2,
+ :response => "response 2"
@r2 = create :task_response, :task => @e.tasks.second, :approved => false, :work_duration => 20
- create :mc_question_response, :task_response => @r2,
- :mc_question_option => @mc1_opt2
+ create :mc_question_response, :task_response => @r2,
+ :mc_question_options => [@mc1_opt2]
- create :mc_question_response, :task_response => @r2,
- :mc_question_option => @mc2_opt3
+ create :mc_question_response, :task_response => @r2,
+ :mc_question_options => [@mc2_opt3]
- create :fr_question_response, :task_response => @r2,
- :fr_question => @fr1,
- :response => "response 3"
+ create :fr_question_response, :task_response => @r2,
+ :fr_question => @fr1,
+ :response => "response 3"
- create :fr_question_response, :task_response => @r2,
- :fr_question => @fr2,
- :response => "response 4"
+ create :fr_question_response, :task_response => @r2,
+ :fr_question => @fr2,
+ :response => "response 4"
end
def get_page
Oops, something went wrong.

0 comments on commit 3e1a20d

Please sign in to comment.