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:
parent
13bec1ac46
commit
07e03c7b15
3 changed files with 51 additions and 29 deletions
|
|
@ -912,8 +912,8 @@ sub getResponseInfoForView {
|
||||||
|
|
||||||
my ( $code, $taken );
|
my ( $code, $taken );
|
||||||
|
|
||||||
my $maxTakes = $self->getValue('maxResponsesPerUser');
|
my $maxResponsesPerUser = $self->getValue('maxResponsesPerUser');
|
||||||
my $id = $self->session->user->userId();
|
my $userId = $self->session->user->userId();
|
||||||
my $anonId
|
my $anonId
|
||||||
= $self->session->form->process('userid')
|
= $self->session->form->process('userid')
|
||||||
|| $self->session->http->getCookies->{Survey2AnonId}
|
|| $self->session->http->getCookies->{Survey2AnonId}
|
||||||
|
|
@ -923,45 +923,45 @@ sub getResponseInfoForView {
|
||||||
my $string;
|
my $string;
|
||||||
|
|
||||||
#if there is an anonid or id is for a WG user
|
#if there is an anonid or id is for a WG user
|
||||||
if ( $anonId or $id != 1 ) {
|
if ( $anonId or $userId != 1 ) {
|
||||||
$string = 'userId';
|
$string = 'userId';
|
||||||
if ($anonId) {
|
if ($anonId) {
|
||||||
$string = 'anonId';
|
$string = 'anonId';
|
||||||
$id = $anonId;
|
$userId = $anonId;
|
||||||
}
|
}
|
||||||
my $responseId
|
my $responseId
|
||||||
= $self->session->db->quickScalar(
|
= $self->session->db->quickScalar(
|
||||||
"select Survey_responseId from Survey_response where $string = ? and assetId = ? and isComplete = 0",
|
"select Survey_responseId from Survey_response where $string = ? and assetId = ? and isComplete = 0",
|
||||||
[ $id, $self->getId() ] );
|
[ $userId, $self->getId() ] );
|
||||||
if ( !$responseId ) {
|
if ( !$responseId ) {
|
||||||
$code = $self->session->db->quickScalar(
|
$code = $self->session->db->quickScalar(
|
||||||
"select isComplete from Survey_response where $string = ? and assetId = ? and isComplete > 0 order by endDate desc limit 1",
|
"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
|
$taken
|
||||||
= $self->session->db->quickScalar(
|
= $self->session->db->quickScalar(
|
||||||
"select count(*) from Survey_response where $string = ? and assetId = ? and isComplete > 0",
|
"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(
|
my $responseId = $self->session->db->quickScalar(
|
||||||
'select Survey_responseId from Survey_response where userId = ? and ipAddress = ? and assetId = ? and isComplete = 0',
|
'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 ) {
|
if ( !$responseId ) {
|
||||||
$code = $self->session->db->quickScalar(
|
$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',
|
'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(
|
$taken = $self->session->db->quickScalar(
|
||||||
'select count(*) from Survey_response where userId = ? and ipAddress = ? and assetId = ? and isComplete > 0',
|
'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 ) {
|
if ( !$responseId ) {
|
||||||
my $allowedTakes = $self->get('maxResponsesPerUser');
|
my $maxResponsesPerUser = $self->get('maxResponsesPerUser');
|
||||||
my $haveTaken;
|
my $haveTaken;
|
||||||
|
|
||||||
if ( $id == 1 ) {
|
if ( $id == 1 ) {
|
||||||
|
|
@ -1478,7 +1478,7 @@ sub responseId {
|
||||||
[ $id, $self->getId() ] );
|
[ $id, $self->getId() ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $haveTaken < $allowedTakes ) {
|
if ( $maxResponsesPerUser == 0 || $haveTaken < $maxResponsesPerUser ) {
|
||||||
$responseId = $self->session->db->setRow(
|
$responseId = $self->session->db->setRow(
|
||||||
'Survey_response',
|
'Survey_response',
|
||||||
'Survey_responseId', {
|
'Survey_responseId', {
|
||||||
|
|
@ -1500,7 +1500,7 @@ sub responseId {
|
||||||
$self->persistResponseJSON();
|
$self->persistResponseJSON();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$self->session->log->debug("haveTaken ($haveTaken) >= allowedTakes ($allowedTakes)");
|
$self->session->log->debug("haveTaken ($haveTaken) >= maxResponsesPerUser ($maxResponsesPerUser)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$self->{responseId} = $responseId;
|
$self->{responseId} = $responseId;
|
||||||
|
|
@ -1525,25 +1525,26 @@ sub canTakeSurvey {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
my $maxTakes = $self->getValue('maxResponsesPerUser');
|
my $maxResponsesPerUser = $self->getValue('maxResponsesPerUser');
|
||||||
my $ip = $self->session->env->getIp;
|
my $ip = $self->session->env->getIp;
|
||||||
my $id = $self->session->user->userId();
|
my $userId = $self->session->user->userId();
|
||||||
my $takenCount = 0;
|
my $takenCount = 0;
|
||||||
|
|
||||||
if ( $id == 1 ) {
|
if ( $userId == 1 ) {
|
||||||
$takenCount = $self->session->db->quickScalar(
|
$takenCount = $self->session->db->quickScalar(
|
||||||
'select count(*) from Survey_response where userId = ? and ipAddress = ? '
|
'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 {
|
else {
|
||||||
$takenCount
|
$takenCount
|
||||||
= $self->session->db->quickScalar(
|
= $self->session->db->quickScalar(
|
||||||
'select count(*) from Survey_response where userId = ? and assetId = ? and isComplete > ?',
|
'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;
|
$self->{canTake} = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
||||||
|
|
@ -960,9 +960,9 @@ directly inside the answer_loop for other types of questions.|,
|
||||||
},
|
},
|
||||||
|
|
||||||
'maxResponsesPerUser' => {
|
'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.|,
|
context => q|Description of a template variable for a template Help page.|,
|
||||||
lastUpdated => 1168643566,
|
lastUpdated => 1238131023,
|
||||||
},
|
},
|
||||||
|
|
||||||
'survey questions template title' => {
|
'survey questions template title' => {
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ my $session = WebGUI::Test->session;
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
# Tests
|
# Tests
|
||||||
my $tests = 11;
|
my $tests = 19;
|
||||||
plan tests => $tests + 1;
|
plan tests => $tests + 1;
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
|
|
@ -63,10 +63,31 @@ $survey->responseIdCookies(0);
|
||||||
my $responseId = $survey->responseId;
|
my $responseId = $survey->responseId;
|
||||||
my $s = WebGUI::Asset::Wobject::Survey->newByResponseId($session, $responseId);
|
my $s = WebGUI::Asset::Wobject::Survey->newByResponseId($session, $responseId);
|
||||||
is($s->getId, $survey->getId, 'newByResponseId returns same Survey');
|
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 }) {
|
# Complete Survey
|
||||||
# diag (Dumper $address);
|
$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
|
# www_jumpTo
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue