diff --git a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm index d8d6e3e10..5cb46be09 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm @@ -217,21 +217,57 @@ sub startTime { } #array of addresses in which the survey should be presented + +=head2 surveyOrder + +Accessor for the survey order data structure. It is a deep set of arrays, similar in +structure to a WebGUI::Asset::Wobject::Survey::SurveyJSON address. + + [ $sectionIndex, $questionIndex, [ $answerIndex1, $answerIndex2, ....] + +There is one array element for every section and address in the survey. + +If there are no questions, or no addresses, those array elements will not be present. + +=cut + sub surveyOrder { my $self = shift; return $self->{surveyOrder}; } +=head2 nextSectionId + +Relative to the surveyOrder and the lastResponse index, get the index of the +next section. Note, based on the number of questions in an section, this can +be the same as the current section index. + +=cut + sub nextSectionId { my $self = shift; return $self->surveyOrder->[ $self->lastResponse + 1 ]->[0]; } +=head2 nextSection + +Relative to the surveyOrder and the lastResponse index, gets the next section. +Note, based on the number of questions in a section, this can be the same as +the current section. + +=cut + sub nextSection { my $self = shift; return $self->survey->section( [ $self->surveyOrder->[ $self->lastResponse + 1 ]->[0] ] ); } +=head2 currentSection + +Relative to the surveyOrder and the lastResponse index, get the current section. + +=cut + sub currentSection { my $self = shift; return $self->survey->section( [ $self->surveyOrder->[ $self->lastResponse ]->[0] ] ); @@ -363,9 +399,7 @@ sub getPreviousAnswer { sub nextQuestions { my $self = shift; - if ( $self->lastResponse >= $#{ $self->surveyOrder } ) { - return []; - } + return [] if $self->surveyEnd; my $nextSectionId = $self->nextSectionId; @@ -400,6 +434,13 @@ sub nextQuestions { return $questions; } ## end sub nextQuestions +=head2 surveyEnd + +Returns true if the current index stored in lastResponse is greater than or +equal to the number of sections in the survey order. + +=cut + sub surveyEnd { my $self = shift; return 1 if ( $self->lastResponse >= $#{ $self->surveyOrder } ); diff --git a/t/Asset/Wobject/Survey/ResponseJSON.t b/t/Asset/Wobject/Survey/ResponseJSON.t index ae24f417e..6f8e12c27 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 = 18; +my $tests = 32; plan tests => $tests + 1; #---------------------------------------------------------------------------- @@ -152,6 +152,69 @@ cmp_deeply( *$shuffleName = &$shuffleRef; } +#################################################### +# +# surveyEnd +# +#################################################### + +$rJSON->lastResponse(2); +ok( ! $rJSON->surveyEnd(), 'surveyEnd, with 9 elements, 2 != end of survey'); +$rJSON->lastResponse(7); +ok( ! $rJSON->surveyEnd(), 'surveyEnd, with 9 elements, 7 != end of survey'); +$rJSON->lastResponse(8); +ok( $rJSON->surveyEnd(), 'surveyEnd, with 9 elements, 8 == end of survey'); +$rJSON->lastResponse(20); +ok( $rJSON->surveyEnd(), 'surveyEnd, with 9 elements, 20 >= end of survey'); + +#################################################### +# +# nextSectionId, nextSection +# +#################################################### + +$rJSON->lastResponse(0); +is($rJSON->nextSectionId(), 0, 'nextSectionId, lastResponse=0, nextSectionId=0'); +cmp_deeply( + $rJSON->nextSection, + $rJSON->survey->section([0]), + 'lastResponse=0, nextSection = section 0' +); +cmp_deeply( + $rJSON->currentSection, + $rJSON->survey->section([0]), + 'lastResponse=0, currentSection = section 0' +); + +$rJSON->lastResponse(2); +is($rJSON->nextSectionId(), 1, 'nextSectionId, lastResponse=2, nextSectionId=1'); +cmp_deeply( + $rJSON->nextSection, + $rJSON->survey->section([1]), + 'lastResponse=2, nextSection = section 1' +); +cmp_deeply( + $rJSON->currentSection, + $rJSON->survey->section([0]), + 'lastResponse=2, currentSection = section 0' +); + +$rJSON->lastResponse(6); +is($rJSON->nextSectionId(), 3, 'nextSectionId, lastResponse=6, nextSectionId=3'); +cmp_deeply( + $rJSON->nextSection, + $rJSON->survey->section([3]), + 'lastResponse=0, nextSection = section 3' +); +cmp_deeply( + $rJSON->currentSection, + $rJSON->survey->section([3]), + 'lastResponse=6, currentSection = section 3' +); + +$rJSON->lastResponse(20); +is($rJSON->nextSectionId(), undef, 'nextSectionId, lastResponse > surveyEnd, nextSectionId=undef'); + } ####################################################