Changeset 4084 for trunk/VotePlugin/lib/Foswiki/Plugins/VotePlugin/Core.pm
- Timestamp:
- 06/10/09 16:37:46 (3 years ago)
- Location:
- trunk/VotePlugin/lib/Foswiki
- Files:
-
- 1 edited
- 1 moved
-
. (moved) (moved from trunk/VotePlugin/lib/TWiki)
-
Plugins/VotePlugin/Core.pm (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/VotePlugin/lib/Foswiki/Plugins/VotePlugin/Core.pm
r1340 r4084 11 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 12 # 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 14 14 # http://www.gnu.org/copyleft/gpl.html 15 15 16 16 ############################################################################### 17 package TWiki::Plugins::VotePlugin::Core;17 package Foswiki::Plugins::VotePlugin::Core; 18 18 19 19 ############################################################################### … … 25 25 use CGI; 26 26 27 $debug = 0; # toggle me27 $debug = 0; # toggle me 28 28 29 29 ############################################################################### 30 30 sub 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%'); 35 35 36 36 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') 41 44 || ''; 42 45 43 while ( $defaults =~ s/^\s*(\w+)=\"(.*?)\"//) {46 while ( $defaults =~ s/^\s*(\w+)=\"(.*?)\"// ) { 44 47 $params->{$1} = $2 unless defined $params->{$1}; 45 48 } 46 49 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}; 54 57 my $canReset = canReset($id); 55 58 … … 57 60 58 61 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 64 68 # Compatibility 65 69 my $format = ''; 66 if ( $params->{style} =~ /perc/) {70 if ( $params->{style} =~ /perc/ ) { 67 71 $format .= '$perc% '; 68 72 } 69 if ( $params->{style} =~ /total/) {73 if ( $params->{style} =~ /total/ ) { 70 74 $format .= '($freq)'; 71 75 } 72 if ( $params->{style} =~ /sum/) {76 if ( $params->{style} =~ /sum/ ) { 73 77 $format .= '$sum votes'; 74 78 } … … 80 84 81 85 # 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 ); 90 99 } 91 100 92 101 my $n = 1; 93 102 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+$/ ) { 105 120 return inlineError("Expected integer width for stars$n="); 106 121 } 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 { 113 133 last; 114 134 } … … 117 137 118 138 # 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() ); 121 141 } 122 142 123 143 my $mess; 124 144 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 ); 128 148 $mess = "$id has been reset"; 129 149 } … … 131 151 132 152 # read in the votes 133 my $lines = getVoteData( $web, $topic, $id, $isGlobal, $saveto);153 my $lines = getVoteData( $web, $topic, $id, $isGlobal, $saveto ); 134 154 my %votes; 135 155 136 156 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]; 142 162 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 ); 148 168 $votes{$voter}{$vid}{$row} = [ $choice, $weight, $date ]; 149 169 } 150 170 } 151 } elsif (!$saveto && $line =~ /^([^\|]+)\|([^\|]+)\|(.*?)\|(.+)$/) { 171 } 172 elsif ( !$saveto && $line =~ /^([^\|]+)\|([^\|]+)\|(.*?)\|(.+)$/ ) { 173 152 174 # Old format - compatibility only 153 my $date = $1;154 my $voter = $2;175 my $date = $1; 176 my $voter = $2; 155 177 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 ); 160 182 $votes{$voter}{$id}{$row} = [ $choice, $weight, $date ]; 161 183 } … … 176 198 # leaf value, so the frequency of that value should be the same as the 177 199 # 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} } ) { 188 211 my $choice = $votes{$voter}{$vid}{$key}->[0]; 189 212 my $weight = $votes{$voter}{$vid}{$key}->[1]; … … 192 215 $items{$key}{$vid} = 1; 193 216 $voteSum += $weight; 194 if ( $choice =~ /^[\d.]+$/) {217 if ( $choice =~ /^[\d.]+$/ ) { 195 218 $totalRate{$key} += $choice * $weight; 196 219 $rateSum += $choice * $weight; … … 203 226 my $act; 204 227 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' ); 208 232 } 209 233 … … 213 237 vote_isGlobal => $isGlobal, 214 238 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 ); 219 243 220 244 # Do we need a submit button? … … 226 250 my $row; 227 251 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} } ) ); 230 254 231 255 # avg_num_votes: The average number of votes of all items that have … … 234 258 # have num_votes>0) 235 259 my $avg_num_votes = $numItems ? $voteSum / $numItems : 0; 236 my $avg_rating = $voteSum ? $rateSum / $voteSum: 0;260 my $avg_rating = $voteSum ? $rateSum / $voteSum : 0; 237 261 my $myLastVote = 238 $votes{ getIdent($isSecret, $isOpen)}{$id}{$key}->[0] || 0;262 $votes{ getIdent( $isSecret, $isOpen ) }{$id}{$key}->[0] || 0; 239 263 my $mean = 0; 240 if ( $totalVotes{$key}) {264 if ( $totalVotes{$key} ) { 241 265 $mean = $totalRate{$key} / $totalVotes{$key}; 242 266 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} ); 246 271 } 247 272 } 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 ); 251 280 } 252 281 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} } ) { 257 291 $opts .= CGI::option($optionName); 258 292 } 259 my $o = { name => 'vote_data_' .$key, size => 1 };293 my $o = { name => 'vote_data_' . $key, size => 1 }; 260 294 unless ($needSubmit) { 261 295 $o->{onchange} = 'javascript: submit()'; 262 296 } 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 ); 272 311 if ($submit) { 273 312 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 278 318 # 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; 283 326 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(); 289 337 } 290 338 291 339 if ($canReset) { 292 340 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' } ) 299 351 . CGI::end_form(); 300 352 } 301 353 $result =~ s/\$reset/ $mess /g; 302 } else { 354 } 355 else { 303 356 $result =~ s/\$reset//g; 304 357 } 305 358 306 359 # Render tables and remove newlines 307 # so we can embed votes in TWikitables308 $result = TWiki::Func::renderText($result);360 # so we can embed votes in tables 361 $result = Foswiki::Func::renderText($result); 309 362 $result =~ s/\n//g; 310 363 return $result; … … 317 370 my $id = shift; 318 371 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 ) ) { 322 375 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 ) ) { 326 379 $ide = quotemeta($ide); 327 380 $ide =~ s/\\\*/.*/g; 328 381 $ide =~ s/\\\?/./g; 329 if ( $id =~ /^$ide$/) {382 if ( $id =~ /^$ide$/ ) { 330 383 return 1; 331 384 } … … 337 390 ############################################################################### 338 391 sub registerVote { 392 339 393 #print STDERR "called registerVote()\n"; 340 394 341 395 # 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'); 344 398 345 399 return unless defined $id; … … 347 401 my $web; 348 402 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(); 351 405 my $isSecret = $query->param('vote_isSecret') || 0; 352 406 353 407 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 357 412 358 413 # 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'); 361 416 my $weight = 1; 362 417 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" ) { 372 427 $weight = $2 / 100.0; 373 428 } … … 380 435 my $voteData = "|$id|$ident|$weight|"; 381 436 my @v; 382 foreach my $key ( $query->param()) {437 foreach my $key ( $query->param() ) { 383 438 my $val = $query->param($key); 384 439 next unless $key =~ s/^vote_data_//; 385 440 push @v, "$key=$val"; 386 441 } 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 391 450 # 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 ); 394 453 } 395 454 } 396 455 397 456 sub saveVotesData { 398 my ( $web, $topic, $id, $isGlobal, $saveto, $voteData) = @_;457 my ( $web, $topic, $id, $isGlobal, $saveto, $voteData ) = @_; 399 458 if ($saveto) { 400 459 my $text = ''; 401 my $meta = '';460 my $meta = ''; 402 461 $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 411 472 # open and lock the votes 412 open( VOTES, ">>$votesFile") || die "cannot append $votesFile";413 flock( VOTES, LOCK_EX);# wait for exclusive rights414 seek( VOTES, 0, 2);# seek EOF in case someone else appended415 # stuff while we were waiting473 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 416 477 print VOTES $voteData; 478 417 479 # unlock and close 418 flock( VOTES, LOCK_UN);480 flock( VOTES, LOCK_UN ); 419 481 close VOTES; 420 482 } … … 422 484 423 485 sub clearVotesData { 424 my ( $web, $topic, $id, $isGlobal, $saveto) = @_;486 my ( $web, $topic, $id, $isGlobal, $saveto ) = @_; 425 487 if ($saveto) { 426 488 my $text = ''; 427 my $meta = '';489 my $meta = ''; 428 490 $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 ); 432 494 } 433 495 $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 ); 437 500 unlink($votesFile) || die "cannot remove $votesFile: $!"; 438 501 } … … 440 503 441 504 sub getVoteData { 442 my ( $web, $topic, $id, $isGlobal, $saveto) = @_;505 my ( $web, $topic, $id, $isGlobal, $saveto ) = @_; 443 506 444 507 my $lines = ''; 445 508 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 ) ) { 448 511 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" ) ) { 454 518 local $/ = undef; 455 519 $lines = <F>; … … 460 524 } 461 525 462 463 526 ############################################################################### 464 527 sub 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'); 468 531 my $flatweb = $web; 469 532 $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" : '' ); 473 537 $votesFile = normalizeFileName($votesFile); 474 538 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" ) ) { 484 550 local $/; 485 551 print G <F>; 486 close(G); close(F); 552 close(G); 553 close(F); 487 554 unlink $oldVotesFile; 488 555 } … … 497 564 my $fileName = shift; 498 565 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"); 508 576 return $fileName; 509 577 } 510 578 511 512 579 ############################################################################### 513 580 sub _getLocalDate { 514 my ( $sec, $min, $hour, $mday, $mon, $year) = localtime(time());515 $year = sprintf( "%.4u", $year + 1900);# Y2K fix516 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 ); 517 584 return $date; 518 585 } … … 520 587 ############################################################################### 521 588 sub inlineError { 522 return '<span class=" twikiAlert">Error: '.$_[0].'</span>';589 return '<span class="foswikiAlert">Error: ' . $_[0] . '</span>'; 523 590 } 524 591 … … 526 593 sub expandFormattingTokens { 527 594 my $text = shift; 528 $text =~ s/\$quote/\'/go; # Compatibility595 $text =~ s/\$quote/\'/go; # Compatibility 529 596 530 597 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 { 534 602 $text =~ s/\$n\(\)/\n/gs; 535 603 $text =~ s/\$n\b/\n$1/gs; … … 545 613 ############################################################################### 546 614 sub getIdent { 547 my ( $isSecret, $isOpen) = @_;548 549 my $user = TWiki::Func::getWikiUserName();615 my ( $isSecret, $isOpen ) = @_; 616 617 my $user = Foswiki::Func::getWikiUserName(); 550 618 551 619 my $ident; … … 553 621 if ($isOpen) { 554 622 my $date = _getLocalDate(); 555 $ident = ($ENV{REMOTE_ADDR}||'').",$user,$date"; 556 } else { 623 $ident = ( $ENV{REMOTE_ADDR} || '' ) . ",$user,$date"; 624 } 625 else { 557 626 $ident = $user; 558 627 } … … 560 629 if ($isSecret) { 561 630 return md5_base64($ident); 562 } else { 631 } 632 else { 563 633 return $ident; 564 634 } … … 567 637 ############################################################################### 568 638 sub 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}; 572 643 my $totty = $totalVotes || 0; 573 my $row = $prompt->{format};644 my $row = $prompt->{format}; 574 645 $row =~ s/\$key/$key/g; 575 646 $row =~ s/\$prompt/$select/g; 576 647 $row =~ s/\$sum/$totty/; 577 648 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 { 581 654 my $score = $keyValueFreq->{$value} || 0; 582 655 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} ); 585 658 $bar =~ s/\$option/$value/; 586 659 $bar =~ s/\$perc/$perc/g; … … 594 667 595 668 sub _makeBar { 596 my ( $width, $perc, $params) = @_;669 my ( $width, $perc, $params ) = @_; 597 670 $width = $width || $params->{width} || 300; 598 671 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 ); 602 678 $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 ); 607 686 $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 ); 612 695 return $graph; 613 696 } … … 615 698 ############################################################################### 616 699 sub showLineOfStars { 617 my ( $form, $prompt, $submit, $needSubmit, $act,618 $mean, $myLast, $total) =@_;700 my ( $form, $prompt, $submit, $needSubmit, $act, $mean, $myLast, $total ) = 701 @_; 619 702 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} ); 624 707 $row =~ s/\$key/$prompt->{name}/g; 625 708 $row =~ s/\$sum/$total/g; … … 627 710 $row =~ s/\$perc/$perc/g; 628 711 $row =~ s/\$mylast/$myLast/g; 629 require TWiki::Contrib::RatingContrib;712 require Foswiki::Contrib::RatingContrib; 630 713 my $eAttrs = {}; 631 if ($submit && !$needSubmit) { 632 $eAttrs ={633 onChange =>714 715 if ( $submit && !$needSubmit ) { 716 $eAttrs = { onChange => 634 717 "javascript: document.getElementById('$form').submit()" }; 635 718 } 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 ); 639 724 640 725 $row =~ s/\$(small|large)/$ul/g; … … 644 729 645 730 sub isTrue { 646 my ( $value, $default ) = @_;731 my ( $value, $default ) = @_; 647 732 648 733 $default ||= 0; 649 734 650 return $default unless defined( $value);735 return $default unless defined($value); 651 736 652 737 $value =~ s/^\s*(.*?)\s*$/$1/gi; … … 654 739 $value =~ s/no//gi; 655 740 $value =~ s/false//gi; 656 return ( $value) ? 1 : 0;741 return ($value) ? 1 : 0; 657 742 } 658 743
Note: See TracChangeset
for help on using the changeset viewer.
