diff --git a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm index 8cfffc6dd..f6094d553 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm @@ -545,6 +545,8 @@ the precedence order is inside-out, in order of questions displayed, e.g. The first to trigger a jump short-circuits the process, meaning that subsequent items are not attempted. +For Sections with questions spread out over several pages, Section-level actions are only performed on the final page of the Section. + =cut sub recordResponses { @@ -557,6 +559,7 @@ sub recordResponses { # We want to record responses against the "next" response section and questions, since these are # the items that have just been displayed to the user. my $section = $self->nextResponseSection(); + my $sId = $self->nextResponseSectionIndex(); # make note of the section id prior to recording any responses # Process responses by looping over expected questions in survey order my @questions = $self->nextQuestions(); @@ -628,9 +631,10 @@ sub recordResponses { time => time, }; - # Only record verbatim if this is a verbatim answer - if ($answer->{verbatim}) { - $newResponse{ $aId }{verbatim} = $responses->{ "${aId}verbatim" }; + # Only record verbatim if answer is marked verbatim + my $verbatim = $responses->{ "${aId}verbatim" }; + if ($answer->{verbatim} && defined $verbatim && length $verbatim) { + $newResponse{ $aId }{verbatim} = $verbatim; } } @@ -720,22 +724,24 @@ sub recordResponses { # N.B. Questions don't have terminalUrls } - # Then Sections.. - - # Section goto - if (my $action = $section->{goto} && $self->processGoto($section->{goto})) { - $self->session->log->debug("Branching on Section goto: $section->{goto}"); - return $action; - } - # Then section gotoExpression - if (my $action = $section->{gotoExpression} && $self->processExpression($section->{gotoExpression})) { - $self->session->log->debug("Branching on Section gotoExpression: $section->{gotoExpression}"); - return $action; - } - # Then section terminal - if ($section->{terminal} && $self->nextResponseSectionIndex != $self->lastResponseSectionIndex) { - $self->session->log->debug("Section terminal: $section->{terminalUrl}"); - return { terminal => $section->{terminalUrl} }; + # Then Sections.. (but if this is the last page of the Section) + my $newSectionIndex = $self->nextResponseSectionIndex; + if ($newSectionIndex != $sId) { + # Section goto + if (my $action = $section->{goto} && $self->processGoto($section->{goto})) { + $self->session->log->debug("Branching on Section goto: $section->{goto}"); + return $action; + } + # Then section gotoExpression + if (my $action = $section->{gotoExpression} && $self->processExpression($section->{gotoExpression})) { + $self->session->log->debug("Branching on Section gotoExpression: $section->{gotoExpression}"); + return $action; + } + # Then section terminal + if ($section->{terminal} && $self->nextResponseSectionIndex != $self->lastResponseSectionIndex) { + $self->session->log->debug("Section terminal: $section->{terminalUrl}"); + return { terminal => $section->{terminalUrl} }; + } } # The above goto and gotoExpression checks will have already called $self->checkForLogicalSection after diff --git a/t/Asset/Wobject/Survey/ResponseJSON.t b/t/Asset/Wobject/Survey/ResponseJSON.t index 5a16f0d32..f4d9c8555 100644 --- a/t/Asset/Wobject/Survey/ResponseJSON.t +++ b/t/Asset/Wobject/Survey/ResponseJSON.t @@ -22,7 +22,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 113; +my $tests = 115; plan tests => $tests + 1; #---------------------------------------------------------------------------- @@ -506,8 +506,23 @@ cmp_deeply($rJSON->processExpression(q{restart()}), { restart => 1 }, 'restart w cmp_deeply($rJSON->processExpression(q{exitUrl(blah)}), { exitUrl => 'blah' }, 'explicit exitUrl works'); cmp_deeply($rJSON->processExpression(q{exitUrl()}), { exitUrl => undef }, 'unspecified exitUrl works too'); +# Section branching should not happen until all questions in a section have been completed +$rJSON->survey->section([0])->{questionsPerPage} = 2; # Has 3 questions, so first submit will not trigger section-branching +$rJSON->survey->section([0])->{gotoExpression} = q{ tag('not so fast'); }; +$rJSON->reset; +$rJSON->recordResponses({ + '0-0-0' => 1, + '0-1-0' => '13 11 66', +}); +cmp_deeply($rJSON->tags, {}, 'No tags yet, section branching should not run yet'); +$rJSON->recordResponses({ + '0-2-1' => 1, +}); +cmp_deeply($rJSON->tags, { 'not so fast' => 1 }, 'Section branching has now run'); + # Clean up after this set of tests -$rJSON->responses({}); +$rJSON->reset; +$rJSON->survey->section([0])->{gotoExpression} = undef; $rJSON->questionsAnswered(-1 * $rJSON->questionsAnswered); #################################################### @@ -851,7 +866,7 @@ $rJSON->recordResponses( { '1-0-0' => 1, # Multi-choice answers are submitted like this, '1-0-0verbatim' => 'insert witty comment', '1-1-1' => 1, # with the selected answer set to 1 - '1-1-1verbatim' => '', + '1-1-1verbatim' => ' ', }); cmp_deeply( $rJSON->responses->{'1-0-0'}, @@ -865,7 +880,7 @@ cmp_deeply( cmp_deeply( $rJSON->responses->{'1-1-1'}, { - 'verbatim' => '', + 'verbatim' => ' ', 'time' => num(time(), 3), 'value' => 'No' },