Bug 20256: DBIC schema
[koha.git] / Koha / Suggestions.pm
1 package Koha::Suggestions;
2
3 # Copyright ByWater Solutions 2015
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 use Koha::Suggestion;
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::Suggestions - Koha Suggestion object set class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 search_limited
38
39     my $suggestions = Koha::Suggestions->search_limited( $params, $attributes );
40
41 Returns all the suggestions the logged in user is allowed to see.
42
43 =cut
44
45 sub search_limited {
46     my ( $self, $params, $attributes ) = @_;
47
48     my $resultset = $self;
49
50     # filter on user branch
51     if (   C4::Context->preference('IndependentBranches')
52         && !C4::Context->IsSuperLibrarian() )
53     {
54         # If IndependentBranches is set and the logged in user is not superlibrarian
55         # Then we want to filter by the user's library (i.e. cannot see suggestions
56         # from other libraries)
57         my $userenv = C4::Context->userenv;
58
59         $resultset = $self->search({ branchcode => $userenv->{branch} })
60             if $userenv && $userenv->{branch};
61     }
62
63     return $resultset->search( $params, $attributes);
64 }
65
66 =head3 filter_by_pending
67
68     my $open = $suggestions->filter_by_pending;
69
70 Filters the resultset on those that are considered pending (i.e. STATUS = ASKED).
71
72 =cut
73
74 sub filter_by_pending {
75     my ($self) = @_;
76
77     return $self->search( { STATUS => 'ASKED' } );
78 }
79
80 =head3 filter_by_suggested_days_range
81
82     my $suggestions = $suggestions->filter_by_suggested_days_range( $days );
83
84 Filters the resultset on those placed within some I<$days> range.
85
86 =cut
87
88 sub filter_by_suggested_days_range {
89     my ( $self, $days ) = @_;
90
91     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
92
93     return $self->search(
94         { suggesteddate => { '>=' => $dtf->format_date( dt_from_string->subtract( days => $days ) ) } } );
95 }
96
97 =head2 Internal methods
98
99 =head3 _type
100
101 =cut
102
103 sub _type {
104     return 'Suggestion';
105 }
106
107 =head3 object_class
108
109 =cut
110
111 sub object_class {
112     return 'Koha::Suggestion';
113 }
114
115 =head1 AUTHOR
116
117 Kyle M Hall <kyle@bywatersolutions.com>
118
119 =cut
120
121 1;