Schrift
Wiki:Tipp zum Debugging: use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper \@var;
[thread]4584[/thread]

Ein simples Voting-Skript

Leser: 1


<< >> 7 Einträge, 1 Seite
Ronnie
 2006-04-22 01:18
#38688 #38688
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Dies soll ein Beispiel sein, wie man ein einfaches Voting-Skript realisieren kann. Es ist mehr ein proof-of-concept, als ein einsetzbares Werkzeug. Mehrfachabstimmungen werden nicht verhindert, es wäre sinnvoll eine Datenbank als Backend zu verwenden usw. - es kann und soll nur eine Möglichkeit aufzeigen, wie so etwas umgesetzt werden könnte!

Was mir gut gefällt und was ich hier erstmals wirklich nutze ist die Möglichkeit, die sich durch HTML::Template::Compiled ergibt, Methodenaufrufe innerhalb des Templates zu verwenden. Ich verwende sie hier u.a. für die Erzeugung der Charts, was so wirklich deutlich bequemer möglich ist.

So sieht ein Abstimmungs-Ergebnis aus:

http://www.ronnie-neumann.de/trashbin/votinge.png

Das Script:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/perl -T

use strict;
use warnings;

use CGI;
use CGI::Carp qw/warningsToBrowser fatalsToBrowser/;
use Data::Dumper;
use HTML::Template::Compiled;

package Questions;

sub _new {
my $class = shift;
my $id = shift or die 'It needs an ID to create an instance!';
my $question = shift or die 'It needs a question to create an instance!';
my $answers = [@_];
my $type = 'selection' if @$answers;
my $self = { id => $id, question => $question, type => $type, answers => $answers };

return bless $self, $class;
}

sub question {
my $self = shift;
return $self->{question};
}

sub render_form {
my $self = shift;
my $output;
if ($self->{type} ne 'selection') {
$output = "<select name='$self->{id}'>\\n"
. "<option value='1'>Sehr gut</option>\\n"
. "<option value='2'>Gut</option>\\n"
. "<option value='3'>Befriedigend</option>\\n"
. "<option value='4'>Ausreichend</option>\\n"
. "</select>\\n";
} else {
$output = "<select name='$self->{id}'>\\n";
for (0..$#{$self->{answers}}) {
$output .= "<option value='$_'>"
. $self->{answers}->[$_]
. "</option>\\n";
}
$output .= "</select>\\n";
}

return $output;
}

sub render_radio {
my $self = shift;
my $output;
if ($self->{type} ne 'selection') {
$output = "<select name='$self->{id}'>\\n"
. "<option value='1'>Sehr gut</option>\\n"
. "<option value='2'>Gut</option>\\n"
. "<option value='3'>Befriedigend</option>\\n"
. "<option value='4'>Ausreichend</option>\\n"
. "</select>\\n";
} else {
for (0..$#{$self->{answers}}) {
$output .= "<input type='radio' name='$self->{id}' value='$_' />";
$output .= $self->{answers}->[$_] . "<br />\\n";
}
}

return $output;
}

sub bulk_create {
my $class = shift;
my $filename = shift or die 'It needs an filename to bulk_create!';
my @catalog;

open (BULK, '<', $filename) or die "bulk_create died cause of: $!";
while (<BULK>) {
next unless /^\\d+/;
chomp;
my ($id, $question, @answers) = split /;/;
$question =~ s/"//g;
s/"//g for @answers;
push @catalog, $class->_new($id, $question, @answers);
}
close (BULK);

return \\@catalog;
}

1;

package Result;
use GD::Graph;
use GD::Graph::pie;
use MIME::Base64;

sub new {
my $class = shift;
my $self = {};
return bless $self, $class;
}

sub topic {
my $self = shift;
$self->{topic} = shift if @_;
return $self->{topic};
}

sub answer {
my $self = shift;
if (@_) {
my $choise = shift;
my $cnt = shift;
push @{$self->{answer}}, { choice => $choise, cnt => $cnt };
}
return $self->{answer};
}

sub as_pie_chart {
my $self = shift;
my $g = GD::Graph::pie->new(250, 250);
my @data = (
[map {$_->{choice}} @{$self->answer()}],
[map {$_->{cnt}} @{$self->answer()}],
);
my $gd = $g->plot(\\@data) or die $g->error;
my $img_mime = '<IMG SRC="data:image/png;base64,'. encode_base64($gd->png). '" alt=""/>';
return $img_mime;
}

1;

package Answers;

use Storable;
use Data::Dumper;

our $source; # let's try a class-variable for this (and for now)

sub new {
my $class = shift;
$source = shift or die 'new() needs a sourcename!';
my $self = -e "$source.db" ? retrieve("$source.db") : {};
return bless $self, $class;
}

sub update {
my $self = shift;
my $form = shift || {};
for (keys %$form) {
next if /action/;
$self->{$_}->{$form->{$_}}++;
}
store $self, "$source.db" or die "Can't store $source.dat: $!\\n";
}

sub as_csv {
my $self = shift;
my $questions = shift;
my $output = '';

for my $id (sort { $a <=> $b } keys %$self) {
if (ref $questions) {
$output .= '"' . $questions->[$id-1]->{question} . '"' . "\\n";
} else {
$output .= $id . "\\n";
}

for (sort { $a <=> $b } keys %{$self->{$id}}) {
if ($questions->[$id-1]->{answers}->[$_]) {
$output .= '"' . $questions->[$id-1]->{answers}->[$_] . '"'
. ";" . $self->{$id}->{$_} . "\\n";
} else {
$output .= $_ . ";" . $self->{$id}->{$_} . "\\n";
}
}
$output .= "\\n";
}
return $output;
}

sub as_report {
my $self = shift;
my $questions = shift;
my $output = [];

for my $id (sort { $a <=> $b } keys %$self) {
my $current = new Result;

if (ref $questions) {
$current->topic($questions->[$id-1]->{question});
} else {
$current->topic($id);
}

for (sort { $a <=> $b } keys %{$self->{$id}}) {
if ($questions->[$id-1]->{answers}->[$_]) {
$current->answer($questions->[$id-1]->{answers}->[$_], $self->{$id}->{$_});
} else {
$current->answer($_, $self->{$id}->{$_})
}
}
push @$output, $current;
}
return $output;
}

1;

package main;

my $source = 'intel';
my $questions = bulk_create Questions($source . '.txt');
my $q = new CGI;
my $template = HTML::Template::Compiled->new(filename => "vote5.html");
my $answers = new Answers( $source );

if ($q->param('action') eq 'Abstimmung') {
$answers->update({$q->Vars()});
$template->param( message => 'Danke, Ihre Stimme wurde gezaehlt!' );
} elsif ( $q->param('action') eq 'rep' ) {
$template->param( debug => $answers->as_report($questions) );
} elsif ( $q->param('action') eq 'csv' ) {
print $q->header('text/plain'), $answers->as_csv($questions);
exit;
} else {
$template->param( questions => $questions );
}
print $q->header(), $template->output();

Sp, das Template:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="de">

<head>
<title>Simple Report</title>
<meta name="author" content="Ronnie Neumann" />
<meta name="keywords" content="status report" />
<style type="text/css" media="screen">
/* <![CDATA[ */
h1 { color: #ff9; }
h2 { color: #99f; }

.default {
font-size: 80%;
color: #fff;
background-color: Ƽ font-family: Arial, Helvectia;
}

#content {
border-width: 2px;
border-color: #fff;
border-style: solid;
background-color: ō
color: #fff;
position: absolute;
top: 20px;
left: 180px;
right: 20px;
padding: 10px;
}

#sidebar {
position: absolute;
top: 20px;
left: 20px;
width: 120px;
height: 160px;
padding: 8px;
font-size: 90%;
border-width: 2px;
border-color: #fff;
border-style: solid;
background-color: ō
}

a.nav {
padding-left:8px;
padding-top:4px;
color: #bbb;
}

a.nav:hover {
color: #fff;
text-decoration: underline;
}

/* ]]> */
</style>

</head>

<body class="default">
<div id="sidebar">
<p><h2>Navigation</h2>
<a href="vote5.pl" class="nav">Umfrage</a><br />
<a href="vote5.pl?action=csv" class="nav" target="_blank">csv-Datei erstellen</a><br />
<a href="vote5.pl?action=rep" class="nav">Ergebnisse</a></p>
</div>
<div id="content">
<%IF questions%>
<h1>Umfrage</h1>
<form method="get">

<%LOOP questions%>
<h2><%= _.question%></h2>
<%= _.render_form%>
<hr />
<%/LOOP%>
<input type="submit" value="Abstimmung" name="action" />
<input type="reset" value="Reset" />
</form>
<%ELSE%>
<h1>Danke für die Teilnahme!</h1>
<%IF message%><%= message%><%/IF%>
<%IF debug%>
<%LOOP debug%>
<h2><%= _.topic%></h2>
<div class="chart"><%= _.as_pie_chart%></div>
<hr />
<%/LOOP%>
<%/IF%>
<%/IF%>
</div>
</body>
</html>

und so eine .csv-Datei die als Grundlage der Abstimmung dienen kann:
Code: (dl )
1
2
1;"Geschlecht?";"maennlich";"weiblich"
2;"Alter?";"20 bis 25 Jahre";"26 bis 30 Jahre";"31 bis 35 Jahre";"36 bis 40 Jahre";"40 bis 50 Jahre";"ueber 50 Jahre"
pq
 2006-04-22 01:49
#38689 #38689
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
sehr cool. hast du das irgendwo live laufen?
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
Ronnie
 2006-04-22 01:56
#38690 #38690
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
[quote=pq,21.04.2006, 23:49]sehr cool. hast du das irgendwo live laufen?[/quote]
Danke! Nein, leider läuft es nirgendwo live. Es ist auch eher ein Zwischenstand, aber ich war damit erstmal ganz zufrieden, da es die grundlegenden Funktionen erstmal so liefert, wie ich es vorgesehen habe.
GwenDragon
 2006-04-22 12:23
#38691 #38691
User since
2005-01-17
14788 Artikel
Admin1
[Homepage]
user image
Warum generierst du den HTML-Code im Skript nicht auch mit CGI? Das ist doch sinnvoller ;)
Ronnie
 2006-04-22 12:34
#38692 #38692
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
[quote=GwenDragon,22.04.2006, 10:23]Warum generierst du den HTML-Code im Skript nicht auch mit CGI? Das ist doch sinnvoller ;)[/quote]
Ich hatte ursprünglich überlegt auch dafür Templates zu verwenden, aber das erschien mir übertrieben, CGI wäre auch ein Möglichkeit gewesen, die ich aber nicht in Erwägung gezogen habe, wohl weil ich immer Templates verwende und quasi nie die Möglichkeiten von CGI zur HTML-Generierung. Danke für den Hinweis!
GwenDragon
 2006-04-22 13:22
#38693 #38693
User since
2005-01-17
14788 Artikel
Admin1
[Homepage]
user image
Nein, ich habe nicht die templates gemeint, die schreibe einer sowieso ganz normal als HTML hin.
Dies meinte ich z. B.:
Code: (dl )
1
2
3
4
5
6
output = "<select name='$self->{id}'>\\n"
. "<option value='1'>Sehr gut</option>\\n"
. "<option value='2'>Gut</option>\\n"
. "<option value='3'>Befriedigend</option>\\n"
. "<option value='4'>Ausreichend</option>\\n"
. "</select>\\n";
Ronnie
 2006-04-22 13:28
#38694 #38694
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Ja, so hatte ich dich auch verstanden - ich hatte ursprünglich überlegt auch dafür Templates zu verwenden.
<< >> 7 Einträge, 1 Seite



View all threads created 2006-04-22 01:18.