Fixed Survey to follow convention that a value of 0 for maxResponsesPerUser

should imply unlimited. Updated tests and docs to match.
This commit is contained in:
Patrick Donelan 2009-03-27 05:12:57 +00:00
parent 13bec1ac46
commit 07e03c7b15
3 changed files with 51 additions and 29 deletions

View file

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

View file

@ -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' => {

View file

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