Poll answers are now randomizable.

This commit is contained in:
JT Smith 2002-08-01 02:47:03 +00:00
parent 5c903b5c39
commit 80d9099815
3 changed files with 29 additions and 6 deletions

View file

@ -1,4 +1,5 @@
insert into webguiVersion values ('4.5.0','upgrade',unix_timestamp()); 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;

View file

@ -15,7 +15,7 @@ use strict;
use Tie::IxHash; use Tie::IxHash;
our @ISA = qw(Exporter); our @ISA = qw(Exporter);
our @EXPORT = qw(&sortHashDescending &sortHash &isIn &randint &round); our @EXPORT = qw(&randomizeArray &sortHashDescending &sortHash &isIn &randint &round);
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub isIn { sub isIn {
@ -47,6 +47,17 @@ sub randint {
return $low + int( rand( $high - $low + 1 ) ); 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 { sub round {
return sprintf("%.0f", $_[0]); return sprintf("%.0f", $_[0]);

View file

@ -36,6 +36,7 @@ sub duplicate {
$w = WebGUI::Wobject::Poll->new({wobjectId=>$w,namespace=>$namespace}); $w = WebGUI::Wobject::Poll->new({wobjectId=>$w,namespace=>$namespace});
$w->set({ $w->set({
active=>$_[0]->get("active"), active=>$_[0]->get("active"),
randomizeAnswers=>$_[0]->get("randomizeAnswers"),
graphWidth=>$_[0]->get("graphWidth"), graphWidth=>$_[0]->get("graphWidth"),
voteGroup=>$_[0]->get("voteGroup"), voteGroup=>$_[0]->get("voteGroup"),
question=>$_[0]->get("question"), question=>$_[0]->get("question"),
@ -85,7 +86,7 @@ sub purge {
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub set { 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 { 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 (WebGUI::Privilege::canEditPage()) {
if ($_[0]->get("wobjectId") eq "new") { if ($_[0]->get("wobjectId") eq "new") {
$active = 1; $active = 1;
$randomizeAnswers = 1;
} else { } else {
$active = $_[0]->get("active"); $active = $_[0]->get("active");
$randomizeAnswers = $_[0]->get("randomizeAnswers");
} }
$voteGroup = $_[0]->get("voteGroup") || 7; $voteGroup = $_[0]->get("voteGroup") || 7;
$graphWidth = $_[0]->get("graphWidth") || 150; $graphWidth = $_[0]->get("graphWidth") || 150;
@ -127,6 +130,7 @@ sub www_edit {
$f->integer("graphWidth",WebGUI::International::get(5,$namespace),$graphWidth); $f->integer("graphWidth",WebGUI::International::get(5,$namespace),$graphWidth);
$f->text("question",WebGUI::International::get(6,$namespace),$_[0]->get("question")); $f->text("question",WebGUI::International::get(6,$namespace),$_[0]->get("question"));
$f->textarea("answers",WebGUI::International::get(7,$namespace).'<span class="formSubtext"><br>'.WebGUI::International::get(8,$namespace).'</span>',$answers); $f->textarea("answers",WebGUI::International::get(7,$namespace).'<span class="formSubtext"><br>'.WebGUI::International::get(8,$namespace).'</span>',$answers);
$f->yesNo("randomizeAnswers",WebGUI::International::get(72,$namespace),$randomizeAnswers);
$output .= $_[0]->SUPER::www_edit($f->printRowsOnly); $output .= $_[0]->SUPER::www_edit($f->printRowsOnly);
if ($_[0]->get("wobjectId") ne "new") { if ($_[0]->get("wobjectId") ne "new") {
$output .= '<p>'; $output .= '<p>';
@ -148,6 +152,7 @@ sub www_editSave {
for ($i=1; $i<=20; $i++) { for ($i=1; $i<=20; $i++) {
$property->{'a'.$i} = $answer[($i-1)]; $property->{'a'.$i} = $answer[($i-1)];
} }
$property->{randomizeAnswers} = $session{form}{randomizeAnswers};
$property->{karmaPerVote} = $session{form}{karmaPerVote}; $property->{karmaPerVote} = $session{form}{karmaPerVote};
$property->{voteGroup} = $session{form}{voteGroup}; $property->{voteGroup} = $session{form}{voteGroup};
$property->{graphWidth} = $session{form}{graphWidth}; $property->{graphWidth} = $session{form}{graphWidth};
@ -170,7 +175,7 @@ sub www_resetVotes {
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub www_view { 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]->displayTitle;
$output .= $_[0]->description; $output .= $_[0]->description;
if ($_[0]->get("active") eq "0") { if ($_[0]->get("active") eq "0") {
@ -193,9 +198,15 @@ sub www_view {
$f->hidden('func','vote'); $f->hidden('func','vote');
for ($i=1; $i<=20; $i++) { for ($i=1; $i<=20; $i++) {
if ($_[0]->get('a'.$i) =~ /\w/) { if ($_[0]->get('a'.$i) =~ /\w/) {
$f->raw('<input type="radio" name="answer" value="a'.$i.'"> <span class="pollAnswer">'.$_[0]->get('a'.$i).'</span><br>'); $answers[($i-1)] = '<input type="radio" name="answer" value="a'.$i.'"> <span class="pollAnswer">'.$_[0]->get('a'.$i).'</span><br>';
} }
} }
if ($_[0]->get("randomizeAnswers")) {
randomizeArray(\@answers);
}
foreach $answer (@answers) {
$f->raw($answer);
}
$f->raw('<br>'); $f->raw('<br>');
$f->submit(WebGUI::International::get(11,$namespace)); $f->submit(WebGUI::International::get(11,$namespace));
$output .= $f->print; $output .= $f->print;