Hah, it ah, doesnt throw errors upon modperl start and ah, the editor, ah, kind of works, and err. ah, the JSON is 99% working, with objects instantiating themselves correctly then serializing themselves as appropriate. Definitely not a working version though.

This commit is contained in:
Kaleb Murphy 2008-09-24 01:30:57 +00:00
parent 6832eacc7c
commit c4f939f306
9 changed files with 482 additions and 246 deletions

View file

@ -16,6 +16,9 @@ use JSON;
use WebGUI::International;
use WebGUI::Form::File;
use base 'WebGUI::Asset::Wobject';
use Data::Dumper;
#<tmpl if admin <tmpl_if canEditSurvey><a href="<tmpl_var editSUrvey_url>"><tmpl_var editSurvey_label></a></tmpl_if>
#-------------------------------------------------------------------
sub definition {
@ -80,21 +83,21 @@ sub definition {
tab => 'display',
fieldType => 'template',
label => "Survey edit template id",
defaultValue => 'lWcFPcZOf6f84fNNKo3LGw',
defaultValue => 'GRUNFctldUgop-qRLuo_DA',
namespace => 'Survey/Edit',
},
surveyTakeTemplateId => {
tab => 'display',
fieldType => 'template',
label => "Take survey template id",
defaultValue => 'TOCwx3VNHVmNr5eSPIAMNg',
defaultValue => 'd8jMMMRddSQ7twP4l1ZSIw',
namespace => 'Survey/Take',
},
surveyQuestionsId => {
tab => 'display',
fieldType => 'template',
label => "Questions template id",
defaultValue => 'h0pJBF7LVqCCfWYr_g6YXQ',
defaultValue => 'CxMpE_UPauZA3p8jdrOABw',
namespace => 'Survey/Take',
},
);
@ -137,221 +140,125 @@ This overloads the normal call to the super, to call the super call like normal
#-------------------------------------------------------------------
sub processPropertiesFromFormPost {
my $self = shift;
$self->SUPER::processPropertiesFromFormPost;
my $firstSection = $self->getFirstSection();
if(! $firstSection){
$self->insertSection( [$self->getId,$self->session->id->generate(),'',"First Section",1,1,"Tis is the first page",0,0,1,0,0,'',''] );
# $self->insertSection( [$self->getId,$self->session->id->generate(),"Last Section",9999,0,"This is the last page",0,0] );
$self->SUPER::processPropertiesFromFormPost;
$self->loadSurveyJSON();
if($#{$self->{_data}->{sections}} < 0){
$self->{_data}->update({ids=>['NEW'],object=>{}});
}
$self->saveSurveyJSON();
}
#-------------------------------------------------------------------
=head2 loadSurveyJSON ( )
Loads the survey collateral into memory so that the survey objects can be created
=cut
sub loadSurveyJSON{
my $self = shift;
if(defined $self->{_data}){return;}#already loaded
my $jsonHash = $self->session->db->quickScalar("select surveyJSON from Survey where assetId = ?",[$self->getId]);
use Data::Dumper;
$self->session->errorHandler->error("LOADING".Dumper $jsonHash);
my $hashRef = {};
$hashRef = decode_json($jsonHash) if defined $jsonHash;
$self->{_data} = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($hashRef);#,$self->session->errorHandler);
}
#-------------------------------------------------------------------
=head2 saveSurveyJSON ( )
Saves the survey collateral to the DB
=cut
sub saveSurveyJSON{
my $self = shift;
$self->{_data}->{log} = $self->session->errorHandler;
my $data = $self->{_data}->freeze();
use Data::Dumper;
$self->session->errorHandler->error("SAVING".Dumper $data);
$data = encode_json($data);
$self->session->db->write("update Survey set surveyJSON = ? where assetId = ?",[$data,$self->getId]);
}
#-------------------------------------------------------------------
=head2 prepareView ( )
=head2 www_editSurvey ( )
See WebGUI::Asset::prepareView() for details.
Loads the initial edit survey page. All other edit actions are JSON calls from this page.
=cut
sub www_editSurvey {
my $self = shift;
my %var;
# my $out = $self->processStyle($self->processTemplate(\%var,$self->get("surveyEditTemplateId")));
my $out = $self->processTemplate(\%var,$self->get("surveyEditTemplateId"));
return $out;
}
#-------------------------------------------------------------------
sub www_submitAnswerEdit{
my $self = shift;
my $ref = $self->session->form->paramsHashRef();
if($ref->{'Survey_answerId'}){
$self->session->db->write("
update Survey_answer
set answerText = ?, gotoQuestion = ?, recordedAnswer = ?, isCorrect = ?, min = ?, max = ?, step = ?, verbatim = ?, textCols = ?, textRows = ?
where Survey_answerId = ?",
[
$$ref{'answerText'},$$ref{'gotoQuestion'},$$ref{'recordedAnswer'},$$ref{'isCorrect'},$$ref{'min'},
$$ref{'max'},$$ref{'step'},$$ref{'verbatim'},$$ref{'textCols'},$$ref{'textRows'}
,$$ref{'Survey_answerId'}
]
);
}else{
my $seqNum = $self->session->db->quickScalar("select max(sequenceNumber) from Survey_answer where Survey_questionId = ?",[$ref->{'Survey_questionId'}]);
$seqNum++;
$ref->{'Survey_answerId'} = $self->session->id->generate();
$self->session->db->write("insert into Survey_answer values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
[ $self->getId,$$ref{'Survey_sectionId'},$$ref{'Survey_questionId'},$$ref{'Survey_answerId'},$seqNum,$$ref{'gotoQuestion'},$$ref{'answerText'},
$$ref{'recordedAnswer'},$$ref{'isCorrect'},$$ref{'min'},$$ref{'max'},$$ref{'step'},$$ref{'verbatim'},$$ref{'textCols'},$$ref{'textRows'} ]
);
}
return $self->www_loadSurvey($ref->{'Survey_sectionId'}."||||".$ref->{'Survey_questionId'}."||||".$$ref{'Survey_answerId'});
}
#-------------------------------------------------------------------
sub www_submitQuestionEdit{
my $self = shift;
my $ref = $self->session->form->paramsHashRef();
if($ref->{'Survey_questionId'}){
my $type = $self->session->db->quickScalar("select questionType from Survey_question where Survey_questionId = ?",[$ref->{'Survey_questionId'}]);
$self->session->db->write("
update Survey_question
set questionText = ?, allowComment = ?, randomizeAnswers = ?, questionType = ?, randomizedWords = ?, previousAnswerWords = ?, verticalDisplay = ?,
required = ?, maxAnswers = ?, questionVariable = ?, points = ?, commentCols = ?, commentRows = ?, textInButton = ?
where Survey_questionId = ?",
[
$$ref{'questionText'},$$ref{'allowComment'},$$ref{'randomizeAnswers'},$$ref{'questionType'},$$ref{'randomizedWords'},
$$ref{'previousAnswerWords'},$$ref{'verticalDisplay'},$$ref{'required'},$$ref{'maxAnswers'}, $$ref{'questionVariable'}, $$ref{'points'},
$$ref{'commentCols'},$$ref{'commentRows'},$$ref{'textInButton'},
$$ref{'Survey_questionId'}
]
);
$self->session->errorHandler->warn("textInButton was ".$$ref{'textInButton'});
if($type ne $ref->{'questionType'}){
$self->createDefaultAnswers($ref->{'Survey_sectionId'},$ref->{'Survey_questionId'},$ref->{'questionType'});
}
}else{
my $seqNum = $self->session->db->quickScalar("select max(sequenceNumber) from Survey_question where Survey_sectionId = ?",[$ref->{'Survey_sectionId'}]);
$seqNum++;
$ref->{'Survey_questionId'} = $self->session->id->generate();
$self->session->db->write("insert into Survey_question values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
[ $self->getId,$$ref{'Survey_questionId'},$$ref{'questionVariable'},$$ref{'questionText'},$seqNum,$$ref{'allowComment'},$$ref{'commentCols'},$$ref{'commentRows'},
$$ref{'randomizeAnswers'},$$ref{'questionType'}, $$ref{'Survey_sectionId'},$$ref{'randomizedWords'},
$$ref{'previousAnswerWords'},$$ref{'verticalDisplay'},$$ref{'required'},$$ref{'maxAnswers'}, $$ref{'points'}, $$ref{'textInButton'} ]
);
$self->createDefaultAnswers($ref->{'Survey_sectionId'},$ref->{'Survey_questionId'},$ref->{'questionType'});
}
return $self->www_loadSurvey($ref->{'Survey_sectionId'}."||||".$ref->{'Survey_questionId'});
}
#-------------------------------------------------------------------
sub www_submitSectionEdit{
sub www_submitEditObject{
my $self = shift;
my $p = $self->session->form->paramsHashRef();
if(!$p->{'Survey_sectionId'}){
my $seqNum = $self->session->db->quickScalar("select max(sequenceNumber) from Survey_section where assetId = ?",[$self->getId]);
my $id = $self->session->id->generate();
my $ref = @{decode_json($self->session->form->process("data"))};
$self->insertSection([$self->getId, $id, $p->{'sectionVariable'},$p->{'sectionName'},$seqNum+1,$p->{'questionsPerPage'},$p->{'sectionText'},$p->{'randomizeQuestions'},
$p->{'questionsOnSectionPage'}, $p->{'everyPageTitle'},$p->{'everyPageText'},$p->{'terminal'},$p->{'terminalURL'},$p->{'goto'}]);
$self->loadSurveyJSON();
$p->{'Survey_sectionId'} = $id;
my $message = $self->{_data}->update($ref);#each object checks the ref and then either updates or passes it to the correct child. New objects will have an index of -1.
}else{
$self->saveSurveyJSON();
$self->session->db->write("update Survey_section set sectionName = ?, questionsPerPage = ?, sectionText = ?, randomizeQuestions = ?, questionsOnSectionPage = ?,
everyPageTitle = ?, everyPageText = ?,terminal = ?, terminalURL = ?,sectionVariable = ?, goto = ?
where Survey_sectionId = ?",
[ $p->{'sectionName'}, $p->{'questionsPerPage'}, $p->{'sectionText'}, $p->{'randomizeQuestions'}, $p->{'questionsOnSectionPage'},$p->{'everyPageTitle'},
$p->{'everyPageText'}, $p->{'terminal'}, $p->{'terminalURL'}, $p->{'sectionVariable'},$p->{'goto'},
$p->{'Survey_sectionId'} ]
);
$self->session->errorHandler->warn("here");
}
use Data::Dumper;
$self->session->errorHandler->warn(Dumper $p);
return $self->www_loadSurvey($p->{'Survey_sectionId'});
return $self->www_loadSurvey({address => $ref->{ids},message=>$message});
}
#-------------------------------------------------------------------
sub www_newAnswer{
sub www_deleteObject{
my $self = shift;
my ($sid,$qid) = @{decode_json($self->session->form->process("data"))};
$self->session->errorHandler->warn("NEW ANSWER ".$sid."\t\t".$qid);
my $id = $self->session->db->quickScalar("select max(sequenceNumber) from Survey_answer where Survey_questionId = ?",[$qid]);
if(!$id){$id = 1;}else{$id++;}
my $edit;
$edit->{'type'} = 'loadAnswer';
$edit->{'params'} = {'assetId', $self->getId, 'Survey_sectionId', $sid, 'Survey_questionId',$qid, 'Survey_answerId','', 'sequenceNumber', $id, 'gotoQuestion','',
'answerText','','recordedAnswer','','isCorrect',1,'min',1,'max',10,'step',1,'verbatim',0 };
$sid .= "||||".$qid;
return $self->www_loadSurvey($sid,$edit);
my $ref = @{decode_json($self->session->form->process("data"))};
$self->loadSurveyJSON();
my $message = $self->{_data}->remove($ref);#each object checks the ref and then either updates or passes it to the correct child. New objects will have an index of -1.
$self->saveSurveyJSON();
#The last address in ideas is to a deleted object so that should not be returned.
pop(@{$ref->{ids}});
return $self->www_loadSurvey({address => $ref->{ids}, message=>$message});
}
#-------------------------------------------------------------------
sub www_newQuestion{
sub www_newObject{
my $self = shift;
my $sid = $self->session->form->process("data");
my $id = $self->session->db->quickScalar("select max(sequenceNumber) from Survey_question where Survey_sectionId = ?",[$sid]);
if(!$id){$id = 1;}else{$id++;}
my $edit;
$edit->{'type'} = 'loadQuestion';
$edit->{'params'} = {'assetId', $self->getId, 'Survey_sectionId', $sid, 'Survey_questionId','', 'questionText','', 'sequenceNumber', $id,
'allowComment',0, 'randomizeAnswers',0, 'questionType',1, 'required',0,'randomizedWords','','previousAnswerWords','','verticalDisplay',0,'maxAnswers','1',
'questionVariable','','commentCols',20,'commentRows',1, 'textInButton',0 };
return $self->www_loadSurvey($sid,$edit);
}
#-------------------------------------------------------------------
sub www_newSection{
my $self = shift;
my $last = $self->session->db->quickScalar("select max(sequenceNumber) from Survey_section where assetId = ?",[$self->getId]);
my $focus = '';
my $edit;
$edit->{'type'} = 'loadSection';
$edit->{'params'} = {'assetId', $self->getId, 'Survey_sectionId', $focus, 'sectionName', 'New Section', 'sequenceNumber', ++$last,
'questionsPerPage',1, 'sectionText', 'Text goes here', 'randomizeQuestions',0, 'questionsOnSectionPage', 0, 'everyPageTitle',1,'everyPageText',0,'terminal',0};
return $self->www_loadSurvey('',$edit);
}
#-------------------------------------------------------------------
sub www_deleteAnswer{
my $self = shift;
my $id = $self->session->form->process("data");
my $ref = $self->session->db->quickHashRef("select Survey_sectionId,Survey_questionId,sequenceNumber from Survey_answer where Survey_answerId = ?",[$id]);
$self->session->db->write("update Survey_answer set sequenceNumber = sequenceNumber -1 where Survey_questionId = ? and sequenceNumber > ?",
[$$ref{'Survey_questionId'},$$ref{'sequenceNumber'}]);
$self->session->db->write("delete from Survey_answer where Survey_answerId = ?",[$id]);
return $self->www_loadSurvey($$ref{'Survey_sectionId'}."||||".$$ref{'Survey_questionId'});
}
sub www_deleteQuestion{
my $self = shift;
my $id = $self->session->form->process("data");
my $ref = $self->session->db->quickHashRef("select Survey_sectionId,sequenceNumber from Survey_question where Survey_questionId = ?",[$id]);
$self->session->db->write("update Survey_question set sequenceNumber = sequenceNumber -1 where Survey_sectionId = ? and sequenceNumber > ?",
[$$ref{'Survey_sectionId'},$$ref{'sequenceNumber'}]);
$self->session->db->write("delete from Survey_question where Survey_questionId = ?",[$id]);
$self->session->db->write("delete from Survey_answer where Survey_questionId = ?",[$id]);
return $self->www_loadSurvey($$ref{'Survey_sectionId'});
}
sub www_deleteSection{
my $self = shift;
my $id = $self->session->form->process("data");
my $seq = $self->session->db->quickScalar("select sequenceNumber from Survey_section where Survey_sectionId = ?",[$id]);
my $ref;
$self->session->db->write("update Survey_section set sequenceNumber = sequenceNumber -1 where assetId= ? and sequenceNumber > ?",
[$self->getId(),$seq]);
$ref->{ids} = @{decode_json($self->session->form->process("data"))};
$self->session->db->write("delete from Survey_answer where Survey_sectionId = ?",[$id]);
$self->session->db->write("delete from Survey_question where Survey_sectionId = ?",[$id]);
$self->session->db->write("delete from Survey_section where Survey_sectionId = ?",[$id]);
return $self->www_loadSurvey('undefined');#faking an empty JS entry
$self->loadSurveyJSON();
my $object = $self->{_data}->createTemp($ref);
#The new temp object has an address of NEW, which means it is not a real final address.
push(@{$ref->{ids}},'NEW');
return $self->www_loadSurvey({address => $ref->{ids}, message => undef, object => $object});
}
#-------------------------------------------------------------------
sub www_dragDrop{
my $self = shift;
@ -463,60 +370,26 @@ sub www_dragDrop{
#-------------------------------------------------------------------
sub www_loadSurvey{
my $self = shift;
my $p = shift;#The id of the the object(s) in focus, can be empty.
my $edit = shift;#If edit data has already been generated and does not need to be grabbed for the focus id, usually is empty.
if(!$p){
$p = $self->session->form->process("data");
}
my $focus;#Survey object that has the focus
if($p eq "undefined"){
$focus = $self->getFirstSection();
}else{$focus = $p;}
my @dfocus = split(/\|\|\|\|/,$focus);
my ($self,$options) = @_;
my $sections = $self->getSections();
my $questions = $self->getQuestions($dfocus[0]);#pass in the section
my $answers = $self->getAnswers($dfocus[1]);#pass in the question
$self->loadSurveyJSON();
$self->session->errorHandler->error("The object isa ".Dumper $self->{_data});
my $address = $options->{address} ? defined $options : [0];
my $message = $options->{message} ? defined $options : '';
my $object = $options->{object} ? defined $options : $self->{_data}->getObject($address);
$self->session->errorHandler->error("The object isa ".Dumper $object);
$object = $object->freeze();#just want the hashref
$self->session->errorHandler->error(1);
if(! $edit){
if($#dfocus == 0){
$edit->{"type"} = "loadSection";
$edit->{"params"} = $self->getSpecificSection($dfocus[0]);
#get specific data/edit template for section
}elsif($#dfocus == 1){
$edit->{"type"} = "loadQuestion";
$edit->{"params"} = $self->getSpecificQuestion($dfocus[1]);
#get specific data/edit template for question
}elsif($#dfocus == 2){
$edit->{"type"} = "loadAnswer";
$edit->{"params"} = $self->getSpecificAnswer($dfocus[2]);
#get specific data/edit template for answer
}
}
my @data;
foreach my $s(@$sections){
push( @data, { "type","section","text",$s->{"sectionName"},"id",$s->{'Survey_sectionId'} } );
if($#$questions >= 0 and $questions->[0]->{'Survey_sectionId'} eq $s->{'Survey_sectionId'}){
foreach my $q(@$questions){
push( @data, { "type","question","text",$q->{"text"},"id",$q->{'Survey_questionId'} } );
if($#$answers >= 0 and $answers->[0]->{'Survey_questionId'} eq $q->{'Survey_questionId'}){
foreach my $a(@$answers){
push( @data, { "type","answer","text",$a->{"text"},"id",$a->{'Survey_answerId'}, "recorded", $a->{'recordedAnswer'} } );
}
}
}
}
}
@data = $self->{_data}->getDragDropList($address,\@data);
$self->session->errorHandler->error(2);
my $return = {"focus",$focus,"data",\@data,"edit",$edit};
# $self->session->errorHandler->warn(encode_json($return));
my $return = {"address",$address,"data",\@data,"object",$object};
$self->session->errorHandler->warn(encode_json($return));
$self->session->errorHandler->error(3);
return encode_json($return);
}
@ -1623,10 +1496,5 @@ sub AnswersInsert{
$self->session->db->write("insert into Survey_answer values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",$array);
}
1;
package ktest;
use Class::InsideOut qw( public readonly private register id );
public name => my %name;
sub new{register(shift)}
1;

View file

@ -0,0 +1,55 @@
package WebGUI::Asset::Wobject::Survey::AnswerJSON;
use strict;
use Data::Structure::Util qw/unbless/;
sub new{
my $class = shift;
my $parent = shift;
my $self = shift || {};
$self->{answers} = $self->{answers} || [];
$self->{text};
$self->{index};
$self->{parentIndex};
$self->{parent} = $parent;
$self->{verbatim};
$self->{textCols};
$self->{textRows};
$self->{gotoQuestion};
$self->{recordedAnswer};
$self->{isCorrect};
$self->{min};
$self->{max};
$self->{step};
$self->{value};
$self->{terminal};
$self->{terminalUrl};
bless($self,$class);
return $self;
}
sub update{
my ($self,$ref) = @_;
while(my ($key,$value) = keys %{$ref->{object}}){
$self->{$key} = $value;
}
}
sub remove{
my $self = shift;
$self->{parent} = undef;
}
sub freeze{
my $self = shift;
my %temp = %{$self};
$temp{parent} = undef;
# unbless $self;
return \%temp;
}
#address is the array of objects currently selected in the edit screen
#data is the array of hash items for displaying
sub getDragDropList{
my ($self,$data,$address,$selected) = @_;
push(@$data, { "type","answer","text",$self->{"text"}, "recorded", $self->{'recordedAnswer'} });
}
1;

View file

@ -0,0 +1,101 @@
package WebGUI::Asset::Wobject::Survey::QuestionJSON;
use strict;
use Data::Structure::Util qw/unbless/;
sub new{
my $class = shift;
my $self = shift || {};
my $parent = shift;
if(defined $self->{answers}){
foreach(@{$self->{answers}}){
$_ = WebGUI::Asset::Wobject::Survey::AnswerJSON->new($_);
}
}else{
$self->{answers} = [];
}
$self->{variableName} = $self->{variableName} || '';
$self->{text} = $self->{text} || '';
$self->{parent} = $self->{parent} || $parent;
$self->{allowComment};
$self->{commentCols};
$self->{commentRows};
$self->{randomizeAnswers};
$self->{questionType};
$self->{randomizedWords};
$self->{verticalDisplay};
$self->{required};
$self->{maxAnswers};
$self->{value};
$self->{textInButton};
$self->{terminal};
$self->{terminalUrl};
bless($self,$class);
return $self;
}
sub update{
my ($self,$ref) = @_;
#is a question
if(@{$$ref{ids}} == 1){
while (my ($key,$value) = each %{$ref->{object}}){
$self->{$key} = $value;
}
#is a new answer
}elsif($$ref{ids}->[2] eq 'NEW'){
push(@{$self->{answers}}, WebGUI::Assest::Wobject::Survey::AnswerJSON->new( $self,@{$self->{object}}) );
#is updating a answer
}else{
$self->{answers}->[$$ref{ids}->[2]]->update($ref);
}
}
sub getObject{
my ($self,$address) = @_;
return $self->{answers}->[$address->[2]];
}
sub createTemp{
my ($self,$ref) = @_;
return WebGUI::Asset::Wobject::Survey::AnswerJSON->new($self);
}
sub remove{
my ($self,$ref) = @_;
if(@$$ref{ids} <= 1){
$self->{parent} = undef;
for my $answer(@{$self->{answers}}){
$answer->remove();
}
}
elsif(@$$ref{ids} == 2){
$self->{answers}->[$$ref{ids}->[2]]->remove();
splice(@{$self->{answers}},$$ref->{ids}->[2],1);
}
}
sub freeze{
my $self = shift;
$self->{parent} = undef;
my %temp = %{$self};
$temp{answers} = [];
foreach(@{$self->{answers}}){
push(@{$temp{answers}},$_->freeze());
}
return \%temp;
}
#address is the array of objects currently selected in the edit screen
#data is the array of hash items for displaying
sub getDragDropList{
my ($self,$data,$address,$selected) = @_;
push(@$data, { "type","question","text",$self->{text} });
if($selected){
for (@{$self->{answers}}){
$_->getDragDropList($data, $address);
}
}
}
1;

View file

@ -0,0 +1,126 @@
package WebGUI::Asset::Wobject::Survey::SectionJSON;
use strict;
use Data::Structure::Util qw/unbless/;
use WebGUI::Asset::Wobject::Survey::QuestionJSON;
sub new{
my $class = shift;
my $self = shift || {};
my $parent = shift;
if(defined $self->{questions}){
foreach(@{$self->{questions}}){
$_ = WebGUI::Asset::Wobject::Survey::QuestionJSON->new($_);
}
}else{
$self->{questions} = [];
}
$self->{text} = $self->{text} || '';
$self->{title} = $self->{title} || '';
$self->{parent} = $parent;
$self->{questionsPerPage} = $self->{questionsPerPage} || 5;
$self->{questionsOnSectionPage} = $self->{questionsOnSectionPage} || 1;
$self->{randomizeQuestions} = $self->{randomizeQuestions} || 0;
$self->{everyPageTitle} = $self->{everyPageTitle} || 1;
$self->{everyPageText} = $self->{everyPageText} || 1;
$self->{terminal} = $self->{terminal} || 0;
$self->{terminalUrl};
$self->{goto};
$self->{timeLimit};
bless($self,$class);
return $self;
}
sub getObject{
my ($self,$address) = @_;
if(@$address == 1){
return $self->{questions}->[$address->[1]];
}else{
return $self->{questions}->[$address->[1]]->getObject($address);
}
}
sub newQuestion{
my $self = shift;
push(@{$self->{questions}}, WebGUI::Assest::Wobject::Survey::QuestionJSON->new( $self,@{$self->{questions}}) );
}
sub remove{
my ($self,$ref) = @_;
$self->{questions}->[$$ref{ids}->[1]]->remove($ref);
if(@$$ref{ids} == 0){
for my $question(@{$self->{questions}}){
$question->remove($ref);
}
$self->{parent} = undef;
}
if(@$$ref{ids} == 1){
splice(@{$self->{questions}},$$ref->{ids}->[1],1);
}
}
sub update{
my ($self,$ref) = @_;
#is a section
if(@{$$ref{ids}} == 0){
while(my ($key,$value) = keys %{$ref->{object}}){
$self->{$key} = $value;
}
#is a new question
}elsif($$ref{ids}->[1] eq 'NEW'){
push(@{$self->{questions}}, WebGUI::Assest::Wobject::Survey::QuestionJSON->new( $self,@{$self->{object}}) );
#is updating a question or answer
}else{
$self->{questions}->[$$ref{ids}->[1]]->update($ref);
}
}
sub loadQuestion{
my ($self,$questionHash) = @_;
push(@{$self->{questions}}, WebGUI::Assest::Wobject::Survey::QuestionJSON->new( $self,@{$self->{questions}},$questionHash) );
}
sub deleteQuestion{
my $self = shift;
my $index = shift;
splice(@{$self->{questions}},$index,1) if defined $index;
}
#address is the array of objects currently selected in the edit screen
#data is the array of hash items for displaying
sub getDragDropList{
my ($self,$data,$address,$selected) = @_;
push(@$data,{ "type","section","text",$self->{"title"} });
if($selected){
for(my $i=0; $i<=$#{$self->{questions}}; $i++){
$self->{questions}->[$i]->getDragDropList($data, $address, $i == $address->[1]);
}
}
}
sub getQuestion{
my $self = shift;
my $index = shift;
return $self->{questions}->[$index] if defined $index;
}
sub freeze{
my $self = shift;
$self->{parent} = undef;
my %temp = %{$self};
$temp{questions} = [];
foreach(@{$self->{questions}}){
push(@{$temp{questions}}, $_->freeze());
}
return \%temp;
}
sub createTemp{
my ($self,$ref) = @_;
if(@{$$ref{ids}} > 1){
return $self->{questions}->[$$ref{ids}->[1]]->createTemp($ref);
}else{
return WebGUI::Asset::Wobject::Survey::QuestionJSON->new($self);
}
}
1;

View file

@ -0,0 +1,79 @@
package WebGUI::Asset::Wobject::Survey::SurveyJSON;
use strict;
use Data::Structure::Util qw/unbless/;
use WebGUI::Asset::Wobject::Survey::SectionJSON;
sub new{
my $class = shift;
my $self = shift || {};
my $log = shift;
if(defined $self->{sections}){
foreach(@{$self->{sections}}){
$_ = WebGUI::Asset::Wobject::Survey::SectionJSON->new($_);
}
}else{
$self->{sections} = [];
}
$self->{log} = $log;
bless($self,$class);
return $self;
}
#address is the array of objects currently selected in the edit screen
#data is the array of hash items for displaying
sub getDragDropList{
my ($self,$address,$data) = @_;
for(my $i=0; $i<=$#{$self->{sections}}; $i++){
$self->{sections}->[$i]->getDragDropList($data, $address, $i == $address->[0]);
}
}
sub getObject{
my ($self,$address) = @_;
if(@$address == 1){
return $self->{sections}->[$address->[0]];
}else{
return $self->{sections}->[$address->[0]]->getObject($address);
}
}
sub update{
my ($self,$ref) = @_;
if(ref $$ref{ids} eq 'ARRAY' and $$ref{ids}->[0] ne 'NEW'){
$self->{sections}->[$$ref{ids}->[0]]->update($ref);
}else{
push(@{$self->{sections}}, WebGUI::Asset::Wobject::Survey::SectionJSON->new($self,$ref->{object}));
}
}
#determine what to add and add it.
# ref should contain all the information for the new
sub remove{
my ($self,$ref) = @_;
$self->{sections}->[$$ref{ids}->[0]]->remove($ref);
if(@$$ref{ids} == 0){
splice(@{$self->{sections}},$$ref->{ids}->[0],1);
}
}
sub createTemp{
my ($self,$ref) = @_;#ref{ids} contains the parent of the temp object which should be created and returned.
if(ref $$ref{ids} eq 'ARRAY'){
return $self->{sections}->[$$ref{ids}->[0]]->createTemp($ref);
}else{
return WebGUI::Asset::Wobject::Survey::SectionJSON->new($self);
}
}
sub freeze{
my $self = shift;
my %temp = %{$self};
$temp{sections} = [];
$temp{log} = undef;
foreach (@{$self->{sections}}){
push(@{$temp{sections}},$_->freeze($self->{log}));
}
return \%temp;
}
1;

View file

@ -7,7 +7,7 @@ Survey.Comm = new function(){
var request = function(sUrl,callback,postData){
if(callMade == 1){
alert("Waiting on previous call");
alert("Waiting on previous request");
}else{
callMade = 1;
YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
@ -16,6 +16,7 @@ Survey.Comm = new function(){
this.callback = {
success:function(o){
callMade = 0;
console.log(o.responseText);
Survey.Data.loadData(YAHOO.lang.JSON.parse(o.responseText));
},
failure: function(o){

View file

@ -531,4 +531,4 @@ YAHOO.extend(YAHOO.draglist.DDList, YAHOO.util.DDProxy, {
Event.onDOMReady(YAHOO.draglist.DDApp.init, YAHOO.draglist.DDApp, true);
})();
})();

View file

@ -4,7 +4,7 @@ if (typeof Survey == "undefined") {
Survey.Data = new function(){
var lastDataSet = {};
var focus;
var address;
this.dragDrop = function(did){
@ -45,7 +45,7 @@ Survey.Data = new function(){
this.loadData = function(d){
focus = d.focus;//What is the current highlighted item.
address = d.address;//What is the current highlighted item.
var lastType = '';//What was the last type created.
var lastId = {'section': '', 'question': '', 'answer': ''};//what is the last id of each type placed, so we know a child's parent.
var buttons = {'question':0,'answer':0,'section':0}; //array of bools on if buttons put down
@ -81,13 +81,13 @@ Survey.Data = new function(){
acount = 1;
qcount = 1;
}
else if(d.data[x].type == 'section' && lastType == 'section' && lastId['section'] == focus){
else if(d.data[x].type == 'section' && lastType == 'section' && lastId['section'] == address){
this.addQuestionButton(lastId['section']);
buttons['question'] = 1;
acount = 1;
qcount = 1;
}
else if(d.data[x].type != 'answer' && lastType == 'question' && lastId['section'] + '||||'+ lastId['question'] == focus){
else if(d.data[x].type != 'answer' && lastType == 'question' && lastId['section'] + '||||'+ lastId['question'] == address){
this.addAnswerButton(lastId['section'],lastId['question']);
buttons['answer']=1;
acount = 1;
@ -95,12 +95,12 @@ Survey.Data = new function(){
}
var node = document.createElement('li');
if(focus != undefined && focus.indexOf(d.data[x].id) > -1){
if(address != undefined && address.indexOf(d.data[x].id) > -1){
node.className = "s"+d.data[x].type;
}else{
node.className = d.data[x].type;
}
if(d.data[x].text == undefined){//== 'null'){
if(d.data[x].text == undefined){
d.data[x].text = '<empty>';
}
var id = '';
@ -108,17 +108,20 @@ Survey.Data = new function(){
var pre;
if(d.data[x].type == 'section'){
pre = 'S'+ scount++ +':';
id = d.data[x].id;
//id = d.data[x].id;
id = scount-1;
}
else if(d.data[x].type == 'question'){
pre = 'Q'+ qcount++ +':';
id = lastId['section'] + delim + d.data[x].id;
pre = 'Q'+ qcount++ + ':';
id = scount-1 +"-"+qcount-1;
//id = lastId['section'] + delim + d.data[x].id;
}
else if(d.data[x].type == 'answer'){
if(d.data[x].recordedAnswers != null){
}
pre = 'A'+ acount++ +':';
id = lastId['section'] + delim + lastId['question'] + delim + d.data[x].id;
id = scount-1 +"-"+qcount-1+"-"+acount-1;
//id = lastId['section'] + delim + lastId['question'] + delim + d.data[x].id;
}
node.innerHTML = pre + ' ' + d.data[x].text;
node.id = id;
@ -127,7 +130,8 @@ Survey.Data = new function(){
YAHOO.util.Event.addListener(id, "click", this.clicked);
lastType = d.data[x].type;
lastId[d.data[x].type] = d.data[x].id;
//lastId[d.data[x].type] = d.data[x].id;
lastId[d.data[x].type] = id;
}
if(lastType == 'answer' && ! buttons['answer']){
this.addAnswerButton(lastId['section'],lastId['question']);

View file

@ -9,6 +9,8 @@ Survey.Comm = new function(){
}
this.callback = {
success:function(o){
alert('here');
this.logger('hi');
Survey.Data.loadData(YAHOO.lang.JSON.parse(o.responseText));
},
failure: function(o){