webgui/lib/WebGUI/Widget/Poll.pm

259 lines
14 KiB
Perl

package WebGUI::Widget::Poll;
our $namespace = "Poll";
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2002 Plain Black Software.
#-------------------------------------------------------------------
# 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::International;
use WebGUI::Privilege;
use WebGUI::Session;
use WebGUI::Shortcut;
use WebGUI::SQL;
use WebGUI::URL;
use WebGUI::Utility;
use WebGUI::Widget;
#-------------------------------------------------------------------
sub _viewPoll {
my (%poll, $i, $output);
tie %poll, 'Tie::CPHash';
%poll = getProperties($namespace,$_[0]);
if (defined %poll) {
$output = formHeader();
$output .= WebGUI::Form::hidden('wid',$_[0]);
$output .= WebGUI::Form::hidden('func','vote');
$output .= '<span class="pollQuestion">'.$poll{question}.'</span><br>';
for ($i=1; $i<=20; $i++) {
if ($poll{'a'.$i} =~ /\w/) {
$output .= WebGUI::Form::radio("answer",'a'.$i).' <span class="pollAnswer">'.$poll{'a'.$i}.'</span><br>';
}
}
$output .= '<br>'.WebGUI::Form::submit(WebGUI::International::get(11,$namespace));
$output .= '</form>';
}
return $output;
}
#-------------------------------------------------------------------
sub _viewResults {
my (%poll, @data, $i, $output, $totalResponses);
tie %poll, 'Tie::CPHash';
%poll = getProperties($namespace,$_[0]);
if (defined %poll) {
$output = '<span class="pollQuestion">'.$poll{question}.'</span>';
($totalResponses) = WebGUI::SQL->quickArray("select count(*) from Poll_answer where widgetId=$_[0]");
if ($totalResponses < 1) {
$totalResponses = 1;
}
for ($i=1; $i<=20; $i++) {
if ($poll{'a'.$i} =~ /\w/) {
$output .= '<span class="pollAnswer"><hr size=1>'.$poll{'a'.$i}.'<br></span>';
@data = WebGUI::SQL->quickArray("select count(*), answer from Poll_answer where answer='a$i' and widgetId=$_[0] group by answer");
$output .= '<table cellpadding=0 cellspacing=0 border=0><tr><td width="'.round($poll{graphWidth}*$data[0]/$totalResponses).'" class="pollColor"><img src="'.$session{setting}{lib}.'/spacer.gif" height="1" width="1"></td><td class="pollAnswer">&nbsp;&nbsp;'.round(100*$data[0]/$totalResponses).'%</td></tr></table>';
}
}
$output .= '<span class="pollAnswer"><hr size=1><b>Total Votes:</b> '.$totalResponses.'</span>';
}
return $output;
}
#-------------------------------------------------------------------
sub duplicate {
my ($sth, %data, $newWidgetId, $pageId, @row);
tie %data, 'Tie::CPHash';
%data = getProperties($namespace,$_[0]);
$pageId = $_[1] || $data{pageId};
$newWidgetId = create($pageId,$namespace,$data{title},$data{displayTitle},$data{description},$data{processMacros},$data{templatePosition});
WebGUI::SQL->write("insert into Poll values ($newWidgetId, '$data{active}', '$data{graphWidth}', '$data{voteGroup}', ".quote($data{question}).", ".quote($data{a1}).", ".quote($data{a2}).", ".quote($data{a3}).", ".quote($data{a4}).", ".quote($data{a5}).", ".quote($data{a6}).", ".quote($data{a7}).", ".quote($data{a8}).", ".quote($data{a9}).", ".quote($data{a10}).", ".quote($data{a11}).", ".quote($data{a12}).", ".quote($data{a13}).", ".quote($data{a14}).", ".quote($data{a15}).", ".quote($data{a16}).", ".quote($data{a17}).", ".quote($data{a18}).", ".quote($data{a19}).", ".quote($data{a20}).")");
$sth = WebGUI::SQL->read("select * from Poll_answer where widgetId=$_[0]");
while (@row = $sth->array) {
WebGUI::SQL->write("insert into Poll_answer values ($newWidgetId, '$row[1]', $row[2], '$row[3]')");
}
$sth->finish;
}
#-------------------------------------------------------------------
sub purge {
WebGUI::SQL->write("delete from Poll_answer where widgetId=$_[0]",$_[1]);
purgeWidget($_[0],$_[1],$namespace);
}
#-------------------------------------------------------------------
sub widgetName {
return WebGUI::International::get(1,$namespace);
}
#-------------------------------------------------------------------
sub www_add {
my ($output, %hash);
tie %hash, 'Tie::IxHash';
if (WebGUI::Privilege::canEditPage()) {
$output = helpLink(1,$namespace);
$output .= '<h1>'.WebGUI::International::get(2,$namespace).'</h1>';
$output .= formHeader();
$output .= WebGUI::Form::hidden("widget",$namespace);
$output .= WebGUI::Form::hidden("func","addSave");
$output .= '<table>';
$output .= tableFormRow(WebGUI::International::get(99),WebGUI::Form::text("title",20,128,'Poll'));
$output .= tableFormRow(WebGUI::International::get(174),WebGUI::Form::checkbox("displayTitle",1));
$output .= tableFormRow(WebGUI::International::get(175),WebGUI::Form::checkbox("processMacros",1));
%hash = WebGUI::Widget::getPositions();
$output .= tableFormRow(WebGUI::International::get(363),
WebGUI::Form::selectList("templatePosition",\%hash));
$output .= tableFormRow(WebGUI::International::get(85),WebGUI::Form::textArea("description",'',50,5,1));
$output .= tableFormRow(WebGUI::International::get(3,$namespace),WebGUI::Form::checkbox("active",1,1));
$output .= tableFormRow(WebGUI::International::get(4,$namespace),
WebGUI::Form::groupList("voteGroup",7)); # Group "Everyone"
$output .= tableFormRow(WebGUI::International::get(5,$namespace),
WebGUI::Form::text("graphWidth",20,3,150));
$output .= tableFormRow(WebGUI::International::get(6,$namespace),WebGUI::Form::text("question",50,255));
$output .= tableFormRow(WebGUI::International::get(7,$namespace).'<span><br>'.WebGUI::International::get(8,$namespace).'</span>',
WebGUI::Form::textArea("answers",'',50,8,0,'on'));
$output .= formSave();
$output .= '</table></form>';
return $output;
} else {
return WebGUI::Privilege::insufficient();
}
return $output;
}
#-------------------------------------------------------------------
sub www_addSave {
my ($widgetId, @answer);
if (WebGUI::Privilege::canEditPage()) {
$widgetId = create($session{page}{pageId},$session{form}{widget},$session{form}{title},$session{form}{displayTitle},$session{form}{description},$session{form}{processMacros},$session{form}{templatePosition});
@answer = split("\n",$session{form}{answers});
WebGUI::SQL->write("insert into Poll values ($widgetId, '$session{form}{active}', '$session{form}{graphWidth}', '$session{form}{voteGroup}', ".quote($session{form}{question}).", ".quote($answer[0]).", ".quote($answer[1]).", ".quote($answer[2]).", ".quote($answer[3]).", ".quote($answer[4]).", ".quote($answer[5]).", ".quote($answer[6]).", ".quote($answer[7]).", ".quote($answer[8]).", ".quote($answer[9]).", ".quote($answer[10]).", ".quote($answer[11]).", ".quote($answer[12]).", ".quote($answer[13]).", ".quote($answer[14]).", ".quote($answer[15]).", ".quote($answer[16]).", ".quote($answer[17]).", ".quote($answer[18]).", ".quote($answer[19]).")");
return "";
} else {
return WebGUI::Privilege::insufficient();
}
}
#-------------------------------------------------------------------
sub www_copy {
if (WebGUI::Privilege::canEditPage()) {
duplicate($session{form}{wid});
return "";
} else {
return WebGUI::Privilege::insufficient();
}
}
#-------------------------------------------------------------------
sub www_edit {
my ($output, %data, %hash, @array);
tie %hash, "Tie::IxHash";
tie %data, 'Tie::CPHash';
if (WebGUI::Privilege::canEditPage()) {
%data = getProperties($namespace,$session{form}{wid});
$output = helpLink(1,$namespace);
$output .= '<h1>'.WebGUI::International::get(9,$namespace).'</h1>';
$output .= formHeader();
$output .= WebGUI::Form::hidden("wid",$session{form}{wid});
$output .= WebGUI::Form::hidden("func","editSave");
$output .= '<table>';
$output .= tableFormRow(WebGUI::International::get(99),WebGUI::Form::text("title",20,128,$data{title}));
$output .= tableFormRow(WebGUI::International::get(174),
WebGUI::Form::checkbox("displayTitle",1,$data{displayTitle}));
$output .= tableFormRow(WebGUI::International::get(175),
WebGUI::Form::checkbox("processMacros",1,$data{processMacros}));
%hash = WebGUI::Widget::getPositions();
$array[0] = $data{templatePosition};
$output .= tableFormRow(WebGUI::International::get(363),
WebGUI::Form::selectList("templatePosition",\%hash,\@array));
$output .= tableFormRow(WebGUI::International::get(85),
WebGUI::Form::textArea("description",$data{description},50,5,1));
$output .= tableFormRow(WebGUI::International::get(3,$namespace),
WebGUI::Form::checkbox("active",1,$data{active}));
$output .= tableFormRow(WebGUI::International::get(4,$namespace),
WebGUI::Form::groupList("voteGroup",$data{voteGroup}));
$output .= tableFormRow(WebGUI::International::get(5,$namespace),
WebGUI::Form::text("graphWidth",20,3,$data{graphWidth}));
$output .= tableFormRow(WebGUI::International::get(6,$namespace),
WebGUI::Form::text("question",50,255,$data{question}));
$output .= tableFormRow(WebGUI::International::get(7,$namespace).'<span><br>'.WebGUI::International::get(8,$namespace).'</span>',
WebGUI::Form::textArea("answers",$data{a1}."\n".$data{a2}."\n".$data{a3}."\n".$data{a4}."\n".$data{a5}."\n".$data{a6}."\n".$data{a7}."\n".$data{a8}."\n".$data{a9}."\n".$data{a10}."\n".$data{a11}."\n".$data{a12}."\n".$data{a13}."\n".$data{a14}."\n".$data{a15}."\n".$data{a16}."\n".$data{a17}."\n".$data{a18}."\n".$data{a19}."\n".$data{a20}."\n",50,8,0,'on'));
$output .= formSave();
$output .= tableFormRow("",'<a href="'.WebGUI::URL::page('func=resetVotes&wid='.$session{form}{wid})
.'">'.WebGUI::International::get(10,$namespace).'</a>');
$output .= '</table></form>';
return $output;
} else {
return WebGUI::Privilege::insufficient();
}
}
#-------------------------------------------------------------------
sub www_editSave {
my (@answer);
if (WebGUI::Privilege::canEditPage()) {
update();
@answer = split("\n",$session{form}{answers});
WebGUI::SQL->write("update Poll set active='$session{form}{active}', voteGroup='$session{form}{voteGroup}', graphWidth=$session{form}{graphWidth}, question=".quote($session{form}{question}).", a1=".quote($answer[0]).", a2=".quote($answer[1]).", a3=".quote($answer[2]).", a4=".quote($answer[3]).", a5=".quote($answer[4]).", a6=".quote($answer[5]).", a7=".quote($answer[6]).", a8=".quote($answer[7]).", a9=".quote($answer[8]).", a10=".quote($answer[9]).", a11=".quote($answer[10]).", a12=".quote($answer[11]).", a13=".quote($answer[12]).", a14=".quote($answer[13]).", a15=".quote($answer[14]).", a16=".quote($answer[15]).", a17=".quote($answer[16]).", a18=".quote($answer[17]).", a19=".quote($answer[18]).", a20=".quote($answer[19])." where widgetId=$session{form}{wid}");
return "";
} else {
return WebGUI::Privilege::insufficient();
}
}
#-------------------------------------------------------------------
sub www_resetVotes {
if (WebGUI::Privilege::canEditPage()) {
WebGUI::SQL->write("delete from Poll_answer where widgetId='$session{form}{wid}'");
}
return "";
}
#-------------------------------------------------------------------
sub www_view {
my ($hasVoted, %data, $output);
tie %data, 'Tie::CPHash';
%data = getProperties($namespace,$_[0]);
if ($data{displayTitle} == 1) {
$output = "<h1>".$data{title}."</h1>";
}
if ($data{description} ne "") {
$output .= $data{description}.'<p>';
}
if ($data{active} eq "0") {
$output .= _viewResults($_[0]);
} elsif (WebGUI::Privilege::isInGroup($data{voteGroup},$session{user}{userId})) {
($hasVoted) = WebGUI::SQL->quickArray("select count(*) from Poll_answer where widgetId=$_[0] and ((userId=$session{user}{userId} and userId<>1) or (userId=1 and ipAddress='$session{env}{REMOTE_ADDR}'))");
if ($hasVoted) {
$output .= _viewResults($_[0]);
} else {
$output .= _viewPoll($_[0]);
}
} else {
$output .= _viewResults($_[0]);
}
return $output;
}
#-------------------------------------------------------------------
sub www_vote {
my ($voteGroup,$hasVoted);
($voteGroup) = WebGUI::SQL->quickArray("select voteGroup from Poll where widgetId='$session{form}{wid}'");
($hasVoted) = WebGUI::SQL->quickArray("select count(*) from Poll_answer where widgetId=$session{form}{wid} and ((userId=$session{user}{userId} and userId<>1) or (userId=1 and ipAddress='$session{env}{REMOTE_ADDR}'))");
if (WebGUI::Privilege::isInGroup($voteGroup,$session{user}{userId}) && !($hasVoted)) {
WebGUI::SQL->write("insert into Poll_answer values ($session{form}{wid}, '$session{form}{answer}', $session{user}{userId}, '$session{env}{REMOTE_ADDR}')");
}
return "";
}
1;