Bug 7642 - fix the lost display of tag sizes

This fixes the display of different sized words for differently popular
tags. It is a bit of a refactor of that part of the system, moving
logic to more sensible places (and removing an unused method on the
way.)

Note that it isn't an attempt to reproduce what was there previously,
just to do something similar, and in an easier to change fashion.

Sponsored-By: New Zealand Educational Institute
Signed-off-by: Marc Veron <veron@veron.ch>
Works as expected.
This commit is contained in:
Robin Sheat 2012-09-29 11:34:32 +12:00 committed by Paul Poulain
parent f58a6f4e10
commit 79e0fb6fd6
6 changed files with 189 additions and 91 deletions

View file

@ -33,21 +33,21 @@ use vars qw($ext_dict $select_all @fields);
BEGIN {
$VERSION = 3.07.00.049;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
&get_tag &get_tags &get_tag_rows
&add_tags &add_tag
&delete_tag_row_by_id
&remove_tag
&delete_tag_rows_by_ids
&rectify_weights
&get_approval_rows
&blacklist
&whitelist
&is_approved
&approval_counts
&get_count_by_tag_status
&get_filters
);
@EXPORT_OK = qw(
&get_tag &get_tags &get_tag_rows
&add_tags &add_tag
&delete_tag_row_by_id
&remove_tag
&delete_tag_rows_by_ids
&get_approval_rows
&blacklist
&whitelist
&is_approved
&approval_counts
&get_count_by_tag_status
&get_filters
stratify_tags
);
# %EXPORT_TAGS = ();
$ext_dict = C4::Context->preference('TagsExternalDictionary');
if ($debug) {
@ -490,33 +490,6 @@ sub get_tag { # by tag_id
return $sth->fetchrow_hashref;
}
sub rectify_weights {
my $dbh = C4::Context->dbh;
my $sth;
my $query = "
SELECT term,biblionumber,count(*) as count
FROM tags_all
";
(@_) and $query .= " WHERE term =? ";
$query .= " GROUP BY term,biblionumber ";
$sth = $dbh->prepare($query);
if (@_) {
$sth->execute(shift);
} else {
$sth->execute();
}
my $results = $sth->fetchall_arrayref({}) or return;
my %tally = ();
foreach (@$results) {
_set_weight($_->{count},$_->{term},$_->{biblionumber});
$tally{$_->{term}} += $_->{count};
}
foreach (keys %tally) {
_set_weight_total($tally{$_},$_);
}
return ($results,\%tally);
}
sub increment_weights {
increment_weight(@_);
increment_weight_total(shift);
@ -594,6 +567,43 @@ sub add_tag { # biblionumber,term,[borrowernumber,approvernumber]
}
}
# This takes a set of tags, as returned by C<get_approval_rows> and divides
# them up into a number of "strata" based on their weight. This is useful
# to display them in a number of different sizes.
#
# Usage:
# ($min, $max) = stratify_tags($strata, $tags);
# $stratum: the number of divisions you want
# $tags: the tags, as provided by get_approval_rows
# $min: the minumum stratum value
# $max: the maximum stratum value. This may be the same as $min if there
# is only one weight. Beware of divide by zeros.
# This will add a field to the tag called "stratum" containing the calculated
# value.
sub stratify_tags {
my ( $strata, $tags ) = @_;
my ( $min, $max );
foreach (@$tags) {
my $w = $_->{weight_total};
$min = $w if ( !defined($min) || $min > $w );
$max = $w if ( !defined($max) || $max < $w );
}
# normalise min to zero
$max = $max - $min;
my $orig_min = $min;
$min = 0;
# if min and max are the same, just make it 1
my $span = ( $strata - 1 ) / ( $max || 1 );
foreach (@$tags) {
my $w = $_->{weight_total};
$_->{stratum} = int( ( $w - $orig_min ) * $span );
}
return ( $min, $max );
}
1;
__END__

View file

@ -2657,3 +2657,44 @@ body#opac-main #opacmainuserblockmobile {
.mobile_only {
display : none;
}
/* different sizes for different tags in opac-tags.tt */
.tagweight0 {
font-size: 12px;
}
.tagweight1 {
font-size: 14px;
}
.tagweight2 {
font-size: 16px;
}
.tagweight3 {
font-size: 18px;
}
.tagweight4 {
font-size: 20px;
}
.tagweight5 {
font-size: 22px;
}
.tagweight6 {
font-size: 24px;
}
.tagweight7 {
font-size: 26px;
}
.tagweight8 {
font-size: 28px;
}
.tagweight9 {
font-size: 30px;
}

View file

@ -2767,3 +2767,44 @@ body#opac-main #opacmainuserblockmobile {
.mobile_only {
display : none;
}
/* different sizes for different tags in opac-tags.tt */
.tagweight0 {
font-size: 12px;
}
.tagweight1 {
font-size: 14px;
}
.tagweight2 {
font-size: 16px;
}
.tagweight3 {
font-size: 18px;
}
.tagweight4 {
font-size: 20px;
}
.tagweight5 {
font-size: 22px;
}
.tagweight6 {
font-size: 24px;
}
.tagweight7 {
font-size: 26px;
}
.tagweight8 {
font-size: 28px;
}
.tagweight9 {
font-size: 30px;
}

View file

@ -21,26 +21,6 @@
</style>
<script type="text/javascript">
//<![CDATA[
var fontsizes = new Array (12,14,16,18,20,22,24,26,28,30);
var fontcount = fontsizes.length;
var maxcloudweight = 1;
$(document).ready(function() {
// $('#tagcloud').css('background-color','lightgrey');
// $('#tagcloud .tag').css('border','1px solid black');
$('#tagcloud .tag').each(function() {
if (maxcloudweight < this.title) { maxcloudweight = this.title; }
// have to run through the set of tags once to get the max: cannot be combined w/ 2nd pass
});
$('#tagcloud .tag').each(function(i) {
var pos = this.id;
var weight = this.title; // "cloudweight"
weight = (! weight) ? 1 : (weight > maxcloudweight) ? maxcloudweight : weight ;
var index = Math.round(fontcount * weight/maxcloudweight) - 1;
index = (! index ) ? 0 : ( index > fontcount ) ? fontcount : index ;
var newsize = fontsizes[index];
// alert(pos+ " (" +i+ ") weight = " +weight+ " of " +maxcloudweight+ ", fontsize[" +index+ " of " +fontcount+ "] = " +newsize);
$('#' + pos).css({"font-size":(newsize + 'px'), display:"inline"});
});
$("#mytagst").tablesorter({[% IF ( dateformat == 'metric' ) %]
dateFormat: 'uk',[% END %]
widgets : ['zebra'],
@ -90,7 +70,7 @@
[% IF ( TAGLOOP ) %]
<div id="tagcloud">
[% FOREACH TAGLOO IN TAGLOOP %]
<span class="tag" id="tag[% loop.count %]">
<span class="tag tagweight[% TAGLOO.stratum %]" id="tag[% loop.count %]" style="display:inline;">
<a href="/cgi-bin/koha/opac-search.pl?tag=[% TAGLOO.term |url %]&amp;q=[% TAGLOO.term |url %]">
[% TAGLOO.term |html %]</a>
<span class="tagweight">[% TAGLOO.weight_total %]</span>

View file

@ -41,7 +41,7 @@ use C4::Debug;
use C4::Output qw(:html :ajax pagination_bar);
use C4::Scrubber;
use C4::Biblio;
use C4::Tags qw(add_tag get_approval_rows get_tag_rows remove_tag);
use C4::Tags qw(add_tag get_approval_rows get_tag_rows remove_tag stratify_tags);
use Data::Dumper;
@ -263,34 +263,9 @@ if ($add_op) {
$arghash->{biblionumber} = $arg;
}
$results = get_approval_rows($arghash);
stratify_tags(10, $results); # work out the differents sizes for things
my $count = scalar @$results;
$template->param(TAGLOOP_COUNT => $count, mine => $mine);
# Here we make a halfhearted attempt to separate the tags into "strata" based on weight_total
# FIXME: code4lib probably has a better algorithm, iirc
# FIXME: when we get a better algorithm, move to C4
my $maxstrata = 5;
my $strata = 1;
my $previous = 0;
my $chunk = ($count/$maxstrata)/2;
my $total = 0;
my %cloud;
foreach (reverse @$results) {
my $current = $_->{weight_total};
$total++;
$cloud{$strata}++;
if ($current == $previous) {
$_->{cloudweight} = $strata;
next;
}
if ($strata < $maxstrata and
($cloud{$strata} > $chunk or
$count-$total <= $maxstrata-$strata)) {
$strata++;
}
$_->{cloudweight} = $strata;
$previous = $current;
}
}
(scalar @errors ) and $template->param(ERRORS => \@errors);
my @orderedresult = sort { uc($a->{'term'}) cmp uc($b->{'term'}) } @$results;

View file

@ -6,9 +6,60 @@
use strict;
use warnings;
use Test::More tests => 1;
use Test::More tests => 30;
BEGIN {
use_ok('C4::Tags');
}
# Simple 'sequential 5' test
my $tags = make_tags(1,2,3,4,5);
my @strata = (0,1,2,3,4);
my ($min, $max) = C4::Tags::stratify_tags(5, $tags);
check_tag_strata($tags, \@strata, 'Sequential 5');
is($min, 0, 'Sequential 5 min');
is($max, 4, 'Sequential 5 max');
# Reverse test - should have the same results as previous
$tags = make_tags(5,4,3,2,1);
@strata = (4,3,2,1,0);
($min, $max) = C4::Tags::stratify_tags(5, $tags);
check_tag_strata($tags, \@strata, 'Reverse Sequential 5');
is($min, 0, 'Sequential 5 min');
is($max, 4, 'Sequential 5 max');
# All the same test - should all have the same results
$tags = make_tags(4,4,4,4,4);
@strata = (0,0,0,0,0);
($min, $max) = C4::Tags::stratify_tags(5, $tags);
check_tag_strata($tags, \@strata, 'All The Same');
is($min, 0, 'Sequential 5 min');
is($max, 0, 'Sequential 5 max');
# Some the same, some different
$tags = make_tags(1,2,2,3,3,8);
@strata = (0,0,0,1,1,4);
($min, $max) = C4::Tags::stratify_tags(5, $tags);
check_tag_strata($tags, \@strata, 'All The Same');
is($min, 0, 'Sequential 5 min');
is($max, 7, 'Sequential 5 max');
# Runs tests against the results
sub check_tag_strata {
my ($tags, $expected, $name) = @_;
foreach my $t (@$tags) {
my $w = $t->{weight_total};
my $s = $t->{stratum};
is($s, shift @$expected, $name . " - $w ($s)");
}
}
# Makes some tags with just enough info to test
sub make_tags {
my @res;
while (@_) {
push @res, { weight_total => shift @_ };
}
return \@res;
}