From cc4a9fb949b838988f4377be8ec7221f30ab23de Mon Sep 17 00:00:00 2001 From: Michael Nomitch Date: Mon, 8 Jun 2015 17:27:30 -0500 Subject: [PATCH] quiz question types works with groups fixes CNVS-20948 test plan: - make a quiz with multiple questions and a quiz group with questions of different types in it - ping the API endpoint for this quiz "api/v1/courses/:id/quizzes/:id" - the question_types array contains the types from the quiz group and the types of the non-nested questions Change-Id: I647499707fa024a19fac564688653db3c9f9e4c7 Reviewed-on: https://gerrit.instructure.com/55993 Reviewed-by: Ryan Taylor Tested-by: Jenkins QA-Review: Michael Hargiss Product-Review: Ryan Taylor --- app/models/quizzes/quiz.rb | 13 ++++++++++--- spec/models/quizzes/quiz_spec.rb | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/models/quizzes/quiz.rb b/app/models/quizzes/quiz.rb index 3fad1afef23..34340256e37 100644 --- a/app/models/quizzes/quiz.rb +++ b/app/models/quizzes/quiz.rb @@ -552,9 +552,16 @@ class Quizzes::Quiz < ActiveRecord::Base # Lists all the question types available in this quiz def question_types return [] unless quiz_data - quiz_data.map do |question| - question["question_type"] - end.uniq + + all_question_types = quiz_data.flat_map do |datum| + if datum["entry_type"] == "quiz_group" + datum["questions"].map{|q| q["question_type"]} + else + datum["question_type"] + end + end + + all_question_types.uniq end def has_access_code diff --git a/spec/models/quizzes/quiz_spec.rb b/spec/models/quizzes/quiz_spec.rb index 9844b276407..83726b9f333 100644 --- a/spec/models/quizzes/quiz_spec.rb +++ b/spec/models/quizzes/quiz_spec.rb @@ -1066,6 +1066,25 @@ describe Quizzes::Quiz do end end + describe "#question_types" do + before :once do + @quiz = @course.quizzes.build :title => "test quiz", :hide_results => 'invalid' + end + + it "returns an empty array when no quiz data" do + @quiz.stubs(:quiz_data).returns nil + expect(@quiz.question_types).to eq([]) + end + + it "returns quiz types of question in quiz groups" do + @quiz.stubs(:quiz_data).returns([ + {"question_type" => "foo"}, + {"entry_type" => "quiz_group", "questions" => [{"question_type" => "bar"},{"question_type" => "baz"}]} + ]) + expect(@quiz.question_types).to eq(["foo", "bar", "baz"]) + end + end + describe "#has_file_upload_question?" do let(:quiz) { @course.quizzes.build title: 'File Upload Quiz' }