#!/usr/bin/perl # MimerDesk # Web based groupware and eLearning environment # www.mimerdesk.org # # Copyright (C) 2001 Ionstream Ltd. # www.ionstream.fi # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 2 of the License, or (at your option) # any later version. # # This program is distributed with a hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #$Id: vote.html,v 1.175 2002/07/24 12:34:18 inf Exp $ #"""""""""""""""""""""""""""""""""""""""""# # # # MimerDesk: Voting System # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # (c) Ionstream Oy 1999 - 2001 # # # # Programmed by: Sabir Samaletdin # # # #_________________________________________# # Begin ################################## use strict; use vars qw ($APPLICATION $ACTIVEGRP $admin_status $USER $IP $LAST_ACT $FORWARDED $TIME_USED $IDLE $form $ref $sth $GROUPACTIVOR $VOTES $VOTE_USERS $VOTE_VARS $ADMIN $htmlcolors $SCRIPT @months $trans $USERS_DB); use lib::MimerDesk; use CGI::Carp "fatalsToBrowser"; sub template_tulostus; sub create_menu; $APPLICATION = 'Resources - Voting'; $SCRIPT = 'vote.html'; # Tables $VOTES = 'votes'; $VOTE_USERS = 'vote_users'; $VOTE_VARS = 'vote_vars'; $USERS_DB = 'users'; ################################## read_config('../config/mimerdesk.cfg'); $htmlcolors = initialize('colors'); $form = decode_multipart(); $form->{'ID'} =~ tr/0-9//cd; $form->{'auth'} =~ tr/0-9a-z//cd; ($USER, $IP, $LAST_ACT, $FORWARDED, $TIME_USED, $IDLE, $ACTIVEGRP) = authenticate($form->{'ID'}, $form->{'auth'}, $form->{'changeGroup'}); $trans = lib::MimerDesk->new_gettext(program => 'vote',language => $config{'language'}); @months = ( '', $trans->gettext('January'), $trans->gettext('February'), $trans->gettext('March'), $trans->gettext('April'), $trans->gettext('May'), $trans->gettext('June'), $trans->gettext('July'), $trans->gettext('August'), $trans->gettext('September'), $trans->gettext('October'), $trans->gettext('November'), $trans->gettext('December') ); $APPLICATION = $trans->gettext("Resources - Voting"); if ($form->{'grouptool'} =~ /^\d+$/) { my ($test); lock_tables('READ', 'groupusers'); db_list("SELECT RIGHTS,USER FROM groupusers where USER = '$USER' and GID = '$form->{'grouptool'}'"); while (my $ref = $sth->fetchrow_hashref()) { $admin_status = $ref->{'RIGHTS'}; $GROUPACTIVOR = $ref->{'USER'}; } db_end(); unlock_tables(); if (!$GROUPACTIVOR) { lock_tables('READ', 'users'); db_list("SELECT * FROM users where nimi = '$USER'"); while (my $ref = $sth->fetchrow_hashref()) {$admin_status = $ref->{'flags'};} db_end(); unlock_tables(); } else { $VOTES = "$form->{'grouptool'}_$VOTES"; $VOTE_USERS = "$form->{'grouptool'}_$VOTE_USERS"; $VOTE_VARS = "$form->{'grouptool'}_$VOTE_VARS"; } } else { lock_tables('READ', 'users'); db_list("SELECT * FROM users where nimi = '$USER'"); while (my $ref = $sth->fetchrow_hashref()) {$admin_status = $ref->{'flags'};} db_end(); unlock_tables(); } if ($form->{'quit'}) {redirect("$config{'loc_server'}$config{'bin_dir'}/index.html?ID=$form->{'ID'}&auth=$form->{'auth'}&quit=quit");} HandleFormStuff(); ################################################################ # # HandleFormStuff(): # # Handels the form stuff and calls the functions desired. # ################################################################ sub HandleFormStuff { unless ($form->{'vid'} =~ /^[0-9]+$/) { $form->{'vid'} = undef; } if ($form->{'cancel_show'}) { if ($form->{'vid'}) { ShowVote($form->{'vid'}); exit(); } else { ListVotes(); exit(); } } if ($form->{'cancel_list'}) { ListVotes(); exit(); } if ($form->{'vote_data'}) #if edit/add data has been given { if ($form->{'vid'}) { CollectFormData($form); ShowVote($form->{'vid'}); } else { if (CollectFormData($form) == -1) { AddEditVote(undef, $form); exit;} else { ListVotes(); exit; } } } elsif ($form->{'vid'}) { if (CheckIfClosed($form->{'vid'}) == 0 && $form->{'vote'} ne undef) { DoVote($form->{'vote'}, $form->{'vid'}); ShowVote($form->{'vid'}); exit; } elsif ($form->{'del'}) { DeleteVote($form->{'vid'}); ListVotes(); exit; } elsif ($form->{'edit'}) { AddEditVote($form->{'vid'}); exit; } else { ShowVote($form->{'vid'}); exit; } } else { if ($form->{'add'}) { AddEditVote(); exit; } elsif ($form->{'addoptbutton'} || $form->{'deloptbutton'}) { AddEditVote(); exit; } else { ListVotes(); exit; } } } ################################################################# # # CollectFormData(): # # Collects the data which has been inserted with Edit/Add # and inserts/uptates (to) database. # ################################################################# sub CollectFormData { my($data, $expiration, @vars, %voteinfo, $cnt, @var, $err, %varinfo, $varcnt); $data = shift; if ($data->{'vid'}) { $voteinfo{'vid'} = $data->{'vid'}; if ($data->{'expday'} eq undef) { $voteinfo{'expiration'} = undef; } elsif ($data->{'expmonth'} eq undef) { $voteinfo{'expiration'} = undef; } elsif ($data->{'expyear'} eq undef) { $voteinfo{'expiration'} = undef; } else { $voteinfo{'expiration'} = local_date2utc_epoch('00', $data->{'expmin'}, $data->{'exphour'}, $data->{'expday'}, $data->{'expmonth'}, $data->{'expyear'}); } if ($data->{'status'} eq 'open') { $voteinfo{'status'} = 1; } elsif ($data->{'status'} eq 'cancel') { $voteinfo{'status'} = 0; } else { $voteinfo{'status'} = undef; } } else { $data->{'vid'} = undef; if ($data->{'topic'} eq undef) { $err = 1; } else { $voteinfo{'topic'} = $data->{'topic'}; } if ($data->{'expday'} eq undef) { $voteinfo{'expiration'} = time() + 1209600; } elsif ($data->{'expmonth'} eq undef) { $voteinfo{'expiration'} = time() + 1209600; } elsif ($data->{'expyear'} eq undef) { $voteinfo{'expiration'} = time() + 1209600; } else { $voteinfo{'expiration'} = local_date2utc_epoch('00', $data->{'expmin'}, $data->{'exphour'}, $data->{'expday'}, $data->{'expmonth'}, $data->{'expyear'}); } if ($data->{'description'} ne undef) { $voteinfo{'description'} = $data->{'description'}; } else { $voteinfo{'description'} = undef; } $voteinfo{'status'} = 1; $voteinfo{'organizer'} = $USER; $voteinfo{'date'} = time(); for ($cnt = 0, $varcnt = 0; $data->{"var$cnt"} ne undef; $cnt++) { if ($data->{"var$cnt"}) { $var[$cnt] = $data->{"var$cnt"}; $varcnt++} else { $var[$cnt] = undef; } } if ($varcnt < 2) {$err = 1; } } if ($err == 1) { return -1; } if ($voteinfo{'vid'} eq undef) { lock_tables('WRITE', $VOTES); db_insert($VOTES, \%voteinfo); db_list("SELECT LAST_INSERT_ID(vid) as vid from $VOTES"); while (my $ref = $sth->fetchrow_hashref()) { $varinfo{'vid'} = $ref->{'vid'}; } db_end(); unlock_tables(); lock_tables('WRITE', $VOTE_VARS); $cnt = 0; @var = prepare_fordb(@var); foreach my $key (keys %varinfo) { $varinfo{$key} = prepare_fordb($varinfo{$key}); } foreach (@var) { if ($_ ne undef) { $varinfo{'var'} = $_; $varinfo{'varid'} = $cnt++; db_insert($VOTE_VARS, \%varinfo); } else { next; } } unlock_tables(); } else { lock_tables('WRITE', $VOTES); foreach my $key (keys %varinfo) { $varinfo{$key} = prepare_fordb($varinfo{$key}); } db_update($VOTES, \%voteinfo, "vid = '$voteinfo{'vid'}'"); unlock_tables(); } } ################################################################ # # AddEditVote(): # # Generates Add/Edit pages. # ################################################################ sub AddEditVote { my($vid, $tmpl, $votelist, $paiva, $kuukausi, $vuosi, $tunti, $minuutti, %voteinfo, $ref, @vars, $cnt, @var, $varcnt); $vid = shift; $tmpl = template_tulostus("$config{'theme'}_votelist"); $votelist .= starttable(cellpadding => 5, cellspacing => 1, gridcolor => 'black', width => '60%', align => 'center'); if (!$vid) { $votelist .= startrow(). column(class => 'blackTitle', bgcolor => '#AABFD7', colspan => '2', content => $trans->gettext('Add new vote')). endrow(); $votelist .= startrow(). column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Topic')); if (!$form->{'topic'}) { $votelist .= column(class => 'content', bgcolor => 'white', content => textfield(name => "topic", size => "30")); } else { $votelist .= column(class => 'content', bgcolor => 'white', content => textfield(name => "topic", size => "30", value => $form->{'topic'})); } $votelist .= endrow(); $votelist .= startrow(). column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Description')); if (!$form->{'description'}) { $votelist .= column(class => 'content', bgcolor => 'white', content => textarea(name => "description", cols => "30", rows => "4", wrap => 'VIRTUAL')); } else { $votelist .= column(class => 'content', bgcolor => 'white', content => textarea(name => "description", cols => "30", rows => "4", wrap => 'VIRTUAL', value => $form->{'description'})); } $votelist .= endrow(); $votelist .= startrow(); (undef,$minuutti,$tunti,$paiva,$kuukausi,$vuosi) = utc_epoch2date(time() + 1209600); if ($form->{'expday'}) { $paiva = $form->{'expday'}; } if ($form->{'expmonth'}) { $kuukausi = $form->{'expmonth'}; } if ($form->{'expyear'}) { $vuosi = $form->{'expyear'}; } if ($form->{'exphour'}) { $tunti = $form->{'exphour'}; } if ($form->{'expmin'}) { $minuutti = $form->{'expmin'}; } else { $minuutti = '00'; } $votelist .= column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Expires')). column(class => 'content', bgcolor => 'white', content => dropdownmenu(name => 'expday', order => ['01'..'31'], default => $paiva). '.'. dropdownmenu(name => 'expmonth', order => ['01'..'12'], default => $kuukausi). '.'. dropdownmenu(name => 'expyear', order => [utc_epoch2date_string(time(), '%y')-2..utc_epoch2date_string(time(), '%y')+10], default => $vuosi). ' '. dropdownmenu(name => 'exphour', order => ['00'..'23'], default => $tunti). ':'. dropdownmenu(name => 'expmin', order => ['00'..'59'], default => $minuutti) ); $votelist .= endrow(); $votelist .= endtable('hasgrid'); for ($cnt = 0, $varcnt = 0; $cnt < (($form->{'varnr'} + 1)) && $form->{'varnr'} ne undef; $cnt++) { if ($form->{"var$cnt"} ne undef) { if (($form->{"del$cnt"} ne undef && $form->{'deloptbutton'} ne undef)) { next; } else { $var[$varcnt++] = $form->{"var$cnt"}; } } else { $var[$cnt] = undef; } } if ($form->{'addopt'} ne undef && $form->{'deloptbutton'} eq undef) { $var[$varcnt] = $form->{'addopt'}; $var[$varcnt + 1] = undef; } $votelist .= (tag('br') x 3); $votelist .= starttable(cellpadding => 5, cellspacing => 1, gridcolor => 'black', width => '60%', align => 'center'); $votelist .= startrow(); if (@var > 0) { $votelist .= column(class => 'blackTitle', bgcolor => '#AABFD7', colspan => '3', content => $trans->gettext('Add options')); } else { $votelist .= column(class => 'blackTitle', bgcolor => '#AABFD7', colspan => '2', content => $trans->gettext('Add options')); } $votelist .= endrow(); $votelist .= hiddenfield("varnr", $varcnt); for ($cnt = 0; $var[$cnt] ne undef; $cnt++) { $votelist .= startrow(). column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Option').' '.($cnt + 1).' '). column(class => 'content', bgcolor => 'white', content => textfield(name => "var$cnt", value => $var[$cnt], size => '30').' '). column(class => 'content', bgcolor => 'white', content => checkbox(name => "del$cnt", value => 'delete').' '); $votelist .= endrow(); } $votelist .= startrow(). column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Insert option')). column(class => 'content', bgcolor => 'white', content => textfield(name => "addopt", size => "30").' '. picbutton(form => 'form_vote', name => 'addoptbutton', image => 'add.gif').' '); if (@var > 0) { $votelist .= column(class => 'content', bgcolor => 'white', content => picbutton(form => 'form_vote', name => 'deloptbutton', image => 'delete.gif').' '); } $votelist .= endrow(); } else { lock_tables('READ', $VOTES, $VOTE_VARS); db_list("SELECT * from $VOTES, $VOTE_VARS where $VOTES.vid = $VOTE_VARS.vid and $VOTES.vid = $vid order by $VOTE_VARS.varid"); for ($cnt = 0; $ref = $sth->fetchrow_hashref(); $cnt++) { $voteinfo{'status'} = $ref->{'STATUS'}; $voteinfo{'topic'} = $ref->{'TOPIC'}; $voteinfo{'description'} = $ref->{'DESCRIPTION'}; $voteinfo{'expiration'} = $ref->{'EXPIRATION'}; $voteinfo{'vid'} = $ref->{'VID'}; $vars[$cnt] = $ref->{'VAR'}; } db_end(); unlock_tables(); if ($voteinfo{'status'} == 1) { $voteinfo{'status'} = 'open'; } else { $voteinfo{'status'} = 'closed'; } $votelist .= hiddenfield("vid", $vid); $votelist .= startrow(). column(class => 'blackTitle', bgcolor => '#AABFD7', colspan => '2', content => $trans->gettext('Edit vote')). endrow(); $votelist .= startrow(). column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Topic')). column(class => 'content', bgcolor => 'white', content => $voteinfo{'topic'}.' '); $votelist .= endrow(); $votelist .= startrow(). column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Description')). column(class => 'content', bgcolor => 'white', content => itag_replacer(html_escape($voteinfo{'description'})).' '); $votelist .= endrow(); $votelist .= startrow(). column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Status')). column(class => 'content', bgcolor => 'white', content => dropdownmenu(name => 'status', order => ['open', 'closed'], default => $voteinfo{'status'})); $votelist .= endrow(); $votelist .= startrow(); (undef,$minuutti,$tunti,$paiva,$kuukausi,undef) = utc_epoch2date($voteinfo{'expiration'}); (undef, undef, undef, undef, undef, $vuosi) = utc_epoch2date(time()); $votelist .= column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Expires')). column(class => 'content', bgcolor => 'white', content => dropdownmenu(name => 'expday', order => ['01'..'31'], default => $paiva). '.'. dropdownmenu(name => 'expmonth', order => ['01'..'12'], default => $kuukausi). '.'. dropdownmenu(name => 'expyear', order => [$vuosi-2..$vuosi+10], default => utc_epoch2date_string($voteinfo{'expiration'}, '%y')). ' '. dropdownmenu(name => 'exphour', order => ['00'..'23'], default => $tunti). ':'. dropdownmenu(name => 'expmin', order => ['00'..'59'], default => $minuutti) ); $votelist .= endrow(); for ($cnt = 0; $cnt < @vars; $cnt++) { $votelist .= startrow(). column(class => 'content', bgcolor => '#CEDFE8', content => $trans->gettext('Option').' '.($cnt + 1)). column(class => 'content', bgcolor => 'white', content => $vars[$cnt].' '); $votelist .= endrow(); } } $votelist .= endtable('hasgrid'); $votelist .= tag('br') x 2; $votelist .= ' ' x 1; $votelist .= picbutton(form => 'form_vote', name => 'vote_data', image => 'save.gif'); $votelist .= ' ' x 2; $votelist .= picbutton(form => 'form_vote', name => 'cancel_show', image => 'cancel.gif'); $tmpl =~ s/<>/$votelist/m; print_header('pragma'); print($tmpl); } ################################################################# # # DeleteVote(): # # Deletes votes, returns 0 if error (if $vid eq undef or # user does not have admin rights). # ################################################################# sub DeleteVote { my($vid); $vid = shift; if (CheckIfAdmin() == 0 || $vid eq undef) { return 0; } else { db_delete($VOTES, "vid = $vid"); db_delete($VOTE_VARS, "vid = $vid"); db_delete($VOTE_USERS, "vid = $vid"); return 1; } } ################################################################# # # CountResults(): # # Counts the vote results and returns them. # ################################################################# sub CountResults { my($vid, @result, @varnames, $ref, $cnt, $all); $vid = shift; lock_tables('READ',$VOTE_VARS); db_list("select varid, var, result from $VOTE_VARS where vid = $vid order by varid"); while ($ref = $sth->fetchrow_hashref()) { $result[$ref->{'varid'}] = $ref->{'result'}; $varnames[$ref->{'varid'}] = $ref->{'var'}; } db_end(); unlock_tables(); for ($cnt = 0; $cnt < @result; $cnt++) { $all += $result[$cnt]; } return (\@result, \@varnames, $all); } ################################################################# # # CountVars(): # # Counts the number of vars. # ################################################################# sub CountVars { my($vid, $cnt, $ref); $vid = shift; lock_tables('READ', $VOTE_VARS); db_list("select distinct count(varid) from $VOTE_VARS where vid = $vid"); $ref = $sth->fetchrow_arrayref(); db_end(); unlock_tables(); return $ref->[0]; } ################################################################# # # GenerateResults(): # # Generates the vote results and returns them. # ################################################################# sub GenerateResults { my($vid, $results, $varnames, $varcnt, $cnt, $str, $cnt2, $res_all); $vid = shift; ($results, $varnames, $res_all) = CountResults($vid); if ($res_all == 0) { $str .= starttable(cellpadding => 5, cellspacing => 1, gridcolor => 'black', width => '85%', align => 'center'). startrow(). column(class => 'blackTitle', bgcolor => '#AABFD7', content => $trans->gettext('Vote results')). endrow(). startrow(). column(class => 'content', bgcolor => 'white', content => $trans->gettext('No one has voted yet')). endrow(). endtable('hasgrid'); return $str; } else { $str .= starttable(cellpadding => 5, cellspacing => 1, gridcolor => 'black', width => '85%', align => 'center'); $str .= startrow(). column(class => 'blackTitle', bgcolor => '#AABFD7', colspan => '2', content => $trans->gettext('Vote results')); $str .= endrow(); $varcnt = CountVars($vid); for ($cnt = 0; $cnt < $varcnt; $cnt++) { if (!@$varnames[$cnt]) { if (!(@$varnames[$cnt] = GetVarName($vid, $cnt))) { next; } else { @$results[$cnt] = 0; } } $str .= startrow(). column(class => 'content', bgcolor => '#CEDFE8', content => @$varnames[$cnt]). column(class => 'content', bgcolor => 'white', content => sprintf("%s %d% (%d %s)", GenerateChart(int((@$results[$cnt]/$res_all * 100) + 0.5)), int((@$results[$cnt]/$res_all * 100) + 0.5), @$results[$cnt], $trans->gettext('votes'))). endrow(); } $str .= startrow(). column(class => 'content', bgcolor => '#AABFD7', colspan => '2', align => 'center', content => sprintf($trans->gettext("Total votes: %d"), $res_all)). endrow(); $str .= endtable('hasgrid'); } return $str; } ################################################################# # # GenerateChart(): # # Generates the chart and returns it. # ################################################################# sub GenerateChart { my($value, $chart, $img, $height, $width, $alt); $value = shift; $img = "$config{'loc_pictures'}/pixel_black.gif"; $height = '7'; $width = ($value * 2); $chart = image(src => $img, height => $height, width => $width); return $chart; } ################################################################# # # GetVarName(): # # Returns the content of voting variable. # ################################################################# sub GetVarName { my($vid, $varid, $ref, $varname); $vid = shift; $varid = shift; lock_tables('READ', $VOTE_VARS); db_list("select var from $VOTE_VARS where vid = $vid and varid = '".prepare_fordb($varid)."'"); while ($ref = $sth->fetchrow_hashref()) { $varname = $ref->{'var'}; } db_end(); unlock_tables(); return $varname; } ################################################################# # # DoVote(): # # Inserts users vote record to database, checks the needed # stuff before doing so. # ################################################################# sub DoVote { my($varid, $vid); $varid = shift; $vid = shift; if (CheckIfVoted($vid) == 1) { return 0; } lock_tables('WRITE', $VOTE_USERS, $VOTE_VARS); db_insert($VOTE_USERS, {vid => $vid, user => $USER, time => time()}); db_list("update $VOTE_VARS set result = \(result + 1\) where vid = $vid and varid = '".prepare_fordb($varid)."'"); db_end(); unlock_tables(); return 1; } ################################################################# # # CheckIfVoted(): # # Checks if user is already voted. # ################################################################# sub CheckIfVoted { my($vid, $val); $vid = shift; lock_tables('READ', $VOTE_USERS); db_list("select * from $VOTE_USERS where vid = '$vid' and USER = '$USER'"); if (!$sth->fetchrow_hashref()) { $val = 0; } else { $val = 1; } db_end(); unlock_tables(); return $val; } ################################################################# # # CheckIfClosed(): # # Checks if the vote is closed/open # ################################################################# sub CheckIfClosed { my($vid, $val, $ref); $vid = shift; lock_tables('READ', $VOTES); db_list("select EXPIRATION, STATUS from $VOTES where VID = '$vid'"); while (($ref = $sth->fetchrow_hashref())) { if ($ref->{'EXPIRATION'} < time() || $ref->{'STATUS'} == 0) { $val = 1; } else { $val = 0; } } db_end(); unlock_tables(); return $val; } ################################################################# # # CheckIfExist(): # # Cheks if any vote exists in db. # ################################################################# sub CheckIfExist { my($ref, $val); lock_tables('READ', $VOTES); db_list("select * from $VOTES"); if (!$sth->fetchrow_hashref()) { $val = 0; } else { $val = 1; } db_end(); unlock_tables(); return $val; } ################################################################# # # CheckIfAdmin(): # # Checks if the user haves admin rights. # ################################################################# sub CheckIfAdmin { if ($admin_status =~ /V0/) { return 1; } else { return 0; } } ################################################################# # # ShowVote(): # # Shows the vote information. # ################################################################# sub ShowVote { my($tmpl, %votelist, @date, $status, @expiration, $vid, @vars, $rbuttons, %varnames, $ref, $cnt, $isvoted, $isopen, $results, $topic, %voteinfo); $vid = shift; $isvoted = CheckIfVoted($vid); $tmpl = template_tulostus("$config{'theme'}_voteshow"); if ($isvoted != 0 || CheckIfAdmin() == 1 || CheckIfClosed($vid) == 1) { $votelist{'voteresults'} = GenerateResults($vid); } lock_tables('READ', $VOTES, $USERS_DB); db_list("select $VOTES.*, $USERS_DB.INFO from $VOTES, $USERS_DB where $VOTES.vid = $vid and $VOTES.organizer = $USERS_DB.nimi order by date"); for ($cnt = 0; ($ref = $sth->fetchrow_hashref()); $cnt++) { @date = utc_epoch2date($ref->{'DATE'}); @expiration = utc_epoch2date($ref->{'EXPIRATION'}); $topic = $ref->{'TOPIC'}; if ($ref->{'STATUS'} == 0 || $ref->{'EXPIRATION'} < time()) { $status = $trans->gettext('closed'); $isopen = 0; } else { $status = $trans->gettext('open'); $isopen = 1; } $voteinfo{'date'} = "$date[5]/$date[4]/$date[3] $date[2]:$date[1]".' '; $voteinfo{'organizer'} = tagged('a', {href => "profile_view.html?ID=$form->{'ID'}&auth=$form->{'auth'}&showprofile=$ref->{'ORGANIZER'}", content => $ref->{'INFO'}.' '}); $voteinfo{'topic'} = $ref->{'TOPIC'}.' '; $voteinfo{'description'} = itag_replacer(html_escape($ref->{'DESCRIPTION'})).' '; $voteinfo{'status'} = $status.' '; $voteinfo{'expiration'} = "$expiration[5]/$expiration[4]/$expiration[3] $expiration[2]:$expiration[1]".' '; } db_end(); unlock_tables(); if ($cnt == 0) { ListVotes(); exit; } if (CheckIfAdmin() == 1) { $voteinfo{'nappor'} = picbutton(form => 'form_vote', name => 'edit', image => 'edit.gif'). '  '. picbutton(form => 'form_vote', name => 'del', image => 'delete.gif'); } lock_tables('READ', $VOTE_VARS); db_list("select * from $VOTE_VARS where vid = '$vid' order by varid"); while (my $ref = $sth->fetchrow_hashref()) { push (@vars, $ref->{'VARID'}); $varnames{$ref->{'VARID'}} = $ref->{'VAR'}; } db_end(); unlock_tables(); if ($isvoted == 0 && $isopen == 1) { $rbuttons = radiobuttons(name => 'vote', values => \@vars); $votelist{'voteoptions'} .= starttable(cellpadding => 5, cellspacing => 1, width => '85%', align => 'center', gridcolor => 'black'); $votelist{'voteoptions'} .= startrow(). column(class => 'blackTitle', bgcolor => '#AABFD7', content => $topic); $votelist{'voteoptions'} .= endrow(); foreach (@vars) { $votelist{'voteoptions'} .= startrow(). column(class => 'content', bgcolor => 'white', content => $rbuttons->{$_}.'  '. $varnames{$_}). endrow(); } $votelist{'voteoptions'} .= endtable('hasgrid'); $votelist{'voteoptions'} .= tag('br') x 2; $votelist{'voteoptions'} .= picbutton(form => 'form_vote', name => 'sendvote', image => 'vote.gif'); } if ($votelist{'voteoptions'}) { $votelist{'main'} .= $votelist{'voteoptions'}. (tag('br') x 2); } if ($votelist{'voteresults'}) { $votelist{'main'} .= $votelist{'voteresults'}. (tag('br') x 2); } $votelist{'main'} .= picbutton(form => 'form_vote', name => 'cancel_list', image => 'back2.gif'); $tmpl =~ s/<>/$vid/m; $tmpl =~ s/<>/$voteinfo{'date'}/m; $tmpl =~ s/<>/$voteinfo{'organizer'}/m; $tmpl =~ s/<>/$voteinfo{'topic'}/m; $tmpl =~ s/<>/$voteinfo{'description'}/m; $tmpl =~ s/<>/$voteinfo{'status'}/m; $tmpl =~ s/<>/$voteinfo{'expiration'}/m; $tmpl =~ s/<>/$voteinfo{'nappor'}/m; $tmpl =~ s/<>/$votelist{'main'}/m; print_header('pragma'); print($tmpl); } ################################################################# # # ListVotes(): # # Lists all the votes. # ################################################################# sub ListVotes { my($tmpl, $votelist, @date, $status, $vnr, $limit, $vtotal, $recnext); $limit = '25'; $tmpl = template_tulostus("$config{'theme'}_votelist"); if (CheckIfAdmin() == 1) { $votelist .= starttable(cellpadding => 5, cellspacing => 2, width => '80%', align => 'center'). startrow(). column(class => 'content', align => 'right', content => picbutton(form => 'form_vote', name => 'add', image => 'addvote.gif')). endrow(); $votelist .= endtable(); } if (CheckIfExist()) { $votelist .= starttable(cellpadding => 5, cellspacing => 1, gridcolor => 'black', width => '80%', align => 'center'). startrow(); foreach($trans->gettext('Posted'), $trans->gettext('Organizer'), $trans->gettext('Topic'), $trans->gettext('Status')) { $votelist .= column(class => "blackTitle", bgcolor => $htmlcolors->{'title'}, content => $_); } $votelist .= endrow(); } else { $votelist .= starttable(cellpadding => 5, cellspacing => 1, width => '80%', align => 'center'). startrow(); $votelist .= column(class => "blackTitle", bgcolor => 'white', align => 'center', content => $trans->gettext('No votes in database')). endrow(); } $vtotal = row_count($VOTES); if ($form->{'next'} && $form->{'vnr'} ne undef) { $vnr = ($form->{'vnr'} + $limit); } elsif ($form->{'previous'} && $form->{'vnr'} ne undef) { $vnr = ($form->{'vnr'} - $limit); } else { $vnr = 0; } lock_tables('READ', $VOTES, $USERS_DB); db_list("select $VOTES.*, $USERS_DB.INFO from $VOTES, $USERS_DB where $VOTES.organizer = $USERS_DB.nimi order by date desc limit ".prepare_fordb($vnr).",$limit"); while (my $ref = $sth->fetchrow_hashref()) { @date = utc_epoch2date($ref->{'DATE'}); if (!$ref->{'STATUS'}) { $status = $trans->gettext('closed'); } elsif ($ref->{'EXPIRATION'} < time()) { $status = $trans->gettext('closed'); } else { $status = $trans->gettext('open'); } $votelist .= startrow(). column(class => 'content', bgcolor => 'white', content => "$date[5]/$date[4]/$date[3] $date[2]:$date[1]".' '). column(class => 'content', bgcolor => 'white', content => tagged('a', {href => "profile_view.html?ID=$form->{'ID'}&auth=$form->{'auth'}&showprofile=$ref->{'ORGANIZER'}", content => $ref->{'INFO'}.' '})). column(class => 'content', bgcolor => 'white', content => tagged('a', {href => "$SCRIPT?ID=$form->{'ID'}&auth=$form->{'auth'}&vid=$ref->{'VID'}&grouptool=$form->{'grouptool'}", content => $ref->{'TOPIC'}.' '})). column(class => 'content', bgcolor => 'white', content => $status.' '). endrow(); } db_end(); unlock_tables(); $votelist .= endtable('hasgrid'); $votelist .= "\n".hiddenfield('vnr', $vnr)."\n". tag('br'); $votelist .= starttable(cellpadding => 5, cellspacing => 2, width => '75%', align => 'center'); $votelist .= startrow(); if (($vnr + 1) > $limit) { $votelist .= column(class => 'content', align => 'left', content => picbutton(form => 'form_vote', name => 'previous', image => 'previous.gif').' '. "(".($vnr - $limit + 1)." - $vnr)"); } if (($vnr + $limit) < $vtotal) { if(($vtotal - ($vnr + $limit)) < $limit) { $recnext = ($vnr + $limit) + ($vtotal - ($vnr + $limit)); } else { $recnext = ($vnr + ($limit * 2)); } $votelist .= column(class => 'content', align => 'right', content => picbutton(form => 'form_vote', name => 'next', image => 'next.gif').' '. '('.($vnr + $limit + 1)." - ".$recnext.')'); } $votelist .= endrow(); $votelist .= endtable(); $tmpl =~ s/<>/$votelist/m; print_header('pragma'); print($tmpl); } db_end('disconnect'); exit(0); ############################### # Lue template ja tulosta se # ############################### sub template_tulostus { my (@categories,$gen,$stuff,$ref,$fulltemplate,$template); $template = shift; $ref = get_template('maintemplate',$template); $ref->{'maintemplate'} =~ s/<>/$ref->{$template}/m; $fulltemplate = $ref->{'maintemplate'}; if ($GROUPACTIVOR) {$fulltemplate = create_buttons($fulltemplate, 'Group', 'Voting', $form);} else {$fulltemplate = create_buttons($fulltemplate, 'Resources', 'Voting', $form);} $ref = get_template('js_doClock', 'js_help', 'js_gotosite'); my $javascripts = $ref->{'js_doClock'}.$ref->{'js_help'}.$ref->{'js_gotosite'}; $fulltemplate =~ s/<>/$javascripts/m; $fulltemplate =~ s/<>/minea/gm; $fulltemplate =~ s/<>/MimerDesk\: $APPLICATION/m; $fulltemplate =~ s/<>/$form->{'grouptool'}/mg; $fulltemplate = replace_tags($fulltemplate, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); $fulltemplate = add_popups($fulltemplate, $USER, $form->{'auth'}, $form->{'ID'}); return $fulltemplate; }