Bug 16371: Combine get_daily_quote and get_daily_quote_for_interface
[koha.git] / Koha / Quotes.pm
blobc315b32423be452c8a813a47ada65dd856c4c33f
1 package Koha::Quotes;
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
19 use Carp;
20 use DateTime::Format::MySQL;
22 use Koha::Database;
23 use Koha::DateUtils qw(dt_from_string);
24 use Koha::Quote;
26 use base qw(Koha::Objects);
28 =head1 NAME
30 Koha::Quotes - Koha Quote object class
32 =head1 API
34 =head2 Class methods
36 =cut
38 =head2 get_daily_quote($opts)
40 Takes a hashref of options
42 Currently supported options are:
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
48 =cut
50 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
51 # at least for default option
53 sub get_daily_quote {
54 my ($self, %opts) = @_;
56 my $qotdPref = C4::Context->preference('QuoteOfTheDay');
57 my $interface = C4::Context->interface();
59 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
61 unless ($qotdPref =~ /$interface/) {
62 return;
65 my $quote = undef;
67 if ($opts{'id'}) {
68 $quote = $self->find({ id => $opts{'id'} });
70 elsif ($opts{'random'}) {
71 # Fall through... we also return a random quote as a catch-all if all else fails
73 else {
74 my $dt = $dtf->format_date(dt_from_string);
75 $quote = $self->search(
77 timestamp => { -between => => [ "$dt 00:00:00", "$dt 23:59:59" ] },
80 order_by => { -desc => 'timestamp' },
81 rows => 1,
83 )->single;
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 {},
91 order_by => 'id',
92 rows => 1,
93 offset => $offset,
95 )->single;
97 unless($quote){
98 return;
101 # update the timestamp for that quote
102 my $dt = $dtf->format_datetime(dt_from_string);
103 $quote->update({ timestamp => $dt });
105 return $quote;
108 =head3 type
110 =cut
112 sub _type {
113 return 'Quote';
116 =head3 object_class
118 =cut
120 sub object_class {
121 return 'Koha::Quote';