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
52 @ISA = qw( Exporter );
64 RemoveItemFromCollection
67 GetCollectionItemBranches
70 =item CreateCollection
71 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
72 Creates a new collection
75 $title: short description of the club or service
76 $description: long description of the club or service
79 $success: 1 if all database operations were successful, 0 otherwise
80 $errorCode: Code for reason of failure, good for translating errors in templates
81 $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 =item UpdateCollection
109 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
113 $colId: id of the collection to be updated
114 $title: short description of the club or service
115 $description: long description of the club or service
118 $success: 1 if all database operations were successful, 0 otherwise
119 $errorCode: Code for reason of failure, good for translating errors in templates
120 $errorMessage: English description of error
122 sub UpdateCollection
{
123 my ( $colId, $title, $description ) = @_;
125 ## Check for all neccessary parameters
127 return ( 0, 1, "No Id Given" );
130 return ( 0, 2, "No Title Given" );
132 if ( ! $description ) {
133 return ( 0, 3, "No Description Given" );
136 my $dbh = C4
::Context
->dbh;
139 $sth = $dbh->prepare("UPDATE collections
141 colTitle = ?, colDesc = ?
143 $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
150 =item DeleteCollection
151 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
152 Deletes a collection of the given id
155 $colId : id of the Archtype to be deleted
158 $success: 1 if all database operations were successful, 0 otherwise
159 $errorCode: Code for reason of failure, good for translating errors in templates
160 $errorMessage: English description of error
162 sub DeleteCollection
{
167 return ( 0, 1, "No Collection Id Given" );;
170 my $dbh = C4
::Context
->dbh;
174 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
175 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
182 $collections = GetCollections();
183 Returns data about all collections
187 $results: Reference to an array of associated arrays
189 $errorCode: Code for reason of failure, good for translating errors in templates
190 $errorMessage: English description of error
194 my $dbh = C4
::Context
->dbh;
196 my $sth = $dbh->prepare("SELECT * FROM collections");
197 $sth->execute() or return ( 1, $sth->errstr() );
200 while ( my $row = $sth->fetchrow_hashref ) {
201 push( @results , $row );
209 =item GetItemsInCollection
210 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
211 Returns information about the items in the given collection
214 $colId: The id of the collection
217 $results: Reference to an array of associated arrays
218 $success: 1 if all database operations were successful, 0 otherwise
219 $errorCode: Code for reason of failure, good for translating errors in templates
220 $errorMessage: English description of error
222 sub GetItemsInCollection
{
227 return ( 0, 0, 1, "No Collection Id Given" );;
230 my $dbh = C4
::Context
->dbh;
232 my $sth = $dbh->prepare("SELECT
234 items.itemcallnumber,
236 FROM collections, collections_tracking, items, biblio
237 WHERE collections.colId = collections_tracking.colId
238 AND collections_tracking.itemnumber = items.itemnumber
239 AND items.biblionumber = biblio.biblionumber
240 AND collections.colId = ? ORDER BY biblio.title");
241 $sth->execute( $colId ) or return ( 0, 0, 2, $sth->errstr() );
244 while ( my $row = $sth->fetchrow_hashref ) {
245 push( @results , $row );
254 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
255 Returns information about a collection
258 $colId: Id of the collection
260 $colId, $colTitle, $colDesc, $colBranchcode
265 my $dbh = C4
::Context
->dbh;
267 my ( $sth, @results );
268 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
269 $sth->execute( $colId ) or return 0;
271 my $row = $sth->fetchrow_hashref;
279 $$row{'colBranchcode'}
284 =item AddItemToCollection
285 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
286 Adds an item to a rotating collection.
289 $colId: Collection to add the item to.
290 $itemnumber: Item to be added to the collection
292 $success: 1 if all database operations were successful, 0 otherwise
293 $errorCode: Code for reason of failure, good for translating errors in templates
294 $errorMessage: English description of error
296 sub AddItemToCollection
{
297 my ( $colId, $itemnumber ) = @_;
299 ## Check for all neccessary parameters
301 return ( 0, 1, "No Collection Given" );
303 if ( ! $itemnumber ) {
304 return ( 0, 2, "No Itemnumber Given" );
307 if ( isItemInThisCollection
( $itemnumber, $colId ) ) {
308 return ( 0, 2, "Item is already in the collection!" );
309 } elsif ( isItemInAnyCollection
( $itemnumber ) ) {
310 return ( 0, 3, "Item is already in a different collection!" );
313 my $dbh = C4
::Context
->dbh;
316 $sth = $dbh->prepare("INSERT INTO collections_tracking ( ctId, colId, itemnumber )
317 VALUES ( NULL, ?, ? )");
318 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
325 =item RemoveItemFromCollection
326 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
327 Removes an item to a collection
330 $colId: Collection to add the item to.
331 $itemnumber: Item to be removed from collection
334 $success: 1 if all database operations were successful, 0 otherwise
335 $errorCode: Code for reason of failure, good for translating errors in templates
336 $errorMessage: English description of error
338 sub RemoveItemFromCollection
{
339 my ( $colId, $itemnumber ) = @_;
341 ## Check for all neccessary parameters
342 if ( ! $itemnumber ) {
343 return ( 0, 2, "No Itemnumber Given" );
346 if ( ! isItemInThisCollection
( $itemnumber, $colId ) ) {
347 return ( 0, 2, "Item is not in the collection!" );
350 my $dbh = C4
::Context
->dbh;
353 $sth = $dbh->prepare("DELETE FROM collections_tracking
354 WHERE itemnumber = ?");
355 $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
361 =item TransferCollection
362 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
363 Transfers a collection to another branch
366 $colId: id of the collection to be updated
367 $colBranchcode: branch where collection is moving to
370 $success: 1 if all database operations were successful, 0 otherwise
371 $errorCode: Code for reason of failure, good for translating errors in templates
372 $errorMessage: English description of error
374 sub TransferCollection
{
375 my ( $colId, $colBranchcode ) = @_;
377 ## Check for all neccessary parameters
379 return ( 0, 1, "No Id Given" );
381 if ( ! $colBranchcode ) {
382 return ( 0, 2, "No Branchcode Given" );
385 my $dbh = C4
::Context
->dbh;
388 $sth = $dbh->prepare("UPDATE collections
392 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
395 $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking
396 WHERE items.itemnumber = collections_tracking.itemnumber
397 AND collections_tracking.colId = ?");
398 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
400 while ( my $item = $sth->fetchrow_hashref ) {
401 my ( $dotransfer, $messages, $iteminformation ) = transferbook
( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
410 =item GetCollectionItemBranches
411 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
413 sub GetCollectionItemBranches
{
414 my ( $itemnumber ) = @_;
416 if ( ! $itemnumber ) {
420 my $dbh = C4
::Context
->dbh;
422 my ( $sth, @results );
423 $sth = $dbh->prepare("SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
424 WHERE items.itemnumber = collections_tracking.itemnumber
425 AND collections.colId = collections_tracking.colId
426 AND items.itemnumber = ?");
427 $sth->execute( $itemnumber );
429 my $row = $sth->fetchrow_hashref;
434 $$row{'holdingbranch'},
435 $$row{'colBranchcode'},
439 =item isItemInThisCollection
440 $inCollection = isItemInThisCollection( $itemnumber, $colId );
442 sub isItemInThisCollection
{
443 my ( $itemnumber, $colId ) = @_;
445 my $dbh = C4
::Context
->dbh;
447 my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
448 $sth->execute( $itemnumber, $colId ) or return( 0 );
450 my $row = $sth->fetchrow_hashref;
452 return $$row{'inCollection'};
455 =item isItemInAnyCollection
456 $inCollection = isItemInAnyCollection( $itemnumber );
458 sub isItemInAnyCollection
{
459 my ( $itemnumber ) = @_;
461 my $dbh = C4
::Context
->dbh;
463 my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
464 $sth->execute( $itemnumber ) or return( 0 );
466 my $row = $sth->fetchrow_hashref;
468 my $itemnumber = $$row{'itemnumber'};
486 Kyle Hall <kylemhall@gmail.com>