1 package Koha::List::Patron;
3 # Copyright 2013 ByWater Solutions
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 Koha::List::Patron - Management of lists of patrons
30 use Carp qw( carp croak );
34 our (@ISA, @EXPORT_OK);
52 my @lists = GetPatronLists( $params );
54 Returns an array of lists created by the the given user
55 or the logged in user if none is passed in.
61 $params->{owner} ||= C4::Context->userenv->{'number'};
63 unless ( $params->{owner} ) {
64 carp("No owner passed in or defined!");
68 delete $params->{owner} if C4::Context->IsSuperLibrarian();
70 if ( my $owner = $params->{owner} ) {
71 delete $params->{owner};
78 my $schema = Koha::Database->new()->schema();
80 my @patron_lists = $schema->resultset('PatronList')->search($params);
82 return wantarray() ? @patron_lists : \@patron_lists;
87 DelPatronList( { patron_list_id => $list_id [, owner => $owner ] } );
94 $params->{owner} ||= C4::Context->userenv->{'number'};
96 unless ( $params->{patron_list_id} ) {
97 croak("No patron list id passed in!");
99 unless ( $params->{owner} ) {
100 carp("No owner passed in or defined!");
104 delete( $params->{owner} ) if ( C4::Context->IsSuperLibrarian() );
106 return Koha::Database->new()->schema()->resultset('PatronList')
107 ->search($params)->single()->delete();
112 AddPatronList( { name => $name [, owner => $owner ] } );
119 $params->{owner} ||= C4::Context->userenv->{'number'};
121 unless ( $params->{owner} ) {
122 carp("No owner passed in or defined!");
126 unless ( $params->{name} ) {
127 carp("No list name passed in!");
131 return Koha::Database->new()->schema()->resultset('PatronList')
137 ModPatronList( { patron_list_id => $id, name => $name [, owner => $owner ] } );
144 unless ( $params->{patron_list_id} ) {
145 carp("No patron list id passed in!");
149 my ($list) = GetPatronLists(
151 patron_list_id => $params->{patron_list_id},
152 owner => $params->{owner}
156 return $list->update($params);
159 =head2 AddPatronsToList
161 AddPatronsToList({ list => $list, cardnumbers => \@cardnumbers });
165 sub AddPatronsToList {
168 my $list = $params->{list};
169 my $cardnumbers = $params->{'cardnumbers'};
170 my $borrowernumbers = $params->{'borrowernumbers'};
172 return unless ( $list && ( $cardnumbers || $borrowernumbers ) );
178 $search_param{cardnumber} = { 'IN' => $cardnumbers };
180 $search_param{borrowernumber} = { 'IN' => $borrowernumbers };
184 Koha::Database->new()->schema()->resultset('Borrower')->search(
186 { columns => [qw/ borrowernumber /] }
187 )->get_column('borrowernumber')->all();
189 my $patron_list_id = $list->patron_list_id();
191 my $plp_rs = Koha::Database->new()->schema()->resultset('PatronListPatron');
194 foreach my $borrowernumber (@borrowernumbers) {
195 my $result = $plp_rs->update_or_create(
197 patron_list_id => $patron_list_id,
198 borrowernumber => $borrowernumber
201 push( @results, $result );
204 return wantarray() ? @results : \@results;
207 =head2 DelPatronsFromList
209 DelPatronsFromList({ list => $list, patron_list_patrons => \@patron_list_patron_ids });
213 sub DelPatronsFromList {
216 my $list = $params->{list};
217 my $patron_list_patrons = $params->{patron_list_patrons};
219 return unless ( $list && $patron_list_patrons );
221 return Koha::Database->new()->schema()->resultset('PatronListPatron')
222 ->search( { patron_list_patron_id => { 'IN' => $patron_list_patrons } } )
228 Kyle M Hall, E<lt>kyle@bywatersolutions.comE<gt>