Merge remote-tracking branch 'origin/new/bug_6291'
[koha.git] / opac / opac-topissues.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Search;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Branch;
31 use Date::Manip;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39 =cut
40
41 my $input = new CGI;
42 my $branches = GetBranches();
43 my $itemtypes = GetItemTypes();
44
45 my ($template, $borrowernumber, $cookie)
46         = get_template_and_user({template_name => 'opac-topissues.tmpl',
47                                 query => $input,
48                                 type => "opac",
49                authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
50                                 debug => 1,
51                                 });
52 my $dbh = C4::Context->dbh;
53 # Displaying results
54 my $limit = $input->param('limit') || 10;
55 my $branch = $input->param('branch') || '';
56 my $itemtype = $input->param('itemtype') || '';
57 my $timeLimit = $input->param('timeLimit') || 3;
58 my $advanced_search_types = C4::Context->preference('AdvancedSearchTypes');
59
60 my $whereclause = '';
61 $whereclause .= ' AND items.homebranch='.$dbh->quote($branch) if ($branch);
62 $whereclause .= ' AND TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30) if $timeLimit < 999;
63 $whereclause =~ s/ AND $// if $whereclause;
64 my $query;
65
66 if($advanced_search_types eq 'ccode'){
67     $whereclause .= ' AND authorised_values.authorised_value='.$dbh->quote($itemtype) if $itemtype;
68     $query = "SELECT datecreated, biblio.biblionumber, title,
69                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
70                     biblioitems.publishercode,biblioitems.publicationyear,
71                     authorised_values.lib as description
72                     FROM biblio
73                     LEFT JOIN items USING (biblionumber)
74                     LEFT JOIN biblioitems USING (biblionumber)
75                     LEFT JOIN authorised_values ON items.ccode = authorised_values.authorised_value
76                     WHERE 1
77                     $whereclause
78                     AND authorised_values.category = 'ccode' 
79                     GROUP BY biblio.biblionumber
80                     HAVING tot >0
81                     ORDER BY tot DESC
82                     LIMIT $limit
83                     ";
84     $template->param(ccodesearch => 1);
85 }else{
86     $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype) if $itemtype;
87     $query = "SELECT datecreated, biblio.biblionumber, title,
88                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
89                     biblioitems.publishercode,biblioitems.publicationyear,
90                     itemtypes.description
91                     FROM biblio
92                     LEFT JOIN items USING (biblionumber)
93                     LEFT JOIN biblioitems USING (biblionumber)
94                     LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
95                     WHERE 1
96                     $whereclause
97                     GROUP BY biblio.biblionumber
98                     HAVING tot >0
99                     ORDER BY tot DESC
100                     LIMIT $limit
101                     ";
102      $template->param(itemtypesearch => 1);
103 }
104
105 my $sth = $dbh->prepare($query);
106 $sth->execute();
107 my @results;
108 while (my $line= $sth->fetchrow_hashref) {
109     push @results, $line;
110 }
111
112 my $timeLimitFinite = $timeLimit;
113 if($timeLimit eq 999){ $timeLimitFinite = 0 };
114
115 $template->param(do_it => 1,
116                 limit => $limit,
117                 branch => $branches->{$branch}->{branchname} || 'all locations',
118                 itemtype => $itemtypes->{$itemtype}->{description} || 'item types',
119                 timeLimit => $timeLimit,
120                 timeLimitFinite => $timeLimit,
121                 results_loop => \@results,
122                 );
123
124 $template->param( branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}));
125
126 # the index parameter is different for item-level itemtypes
127 my $itype_or_itemtype = (C4::Context->preference("item-level_itypes"))?'itype':'itemtype';
128 $itemtypes = GetItemTypes;
129 my @itemtypesloop;
130 if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
131         foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
132         my %row =( value => $thisitemtype,
133                    description => $itemtypes->{$thisitemtype}->{'description'},
134                    selected    => $thisitemtype eq $itemtype,
135             );
136         push @itemtypesloop, \%row;
137         }
138 } else {
139     my $advsearchtypes = GetAuthorisedValues($advanced_search_types, '', 'opac');
140         for my $thisitemtype (@$advsearchtypes) {
141                 my $selected = 1 if $thisitemtype->{authorised_value} eq $itemtype;
142                 my %row =( value => $thisitemtype->{authorised_value},
143                 selected    => $thisitemtype eq $itemtype,
144                 description => $thisitemtype->{'lib'},
145             );
146                 push @itemtypesloop, \%row;
147         }
148 }
149
150 $template->param(
151                  itemtypeloop =>\@itemtypesloop,
152                  dateformat    => C4::Context->preference("dateformat"),
153                 );
154 output_html_with_http_headers $input, $cookie, $template->output;
155