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
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.
36 use vars qw($VERSION @ISA @EXPORT);
38 # set the version for version checking
43 C4::RotatingCollections - Functions for managing rotating collections
51 @ISA = qw( Exporter );
63 RemoveItemFromCollection
66 GetCollectionItemBranches
69 =item CreateCollection
70 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
71 Creates a new collection
74 $title: short description of the club or service
75 $description: long description of the club or service
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
82 sub CreateCollection {
83 my ( $title, $description ) = @_;
85 ## Check for all neccessary parameters
87 return ( 0, 1, "No Title Given" );
89 if ( ! $description ) {
90 return ( 0, 2, "No Description Given" );
95 my $dbh = C4::Context->dbh;
98 $sth = $dbh->prepare("INSERT INTO collections ( colId, colTitle, colDesc )
99 VALUES ( NULL, ?, ? )");
100 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
107 =item UpdateCollection
108 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
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
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
121 sub UpdateCollection {
122 my ( $colId, $title, $description ) = @_;
124 ## Check for all neccessary parameters
126 return ( 0, 1, "No Id Given" );
129 return ( 0, 2, "No Title Given" );
131 if ( ! $description ) {
132 return ( 0, 3, "No Description Given" );
135 my $dbh = C4::Context->dbh;
138 $sth = $dbh->prepare("UPDATE collections
140 colTitle = ?, colDesc = ?
142 $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
149 =item DeleteCollection
150 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
151 Deletes a collection of the given id
154 $colId : id of the Archtype to be deleted
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
161 sub DeleteCollection {
166 return ( 0, 1, "No Collection Id Given" );;
169 my $dbh = C4::Context->dbh;
173 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
174 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
181 $collections = GetCollections();
182 Returns data about all collections
186 $results: Reference to an array of associated arrays
188 $errorCode: Code for reason of failure, good for translating errors in templates
189 $errorMessage: English description of error
193 my $dbh = C4::Context->dbh;
195 my $sth = $dbh->prepare("SELECT * FROM collections");
196 $sth->execute() or return ( 1, $sth->errstr() );
199 while ( my $row = $sth->fetchrow_hashref ) {
200 push( @results , $row );
208 =item GetItemsInCollection
209 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
210 Returns information about the items in the given collection
213 $colId: The id of the collection
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
221 sub GetItemsInCollection {
226 return ( 0, 0, 1, "No Collection Id Given" );;
229 my $dbh = C4::Context->dbh;
231 my $sth = $dbh->prepare("SELECT
233 items.itemcallnumber,
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() );
243 while ( my $row = $sth->fetchrow_hashref ) {
244 push( @results , $row );
253 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
254 Returns information about a collection
257 $colId: Id of the collection
259 $colId, $colTitle, $colDesc, $colBranchcode
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;
278 $$row{'colBranchcode'}
283 =item AddItemToCollection
284 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
285 Adds an item to a rotating collection.
288 $colId: Collection to add the item to.
289 $itemnumber: Item to be added to the collection
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
295 sub AddItemToCollection {
296 my ( $colId, $itemnumber ) = @_;
298 ## Check for all neccessary parameters
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;
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() );
324 =item RemoveItemFromCollection
325 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
326 Removes an item to a collection
329 $colId: Collection to add the item to.
330 $itemnumber: Item to be removed from collection
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
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;
352 $sth = $dbh->prepare("DELETE FROM collections_tracking
353 WHERE itemnumber = ?");
354 $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
360 =item TransferCollection
361 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
362 Transfers a collection to another branch
365 $colId: id of the collection to be updated
366 $colBranchcode: branch where collection is moving to
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
373 sub TransferCollection {
374 my ( $colId, $colBranchcode ) = @_;
376 ## Check for all neccessary parameters
378 return ( 0, 1, "No Id Given" );
380 if ( ! $colBranchcode ) {
381 return ( 0, 2, "No Branchcode Given" );
384 my $dbh = C4::Context->dbh;
387 $sth = $dbh->prepare("UPDATE collections
391 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
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 );
399 while ( my $item = $sth->fetchrow_hashref ) {
400 my ( $dotransfer, $messages, $iteminformation ) = transferbook( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
409 =item GetCollectionItemBranches
410 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
412 sub GetCollectionItemBranches {
413 my ( $itemnumber ) = @_;
415 if ( ! $itemnumber ) {
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;
433 $$row{'holdingbranch'},
434 $$row{'colBranchcode'},
438 =item isItemInThisCollection
439 $inCollection = isItemInThisCollection( $itemnumber, $colId );
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 );
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'};
485 Kyle Hall <kylemhall@gmail.com>