Change ResponseJSON to use a proper mutator for startTime.

Add tests for startTime.
Survey uses the accessor, and no longer needs to manually set startTime.
This commit is contained in:
Colin Kuskie 2008-12-11 19:45:19 +00:00
parent 5096352780
commit 1c1f01d473
3 changed files with 79 additions and 10 deletions

View file

@ -820,7 +820,7 @@ sub prepareShowSurveyTemplate {
$section->{'totalQuestions'} = @{$self->response->surveyOrder};
$section->{'showProgress'} = $self->get('showProgress');
$section->{'showTimeLimit'} = $self->get('showTimeLimit');
$section->{'minutesLeft'} = int((($self->response->{startTime} + (60 * $self->get('timeLimit'))) - time())/60);
$section->{'minutesLeft'} = int((($self->response->startTime() + (60 * $self->get('timeLimit'))) - time())/60);
my $out = $self->processTemplate( $section, $self->get("surveyQuestionsId") );
@ -953,7 +953,6 @@ sub getResponseId {
$self->loadBothJSON($responseId);
$self->response->createSurveyOrder();
$self->{responseId} = $responseId;
$self->response->{startTime} = $time;
$self->saveResponseJSON();
} ## end if ( $haveTaken < $allowedTakes)

View file

@ -63,13 +63,12 @@ sub new {
my $self = defined $temp ? $temp : {};
$self->{survey} = $survey;
$self->{log} = $log;
$self->{surveyOrder}
= defined $temp->{surveyOrder}
? $temp->{surveyOrder}
: []; #an array of question addresses, with the third member being an array of answers
$self->{responses} = defined $temp->{responses} ? $temp->{responses} : {};
$self->{lastResponse} = defined $temp->{lastResponse} ? $temp->{lastResponse} : -1;
$self->{responses} = defined $temp->{responses} ? $temp->{responses} : {};
$self->{lastResponse} = defined $temp->{lastResponse} ? $temp->{lastResponse} : -1;
$self->{questionsAnswered} = defined $temp->{questionsAnswered} ? $temp->{questionsAnswered} : 0;
$self->{startTime} = defined $temp->{startTime} ? $temp->{startTime} : time();
#an array of question addresses, with the third member being an array of answers
$self->{surveyOrder} = defined $temp->{surveyOrder} ? $temp->{surveyOrder} : [];
bless( $self, $class );
return $self;
} ## end sub new
@ -155,11 +154,23 @@ How long the user has to take the survey, in minutes.
sub hasTimedOut{
my $self=shift;
my $limit = shift;
return 1 if($self->{startTime} + ($limit * 60) < time() and $limit > 0);
return 1 if($self->startTime() + ($limit * 60) < time() and $limit > 0);
return 0;
}
#the index of the last surveyOrder entry shown
=head2 lastResponse ([ $responseIndex ])
Mutator for the index of the last surveyOrder entry shown. With no arguments,
returns the lastResponse index.
=head3 $responseIndex
If defined, sets the lastResponse to $responseIndex.
=cut
sub lastResponse {
my $self = shift;
my $res = shift;
@ -171,6 +182,28 @@ sub lastResponse {
}
}
=head2 startTime ([ $newStartTime ])
Mutator for the time the user began the survey. With no arguments,
returns the startTime.
=head3 $newStarttime
If defined, sets the starting time to $newStartTime.
=cut
sub startTime {
my $self = shift;
my $newTime = shift;
if ( defined $newTime ) {
$self->{startTime} = $newTime;
}
else {
return $self->{startTime};
}
}
#array of addresses in which the survey should be presented
sub surveyOrder {
my $self = shift;

View file

@ -19,7 +19,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 1;
my $tests = 13;
plan tests => $tests + 1;
#----------------------------------------------------------------------------
@ -38,9 +38,46 @@ skip $tests, "Unable to load ResponseJSON" unless $usedOk;
#
####################################################
my $newTime = time();
$responseJSON = WebGUI::Asset::Wobject::Survey::ResponseJSON->new('{}', $session->log);
isa_ok($responseJSON , 'WebGUI::Asset::Wobject::Survey::ResponseJSON');
is($responseJSON->lastResponse(), -1, 'new: default lastResponse is -1');
is($responseJSON->{questionsAnswered}, 0, 'new: questionsAnswered is 0 by default');
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->surveyOrder, [], 'new: by default, responses is an empty arrayref');
my $now = time();
my $rJSON = WebGUI::Asset::Wobject::Survey::ResponseJSON->new(qq!{ "startTime": $now }!, $session->log);
cmp_ok(abs($rJSON->startTime() - $now), '<=', 2, 'new: startTime set using JSON');
####################################################
#
# startTime
#
####################################################
$rJSON->startTime(780321600);
is($rJSON->startTime, 780321600, 'startTime: set and get');
####################################################
#
# hasTimedOut
#
####################################################
##Reset for next set of tests
$rJSON->startTime(time());
ok( ! $rJSON->hasTimedOut(1), 'hasTimedOut, not timed out, checked with 1 minute timeout');
ok( ! $rJSON->hasTimedOut(0), 'hasTimedOut, not timed out, checked with 0 minute timeout');
$rJSON->startTime(time()-7200);
ok( $rJSON->hasTimedOut(1), 'hasTimedOut, timed out');
ok( ! $rJSON->hasTimedOut(0), 'hasTimedOut, bad limit');
ok( ! $rJSON->hasTimedOut(4*60), 'hasTimedOut, limit check');
}
####################################################