Bug 12461 - Add patron clubs feature
[koha-equinox.git] / Koha / Clubs.pm
1 package Koha::Clubs;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25
26 use Koha::Club;
27
28 use base qw(Koha::Objects);
29
30 =head1 NAME
31
32 Koha::Clubs - Koha Clubs Object class
33
34 This object represents a collection of clubs a patron may enroll in.
35
36 =head1 API
37
38 =head2 Class Methods
39
40 =cut
41
42 =head3 get_enrollable
43
44 =cut
45
46 sub get_enrollable {
47     my ( $self, $params ) = @_;
48
49     # We need to filter out all the already enrolled in clubs
50     my $borrower = $params->{borrower};
51     if ($borrower) {
52         delete( $params->{borrower} );
53         my @enrollments = $borrower->get_club_enrollments();
54         if (@enrollments) {
55             $params->{'me.id'} = { -not_in => [ map { $_->club()->id() } @enrollments ] };
56         }
57     }
58
59     my $rs = $self->_resultset()->search( $params, { prefetch => 'club_template' } );
60
61     if (wantarray) {
62         my $class = ref($self) ? ref($self) : $self;
63
64         return $class->_wrap( $rs->all() );
65
66     }
67     else {
68         my $class = ref($self) ? ref($self) : $self;
69
70         return $class->_new_from_dbic($rs);
71     }
72 }
73
74 =head3 type
75
76 =cut
77
78 sub _type {
79     return 'Club';
80 }
81
82 sub object_class {
83     return 'Koha::Club';
84 }
85
86 =head1 AUTHOR
87
88 Kyle M Hall <kyle@bywatersolutions.com>
89
90 =cut
91
92 1;