package WebGUI::Wobject::Poll; #------------------------------------------------------------------- # WebGUI is Copyright 2001-2002 Plain Black LLC. #------------------------------------------------------------------- # Please read the legal notices (docs/legal.txt) and the license # (docs/license.txt) that came with this distribution before using # this software. #------------------------------------------------------------------- # http://www.plainblack.com info@plainblack.com #------------------------------------------------------------------- use strict; use Tie::CPHash; use WebGUI::HTMLForm; use WebGUI::Icon; use WebGUI::International; use WebGUI::Privilege; use WebGUI::Session; use WebGUI::SQL; use WebGUI::URL; use WebGUI::User; use WebGUI::Utility; use WebGUI::Wobject; our @ISA = qw(WebGUI::Wobject); our $namespace = "Poll"; our $name = WebGUI::International::get(1,$namespace); #------------------------------------------------------------------- sub duplicate { my ($w, $f, $sth, @row); $w = $_[0]->SUPER::duplicate($_[1]); $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"), karmaPerVote=>$_[0]->get("karmaPerVote"), a1=>$_[0]->get("a1"), a2=>$_[0]->get("a2"), a3=>$_[0]->get("a3"), a4=>$_[0]->get("a4"), a5=>$_[0]->get("a5"), a6=>$_[0]->get("a6"), a7=>$_[0]->get("a7"), a8=>$_[0]->get("a8"), a9=>$_[0]->get("a9"), a10=>$_[0]->get("a10"), a11=>$_[0]->get("a11"), a12=>$_[0]->get("a12"), a13=>$_[0]->get("a13"), a14=>$_[0]->get("a14"), a15=>$_[0]->get("a15"), a16=>$_[0]->get("a16"), a17=>$_[0]->get("a17"), a18=>$_[0]->get("a18"), a19=>$_[0]->get("a19"), a20=>$_[0]->get("a20") }); $sth = WebGUI::SQL->read("select * from Poll_answer where wobjectId=".$_[0]->get("wobjectId")); while (@row = $sth->array) { WebGUI::SQL->write("insert into Poll_answer values (".$w->get("wobjectId").", '$row[1]', $row[2], '$row[3]')"); } $sth->finish; } #------------------------------------------------------------------- sub purge { WebGUI::SQL->write("delete from Poll_answer where wobjectId=".$_[0]->get("wobjectId")); $_[0]->SUPER::purge(); } #------------------------------------------------------------------- sub set { $_[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)]); } #------------------------------------------------------------------- sub www_edit { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); my ($f, $i, $output, $active, $voteGroup, $graphWidth, $answers, $randomizeAnswers); 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; for ($i=1; $i<=20; $i++) { if ($_[0]->get('a'.$i) =~ /\C/) { $answers .= $_[0]->get("a".$i)."\n"; } } $output = helpIcon(1,$namespace); $output .= '
';
$output .= ''
.WebGUI::International::get(10,$namespace).'';
}
return $output;
}
#-------------------------------------------------------------------
sub www_editSave {
return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage());
my (@answer, $i, $property);
@answer = split("\n",$session{form}{answers});
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};
$property->{active} = $session{form}{active};
$property->{question} = $session{form}{question};
$_[0]->SUPER::www_editSave($property);
return "";
}
#-------------------------------------------------------------------
sub www_resetVotes {
return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage());
$_[0]->deleteCollateral("Poll_answer","wobjectId",$_[0]->get("wobjectId"));
return "";
}
#-------------------------------------------------------------------
sub www_view {
my ($hasVoted, $answer, $output, @answers, $showPoll, $f, $i, $totalResponses, @data);
$output = $_[0]->displayTitle;
$output .= $_[0]->description;
if ($_[0]->get("active") eq "0") {
$showPoll = 0;
} elsif (WebGUI::Privilege::isInGroup($_[0]->get("voteGroup"),$session{user}{userId})) {
($hasVoted) = WebGUI::SQL->quickArray("select count(*) from Poll_answer where wobjectId=".$_[0]->get("wobjectId")."
and ((userId=$session{user}{userId} and userId<>1) or (userId=1 and ipAddress='$session{env}{REMOTE_ADDR}'))");
if ($hasVoted) {
$showPoll = 0;
} else {
$showPoll = 1;
}
} else {
$showPoll = 0;
}
$output .= ''.$_[0]->get("question").'
';
if ($showPoll) {
$f = WebGUI::HTMLForm->new(1);
$f->hidden('wid',$_[0]->get("wobjectId"));
$f->hidden('func','vote');
for ($i=1; $i<=20; $i++) {
if ($_[0]->get('a'.$i) =~ /\C/) {
$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;
} else {
($totalResponses) = WebGUI::SQL->quickArray("select count(*) from Poll_answer where wobjectId=".$_[0]->get("wobjectId"));
if ($totalResponses < 1) {
$totalResponses = 1;
}
for ($i=1; $i<=20; $i++) {
if ($_[0]->get('a'.$i) =~ /\C/) {
$output .= '
'.$_[0]->get('a'.$i).'
';
@data = WebGUI::SQL->quickArray("select count(*), answer from Poll_answer where answer='a$i' and wobjectId="
.$_[0]->get("wobjectId")." group by answer");
$output .= '
| '. round(100*$data[0]/$totalResponses).'% ('.($data[0]+0).') |