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:
parent
0bfc16bf9a
commit
146373937d
9 changed files with 236 additions and 102 deletions
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -34,8 +34,8 @@ instances of this class to the database (one per distinct user response).
|
|||
|
||||
Data stored in this object include the order in which questions and answers are
|
||||
presented to the user (L<"surveyOrder">), a snapshot of all completed questions
|
||||
from the user (L<"responses">), the most recently answered question (L<"lastResponse">), the
|
||||
number of questions answered (L<"questionsAnswered">) and the Survey start time (L<"startTime">).
|
||||
from the user (L<"responses">), the most recently answered question (L<"lastResponse">) and the
|
||||
number of questions answered (L<"questionsAnswered">).
|
||||
|
||||
This package is not intended to be used by any other Asset in WebGUI.
|
||||
|
||||
|
|
@ -64,7 +64,7 @@ survey.
|
|||
=head3 $json
|
||||
|
||||
A JSON string used to construct a new Perl object. The string should represent
|
||||
a JSON hash made up of L<"startTime">, L<"surveyOrder">, L<"responses">, L<"lastResponse">
|
||||
a JSON hash made up of L<"surveyOrder">, L<"responses">, L<"lastResponse">
|
||||
and L<"questionsAnswered"> keys, with appropriate values.
|
||||
|
||||
=cut
|
||||
|
|
@ -108,7 +108,6 @@ sub reset {
|
|||
responses => {},
|
||||
lastResponse => -1,
|
||||
questionsAnswered => 0,
|
||||
startTime => time(),
|
||||
surveyOrder => undef,
|
||||
tags => {},
|
||||
};
|
||||
|
|
@ -225,7 +224,7 @@ sub freeze {
|
|||
my $self = shift;
|
||||
|
||||
# These are the only properties of the response hash that we serialize:
|
||||
my @props = qw(responses lastResponse questionsAnswered startTime tags);
|
||||
my @props = qw(responses lastResponse questionsAnswered tags);
|
||||
my %serialize;
|
||||
@serialize{@props} = @{$self->response}{@props};
|
||||
return to_json(\%serialize);
|
||||
|
|
@ -233,25 +232,6 @@ sub freeze {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=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.
|
||||
|
||||
=cut
|
||||
|
||||
sub hasTimedOut{
|
||||
my $self = shift;
|
||||
my ($limit) = validate_pos(@_, {type => SCALAR});
|
||||
return $limit > 0 && $self->startTime + $limit * 60 < time;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 lastResponse ([ $responseIndex ])
|
||||
|
||||
Mutator. The lastResponse property represents the surveyOrder index of the most recent item shown.
|
||||
|
|
@ -304,30 +284,6 @@ sub questionsAnswered {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 startTime ([ $startTime ])
|
||||
|
||||
Mutator for the time the user began the survey.
|
||||
Returns (and optionally sets) the value of startTime.
|
||||
|
||||
=head3 $startTime (optional)
|
||||
|
||||
If defined, sets the starting time to $startTime.
|
||||
|
||||
=cut
|
||||
|
||||
sub startTime {
|
||||
my $self = shift;
|
||||
my ($startTime) = validate_pos(@_, {type => SCALAR, optional => 1});
|
||||
|
||||
if ( defined $startTime ) {
|
||||
$self->response->{startTime} = $startTime;
|
||||
}
|
||||
|
||||
return $self->response->{startTime};
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 tags ([ $tags ])
|
||||
|
||||
Mutator for the tags that have been applied to the response.
|
||||
|
|
|
|||
|
|
@ -106,20 +106,32 @@ sub execute {
|
|||
my $self = shift;
|
||||
my $session = $self->session;
|
||||
|
||||
my $sql = "select r.Survey_responseId, r.username, r.userId, upd.email,upd.firstName,upd.lastName, r.startDate, s.timeLimit, ad.title, ad.url
|
||||
from Survey s, Survey_response r, assetData ad, userProfileData upd
|
||||
where r.isComplete = 0 and s.timeLimit > 0 and (unix_timestamp() - r.startDate) > (s.timeLimit * 60)
|
||||
and r.assetId = s.assetId and s.revisionDate = (select max(revisionDate) from Survey where assetId = s.assetId)
|
||||
and ad.assetId = s.assetId and ad.revisionDate = s.revisionDate and upd.userId = r.userId";
|
||||
my $sql = <<END_SQL;
|
||||
select r.Survey_responseId, r.username, r.userId, upd.email, upd.firstName, upd.lastName, r.startDate, s.timeLimit, s.doAfterTimeLimit, ad.title, ad.url
|
||||
from Survey s, Survey_response r, assetData ad, userProfileData upd
|
||||
where r.isComplete = 0
|
||||
and s.timeLimit > 0
|
||||
and ( unix_timestamp() - r.startDate ) > ( s.timeLimit * 60 )
|
||||
and r.assetId = s.assetId
|
||||
and s.revisionDate = r.revisionDate
|
||||
and ad.assetId = s.assetId
|
||||
and ad.revisionDate = s.revisionDate
|
||||
and upd.userId = r.userId
|
||||
END_SQL
|
||||
|
||||
my $refs = $self->session->db->buildArrayRefOfHashRefs($sql);
|
||||
for my $ref (@{$refs}) {
|
||||
if($self->get("deleteExpired") == 1){
|
||||
if($self->get("deleteExpired")){
|
||||
$session->log->debug("deleting response: $ref->{Survey_responseId} ");
|
||||
$self->session->db->write("delete from Survey_response where Survey_responseId = ?",[$ref->{Survey_responseId}]);
|
||||
}else{#else sent to expired but not deleted
|
||||
$self->session->db->write("update Survey_response set isComplete = 99 where Survey_responseId = ?",[$ref->{Survey_responseId}]);
|
||||
}else{
|
||||
# Mark response as expired (with appropriate completeCode)
|
||||
my $completeCode = $ref->{doAfterTimeLimit} eq 'restartSurvey' ? 4 : 3;
|
||||
$self->session->db->write("update Survey_response set isComplete = ?, endDate = ? where Survey_responseId = ?",
|
||||
[$completeCode, scalar time, $ref->{Survey_responseId}]
|
||||
);
|
||||
}
|
||||
if($self->get("emailUsers") == 1 && $ref->{email} =~ /\@/){
|
||||
|
||||
my $var = {
|
||||
to => $ref->{email},
|
||||
from => $self->get("from"),
|
||||
|
|
@ -147,6 +159,4 @@ sub execute {
|
|||
return $self->COMPLETE;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
|
||||
1;
|
||||
|
|
@ -3,7 +3,7 @@ use strict;
|
|||
|
||||
our $I18N = {
|
||||
'name' => {
|
||||
message => q|ExpireIncompleteSurveyResponses|,
|
||||
message => q|Expire Incomplete Survey Responses|,
|
||||
lastUpdated => 0,
|
||||
},
|
||||
'Delete expired survey responses' => {
|
||||
|
|
@ -12,7 +12,7 @@ our $I18N = {
|
|||
lastUpdated => 0,
|
||||
},
|
||||
'delete expired' => {
|
||||
message => q|When ran, every survey response which is expired will be completely removed from the database.|,
|
||||
message => q|When run, every survey response which is expired will be completely removed from the database.|,
|
||||
context => q|the hover help for the delete responses field|,
|
||||
lastUpdated => 0,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue