Bug Fixing opac/opac-readingrecord.pl
[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 =over 2
40
41 =cut
42
43 my $input = new CGI;
44 my $branches = GetBranches();
45 my $itemtypes = GetItemTypes();
46
47 my ($template, $borrowernumber, $cookie)
48         = get_template_and_user({template_name => 'opac-topissues.tmpl',
49                                 query => $input,
50                                 type => "opac",
51                                 authnotrequired => 1,
52                                 debug => 1,
53                                 });
54 my $dbh = C4::Context->dbh;
55 # Displaying results
56 my $limit = $input->param('limit') || 10;
57 my $branch = $input->param('branch') || '';
58 my $itemtype = $input->param('itemtype') || '';
59 my $timeLimit = $input->param('timeLimit') || 3;
60 my $whereclause = '';
61 $whereclause .= 'items.homebranch='.$dbh->quote($branch)." AND " if ($branch);
62 $whereclause .= 'biblioitems.itemtype='.$dbh->quote($itemtype)." AND " if $itemtype;
63 $whereclause .= ' TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30).' AND ' if $timeLimit < 999;
64 $whereclause =~ s/ AND $// if $whereclause;
65 $whereclause = " WHERE ".$whereclause if $whereclause;
66
67 my $query = "SELECT datecreated, 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                 HAVING tot >0
78                 ORDER BY tot DESC
79                 LIMIT $limit
80                 ";
81
82 my $sth = $dbh->prepare($query);
83 $sth->execute();
84 my @results;
85 while (my $line= $sth->fetchrow_hashref) {
86     push @results, $line;
87 }
88
89 my $timeLimitFinite = $timeLimit;
90 if($timeLimit eq 999){ $timeLimitFinite = 0 };
91
92 $template->param(do_it => 1,
93                 limit => $limit,
94                 branch => $branches->{$branch}->{branchname} || 'all locations',
95                 itemtype => $itemtypes->{$itemtype}->{description} || 'item types',
96                 timeLimit => $timeLimit,
97                 timeLimitFinite => $timeLimit,
98                 results_loop => \@results,
99                 );
100
101 $template->param( branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}));
102
103 #doctype
104 $itemtypes = GetItemTypes;
105 my @itemtypeloop;
106 foreach my $thisitemtype (sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
107         my $selected = 1 if $thisitemtype eq $itemtype;
108         my %row =(value => $thisitemtype,
109                     description => $itemtypes->{$thisitemtype}->{'description'},
110                     selected => $selected,
111                  );
112         push @itemtypeloop, \%row;
113 }
114
115 $template->param(
116                  itemtypeloop =>\@itemtypeloop,
117                  dateformat    => C4::Context->preference("dateformat"),
118                 );
119 output_html_with_http_headers $input, $cookie, $template->output;
120