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>.
22 use Koha
::DateUtils
qw(dt_from_string);
25 use base
qw(Koha::Objects);
29 Koha::Quotes - Koha Quote object class
37 =head2 get_daily_quote($opts)
39 Takes a hashref of options
41 Currently supported options are:
43 'id' An exact quote id
44 'random' Select a random quote
45 noop When no option is passed in, this sub will return the quote timestamped for the current day
49 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
50 # at least for default option
53 my ($self, %opts) = @_;
55 my $qotdPref = C4
::Context
->preference('QuoteOfTheDay');
56 my $interface = C4
::Context
->interface();
58 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
60 unless ($qotdPref =~ /$interface/) {
67 $quote = $self->find({ id
=> $opts{'id'} });
69 elsif ($opts{'random'}) {
70 # Fall through... we also return a random quote as a catch-all if all else fails
73 my $dt = $dtf->format_date(dt_from_string
);
74 $quote = $self->search(
76 timestamp
=> { -between
=> => [ "$dt 00:00:00", "$dt 23:59:59" ] },
79 order_by
=> { -desc
=> 'timestamp' },
84 unless ($quote) { # if there are not matches, choose a random quote
85 my $range = $self->search->count;
86 my $offset = int(rand($range));
87 $quote = $self->search(
99 # update the timestamp for that quote
100 $quote->update({timestamp
=> dt_from_string
})->discard_changes;
118 return 'Koha::Quote';