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
40 $VERSION = 3.07.00.049;
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() );
107 =head2 UpdateCollection
109 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
114 $colId: id of the collection to be updated
115 $title: short description of the club or service
116 $description: long description of the club or service
119 $success: 1 if all database operations were successful, 0 otherwise
120 $errorCode: Code for reason of failure, good for translating errors in templates
121 $errorMessage: English description of error
125 sub UpdateCollection
{
126 my ( $colId, $title, $description ) = @_;
128 ## Check for all neccessary parameters
130 return ( 0, 1, "No Id Given" );
133 return ( 0, 2, "No Title Given" );
135 if ( ! $description ) {
136 return ( 0, 3, "No Description Given" );
139 my $dbh = C4
::Context
->dbh;
142 $sth = $dbh->prepare("UPDATE collections
144 colTitle = ?, colDesc = ?
146 $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
152 =head2 DeleteCollection
154 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
155 Deletes a collection of the given id
158 $colId : id of the Archtype to be deleted
161 $success: 1 if all database operations were successful, 0 otherwise
162 $errorCode: Code for reason of failure, good for translating errors in templates
163 $errorMessage: English description of error
167 sub DeleteCollection
{
172 return ( 0, 1, "No Collection Id Given" );;
175 my $dbh = C4
::Context
->dbh;
179 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
180 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
185 =head2 GetCollections
187 $collections = GetCollections();
188 Returns data about all collections
192 $results: Reference to an array of associated arrays
194 $errorCode: Code for reason of failure, good for translating errors in templates
195 $errorMessage: English description of error
201 my $dbh = C4
::Context
->dbh;
203 my $sth = $dbh->prepare("SELECT * FROM collections");
204 $sth->execute() or return ( 1, $sth->errstr() );
207 while ( my $row = $sth->fetchrow_hashref ) {
208 push( @results , $row );
214 =head2 GetItemsInCollection
216 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
218 Returns information about the items in the given collection
221 $colId: The id of the collection
224 $results: Reference to an array of associated arrays
225 $success: 1 if all database operations were successful, 0 otherwise
226 $errorCode: Code for reason of failure, good for translating errors in templates
227 $errorMessage: English description of error
231 sub GetItemsInCollection
{
236 return ( 0, 0, 1, "No Collection Id Given" );;
239 my $dbh = C4
::Context
->dbh;
241 my $sth = $dbh->prepare("SELECT
243 items.itemcallnumber,
245 FROM collections, collections_tracking, items, biblio
246 WHERE collections.colId = collections_tracking.colId
247 AND collections_tracking.itemnumber = items.itemnumber
248 AND items.biblionumber = biblio.biblionumber
249 AND collections.colId = ? ORDER BY biblio.title");
250 $sth->execute( $colId ) or return ( 0, 0, 2, $sth->errstr() );
253 while ( my $row = $sth->fetchrow_hashref ) {
254 push( @results , $row );
262 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
264 Returns information about a collection
267 $colId: Id of the collection
269 $colId, $colTitle, $colDesc, $colBranchcode
276 my $dbh = C4
::Context
->dbh;
278 my ( $sth, @results );
279 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
280 $sth->execute( $colId ) or return 0;
282 my $row = $sth->fetchrow_hashref;
288 $$row{'colBranchcode'}
293 =head2 AddItemToCollection
295 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
297 Adds an item to a rotating collection.
300 $colId: Collection to add the item to.
301 $itemnumber: Item to be added to the collection
303 $success: 1 if all database operations were successful, 0 otherwise
304 $errorCode: Code for reason of failure, good for translating errors in templates
305 $errorMessage: English description of error
309 sub AddItemToCollection
{
310 my ( $colId, $itemnumber ) = @_;
312 ## Check for all neccessary parameters
314 return ( 0, 1, "No Collection Given" );
316 if ( ! $itemnumber ) {
317 return ( 0, 2, "No Itemnumber Given" );
320 if ( isItemInThisCollection
( $itemnumber, $colId ) ) {
321 return ( 0, 2, "Item is already in the collection!" );
322 } elsif ( isItemInAnyCollection
( $itemnumber ) ) {
323 return ( 0, 3, "Item is already in a different collection!" );
326 my $dbh = C4
::Context
->dbh;
329 $sth = $dbh->prepare("INSERT INTO collections_tracking ( collections_tracking_id, colId, itemnumber )
330 VALUES ( NULL, ?, ? )");
331 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
337 =head2 RemoveItemFromCollection
339 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
341 Removes an item to a collection
344 $colId: Collection to add the item to.
345 $itemnumber: Item to be removed from collection
348 $success: 1 if all database operations were successful, 0 otherwise
349 $errorCode: Code for reason of failure, good for translating errors in templates
350 $errorMessage: English description of error
354 sub RemoveItemFromCollection
{
355 my ( $colId, $itemnumber ) = @_;
357 ## Check for all neccessary parameters
358 if ( ! $itemnumber ) {
359 return ( 0, 2, "No Itemnumber Given" );
362 if ( ! isItemInThisCollection
( $itemnumber, $colId ) ) {
363 return ( 0, 2, "Item is not in the collection!" );
366 my $dbh = C4
::Context
->dbh;
369 $sth = $dbh->prepare("DELETE FROM collections_tracking
370 WHERE itemnumber = ?");
371 $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
376 =head2 TransferCollection
378 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
380 Transfers a collection to another branch
383 $colId: id of the collection to be updated
384 $colBranchcode: branch where collection is moving to
387 $success: 1 if all database operations were successful, 0 otherwise
388 $errorCode: Code for reason of failure, good for translating errors in templates
389 $errorMessage: English description of error
393 sub TransferCollection
{
394 my ( $colId, $colBranchcode ) = @_;
396 ## Check for all neccessary parameters
398 return ( 0, 1, "No Id Given" );
400 if ( ! $colBranchcode ) {
401 return ( 0, 2, "No Branchcode Given" );
404 my $dbh = C4
::Context
->dbh;
407 $sth = $dbh->prepare("UPDATE collections
411 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
413 $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking
414 WHERE items.itemnumber = collections_tracking.itemnumber
415 AND collections_tracking.colId = ?");
416 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
418 while ( my $item = $sth->fetchrow_hashref ) {
419 my ( $dotransfer, $messages, $iteminformation ) = transferbook
( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
426 =head2 GetCollectionItemBranches
428 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
432 sub GetCollectionItemBranches
{
433 my ( $itemnumber ) = @_;
435 if ( ! $itemnumber ) {
439 my $dbh = C4
::Context
->dbh;
441 my ( $sth, @results );
442 $sth = $dbh->prepare("SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
443 WHERE items.itemnumber = collections_tracking.itemnumber
444 AND collections.colId = collections_tracking.colId
445 AND items.itemnumber = ?");
446 $sth->execute( $itemnumber );
448 my $row = $sth->fetchrow_hashref;
451 $$row{'holdingbranch'},
452 $$row{'colBranchcode'},
456 =head2 isItemInThisCollection
458 $inCollection = isItemInThisCollection( $itemnumber, $colId );
462 sub isItemInThisCollection
{
463 my ( $itemnumber, $colId ) = @_;
465 my $dbh = C4
::Context
->dbh;
467 my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
468 $sth->execute( $itemnumber, $colId ) or return( 0 );
470 my $row = $sth->fetchrow_hashref;
472 return $$row{'inCollection'};
475 =head2 isItemInAnyCollection
477 $inCollection = isItemInAnyCollection( $itemnumber );
481 sub isItemInAnyCollection
{
482 my ( $itemnumber ) = @_;
484 my $dbh = C4
::Context
->dbh;
486 my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
487 $sth->execute( $itemnumber ) or return( 0 );
489 my $row = $sth->fetchrow_hashref;
491 $itemnumber = $row->{itemnumber
};
505 Kyle Hall <kylemhall@gmail.com>