Bug 16371: Combine get_daily_quote and get_daily_quote_for_interface
[koha.git] / Koha / Quotes.pm
1 package Koha::Quotes;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use Carp;
20 use DateTime::Format::MySQL;
21
22 use Koha::Database;
23 use Koha::DateUtils qw(dt_from_string);
24 use Koha::Quote;
25
26 use base qw(Koha::Objects);
27
28 =head1 NAME
29
30 Koha::Quotes - Koha Quote object class
31
32 =head1 API
33
34 =head2 Class methods
35
36 =cut
37
38 =head2 get_daily_quote($opts)
39
40 Takes a hashref of options
41
42 Currently supported options are:
43
44 'id'        An exact quote id
45 'random'    Select a random quote
46 noop        When no option is passed in, this sub will return the quote timestamped for the current day
47
48 =cut
49
50 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
51 # at least for default option
52
53 sub get_daily_quote {
54     my ($self, %opts) = @_;
55
56     my $qotdPref = C4::Context->preference('QuoteOfTheDay');
57     my $interface = C4::Context->interface();
58
59     my $dtf  = Koha::Database->new->schema->storage->datetime_parser;
60
61     unless ($qotdPref =~ /$interface/) {
62         return;
63     }
64
65     my $quote = undef;
66
67     if ($opts{'id'}) {
68         $quote = $self->find({ id => $opts{'id'} });
69     }
70     elsif ($opts{'random'}) {
71         # Fall through... we also return a random quote as a catch-all if all else fails
72     }
73     else {
74         my $dt = $dtf->format_date(dt_from_string);
75         $quote = $self->search(
76             {
77                 timestamp => { -between => => [ "$dt 00:00:00", "$dt 23:59:59" ] },
78             },
79             {
80                 order_by => { -desc => 'timestamp' },
81                 rows => 1,
82             }
83         )->single;
84     }
85     unless ($quote) {        # if there are not matches, choose a random quote
86         my $range = $self->search->count;
87         my $offset = int(rand($range));
88         $quote = $self->search(
89             {},
90             {
91                 order_by => 'id',
92                 rows => 1,
93                 offset => $offset,
94             }
95         )->single;
96
97         unless($quote){
98             return;
99         }
100
101         # update the timestamp for that quote
102         my $dt = $dtf->format_datetime(dt_from_string);
103         $quote->update({ timestamp => $dt });
104     }
105     return $quote;
106 }
107
108 =head3 type
109
110 =cut
111
112 sub _type {
113     return 'Quote';
114 }
115
116 =head3 object_class
117
118 =cut
119
120 sub object_class {
121     return 'Koha::Quote';
122 }
123
124 1;