Allow zebra search for Accelerated Reading Level in field 526$c
[koha.git] / C4 / RotatingCollections.pm
blob83fc6e0fc9774e1a392be2c40c6d40f52f2904be
1 package C4::RotatingCollections;
3 # $Id: RotatingCollections.pm,v 0.1 2007/04/20 kylemhall
5 # This package is inteded to keep track of what library
6 # Items of a certain collection should be at.
8 # Copyright 2007 Kyle Hall
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along
22 # with Koha; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 use strict;
27 require Exporter;
29 use C4::Context;
30 use C4::Circulation;
32 use DBI;
34 use Data::Dumper;
36 use vars qw($VERSION @ISA @EXPORT);
38 # set the version for version checking
39 $VERSION = 0.01;
41 =head1 NAME
43 C4::RotatingCollections - Functions for managing rotating collections
45 =head1 FUNCTIONS
47 =over 2
49 =cut
51 @ISA = qw( Exporter );
52 @EXPORT = qw(
53 CreateCollection
54 UpdateCollection
55 DeleteCollection
57 GetItemsInCollection
59 GetCollection
60 GetCollections
62 AddItemToCollection
63 RemoveItemFromCollection
64 TransferCollection
66 GetCollectionItemBranches
69 =item CreateCollection
70 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
71 Creates a new collection
73 Input:
74 $title: short description of the club or service
75 $description: long description of the club or service
77 Output:
78 $success: 1 if all database operations were successful, 0 otherwise
79 $errorCode: Code for reason of failure, good for translating errors in templates
80 $errorMessage: English description of error
81 =cut
82 sub CreateCollection {
83 my ( $title, $description ) = @_;
85 ## Check for all neccessary parameters
86 if ( ! $title ) {
87 return ( 0, 1, "No Title Given" );
89 if ( ! $description ) {
90 return ( 0, 2, "No Description Given" );
93 my $success = 1;
95 my $dbh = C4::Context->dbh;
97 my $sth;
98 $sth = $dbh->prepare("INSERT INTO collections ( colId, colTitle, colDesc )
99 VALUES ( NULL, ?, ? )");
100 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
101 $sth->finish;
103 return 1;
107 =item UpdateCollection
108 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
109 Updates a collection
111 Input:
112 $colId: id of the collection to be updated
113 $title: short description of the club or service
114 $description: long description of the club or service
116 Output:
117 $success: 1 if all database operations were successful, 0 otherwise
118 $errorCode: Code for reason of failure, good for translating errors in templates
119 $errorMessage: English description of error
120 =cut
121 sub UpdateCollection {
122 my ( $colId, $title, $description ) = @_;
124 ## Check for all neccessary parameters
125 if ( ! $colId ) {
126 return ( 0, 1, "No Id Given" );
128 if ( ! $title ) {
129 return ( 0, 2, "No Title Given" );
131 if ( ! $description ) {
132 return ( 0, 3, "No Description Given" );
135 my $dbh = C4::Context->dbh;
137 my $sth;
138 $sth = $dbh->prepare("UPDATE collections
139 SET
140 colTitle = ?, colDesc = ?
141 WHERE colId = ?");
142 $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
143 $sth->finish;
145 return 1;
149 =item DeleteCollection
150 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
151 Deletes a collection of the given id
153 Input:
154 $colId : id of the Archtype to be deleted
156 Output:
157 $success: 1 if all database operations were successful, 0 otherwise
158 $errorCode: Code for reason of failure, good for translating errors in templates
159 $errorMessage: English description of error
160 =cut
161 sub DeleteCollection {
162 my ( $colId ) = @_;
164 ## Paramter check
165 if ( ! $colId ) {
166 return ( 0, 1, "No Collection Id Given" );;
169 my $dbh = C4::Context->dbh;
171 my $sth;
173 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
174 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
175 $sth->finish;
177 return 1;
180 =item GetCollections
181 $collections = GetCollections();
182 Returns data about all collections
184 Output:
185 On Success:
186 $results: Reference to an array of associated arrays
187 On Failure:
188 $errorCode: Code for reason of failure, good for translating errors in templates
189 $errorMessage: English description of error
190 =cut
191 sub GetCollections {
193 my $dbh = C4::Context->dbh;
195 my $sth = $dbh->prepare("SELECT * FROM collections");
196 $sth->execute() or return ( 1, $sth->errstr() );
198 my @results;
199 while ( my $row = $sth->fetchrow_hashref ) {
200 push( @results , $row );
203 $sth->finish;
205 return \@results;
208 =item GetItemsInCollection
209 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
210 Returns information about the items in the given collection
212 Input:
213 $colId: The id of the collection
215 Output:
216 $results: Reference to an array of associated arrays
217 $success: 1 if all database operations were successful, 0 otherwise
218 $errorCode: Code for reason of failure, good for translating errors in templates
219 $errorMessage: English description of error
220 =cut
221 sub GetItemsInCollection {
222 my ( $colId ) = @_;
224 ## Paramter check
225 if ( ! $colId ) {
226 return ( 0, 0, 1, "No Collection Id Given" );;
229 my $dbh = C4::Context->dbh;
231 my $sth = $dbh->prepare("SELECT
232 biblio.title,
233 items.itemcallnumber,
234 items.barcode
235 FROM collections, collections_tracking, items, biblio
236 WHERE collections.colId = collections_tracking.colId
237 AND collections_tracking.itemnumber = items.itemnumber
238 AND items.biblionumber = biblio.biblionumber
239 AND collections.colId = ? ORDER BY biblio.title");
240 $sth->execute( $colId ) or return ( 0, 0, 2, $sth->errstr() );
242 my @results;
243 while ( my $row = $sth->fetchrow_hashref ) {
244 push( @results , $row );
247 $sth->finish;
249 return \@results;
252 =item GetCollection
253 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
254 Returns information about a collection
256 Input:
257 $colId: Id of the collection
258 Output:
259 $colId, $colTitle, $colDesc, $colBranchcode
260 =cut
261 sub GetCollection {
262 my ( $colId ) = @_;
264 my $dbh = C4::Context->dbh;
266 my ( $sth, @results );
267 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
268 $sth->execute( $colId ) or return 0;
270 my $row = $sth->fetchrow_hashref;
272 $sth->finish;
274 return (
275 $$row{'colId'},
276 $$row{'colTitle'},
277 $$row{'colDesc'},
278 $$row{'colBranchcode'}
283 =item AddItemToCollection
284 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
285 Adds an item to a rotating collection.
287 Input:
288 $colId: Collection to add the item to.
289 $itemnumber: Item to be added to the collection
290 Output:
291 $success: 1 if all database operations were successful, 0 otherwise
292 $errorCode: Code for reason of failure, good for translating errors in templates
293 $errorMessage: English description of error
294 =cut
295 sub AddItemToCollection {
296 my ( $colId, $itemnumber ) = @_;
298 ## Check for all neccessary parameters
299 if ( ! $colId ) {
300 return ( 0, 1, "No Collection Given" );
302 if ( ! $itemnumber ) {
303 return ( 0, 2, "No Itemnumber Given" );
306 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
307 return ( 0, 2, "Item is already in the collection!" );
308 } elsif ( isItemInAnyCollection( $itemnumber ) ) {
309 return ( 0, 3, "Item is already in a different collection!" );
312 my $dbh = C4::Context->dbh;
314 my $sth;
315 $sth = $dbh->prepare("INSERT INTO collections_tracking ( ctId, colId, itemnumber )
316 VALUES ( NULL, ?, ? )");
317 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
318 $sth->finish;
320 return 1;
324 =item RemoveItemFromCollection
325 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
326 Removes an item to a collection
328 Input:
329 $colId: Collection to add the item to.
330 $itemnumber: Item to be removed from collection
332 Output:
333 $success: 1 if all database operations were successful, 0 otherwise
334 $errorCode: Code for reason of failure, good for translating errors in templates
335 $errorMessage: English description of error
336 =cut
337 sub RemoveItemFromCollection {
338 my ( $colId, $itemnumber ) = @_;
340 ## Check for all neccessary parameters
341 if ( ! $itemnumber ) {
342 return ( 0, 2, "No Itemnumber Given" );
345 if ( ! isItemInThisCollection( $itemnumber, $colId ) ) {
346 return ( 0, 2, "Item is not in the collection!" );
349 my $dbh = C4::Context->dbh;
351 my $sth;
352 $sth = $dbh->prepare("DELETE FROM collections_tracking
353 WHERE itemnumber = ?");
354 $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
355 $sth->finish;
357 return 1;
360 =item TransferCollection
361 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
362 Transfers a collection to another branch
364 Input:
365 $colId: id of the collection to be updated
366 $colBranchcode: branch where collection is moving to
368 Output:
369 $success: 1 if all database operations were successful, 0 otherwise
370 $errorCode: Code for reason of failure, good for translating errors in templates
371 $errorMessage: English description of error
372 =cut
373 sub TransferCollection {
374 my ( $colId, $colBranchcode ) = @_;
376 ## Check for all neccessary parameters
377 if ( ! $colId ) {
378 return ( 0, 1, "No Id Given" );
380 if ( ! $colBranchcode ) {
381 return ( 0, 2, "No Branchcode Given" );
384 my $dbh = C4::Context->dbh;
386 my $sth;
387 $sth = $dbh->prepare("UPDATE collections
388 SET
389 colBranchcode = ?
390 WHERE colId = ?");
391 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
392 $sth->finish;
394 $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking
395 WHERE items.itemnumber = collections_tracking.itemnumber
396 AND collections_tracking.colId = ?");
397 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
398 my @results;
399 while ( my $item = $sth->fetchrow_hashref ) {
400 my ( $dotransfer, $messages, $iteminformation ) = transferbook( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
405 return 1;
409 =item GetCollectionItemBranches
410 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
411 =cut
412 sub GetCollectionItemBranches {
413 my ( $itemnumber ) = @_;
415 if ( ! $itemnumber ) {
416 return;
419 my $dbh = C4::Context->dbh;
421 my ( $sth, @results );
422 $sth = $dbh->prepare("SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
423 WHERE items.itemnumber = collections_tracking.itemnumber
424 AND collections.colId = collections_tracking.colId
425 AND items.itemnumber = ?");
426 $sth->execute( $itemnumber );
428 my $row = $sth->fetchrow_hashref;
430 $sth->finish;
432 return (
433 $$row{'holdingbranch'},
434 $$row{'colBranchcode'},
438 =item isItemInThisCollection
439 $inCollection = isItemInThisCollection( $itemnumber, $colId );
440 =cut
441 sub isItemInThisCollection {
442 my ( $itemnumber, $colId ) = @_;
444 my $dbh = C4::Context->dbh;
446 my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
447 $sth->execute( $itemnumber, $colId ) or return( 0 );
449 my $row = $sth->fetchrow_hashref;
451 return $$row{'inCollection'};
454 =item isItemInAnyCollection
455 $inCollection = isItemInAnyCollection( $itemnumber );
456 =cut
457 sub isItemInAnyCollection {
458 my ( $itemnumber ) = @_;
460 my $dbh = C4::Context->dbh;
462 my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
463 $sth->execute( $itemnumber ) or return( 0 );
465 my $row = $sth->fetchrow_hashref;
467 my $itemnumber = $$row{'itemnumber'};
468 $sth->finish;
470 if ( $itemnumber ) {
471 return 1;
472 } else {
473 return 0;
479 __END__
481 =back
483 =head1 AUTHOR
485 Kyle Hall <kylemhall@gmail.com>
487 =cut