Ignore:
Timestamp:
06/10/09 16:37:46 (3 years ago)
Author:
CrawfordCurrie
Message:

Item8010: ported to Foswiki

Location:
trunk/VotePlugin/lib/Foswiki
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/VotePlugin/lib/Foswiki/Plugins/VotePlugin/Core.pm

    r1340 r4084  
    1111# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1212# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    13 # GNU General Public License for more details, published at  
     13# GNU General Public License for more details, published at 
    1414# http://www.gnu.org/copyleft/gpl.html 
    1515 
    1616############################################################################### 
    17 package TWiki::Plugins::VotePlugin::Core; 
     17package Foswiki::Plugins::VotePlugin::Core; 
    1818 
    1919############################################################################### 
     
    2525use CGI; 
    2626 
    27 $debug = 0; # toggle me 
     27$debug = 0;    # toggle me 
    2828 
    2929############################################################################### 
    3030sub handleVote { 
    31     my ($session, $params, $topic, $web) = @_; 
    32  
    33     $web = TWiki::Func::expandCommonVariables('%BASEWEB%'); 
    34     $topic = TWiki::Func::expandCommonVariables('%BASETOPIC%'); 
     31    my ( $session, $params, $topic, $web ) = @_; 
     32 
     33    $web   = Foswiki::Func::expandCommonVariables('%BASEWEB%'); 
     34    $topic = Foswiki::Func::expandCommonVariables('%BASETOPIC%'); 
    3535 
    3636    unless ($pubUrlPath) { 
    37         $pubUrlPath = TWiki::Func::getPubUrlPath().'/'.TWiki::Func::getTwikiWebname().'/VotePlugin'; 
    38     } 
    39  
    40     my $defaults = TWiki::Func::getPreferencesValue('VOTEPLUGIN_DEFAULTS') 
     37        $pubUrlPath = 
     38            Foswiki::Func::getPubUrlPath() . '/' 
     39          . $Foswiki::cfg{SystemWebName} 
     40          . '/VotePlugin'; 
     41    } 
     42 
     43    my $defaults = Foswiki::Func::getPreferencesValue('VOTEPLUGIN_DEFAULTS') 
    4144      || ''; 
    4245 
    43     while ($defaults =~ s/^\s*(\w+)=\"(.*?)\"//) { 
     46    while ( $defaults =~ s/^\s*(\w+)=\"(.*?)\"// ) { 
    4447        $params->{$1} = $2 unless defined $params->{$1}; 
    4548    } 
    4649 
    47     my $id =       defined($params->{id}) ? $params->{id} : '_default'; 
    48     my $isGlobal = isTrue($params->{global}, 0); 
    49     my $isOpen =   isTrue($params->{open}, 1); 
    50     my $isSecret = isTrue($params->{secret}, 1); 
    51     my $bayesian = isTrue($params->{bayesian}, 0); 
    52     my $submit =   isTrue($params->{submit}, 1); 
    53     my $saveto $params->{saveto}; 
     50    my $id = defined( $params->{id} ) ? $params->{id} : '_default'; 
     51    my $isGlobal = isTrue( $params->{global},   0 ); 
     52    my $isOpen   = isTrue( $params->{open},     1 ); 
     53    my $isSecret = isTrue( $params->{secret},   1 ); 
     54    my $bayesian = isTrue( $params->{bayesian}, 0 ); 
     55    my $submit   = isTrue( $params->{submit},   1 ); 
     56    my $saveto   = $params->{saveto}; 
    5457    my $canReset = canReset($id); 
    5558 
     
    5760 
    5861    my $defaultStarsFormat = 
    59       '| $key$reset | $small<div class="star-info">Score: $score, My vote: $mylast, Total votes: $sum</div> |'; 
    60     my $defaultSelectFormat =  '| $key$reset | $prompt | $bars |'; 
    61     my $defaultChartFormat = '<div>$bar(300) $option $perc% ($score)</div>'; 
    62  
    63     if (defined($params->{style})) { 
     62'| $key$reset | $small<div class="star-info">Score: $score, My vote: $mylast, Total votes: $sum</div> |'; 
     63    my $defaultSelectFormat = '| $key$reset | $prompt | $bars |'; 
     64    my $defaultChartFormat  = '<div>$bar(300) $option $perc% ($score)</div>'; 
     65 
     66    if ( defined( $params->{style} ) ) { 
     67 
    6468        # Compatibility 
    6569        my $format = ''; 
    66         if ($params->{style} =~ /perc/) { 
     70        if ( $params->{style} =~ /perc/ ) { 
    6771            $format .= '$perc% '; 
    6872        } 
    69         if ($params->{style} =~ /total/) { 
     73        if ( $params->{style} =~ /total/ ) { 
    7074            $format .= '($freq)'; 
    7175        } 
    72         if ($params->{style} =~ /sum/) { 
     76        if ( $params->{style} =~ /sum/ ) { 
    7377            $format .= '$sum votes'; 
    7478        } 
     
    8084 
    8185    # Compatibility 
    82     if (defined($params->{select})) { 
    83         push(@prompts, { 
    84             type => 'select', 
    85             name => expandFormattingTokens($params->{select}), 
    86             format => $defaultSelectFormat, 
    87             options => 
    88               [ map { expandFormattingTokens($_) } 
    89                   split(/\s*,\s*/, $params->{options} || '') ]}); 
     86    if ( defined( $params->{select} ) ) { 
     87        push( 
     88            @prompts, 
     89            { 
     90                type    => 'select', 
     91                name    => expandFormattingTokens( $params->{select} ), 
     92                format  => $defaultSelectFormat, 
     93                options => [ 
     94                    map { expandFormattingTokens($_) } 
     95                      split( /\s*,\s*/, $params->{options} || '' ) 
     96                ] 
     97            } 
     98        ); 
    9099    } 
    91100 
    92101    my $n = 1; 
    93102    while (1) { 
    94         if (defined($params->{"select$n"})) { 
    95             push(@prompts, { 
    96                 type => 'select', 
    97                 name => expandFormattingTokens($params->{"select$n"}), 
    98                 format => $params->{"format$n"} || $defaultSelectFormat, 
    99                 chart => $params->{"chart$n"} || $defaultChartFormat, 
    100                 options => 
    101                   [ map { expandFormattingTokens($_) } 
    102                       split(/\s*,\s*/, $params->{"options$n"} || '') ]}); 
    103         } elsif (defined($params->{"stars$n"})) { 
    104             unless (($params->{"width$n"} || 5) =~ /^\d+$/) { 
     103        if ( defined( $params->{"select$n"} ) ) { 
     104            push( 
     105                @prompts, 
     106                { 
     107                    type    => 'select', 
     108                    name    => expandFormattingTokens( $params->{"select$n"} ), 
     109                    format  => $params->{"format$n"} || $defaultSelectFormat, 
     110                    chart   => $params->{"chart$n"} || $defaultChartFormat, 
     111                    options => [ 
     112                        map { expandFormattingTokens($_) } 
     113                          split( /\s*,\s*/, $params->{"options$n"} || '' ) 
     114                    ] 
     115                } 
     116            ); 
     117        } 
     118        elsif ( defined( $params->{"stars$n"} ) ) { 
     119            unless ( ( $params->{"width$n"} || 5 ) =~ /^\d+$/ ) { 
    105120                return inlineError("Expected integer width for stars$n="); 
    106121            } 
    107             push(@prompts, { 
    108                 type => 'stars', 
    109                 name => $params->{"stars$n"}, 
    110                 format => $params->{"format$n"} || $defaultStarsFormat, 
    111                 width => $params->{"width$n"} || 5 }); 
    112         } else { 
     122            push( 
     123                @prompts, 
     124                { 
     125                    type   => 'stars', 
     126                    name   => $params->{"stars$n"}, 
     127                    format => $params->{"format$n"} || $defaultStarsFormat, 
     128                    width  => $params->{"width$n"} || 5 
     129                } 
     130            ); 
     131        } 
     132        else { 
    113133            last; 
    114134        } 
     
    117137 
    118138    # check attributes 
    119     if (!scalar(@prompts)) { 
    120         return inlineError("no prompts specified ".$params->stringify()); 
     139    if ( !scalar(@prompts) ) { 
     140        return inlineError( "no prompts specified " . $params->stringify() ); 
    121141    } 
    122142 
    123143    my $mess; 
    124144    if ($canReset) { 
    125         my $query = TWiki::Func::getCgiQuery(); 
    126         if (defined $query->param('Reset'.$id)) { 
    127             clearVotesData($web, $topic, $id, $isGlobal, $saveto); 
     145        my $query = Foswiki::Func::getCgiQuery(); 
     146        if ( defined $query->param( 'Reset' . $id ) ) { 
     147            clearVotesData( $web, $topic, $id, $isGlobal, $saveto ); 
    128148            $mess = "$id has been reset"; 
    129149        } 
     
    131151 
    132152    # read in the votes 
    133     my $lines = getVoteData($web, $topic, $id, $isGlobal, $saveto); 
     153    my $lines = getVoteData( $web, $topic, $id, $isGlobal, $saveto ); 
    134154    my %votes; 
    135155 
    136156    my %lastVote; 
    137     foreach my $line (split/\r?\n/, $lines) { 
    138         if ($line =~ /^\|(.*)\|$/) { 
    139             my @data = split(/\|/, $1); 
    140             my $vid = $data[0]; 
    141             my $voter = $data[1]; 
     157    foreach my $line ( split /\r?\n/, $lines ) { 
     158        if ( $line =~ /^\|(.*)\|$/ ) { 
     159            my @data   = split( /\|/, $1 ); 
     160            my $vid    = $data[0]; 
     161            my $voter  = $data[1]; 
    142162            my $weight = $data[2]; 
    143             my $date = (scalar(@data) > 4) ? $data[4] : ''; 
    144  
    145             foreach my $item (split(/,/, $data[3] || '')) { 
    146                 if ($item =~ /^(.+)=(.+)$/) { 
    147                     my ($row, $choice) = ($1, $2); 
     163            my $date   = ( scalar(@data) > 4 ) ? $data[4] : ''; 
     164 
     165            foreach my $item ( split( /,/, $data[3] || '' ) ) { 
     166                if ( $item =~ /^(.+)=(.+)$/ ) { 
     167                    my ( $row, $choice ) = ( $1, $2 ); 
    148168                    $votes{$voter}{$vid}{$row} = [ $choice, $weight, $date ]; 
    149169                } 
    150170            } 
    151         } elsif (!$saveto && $line =~ /^([^\|]+)\|([^\|]+)\|(.*?)\|(.+)$/) { 
     171        } 
     172        elsif ( !$saveto && $line =~ /^([^\|]+)\|([^\|]+)\|(.*?)\|(.+)$/ ) { 
     173 
    152174            # Old format - compatibility only 
    153             my $date = $1; 
    154             my $voter = $2; 
     175            my $date   = $1; 
     176            my $voter  = $2; 
    155177            my $weight = $3; 
    156             my $data = $4; 
    157             foreach my $item (split(/\|/, $data)) { 
    158                 if ($item =~ /^(.+)=(.+)$/) { 
    159                     my ($row, $choice) = ($1, $2); 
     178            my $data   = $4; 
     179            foreach my $item ( split( /\|/, $data ) ) { 
     180                if ( $item =~ /^(.+)=(.+)$/ ) { 
     181                    my ( $row, $choice ) = ( $1, $2 ); 
    160182                    $votes{$voter}{$id}{$row} = [ $choice, $weight, $date ]; 
    161183                } 
     
    176198    # leaf value, so the frequency of that value should be the same as the 
    177199    # total number of votes for the key. 
    178     my %keyValueFreq; # frequency of a specific value for a given key 
    179     my %totalVotes;   # total votes for a given key 
    180     my %totalVoters;  # how many different people voted for each key 
    181     my %totalRate;    # Total of all ratings for each key 
    182     my %items;        # Hash of id's that have the same key 
    183     my $voteSum = 0;  # Sum of the number of votes on all rated items 
    184     my $rateSum = 0;  # Sum of all ratings of rated items 
    185     foreach my $voter (keys %votes) { 
    186         foreach my $vid (keys %{$votes{$voter}}) { 
    187             foreach my $key (keys %{$votes{$voter}{$vid}}) { 
     200    my %keyValueFreq;    # frequency of a specific value for a given key 
     201    my %totalVotes;      # total votes for a given key 
     202    my %totalVoters;     # how many different people voted for each key 
     203    my %totalRate;       # Total of all ratings for each key 
     204    my %items;           # Hash of id's that have the same key 
     205    my $voteSum = 0;     # Sum of the number of votes on all rated items 
     206    my $rateSum = 0;     # Sum of all ratings of rated items 
     207 
     208    foreach my $voter ( keys %votes ) { 
     209        foreach my $vid ( keys %{ $votes{$voter} } ) { 
     210            foreach my $key ( keys %{ $votes{$voter}{$vid} } ) { 
    188211                my $choice = $votes{$voter}{$vid}{$key}->[0]; 
    189212                my $weight = $votes{$voter}{$vid}{$key}->[1]; 
     
    192215                $items{$key}{$vid} = 1; 
    193216                $voteSum += $weight; 
    194                 if ($choice =~ /^[\d.]+$/) { 
     217                if ( $choice =~ /^[\d.]+$/ ) { 
    195218                    $totalRate{$key} += $choice * $weight; 
    196219                    $rateSum += $choice * $weight; 
     
    203226    my $act; 
    204227    if ($isOpen) { 
    205         $act = TWiki::Func::getScriptUrl($web, $topic, 'view'); 
    206     } else { 
    207         $act = TWiki::Func::getScriptUrl($web, $topic, 'viewauth'); 
     228        $act = Foswiki::Func::getScriptUrl( $web, $topic, 'view' ); 
     229    } 
     230    else { 
     231        $act = Foswiki::Func::getScriptUrl( $web, $topic, 'viewauth' ); 
    208232    } 
    209233 
     
    213237        vote_isGlobal => $isGlobal, 
    214238        vote_isSecret => $isSecret, 
    215         vote_isOpen => $isOpen, 
    216         vote_saveTo => $saveto, 
    217         vote_inTopic => "$web.$topic", 
    218        ); 
     239        vote_isOpen   => $isOpen, 
     240        vote_saveTo   => $saveto, 
     241        vote_inTopic  => "$web.$topic", 
     242    ); 
    219243 
    220244    # Do we need a submit button? 
     
    226250        my $row; 
    227251 
    228         if ($prompt->{type} eq 'stars') { 
    229             my $numItems = scalar(keys(%{$items{$key}})); 
     252        if ( $prompt->{type} eq 'stars' ) { 
     253            my $numItems = scalar( keys( %{ $items{$key} } ) ); 
    230254 
    231255            # avg_num_votes: The average number of votes of all items that have 
     
    234258            # have num_votes>0) 
    235259            my $avg_num_votes = $numItems ? $voteSum / $numItems : 0; 
    236             my $avg_rating = $voteSum ? $rateSum / $voteSum : 0; 
     260            my $avg_rating    = $voteSum  ? $rateSum / $voteSum : 0; 
    237261            my $myLastVote = 
    238               $votes{getIdent($isSecret, $isOpen)}{$id}{$key}->[0] || 0; 
     262              $votes{ getIdent( $isSecret, $isOpen ) }{$id}{$key}->[0] || 0; 
    239263            my $mean = 0; 
    240             if ($totalVotes{$key}) { 
     264            if ( $totalVotes{$key} ) { 
    241265                $mean = $totalRate{$key} / $totalVotes{$key}; 
    242266                if ($bayesian) { 
    243                     $mean = ($avg_num_votes * $avg_rating + 
    244                                $totalVotes{$key} * $mean) / 
    245                                  ($avg_num_votes + $totalVotes{$key}); 
     267                    $mean = 
     268                      ( $avg_num_votes * $avg_rating + 
     269                          $totalVotes{$key} * $mean ) / 
     270                      ( $avg_num_votes + $totalVotes{$key} ); 
    246271                } 
    247272            } 
    248             push(@rows, showLineOfStars( 
    249                 $id, $prompt, $submit, $needSubmit, $act, 
    250                 $mean, $myLastVote, $totalVoters{$key} || 0)); 
     273            push( 
     274                @rows, 
     275                showLineOfStars( 
     276                    $id, $prompt, $submit, $needSubmit, $act, $mean, 
     277                    $myLastVote, $totalVoters{$key} || 0 
     278                ) 
     279            ); 
    251280        } 
    252281        else { 
    253             my $opts = CGI::option({selected=>'selected', 
    254                                     value=>''}, 'Select ...'); 
    255  
    256             foreach my $optionName (@{$prompt->{options}}) { 
     282            my $opts = CGI::option( 
     283                { 
     284                    selected => 'selected', 
     285                    value    => '' 
     286                }, 
     287                'Select ...' 
     288            ); 
     289 
     290            foreach my $optionName ( @{ $prompt->{options} } ) { 
    257291                $opts .= CGI::option($optionName); 
    258292            } 
    259             my $o = { name => 'vote_data_'.$key, size => 1 }; 
     293            my $o = { name => 'vote_data_' . $key, size => 1 }; 
    260294            unless ($needSubmit) { 
    261295                $o->{onchange} = 'javascript: submit()'; 
    262296            } 
    263             my $select = $submit ? CGI::Select($o, $opts) : ''; 
    264  
    265             push(@rows, showSelect( 
    266                 $id, $prompt, $submit, $select, $keyValueFreq{$id}{$key}, 
    267                 $totalVotes{$id}{$key}, $params)); 
    268         } 
    269     } 
    270  
    271     my $result = join($separator, @rows); 
     297            my $select = $submit ? CGI::Select( $o, $opts ) : ''; 
     298 
     299            push( 
     300                @rows, 
     301                showSelect( 
     302                    $id, $prompt, $submit, $select, 
     303                    $keyValueFreq{$id}{$key}, 
     304                    $totalVotes{$key}, $params 
     305                ) 
     306            ); 
     307        } 
     308    } 
     309 
     310    my $result = join( $separator, @rows ); 
    272311    if ($submit) { 
    273312        my $hiddens = ''; 
    274         while (my ($k, $v) = each %hidden) { 
    275             $hiddens .= CGI::input( 
    276                 { type => 'hidden', name => $k, value => $v }); 
    277         } 
     313        while ( my ( $k, $v ) = each %hidden ) { 
     314            $hiddens .= 
     315              CGI::input( { type => 'hidden', name => $k, value => $v } ); 
     316        } 
     317 
    278318        # Don't use CGI::form because it generates TML-busting newlines 
    279         my $form = CGI::start_form(-id => $id, -action => $act, 
    280                                   -method => 'post'); 
    281         $form =~ s/\r?\n//g; # CGI::start_form adds unwanted \n's 
    282         $result = $form.$hiddens.$separator.$result; 
     319        my $form = CGI::start_form( 
     320            -id     => $id, 
     321            -action => $act, 
     322            -method => 'post' 
     323        ); 
     324        $form =~ s/\r?\n//g;    # CGI::start_form adds unwanted \n's 
     325        $result = $form . $hiddens . $separator . $result; 
    283326        if ($needSubmit) { 
    284             $result .= $separator.CGI::submit( 
    285                 { name=> 'OK', value=>'OK', 
    286                   style=>'color:green'}); 
    287         } 
    288         $result .= $separator.CGI::end_form(); 
     327            $result .= $separator 
     328              . CGI::submit( 
     329                { 
     330                    name  => 'OK', 
     331                    value => 'OK', 
     332                    style => 'color:green' 
     333                } 
     334              ); 
     335        } 
     336        $result .= $separator . CGI::end_form(); 
    289337    } 
    290338 
    291339    if ($canReset) { 
    292340        if ($mess) { 
    293             $mess = CGI::span({class => 'twikiAlert'}, $mess); 
    294         } else { 
    295             $mess = CGI::start_form(-name => 'resetForm', 
    296                                     -action => ''); 
    297             $mess =~ s/\r?\n//g; # CGI::start_form adds unwanted \n's 
    298             $mess .= CGI::submit({ name => 'Reset'.$id, value=>'Reset'}) 
     341            $mess = CGI::span( { class => 'foswikiAlert' }, $mess ); 
     342        } 
     343        else { 
     344            $mess = CGI::start_form( 
     345                -name   => 'resetForm', 
     346                -action => '' 
     347            ); 
     348            $mess =~ s/\r?\n//g;    # CGI::start_form adds unwanted \n's 
     349            $mess .= 
     350                CGI::submit( { name => 'Reset' . $id, value => 'Reset' } ) 
    299351              . CGI::end_form(); 
    300352        } 
    301353        $result =~ s/\$reset/ $mess /g; 
    302     } else { 
     354    } 
     355    else { 
    303356        $result =~ s/\$reset//g; 
    304357    } 
    305358 
    306359    # Render tables and remove newlines 
    307     # so we can embed votes in TWiki tables 
    308     $result = TWiki::Func::renderText($result); 
     360    # so we can embed votes in tables 
     361    $result = Foswiki::Func::renderText($result); 
    309362    $result =~ s/\n//g; 
    310363    return $result; 
     
    317370    my $id = shift; 
    318371 
    319     my $controls = TWiki::Func::getPreferencesValue('VOTEPLUGIN_RESETTERS'); 
    320     return '' unless (defined $controls); 
    321     foreach my $control (split(/\s*;\s*/, $controls)) { 
     372    my $controls = Foswiki::Func::getPreferencesValue('VOTEPLUGIN_RESETTERS'); 
     373    return '' unless ( defined $controls ); 
     374    foreach my $control ( split( /\s*;\s*/, $controls ) ) { 
    322375        next unless $control =~ /^\s*(\w+)\s*\((.*)\)\s*$/; 
    323         my ($who, $whats) = ($1, $2); 
    324         next unless $who eq TWiki::Func::getWikiName(); 
    325         foreach my $ide (split(/\s*,\s*/, $whats)) { 
     376        my ( $who, $whats ) = ( $1, $2 ); 
     377        next unless $who eq Foswiki::Func::getWikiName(); 
     378        foreach my $ide ( split( /\s*,\s*/, $whats ) ) { 
    326379            $ide = quotemeta($ide); 
    327380            $ide =~ s/\\\*/.*/g; 
    328381            $ide =~ s/\\\?/./g; 
    329             if ($id =~ /^$ide$/) { 
     382            if ( $id =~ /^$ide$/ ) { 
    330383                return 1; 
    331384            } 
     
    337390############################################################################### 
    338391sub registerVote { 
     392 
    339393    #print STDERR "called registerVote()\n"; 
    340394 
    341395    # check parameters 
    342     my $query = TWiki::Func::getCgiQuery(); 
    343     my $id = $query->param('vote_register'); 
     396    my $query = Foswiki::Func::getCgiQuery(); 
     397    my $id    = $query->param('vote_register'); 
    344398 
    345399    return unless defined $id; 
     
    347401    my $web; 
    348402    my $topic = $query->param('vote_inTopic'); 
    349     ($web, $topic) = TWiki::Func::normalizeWebTopicName('', $topic); 
    350     my $user = TWiki::Func::getWikiUserName(); 
     403    ( $web, $topic ) = Foswiki::Func::normalizeWebTopicName( '', $topic ); 
     404    my $user = Foswiki::Func::getWikiUserName(); 
    351405    my $isSecret = $query->param('vote_isSecret') || 0; 
    352406 
    353407    my $isOpen = $query->param('vote_isOpen') || 0; 
    354     my $ident = getIdent($isSecret, $isOpen); 
    355 #    $ident = int(rand(100))  
    356 #      if $debug; # for testing 
     408    my $ident = getIdent( $isSecret, $isOpen ); 
     409 
     410    #    $ident = int(rand(100)) 
     411    #      if $debug; # for testing 
    357412 
    358413    # Apply a weighting for the voting user 
    359     my $weightsTopic = TWiki::Func::getPreferencesValue( 
    360         'VOTEPLUGIN_WEIGHTINGS'); 
     414    my $weightsTopic = 
     415      Foswiki::Func::getPreferencesValue('VOTEPLUGIN_WEIGHTINGS'); 
    361416    my $weight = 1; 
    362417    if ($weightsTopic) { 
    363         my ($wweb, $wtopic) = TWiki::Func::normalizeWebTopicName( 
    364             $web, $weightsTopic); 
    365         if (TWiki::Func::topicExists($wweb, $wtopic)) { 
    366             my ($meta, $text) = TWiki::Func::readTopic($wweb, $wtopic); 
    367             foreach my $line (split(/\n/, $text)) { 
    368                 if ($line =~ /^\|\s*(\S+)\s*\|\s*(\d+)\s*\|$/) { 
    369                     ($wweb, $wtopic) =  TWiki::Func::normalizeWebTopicName( 
    370                         undef, $1); 
    371                     if ($user eq "$wweb.$wtopic") { 
     418        my ( $wweb, $wtopic ) = 
     419          Foswiki::Func::normalizeWebTopicName( $web, $weightsTopic ); 
     420        if ( Foswiki::Func::topicExists( $wweb, $wtopic ) ) { 
     421            my ( $meta, $text ) = Foswiki::Func::readTopic( $wweb, $wtopic ); 
     422            foreach my $line ( split( /\n/, $text ) ) { 
     423                if ( $line =~ /^\|\s*(\S+)\s*\|\s*(\d+)\s*\|$/ ) { 
     424                    ( $wweb, $wtopic ) = 
     425                      Foswiki::Func::normalizeWebTopicName( undef, $1 ); 
     426                    if ( $user eq "$wweb.$wtopic" ) { 
    372427                        $weight = $2 / 100.0; 
    373428                    } 
     
    380435    my $voteData = "|$id|$ident|$weight|"; 
    381436    my @v; 
    382     foreach my $key ($query->param()) { 
     437    foreach my $key ( $query->param() ) { 
    383438        my $val = $query->param($key); 
    384439        next unless $key =~ s/^vote_data_//; 
    385440        push @v, "$key=$val"; 
    386441    } 
    387     $voteData .= join(',', @v) . '|' . _getLocalDate() . "|\n"; 
    388  
    389     saveVotesData($web, $topic, $id,  $query->param('vote_isGlobal') || 0, 
    390                   $query->param('vote_saveTo') || '', $voteData); 
     442    $voteData .= join( ',', @v ) . '|' . _getLocalDate() . "|\n"; 
     443 
     444    saveVotesData( 
     445        $web, $topic, $id, 
     446        $query->param('vote_isGlobal') || 0, 
     447        $query->param('vote_saveTo')   || '', $voteData 
     448    ); 
     449 
    391450    # invalidate cache entry 
    392     if (defined &TWiki::Cache::invalidateEntry) { 
    393         TWiki::Cache::invalidateEntry($web, $topic); 
     451    if ( defined &Foswiki::Cache::invalidateEntry ) { 
     452        Foswiki::Cache::invalidateEntry( $web, $topic ); 
    394453    } 
    395454} 
    396455 
    397456sub saveVotesData { 
    398     my ($web, $topic, $id, $isGlobal, $saveto, $voteData) = @_; 
     457    my ( $web, $topic, $id, $isGlobal, $saveto, $voteData ) = @_; 
    399458    if ($saveto) { 
    400459        my $text = ''; 
    401         my $meta = ''; 
     460        my $meta = ''; 
    402461        $saveto =~ /(.*)/; 
    403         my ($vw, $vt) = TWiki::Func::normalizeWebTopicName($web, $1); 
    404         if (TWiki::Func::topicExists($vw, $vt)) { 
    405             ( $meta, $text ) = TWiki::Func::readTopic( $vw, $vt ); 
    406         } 
    407         $text .= $voteData; 
    408         TWiki::Func::saveTopic($vw, $vt, $meta, $text, { minor => 1} ); 
    409     } else { 
    410         my $votesFile = getVotesFile($web, $topic, $id, $isGlobal); 
     462        my ( $vw, $vt ) = Foswiki::Func::normalizeWebTopicName( $web, $1 ); 
     463        if ( Foswiki::Func::topicExists( $vw, $vt ) ) { 
     464            ( $meta, $text ) = Foswiki::Func::readTopic( $vw, $vt ); 
     465        } 
     466        $text .= $voteData; 
     467        Foswiki::Func::saveTopic( $vw, $vt, $meta, $text, { minor => 1 } ); 
     468    } 
     469    else { 
     470        my $votesFile = getVotesFile( $web, $topic, $id, $isGlobal ); 
     471 
    411472        # open and lock the votes 
    412         open(VOTES, ">>$votesFile") || die "cannot append $votesFile"; 
    413         flock(VOTES, LOCK_EX); # wait for exclusive rights 
    414         seek(VOTES, 0, 2); # seek EOF in case someone else appended 
    415         # stuff while we were waiting 
     473        open( VOTES, ">>$votesFile" ) || die "cannot append $votesFile"; 
     474        flock( VOTES, LOCK_EX );    # wait for exclusive rights 
     475        seek( VOTES, 0, 2 );        # seek EOF in case someone else appended 
     476                                    # stuff while we were waiting 
    416477        print VOTES $voteData; 
     478 
    417479        # unlock and close 
    418         flock(VOTES, LOCK_UN); 
     480        flock( VOTES, LOCK_UN ); 
    419481        close VOTES; 
    420482    } 
     
    422484 
    423485sub clearVotesData { 
    424     my ($web, $topic, $id, $isGlobal, $saveto) = @_; 
     486    my ( $web, $topic, $id, $isGlobal, $saveto ) = @_; 
    425487    if ($saveto) { 
    426488        my $text = ''; 
    427         my $meta = ''; 
     489        my $meta = ''; 
    428490        $saveto =~ /(.*)/; 
    429         my ($vw, $vt) = TWiki::Func::normalizeWebTopicName($web, $1); 
    430         if (TWiki::Func::topicExists($vw, $vt)) { 
    431             ( $meta, $text ) = TWiki::Func::readTopic( $vw, $vt ); 
     491        my ( $vw, $vt ) = Foswiki::Func::normalizeWebTopicName( $web, $1 ); 
     492        if ( Foswiki::Func::topicExists( $vw, $vt ) ) { 
     493            ( $meta, $text ) = Foswiki::Func::readTopic( $vw, $vt ); 
    432494        } 
    433495        $text =~ s/(^|\n)((\|.*\||\s+)\n+)*$/$1/s; 
    434         TWiki::Func::saveTopicText($vw, $vt, $meta, $text, { minor => 1} ); 
    435     } else { 
    436         my $votesFile = getVotesFile($web, $topic, $id, $isGlobal); 
     496        Foswiki::Func::saveTopicText( $vw, $vt, $meta, $text, { minor => 1 } ); 
     497    } 
     498    else { 
     499        my $votesFile = getVotesFile( $web, $topic, $id, $isGlobal ); 
    437500        unlink($votesFile) || die "cannot remove $votesFile: $!"; 
    438501    } 
     
    440503 
    441504sub getVoteData { 
    442     my ($web, $topic, $id, $isGlobal, $saveto) = @_; 
     505    my ( $web, $topic, $id, $isGlobal, $saveto ) = @_; 
    443506 
    444507    my $lines = ''; 
    445508    if ($saveto) { 
    446         my ($vw, $vt) = TWiki::Func::normalizeWebTopicName($web, $saveto); 
    447         if (TWiki::Func::topicExists($vw, $vt)) { 
     509        my ( $vw, $vt ) = Foswiki::Func::normalizeWebTopicName( $web, $saveto ); 
     510        if ( Foswiki::Func::topicExists( $vw, $vt ) ) { 
    448511            my $meta; 
    449             ( $meta, $lines ) = TWiki::Func::readTopic( $vw, $vt ); 
    450         } 
    451     } else { 
    452         my $votesFile = getVotesFile($web, $topic, $id, $isGlobal); 
    453         if (open(F, "<$votesFile")) { 
     512            ( $meta, $lines ) = Foswiki::Func::readTopic( $vw, $vt ); 
     513        } 
     514    } 
     515    else { 
     516        my $votesFile = getVotesFile( $web, $topic, $id, $isGlobal ); 
     517        if ( open( F, "<$votesFile" ) ) { 
    454518            local $/ = undef; 
    455519            $lines = <F>; 
     
    460524} 
    461525 
    462  
    463526############################################################################### 
    464527sub getVotesFile { 
    465     my ($web, $topic, $id, $global) = @_; 
    466  
    467     my $path = TWiki::Func::getWorkArea('VotePlugin'); 
     528    my ( $web, $topic, $id, $global ) = @_; 
     529 
     530    my $path    = Foswiki::Func::getWorkArea('VotePlugin'); 
    468531    my $flatweb = $web; 
    469532    $flatweb =~ s/\//./g; 
    470     my $votesFile = $path.'/'. 
    471       ($global ? '' : "${flatweb}_${topic}_"). 
    472         ($id ? "_$id" : ''); 
     533    my $votesFile = 
     534        $path . '/' 
     535      . ( $global ? ''     : "${flatweb}_${topic}_" ) 
     536      . ( $id     ? "_$id" : '' ); 
    473537    $votesFile = normalizeFileName($votesFile); 
    474538 
    475     if (! -e $votesFile) { 
    476         my $attachPath = TWiki::Func::getPubDir()."/$web/$topic"; 
    477         my $oldVotesFile = "$attachPath/_Votes" . ($id?"_$id":"") . ".txt"; 
    478  
    479         if (!-e $oldVotesFile ) { 
    480             $oldVotesFile = "$attachPath/Votes" . ($id?"_$id":"") . ".txt"; 
    481         } 
    482  
    483         if (open(F, "<$oldVotesFile") && open(G, ">$votesFile")) { 
     539    if ( !-e $votesFile ) { 
     540        my $attachPath = Foswiki::Func::getPubDir() . "/$web/$topic"; 
     541        my $oldVotesFile = 
     542          "$attachPath/_Votes" . ( $id ? "_$id" : "" ) . ".txt"; 
     543 
     544        if ( !-e $oldVotesFile ) { 
     545            $oldVotesFile = 
     546              "$attachPath/Votes" . ( $id ? "_$id" : "" ) . ".txt"; 
     547        } 
     548 
     549        if ( open( F, "<$oldVotesFile" ) && open( G, ">$votesFile" ) ) { 
    484550            local $/; 
    485551            print G <F>; 
    486             close(G); close(F); 
     552            close(G); 
     553            close(F); 
    487554            unlink $oldVotesFile; 
    488555        } 
     
    497564    my $fileName = shift; 
    498565 
    499     if (defined &TWiki::Sandbox::normalizeFileName) { 
    500         return TWiki::Sandbox::normalizeFileName($fileName); 
    501     } 
    502  
    503     if (defined &TWiki::normalizeFileName) { 
    504         return TWiki::normalizeFileName($fileName) 
    505     } 
    506  
    507     TWiki::Func::writeWarning("normalizeFileName not found ... you live dangerous"); 
     566    if ( defined &Foswiki::Sandbox::normalizeFileName ) { 
     567        return Foswiki::Sandbox::normalizeFileName($fileName); 
     568    } 
     569 
     570    if ( defined &Foswiki::normalizeFileName ) { 
     571        return Foswiki::normalizeFileName($fileName); 
     572    } 
     573 
     574    Foswiki::Func::writeWarning( 
     575        "normalizeFileName not found ... you live dangerous"); 
    508576    return $fileName; 
    509577} 
    510578 
    511  
    512579############################################################################### 
    513580sub _getLocalDate { 
    514     my( $sec, $min, $hour, $mday, $mon, $year) = localtime(time()); 
    515     $year = sprintf("%.4u", $year + 1900);  # Y2K fix 
    516     my $date = sprintf("%.2u-%.2u-%.2u", $year, $mon, $mday); 
     581    my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime( time() ); 
     582    $year = sprintf( "%.4u", $year + 1900 );    # Y2K fix 
     583    my $date = sprintf( "%.2u-%.2u-%.2u", $year, $mon + 1, $mday ); 
    517584    return $date; 
    518585} 
     
    520587############################################################################### 
    521588sub inlineError { 
    522     return '<span class="twikiAlert">Error: '.$_[0].'</span>'; 
     589    return '<span class="foswikiAlert">Error: ' . $_[0] . '</span>'; 
    523590} 
    524591 
     
    526593sub expandFormattingTokens { 
    527594    my $text = shift; 
    528     $text =~ s/\$quote/\'/go;# Compatibility 
     595    $text =~ s/\$quote/\'/go;    # Compatibility 
    529596 
    530597    return $text; 
    531     if( defined( &TWiki::Func::decodeFormatTokens )) { 
    532         $text = TWiki::Func::decodeFormatTokens( $text ); 
    533     } else { 
     598    if ( defined(&Foswiki::Func::decodeFormatTokens) ) { 
     599        $text = Foswiki::Func::decodeFormatTokens($text); 
     600    } 
     601    else { 
    534602        $text =~ s/\$n\(\)/\n/gs; 
    535603        $text =~ s/\$n\b/\n$1/gs; 
     
    545613############################################################################### 
    546614sub getIdent { 
    547     my ($isSecret, $isOpen) = @_; 
    548  
    549     my $user = TWiki::Func::getWikiUserName(); 
     615    my ( $isSecret, $isOpen ) = @_; 
     616 
     617    my $user = Foswiki::Func::getWikiUserName(); 
    550618 
    551619    my $ident; 
     
    553621    if ($isOpen) { 
    554622        my $date = _getLocalDate(); 
    555         $ident = ($ENV{REMOTE_ADDR}||'').",$user,$date"; 
    556     } else { 
     623        $ident = ( $ENV{REMOTE_ADDR} || '' ) . ",$user,$date"; 
     624    } 
     625    else { 
    557626        $ident = $user; 
    558627    } 
     
    560629    if ($isSecret) { 
    561630        return md5_base64($ident); 
    562     } else { 
     631    } 
     632    else { 
    563633        return $ident; 
    564634    } 
     
    567637############################################################################### 
    568638sub showSelect { 
    569     my ($id, $prompt, $submit, $select, $keyValueFreq, $totalVotes, $params) = @_; 
    570  
    571     my $key = $prompt->{name}; 
     639    my ( $id, $prompt, $submit, $select, $keyValueFreq, $totalVotes, $params ) = 
     640      @_; 
     641 
     642    my $key   = $prompt->{name}; 
    572643    my $totty = $totalVotes || 0; 
    573     my $row = $prompt->{format}; 
     644    my $row   = $prompt->{format}; 
    574645    $row =~ s/\$key/$key/g; 
    575646    $row =~ s/\$prompt/$select/g; 
    576647    $row =~ s/\$sum/$totty/; 
    577648    my $bars = ''; 
    578     foreach my $value (sort {$keyValueFreq->{$b} <=> 
    579                                $keyValueFreq->{$a}} 
    580                          keys %{$keyValueFreq}) { 
     649    foreach my $value ( 
     650        sort { $keyValueFreq->{$b} <=> $keyValueFreq->{$a} } 
     651        keys %{$keyValueFreq} 
     652      ) 
     653    { 
    581654        my $score = $keyValueFreq->{$value} || 0; 
    582655 
    583         my $perc = $totty ? int(1000 * $score / $totty) / 10 : 0; 
    584         my $bar = expandFormattingTokens($prompt->{chart}); 
     656        my $perc = $totty ? int( 1000 * $score / $totty ) / 10 : 0; 
     657        my $bar = expandFormattingTokens( $prompt->{chart} ); 
    585658        $bar =~ s/\$option/$value/; 
    586659        $bar =~ s/\$perc/$perc/g; 
     
    594667 
    595668sub _makeBar { 
    596     my ($width, $perc, $params) = @_; 
     669    my ( $width, $perc, $params ) = @_; 
    597670    $width = $width || $params->{width} || 300; 
    598671    my $graph = CGI::img( 
    599         { src=>$pubUrlPath.'/leftbar.gif', 
    600           alt=>'leftbar', 
    601           height=>14}); 
     672        { 
     673            src    => $pubUrlPath . '/leftbar.gif', 
     674            alt    => 'leftbar', 
     675            height => 14 
     676        } 
     677    ); 
    602678    $graph .= CGI::img( 
    603         { src => $pubUrlPath.'/mainbar.gif', 
    604           alt => 'mainbar', 
    605           height => 14, 
    606           width => $width / 100 * $perc }); 
     679        { 
     680            src    => $pubUrlPath . '/mainbar.gif', 
     681            alt    => 'mainbar', 
     682            height => 14, 
     683            width  => $width / 100 * $perc 
     684        } 
     685    ); 
    607686    $graph .= CGI::img( 
    608         { src=>$pubUrlPath.'/rightbar.gif', 
    609           alt => 'rightbar', 
    610           #width => $width - $width / 100 * $perc, 
    611           height => 14}); 
     687        { 
     688            src => $pubUrlPath . '/rightbar.gif', 
     689            alt => 'rightbar', 
     690 
     691            #width => $width - $width / 100 * $perc, 
     692            height => 14 
     693        } 
     694    ); 
    612695    return $graph; 
    613696} 
     
    615698############################################################################### 
    616699sub showLineOfStars { 
    617     my ($form, $prompt, $submit, $needSubmit, $act, 
    618         $mean, $myLast, $total) = @_; 
     700    my ( $form, $prompt, $submit, $needSubmit, $act, $mean, $myLast, $total ) = 
     701      @_; 
    619702    my $max = $prompt->{width}; 
    620     my $perc = $total ? int(1000 * $mean / $total) / 10 : 0; 
    621  
    622     $mean = sprintf("%.3g", $mean); 
    623     my $row = expandFormattingTokens($prompt->{format}); 
     703    my $perc = $total ? int( 1000 * $mean / $total ) / 10 : 0; 
     704 
     705    $mean = sprintf( "%.3g", $mean ); 
     706    my $row = expandFormattingTokens( $prompt->{format} ); 
    624707    $row =~ s/\$key/$prompt->{name}/g; 
    625708    $row =~ s/\$sum/$total/g; 
     
    627710    $row =~ s/\$perc/$perc/g; 
    628711    $row =~ s/\$mylast/$myLast/g; 
    629     require TWiki::Contrib::RatingContrib; 
     712    require Foswiki::Contrib::RatingContrib; 
    630713    my $eAttrs = {}; 
    631     if ($submit && !$needSubmit) { 
    632         $eAttrs = { 
    633             onChange => 
     714 
     715    if ( $submit && !$needSubmit ) { 
     716        $eAttrs = { onChange => 
    634717              "javascript: document.getElementById('$form').submit()" }; 
    635718    } 
    636     my $ul = TWiki::Contrib::RatingContrib::renderRating( 
    637         'vote_data_'.$prompt->{name}, $max, ($row =~ /\$small/) ? 1 : 0, 
    638         $mean, $eAttrs); 
     719    my $ul = Foswiki::Contrib::RatingContrib::renderRating( 
     720        'vote_data_' . $prompt->{name}, 
     721        $max, ( $row =~ /\$small/ ) ? 1 : 0, 
     722        $mean, $eAttrs 
     723    ); 
    639724 
    640725    $row =~ s/\$(small|large)/$ul/g; 
     
    644729 
    645730sub isTrue { 
    646     my( $value, $default ) = @_; 
     731    my ( $value, $default ) = @_; 
    647732 
    648733    $default ||= 0; 
    649734 
    650     return $default unless defined( $value ); 
     735    return $default unless defined($value); 
    651736 
    652737    $value =~ s/^\s*(.*?)\s*$/$1/gi; 
     
    654739    $value =~ s/no//gi; 
    655740    $value =~ s/false//gi; 
    656     return ( $value ) ? 1 : 0; 
     741    return ($value) ? 1 : 0; 
    657742} 
    658743 
Note: See TracChangeset for help on using the changeset viewer.