Line 0
Link Here
|
|
|
1 |
package C4::Recommendations; |
2 |
|
3 |
# Copyright 2009,2011 Catalyst IT |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 2 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along with |
17 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
18 |
# Suite 330, Boston, MA 02111-1307 USA |
19 |
|
20 |
=head1 NAME |
21 |
|
22 |
C4::Recommendations - Koha module for producing reading recommendations |
23 |
|
24 |
=head1 SYNOPSIS |
25 |
|
26 |
use C4::Recommendations; |
27 |
|
28 |
build_recommendations(); |
29 |
|
30 |
my $recommended_books = get_recommendations($biblio); |
31 |
|
32 |
=head1 DESCRIPTION |
33 |
|
34 |
This looks at the issue history, and counts how many times each particular book |
35 |
has been taken out by someone who also has taken out another particular book, |
36 |
recording that as a hit for each pair. |
37 |
|
38 |
For example, if 3 people have taken out book A and book B, then this is |
39 |
recorded as three "hits" for that combination, and so it'll show as a |
40 |
recommendation with that strength. |
41 |
|
42 |
=head1 EXPORT |
43 |
|
44 |
None by default, however C<build_recommendations> and C<get_recommendations> |
45 |
can be imported optionally. |
46 |
|
47 |
=head1 FUNCTIONS |
48 |
|
49 |
=cut |
50 |
|
51 |
use strict; |
52 |
use warnings; |
53 |
use C4::Context; |
54 |
|
55 |
require Exporter; |
56 |
|
57 |
our @ISA = qw(Exporter); |
58 |
|
59 |
# Items to export into callers namespace by default. Note: do not export |
60 |
# names by default without a very good reason. Use EXPORT_OK instead. |
61 |
# Do not simply export all your public functions/methods/constants. |
62 |
our @EXPORT_OK = qw( |
63 |
build_recommendations |
64 |
get_recommendations |
65 |
); |
66 |
|
67 |
our $VERSION = '0.01'; |
68 |
|
69 |
=head2 build_recommendations |
70 |
|
71 |
build_recommendations |
72 |
|
73 |
This runs through all the issues and generates the tables of recommendations. |
74 |
Note that it'll likely take a long time to run, and put stress on the database, |
75 |
so do it at a low peak time. |
76 |
|
77 |
=cut |
78 |
|
79 |
sub build_recommendations { |
80 |
my $dbh = C4::Context->dbh; |
81 |
$dbh->do("TRUNCATE recommendations"); |
82 |
my $all_issues_query = qq/ |
83 |
SELECT biblio.biblionumber,borrowernumber |
84 |
FROM old_issues,biblio,items |
85 |
WHERE old_issues.itemnumber=items.itemnumber |
86 |
AND items.biblionumber=biblio.biblionumber |
87 |
/; |
88 |
my $all_issues_sth = $dbh->prepare($all_issues_query); |
89 |
my $borrower_issues_query = qq/ |
90 |
SELECT biblio.biblionumber,borrowernumber |
91 |
FROM old_issues,biblio,items |
92 |
WHERE old_issues.itemnumber=items.itemnumber |
93 |
AND items.biblionumber=biblio.biblionumber |
94 |
AND old_issues.borrowernumber = ? |
95 |
AND items.biblionumber > ? |
96 |
/; |
97 |
my $borrower_issues_sth = $dbh->prepare($borrower_issues_query); |
98 |
my $recommendations_select = $dbh->prepare(qq/ |
99 |
SELECT * FROM recommendations |
100 |
WHERE biblio_one = ? AND |
101 |
biblio_two = ? |
102 |
/); |
103 |
my $recommendations_update = $dbh->prepare(qq/ |
104 |
UPDATE recommendations |
105 |
SET hit_count = ? |
106 |
WHERE biblio_one = ? |
107 |
AND biblio_two = ? |
108 |
/); |
109 |
my $recommendations_insert = $dbh->prepare(qq/ |
110 |
INSERT INTO recommendations (biblio_one,biblio_two,hit_count) VALUES (?,?,?) |
111 |
/); |
112 |
|
113 |
$all_issues_sth->execute(); |
114 |
while ( my $issue = $all_issues_sth->fetchrow_hashref() ) { |
115 |
# warn $issue->{'borrowernumber'}; |
116 |
$borrower_issues_sth->execute( $issue->{'borrowernumber'}, $issue->{biblionumber} ); |
117 |
while ( my $borrowers_issue = $borrower_issues_sth->fetchrow_hashref() ) { |
118 |
# warn $borrowers_issue->{'biblionumber'}; |
119 |
$recommendations_select->execute( $issue->{'biblionumber'}, |
120 |
$borrowers_issue->{'biblionumber'} ); |
121 |
if ( my $recommendation = $recommendations_select->fetchrow_hashref() ) { |
122 |
$recommendation->{'hit_count'}++; |
123 |
$recommendations_update->execute( |
124 |
$recommendation->{'hit_count'}, |
125 |
$issue->{'biblionumber'}, |
126 |
$borrowers_issue->{'biblionumber'} |
127 |
); |
128 |
} else { |
129 |
$recommendations_insert->execute( |
130 |
$issue->{'biblionumber'}, |
131 |
$borrowers_issue->{'biblionumber'}, |
132 |
1 |
133 |
); |
134 |
} |
135 |
} |
136 |
|
137 |
} |
138 |
} |
139 |
|
140 |
=head2 get_recommendations |
141 |
|
142 |
my $recommendations = get_recommendations($biblionumber, $limit) |
143 |
foreach my $rec (@$recommendations) { |
144 |
print $rec->{biblionumber}.": ".$rec->{title}."\n"; |
145 |
} |
146 |
|
147 |
This gets the recommendations for a particular biblio, returning an array of |
148 |
hashes containing C<biblionumber> and C<title>. The array is ordered from |
149 |
most-recommended to least. |
150 |
|
151 |
C<$limit> restrictes the amount of results returned. If it's not supplied, |
152 |
it defaults to 100. |
153 |
|
154 |
=cut |
155 |
|
156 |
sub get_recommendations { |
157 |
my ($biblionumber, $limit) = @_; |
158 |
$limit ||= 100; |
159 |
|
160 |
my $dbh = C4::Context->dbh(); |
161 |
|
162 |
# Two parts: first get the biblio_one side, then get the |
163 |
# biblio_two side. I'd love to know how to squish this into one query. |
164 |
my $sth = $dbh->prepare(qq/ |
165 |
SELECT biblio.biblionumber,biblio.title, hit_count |
166 |
FROM biblio,recommendations |
167 |
WHERE biblio.biblionumber = biblio_two |
168 |
AND biblio_one = ? |
169 |
ORDER BY hit_count DESC |
170 |
LIMIT ? |
171 |
/); |
172 |
$sth->execute($biblionumber, $limit); |
173 |
my $res = $sth->fetchall_arrayref({}); |
174 |
|
175 |
$sth = $dbh->prepare(qq/ |
176 |
SELECT biblio.biblionumber,biblio.title, hit_count |
177 |
FROM biblio,recommendations |
178 |
WHERE biblio.biblionumber = biblio_one |
179 |
AND biblio_two = ? |
180 |
ORDER BY hit_count DESC |
181 |
LIMIT ? |
182 |
/); |
183 |
$sth->execute($biblionumber, $limit); |
184 |
push @{ $res }, @{$sth->fetchall_arrayref({})}; |
185 |
|
186 |
$res = \@{ @{$res}[0..$limit] } if (@$res > $limit); |
187 |
|
188 |
my @res = sort { $b->{hit_count} <=> $a->{hit_count} } @$res; |
189 |
return \@res; |
190 |
} |
191 |
|
192 |
1; |
193 |
__END__ |
194 |
|
195 |
=head1 AUTHOR |
196 |
|
197 |
=over |
198 |
|
199 |
=item Chris Cormack, E<lt>chrisc@catalyst.net.nzE<gt> |
200 |
|
201 |
=item Robin Sheat, E<lt>robin@catalyst.net.nzE<gt> |
202 |
|
203 |
=back |
204 |
|