Code cleaning :
[koha.git] / opac / opac-topissues.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Search;
27 use C4::Output;
28 use C4::Koha;
29 use C4::Branch;
30 use C4::Interface::CGI::Output;
31 use Date::Manip;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39
40 =over2
41
42 =cut
43
44 my $input = new CGI;
45 my $branches = GetBranches();
46 my $itemtypes = GetItemTypes();
47
48 my ($template, $borrowernumber, $cookie)
49         = get_template_and_user({template_name => 'opac-topissues.tmpl',
50                                 query => $input,
51                                 type => "opac",
52                                 authnotrequired => 1,
53                                 debug => 1,
54                                 });
55 my $dbh = C4::Context->dbh;
56 # Displaying results
57 my $limit = $input->param('limit') || 10;
58 my $branch = $input->param('branch');
59 my $itemtype = $input->param('itemtype');
60 my $timeLimit = $input->param('timeLimit') || 3;
61 my $whereclause;
62 $whereclause .= 'items.homebranch='.$dbh->quote($branch)." AND " if ($branch); 
63 $whereclause .= 'biblioitems.itemtype='.$dbh->quote($itemtype)." AND " if $itemtype;
64 $whereclause .= 'TO_DAYS(NOW()) - TO_DAYS(biblio.timestamp) <= '.$timeLimit*30 if $timeLimit;
65 $whereclause =~ s/ AND $//;
66 $whereclause = " WHERE ".$whereclause if $whereclause;
67 my $query = "SELECT biblio.timestamp, biblio.biblionumber, title, 
68                 author, sum( items.issues ) AS tot, biblioitems.itemtype,
69                 biblioitems.publishercode,biblioitems.publicationyear,
70                 itemtypes.description
71                 FROM biblio
72                 LEFT JOIN items USING (biblionumber)
73                 LEFT JOIN biblioitems USING (biblionumber)
74                 LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
75                 $whereclause
76                 GROUP BY biblio.biblionumber
77                 ORDER BY tot DESC
78                 LIMIT $limit
79                 ";
80
81 my $sth = $dbh->prepare($query);
82 $sth->execute();
83 my @results;
84 while (my $line= $sth->fetchrow_hashref) {
85     push @results, $line;
86 }
87
88 $template->param(do_it => 1,
89                 limit => $limit,
90                 branch => $branches->{$branch}->{branchname},
91                 itemtype => $itemtypes->{$itemtype}->{description},
92                 timeLimit => $timeLimit,
93                 results_loop => \@results,
94                 );
95
96 my $branches = GetBranches;
97 my @branchloop;
98 foreach my $thisbranch (keys %$branches) {
99         my %row =(value => $thisbranch,
100                     branchname => $branches->{$thisbranch}->{'branchname'},
101                         );
102         push @branchloop, \%row;
103 }
104
105 #doctype
106 my $itemtypes = GetItemTypes;
107 my @itemtypeloop;
108 foreach my $thisitemtype (keys %$itemtypes) {
109         my %row =(value => $thisitemtype,
110                     description => $itemtypes->{$thisitemtype}->{'description'},
111                         );
112         push @itemtypeloop, \%row;
113 }
114
115 $template->param(
116                 branchloop =>\@branchloop,
117                 itemtypeloop =>\@itemtypeloop,
118                 );
119 output_html_with_http_headers $input, $cookie, $template->output;
120