From 046dae4f310e6b0c1d9da287a2f36b1bd9f87ae4 Mon Sep 17 00:00:00 2001 From: Kaleb Murphy Date: Thu, 26 Mar 2009 21:02:30 +0000 Subject: [PATCH] Moved multiple choice questions to the data base, and they can now be edited globally from the Survey edito --- docs/changelog/7.x.x.txt | 2 +- docs/upgrades/upgrade_7.7.0-7.7.1.pl | 18 +++ lib/WebGUI/Asset/Wobject/Survey.pm | 46 ++++-- lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm | 147 ++++++++++-------- .../wobject/Survey/editsurvey/object.js | 21 ++- 5 files changed, 157 insertions(+), 77 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 54584bbc3..35004ccf6 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -7,7 +7,7 @@ - rfe #9099: Thingy field-copy function (SDH Consulting Group) - Keywords are now comma separated rather than space separated. - added Keywords form control with autocomplete - + - Survey: Moved multiple choice questions to the data base, and they can now be edited globally from the Survey editor 7.7.0 - fixed #9913: New Content Side Bar missing in Asset window - fixed: New Mail macro never returns any messages diff --git a/docs/upgrades/upgrade_7.7.0-7.7.1.pl b/docs/upgrades/upgrade_7.7.0-7.7.1.pl index 7b0ae84ed..0f7fbb5d9 100644 --- a/docs/upgrades/upgrade_7.7.0-7.7.1.pl +++ b/docs/upgrades/upgrade_7.7.0-7.7.1.pl @@ -35,6 +35,9 @@ adSkuInstall($session); addWelcomeMessageTemplateToSettings( $session ); addStatisticsCacheTimeoutToMatrix( $session ); +#add Survey table +addSurveyQuestionTypes($session); + # image mods addImageAnnotation($session); @@ -43,6 +46,21 @@ addRssLimit($session); finish($session); # this line required +sub addSurveyQuestionTypes{ + my $session = shift; + print "\tAdding new survey table Survey_questionTypes \n" unless $quiet; + $session->db->write(" + CREATE TABLE `Survey_questionTypes` ( + `questionType` varchar(56) NOT NULL, + `answers` text NOT NULL, + PRIMARY KEY (`questionType`)) + "); + $session->db->write(" + INSERT INTO `Survey_questionTypes` VALUES ('Scale',''),('Gender','Male,Female'),('Education','Elementary or some high school,High school/GED,Some college/vocational school,College graduate,Some graduate work,Masters degree,Doctorate (of any type),Other degree (verbatim)'),('Importance','Not at all important,,,,,,,,,,Extremely important'),('Yes/No','Yes,No'),('Confidence','Not at all confident,,,,,,,,,,Extremely confident'),('Effectiveness','Not at all effective,,,,,,,,,,Extremely effective'),('Oppose/Support','Strongly oppose,,,,,,Strongly support'),('Certainty','Not at all certain,,,,,,,,,,Extremely certain'),('True/False','True,False'),('Concern','Not at all concerned,,,,,,,,,,Extremely concerned'),('Ideology','Strongly liberal,Liberal,Somewhat liberal,Middle of the road,Slightly conservative,Conservative,Strongly conservative'),('Security','Not at all secure,,,,,,,,,,Extremely secure'),('Risk','No risk,,,,,,,,,,Extreme risk'),('Agree/Disagree','Strongly disagree,,,,,,Strongly agree'),('Race','American Indian,Asian,Black,Hispanic,White non-Hispanic,Something else (verbatim)'),('Threat','No threat,,,,,,,,,,Extreme threat'),('Party','Democratic party,Republican party (or GOP),Independent party,Other party (verbatim)'),('Likelihood','Not at all likely,,,,,,,,,,Extremely likely'),('Multiple Choice',''),('Satisfaction','Not at all satisfied,,,,,,,,,,Extremely satisfied') + "); + print "Done.\n" unless $quiet; +} + sub addWelcomeMessageTemplateToSettings { my $session = shift; print "\tAdding welcome message template to settings \n" unless $quiet; diff --git a/lib/WebGUI/Asset/Wobject/Survey.pm b/lib/WebGUI/Asset/Wobject/Survey.pm index 0afea44a3..234a6ac73 100644 --- a/lib/WebGUI/Asset/Wobject/Survey.pm +++ b/lib/WebGUI/Asset/Wobject/Survey.pm @@ -195,6 +195,10 @@ sub definition { }, ); + #my $defaultMC = $session-> + + #%properties = (); + push @{$definition}, { assetName => $i18n->get('assetName'), icon => 'survey.gif', @@ -412,6 +416,10 @@ sub www_submitObjectEdit { } elsif ( $params->{copy} ) { return $self->copyObject( \@address ); + }elsif( $params->{removetype} ){ + return $self->removeType(\@address); + }elsif( $params->{addtype} ){ + return $self->addType($params->{addtype},\@address); } # Update the addressed object @@ -493,6 +501,27 @@ sub www_jumpTo { #------------------------------------------------------------------- +sub removeType{ + my $self = shift; + my $address = shift; + $self->surveyJSON->removeType($address); + return $self->www_loadSurvey( { address => $address } ); + +} + +#------------------------------------------------------------------- + +sub addType{ + my $self = shift; + my $name = shift; + my $address = shift; + $self->surveyJSON->addType($name,$address); + $self->persistSurveyJSON(); + return $self->www_loadSurvey( { address => $address } ); +} + +#------------------------------------------------------------------- + =head2 copyObject ( ) Takes the address of a survey object and creates a copy. The copy is placed at the end of this object's parent's list. @@ -1208,13 +1237,13 @@ Sends the processed template and questions structure to the client sub prepareShowSurveyTemplate { my ( $self, $section, $questions ) = @_; - my %multipleChoice = ( - 'Multiple Choice', 1, 'Gender', 1, 'Yes/No', 1, 'True/False', 1, 'Ideology', 1, - 'Race', 1, 'Party', 1, 'Education', 1, 'Scale', 1, 'Agree/Disagree', 1, - 'Oppose/Support', 1, 'Importance', 1, 'Likelihood', 1, 'Certainty', 1, 'Satisfaction', 1, - 'Confidence', 1, 'Effectiveness', 1, 'Concern', 1, 'Risk', 1, 'Threat', 1, - 'Security', 1 - ); +# my %multipleChoice = ( +# 'Multiple Choice', 1, 'Gender', 1, 'Yes/No', 1, 'True/False', 1, 'Ideology', 1, +# 'Race', 1, 'Party', 1, 'Education', 1, 'Scale', 1, 'Agree/Disagree', 1, +# 'Oppose/Support', 1, 'Importance', 1, 'Likelihood', 1, 'Certainty', 1, 'Satisfaction', 1, +# 'Confidence', 1, 'Effectiveness', 1, 'Concern', 1, 'Risk', 1, 'Threat', 1, +# 'Security', 1 +# ); my %textArea = ( 'TextArea', 1 ); my %text = ( 'Text', 1, 'Email', 1, 'Phone Number', 1, 'Text Date', 1, 'Currency', 1 ); my %slider = ( 'Slider', 1, 'Dual Slider - Range', 1, 'Multi Slider - Allocate', 1 ); @@ -1222,14 +1251,13 @@ sub prepareShowSurveyTemplate { my %dateShort = ( 'Year Month', 1 ); my %fileUpload = ( 'File Upload', 1 ); my %hidden = ( 'Hidden', 1 ); -use Data::Dumper; foreach my $q (@$questions) { if ( $fileUpload{ $q->{questionType} } ) { $q->{fileLoader} = 1; } elsif ( $text{ $q->{questionType} } ) { $q->{textType} = 1; } elsif ( $textArea{ $q->{questionType} } ) { $q->{textAreaType} = 1; } elsif ( $hidden{ $q->{questionType} } ) { $q->{hidden} = 1; } - elsif ( $multipleChoice{ $q->{questionType} } ) { + elsif ( $self->surveyJSON->multipleChoiceTypes->{ $q->{questionType} } ) { $q->{multipleChoice} = 1; if ( $q->{maxAnswers} > 1 ) { $q->{maxMoreOne} = 1; diff --git a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm index 40a354874..ac216260b 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm @@ -59,69 +59,9 @@ use Storable qw/dclone/; # The maximum value of questionsPerPage is currently hardcoded here my $MAX_QUESTIONS_PER_PAGE = 20; -my %MULTI_CHOICE_BUNDLES = ( - 'Agree/Disagree' => [ 'Strongly disagree', (q{}) x 5, 'Strongly agree' ], - Certainty => [ 'Not at all certain', (q{}) x 9, 'Extremely certain' ], - Concern => [ 'Not at all concerned', (q{}) x 9, 'Extremely concerned' ], - Confidence => [ 'Not at all confident', (q{}) x 9, 'Extremely confident' ], - Education => [ - 'Elementary or some high school', - 'High school/GED', - 'Some college/vocational school', - 'College graduate', - 'Some graduate work', - 'Master\'s degree', - 'Doctorate (of any type)', - 'Other degree (verbatim)', - ], - Effectiveness => [ 'Not at all effective', (q{}) x 9, 'Extremely effective' ], - Gender => [qw( Male Female )], - Ideology => [ - 'Strongly liberal', - 'Liberal', - 'Somewhat liberal', - 'Middle of the road', - 'Slightly conservative', - 'Conservative', - 'Strongly conservative' - ], - Importance => [ 'Not at all important', (q{}) x 9, 'Extremely important' ], - Likelihood => [ 'Not at all likely', (q{}) x 9, 'Extremely likely' ], - 'Oppose/Support' => [ 'Strongly oppose', (q{}) x 5, 'Strongly support' ], - Party => - [ 'Democratic party', 'Republican party (or GOP)', 'Independent party', 'Other party (verbatim)' ], - Race => - [ 'American Indian', 'Asian', 'Black', 'Hispanic', 'White non-Hispanic', 'Something else (verbatim)' ], - Risk => [ 'No risk', (q{}) x 9, 'Extreme risk' ], - Satisfaction => [ 'Not at all satisfied', (q{}) x 9, 'Extremely satisfied' ], - Security => [ 'Not at all secure', (q{}) x 9, 'Extremely secure' ], - Threat => [ 'No threat', (q{}) x 9, 'Extreme threat' ], - 'True/False' => [qw( True False )], - 'Yes/No' => [qw( Yes No )], - Scale => [q{}], - 'Multiple Choice' => [q{}], -); - -my @SPECIAL_QUESTION_TYPES = ( - 'Dual Slider - Range', - 'Multi Slider - Allocate', - 'Slider', - 'Currency', - 'Email', - 'Phone Number', - 'Text', - 'Text Date', - 'TextArea', - 'File Upload', - 'Date', - 'Date Range', - 'Year Month', - 'Hidden', -); - -sub specialQuestionTypes { - return @SPECIAL_QUESTION_TYPES; -} +#sub specialQuestionTypes { +# return @SPECIAL_QUESTION_TYPES; +#} =head2 new ( $session, json ) @@ -154,6 +94,9 @@ sub new { bless $self, $class; + #Load question types + $self->loadTypes(); + # Initialise the survey data structure if empty.. if ( $self->totalSections == 0 ) { $self->newObject( [] ); @@ -161,6 +104,78 @@ sub new { return $self; } +=head2 loadTypes + +Loads the Multiple Choice and Special Question types + +=cut + +sub loadTypes { + my $self = shift; + @{$self->{specialQuestionTypes}} = ( + 'Dual Slider - Range', + 'Multi Slider - Allocate', + 'Slider', + 'Currency', + 'Email', + 'Phone Number', + 'Text', + 'Text Date', + 'TextArea', + 'File Upload', + 'Date', + 'Date Range', + 'Year Month', + 'Hidden', + ); + my $refs = $self->session->db->buildArrayRefOfHashRefs("SELECT questionType, answers FROM Survey_questionTypes"); + map($self->{multipleChoiceTypes}->{$_->{questionType}} = [split/,/,$_->{answers}], @$refs); +} + +sub addType { + my $self = shift; + my $name = shift; + my $address = shift; + my $obj = $self->getObject($address); + my @answers; + for my $ans(@{$obj->{answers}}){ + push(@answers,$ans->{text}); + } + my $ansString = join(',',@answers); + $self->session->db->write("INSERT INTO Survey_questionTypes VALUES(?,?) ON DUPLICATE KEY UPDATE answers = ?",[$name,$ansString,$ansString]); + $self->question($address)->{questionType} = $name; +} + + +sub removeType { + my $self = shift; + my $address = shift; + my $obj = $self->getObject($address); + $self->session->db->write("DELETE FROM Survey_questionTypes WHERE questionType = ?",[$obj->{questionType}]); +} + +=head2 specialQuestionTypes + +Returns the arrayref to the special question types + +=cut + +sub specialQuestionTypes { + my $self = shift; + return $self->{specialQuestionTypes}; +} + +=head2 multipleChoiceTypes + +Returns the hashref to the multiple choice types + +=cut + +sub multipleChoiceTypes { + my $self = shift; + return $self->{multipleChoiceTypes}; +} + =head2 freeze Serialize this Perl object into a JSON string. The serialized object is made up of the survey and sections @@ -513,7 +528,6 @@ sub getQuestionEditVars { # Change questionType from a single element into an array of hashrefs which list the available # question types and which one is currently selected for this question.. - for my $qType ($self->getValidQuestionTypes) { push @{ $var{questionType} }, { text => $qType, @@ -530,7 +544,8 @@ A convenience method. Returns a list of question types. =cut sub getValidQuestionTypes { - return sort (@SPECIAL_QUESTION_TYPES, keys %MULTI_CHOICE_BUNDLES); + my $self = shift; + return sort (@{$self->{specialQuestionTypes}}, keys %{$self->{multipleChoiceTypes}}); } =head2 getAnswerEditVars ( $address ) @@ -1003,7 +1018,7 @@ sub getMultiChoiceBundle { my $self = shift; my ($type) = validate_pos( @_, { type => SCALAR | UNDEF } ); - return $MULTI_CHOICE_BUNDLES{$type}; + return $self->{multipleChoiceTypes}->{$type}; } =head2 addAnswersToQuestion ($address, $answers, $verbatims) diff --git a/www/extras/wobject/Survey/editsurvey/object.js b/www/extras/wobject/Survey/editsurvey/object.js index be4bab24d..aba0b7f99 100644 --- a/www/extras/wobject/Survey/editsurvey/object.js +++ b/www/extras/wobject/Survey/editsurvey/object.js @@ -73,7 +73,26 @@ Survey.ObjectTemplate = (function(){ } } }]; - + if(type === 'question'){ + btns[btns.length] = { + text: "Make Default Type", + handler: function(){ + var name = prompt("Please change name to new type, or leave to update current type",document.forms[0].questionType.value); + if(name != null){ + document.getElementById('addtype').value = name; + this.submit(); + } + } + } + btns[btns.length] = { + text: "Remove Default Type", + handler: function(){ + document.getElementById('removetype').value = 1; + this.submit(); + } + } + + } dialog = new YAHOO.widget.Dialog(type, { width: "600px", context: [document.body, 'tr', 'tr'],