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

(-)a/C4/Tags.pm (-42 / +52 lines)
Lines 33-53 use vars qw($ext_dict $select_all @fields); Link Here
33
BEGIN {
33
BEGIN {
34
    $VERSION = 3.07.00.049;
34
    $VERSION = 3.07.00.049;
35
	@ISA = qw(Exporter);
35
	@ISA = qw(Exporter);
36
	@EXPORT_OK = qw(
36
    @EXPORT_OK = qw(
37
		&get_tag &get_tags &get_tag_rows
37
      &get_tag &get_tags &get_tag_rows
38
		&add_tags &add_tag
38
      &add_tags &add_tag
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
42
      &get_approval_rows
43
		&get_approval_rows
43
      &blacklist
44
		&blacklist
44
      &whitelist
45
		&whitelist
45
      &is_approved
46
		&is_approved
46
      &approval_counts
47
		&approval_counts
47
      &get_count_by_tag_status
48
		&get_count_by_tag_status
48
      &get_filters
49
		&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');
53
	if ($debug) {
53
	if ($debug) {
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;
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/ccsr/en/css/opac.css (+41 lines)
Lines 2657-2659 body#opac-main #opacmainuserblockmobile { Link Here
2657
.mobile_only {
2657
.mobile_only {
2658
   display : none;
2658
   display : none;
2659
}
2659
}
2660
2661
/* different sizes for different tags in opac-tags.tt */
2662
.tagweight0 {
2663
    font-size: 12px;
2664
}
2665
2666
.tagweight1 {
2667
    font-size: 14px;
2668
}
2669
2670
.tagweight2 {
2671
    font-size: 16px;
2672
}
2673
2674
.tagweight3 {
2675
    font-size: 18px;
2676
}
2677
2678
.tagweight4 {
2679
    font-size: 20px;
2680
}
2681
2682
.tagweight5 {
2683
    font-size: 22px;
2684
}
2685
2686
.tagweight6 {
2687
    font-size: 24px;
2688
}
2689
2690
.tagweight7 {
2691
    font-size: 26px;
2692
}
2693
2694
.tagweight8 {
2695
    font-size: 28px;
2696
}
2697
2698
.tagweight9 {
2699
    font-size: 30px;
2700
}
(-)a/koha-tmpl/opac-tmpl/prog/en/css/opac.css (+41 lines)
Lines 2767-2769 body#opac-main #opacmainuserblockmobile { Link Here
2767
.mobile_only {
2767
.mobile_only {
2768
   display : none;
2768
   display : none;
2769
}
2769
}
2770
2771
/* different sizes for different tags in opac-tags.tt */
2772
.tagweight0 {
2773
    font-size: 12px;
2774
}
2775
2776
.tagweight1 {
2777
    font-size: 14px;
2778
}
2779
2780
.tagweight2 {
2781
    font-size: 16px;
2782
}
2783
2784
.tagweight3 {
2785
    font-size: 18px;
2786
}
2787
2788
.tagweight4 {
2789
    font-size: 20px;
2790
}
2791
2792
.tagweight5 {
2793
    font-size: 22px;
2794
}
2795
2796
.tagweight6 {
2797
    font-size: 24px;
2798
}
2799
2800
.tagweight7 {
2801
    font-size: 26px;
2802
}
2803
2804
.tagweight8 {
2805
    font-size: 28px;
2806
}
2807
2808
.tagweight9 {
2809
    font-size: 30px;
2810
}
(-)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 / +52 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 Test::More tests => 30;
10
10
11
BEGIN {
11
BEGIN {
12
        use_ok('C4::Tags');
12
        use_ok('C4::Tags');
13
}
13
}
14
14
15
- 
15
# Simple 'sequential 5' test
16
my $tags = make_tags(1,2,3,4,5);
17
my @strata = (0,1,2,3,4);
18
my ($min, $max) = C4::Tags::stratify_tags(5, $tags);
19
check_tag_strata($tags, \@strata, 'Sequential 5');
20
is($min, 0, 'Sequential 5 min');
21
is($max, 4, 'Sequential 5 max');
22
23
# Reverse test - should have the same results as previous
24
$tags = make_tags(5,4,3,2,1);
25
@strata = (4,3,2,1,0);
26
($min, $max) = C4::Tags::stratify_tags(5, $tags);
27
check_tag_strata($tags, \@strata, 'Reverse Sequential 5');
28
is($min, 0, 'Sequential 5 min');
29
is($max, 4, 'Sequential 5 max');
30
31
# All the same test - should all have the same results
32
$tags = make_tags(4,4,4,4,4);
33
@strata = (0,0,0,0,0);
34
($min, $max) = C4::Tags::stratify_tags(5, $tags);
35
check_tag_strata($tags, \@strata, 'All The Same');
36
is($min, 0, 'Sequential 5 min');
37
is($max, 0, 'Sequential 5 max');
38
39
# Some the same, some different
40
$tags = make_tags(1,2,2,3,3,8);
41
@strata = (0,0,0,1,1,4);
42
($min, $max) = C4::Tags::stratify_tags(5, $tags);
43
check_tag_strata($tags, \@strata, 'All The Same');
44
is($min, 0, 'Sequential 5 min');
45
is($max, 7, 'Sequential 5 max');
46
47
# Runs tests against the results
48
sub check_tag_strata {
49
    my ($tags, $expected, $name) = @_;
50
51
    foreach my $t (@$tags) {
52
        my $w = $t->{weight_total};
53
        my $s = $t->{stratum};
54
        is($s, shift @$expected, $name . " - $w ($s)");
55
    }
56
}
57
58
# Makes some tags with just enough info to test
59
sub make_tags {
60
    my @res;
61
    while (@_) {
62
        push @res, { weight_total => shift @_ };
63
    }
64
    return \@res;
65
}

Return to bug 7642