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.
26 #use warnings; FIXME - Bug 2505
37 use vars
qw($VERSION @ISA @EXPORT);
39 # set the version for version checking
44 C4::RotatingCollections - Functions for managing rotating collections
50 @ISA = qw( Exporter );
62 RemoveItemFromCollection
65 GetCollectionItemBranches
68 =head2 CreateCollection
69 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
70 Creates a new collection
73 $title: short description of the club or service
74 $description: long description of the club or service
77 $success: 1 if all database operations were successful, 0 otherwise
78 $errorCode: Code for reason of failure, good for translating errors in templates
79 $errorMessage: English description of error
83 sub CreateCollection
{
84 my ( $title, $description ) = @_;
86 ## Check for all neccessary parameters
88 return ( 0, 1, "No Title Given" );
90 if ( ! $description ) {
91 return ( 0, 2, "No Description Given" );
96 my $dbh = C4
::Context
->dbh;
99 $sth = $dbh->prepare("INSERT INTO collections ( colId, colTitle, colDesc )
100 VALUES ( NULL, ?, ? )");
101 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
108 =head2 UpdateCollection
110 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
115 $colId: id of the collection to be updated
116 $title: short description of the club or service
117 $description: long description of the club or service
120 $success: 1 if all database operations were successful, 0 otherwise
121 $errorCode: Code for reason of failure, good for translating errors in templates
122 $errorMessage: English description of error
126 sub UpdateCollection
{
127 my ( $colId, $title, $description ) = @_;
129 ## Check for all neccessary parameters
131 return ( 0, 1, "No Id Given" );
134 return ( 0, 2, "No Title Given" );
136 if ( ! $description ) {
137 return ( 0, 3, "No Description Given" );
140 my $dbh = C4
::Context
->dbh;
143 $sth = $dbh->prepare("UPDATE collections
145 colTitle = ?, colDesc = ?
147 $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
154 =head2 DeleteCollection
156 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
157 Deletes a collection of the given id
160 $colId : id of the Archtype to be deleted
163 $success: 1 if all database operations were successful, 0 otherwise
164 $errorCode: Code for reason of failure, good for translating errors in templates
165 $errorMessage: English description of error
169 sub DeleteCollection
{
174 return ( 0, 1, "No Collection Id Given" );;
177 my $dbh = C4
::Context
->dbh;
181 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
182 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
188 =head2 GetCollections
190 $collections = GetCollections();
191 Returns data about all collections
195 $results: Reference to an array of associated arrays
197 $errorCode: Code for reason of failure, good for translating errors in templates
198 $errorMessage: English description of error
204 my $dbh = C4
::Context
->dbh;
206 my $sth = $dbh->prepare("SELECT * FROM collections");
207 $sth->execute() or return ( 1, $sth->errstr() );
210 while ( my $row = $sth->fetchrow_hashref ) {
211 push( @results , $row );
219 =head2 GetItemsInCollection
221 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
223 Returns information about the items in the given collection
226 $colId: The id of the collection
229 $results: Reference to an array of associated arrays
230 $success: 1 if all database operations were successful, 0 otherwise
231 $errorCode: Code for reason of failure, good for translating errors in templates
232 $errorMessage: English description of error
236 sub GetItemsInCollection
{
241 return ( 0, 0, 1, "No Collection Id Given" );;
244 my $dbh = C4
::Context
->dbh;
246 my $sth = $dbh->prepare("SELECT
248 items.itemcallnumber,
250 FROM collections, collections_tracking, items, biblio
251 WHERE collections.colId = collections_tracking.colId
252 AND collections_tracking.itemnumber = items.itemnumber
253 AND items.biblionumber = biblio.biblionumber
254 AND collections.colId = ? ORDER BY biblio.title");
255 $sth->execute( $colId ) or return ( 0, 0, 2, $sth->errstr() );
258 while ( my $row = $sth->fetchrow_hashref ) {
259 push( @results , $row );
269 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
271 Returns information about a collection
274 $colId: Id of the collection
276 $colId, $colTitle, $colDesc, $colBranchcode
283 my $dbh = C4
::Context
->dbh;
285 my ( $sth, @results );
286 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
287 $sth->execute( $colId ) or return 0;
289 my $row = $sth->fetchrow_hashref;
297 $$row{'colBranchcode'}
302 =head2 AddItemToCollection
304 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
306 Adds an item to a rotating collection.
309 $colId: Collection to add the item to.
310 $itemnumber: Item to be added to the collection
312 $success: 1 if all database operations were successful, 0 otherwise
313 $errorCode: Code for reason of failure, good for translating errors in templates
314 $errorMessage: English description of error
318 sub AddItemToCollection
{
319 my ( $colId, $itemnumber ) = @_;
321 ## Check for all neccessary parameters
323 return ( 0, 1, "No Collection Given" );
325 if ( ! $itemnumber ) {
326 return ( 0, 2, "No Itemnumber Given" );
329 if ( isItemInThisCollection
( $itemnumber, $colId ) ) {
330 return ( 0, 2, "Item is already in the collection!" );
331 } elsif ( isItemInAnyCollection
( $itemnumber ) ) {
332 return ( 0, 3, "Item is already in a different collection!" );
335 my $dbh = C4
::Context
->dbh;
338 $sth = $dbh->prepare("INSERT INTO collections_tracking ( ctId, colId, itemnumber )
339 VALUES ( NULL, ?, ? )");
340 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
347 =head2 RemoveItemFromCollection
349 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
351 Removes an item to a collection
354 $colId: Collection to add the item to.
355 $itemnumber: Item to be removed from collection
358 $success: 1 if all database operations were successful, 0 otherwise
359 $errorCode: Code for reason of failure, good for translating errors in templates
360 $errorMessage: English description of error
364 sub RemoveItemFromCollection
{
365 my ( $colId, $itemnumber ) = @_;
367 ## Check for all neccessary parameters
368 if ( ! $itemnumber ) {
369 return ( 0, 2, "No Itemnumber Given" );
372 if ( ! isItemInThisCollection
( $itemnumber, $colId ) ) {
373 return ( 0, 2, "Item is not in the collection!" );
376 my $dbh = C4
::Context
->dbh;
379 $sth = $dbh->prepare("DELETE FROM collections_tracking
380 WHERE itemnumber = ?");
381 $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
387 =head2 TransferCollection
389 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
391 Transfers a collection to another branch
394 $colId: id of the collection to be updated
395 $colBranchcode: branch where collection is moving to
398 $success: 1 if all database operations were successful, 0 otherwise
399 $errorCode: Code for reason of failure, good for translating errors in templates
400 $errorMessage: English description of error
404 sub TransferCollection
{
405 my ( $colId, $colBranchcode ) = @_;
407 ## Check for all neccessary parameters
409 return ( 0, 1, "No Id Given" );
411 if ( ! $colBranchcode ) {
412 return ( 0, 2, "No Branchcode Given" );
415 my $dbh = C4
::Context
->dbh;
418 $sth = $dbh->prepare("UPDATE collections
422 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
425 $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking
426 WHERE items.itemnumber = collections_tracking.itemnumber
427 AND collections_tracking.colId = ?");
428 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
430 while ( my $item = $sth->fetchrow_hashref ) {
431 my ( $dotransfer, $messages, $iteminformation ) = transferbook
( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
440 =head2 GetCollectionItemBranches
442 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
446 sub GetCollectionItemBranches
{
447 my ( $itemnumber ) = @_;
449 if ( ! $itemnumber ) {
453 my $dbh = C4
::Context
->dbh;
455 my ( $sth, @results );
456 $sth = $dbh->prepare("SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
457 WHERE items.itemnumber = collections_tracking.itemnumber
458 AND collections.colId = collections_tracking.colId
459 AND items.itemnumber = ?");
460 $sth->execute( $itemnumber );
462 my $row = $sth->fetchrow_hashref;
467 $$row{'holdingbranch'},
468 $$row{'colBranchcode'},
472 =head2 isItemInThisCollection
474 $inCollection = isItemInThisCollection( $itemnumber, $colId );
478 sub isItemInThisCollection
{
479 my ( $itemnumber, $colId ) = @_;
481 my $dbh = C4
::Context
->dbh;
483 my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
484 $sth->execute( $itemnumber, $colId ) or return( 0 );
486 my $row = $sth->fetchrow_hashref;
488 return $$row{'inCollection'};
491 =head2 isItemInAnyCollection
493 $inCollection = isItemInAnyCollection( $itemnumber );
497 sub isItemInAnyCollection
{
498 my ( $itemnumber ) = @_;
500 my $dbh = C4
::Context
->dbh;
502 my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
503 $sth->execute( $itemnumber ) or return( 0 );
505 my $row = $sth->fetchrow_hashref;
507 $itemnumber = $row->{itemnumber
};
523 Kyle Hall <kylemhall@gmail.com>