Bug 5385: POD Cleanups (part 2)
[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 => 1,
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 $whereclause = '';
59 $whereclause .= 'items.homebranch='.$dbh->quote($branch)." AND " if ($branch);
60 $whereclause .= 'biblioitems.itemtype='.$dbh->quote($itemtype)." AND " if $itemtype;
61 $whereclause .= ' TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30).' AND ' if $timeLimit < 999;
62 $whereclause =~ s/ AND $// if $whereclause;
63 $whereclause = " WHERE ".$whereclause if $whereclause;
64
65 my $query = "SELECT datecreated, biblio.biblionumber, title, 
66                 author, sum( items.issues ) AS tot, biblioitems.itemtype,
67                 biblioitems.publishercode,biblioitems.publicationyear,
68                 itemtypes.description
69                 FROM biblio
70                 LEFT JOIN items USING (biblionumber)
71                 LEFT JOIN biblioitems USING (biblionumber)
72                 LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
73                 $whereclause
74                 GROUP BY biblio.biblionumber
75                 HAVING tot >0
76                 ORDER BY tot DESC
77                 LIMIT $limit
78                 ";
79
80 my $sth = $dbh->prepare($query);
81 $sth->execute();
82 my @results;
83 while (my $line= $sth->fetchrow_hashref) {
84     push @results, $line;
85 }
86
87 my $timeLimitFinite = $timeLimit;
88 if($timeLimit eq 999){ $timeLimitFinite = 0 };
89
90 $template->param(do_it => 1,
91                 limit => $limit,
92                 branch => $branches->{$branch}->{branchname} || 'all locations',
93                 itemtype => $itemtypes->{$itemtype}->{description} || 'item types',
94                 timeLimit => $timeLimit,
95                 timeLimitFinite => $timeLimit,
96                 results_loop => \@results,
97                 );
98
99 $template->param( branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}));
100
101 #doctype
102 $itemtypes = GetItemTypes;
103 my @itemtypeloop;
104 foreach my $thisitemtype (sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
105         my $selected = 1 if $thisitemtype eq $itemtype;
106         my %row =(value => $thisitemtype,
107                     description => $itemtypes->{$thisitemtype}->{'description'},
108                     selected => $selected,
109                  );
110         push @itemtypeloop, \%row;
111 }
112
113 $template->param(
114                  itemtypeloop =>\@itemtypeloop,
115                  dateformat    => C4::Context->preference("dateformat"),
116                 );
117 output_html_with_http_headers $input, $cookie, $template->output;
118