Moved ResponseJSON data hash to private variable, for security and

consistency with SurveyJSON

Added some more accessors/mutators, and param validation
This commit is contained in:
Patrick Donelan 2009-02-03 08:32:40 +00:00
parent 17dbf7fa66
commit 66a2adcbe5
3 changed files with 47 additions and 31 deletions

View file

@ -53,7 +53,7 @@ and "questionsAnswered" keys, with appropriate values.
sub new { sub new {
my $class = shift; my $class = shift;
my ($survey, $json) = validate_pos(@_, {isa => 'WebGUI::Asset::Wobject::Survey::SurveyJSON' }, { type => SCALAR, optional => 1}); my ($survey, $json) = validate_pos(@_, {isa => 'WebGUI::Asset::Wobject::Survey::SurveyJSON' }, { type => SCALAR | UNDEF, optional => 1});
# Load json object if given.. # Load json object if given..
my $jsonData = $json ? from_json($json) : {}; my $jsonData = $json ? from_json($json) : {};
@ -63,16 +63,18 @@ sub new {
# First define core members.. # First define core members..
_survey => $survey, _survey => $survey,
_session => $survey->session, _session => $survey->session,
_response => {
# And now object defaults.. # Response hash defaults..
responses => {}, responses => {},
lastResponse => -1, lastResponse => -1,
questionsAnswered => 0, questionsAnswered => 0,
startTime => time(), startTime => time(),
surveyOrder => [], surveyOrder => [],
# And finally, allow jsonData to override defaults and/or add other members # And then allow jsonData to override defaults and/or add other members
%{$jsonData}, %{$jsonData},
},
}; };
return bless $self, $class; return bless $self, $class;
@ -123,7 +125,7 @@ sub createSurveyOrder {
push( @$order, [ $s, $_, \@aorder ] ); push( @$order, [ $s, $_, \@aorder ] );
} }
} ## end for ( my $s = 0; $s <= ... } ## end for ( my $s = 0; $s <= ...
$self->{surveyOrder} = $order; $self->response->{surveyOrder} = $order;
} ## end sub createSurveyOrder } ## end sub createSurveyOrder
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -166,10 +168,7 @@ Serializes the object to JSON, after deleting the log and survey objects stored
sub freeze { sub freeze {
my $self = shift; my $self = shift;
my %temp = %{$self}; return to_json($self->response);
delete $temp{_session};
delete $temp{_survey};
return to_json( \%temp );
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -213,10 +212,10 @@ sub lastResponse {
my $self = shift; my $self = shift;
my $res = shift; my $res = shift;
if ( defined $res ) { if ( defined $res ) {
$self->{lastResponse} = $res; $self->response->{lastResponse} = $res;
} }
else { else {
return $self->{lastResponse}; return $self->response->{lastResponse};
} }
} }
@ -237,10 +236,10 @@ sub questionsAnswered {
my $self = shift; my $self = shift;
my $answered = shift; my $answered = shift;
if ( defined $answered ) { if ( defined $answered ) {
$self->{questionsAnswered} += $answered; $self->response->{questionsAnswered} += $answered;
} }
else { else {
return $self->{questionsAnswered}; return $self->response->{questionsAnswered};
} }
} }
@ -261,10 +260,10 @@ sub startTime {
my $self = shift; my $self = shift;
my $newTime = shift; my $newTime = shift;
if ( defined $newTime ) { if ( defined $newTime ) {
$self->{startTime} = $newTime; $self->response->{startTime} = $newTime;
} }
else { else {
return $self->{startTime}; return $self->response->{startTime};
} }
} }
@ -287,7 +286,7 @@ If there are no questions, or no addresses, those array elements will not be pre
sub surveyOrder { sub surveyOrder {
my $self = shift; my $self = shift;
return $self->{surveyOrder}; return $self->response->{surveyOrder};
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -830,6 +829,17 @@ sub returnResponseForReporting {
#Questions only contain the comment and an array of answer Responses. #Questions only contain the comment and an array of answer Responses.
#Answers only contain, entered text, entered verbatim, their index in the Survey Question Answer array, and the assetId to the uploaded file. #Answers only contain, entered text, entered verbatim, their index in the Survey Question Answer array, and the assetId to the uploaded file.
=head2 session
Accessor for the Perl hash containing Response data
=cut
sub response {
my $self = shift;
return $self->{_response};
}
=head2 responses =head2 responses
Returns a reference to the actual responses to the survey. A response is for a question and Returns a reference to the actual responses to the survey. A response is for a question and
@ -843,7 +853,13 @@ Note, this is an unsafe reference.
sub responses { sub responses {
my $self = shift; my $self = shift;
return $self->{responses}; my $responses = shift;
if ( defined $responses ) {
$self->response->{responses} = $responses;
}
else {
return $self->response->{responses};
}
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------

View file

@ -76,7 +76,7 @@ a JSON hash made up of "survey" and "sections" keys.
sub new { sub new {
my $class = shift; my $class = shift;
my ($session, $json) = validate_pos(@_, {isa => 'WebGUI::Session' }, { type => SCALAR, optional => 1}); my ($session, $json) = validate_pos(@_, {isa => 'WebGUI::Session' }, { type => SCALAR | UNDEF, optional => 1});
# Load json object if given.. # Load json object if given..
my $jsonData = $json ? from_json($json) : {}; my $jsonData = $json ? from_json($json) : {};
@ -874,7 +874,7 @@ The question type determines how many answers to add and what answer text (if an
sub updateQuestionAnswers { sub updateQuestionAnswers {
my $self = shift; my $self = shift;
my ($address, $type) = validate_pos(@_, { type => ARRAYREF }, { type => SCALAR, optional => 1}); my ($address, $type) = validate_pos(@_, { type => ARRAYREF }, { type => SCALAR | UNDEF, optional => 1});
# Make a private copy of the $address arrayref that we can use locally # Make a private copy of the $address arrayref that we can use locally
# when updating answer text without causing side-effects for the caller's $address # when updating answer text without causing side-effects for the caller's $address
@ -1185,7 +1185,7 @@ See L<"Address Parameter">.
sub questions { sub questions {
my $self = shift; my $self = shift;
my ($address) = validate_pos(@_, { type => ARRAYREF}); my ($address) = validate_pos(@_, { type => ARRAYREF, optional => 1});
return $self->sections->[ $address->[0] ]->{questions}; return $self->sections->[ $address->[0] ]->{questions};
} }

View file

@ -44,8 +44,8 @@ $responseJSON = WebGUI::Asset::Wobject::Survey::ResponseJSON->new(buildSurveyJSO
isa_ok($responseJSON , 'WebGUI::Asset::Wobject::Survey::ResponseJSON'); isa_ok($responseJSON , 'WebGUI::Asset::Wobject::Survey::ResponseJSON');
is($responseJSON->lastResponse(), -1, 'new: default lastResponse is -1'); is($responseJSON->lastResponse(), -1, 'new: default lastResponse is -1');
is($responseJSON->{questionsAnswered}, 0, 'new: questionsAnswered is 0 by default'); is($responseJSON->questionsAnswered, 0, 'new: questionsAnswered is 0 by default');
cmp_ok((abs$responseJSON->{startTime} - $newTime), '<=', 2, 'new: by default startTime set to time'); cmp_ok((abs$responseJSON->startTime - $newTime), '<=', 2, 'new: by default startTime set to time');
is_deeply( $responseJSON->responses, {}, 'new: by default, responses is an empty hashref'); is_deeply( $responseJSON->responses, {}, 'new: by default, responses is an empty hashref');
is_deeply( $responseJSON->surveyOrder, [], 'new: by default, responses is an empty arrayref'); is_deeply( $responseJSON->surveyOrder, [], 'new: by default, responses is an empty arrayref');
@ -390,7 +390,7 @@ is($rJSON->lastResponse(), -1, '.. lastResponse changed to -1 due to goto(s0)');
$rJSON->gotoExpression('s2: s1q0 = 3'); $rJSON->gotoExpression('s2: s1q0 = 3');
is($rJSON->lastResponse(), 4, '.. lastResponse changed to 4 due to goto(s2)'); is($rJSON->lastResponse(), 4, '.. lastResponse changed to 4 due to goto(s2)');
$rJSON->{responses} = {}; $rJSON->responses({});
$rJSON->questionsAnswered(-1 * $rJSON->questionsAnswered); $rJSON->questionsAnswered(-1 * $rJSON->questionsAnswered);
#################################################### ####################################################
@ -455,7 +455,7 @@ cmp_deeply(
$rJSON->survey->question([1,0,0])->{terminal} = 1; $rJSON->survey->question([1,0,0])->{terminal} = 1;
$rJSON->survey->question([1,0,0])->{terminalUrl} = 'answer 1-0-0 terminal'; $rJSON->survey->question([1,0,0])->{terminalUrl} = 'answer 1-0-0 terminal';
$rJSON->{responses} = {}; $rJSON->responses({});
$rJSON->lastResponse(2); $rJSON->lastResponse(2);
$rJSON->questionsAnswered(-1 * $rJSON->questionsAnswered); $rJSON->questionsAnswered(-1 * $rJSON->questionsAnswered);