From 941240370bee2cb346572c7907143713530382b5 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 15 Dec 2008 00:09:03 +0000 Subject: [PATCH] Add a mutator for questionsAnswered, with tests. Correct POD for recordResponses. Begin recordResponses answer processing tests. --- .../Asset/Wobject/Survey/ResponseJSON.pm | 37 ++++++++-- t/Asset/Wobject/Survey/ResponseJSON.t | 67 ++++++++++++++++++- 2 files changed, 98 insertions(+), 6 deletions(-) diff --git a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm index fa60c9bde..c939d2809 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm @@ -206,6 +206,30 @@ sub lastResponse { #------------------------------------------------------------------- +=head2 questionsAnswered ([ $questionsAnswered ]) + +Mutator for the number of questions answered. With no arguments, +does a set. + +=head3 $questionsAnswered. + +If defined, increments the number of questions by $questionsAnswered + +=cut + +sub questionsAnswered { + my $self = shift; + my $answered = shift; + if ( defined $answered ) { + $self->{questionsAnswered} += $answered; + } + else { + return $self->{questionsAnswered}; + } +} + +#------------------------------------------------------------------- + =head2 startTime ([ $newStartTime ]) Mutator for the time the user began the survey. With no arguments, @@ -322,10 +346,11 @@ answer id, also described there. =head3 terminal processing -Terminal processing for a section and its question are handled in order. The terminalUrl -setting in a question overrides the terminalUrl setting for its section. Similarly, with -questions, the last terminalUrl setting of the set of questions is what is returned for -the page. +Terminal processing for a section and its questions and answers are handled in +order. The terminalUrl setting in a question overrides the terminalUrl setting +for its section. Similarly, with questions and answers, the last terminalUrl +setting of the set of questions is what is returned for the page, with the questions +and answers being answered in surveyOrder. =head3 goto processing @@ -422,7 +447,9 @@ sub recordResponses { } ## end if ( defined( $responses... } ## end for my $answer ( @{ $question... $qAnswered = 0 if ( !$aAnswered and $question->{required} ); - $self->{questionsAnswered}++ if($aAnswered); + if ($aAnswered) { + $self->questionsAnswered( +1 ); + } } ## end for my $question (@$questions) #if all responses completed, move the lastResponse index to the last question shown diff --git a/t/Asset/Wobject/Survey/ResponseJSON.t b/t/Asset/Wobject/Survey/ResponseJSON.t index 43cc27563..f8f424d52 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 = 44; +my $tests = 52; plan tests => $tests + 1; #---------------------------------------------------------------------------- @@ -343,6 +343,71 @@ cmp_deeply( [ 1, '/terminal' ], 'recordResponses, if section has no questions, returns terminal info in the section.', ); +is($rJSON->questionsAnswered, 0, 'questionsAnswered=0, no questions answered'); + +$rJSON->survey->question([1,0])->{terminal} = 1; +$rJSON->survey->question([1,0])->{terminalUrl} = 'question 1-0 terminal'; + +$rJSON->lastResponse(2); +cmp_deeply( + $rJSON->recordResponses($session, { + '1-0comment' => 'Section 1, question 0 comment', + '1-0-0' => 'First answer', + '1-0-0comment' => 'Section 1, question 0, answer 0 comment', + }), + [ 1, 'question 1-0 terminal' ], + 'recordResponses: question terminal overrides section terminal', +); +is($rJSON->lastResponse(), 4, 'lastResponse advanced to next page of questions'); +is($rJSON->questionsAnswered, 1, 'questionsAnswered=1, answered one question'); + +cmp_deeply( + $rJSON->responses, + { + '1-0' => { + comment => 'Section 1, question 0 comment', + }, + '1-0-0' => { + comment => 'Section 1, question 0, answer 0 comment', + 'time' => num(time(), 3), + value => 1, + }, + '1-1' => { + comment => undef, + } + }, + 'recordResponses: recorded responses correctly, two questions, one answer, comments, values and time' +); + +$rJSON->survey->question([1,0,0])->{terminal} = 1; +$rJSON->survey->question([1,0,0])->{terminalUrl} = 'answer 1-0-0 terminal'; +$rJSON->{responses} = {}; +$rJSON->lastResponse(2); +$rJSON->questionsAnswered(-1 * $rJSON->questionsAnswered); + +cmp_deeply( + $rJSON->recordResponses($session, { + '1-0comment' => 'Section 1, question 0 comment', + '1-0-0' => "\t\t\t\n\n\n\t\t\t", #SOS in whitespace + '1-0-0comment' => 'Section 1, question 0, answer 0 comment', + }), + [ 1, 'answer 1-0-0 terminal' ], + 'recordResponses: answer terminal overrides question and section terminals', +); + +cmp_deeply( + $rJSON->responses, + { + '1-0' => { + comment => 'Section 1, question 0 comment', + }, + '1-1' => { + comment => undef, + } + }, + 'recordResponses: if the answer is all whitespace, it is skipped over' +); +is($rJSON->questionsAnswered, 0, 'question was all whitespace, not answered'); }