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

@ -29,6 +29,8 @@
- fixed #10029: Account CSS rule scoping
- fixed #10641: Matrix Asset - Compare/Search buttons broken in Opera
- fixed #10723: RSS Feed Error in Gallery
- fixed Survey response startDate stored twice (Patrick Donelan, SDH Consulting)
- fixed Survey ExpireIncompleteSurveyResponses Workflow Activity not enabled (Patrick Donelan, SDH Consulting)
7.7.16
- fixed #10590: Session::DateTime->secondsToInterval doesn't allow 7 weeks
@ -57,7 +59,7 @@
- fixed #10685: i18n Asset_StoryTopic::rssUrl
- fixed #10686: Can't access Database Links
- fixed #10650: Unflatten WebGUI storage locations
- fixed #10664: ThiingyRecord disappeared... sort of
- fixed #10664: ThingyRecord disappeared... sort of
- fixed #10687: i18n Asset_Product::buy_form_options
- fixed #10651: Dashboard Content positions
- fixed #10695: Adding a new article creates a new version tag

View file

@ -34,6 +34,7 @@ my $session = start(); # this line required
addFriendManagerSettings($session);
fixGalleyImageFolderStyle($session);
fixMapTemplateFolderStyle($session);
addExpireIncompleteSurveyResponsesWorkflow($session);
finish($session); # this line required
@ -86,6 +87,25 @@ sub fixGalleyImageFolderStyle {
print "DONE!\n" unless $quiet;
}
sub addExpireIncompleteSurveyResponsesWorkflow {
my $session = shift;
print "\tAdd ExpireIncompleteSurveyResponses workflow activity... " unless $quiet;
my $none = $session->config->get('workflowActivities/None');
if (! grep { $_ eq 'WebGUI::Workflow::Activity::ExpireIncompleteSurveyResponses' } @$none) {
push @$none, 'WebGUI::Workflow::Activity::ExpireIncompleteSurveyResponses';
}
$session->config->set('workflowActivities/None', [@$none]);
my $workflow = WebGUI::Workflow->new($session, 'pbworkflow000000000001');
my $activity = $workflow->addActivity('WebGUI::Workflow::Activity::ExpireIncompleteSurveyResponses');
$activity->set('title', 'Expire Incomplete Survey Responses');
$activity->set('description', 'Expires incomplete Survey Responses according to per-instance Survey settings');
print "DONE!\n" unless $quiet;
}
# -------------- DO NOT EDIT BELOW THIS LINE --------------------------------
#----------------------------------------------------------------------------

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);

View file

@ -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.

View file

@ -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
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 = (select max(revisionDate) from Survey where assetId = s.assetId)
and ad.assetId = s.assetId and ad.revisionDate = s.revisionDate and upd.userId = r.userId";
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"),
@ -148,5 +160,3 @@ sub execute {
}
1;

View file

@ -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,
},

View file

@ -18,7 +18,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 42;
my $tests = 45;
plan tests => $tests + 1;
#----------------------------------------------------------------------------
@ -73,6 +73,27 @@ is($survey->get('maxResponsesPerUser'), 1, 'maxResponsesPerUser defaults to 1');
ok($survey->canTakeSurvey, '..which means user can take survey');
is($survey->get('revisionDate'), $session->db->quickScalar('select revisionDate from Survey_response where Survey_responseId = ?', [$responseId]), 'Current revisionDate used');
####################################################
#
# startDate
#
####################################################
my $startDate = $survey->startDate;
$survey->startDate($startDate + 10);
is($survey->startDate, $startDate + 10, 'startDate get/set');
####################################################
#
# hasTimedOut
#
####################################################
ok(!$survey->hasTimedOut, 'Survey has not timed out');
$survey->update( { timeLimit => 1 });
$survey->startDate($startDate - 100);
ok($survey->hasTimedOut, '..until we set timeLimit and change startDate');
# Complete Survey
$survey->surveyEnd();
@ -249,7 +270,6 @@ like($storage->getFileContentsAsScalar($filename), qr{
}
#----------------------------------------------------------------------------
# Cleanup
END {

View file

@ -22,7 +22,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 115;
my $tests = 106;
plan tests => $tests + 1;
#----------------------------------------------------------------------------
@ -47,38 +47,6 @@ 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');
my $now = time();
my $rJSON = WebGUI::Asset::Wobject::Survey::ResponseJSON->new(buildSurveyJSON($session), qq!{ "startTime": $now }!);
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');
####################################################
#
@ -86,7 +54,7 @@ ok( ! $rJSON->hasTimedOut(4*60), 'hasTimedOut, limit check');
#
####################################################
$rJSON = WebGUI::Asset::Wobject::Survey::ResponseJSON->new(buildSurveyJSON($session), q!{}!);
my $rJSON = WebGUI::Asset::Wobject::Survey::ResponseJSON->new(buildSurveyJSON($session), q!{}!);
#$rJSON->initSurveyOrder();
cmp_deeply(

View file

@ -0,0 +1,107 @@
# Test WebGUI::Workflow::Activity::ExpireIncompleteSurveyResponses
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$FindBin::Bin/../../lib";
use WebGUI::Test;
use Test::More;
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
plan tests => 15;
use_ok('WebGUI::Workflow::Activity::ExpireIncompleteSurveyResponses');
my $wf = WebGUI::Workflow->create($session, {
title => 'Test ExpireIncompleteSurveyResponses',
enabled => 1,
type => 'None',
mode => 'realtime',
});
isa_ok($wf, 'WebGUI::Workflow', 'Test workflow');
WebGUI::Test->workflowsToDelete($wf);
my $activity = $wf->addActivity('WebGUI::Workflow::Activity::ExpireIncompleteSurveyResponses');
isa_ok($activity, 'WebGUI::Workflow::Activity::ExpireIncompleteSurveyResponses', 'Test wf activity');
$activity->set('title', 'Test Expire Incomplete Survey Responses');
my $user = WebGUI::User->new($session, 'new');
WebGUI::Test->usersToDelete($user);
# Create a Survey
my $survey = WebGUI::Asset->getImportNode($session)->addChild( { className => 'WebGUI::Asset::Wobject::Survey', } );
my $sJSON = $survey->surveyJSON;
$sJSON->newObject([0]); # add a question to 0th section
$sJSON->update([0,0], { questionType => 'Yes/No' });
$survey->persistSurveyJSON;
# Now start a response as the test user
$session->user( { user => $user } );
my $responseId = $survey->responseId;
ok($responseId, 'Started a survey response');
my ($endDate, $isComplete) = $session->db->quickArray('select endDate, isComplete from Survey_response where Survey_responseId = ?', [$responseId]);
is($endDate, 0, '..currently with no endDate');
is($isComplete, 0, '..and marked as in-progress');
WebGUI::Workflow::Instance->create($session,
{
workflowId => $wf->getId,
skipSpectreNotification => 1,
priority => 1,
}
)->start;
($endDate, $isComplete) = $session->db->quickArray('select endDate, isComplete from Survey_response where Survey_responseId = ?', [$responseId]);
is($endDate + $isComplete, 0, '..no change after workflow runs the first time');
# Change survey time limit
ok(!$survey->hasTimedOut, 'Survey has not timed out (yet)');
$survey->update( { timeLimit => 1 } );
$survey->startDate($survey->startDate - 100);
ok($survey->hasTimedOut, '..until we set a timeLimit and push startDate into the past');
WebGUI::Workflow::Instance->create($session,
{
workflowId => $wf->getId,
skipSpectreNotification => 1,
priority => 1,
}
)->start;
($endDate, $isComplete) = $session->db->quickArray('select endDate, isComplete from Survey_response where Survey_responseId = ?', [$responseId]);
ok($endDate, 'endDate now set');
is($isComplete, 3, '..and isComplete set to timeout code');
# Undo out handiwork, and chage doAfterTimeLimit to restartSurvey so that we get a different completeCode
$session->db->write('update Survey_response set endDate = 0, isComplete = 0 where Survey_responseId = ?', [$responseId]);
$survey->update( { doAfterTimeLimit => 'restartSurvey' } );
WebGUI::Workflow::Instance->create($session,
{
workflowId => $wf->getId,
skipSpectreNotification => 1,
priority => 1,
}
)->start;
($endDate, $isComplete) = $session->db->quickArray('select endDate, isComplete from Survey_response where Survey_responseId = ?', [$responseId]);
ok($endDate, 'endDate set again');
is($isComplete, 4, '..and isComplete now set to timeoutRestart code');
# Undo out handiwork again, and chage workflow to delete
is($session->db->quickScalar('select count(*) from Survey_response where Survey_responseId = ?', [$responseId]), 1, 'Start off with 1 response');
$session->db->write('update Survey_response set endDate = 0, isComplete = 0 where Survey_responseId = ?', [$responseId]);
$activity->set('deleteExpired', 1);
WebGUI::Workflow::Instance->create($session,
{
workflowId => $wf->getId,
skipSpectreNotification => 1,
priority => 1,
}
)->start;
is($session->db->quickScalar('select count(*) from Survey_response where Survey_responseId = ?', [$responseId]), 0, 'Response has now been deleted');
END {
$session->db->write('delete from Survey_response where userId = ?', [$user->userId]) if $user;
}