-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprototype.cgi
executable file
·262 lines (229 loc) · 9.48 KB
/
prototype.cgi
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use DBI;
use FindBin;
use vars qw($dbh);
my $q = new CGI;
if ($q->param('person_id')) {
show_person($q);
} elsif ($q->param('division_id')) {
show_division($q);
} else {
show_frontpage($q);
}
db_disconnect();
exit 0;
sub print_html_header {
my ($q) = @_;
print($q->header( -type => 'text/html; charset=utf-8' ));
print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">', "\n";
print "<html>\n";
print "<head>\n";
print $q->title('Valgt av folket'), "\n";
print "</head>\n";
print "<body>\n";
print $q->p(
$q->a({href => $q->url(-path_info=>1)},
'Valgt av folket') .
' | ' .
$q->a({href => 'http://www.nuug.no/' },
'Foreningen NUUG')
), "\n";
print $q->hr();
}
sub print_html_footer {
my ($q) = @_;
print $q->hr();
print $q->p(
'Inspirert av ' .
$q->a({href => 'http://www.publicwhip.org.uk/'},
'Public Whip') .
' og ' .
$q->a({href => 'http://www.theyworkforyou.com/'},
'TheyWorkForYou') .
'. Basert på ' .
$q->a({href => 'https://gitorious.org/nuug/folketingparser'},
'snedig fri programvare') .
'.'
), "\n";
print "</body>\n";
print "</html>\n";
}
sub show_division {
my ($q) = @_;
my $division_id = $q->param('division_id');
print_html_header($q);
print $q->h2('Votering'), "\n";
my $division =
select_all('SELECT id, description, when_divided, heading_id, '.
' session_num, map_num, topic_num, yes_count, no_count ' .
' FROM division '.
' WHERE id = ? ', $division_id);
print $q->p('Tema: ' .
$q->a({href => $q->url(-path_info=>1) .
'?division_id=' . $division_id},
$division->[0]->{description})), "\n";
print $q->p('Tidspunkt: ' . $division->[0]->{when_divided}), "\n";
if ($division->[0]->{heading_id}) {
my $heading_id = $division->[0]->{heading_id};
my $link = "http://www.stortinget.no/no/Saker-og-publikasjoner/Saker/Sak/?p=$heading_id";
print $q->p($q->a({href => $link}, 'Saksinfo fra stortinget')), "\n";
}
if ($division->[0]->{when_divided} =~ m/^(\d{4})-(\d{2})-(\d{2})T/) {
my ($year, $month, $day) = ($1, $2, $3);
$year =~ s/^..//;
my $session_num = $division->[0]->{session_num};
my $topic_num = $division->[0]->{topic_num};
my %sessionmap = (
'150' => '2005-2006',
'151' => '2006-2007',
'152' => '2007-2008',
'153' => '2008-2009',
'154' => '2009-2010',
'155' => '2010-2011',
);
my $yearstr = $sessionmap{$session_num};
if (defined $yearstr) {
my $link = "http://www.stortinget.no/no/Saker-og-publikasjoner/Publikasjoner/Referater/Stortinget/$yearstr/$year$month$day/$topic_num/";
print $q->p($q->a({href => $link}, 'Referat fra stortinget')), "\n";
}
}
print($q->p(sprintf('Opptelling gav %d for og %d mot.',
$division->[0]->{yes_count},
$division->[0]->{no_count})), "\n");
my $votecount =
select_all('SELECT vote, count(*) AS count FROM vote '.
'WHERE division_id = ? GROUP BY vote '.
'ORDER BY count DESC', $division_id);
my %count;
map { $count{$_->{vote}} = $_->{count}; } @{$votecount};
printf($q->p(
sprintf('Databasen inneholder %d for, %d mot og %d fraværende.',
$count{'yes'}, $count{'no'}, $count{'absent'})));
my $votes =
select_all('SELECT person_id, vote, first_name, last_name, party '.
'FROM division, vote, person '.
'WHERE division.id = vote.division_id '.
' AND person.id = vote.person_id '.
' AND division_id = ? '.
'ORDER BY vote DESC, party, last_name, first_name', $division_id);
print $q->start_table({border=>1, cellpadding=>2, cellspacing=>0}), "\n";
print $q->Tr({}, $q->th({}, ['Hvem', 'Parti', 'Stemme'])), "\n";
map {
print $q->Tr({}, $q->td({}, [ $q->a({href => $q->url(-path_info=>1) .
'?person_id=' . $_->{person_id}},
$_->{first_name} . ' ' .
$_->{last_name} ),
$_->{party},
$_->{vote} ])), "\n";
} @{$votes};
print $q->end_table(), "\n";
print_html_footer($q);
}
sub show_person {
my ($q) = @_;
my $person_id = $q->param('person_id');
print_html_header($q);
my $person = select_all('SELECT first_name, last_name, type, ref '.
' FROM person, person_ref '.
' WHERE person_id = id AND id = ?', $person_id);
if ($person) {
print($q->h1($person->[0]->{first_name} . ' ' .
$person->[0]->{last_name}), "\n");
}
for my $ref (@$person) {
my $value = $ref->{ref};
my ($link, $text);
my ($link2, $text2);
if ('stortinget-perid' eq $ref->{type}) {
$link = "http://www.stortinget.no/no/Representanter-og-komiteer/Representantene/Representantfordeling/Representant/?perid=$value";
$text = 'Informasjon fra Stortinget';
$link2 = "http://sok.stortinget.no/?customercode=$value";
$text2 = 'Innlegg fra Stortingets talerstol'
} elsif ('nsd-polsys-id' eq $ref->{type}) {
$link = "http://www.nsd.uib.no/polsys/index.cfm?UttakNr=33&person=$value";
$text = 'Informasjon fra NSD';
} elsif ('no.wikipedia.org' eq $ref->{type}) {
$link = "http://no.wikipedia.org/wiki/$value";
$text = 'Informasjon fra norsk Wikipedia';
}
print( $q->p( $q->a({href => $link}, $text) ), "\n") if ($link);
print( $q->p( $q->a({href => $link2}, $text2) ), "\n") if ($link2);
}
print $q->h2('Voteringer'), "\n";
my $count = select_all('SELECT COUNT(*) as count FROM vote WHERE person_id = ?',
$person_id);
my $absent = select_all('SELECT COUNT(*) as count FROM vote '.
' WHERE person_id = ? '.
' AND vote = "absent"',
$person_id,);
if ($count->[0]->{count}) {
print $q->p(sprintf('Fraværende på %d av %d (%.0f%%) voteringer.',
$absent->[0]->{count},$count->[0]->{count},
100*$absent->[0]->{count}/$count->[0]->{count}));
}
my $votes =
select_all('SELECT id, description, when_divided, vote '.
'FROM division, vote '.
'WHERE division.id = vote.division_id '.
'AND vote.person_id = ? '.
'ORDER BY when_divided desc', $person_id);
print $q->start_table({border=>1, cellpadding=>2, cellspacing=>0}), "\n";
print $q->Tr({}, $q->th({}, [ 'Når', 'Stemme', 'Tema' ])), "\n";
map {
print $q->Tr({}, $q->td({}, [ $_->{when_divided}, $_->{vote},
$q->a({href => $q->url(-path_info=>1) .
'?division_id=' . $_->{id}},
$_->{description}) ])), "\n";
} @{$votes};
print $q->end_table(), "\n";
print_html_footer($q);
}
sub show_frontpage {
my ($q) = @_;
print_html_header($q);
print($q->h1('Valgt av folket'), "\n");
print($q->p('Nettsted som lar deg følge de folkevalgte fra dag til dag. Dette er en proof-of-concept-løsning som demonstrerer hva som kan vises frem når voterings-data fra Stortinget blir tilgjengelig.'),
"\n\n");
print($q->h2('Siste 20 voteringer'), "\n");
my $divisions =
select_all('SELECT id, when_divided, description FROM division '.
'ORDER BY when_divided DESC LIMIT 20');
print($q->ul(
map { ($q->li($_->{when_divided} . ' ' .
$q->a({href => $q->url(-path_info=>1) .
'?division_id=' . $_->{id}},
$_->{description})), "\n"); }
@{$divisions}
), "\n");
print($q->h2('Representanter'), "\n");
my $people = select_all('SELECT id, first_name, last_name FROM person '.
'WHERE id in (SELECT person_id FROM vote) '.
'ORDER BY last_name, first_name');
print($q->ul(
map { ($q->li($q->a({href => $q->url(-path_info=>1) .
'?person_id=' . $_->{id}},
$_->{first_name} . ' ' .
$_->{last_name} )), "\n"); } @{$people}
), "\n");
print_html_footer($q);
}
sub db_connect {
my $dbfile = "$FindBin::Bin/prototype.sqlite";
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
}
sub db_disconnect {
if ($dbh) {
$dbh->disconnect or warn $dbh->errstr;
$dbh = undef;
}
}
sub select_all {
my ($query, @bind_values) = @_;
unless ($dbh) {
$dbh = db_connect();
}
$dbh->selectall_arrayref($query, { Slice => {} }, @bind_values);
}