diff --git a/lib/WebGUI/Asset/Wobject/Survey.pm b/lib/WebGUI/Asset/Wobject/Survey.pm index 234a6ac73..18ad47f8f 100644 --- a/lib/WebGUI/Asset/Wobject/Survey.pm +++ b/lib/WebGUI/Asset/Wobject/Survey.pm @@ -912,8 +912,8 @@ sub getResponseInfoForView { my ( $code, $taken ); - my $maxTakes = $self->getValue('maxResponsesPerUser'); - my $id = $self->session->user->userId(); + my $maxResponsesPerUser = $self->getValue('maxResponsesPerUser'); + my $userId = $self->session->user->userId(); my $anonId = $self->session->form->process('userid') || $self->session->http->getCookies->{Survey2AnonId} @@ -923,45 +923,45 @@ sub getResponseInfoForView { my $string; #if there is an anonid or id is for a WG user - if ( $anonId or $id != 1 ) { + if ( $anonId or $userId != 1 ) { $string = 'userId'; if ($anonId) { $string = 'anonId'; - $id = $anonId; + $userId = $anonId; } my $responseId = $self->session->db->quickScalar( "select Survey_responseId from Survey_response where $string = ? and assetId = ? and isComplete = 0", - [ $id, $self->getId() ] ); + [ $userId, $self->getId() ] ); if ( !$responseId ) { $code = $self->session->db->quickScalar( "select isComplete from Survey_response where $string = ? and assetId = ? and isComplete > 0 order by endDate desc limit 1", - [ $id, $self->getId() ] + [ $userId, $self->getId() ] ); } $taken = $self->session->db->quickScalar( "select count(*) from Survey_response where $string = ? and assetId = ? and isComplete > 0", - [ $id, $self->getId() ] ); + [ $userId, $self->getId() ] ); } - elsif ( $id == 1 ) { + elsif ( $userId == 1 ) { my $responseId = $self->session->db->quickScalar( 'select Survey_responseId from Survey_response where userId = ? and ipAddress = ? and assetId = ? and isComplete = 0', - [ $id, $ip, $self->getId() ] + [ $userId, $ip, $self->getId() ] ); if ( !$responseId ) { $code = $self->session->db->quickScalar( 'select isComplete from Survey_response where userId = ? and ipAddress = ? and assetId = ? and isComplete > 0 order by endDate desc limit 1', - [ $id, $ip, $self->getId() ] + [ $userId, $ip, $self->getId() ] ); } $taken = $self->session->db->quickScalar( 'select count(*) from Survey_response where userId = ? and ipAddress = ? and assetId = ? and isComplete > 0', - [ $id, $ip, $self->getId() ] + [ $userId, $ip, $self->getId() ] ); } - return ( $code, $taken >= $maxTakes ); + return ( $code, $maxResponsesPerUser > 0 && $taken >= $maxResponsesPerUser ); } #------------------------------------------------------------------- @@ -1462,7 +1462,7 @@ sub responseId { } if ( !$responseId ) { - my $allowedTakes = $self->get('maxResponsesPerUser'); + my $maxResponsesPerUser = $self->get('maxResponsesPerUser'); my $haveTaken; if ( $id == 1 ) { @@ -1478,7 +1478,7 @@ sub responseId { [ $id, $self->getId() ] ); } - if ( $haveTaken < $allowedTakes ) { + if ( $maxResponsesPerUser == 0 || $haveTaken < $maxResponsesPerUser ) { $responseId = $self->session->db->setRow( 'Survey_response', 'Survey_responseId', { @@ -1500,7 +1500,7 @@ sub responseId { $self->persistResponseJSON(); } else { - $self->session->log->debug("haveTaken ($haveTaken) >= allowedTakes ($allowedTakes)"); + $self->session->log->debug("haveTaken ($haveTaken) >= maxResponsesPerUser ($maxResponsesPerUser)"); } } $self->{responseId} = $responseId; @@ -1525,25 +1525,26 @@ sub canTakeSurvey { return 0; } - my $maxTakes = $self->getValue('maxResponsesPerUser'); - my $ip = $self->session->env->getIp; - my $id = $self->session->user->userId(); - my $takenCount = 0; + my $maxResponsesPerUser = $self->getValue('maxResponsesPerUser'); + my $ip = $self->session->env->getIp; + my $userId = $self->session->user->userId(); + my $takenCount = 0; - if ( $id == 1 ) { + if ( $userId == 1 ) { $takenCount = $self->session->db->quickScalar( 'select count(*) from Survey_response where userId = ? and ipAddress = ? ' - . 'and assetId = ? and isComplete > ?', [ $id, $ip, $self->getId(), 0 ] + . 'and assetId = ? and isComplete > ?', [ $userId, $ip, $self->getId(), 0 ] ); } else { $takenCount = $self->session->db->quickScalar( 'select count(*) from Survey_response where userId = ? and assetId = ? and isComplete > ?', - [ $id, $self->getId(), 0 ] ); + [ $userId, $self->getId(), 0 ] ); } - if ( $takenCount >= $maxTakes ) { + # A maxResponsesPerUser value of 0 implies unlimited + if ( $maxResponsesPerUser > 0 && $takenCount >= $maxResponsesPerUser ) { $self->{canTake} = 0; } else { diff --git a/lib/WebGUI/i18n/English/Asset_Survey.pm b/lib/WebGUI/i18n/English/Asset_Survey.pm index c08068381..85c7532b1 100644 --- a/lib/WebGUI/i18n/English/Asset_Survey.pm +++ b/lib/WebGUI/i18n/English/Asset_Survey.pm @@ -960,9 +960,9 @@ directly inside the answer_loop for other types of questions.|, }, 'maxResponsesPerUser' => { - message => q|The number of times the user can attempt to get the correct answer on each question. The default is 1.|, + message => q|The number of times the user can attempt to get the correct answer on each question. 0 means unlimited. The default is 1.|, context => q|Description of a template variable for a template Help page.|, - lastUpdated => 1168643566, + lastUpdated => 1238131023, }, 'survey questions template title' => { diff --git a/t/Asset/Wobject/Survey.t b/t/Asset/Wobject/Survey.t index e2361a58a..4ce46246c 100644 --- a/t/Asset/Wobject/Survey.t +++ b/t/Asset/Wobject/Survey.t @@ -18,7 +18,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 11; +my $tests = 19; plan tests => $tests + 1; #---------------------------------------------------------------------------- @@ -63,10 +63,31 @@ $survey->responseIdCookies(0); my $responseId = $survey->responseId; my $s = WebGUI::Asset::Wobject::Survey->newByResponseId($session, $responseId); is($s->getId, $survey->getId, 'newByResponseId returns same Survey'); +is($s->get('maxResponsesPerUser'), 1, 'maxResponsesPerUser defaults to 1'); +ok($s->canTakeSurvey, '..which means user can take survey'); -#for my $address (@{ $survey->responseJSON->surveyOrder }) { -# diag (Dumper $address); -#} +# Complete Survey +$s->surveyEnd(); + +# Uncache canTake +delete $s->{canTake}; +delete $s->{responseId}; +$s->responseIdCookies(0); +ok(!$s->canTakeSurvey, 'Cannot take survey a second time (maxResponsesPerUser=1)'); +cmp_deeply($s->responseId, undef, '..and similarly cannot get responseId'); + +# Change maxResponsesPerUser to 2 +$s->update({maxResponsesPerUser => 2}); +delete $s->{canTake}; +ok($s->canTakeSurvey, '..but can take when maxResponsesPerUser increased to 2'); +ok($s->responseId, '..and similarly can get responseId'); + +# Change maxResponsesPerUser to 0 +$s->update({maxResponsesPerUser => 0}); +delete $s->{canTake}; +delete $s->{responseId}; +ok($s->canTakeSurvey, '..and also when maxResponsesPerUser set to 0 (unlimited)'); +ok($s->responseId, '..(and similarly for responseId)'); # www_jumpTo {