Moved Survey.pm's "survey" and "response" properties to private hash

vars and added accessors.

So that you don't end up with mind-bending code that looks like:
$survey->survey->{survey}
This commit is contained in:
Patrick Donelan 2009-02-11 09:26:27 +00:00
parent 316d133a02
commit 8d7599d781
2 changed files with 64 additions and 64 deletions

View file

@ -208,7 +208,7 @@ sub exportAssetData {
# Add in the SurveyJSON data.. # Add in the SurveyJSON data..
$self->loadSurveyJSON(); $self->loadSurveyJSON();
$hash->{properties}{surveyJSON} = $self->survey->freeze; $hash->{properties}{surveyJSON} = $self->surveyJSON->freeze;
return $hash; return $hash;
} }
@ -253,7 +253,7 @@ sub duplicate {
# Make sure SurveyJSON data also gets duplicated # Make sure SurveyJSON data also gets duplicated
$self->loadSurveyJSON(); $self->loadSurveyJSON();
$self->session->db->write( 'update Survey set surveyJSON = ? where assetId = ?', $self->session->db->write( 'update Survey set surveyJSON = ? where assetId = ?',
[ $self->survey->freeze, $newAsset->getId ] ); [ $self->surveyJSON->freeze, $newAsset->getId ] );
return $newAsset; return $newAsset;
} }
@ -282,7 +282,7 @@ sub loadSurveyJSON {
my ($json) = validate_pos(@_, { type => SCALAR, optional => 1 }); my ($json) = validate_pos(@_, { type => SCALAR, optional => 1 });
# Do nothing if survey is already loaded # Do nothing if survey is already loaded
return if $self->survey; return if $self->surveyJSON;
# See if we need to load surveyJSON from the database # See if we need to load surveyJSON from the database
if ( ! defined $json ) { if ( ! defined $json ) {
@ -291,7 +291,7 @@ sub loadSurveyJSON {
} }
# Instantiate the SurveyJSON instance, and store it # Instantiate the SurveyJSON instance, and store it
return $self->{survey} = WebGUI::Asset::Wobject::Survey::SurveyJSON->new( $self->session, $json ); return $self->{_surveyJSON} = WebGUI::Asset::Wobject::Survey::SurveyJSON->new( $self->session, $json );
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -305,7 +305,7 @@ Serializes the SurveyJSON instance and persists it to the database
sub saveSurveyJSON { sub saveSurveyJSON {
my $self = shift; my $self = shift;
my $data = $self->survey->freeze(); my $data = $self->surveyJSON->freeze();
$self->session->db->write( 'update Survey set surveyJSON = ? where assetId = ?', [ $data, $self->getId ] ); $self->session->db->write( 'update Survey set surveyJSON = ? where assetId = ?', [ $data, $self->getId ] );
return; return;
@ -313,14 +313,14 @@ sub saveSurveyJSON {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 survey ( ) =head2 surveyJSON ( )
Accessor for the SurveyJSON object. See L<"loadSurveyJSON"> and L<"saveSurveyJSON"> Accessor for the SurveyJSON object. See L<"loadSurveyJSON"> and L<"saveSurveyJSON">
=cut =cut
sub survey { sub surveyJSON {
return shift->{survey}; return shift->{_surveyJSON};
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -374,7 +374,7 @@ sub www_submitObjectEdit {
} }
# Each object checks the address and then either updates or passes it to the correct child. New objects will have an index of -1. # Each object checks the address and then either updates or passes it to the correct child. New objects will have an index of -1.
my $message = $self->survey->update( \@address, $responses ); my $message = $self->surveyJSON->update( \@address, $responses );
# Persist the changes # Persist the changes
$self->saveSurveyJSON(); $self->saveSurveyJSON();
@ -425,7 +425,7 @@ sub www_jumpTo {
# Go through items in surveyOrder until we find the item corresponding to $id # Go through items in surveyOrder until we find the item corresponding to $id
my $currentIndex = 0; my $currentIndex = 0;
for my $address (@{ $self->response->surveyOrder }) { for my $address (@{ $self->responseJSON->surveyOrder }) {
my ($order_sIndex, $order_qIndex) = @{$address}[0,1]; my ($order_sIndex, $order_qIndex) = @{$address}[0,1];
# For starters, check that we're on the right Section # For starters, check that we're on the right Section
@ -442,7 +442,7 @@ sub www_jumpTo {
# Set the nextResponse to be the index we're up to # Set the nextResponse to be the index we're up to
$self->session->log->debug("Found id: $id at index: $currentIndex in surveyOrder"); $self->session->log->debug("Found id: $id at index: $currentIndex in surveyOrder");
$self->response->nextResponse( $currentIndex ); $self->responseJSON->nextResponse( $currentIndex );
$self->saveResponseJSON(); $self->saveResponseJSON();
return $self->www_takeSurvey; return $self->www_takeSurvey;
} }
@ -472,7 +472,7 @@ sub copyObject {
$self->loadSurveyJSON(); $self->loadSurveyJSON();
#each object checks the ref and then either updates or passes it to the correct child. New objects will have an index of -1. #each object checks the ref and then either updates or passes it to the correct child. New objects will have an index of -1.
$address = $self->survey->copy($address); $address = $self->surveyJSON->copy($address);
$self->saveSurveyJSON(); $self->saveSurveyJSON();
@ -503,7 +503,7 @@ sub deleteObject {
$self->loadSurveyJSON(); $self->loadSurveyJSON();
#each object checks the ref and then either updates or passes it to the correct child. New objects will have an index of -1. #each object checks the ref and then either updates or passes it to the correct child. New objects will have an index of -1.
my $message = $self->survey->remove($address); my $message = $self->surveyJSON->remove($address);
$self->saveSurveyJSON(); $self->saveSurveyJSON();
@ -541,7 +541,7 @@ sub www_newObject {
$self->loadSurveyJSON(); $self->loadSurveyJSON();
#Don't save after this as the new object should not stay in the survey #Don't save after this as the new object should not stay in the survey
my $address = $self->survey->newObject( \@inAddress ); my $address = $self->surveyJSON->newObject( \@inAddress );
#The new temp object has an address of NEW, which means it is not a real final address. #The new temp object has an address of NEW, which means it is not a real final address.
@ -569,15 +569,15 @@ sub www_dragDrop {
my @bid = split /-/, $p->{before}->{id}; my @bid = split /-/, $p->{before}->{id};
$self->loadSurveyJSON(); $self->loadSurveyJSON();
my $target = $self->survey->getObject( \@tid ); my $target = $self->surveyJSON->getObject( \@tid );
$self->survey->remove( \@tid, 1 ); $self->surveyJSON->remove( \@tid, 1 );
my $address = [0]; my $address = [0];
if ( @tid == 1 ) { if ( @tid == 1 ) {
#sections can only be inserted after another section so chop off the question and answer portion of #sections can only be inserted after another section so chop off the question and answer portion of
$#bid = 0; $#bid = 0;
$bid[0] = -1 if ( !defined $bid[0] ); $bid[0] = -1 if ( !defined $bid[0] );
$self->survey->insertObject( $target, [ $bid[0] ] ); $self->surveyJSON->insertObject( $target, [ $bid[0] ] );
} }
elsif ( @tid == 2 ) { #questions can be moved to any section, but a pushed to the end of a new section. elsif ( @tid == 2 ) { #questions can be moved to any section, but a pushed to the end of a new section.
if ( $bid[0] !~ /\d/ ) { if ( $bid[0] !~ /\d/ ) {
@ -597,23 +597,23 @@ sub www_dragDrop {
else { else {
#else move to the end of the selected section #else move to the end of the selected section
$bid[1] = $#{ $self->survey->questions( [ $bid[0] ] ) }; $bid[1] = $#{ $self->surveyJSON->questions( [ $bid[0] ] ) };
} }
} ## end elsif ( @bid == 1 ) } ## end elsif ( @bid == 1 )
$self->survey->insertObject( $target, [ $bid[0], $bid[1] ] ); $self->surveyJSON->insertObject( $target, [ $bid[0], $bid[1] ] );
} ## end elsif ( @tid == 2 ) } ## end elsif ( @tid == 2 )
elsif ( @tid == 3 ) { #answers can only be rearranged in the same question elsif ( @tid == 3 ) { #answers can only be rearranged in the same question
if ( @bid == 2 and $bid[1] == $tid[1] ) { if ( @bid == 2 and $bid[1] == $tid[1] ) {
$bid[2] = -1; $bid[2] = -1;
$self->survey->insertObject( $target, [ $bid[0], $bid[1], $bid[2] ] ); $self->surveyJSON->insertObject( $target, [ $bid[0], $bid[1], $bid[2] ] );
} }
elsif ( @bid == 3 ) { elsif ( @bid == 3 ) {
$self->survey->insertObject( $target, [ $bid[0], $bid[1], $bid[2] ] ); $self->surveyJSON->insertObject( $target, [ $bid[0], $bid[1], $bid[2] ] );
} }
else { else {
#else put it back where it was #else put it back where it was
$self->survey->insertObject( $target, \@tid ); $self->surveyJSON->insertObject( $target, \@tid );
} }
} }
@ -658,7 +658,7 @@ sub www_loadSurvey {
my $var my $var
= defined $options->{var} = defined $options->{var}
? $options->{var} ? $options->{var}
: $self->survey->getEditVars($address); : $self->surveyJSON->getEditVars($address);
my $editHtml; my $editHtml;
if ( $var->{type} eq 'section' ) { if ( $var->{type} eq 'section' ) {
@ -672,7 +672,7 @@ sub www_loadSurvey {
} }
# Generate the list of valid goto targets # Generate the list of valid goto targets
my @gotoTargets = $self->survey->getGotoTargets; my @gotoTargets = $self->surveyJSON->getGotoTargets;
my %buttons; my %buttons;
$buttons{question} = $$address[0]; $buttons{question} = $$address[0];
@ -680,7 +680,7 @@ sub www_loadSurvey {
$buttons{answer} = "$$address[0]-$$address[1]"; $buttons{answer} = "$$address[0]-$$address[1]";
} }
my $data = $self->survey->getDragDropList($address); my $data = $self->surveyJSON->getDragDropList($address);
my $html; my $html;
my ( $scount, $qcount, $acount ) = ( -1, -1, -1 ); my ( $scount, $qcount, $acount ) = ( -1, -1, -1 );
my $lastType; my $lastType;
@ -1011,7 +1011,7 @@ sub www_submitQuestions {
$self->loadBothJSON(); $self->loadBothJSON();
my $termInfo = $self->response->recordResponses( $responses ); my $termInfo = $self->responseJSON->recordResponses( $responses );
$self->saveResponseJSON(); $self->saveResponseJSON();
@ -1076,23 +1076,23 @@ sub www_loadQuestions {
$self->session->log->debug('No responseId, surveyEnd'); $self->session->log->debug('No responseId, surveyEnd');
return $self->surveyEnd(); return $self->surveyEnd();
} }
if ( $self->response->hasTimedOut( $self->get('timeLimit') ) ) { if ( $self->responseJSON->hasTimedOut( $self->get('timeLimit') ) ) {
$self->session->log->debug('Response hasTimedOut, surveyEnd'); $self->session->log->debug('Response hasTimedOut, surveyEnd');
return $self->surveyEnd( undef, 2 ); return $self->surveyEnd( undef, 2 );
} }
if ( $self->response->surveyEnd() ) { if ( $self->responseJSON->surveyEnd() ) {
$self->session->log->debug('Response surveyEnd, so calling surveyEnd'); $self->session->log->debug('Response surveyEnd, so calling surveyEnd');
return $self->surveyEnd(); return $self->surveyEnd();
} }
my @questions; my @questions;
eval { @questions = $self->response->nextQuestions(); }; eval { @questions = $self->responseJSON->nextQuestions(); };
my $section = $self->response->nextResponseSection(); my $section = $self->responseJSON->nextResponseSection();
#return $self->prepareShowSurveyTemplate($section,$questions); #return $self->prepareShowSurveyTemplate($section,$questions);
$section->{id} = $self->response->nextResponseSectionIndex(); $section->{id} = $self->responseJSON->nextResponseSectionIndex();
$section->{wasRestarted} = $wasRestarted; $section->{wasRestarted} = $wasRestarted;
my $text = $self->prepareShowSurveyTemplate( $section, \@questions ); my $text = $self->prepareShowSurveyTemplate( $section, \@questions );
@ -1139,8 +1139,8 @@ sub surveyEnd {
); );
} }
if ($self->get('doAfterTimeLimit') eq 'restartSurvey' && $completeCode == 2){ if ($self->get('doAfterTimeLimit') eq 'restartSurvey' && $completeCode == 2){
$self->response->startTime(time()); $self->responseJSON->startTime(time());
undef $self->{response}; undef $self->{_responseJSON};
undef $self->{responseId}; undef $self->{responseId};
return $self->www_loadQuestions('1'); return $self->www_loadQuestions('1');
}else{ }else{
@ -1216,12 +1216,12 @@ sub prepareShowSurveyTemplate {
} }
} ## end foreach my $q (@$questions) } ## end foreach my $q (@$questions)
$section->{'questions'} = $questions; $section->{'questions'} = $questions;
$section->{'questionsAnswered'} = $self->response->{questionsAnswered}; $section->{'questionsAnswered'} = $self->responseJSON->{questionsAnswered};
$section->{'totalQuestions'} = @{ $self->response->surveyOrder }; $section->{'totalQuestions'} = @{ $self->responseJSON->surveyOrder };
$section->{'showProgress'} = $self->get('showProgress'); $section->{'showProgress'} = $self->get('showProgress');
$section->{'showTimeLimit'} = $self->get('showTimeLimit'); $section->{'showTimeLimit'} = $self->get('showTimeLimit');
$section->{'minutesLeft'} $section->{'minutesLeft'}
= int( ( ( $self->response->startTime() + ( 60 * $self->get('timeLimit') ) ) - time() ) / 60 ); = int( ( ( $self->responseJSON->startTime() + ( 60 * $self->get('timeLimit') ) ) - time() ) / 60 );
if(scalar @$questions == ($section->{'totalQuestions'} - $section->{'questionsAnswered'})){ if(scalar @$questions == ($section->{'totalQuestions'} - $section->{'questionsAnswered'})){
$section->{isLastPage} = 1 $section->{isLastPage} = 1
@ -1248,7 +1248,7 @@ The reponse id to load.
sub loadBothJSON { sub loadBothJSON {
my $self = shift; my $self = shift;
my $rId = shift; my $rId = shift;
if ( defined $self->survey and defined $self->response ) { return; } if ( defined $self->surveyJSON and defined $self->responseJSON ) { return; }
my $ref = $self->session->db->buildArrayRefOfHashRefs( " my $ref = $self->session->db->buildArrayRefOfHashRefs( "
select s.surveyJSON,r.responseJSON select s.surveyJSON,r.responseJSON
from Survey s, Survey_response r from Survey s, Survey_response r
@ -1279,7 +1279,7 @@ sub loadResponseJSON {
my $jsonHash = shift; my $jsonHash = shift;
my $rId = shift; my $rId = shift;
$rId = defined $rId ? $rId : $self->{responseId}; $rId = defined $rId ? $rId : $self->{responseId};
if ( defined $self->response and !defined $rId ) { return; } if ( defined $self->responseJSON and !defined $rId ) { return; }
$jsonHash $jsonHash
= $self->session->db->quickScalar( = $self->session->db->quickScalar(
@ -1287,8 +1287,8 @@ sub loadResponseJSON {
[ $self->getId, $rId ] ) [ $self->getId, $rId ] )
if ( !defined $jsonHash ); if ( !defined $jsonHash );
$self->{response} $self->{_responseJSON}
= WebGUI::Asset::Wobject::Survey::ResponseJSON->new( $self->survey, $jsonHash ); = WebGUI::Asset::Wobject::Survey::ResponseJSON->new( $self->surveyJSON, $jsonHash );
} ## end sub loadResponseJSON } ## end sub loadResponseJSON
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -1302,7 +1302,7 @@ Turns the response object into JSON and saves it to the DB.
sub saveResponseJSON { sub saveResponseJSON {
my $self = shift; my $self = shift;
my $data = $self->response->freeze(); my $data = $self->responseJSON->freeze();
$self->session->db->write( "update Survey_response set responseJSON = ? where Survey_responseId = ?", $self->session->db->write( "update Survey_response set responseJSON = ? where Survey_responseId = ?",
@ -1311,15 +1311,15 @@ sub saveResponseJSON {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 response =head2 responseJSON
Helper to easily grab the response object and prevent typos. Accessor for the ResponseJSON object
=cut =cut
sub response { sub responseJSON {
my $self = shift; my $self = shift;
return $self->{response}; return $self->{_responseJSON};
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -1413,7 +1413,7 @@ sub getResponseId {
); );
# $self->session->log->warn("post: $responseId"); # $self->session->log->warn("post: $responseId");
$self->loadBothJSON($responseId); $self->loadBothJSON($responseId);
# $self->response->createSurveyOrder(); # $self->responseJSON->createSurveyOrder();
$self->{responseId} = $responseId; $self->{responseId} = $responseId;
$self->saveResponseJSON(); $self->saveResponseJSON();
@ -1499,7 +1499,7 @@ sub www_viewGradeBook {
my $users = $paginator->getPageData; my $users = $paginator->getPageData;
$self->loadSurveyJSON(); $self->loadSurveyJSON();
$var->{question_count} = $self->survey->questionCount; $var->{question_count} = $self->surveyJSON->questionCount;
my @responseloop; my @responseloop;
foreach my $user (@$users) { foreach my $user (@$users) {
@ -1539,7 +1539,7 @@ sub www_viewStatisticalOverview {
$self->loadTempReportTable(); $self->loadTempReportTable();
$self->loadSurveyJSON(); $self->loadSurveyJSON();
my $survey = $self->survey; my $survey = $self->surveyJSON;
my $var = $self->getMenuVars; my $var = $self->getMenuVars;
my $paginator = WebGUI::Paginator->new($self->session,$self->getUrl('func=viewStatisticalOverview')); my $paginator = WebGUI::Paginator->new($self->session,$self->getUrl('func=viewStatisticalOverview'));
@ -1715,7 +1715,7 @@ sub loadTempReportTable {
for my $ref (@$refs) { for my $ref (@$refs) {
$self->loadResponseJSON( undef, $ref->{Survey_responseId} ); $self->loadResponseJSON( undef, $ref->{Survey_responseId} );
my $count = 1; my $count = 1;
for my $q ( @{ $self->response->returnResponseForReporting() } ) { for my $q ( @{ $self->responseJSON->returnResponseForReporting() } ) {
if ( @{ $q->{answers} } == 0 and $q->{comment} =~ /\w/ ) { if ( @{ $q->{answers} } == 0 and $q->{comment} =~ /\w/ ) {
$self->session->db->write( $self->session->db->write(
"insert into Survey_tempReport VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", [ "insert into Survey_tempReport VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", [
@ -1737,7 +1737,7 @@ sub loadTempReportTable {
] ]
); );
} }
} ## end for my $q ( @{ $self->response... } ## end for my $q ( @{ $self->responseJSON...
} ## end for my $ref (@$refs) } ## end for my $ref (@$refs)
return 1; return 1;
} ## end sub loadTempReportTable } ## end sub loadTempReportTable

View file

@ -39,23 +39,23 @@ isa_ok($survey, 'WebGUI::Asset::Wobject::Survey');
# Load bare-bones survey, containing a single section (S0) # Load bare-bones survey, containing a single section (S0)
$survey->loadSurveyJSON(); $survey->loadSurveyJSON();
$survey->survey->update([0], { variable => 'S0' }); $survey->surveyJSON->update([0], { variable => 'S0' });
# Add 2 questions to S0 # Add 2 questions to S0
$survey->survey->newObject([0]); # S0Q0 $survey->surveyJSON->newObject([0]); # S0Q0
$survey->survey->update([0,0], { variable => 'S0Q0' }); $survey->surveyJSON->update([0,0], { variable => 'S0Q0' });
$survey->survey->newObject([0]); # S0Q1 $survey->surveyJSON->newObject([0]); # S0Q1
$survey->survey->update([0,1], { variable => 'S0Q1' }); $survey->surveyJSON->update([0,1], { variable => 'S0Q1' });
# Add a new section (S1) # Add a new section (S1)
$survey->survey->newObject([]); # S1 $survey->surveyJSON->newObject([]); # S1
$survey->survey->update([1], { variable => 'S1' }); $survey->surveyJSON->update([1], { variable => 'S1' });
# Add 2 questions to S1 # Add 2 questions to S1
$survey->survey->newObject([1]); # S1Q0 $survey->surveyJSON->newObject([1]); # S1Q0
$survey->survey->update([1,0], { variable => 'S1Q0' }); $survey->surveyJSON->update([1,0], { variable => 'S1Q0' });
$survey->survey->newObject([1]); # S1Q1 $survey->surveyJSON->newObject([1]); # S1Q1
$survey->survey->update([1,1], { variable => 'S1Q1' }); $survey->surveyJSON->update([1,1], { variable => 'S1Q1' });
# Persist to db # Persist to db
$survey->saveSurveyJSON(); $survey->saveSurveyJSON();
@ -64,7 +64,7 @@ $survey->saveSurveyJSON();
$session->user( { userId =>3 } ); $session->user( { userId =>3 } );
$survey->getResponseId( { noCookie => 1 }); # triggers loadBothJSON() $survey->getResponseId( { noCookie => 1 }); # triggers loadBothJSON()
#for my $address (@{ $survey->response->surveyOrder }) { #for my $address (@{ $survey->responseJSON->surveyOrder }) {
# diag (Dumper $address); # diag (Dumper $address);
#} #}
@ -73,7 +73,7 @@ $survey->getResponseId( { noCookie => 1 }); # triggers loadBothJSON()
# Check a simple www_jumpTo request # Check a simple www_jumpTo request
WebGUI::Test->getPage( $survey, 'www_jumpTo', { formParams => {id => '0'} } ); WebGUI::Test->getPage( $survey, 'www_jumpTo', { formParams => {id => '0'} } );
is( $session->http->getStatus, '201', 'Page request ok' ); # why is "201 - created" status used?? is( $session->http->getStatus, '201', 'Page request ok' ); # why is "201 - created" status used??
is($survey->response->nextResponse, 0, 'S0 is the first response'); is($survey->responseJSON->nextResponse, 0, 'S0 is the first response');
tie my %expectedSurveyOrder, 'Tie::IxHash'; tie my %expectedSurveyOrder, 'Tie::IxHash';
%expectedSurveyOrder = ( %expectedSurveyOrder = (
@ -88,7 +88,7 @@ $survey->getResponseId( { noCookie => 1 }); # triggers loadBothJSON()
while (my ($id, $index) = each %expectedSurveyOrder) { while (my ($id, $index) = each %expectedSurveyOrder) {
WebGUI::Test->getPage( $survey, 'www_jumpTo', { formParams => {id => $id} } ); WebGUI::Test->getPage( $survey, 'www_jumpTo', { formParams => {id => $id} } );
$survey->loadSurveyJSON(); $survey->loadSurveyJSON();
is($survey->response->nextResponse, $index, "jumpTo($id) sets nextResponse to $index"); is($survey->responseJSON->nextResponse, $index, "jumpTo($id) sets nextResponse to $index");
} }
} }