From 80d909981595bf3ca0c860f5b760ea6231d12dce Mon Sep 17 00:00:00 2001 From: JT Smith Date: Thu, 1 Aug 2002 02:47:03 +0000 Subject: [PATCH] Poll answers are now randomizable. --- docs/upgrades/upgrade_4.4.1-4.5.0.sql | 3 ++- lib/WebGUI/Utility.pm | 13 ++++++++++++- lib/WebGUI/Wobject/Poll.pm | 19 +++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/docs/upgrades/upgrade_4.4.1-4.5.0.sql b/docs/upgrades/upgrade_4.4.1-4.5.0.sql index 3219abb57..56050d60f 100644 --- a/docs/upgrades/upgrade_4.4.1-4.5.0.sql +++ b/docs/upgrades/upgrade_4.4.1-4.5.0.sql @@ -1,4 +1,5 @@ insert into webguiVersion values ('4.5.0','upgrade',unix_timestamp()); - +insert into international values (72,'Poll',1,'Randomize answers?'); +alter table Poll add column randomizeAnswers int not null default 0; diff --git a/lib/WebGUI/Utility.pm b/lib/WebGUI/Utility.pm index cddcde2dc..d4bd451eb 100644 --- a/lib/WebGUI/Utility.pm +++ b/lib/WebGUI/Utility.pm @@ -15,7 +15,7 @@ use strict; use Tie::IxHash; our @ISA = qw(Exporter); -our @EXPORT = qw(&sortHashDescending &sortHash &isIn &randint &round); +our @EXPORT = qw(&randomizeArray &sortHashDescending &sortHash &isIn &randint &round); #------------------------------------------------------------------- sub isIn { @@ -47,6 +47,17 @@ sub randint { return $low + int( rand( $high - $low + 1 ) ); } +#------------------------------------------------------------------- +sub randomizeArray { + my ($array, $i, $j); + $array = shift; + for ($i = @$array; --$i; ) { + $j = int rand ($i+1); + next if $i == $j; + @$array[$i,$j] = @$array[$j,$i]; + } +} + #------------------------------------------------------------------- sub round { return sprintf("%.0f", $_[0]); diff --git a/lib/WebGUI/Wobject/Poll.pm b/lib/WebGUI/Wobject/Poll.pm index 46085aa41..ec38230b3 100644 --- a/lib/WebGUI/Wobject/Poll.pm +++ b/lib/WebGUI/Wobject/Poll.pm @@ -36,6 +36,7 @@ sub duplicate { $w = WebGUI::Wobject::Poll->new({wobjectId=>$w,namespace=>$namespace}); $w->set({ active=>$_[0]->get("active"), + randomizeAnswers=>$_[0]->get("randomizeAnswers"), graphWidth=>$_[0]->get("graphWidth"), voteGroup=>$_[0]->get("voteGroup"), question=>$_[0]->get("question"), @@ -85,7 +86,7 @@ sub purge { #------------------------------------------------------------------- sub set { - $_[0]->SUPER::set($_[1],[qw(active karmaPerVote graphWidth voteGroup question a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20)]); + $_[0]->SUPER::set($_[1],[qw(active karmaPerVote graphWidth voteGroup question randomizeAnswers a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20)]); } #------------------------------------------------------------------- @@ -100,12 +101,14 @@ sub www_copy { #------------------------------------------------------------------- sub www_edit { - my ($f, $i, $output, $active, $voteGroup, $graphWidth, $answers); + my ($f, $i, $output, $active, $voteGroup, $graphWidth, $answers, $randomizeAnswers); if (WebGUI::Privilege::canEditPage()) { if ($_[0]->get("wobjectId") eq "new") { $active = 1; + $randomizeAnswers = 1; } else { $active = $_[0]->get("active"); + $randomizeAnswers = $_[0]->get("randomizeAnswers"); } $voteGroup = $_[0]->get("voteGroup") || 7; $graphWidth = $_[0]->get("graphWidth") || 150; @@ -127,6 +130,7 @@ sub www_edit { $f->integer("graphWidth",WebGUI::International::get(5,$namespace),$graphWidth); $f->text("question",WebGUI::International::get(6,$namespace),$_[0]->get("question")); $f->textarea("answers",WebGUI::International::get(7,$namespace).'
'.WebGUI::International::get(8,$namespace).'
',$answers); + $f->yesNo("randomizeAnswers",WebGUI::International::get(72,$namespace),$randomizeAnswers); $output .= $_[0]->SUPER::www_edit($f->printRowsOnly); if ($_[0]->get("wobjectId") ne "new") { $output .= '

'; @@ -148,6 +152,7 @@ sub www_editSave { for ($i=1; $i<=20; $i++) { $property->{'a'.$i} = $answer[($i-1)]; } + $property->{randomizeAnswers} = $session{form}{randomizeAnswers}; $property->{karmaPerVote} = $session{form}{karmaPerVote}; $property->{voteGroup} = $session{form}{voteGroup}; $property->{graphWidth} = $session{form}{graphWidth}; @@ -170,7 +175,7 @@ sub www_resetVotes { #------------------------------------------------------------------- sub www_view { - my ($hasVoted, $output, $showPoll, $f, $i, $totalResponses, @data); + my ($hasVoted, $answer, $output, @answers, $showPoll, $f, $i, $totalResponses, @data); $output = $_[0]->displayTitle; $output .= $_[0]->description; if ($_[0]->get("active") eq "0") { @@ -193,9 +198,15 @@ sub www_view { $f->hidden('func','vote'); for ($i=1; $i<=20; $i++) { if ($_[0]->get('a'.$i) =~ /\w/) { - $f->raw(' '.$_[0]->get('a'.$i).'
'); + $answers[($i-1)] = ' '.$_[0]->get('a'.$i).'
'; } } + if ($_[0]->get("randomizeAnswers")) { + randomizeArray(\@answers); + } + foreach $answer (@answers) { + $f->raw($answer); + } $f->raw('
'); $f->submit(WebGUI::International::get(11,$namespace)); $output .= $f->print;