Finishing up editor

This commit is contained in:
Kaleb Murphy 2008-09-26 02:06:21 +00:00
parent 38c7ee33c4
commit 5174824b5f
7 changed files with 196 additions and 220 deletions

View file

@ -100,6 +100,13 @@ sub definition {
defaultValue => 'CxMpE_UPauZA3p8jdrOABw',
namespace => 'Survey/Take',
},
sectionEditTemplateId => {
tab => 'display',
fieldType => 'template',
label => "Section Edit Tempalte",
defaultValue => '1oBRscNIcFOI-pETrCOspA',
namespace => 'Survey/Edit',
},
);
push(@{$definition}, {
@ -144,7 +151,10 @@ sub processPropertiesFromFormPost {
$self->loadSurveyJSON();
if($#{$self->{_data}->{sections}} < 0){
$self->{_data}->update({ids=>['NEW'],object=>{}});
$self->session->errorHandler->error("In Processing from Post\n");
$self->{_data}->update(['NEW'],undef,$self->session->errorHandler);
$self->session->errorHandler->error("Processing from creation\n".Dumper $self->{_data});
}
$self->saveSurveyJSON();
}
@ -163,12 +173,11 @@ sub loadSurveyJSON{
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\n".Dumper $jsonHash."\n\n");
#$self->session->errorHandler->error("LOADING\n".Dumper $jsonHash."\n\n");
my $hashRef = {};
$hashRef = decode_json($jsonHash) if defined $jsonHash;
$self->{_data} = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($hashRef);#,$self->session->errorHandler);
$self->{_data} = WebGUI::Asset::Wobject::Survey::SurveyJSON->new($hashRef,$self->session->errorHandler);
}
@ -184,26 +193,10 @@ sub saveSurveyJSON{
my $self = shift;
$self->{_data}->{log} = $self->session->errorHandler;
my $data;
$self->session->errorHandler->error("Calling Freeze");
$self->session->errorHandler->error("data type = ".ref $self->{_data});
eval{
$data = $self->{_data}->freeze();
};
$self->session->errorHandler->error("Freeze error".$@);
$self->session->errorHandler->error("data type = ".ref $data);
use Data::Dumper;
$self->session->errorHandler->error("Log defined:".defined $data->{log});
#$self->session->errorHandler->error(Dumper $data);
eval{
$self->session->errorHandler->error(join(',',keys %{$data}));
$data = encode_json($data);
};
if($@){
$self->session->errorHandler->error("Could not encode Survey object".$@);
}
$self->session->errorHandler->error("----------------SAving THIS DATA".Dumper $data);
$data = encode_json($data);
$self->session->db->write("update Survey set surveyJSON = ? where assetId = ?",[$data,$self->getId]);
}
@ -225,39 +218,51 @@ sub www_editSurvey {
return $out;
}
#-------------------------------------------------------------------
sub www_submitEditObject{
sub www_submitObjectEdit{
my $self = shift;
$self->session->errorHandler->error("Submit Edit Object");
my $ref = @{decode_json($self->session->form->process("data"))};
# my $ref = @{decode_json($self->session->form->process("data"))};
my $responses = $self->session->form->paramsHashRef();
my @address = split/-/,$responses->{id};
#$self->session->errorHandler->error("Submit Edit Object".Dumper $responses);
$self->loadSurveyJSON();
if($responses->{delete}){
$self->session->errorHandler->error("Deleting ".join(',',@address));
return $self->deleteObject(\@address);
}
$self->session->errorHandler->error("Updating ".join(',',@address)."Which has $$responses{terminalUrl} as the tu");
#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->{_data}->update(\@address,$responses,$self->session->errorHandler);
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.
# $self->saveSurveyJSON();
# return $self->www_loadSurvey({address => $ref->{ids},message=>$message});
$self->saveSurveyJSON();
return $self->www_loadSurvey({address => $ref->{ids},message=>$message});
return $self->www_loadSurvey();
}
#-------------------------------------------------------------------
sub www_deleteObject{
my $self = shift;
my $ref = @{decode_json($self->session->form->process("data"))};
sub deleteObject{
my ($self,$address) = @_;
$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.
my $message = $self->{_data}->remove($address);#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}});
pop(@{$address});
return $self->www_loadSurvey({address => $ref->{ids}, message=>$message});
return $self->www_loadSurvey({address => $address, message=>$message});
}
#-------------------------------------------------------------------
sub www_newObject{
my $self = shift;
my $ref;
@ -388,16 +393,24 @@ sub www_dragDrop{
sub www_loadSurvey{
my ($self,$options) = @_;
$self->session->errorHandler->error("Entering loadSurvey");
$self->loadSurveyJSON();
$self->session->errorHandler->error("The object isa\n".Dumper $self->{_data});
$self->session->errorHandler->error("Loaded JSON");
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\n".Dumper $object);
$object = $object->freeze();#just want the hashref
my $var = $options->{var} ? defined $options : $self->{_data}->getEditVars($address);
$self->session->errorHandler->error("Loaded beginning params");
my $editHtml;
if($var->{type} eq 'section'){
$var->{id} = join('-',@$address);
$var->{displayed_id} = $address->[$#$address];
if($var->{displayed_id} ne 'NEW'){$var->{displayed_id}++;}
$self->session->errorHandler->error(1);
$editHtml = $self->processTemplate($var,$self->get("sectionEditTemplateId"));
}
$self->session->errorHandler->error(Dumper $var);
my @data;
my %buttons;
@ -478,17 +491,17 @@ $self->session->errorHandler->error(1);
$buttons{'section'} = "$lastId{section}";
}
}
#my $out = $self->processTemplate(\%var,undef,$self->{_viewTemplate});
$self->session->errorHandler->error($html);
#$self->session->errorHandler->error($html);
#address is the address of the focused object
#buttons are the data to create the Add buttons
#object is the data to create the edited object
#edithtml is the html edit the object
#ddhtml is the html to create the draggable html divs
#ids is a list of all ids passed in which are draggable (for adding events)
my $return = {"address",$address,"buttons",\%buttons,"object",$object,"ddhtml",$html,"ids",\@ids};
#type is the object type
my $return = {"address",$address,"buttons",\%buttons,"edithtml",$editHtml,"ddhtml",$html,"ids",\@ids,"type",$var->{type}};
$self->session->errorHandler->error(encode_json($return));
$self->session->errorHandler->error(3);
$self->session->errorHandler->error("Returning from loadSurvey");
return encode_json($return);
}

View file

@ -6,25 +6,30 @@ use Data::Structure::Util qw/unbless/;
sub new{
my $class = shift;
my $self = shift || {};
my $parent = shift;
my $posts = 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};
$self->{text} = $self->{text} || '';
$self->{verbatim} = $self->{verbatim} || '';
$self->{textCols} = $self->{textCols} || 10;
$self->{textRows} = $self->{textRows} || 5;
$self->{gotoQuestion} = $self->{gotoQuestion} || '';
$self->{recordedAnswer} = $self->{recordedAnswer} || '';
$self->{isCorrect} = $self->{isCorrect} || 1;
$self->{min} = $self->{min} || 1;
$self->{max} = $self->{max} || 10;
$self->{step} = $self->{step} || 1;
$self->{value} = $self->{value} || 1;
$self->{terminal} = $self->{terminal} || 0;
$self->{terminalUrl} = $self->{terminalUrl} || '';
$self->{type} = 'answer';
if(defined $posts and ref $posts eq 'HASH'){
while(my ($key,$value) = each %$posts){
if(defined $self->{$key}){
$self->{$key} = $value;
}
}
}
bless($self,$class);
return $self;
}
@ -37,12 +42,11 @@ sub update{
}
sub remove{
my $self = shift;
$self->{parent} = undef;
}
sub freeze{
my $self = shift;
my %temp = %{$self};
$temp{parent} = undef;
# $temp{parent} = undef;
# unbless $self;
return \%temp;
}

View file

@ -6,7 +6,7 @@ use Data::Structure::Util qw/unbless/;
sub new{
my $class = shift;
my $self = shift || {};
my $parent = shift;
my $posts = shift;
if(defined $self->{answers}){
foreach(@{$self->{answers}}){
@ -18,21 +18,29 @@ sub new{
$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};
$self->{allowComment} = $self->{allowComment} || 0;
$self->{commentCols} = $self->{commentCols} || 10;
$self->{commentRows} = $self->{commentRows} || 5;
$self->{randomizeAnswers} = $self->{randomizeAnswers} || 0;
$self->{questionType} = $self->{questionType} || '';
$self->{randomizedWords} = $self->{randomizedWords} || '';
$self->{verticalDisplay} = $self->{verticalDisplay} || 0;
$self->{required} = $self->{required} || 0;
$self->{maxAnswers} = $self->{maxAnswers} || 1;
$self->{value} = $self->{value} || 1;
$self->{textInButton} = $self->{textInButton} || 0;
$self->{terminal} = $self->{terminal} || 0;
$self->{terminalUrl} = $self->{terminalUrl} || '';
$self->{type} = 'question';
if(defined $posts and ref $posts eq 'HASH'){
while(my ($key,$value) = each %$posts){
if(defined $self->{$key}){
$self->{$key} = $value;
}
}
}
bless($self,$class);
return $self;
}
@ -46,7 +54,7 @@ sub update{
}
#is a new answer
}elsif($$ref{ids}->[2] eq 'NEW'){
push(@{$self->{answers}}, WebGUI::Assest::Wobject::Survey::AnswerJSON->new( @{$self->{object}}) );
push(@{$self->{answers}}, WebGUI::Assest::Wobject::Survey::AnswerJSON->new( {},$ref) );
#is updating a answer
}else{
$self->{answers}->[$$ref{ids}->[2]]->update($ref);
@ -63,17 +71,8 @@ sub createTemp{
}
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);
}
my ($self,$address) = @_;
splice(@{$self->{answers}},$$address[2],1);
}

View file

@ -4,11 +4,13 @@ use strict;
use Data::Structure::Util qw/unbless/;
use WebGUI::Asset::Wobject::Survey::QuestionJSON;
use Data::Dumper;
sub new{
my $class = shift;
my $self = shift || {};
my $parent = shift;
my $postInfo = shift;
my $log = shift;
if(defined $self->{questions}){
foreach(@{$self->{questions}}){
$_ = WebGUI::Asset::Wobject::Survey::QuestionJSON->new($_);
@ -16,65 +18,88 @@ sub new{
}else{
$self->{questions} = [];
}
$self->{log} = $log if defined $log;
$self->{text} = $self->{text} || '';
$self->{title} = $self->{title} || 'New Section';
$self->{parent} = $parent;
$self->{variable} = $self->{variable} || '';
$self->{questionsPerPage} = $self->{questionsPerPage} || 5;
$self->{questionsOnSectionPage} = $self->{questionsOnSectionPage} || 1;
$self->{questionsOnSectionPage} = defined $self->{questionsOnSectionPage} ? $self->{questionsOnSectionPage} : 1;
$self->{randomizeQuestions} = $self->{randomizeQuestions} || 0;
$self->{everyPageTitle} = $self->{everyPageTitle} || 1;
$self->{everyPageText} = $self->{everyPageText} || 1;
$self->{everyPageTitle} = defined $self->{everyPageTitle} ? $self->{everyPageTitle} : 1;
$self->{everyPageText} = defined $self->{everyPageText} ? $self->{everyPageText} : 1;
$self->{terminal} = $self->{terminal} || 0;
$self->{terminalUrl};
$self->{goto};
$self->{timeLimit};
$self->{terminalUrl} = $self->{terminalUrl} || '';
$self->{goto} = $self->{goto} || '';
$self->{timeLimit} = $self->{timeLimit} || 0;
$self->{type} = 'section';
if(defined $postInfo and ref $postInfo eq 'HASH'){
while(my ($key,$value) = each %$postInfo){
if(defined $self->{$key}){
$self->{$key} = $value;
}
}
}
bless($self,$class);
return $self;
}
sub getObject{
my ($self,$address) = @_;
if(@$address == 1){
if(@$address == 2){
return $self->{questions}->[$address->[1]];
}else{
return $self->{questions}->[$address->[1]]->getObject($address);
}
}
sub getEditVars{
my ($self,$address) = @_;
if(@$address > 1){
return $self->{questions}->[$address->[1]]->getEditVars($address);
}
#Fill in a template var hash and return it
my %var;
while (my ($key,$value) = each %{$self}){
if($key ne 'questions' and $key ne 'questionsPerPage' and $key ne 'log'){
$var{$key} = $value;
}
}
for(1 .. 10){
if($_ == $self->{questionsPerPage}){
push(@{$var{questionsPerPage}},{'index',$_,'selected',1});
}else{
push(@{$var{questionsPerPage}},{'index',$_,'selected',0});
}
}
return \%var;
}
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);
my ($self,$address) = @_;
if(@$address == 2){
splice(@{$self->{questions}},$$address[1],1);
}else{
$self->{questions}->[$$address[1]]->remove($address);
}
}
sub update{
my ($self,$ref) = @_;
#is a section
if(@{$$ref{ids}} == 0){
while(my ($key,$value) = keys %{$ref->{object}}){
$self->{$key} = $value;
my ($self,$address,$ref,$log) = @_;
if(@{$address} == 1){
while(my ($key,$value) = each %{$ref}){
$self->{$key} = $value if defined $self->{$key};
}
#is a new question
}elsif($$ref{ids}->[1] eq 'NEW'){
push(@{$self->{questions}}, WebGUI::Assest::Wobject::Survey::QuestionJSON->new( $self,@{$self->{object}}) );
push(@{$self->{questions}}, WebGUI::Assest::Wobject::Survey::QuestionJSON->new({},$ref) );
#is updating a question or answer
}else{
$self->{questions}->[$$ref{ids}->[1]]->update($ref);
$self->{questions}->[$$ref{ids}->[1]]->update($address,$ref);
}
}
@ -109,11 +134,13 @@ sub freeze{
my $self = shift;
my %temp = %{$self};
$temp{parent} = undef;
$temp{questions} = [];
foreach(@{$self->{questions}}){
push(@{$temp{questions}}, $_->freeze());
}
$temp{log} = undef;
delete $temp{log};
return \%temp;
}

View file

@ -11,7 +11,7 @@ sub new{
my $log = shift;
if(defined $self->{sections}){
foreach(@{$self->{sections}}){
$_ = WebGUI::Asset::Wobject::Survey::SectionJSON->new($_);
$_ = WebGUI::Asset::Wobject::Survey::SectionJSON->new($_,undef,$log);
}
}else{
$self->{sections} = [];
@ -34,9 +34,7 @@ sub getDragDropList{
my $data = shift;
$self->{log} = shift;
for(my $i=0; $i<=$#{$self->{sections}}; $i++){
$self->log("Survey passing ".Dumper $data);#,$address,$i, $$address[0]");
$self->{sections}->[$i]->getDragDropList($data, $address, $i == $address->[0]);
$self->log("Survey passing ".Dumper $data);#,$address,$i, $$address[0]");
}
}
@ -49,22 +47,30 @@ sub getObject{
}
}
sub getEditVars{
my ($self,$address) = @_;
return $self->{sections}->[$address->[0]]->getEditVars($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);
my ($self,$address,$ref,$log) = @_;
if(ref $address eq 'ARRAY' and $$address[0] ne 'NEW'){
$log->error('Address an array and sectionid not equal to NEW') if(defined $log);
$self->{sections}->[$$ref{ids}->[0]]->update($address,$ref,$log);
}else{
push(@{$self->{sections}}, WebGUI::Asset::Wobject::Survey::SectionJSON->new($ref->{object}));
$log->error('Either address not an array or sectionid equal to NEW') if(defined $log);
push(@{$self->{sections}}, WebGUI::Asset::Wobject::Survey::SectionJSON->new({},$ref));
}
}
#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);
my ($self,$address) = @_;
if(@$address == 1){
splice(@{$self->{sections}},$$address[0],1) if($$address[0] != 0);#can't delete the first section
}else{
$self->{sections}->[$address->[0]]->remove($address);
}
}
@ -81,11 +87,16 @@ sub freeze{
my ($self) = @_;
my %temp = %{$self};
$temp{sections} = [];
delete $temp{log};
foreach (@{$self->{sections}}){
push(@{$temp{sections}},$_->freeze());
}
$self->{log}->error(Dumper %temp);
foreach my $key (keys %temp){
if($key ne 'log'){
$self->{log}->error("$key $temp{$key}");
}
}
$temp{log} = undef;
delete $temp{log};
return \%temp;
}
1;