diff options
author | Yorhel <git@yorhel.nl> | 2012-01-10 15:44:32 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2012-01-10 15:44:32 +0100 |
commit | 7f48af30ed456688391e46b1a577c5907cdd7be7 (patch) | |
tree | 468dde27c392eab5af23e05e87f452a622c6316e /lib/VNDB | |
parent | 3ea8eef12df3e84645b59301211cd233b6c27a2d (diff) |
Allow one fractional digit for VN votes
The interface to set a non-integer vote isn't very nice, but at least it
works. Or so I hope.
Diffstat (limited to 'lib/VNDB')
-rw-r--r-- | lib/VNDB/DB/ULists.pm | 11 | ||||
-rw-r--r-- | lib/VNDB/Func.pm | 7 | ||||
-rw-r--r-- | lib/VNDB/Handler/ULists.pm | 12 | ||||
-rw-r--r-- | lib/VNDB/Handler/Users.pm | 6 | ||||
-rw-r--r-- | lib/VNDB/Handler/VNPage.pm | 5 | ||||
-rw-r--r-- | lib/VNDB/Util/BrowseHTML.pm | 2 | ||||
-rw-r--r-- | lib/VNDB/Util/CommonHTML.pm | 20 |
7 files changed, 35 insertions, 28 deletions
diff --git a/lib/VNDB/DB/ULists.pm b/lib/VNDB/DB/ULists.pm index e1c22594..a0cf4f18 100644 --- a/lib/VNDB/DB/ULists.pm +++ b/lib/VNDB/DB/ULists.pm @@ -235,17 +235,18 @@ sub dbVoteGet { # Arguments: (uid|vid), id, use_ignore_list -# Returns an arrayref with 10 elements containing the number of votes for index+1 +# Returns an arrayref with 10 elements containing the [ count(vote), sum(vote) ] +# for votes in the range of ($index+0.5) .. ($index+1.4) sub dbVoteStats { my($self, $col, $id, $ign) = @_; my $u = $self->authInfo->{id}; - my $r = [ qw| 0 0 0 0 0 0 0 0 0 0 | ]; - $r->[$_->{vote}-1] = $_->{votes} for (@{$self->dbAll(q| - SELECT vote, COUNT(vote) as votes + my $r = [ map [0,0], 0..9 ]; + $r->[$_->{idx}] = [ $_->{votes}, $_->{total} ] for (@{$self->dbAll(q| + SELECT (vote::numeric/10)::int-1 AS idx, COUNT(vote) as votes, SUM(vote) AS total FROM votes !s !W - GROUP BY vote|, + GROUP BY (vote::numeric/10)::int|, $ign ? 'JOIN users ON id = uid AND (NOT ign_votes'.($u?sprintf(' OR id = %d',$u):'').')' : '', $col ? { '!s = ?' => [ $col, $id ] } : {}, )}); diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm index 91659f13..4f564a18 100644 --- a/lib/VNDB/Func.pm +++ b/lib/VNDB/Func.pm @@ -9,7 +9,7 @@ use POSIX 'strftime', 'ceil', 'floor'; use VNDBUtil; our @EXPORT = (@VNDBUtil::EXPORT, qw| clearfloat cssicon tagscore mt minage fil_parse fil_serialize parenttags - childtags charspoil imgpath imgurl + childtags charspoil imgpath imgurl fmtvote |); @@ -195,5 +195,10 @@ sub imgurl { } +# Formats a vote number. +sub fmtvote { + return !$_[0] ? '-' : $_[0] % 10 == 0 ? $_[0]/10 : sprintf '%.1f', $_[0]/10; +} + 1; diff --git a/lib/VNDB/Handler/ULists.pm b/lib/VNDB/Handler/ULists.pm index 1c82d982..6e28a61a 100644 --- a/lib/VNDB/Handler/ULists.pm +++ b/lib/VNDB/Handler/ULists.pm @@ -27,12 +27,12 @@ sub vnvote { return if !$self->authCheckCode; my $f = $self->formValidate( - { get => 'v', enum => [ -1, 1..10 ] } + { get => 'v', regex => qr/^(-1|([1-9]|10)(\.[0-9])?)$/ } ); - return $self->resNotFound if $f->{_err}; + return $self->resNotFound if $f->{_err} || ($f->{v} != -1 && ($f->{v} > 10 || $f->{v} < 1)); $self->dbVoteDel($uid, $id) if $f->{v} == -1; - $self->dbVoteAdd($id, $uid, $f->{v}) if $f->{v} > 0; + $self->dbVoteAdd($id, $uid, $f->{v}*10) if $f->{v} > 0; $self->resRedirect('/v'.$id, 'temp'); } @@ -138,7 +138,7 @@ sub votelist { my @vid = grep $_ && $_ > 0, @{$frm->{vid}}; if(!$frm->{_err} && @vid && $frm->{batchedit} > -2) { $self->dbVoteDel($id, \@vid) if $frm->{batchedit} == -1; - $self->dbVoteAdd(\@vid, $id, $frm->{batchedit}) if $frm->{batchedit} >= 0; + $self->dbVoteAdd(\@vid, $id, $frm->{batchedit}*10) if $frm->{batchedit} > 0; } } @@ -191,7 +191,7 @@ sub votelist { input type => 'checkbox', name => 'vid', value => $l->{vid} if $own; txt ' '.$self->{l10n}->date($l->{date}); end; - td class => 'tc2', $l->{vote}; + td class => 'tc2', fmtvote $l->{vote}; td class => 'tc3'; a href => $type eq 'v' ? ("/u$l->{uid}", $l->{username}) : ("/v$l->{vid}", shorten $l->{title}, 100); end; @@ -460,7 +460,7 @@ sub _vnlist_browse { $txt = qq|<b class="todo">$txt</b>| if $obtained < $total; lit $txt; end; - td class => 'tc8', $i->{vote} || '-'; + td class => 'tc8', fmtvote $i->{vote}; end 'tr'; for (@{$i->{rels}}) { diff --git a/lib/VNDB/Handler/Users.pm b/lib/VNDB/Handler/Users.pm index cc5ce28c..cc42f4d0 100644 --- a/lib/VNDB/Handler/Users.pm +++ b/lib/VNDB/Handler/Users.pm @@ -76,10 +76,10 @@ sub userpage { } elsif($votes) { my($total, $count) = (0, 0); for (1..@$votes) { - $total += $_*$votes->[$_-1]; - $count += $votes->[$_-1]; + $count += $votes->[$_-1][0]; + $total += $votes->[$_-1][1]; } - lit mt '_userpage_votes_item', "/u$uid/votes", $count, sprintf '%.2f', $total/$count; + lit mt '_userpage_votes_item', "/u$uid/votes", $count, sprintf '%.2f', $total/$count/10; } else { txt '-'; } diff --git a/lib/VNDB/Handler/VNPage.pm b/lib/VNDB/Handler/VNPage.pm index 1f73c3be..f71baf30 100644 --- a/lib/VNDB/Handler/VNPage.pm +++ b/lib/VNDB/Handler/VNPage.pm @@ -381,9 +381,10 @@ sub _useroptions { td; if($vote || !$wish) { Select id => 'votesel', name => $self->authGetCode("/v$v->{id}/vote"); - option $vote ? mt '_vnpage_uopt_voted', $vote->{vote} : mt '_vnpage_uopt_novote'; + option value => -3, $vote ? mt '_vnpage_uopt_voted', fmtvote($vote->{vote}) : mt '_vnpage_uopt_novote'; optgroup label => $vote ? mt '_vnpage_uopt_changevote' : mt '_vnpage_uopt_dovote'; option value => $_, "$_ (".mt("_vote_$_").')' for (reverse 1..10); + option value => -2, mt '_vnpage_uopt_othvote'; end; option value => -1, mt '_vnpage_uopt_delvote' if $vote; end; @@ -566,7 +567,7 @@ sub _stats { my $stats = $self->dbVoteStats(vid => $v->{id}, 1); div class => 'mainbox'; h1 mt '_vnpage_stats'; - if(!grep $_ > 0, @$stats) { + if(!grep $_->[0] > 0, @$stats) { p mt '_vnpage_stats_none'; } else { $self->htmlVoteStats(v => $v, $stats); diff --git a/lib/VNDB/Util/BrowseHTML.pm b/lib/VNDB/Util/BrowseHTML.pm index 6d6cdeda..993f8746 100644 --- a/lib/VNDB/Util/BrowseHTML.pm +++ b/lib/VNDB/Util/BrowseHTML.pm @@ -202,7 +202,7 @@ sub htmlBrowseVN { end; td class => 'tc5', sprintf '%.2f', ($l->{c_popularity}||0)*100; td class => 'tc6'; - txt sprintf '%.2f', $l->{c_rating}||0; + txt sprintf '%.2f', ($l->{c_rating}||0)/10; b class => 'grayedout', sprintf ' (%d)', $l->{c_votecount}; end; end 'tr'; diff --git a/lib/VNDB/Util/CommonHTML.pm b/lib/VNDB/Util/CommonHTML.pm index 73e2c5ba..8f908b19 100644 --- a/lib/VNDB/Util/CommonHTML.pm +++ b/lib/VNDB/Util/CommonHTML.pm @@ -338,11 +338,11 @@ sub htmlItemMessage { sub htmlVoteStats { my($self, $type, $obj, $stats) = @_; - my($max, $count, $total) = (0, 0); + my($max, $count, $total) = (0, 0, 0); for (0..$#$stats) { - $max = $stats->[$_] if $stats->[$_] > $max; - $count += $stats->[$_]; - $total += $stats->[$_]*($_+1); + $max = $stats->[$_][0] if $stats->[$_][0] > $max; + $count += $stats->[$_][0]; + $total += $stats->[$_][1]; } div class => 'votestats'; table class => 'votegraph'; @@ -350,15 +350,15 @@ sub htmlVoteStats { td colspan => 2, mt '_votestats_title'; end; end; tfoot; Tr; - td colspan => 2, mt('_votestats_sum', $count, sprintf('%.2f', $total/$count)) - .($type eq 'v' ? ' ('.mt('_vote_'.(ceil($total/$count-1)||1)).')' : ''); + td colspan => 2, mt('_votestats_sum', $count, sprintf('%.2f', $total/$count/10)) + .($type eq 'v' ? ' ('.mt('_vote_'.(ceil($total/$count/10-1)||1)).')' : ''); end; end; for (reverse 0..$#$stats) { Tr; td class => 'number', $_+1; td class => 'graph'; - div style => 'width: '.($stats->[$_] ? $stats->[$_]/$max*250 : 0).'px', ' '; - txt $stats->[$_]; + div style => 'width: '.($stats->[$_][0]/$max*250).'px', ' '; + txt $stats->[$_][0]; end; end; } @@ -392,7 +392,7 @@ sub htmlVoteStats { a href => "/u$recent->[$_]{uid}", $recent->[$_]{username}; } end; - td $recent->[$_]{vote}; + td fmtvote $recent->[$_]{vote}; td $self->{l10n}->date($recent->[$_]{date}); end; } @@ -404,7 +404,7 @@ sub htmlVoteStats { div; h3 mt '_votestats_rank_title'; p mt '_votestats_rank_pop', $obj->{p_ranking}, sprintf '%.2f', ($obj->{c_popularity}||0)*100; - p mt '_votestats_rank_rat', $obj->{r_ranking}, sprintf '%.2f', $obj->{c_rating}; + p mt '_votestats_rank_rat', $obj->{r_ranking}, sprintf '%.2f', $obj->{c_rating}/10; end; } end 'div'; |