fixed #10565: Survey: add question doing double-create

This commit is contained in:
Patrick Donelan 2009-06-22 07:08:08 +00:00
parent 3fccab7743
commit b5186bfbab
5 changed files with 79 additions and 31 deletions

View file

@ -244,21 +244,21 @@ sub newObject {
if ( $count == 0 ) {
# Add a new section to the end of the list of sections..
push @{ $self->sections }, $self->newSection();
push @{ $self->{_sections} }, $self->newSection();
# Update $address with the index of the newly created section
$address->[0] = $self->lastSectionIndex;
}
elsif ( $count == 1 ) {
# 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->section($address)->{questions} }, $self->newQuestion($address);
# Update $address with the index of the newly created question
$address->[1] = $self->lastQuestionIndex($address);
}
elsif ( $count == 2 ) {
# 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->question($address)->{answers} }, $self->newAnswer($address);
# Update $address with the index of the newly created answer
$address->[2] = $self->lastAnswerIndex($address);
@ -674,18 +674,21 @@ sub uncompress {
my ($smold, $qmold, $amold) = @{$self->mold}{'section', 'question', 'answer'};
# Iterate over all objects, adding back in the missing properties
for my $s (@{$sections || []}) {
for my $s (@$sections) {
for my $q (@{$s->{questions} || []}) {
for my $a (@{$q->{answers} || []}) {
while (my($key, $value) = each %$amold) {
next if ref $value;
$a->{$key} = $value unless exists $a->{$key};
}
}
while (my($key, $value) = each %$qmold) {
next if ref $value;
$q->{$key} = $value unless exists $q->{$key};
}
}
while (my($key, $value) = each %$smold) {
next if ref $value;
$s->{$key} = $value unless exists $s->{$key};
}
}
@ -751,7 +754,7 @@ sub update {
$object = $self->section($address);
if ( !defined $object ) {
$object = $self->newSection();
push @{ $self->sections }, $object;
push @{ $self->{_sections} }, $object;
}
}
elsif ( $count == 2 ) {
@ -759,7 +762,7 @@ sub update {
if ( !defined $object ) {
$object = $self->newQuestion();
$newQuestion = 1; # make note that a new question was created
push @{ $self->questions($address) }, $object;
push @{ $self->section($address)->{questions} }, $object;
}
# If questionType supplied, see if we need to update all of the answers to reflect the new questionType
if ( $properties->{questionType} && $properties->{questionType} ne $object->{questionType} ) {
@ -770,17 +773,26 @@ sub update {
$object = $self->answer($address);
if ( !defined $object ) {
$object = $self->newAnswer();
push @{ $self->answers($address) }, $object;
push @{ $self->question($address)->{answers} }, $object;
}
}
$self->_handleSpecialAnswerUpdates($address,$properties);
my $validSectionProps = $self->newSection;
my $validQuestionProps = $self->newQuestion;
my $validAnswerProps = $self->newAnswer;
# Update $object with all of the data in $properties
while (my ($key, $value) = each %{$properties}) {
if (defined $value) {
$object->{$key} = $value;
}
# Only allow properties that we know about
delete $object->{$key} if $count == 1 and !exists $validSectionProps->{$key};
delete $object->{$key} if $count == 2 and !exists $validQuestionProps->{$key};
delete $object->{$key} if $count == 3 and !exists $validAnswerProps->{$key};
}
return;
@ -849,15 +861,15 @@ sub insertObject {
# Use splice to rearrange the relevant array of objects..
if ( $count == 1 ) {
splice @{ $self->sections($address) }, sIndex($address) +1, 0, $object;
splice @{ $self->{_sections} }, sIndex($address) +1, 0, $object;
$address->[0]++;
}
elsif ( $count == 2 ) {
splice @{ $self->questions($address) }, qIndex($address) + 1, 0, $object;
splice @{ $self->section($address)->{questions} }, qIndex($address) + 1, 0, $object;
$address->[1]++;
}
elsif ( $count == 3 ) {
splice @{ $self->answers($address) }, aIndex($address) + 1, 0, $object;
splice @{ $self->question($address)->{answers} }, aIndex($address) + 1, 0, $object;
$address->[2]++;
}
@ -903,21 +915,21 @@ sub copy {
if ( $count == 1 ) {
# Clone the indexed section onto the end of the list of sections..
push @{ $self->sections }, clone $self->section($address);
push @{ $self->{_sections} }, clone $self->section($address);
# Update $address with the index of the newly created section
$address->[0] = $self->lastSectionIndex;
}
elsif ( $count == 2 ) {
# Clone the indexed question onto the end of the list of questions..
push @{ $self->questions($address) }, clone $self->question($address);
push @{ $self->section($address)->{questions} }, clone $self->question($address);
# Update $address with the index of the newly created question
$address->[1] = $self->lastQuestionIndex($address);
}
elsif ( $count == 3 ) {
# Clone the indexed answer onto the end of the list of answers..
push @{ $self->answers($address) }, clone $self->answer($address);
push @{ $self->question($address)->{answers} }, clone $self->answer($address);
# Update $address with the index of the newly created answer
$address->[2]++;
@ -969,14 +981,14 @@ sub remove {
if ( $count == 1 ) {
# Make sure the first section isn't removed unless we REALLY want to
if ( sIndex($address) != 0 || defined $movingOverride ) {
splice @{ $self->sections }, sIndex($address), 1;
splice @{ $self->{_sections} }, sIndex($address), 1;
}
}
elsif ( $count == 2 ) {
splice @{ $self->questions($address) }, qIndex($address), 1;
splice @{ $self->section($address)->{questions} }, qIndex($address), 1;
}
elsif ( $count == 3 ) {
splice @{ $self->answers($address) }, aIndex($address), 1;
splice @{ $self->question($address)->{answers} }, aIndex($address), 1;
}
return;
@ -1190,7 +1202,7 @@ Returns a reference to all the sections in this object.
sub sections {
my $self = shift;
return $self->{_sections};
return $self->{_sections} || [];
}
=head2 mold
@ -1259,7 +1271,7 @@ Returns the total number of Sections
sub totalSections {
my $self = shift;
return scalar @{ $self->sections || [] };
return scalar @{ $self->sections };
}
=head2 totalQuestions ($address)
@ -1277,7 +1289,7 @@ sub totalQuestions {
my ($address) = validate_pos(@_, { type => ARRAYREF, optional => 1 });
if ($address) {
return scalar @{ $self->questions($address) || [] };
return scalar @{ $self->questions($address) };
} else {
my $count = 0;
for my $sIndex (0 .. $self->lastSectionIndex) {
@ -1302,7 +1314,7 @@ sub totalAnswers {
my ($address) = validate_pos(@_, { type => ARRAYREF, optional => 1 });
if ($address) {
return scalar @{ $self->answers($address) || [] };
return scalar @{ $self->answers($address) };
} else {
my $count = 0;
for my $sIndex (0 .. $self->lastSectionIndex) {
@ -1521,11 +1533,11 @@ sub questions {
my ($address) = validate_pos(@_, { type => ARRAYREF, optional => 1});
if ($address) {
return $self->sections->[ $address->[0] ]->{questions};
return $self->sections->[ $address->[0] ]->{questions} || [];
} else {
my $questions;
push @$questions, @{$_->{questions} || []} for @{$self->sections};
return $questions;
return $questions || [];
}
}
@ -1579,7 +1591,7 @@ sub answers {
my $self = shift;
my ($address) = validate_pos(@_, { type => ARRAYREF});
return $self->sections->[ $address->[0] ]->{questions}->[ $address->[1] ]->{answers};
return $self->sections->[ $address->[0] ]->{questions}->[ $address->[1] ]->{answers} || [];
}
=head2 answer ($address)