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>.
20 use DateTime
::Format
::MySQL
;
23 use Koha
::DateUtils
qw(dt_from_string);
26 use base
qw(Koha::Objects);
30 Koha::Quotes - Koha Quote object class
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
50 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
51 # at least for default option
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/) {
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
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' },
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(
101 # update the timestamp for that quote
102 my $dt = $dtf->format_datetime(dt_from_string
);
103 $quote->update({ timestamp
=> $dt });
121 return 'Koha::Quote';