Refactored createSurveyOrder in ResponseJSON

Added lastSectionIndex, lastQuestionIndex, lastAswerIndex convenience
methods to SurveyJSON to simplify iterations over Sections, Questions
and Answers
This commit is contained in:
Patrick Donelan 2009-02-04 00:16:26 +00:00
parent 66a2adcbe5
commit 6fb325699f
2 changed files with 84 additions and 36 deletions

View file

@ -94,39 +94,41 @@ If questions and/or answers were set to be randomized, it is handled in here.
sub createSurveyOrder { sub createSurveyOrder {
my $self = shift; my $self = shift;
my $order;
my $qstarting = 0; # Order Questions in each Section
for ( my $s = 0; $s <= $#{ $self->survey->sections() }; $s++ ) { my @surveyOrder;
for my $sIndex ( 0 .. $self->survey->lastSectionIndex ) {
#create question order for section # Randomize Questions if required..
my @qorder; my @qOrder;
if ( $self->survey->section( [$s] )->{randomizeQuestions} ) { if ( $self->survey->section( [$sIndex] )->{randomizeQuestions} ) {
@qorder = shuffle( ( $qstarting .. $#{ $self->survey->questions( [$s] ) } ) ); @qOrder = shuffle( 0 .. $self->survey->lastQuestionIndex( [$sIndex] ) );
} }
else { else {
@qorder = ( ( $qstarting .. $#{ $self->survey->questions( [$s] ) } ) ); @qOrder = ( 0 .. $self->survey->lastQuestionIndex( [$sIndex] ) );
} }
#if this is an empty section, make sure it is still on the list to be seen # Order Answers in each Question
if ( @qorder == 0 ) { for my $q (@qOrder) {
push( @$order, [$s] );
} # Randomize Answers if required..
$qstarting = 0; my @aOrder;
if ( $self->survey->question( [ $sIndex, $q ] )->{randomizeAnswers} ) {
#create answer order for question @aOrder = shuffle( 0 .. $self->survey->lastAnswerIndex( [ $sIndex, $q ] ) );
for (@qorder) {
my @aorder;
if ( $self->survey->question( [ $s, $_ ] )->{randomizeAnswers} ) {
@aorder = shuffle( ( $qstarting .. $#{ $self->survey->question( [ $s, $_ ] )->{answers} } ) );
} }
else { else {
@aorder = ( ( $qstarting .. $#{ $self->survey->question( [ $s, $_ ] )->{answers} } ) ); @aOrder = ( 0 .. $self->survey->lastAnswerIndex( [ $sIndex, $q ] ) );
} }
push( @$order, [ $s, $_, \@aorder ] ); push @surveyOrder, [ $sIndex, $q, \@aOrder ];
} }
} ## end for ( my $s = 0; $s <= ...
$self->response->{surveyOrder} = $order; # If Section had no Questions, make sure it is still added to @surveyOrder
} ## end sub createSurveyOrder if ( !@qOrder ) {
push @surveyOrder, [$sIndex];
}
}
$self->response->{surveyOrder} = \@surveyOrder;
}
#------------------------------------------------------------------- #-------------------------------------------------------------------

View file

@ -156,21 +156,21 @@ sub newObject {
push @{ $self->sections }, $self->newSection(); push @{ $self->sections }, $self->newSection();
# Update $address with the index of the newly created section # Update $address with the index of the newly created section
$address->[0] = $self->totalSections - 1; $address->[0] = $self->lastSectionIndex;
} }
elsif ( $count == 1 ) { elsif ( $count == 1 ) {
# Add a new question to the end of the list of questions in section located at $address # Add a new question to the end of the list of questions in section located at $address
push @{ $self->questions($address) }, $self->newQuestion($address); push @{ $self->questions($address) }, $self->newQuestion($address);
# Update $address with the index of the newly created question # Update $address with the index of the newly created question
$address->[1] = $self->totalQuestions($address) - 1; $address->[1] = $self->lastQuestionIndex($address);
} }
elsif ( $count == 2 ) { elsif ( $count == 2 ) {
# Add a new answer to the end of the list of answers in section/question located at $address # Add a new answer to the end of the list of answers in section/question located at $address
push @{ $self->answers($address) }, $self->newAnswer($address); push @{ $self->answers($address) }, $self->newAnswer($address);
# Update $address with the index of the newly created answer # Update $address with the index of the newly created answer
$address->[2] = $self->totalAnswers($address) - 1; $address->[2] = $self->lastAnswerIndex($address);
} }
# Return the (modified) $address # Return the (modified) $address
return $address; return $address;
@ -219,18 +219,18 @@ sub getDragDropList {
my ($address) = validate_pos(@_, { type => ARRAYREF }); my ($address) = validate_pos(@_, { type => ARRAYREF });
my @data; my @data;
for my $sIndex (0 .. $self->totalSections - 1) { for my $sIndex (0 .. $self->lastSectionIndex) {
push @data, { text => $self->section( [$sIndex] )->{title}, type => 'section' }; push @data, { text => $self->section( [$sIndex] )->{title}, type => 'section' };
if ( sIndex($address) == $sIndex ) { if ( sIndex($address) == $sIndex ) {
for my $qIndex (0 .. $self->totalQuestions($address) - 1) { for my $qIndex (0 .. $self->lastQuestionIndex($address)) {
push @data, push @data,
{ text => $self->question( [ $sIndex, $qIndex ] )->{text}, { text => $self->question( [ $sIndex, $qIndex ] )->{text},
type => 'question' type => 'question'
} }
; ;
if ( qIndex($address) == $qIndex ) { if ( qIndex($address) == $qIndex ) {
for my $aIndex (0 .. $self->totalAnswers($address) - 1) { for my $aIndex (0 .. $self->lastAnswerIndex($address)) {
push @data, push @data,
{ text => $self->answer( [ $sIndex, $qIndex, $aIndex ] )->{text}, { text => $self->answer( [ $sIndex, $qIndex, $aIndex ] )->{text},
type => 'answer' type => 'answer'
@ -712,14 +712,14 @@ sub copy {
push @{ $self->sections }, dclone $self->section($address); push @{ $self->sections }, dclone $self->section($address);
# Update $address with the index of the newly created section # Update $address with the index of the newly created section
$address->[0] = $self->totalSections - 1; $address->[0] = $self->lastSectionIndex;
} }
elsif ( $count == 2 ) { elsif ( $count == 2 ) {
# Clone the indexed question onto the end of the list of questions.. # Clone the indexed question onto the end of the list of questions..
push @{ $self->questions($address) }, dclone $self->question($address); push @{ $self->questions($address) }, dclone $self->question($address);
# Update $address with the index of the newly created question # Update $address with the index of the newly created question
$address->[1] = $self->totalQuestions($address) - 1; $address->[1] = $self->lastQuestionIndex($address);
} }
# Return the (modified) $address # Return the (modified) $address
return $address; return $address;
@ -1082,6 +1082,52 @@ sub sections {
return $self->{_sections}; return $self->{_sections};
} }
=head2 lastSectionIndex
Convenience method to return the index of the last Section. Frequently used to
iterate over all Sections. e.g. ( 0 .. lastSectionIndex )
=cut
sub lastSectionIndex {
my $self = shift;
return $self->totalSections(@_) - 1;
}
=head2 lastQuestionIndex
Convenience method to return the index of the last Question, overall, or in the
given Section if $address given. Frequently used to
iterate over all Questions. e.g. ( 0 .. lastQuestionIndex )
=head3 $address (optional)
See L<"Address Parameter">.
=cut
sub lastQuestionIndex {
my $self = shift;
return $self->totalQuestions(@_) - 1;
}
=head2 lastQuestionIndex
Convenience method to return the index of the last Answer, overall, or in the
given Question if $address given. Frequently used to
iterate over all Answers. e.g. ( 0 .. lastAnswerIndex )
=head3 $address (optional)
See L<"Address Parameter">.
=cut
sub lastAnswerIndex {
my $self = shift;
return $self->totalAnswers(@_) - 1;
}
=head2 totalSections =head2 totalSections
Returns the total number of Sections Returns the total number of Sections
@ -1095,7 +1141,7 @@ sub totalSections {
=head2 totalQuestions ($address) =head2 totalQuestions ($address)
Returns the total number of Questions overall, or in the given Section if $address given Returns the total number of Questions, overall, or in the given Section if $address given
=head3 $address (optional) =head3 $address (optional)
@ -1111,7 +1157,7 @@ sub totalQuestions {
return scalar @{ $self->questions($address) || [] }; return scalar @{ $self->questions($address) || [] };
} else { } else {
my $count = 0; my $count = 0;
for my $sIndex (0 .. $self->totalSections - 1) { for my $sIndex (0 .. $self->lastSectionIndex) {
$count += $self->totalQuestions([$sIndex]); $count += $self->totalQuestions([$sIndex]);
} }
return $count; return $count;
@ -1136,8 +1182,8 @@ sub totalAnswers {
return scalar @{ $self->answers($address) || [] }; return scalar @{ $self->answers($address) || [] };
} else { } else {
my $count = 0; my $count = 0;
for my $sIndex (0 .. $self->totalSections - 1) { for my $sIndex (0 .. $self->lastSectionIndex) {
for my $qIndex (0 .. $self->totalQuestions([$sIndex]) - 1) { for my $qIndex (0 .. $self->lastQuestionIndex([$sIndex])) {
$count += $self->totalAnswers([$sIndex, $qIndex]); $count += $self->totalAnswers([$sIndex, $qIndex]);
} }
} }