Bug 29736: Don't return empty clubs
[koha.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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
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 $dtf = Koha::Database->new->schema->storage->datetime_parser;
60
61     # Only clubs with no end date or an end date in the future can be enrolled in
62     $params->{'-and'} = [
63         -or => [
64             date_end => { '>=' => $dtf->format_datetime( dt_from_string() ) },
65             date_end => undef,
66         ],
67         -or => [
68             'me.branchcode' => $borrower->branchcode,
69             'me.branchcode' => undef,
70         ]
71     ];
72
73     my $rs = $self->_resultset()->search( $params, { prefetch => 'club_template' } );
74
75     if (wantarray) {
76         my $class = ref($self) ? ref($self) : $self;
77
78         return $class->_wrap( $rs->all() );
79
80     }
81     else {
82         my $class = ref($self) ? ref($self) : $self;
83
84         return $class->_new_from_dbic($rs);
85     }
86 }
87
88 =head3 filter_out_empty
89
90     Remove clubs without current enrollments.
91
92 =cut
93
94 sub filter_out_empty {
95     my ($self) = @_;
96     return $self->search(
97         {
98             -and => [
99                 [
100                     { name        => { like => '%x%' } },
101                     { description => { like => '%x%' } },
102                 ],
103                 { 'club_enrollments.club_id'       => { '!=' => undef } },
104                 { 'club_enrollments.date_canceled' => undef },
105             ]
106         },
107         {
108             join     => 'club_enrollments',
109             distinct => 1,
110         }
111     );
112 }
113
114 =head3 type
115
116 =cut
117
118 sub _type {
119     return 'Club';
120 }
121
122 sub object_class {
123     return 'Koha::Club';
124 }
125
126 =head1 AUTHOR
127
128 Kyle M Hall <kyle@bywatersolutions.com>
129
130 =cut
131
132 1;