Fixed two new Survey bugs

* Survey response startDate stored twice
startDate was being stored both in a column in Survey_response and also inside the
serialised responseJSON. Consolidated to column and moved startDate methods to
Survey.pm where they are actually used. Was not causing errors because both copies
were initialised to "now" at response creation time, and then never changed (this is also
why we don't need any repair code to fix existing survey repsonses in the wild).

* Survey ExpireIncompleteSurveyResponses Workflow Activity not enabled
The only time you'd actually want to modify startDate is when you're trying to simulate
response expiry in test code, which is why I found the above bug when I was writing the
missing test suite for ExpireIncompleteSurveyResponses. Along with test suite, added
upgrade code to enable workflow activity and add it  to the Daily Maintenance Tasks
workflow. Also made minor fixes to the workflow activity, such as making sure it uses
the correct isComplete code.
This commit is contained in:
Patrick Donelan 2009-08-07 01:08:39 +00:00
parent 0bfc16bf9a
commit 146373937d
9 changed files with 236 additions and 102 deletions

View file

@ -782,6 +782,55 @@ sub hasResponses {
#-------------------------------------------------------------------
=head2 hasTimedOut ( $limit )
Checks to see whether this survey has timed out, based on the internally stored starting
time, and the suppied $limit value.
=head3 $limit
How long the user has to take the survey, in minutes. Defaults to the value of C<timeLimit>
=cut
sub hasTimedOut {
my $self = shift;
my $limit = shift;
$limit = $self->get('timeLimit') if not defined $limit;
return $limit > 0 && $self->startDate + $limit * 60 < time;
}
#-------------------------------------------------------------------
=head2 startDate ([ $startDate ])
Mutator for the Response start date, which is stored in a column of
the Survey_response table.
=head3 $startDate (optional)
If defined, sets the starting time to $startDate.
=cut
sub startDate {
my $self = shift;
my ($startDate) = validate_pos(@_, {type => SCALAR, optional => 1});
if ( defined $startDate ) {
$self->session->db->write('update Survey_response set startDate = ? where Survey_responseId = ?', [$startDate, $self->responseId]);
$self->{_startDate} = $startDate;
}
if (!$self->{_startDate}) {
$self->{_startDate} = $self->session->db->quickScalar('select startDate from Survey_response where Survey_responseId = ?', [$self->responseId]);
}
return $self->{_startDate};
}
#-------------------------------------------------------------------
=head2 submitObjectEdit ( $params )
Called by L<www_submitObjectEdit> when an edit is submitted to a survey object.
@ -1737,7 +1786,7 @@ sub www_loadQuestions {
$self->session->log->debug('No responseId, surveyEnd');
return $self->surveyEnd();
}
if ( $self->responseJSON->hasTimedOut( $self->get('timeLimit') ) ) {
if ( $self->hasTimedOut ) {
$self->session->log->debug('Response hasTimedOut, surveyEnd');
return $self->surveyEnd( { timeout => 1 } );
}
@ -1939,7 +1988,7 @@ sub prepareShowSurveyTemplate {
$section->{showProgress} = $self->get('showProgress');
$section->{showTimeLimit} = $self->get('showTimeLimit');
$section->{minutesLeft}
= int( ( ( $self->responseJSON->startTime() + ( 60 * $self->get('timeLimit') ) ) - time() ) / 60 );
= int( ( ( $self->startDate() + ( 60 * $self->get('timeLimit') ) ) - time() ) / 60 );
if(scalar @{$questions} == ($section->{totalQuestions} - $section->{questionsAnswered})){
$section->{isLastPage} = 1
@ -2062,6 +2111,7 @@ sub responseId {
my $takenCount = $self->takenCount( { userId => $userId } );
if ( $maxResponsesPerUser == 0 || $takenCount < $maxResponsesPerUser ) {
# Create a new response
my $startDate = time;
$responseId = $self->session->db->setRow(
'Survey_response',
'Survey_responseId', {
@ -2069,7 +2119,7 @@ sub responseId {
userId => $userId,
ipAddress => $ip,
username => $user->username,
startDate => scalar time,
startDate => $startDate,
endDate => 0,
assetId => $self->getId,
revisionDate => $self->get('revisionDate'),
@ -2079,6 +2129,7 @@ sub responseId {
# Store the newly created responseId
$self->{responseId} = $responseId;
$self->startDate($startDate);
$self->session->log->debug("Created new Survey response: $responseId for user: $userId for Survey: " . $self->getId);