Branching now works, but now it isn't recognizing the end of the survey properly. Will be easy fix tomorrow.

This commit is contained in:
Kaleb Murphy 2008-10-20 03:35:40 +00:00
parent a4afde5786
commit ff2ecaa8fc
2 changed files with 45 additions and 15 deletions

View file

@ -587,10 +587,6 @@ $self->session->errorHandler->error(Dumper $responses);
my @goodResponses = keys %$responses;#load everything. my @goodResponses = keys %$responses;#load everything.
if(@goodResponses == 0){##nothing to load
return $self->www_loadQuestions();
}
$self->loadBothJSON(); $self->loadBothJSON();
my $termInfo = $self->response->recordResponses($responses); my $termInfo = $self->response->recordResponses($responses);
@ -660,8 +656,13 @@ $self->session->errorHandler->error("Can take survey");
return $self->surveyEnd() if($self->response->surveyEnd()); return $self->surveyEnd() if($self->response->surveyEnd());
my $questions = $self->response->nextQuestions(); my $questions;
$self->session->errorHandler->error("Load Questions had ".@$questions." questions"); eval{
$questions = $self->response->nextQuestions();
};
$self->session->errorHandler->error($@) if($@);
$self->session->errorHandler->error("Load Questions had ".@$questions." questions") if(ref $questions eq 'ARRAY');
my $section = $self->response->nextSection(); my $section = $self->response->nextSection();

View file

@ -15,7 +15,6 @@ sub new{
$self->{log} = $log; $self->{log} = $log;
$self->{responseId} = $rId; $self->{responseId} = $rId;
my $temp = decode_json($json) if defined $json; my $temp = decode_json($json) if defined $json;
$self->{goto} = defined $temp->{goto} ? $temp->{goto} : [];
$self->{surveyOrder} = defined $temp->{surveyOrder} ? $temp->{surveyOrder} : [];#an array of question addresses, with the third member being an array of answers $self->{surveyOrder} = defined $temp->{surveyOrder} ? $temp->{surveyOrder} : [];#an array of question addresses, with the third member being an array of answers
$self->{responses} = defined $temp->{responses} ? $temp->{responses} : {}; $self->{responses} = defined $temp->{responses} ? $temp->{responses} : {};
$self->{lastResponse} = defined $temp->{lastResponse} ? $temp->{lastResponse} : -1; $self->{lastResponse} = defined $temp->{lastResponse} ? $temp->{lastResponse} : -1;
@ -118,7 +117,10 @@ sub nextSection{
my $self = shift; my $self = shift;
return $self->survey->section([$self->surveyOrder->[$self->lastResponse + 1]->[0]]); return $self->survey->section([$self->surveyOrder->[$self->lastResponse + 1]->[0]]);
} }
sub currentSection{
my $self = shift;
return $self->survey->section([$self->surveyOrder->[$self->lastResponse]->[0]]);
}
sub recordResponses{ sub recordResponses{
my $self = shift; my $self = shift;
@ -133,18 +135,27 @@ sub recordResponses{
my %fileTypes = ('File Upload',1); my %fileTypes = ('File Upload',1);
my %dateTypes = ('Date','Date Range',1); my %dateTypes = ('Date','Date Range',1);
my %hiddenTypes = ('Hidden',1); my %hiddenTypes = ('Hidden',1);
#These were just submitted from the user, so we need to see what and how they were (un)answered. #These were just submitted from the user, so we need to see what and how they were (un)answered.
my $questions = $self->nextQuestions(); my $questions = $self->nextQuestions();
my $qAnswered = 1; my $qAnswered = 1;
my $terminal = 0; my $terminal = 0;
my $terminalUrl; my $terminalUrl;
my $goto; my $goto;
my $section = $self->survey->section([$questions->[0]->{sid}]); #my $section = $self->survey->section([$questions->[0]->{sid}]);
my $section = $self->currentSection();
if($section->{terminal}){ if($section->{terminal}){
$terminal = 1; $terminal = 1;
$terminalUrl = $section->{terminalUrl}; $terminalUrl = $section->{terminalUrl};
} }
#There were no questions in the section just displayed, so increment the lastResponse by one
if(ref $questions ne 'ARRAY'){
$self->lastResponse($self->lastResponse + 1);
$self->log("Incrementing last response by one");
return [$terminal,$terminalUrl];
}
$self->log("There are questions to be submitted in this section");
for my $question(@$questions){ for my $question(@$questions){
my $aAnswered = 0; my $aAnswered = 0;
if($question->{terminal}){ if($question->{terminal}){
@ -189,8 +200,21 @@ sub recordResponses{
sub goto{ sub goto{
my $self = shift; my $self = shift;
my $goto = shift; my $goto = shift;
$self->log("In goto for $goto");
for(my $i = 0; $i <= $#{$self->surveyOrder()}; $i++){
my $section = $self->survey->section($self->surveyOrder()->[$i]);
my $question = $self->survey->question($self->surveyOrder()->[$i]);
if(ref $section eq 'HASH' and $section->{variable} eq $goto){
$self->log("setting lastResponse to section ".($i-1));
$self->lastResponse($i - 1);
last;
}
if(ref $question eq 'HASH' and $question->{variable} eq $goto){
$self->log("setting lastResponse to question ".($i-1));
$self->lastResponse($i - 1);
last;
}
}
} }
sub getPreviousAnswer{ sub getPreviousAnswer{
my $self = shift; my $self = shift;
@ -232,13 +256,15 @@ $self->log("qperpage $qPerPage");
my $questions; my $questions;
for(my $i = 1; $i <= $qPerPage; $i++){ for(my $i = 1; $i <= $qPerPage; $i++){
my $qAddy= $self->surveyOrder->[$self->lastResponse + $i]; my $qAddy = $self->surveyOrder->[$self->lastResponse + $i];
$self->log("qAddy was $$qAddy[0]-$$qAddy[1]"); $self->log("qAddy was $$qAddy[0]-$$qAddy[1]");
next if(! exists $$qAddy[1]);#skip this if it doesn't have a question (for sections with no questions)
if($$qAddy[0] != $nextSectionId){ if($$qAddy[0] != $nextSectionId){
$self->log("Next question section did not match current section");
last; last;
} }
$self->log("wtf");
my %question = %{$self->survey->question([$$qAddy[0],$$qAddy[1]])}; my %question = %{$self->survey->question([$$qAddy[0],$$qAddy[1]])};
$question{'text'} =~ s/\[\[([^\%]*?)\]\]/$self->getPreviousAnswer($1)/eg; $question{'text'} =~ s/\[\[([^\%]*?)\]\]/$self->getPreviousAnswer($1)/eg;
delete $question{answers}; delete $question{answers};
@ -252,11 +278,14 @@ $self->log("qAddy was $$qAddy[0]-$$qAddy[1]");
} }
push(@$questions,\%question); push(@$questions,\%question);
} }
$self->log("Next Questions returning with ");
return $questions return $questions
} }
sub surveyEnd{ sub surveyEnd{
my $self = shift; my $self = shift;
return 1 if($self->lastResponse > $#{$self->surveyOrder}); $self->log("LR is ".$self->lastResponse." and order is ".$#{$self->surveyOrder});
return 1 if($self->lastResponse >= $#{$self->surveyOrder});
return 0; return 0;
} }