View | Details | Raw Unified | Return to bug 7642
Collapse All | Expand All

(-)a/C4/Tags.pm (-28 / +38 lines)
Lines 39-45 BEGIN { Link Here
39
		&delete_tag_row_by_id
39
		&delete_tag_row_by_id
40
		&remove_tag
40
		&remove_tag
41
		&delete_tag_rows_by_ids
41
		&delete_tag_rows_by_ids
42
		&rectify_weights
43
		&get_approval_rows
42
		&get_approval_rows
44
		&blacklist
43
		&blacklist
45
		&whitelist
44
		&whitelist
Lines 47-52 BEGIN { Link Here
47
		&approval_counts
46
		&approval_counts
48
		&get_count_by_tag_status
47
		&get_count_by_tag_status
49
		&get_filters
48
		&get_filters
49
		stratify_tags
50
	);
50
	);
51
	# %EXPORT_TAGS = ();
51
	# %EXPORT_TAGS = ();
52
	$ext_dict = C4::Context->preference('TagsExternalDictionary');
52
	$ext_dict = C4::Context->preference('TagsExternalDictionary');
Lines 490-522 sub get_tag { # by tag_id Link Here
490
	return $sth->fetchrow_hashref;
490
	return $sth->fetchrow_hashref;
491
}
491
}
492
492
493
sub rectify_weights {
494
	my $dbh = C4::Context->dbh;
495
	my $sth;
496
	my $query = "
497
	SELECT term,biblionumber,count(*) as count
498
	FROM   tags_all
499
	";
500
	(@_) and $query .= " WHERE term =? ";
501
	$query .= " GROUP BY term,biblionumber ";
502
	$sth = $dbh->prepare($query);
503
	if (@_) {
504
		$sth->execute(shift);
505
	} else {
506
		$sth->execute();
507
	}
508
	my $results = $sth->fetchall_arrayref({}) or return undef;
509
	my %tally = ();
510
	foreach (@$results) {
511
		_set_weight($_->{count},$_->{term},$_->{biblionumber});
512
		$tally{$_->{term}} += $_->{count};
513
	}
514
	foreach (keys %tally) {
515
		_set_weight_total($tally{$_},$_);
516
	}
517
	return ($results,\%tally);
518
}
519
520
sub increment_weights {
493
sub increment_weights {
521
	increment_weight(@_);
494
	increment_weight(@_);
522
	increment_weight_total(shift);
495
	increment_weight_total(shift);
Lines 594-599 sub add_tag { # biblionumber,term,[borrowernumber,approvernumber] Link Here
594
	}
567
	}
595
}
568
}
596
569
570
# This takes a set of tags, as returned by C<get_approval_rows> and divides
571
# them up into a number of "strata" based on their weight. This is useful
572
# to display them in a number of different sizes.
573
#
574
# Usage:
575
#   ($min, $max) = stratify_tags($strata, $tags);
576
# $stratum: the number of divisions you want
577
# $tags: the tags, as provided by get_approval_rows
578
# $min: the minumum stratum value
579
# $max: the maximum stratum value. This may be the same as $min if there
580
# is only one weight. Beware of divide by zeros.
581
# This will add a field to the tag called "stratum" containing the calculated
582
# value.
583
sub stratify_tags {
584
    my ( $strata, $tags ) = @_;
585
586
    my ( $min, $max );
587
    foreach (@$tags) {
588
        my $w = $_->{weight_total};
589
        $min = $w if ( !defined($min) || $min > $w );
590
        $max = $w if ( !defined($max) || $max < $w );
591
    }
592
593
    # normalise min to zero
594
    $max = $max - $min;
595
    my $orig_min = $min;
596
    $min = 0;
597
598
    # if min and max are the same, just make it 1
599
    my $span = ( $strata - 1 ) / ( $max || 1 );
600
    foreach (@$tags) {
601
        my $w = $_->{weight_total};
602
        $_->{stratum} = int( ( $w - $orig_min ) * $span );
603
    }
604
    return ( $min, $max );
605
}
606
597
1;
607
1;
598
__END__
608
__END__
599
609
(-)a/koha-tmpl/opac-tmpl/prog/en/css/opac.css (+42 lines)
Lines 2609-2614 ul.ui-tabs-nav li { Link Here
2609
    box-shadow: 1px 1px 3px 0 #666;
2609
    box-shadow: 1px 1px 3px 0 #666;
2610
}
2610
}
2611
2611
2612
/* different sizes for different tags in opac-tags.tt */
2613
.tagweight0 {
2614
    font-size: 12px;
2615
    display: inline;
2616
}
2617
2618
.tagweight1 {
2619
    font-size: 14px;
2620
}
2621
2622
.tagweight2 {
2623
    font-size: 16px;
2624
}
2625
2626
.tagweight3 {
2627
    font-size: 18px;
2628
}
2629
2630
.tagweight4 {
2631
    font-size: 20px;
2632
}
2633
2634
.tagweight5 {
2635
    font-size: 22px;
2636
}
2637
2638
.tagweight6 {
2639
    font-size: 24px;
2640
}
2641
2642
.tagweight7 {
2643
    font-size: 26px;
2644
}
2645
2646
.tagweight8 {
2647
    font-size: 28px;
2648
}
2649
2650
.tagweight9 {
2651
    font-size: 30px;
2652
}
2612
/* ## BABELTHEQUE ## */
2653
/* ## BABELTHEQUE ## */
2613
/* Uncomment if babeltheque configuration no contains these lines */
2654
/* Uncomment if babeltheque configuration no contains these lines */
2614
/*
2655
/*
Lines 2757-2760 ul.ui-tabs-nav li { Link Here
2757
  color: #FFFFCC;
2798
  color: #FFFFCC;
2758
}
2799
}
2759
2800
2801
2760
*/
2802
*/
(-)a/koha-tmpl/opac-tmpl/prog/en/modules/opac-tags.tt (-21 / +1 lines)
Lines 21-46 Link Here
21
</style>
21
</style>
22
<script type="text/javascript">
22
<script type="text/javascript">
23
//<![CDATA[
23
//<![CDATA[
24
	var fontsizes = new Array (12,14,16,18,20,22,24,26,28,30);
25
	var fontcount = fontsizes.length;
26
	var maxcloudweight = 1;
27
	$(document).ready(function() {
28
		// $('#tagcloud').css('background-color','lightgrey');
29
		// $('#tagcloud .tag').css('border','1px solid black');
30
		$('#tagcloud .tag').each(function() {
31
			if (maxcloudweight < this.title) { maxcloudweight = this.title; }
32
			// have to run through the set of tags once to get the max: cannot be combined w/ 2nd pass
33
		});
34
		$('#tagcloud .tag').each(function(i) {
35
			var pos = this.id;
36
			var weight = this.title;	// "cloudweight"
37
			weight = (! weight) ? 1 : (weight > maxcloudweight) ? maxcloudweight : weight ;
38
			var index = Math.round(fontcount * weight/maxcloudweight) - 1;
39
			index  = (! index ) ? 0 : ( index > fontcount     ) ? fontcount      : index  ;
40
			var newsize = fontsizes[index];
41
			// alert(pos+ " (" +i+ ") weight = " +weight+ " of " +maxcloudweight+ ", fontsize[" +index+ " of " +fontcount+ "] = " +newsize);
42
			$('#' + pos).css({"font-size":(newsize + 'px'), display:"inline"});
43
		});
44
		$("#mytagst").tablesorter({[% IF ( dateformat == 'metric' ) %]
24
		$("#mytagst").tablesorter({[% IF ( dateformat == 'metric' ) %]
45
    dateFormat: 'uk',[% END %]
25
    dateFormat: 'uk',[% END %]
46
            widgets : ['zebra'],
26
            widgets : ['zebra'],
Lines 90-96 Link Here
90
	[% IF ( TAGLOOP ) %]
70
	[% IF ( TAGLOOP ) %]
91
	<div id="tagcloud">
71
	<div id="tagcloud">
92
	[% FOREACH TAGLOO IN TAGLOOP %]
72
	[% FOREACH TAGLOO IN TAGLOOP %]
93
            <span class="tag" id="tag[% loop.count %]">
73
            <span class="tag tagweight[% TAGLOO.stratum %]" id="tag[% loop.count %]" style="display:inline;">
94
		<a href="/cgi-bin/koha/opac-search.pl?tag=[% TAGLOO.term |url %]&amp;q=[% TAGLOO.term |url %]">
74
		<a href="/cgi-bin/koha/opac-search.pl?tag=[% TAGLOO.term |url %]&amp;q=[% TAGLOO.term |url %]">
95
		[% TAGLOO.term |html %]</a>
75
		[% TAGLOO.term |html %]</a>
96
			<span class="tagweight">[% TAGLOO.weight_total %]</span>
76
			<span class="tagweight">[% TAGLOO.weight_total %]</span>
(-)a/opac/opac-tags.pl (-27 / +2 lines)
Lines 41-47 use C4::Debug; Link Here
41
use C4::Output qw(:html :ajax pagination_bar);
41
use C4::Output qw(:html :ajax pagination_bar);
42
use C4::Scrubber;
42
use C4::Scrubber;
43
use C4::Biblio;
43
use C4::Biblio;
44
use C4::Tags qw(add_tag get_approval_rows get_tag_rows remove_tag);
44
use C4::Tags qw(add_tag get_approval_rows get_tag_rows remove_tag stratify_tags);
45
45
46
use Data::Dumper;
46
use Data::Dumper;
47
47
Lines 263-296 if ($add_op) { Link Here
263
		$arghash->{biblionumber} = $arg;
263
		$arghash->{biblionumber} = $arg;
264
	}
264
	}
265
	$results = get_approval_rows($arghash);
265
	$results = get_approval_rows($arghash);
266
266
    stratify_tags(10, $results); # work out the differents sizes for things
267
	my $count = scalar @$results;
267
	my $count = scalar @$results;
268
	$template->param(TAGLOOP_COUNT => $count, mine => $mine);
268
	$template->param(TAGLOOP_COUNT => $count, mine => $mine);
269
	# Here we make a halfhearted attempt to separate the tags into "strata" based on weight_total
270
	# FIXME: code4lib probably has a better algorithm, iirc
271
	# FIXME: when we get a better algorithm, move to C4
272
	my $maxstrata = 5;
273
	my $strata = 1;
274
	my $previous = 0;
275
	my $chunk = ($count/$maxstrata)/2;
276
	my $total = 0;
277
	my %cloud;
278
	foreach (reverse @$results) {
279
		my $current = $_->{weight_total};
280
		$total++;
281
		$cloud{$strata}++;
282
		if ($current == $previous) {
283
			$_->{cloudweight} = $strata;
284
			next;
285
		} 
286
		if ($strata < $maxstrata and 
287
			($cloud{$strata} > $chunk or 
288
			$count-$total <= $maxstrata-$strata)) {
289
			$strata++;
290
		}
291
		$_->{cloudweight} = $strata;
292
		$previous = $current;
293
	}
294
}
269
}
295
(scalar @errors  ) and $template->param(ERRORS  => \@errors);
270
(scalar @errors  ) and $template->param(ERRORS  => \@errors);
296
my @orderedresult = sort { uc($a->{'term'}) cmp uc($b->{'term'}) } @$results;
271
my @orderedresult = sort { uc($a->{'term'}) cmp uc($b->{'term'}) } @$results;
(-)a/t/db_dependent/Tags.t (-2 / +54 lines)
Lines 6-14 Link Here
6
use strict;
6
use strict;
7
use warnings;
7
use warnings;
8
8
9
use Test::More tests => 1;
9
use Data::Dumper;
10
11
use Test::More tests => 30;
10
12
11
BEGIN {
13
BEGIN {
12
        use_ok('C4::Tags');
14
        use_ok('C4::Tags');
13
}
15
}
14
16
15
- 
17
# Simple 'sequential 5' test
18
my $tags = make_tags(1,2,3,4,5);
19
my @strata = (0,1,2,3,4);
20
my ($min, $max) = C4::Tags::stratify_tags(5, $tags);
21
check_tag_strata($tags, \@strata, 'Sequential 5');
22
is($min, 0, 'Sequential 5 min');
23
is($max, 4, 'Sequential 5 max');
24
25
# Reverse test - should have the same results as previous
26
$tags = make_tags(5,4,3,2,1);
27
@strata = (4,3,2,1,0);
28
($min, $max) = C4::Tags::stratify_tags(5, $tags);
29
check_tag_strata($tags, \@strata, 'Reverse Sequential 5');
30
is($min, 0, 'Sequential 5 min');
31
is($max, 4, 'Sequential 5 max');
32
33
# All the same test - should all have the same results
34
$tags = make_tags(4,4,4,4,4);
35
@strata = (0,0,0,0,0);
36
($min, $max) = C4::Tags::stratify_tags(5, $tags);
37
check_tag_strata($tags, \@strata, 'All The Same');
38
is($min, 0, 'Sequential 5 min');
39
is($max, 0, 'Sequential 5 max');
40
41
# Some the same, some different
42
$tags = make_tags(1,2,2,3,3,8);
43
@strata = (0,0,0,1,1,4);
44
($min, $max) = C4::Tags::stratify_tags(5, $tags);
45
check_tag_strata($tags, \@strata, 'All The Same');
46
is($min, 0, 'Sequential 5 min');
47
is($max, 7, 'Sequential 5 max');
48
49
# Runs tests against the results
50
sub check_tag_strata {
51
    my ($tags, $expected, $name) = @_;
52
53
    foreach my $t (@$tags) {
54
        my $w = $t->{weight_total};
55
        my $s = $t->{stratum};
56
        is($s, shift @$expected, $name . " - $w ($s)");
57
    }
58
}
59
60
# Makes some tags with just enough info to test
61
sub make_tags {
62
    my @res;
63
    while (@_) {
64
        push @res, { weight_total => shift @_ };
65
    }
66
    return \@res;
67
}

Return to bug 7642