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>.
21 use Koha::DateUtils qw( dt_from_string );
24 use base qw(Koha::Objects);
28 Koha::Quotes - Koha Quote object class
36 =head2 get_daily_quote($opts)
38 Takes a hashref of options
40 Currently supported options are:
42 'id' An exact quote id
43 'random' Select a random quote
44 noop When no option is passed in, this sub will return the quote timestamped for the current day
48 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
49 # at least for default option
52 my ($self, %opts) = @_;
54 my $qotdPref = C4::Context->preference('QuoteOfTheDay');
55 my $interface = C4::Context->interface();
57 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
59 unless ($qotdPref =~ /$interface/) {
66 $quote = $self->find({ id => $opts{'id'} });
68 elsif ($opts{'random'}) {
69 # Fall through... we also return a random quote as a catch-all if all else fails
72 my $dt = $dtf->format_date(dt_from_string);
73 $quote = $self->search(
75 timestamp => { -between => => [ "$dt 00:00:00", "$dt 23:59:59" ] },
78 order_by => { -desc => 'timestamp' },
83 unless ($quote) { # if there are not matches, choose a random quote
84 my $range = $self->search->count;
85 my $offset = int(rand($range));
86 $quote = $self->search(
98 # update the timestamp for that quote
99 $quote->update({timestamp => dt_from_string})->discard_changes;
117 return 'Koha::Quote';