diff --git a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm index 50f7e3d18..91dca0625 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm @@ -426,6 +426,27 @@ sub getPreviousAnswer { #------------------------------------------------------------------- +=head2 nextQuestions + +Returns an array ref of the next questions in the survey. The number of questions +returned is set by the questionsPerPage property of the next section, as determined +by nextSectionId rather than logical section ordering. + +If no questions are available, then it returns an empty array ref. + +Each element of the array ref is a question data structure, from the +WebGUI::Asset::Wobject::Survey::SurveyJSON class, with a section sid field (index of +the containing section) and question id (section and question id concatenated with a +'-') added. The answers array of the question contains answer data structures, also +from WebGUI::Asset::Wobject::Survey::SurveyJSON, with an id field which is the section, +question and answer indexes concatentated together with dashes. + +Section and question [[var]] replacements in text fields. + +All questions and answers are safe copies of the survey data. + +=cut + sub nextQuestions { my $self = shift; @@ -454,10 +475,10 @@ sub nextQuestions { $question{id} = "$$qAddy[0]-$$qAddy[1]"; $question{sid} = "$$qAddy[0]"; for ( @{ $$qAddy[2] } ) { - my $ans = $self->survey->answer( [ $$qAddy[0], $$qAddy[1], $_ ] ); - $ans->{'text'} =~ s/\[\[([^\%]*?)\]\]/$self->getPreviousAnswer($1)/eg; - $ans->{id} = "$$qAddy[0]-$$qAddy[1]-$_"; - push( @{ $question{answers} }, $ans ); + my %ans = %{ $self->survey->answer( [ $$qAddy[0], $$qAddy[1], $_ ] ) }; + $ans{'text'} =~ s/\[\[([^\%]*?)\]\]/$self->getPreviousAnswer($1)/eg; + $ans{id} = "$$qAddy[0]-$$qAddy[1]-$_"; + push( @{ $question{answers} }, \%ans ); } push( @$questions, \%question ); } ## end for ( my $i = 1; $i <= ... diff --git a/t/Asset/Wobject/Survey/ResponseJSON.t b/t/Asset/Wobject/Survey/ResponseJSON.t index 6f8e12c27..05a19f9a9 100644 --- a/t/Asset/Wobject/Survey/ResponseJSON.t +++ b/t/Asset/Wobject/Survey/ResponseJSON.t @@ -20,7 +20,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 32; +my $tests = 36; plan tests => $tests + 1; #---------------------------------------------------------------------------- @@ -169,7 +169,7 @@ ok( $rJSON->surveyEnd(), 'surveyEnd, with 9 elements, 20 >= end of survey'); #################################################### # -# nextSectionId, nextSection +# nextSectionId, nextSection, currentSection # #################################################### @@ -215,6 +215,82 @@ cmp_deeply( $rJSON->lastResponse(20); is($rJSON->nextSectionId(), undef, 'nextSectionId, lastResponse > surveyEnd, nextSectionId=undef'); +#################################################### +# +# nextQuestions +# +#################################################### + +$rJSON->lastResponse(20); +ok($rJSON->surveyEnd, 'nextQuestions: lastResponse indicates end of survey'); +is_deeply($rJSON->nextQuestions, [], 'nextQuestions returns an empty array ref if there are no questions available'); +$rJSON->survey->section([0])->{questionsPerPage} = 2; +$rJSON->survey->section([1])->{questionsPerPage} = 2; +$rJSON->survey->section([2])->{questionsPerPage} = 2; +$rJSON->survey->section([3])->{questionsPerPage} = 2; +$rJSON->lastResponse(-1); +cmp_deeply( + $rJSON->nextQuestions(), + [ + superhashof({ + sid => 0, + id => '0-0', + text => 'Question 0-0', + type => 'question', + answers => [ + superhashof({ + type => 'answer', + id => '0-0-0', + }), + ], + }), + superhashof({ + sid => 0, + id => '0-1', + text => 'Question 0-1', + type => 'question', + answers => [ + superhashof({ + type => 'answer', + id => '0-1-0', + }), + ], + }), + ], + 'nextQuestions returns the correct data structre, amounts and members' +); + +$rJSON->lastResponse(1); +cmp_deeply( + $rJSON->nextQuestions(), + [ + superhashof({ + sid => 0, + id => '0-2', + text => 'Question 0-2', + type => 'question', + answers => [ + superhashof({ + type => 'answer', + id => '0-2-0', + }), + superhashof({ + type => 'answer', + id => '0-2-1', + }), + ], + }), + ], + 'nextQuestions obeys questionPerPage' +); + +$rJSON->lastResponse(4); +cmp_deeply( + $rJSON->nextQuestions(), + undef, + 'nextQuestions: returns undef if the next section is empty' +); + } #################################################### @@ -254,14 +330,14 @@ sub buildSurveyJSON { $sjson->section([1])->{title} = "Section 1"; $sjson->section([2])->{title} = "Section 2"; $sjson->section([3])->{title} = "Section 3"; - $sjson->question([0,0])->{title} = "Question 0-0"; - $sjson->question([0,1])->{title} = "Question 0-1"; - $sjson->question([0,2])->{title} = "Question 0-2"; - $sjson->question([1,0])->{title} = "Question 1-0"; - $sjson->question([1,1])->{title} = "Question 1-1"; - $sjson->question([3,0])->{title} = "Question 3-0"; - $sjson->question([3,1])->{title} = "Question 3-1"; - $sjson->question([3,2])->{title} = "Question 3-2"; + $sjson->question([0,0])->{text} = "Question 0-0"; + $sjson->question([0,1])->{text} = "Question 0-1"; + $sjson->question([0,2])->{text} = "Question 0-2"; + $sjson->question([1,0])->{text} = "Question 1-0"; + $sjson->question([1,1])->{text} = "Question 1-1"; + $sjson->question([3,0])->{text} = "Question 3-0"; + $sjson->question([3,1])->{text} = "Question 3-1"; + $sjson->question([3,2])->{text} = "Question 3-2"; return $sjson; }