diff --git a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm index 076355de1..cf8e06aeb 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm @@ -293,16 +293,62 @@ sub update { #determine what to add and add it. # ref should contain all the information for the new + +=head2 insertObject ( $object, $address ) + +Add new "objects" into the current data structure. It does not +return anything significant. + +=head3 $object + +A perl data structure. Note, that it is not checked for homegeneity, +so it is possible to add a "question" object into the list of section +objects. + +=head3 $address + +An array ref. The number of elements array set what is added, and +where. + +=over 4 + +=item empty + +If the array ref is empty, nothing is done. + +=item 1 element + +If there's just 1 element, then that element is used as an index into +the array of sections, and $object is spliced into place right after +that index. + +=item 2 elements + +If there are 2 elements, then the first element is an index into +section array, and the second element is an index into the questions +in that section. $object is added right after that question. + +=back + +=item 3 elements + +Three elements are enough to reference an answer, inside of a particular +question in a section. $object is spliced in right after that answer. + +=back + +=cut + sub insertObject { my ( $self, $object, $address ) = @_; if ( @$address == 1 ) { - splice( @{ $self->sections($address) }, $$address[0] + 1, 0, $object ); + splice( @{ $self->sections($address) }, $$address[0] + 1, 0, $object ); ##always a default section } elsif ( @$address == 2 ) { - splice( @{ $self->questions($address) }, $$address[1] + 1, 0, $object ); + splice( @{ $self->questions($address) }, $$address[1] + 1, 0, $object ); ##warning, beyond end of array } elsif ( @$address == 3 ) { - splice( @{ $self->answers($address) }, $$address[2] + 1, 0, $object ); + splice( @{ $self->answers($address) }, $$address[2] + 1, 0, $object ); ##warning, beyond end of array } } diff --git a/t/Asset/Wobject/Survey/SurveyJSON.t b/t/Asset/Wobject/Survey/SurveyJSON.t index 2bda3b2d1..ca9ad0254 100644 --- a/t/Asset/Wobject/Survey/SurveyJSON.t +++ b/t/Asset/Wobject/Survey/SurveyJSON.t @@ -20,7 +20,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 22; +my $tests = 26; plan tests => $tests + 1 + 3; #---------------------------------------------------------------------------- @@ -252,6 +252,101 @@ cmp_deeply( 'newObject: Added an answer to the 2nd question in the 2nd section' ); +#################################################### +# +# insertObject, section +# +#################################################### + +$surveyJSON = WebGUI::Asset::Wobject::Survey::SurveyJSON->new('{}', $session->log); +{ + my $section = $surveyJSON->section([0]); + $section->{title} = 'Section 0'; +} +cmp_deeply( + summarizeSectionSkeleton($surveyJSON), + [ + { + title => 'Section 0', + questions => [], + }, + ], + 'section: Set the title for the default section' +); + +{ + my $section = $surveyJSON->newSection; + $section->{title} = 'Section 1'; + $surveyJSON->insertObject($section, [0]); +} +cmp_deeply( + summarizeSectionSkeleton($surveyJSON), + [ + { + title => 'Section 0', + questions => [], + }, + { + title => 'Section 1', + questions => [], + }, + ], + 'section: Insert a new section after the default section' +); + +{ + my $section = $surveyJSON->newSection; + $section->{title} = 'Section 0+'; + $surveyJSON->insertObject($section, [0]); +} +cmp_deeply( + summarizeSectionSkeleton($surveyJSON), + [ + { + title => 'Section 0', + questions => [], + }, + { + title => 'Section 0+', + questions => [], + }, + { + title => 'Section 1', + questions => [], + }, + ], + 'section: Insert another new section after the default section' +); + +{ + my $question = $surveyJSON->newQuestion; + $question->{text} = 'Question 0-0'; + $surveyJSON->insertObject($question, [0,0]); +} +cmp_deeply( + summarizeSectionSkeleton($surveyJSON), + [ + { + title => 'Section 0', + questions => [ + { + text => 'Question 0-0', + answers => [], + } + ], + }, + { + title => 'Section 0+', + questions => [], + }, + { + title => 'Section 1', + questions => [], + }, + ], + 'section: Insert a question into the first section' +); + #################################################### # # TODO @@ -265,6 +360,35 @@ cmp_deeply( } +# Go through a JSON survey type data structure and just grab some unique +# elements + +sub summarizeSectionSkeleton { + my ($skeleton) = @_; + my $summary = []; + foreach my $section (@{ $skeleton->{sections} }) { + my $summarySection = { + title => $section->{title}, + questions => [], + }; + foreach my $question ( @{ $section->{questions} } ) { + my $summaryQuestion = { + text => $question->{text}, + answers => [], + }; + foreach my $answer ( @{ $question->{answers} } ) { + my $summaryAnswer = { + text => $answer->{text}, + }; + push @{ $summaryQuestion->{answers} }, $summaryAnswer; + } + push @{ $summarySection->{questions} }, $summaryQuestion; + } + push @{ $summary }, $summarySection; + } + return $summary; +} + # [ # [0,1,1], # A section with three questions, no answer, 1 answer, 1 answer # [4], # A section with 1 question with 4 answers