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;
21 use DBI qw(:sql_types);
24 use Koha::DateUtils qw(dt_from_string);
25 use Koha::Exceptions::UnknownProgramState;
28 use base qw(Koha::Object);
32 Koha::Quote - Koha Quote object class
40 =head2 get_daily_quote($opts)
42 Takes a hashref of options
44 Currently supported options are:
46 'id' An exact quote id
47 'random' Select a random quote
48 noop When no option is passed in, this sub will return the quote timestamped for the current day
52 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
53 # at least for default option
56 my ($self, %opts) = @_;
61 $quote = Koha::Quotes->find({ id => $opts{'id'} });
63 elsif ($opts{'random'}) {
64 # Fall through... we also return a random quote as a catch-all if all else fails
67 my $dt = dt_from_string()->ymd();
68 $quote = Koha::Quotes->search(
70 timestamp => { -like => "$dt%" },
73 order_by => { -desc => 'timestamp' },
78 unless ($quote) { # if there are not matches, choose a random quote
79 my $range = Koha::Quotes->search->count;
80 my $offset = int(rand($range));
81 $quote = Koha::Quotes->search(
94 # update the timestamp for that quote
95 my $dt = DateTime::Format::MySQL->format_datetime(dt_from_string());
96 $quote->update({ timestamp => $dt });
101 =head2 get_daily_quote_for_interface
103 my $quote = Koha::Quote->get_daily_quote_for_interface();
105 Is a wrapper for get_daily_quote(), with an extra check for using the correct
106 interface defined in the syspref 'QuoteOfTheDay'.
107 If the current interface is not allowed to display quotes, then returns nothing.
111 sub get_daily_quote_for_interface {
112 my ($self, %opts) = @_;
113 my $qotdPref = C4::Context->preference('QuoteOfTheDay');
114 my $interface = C4::Context->interface();
115 unless ($interface) {
117 Koha::Exceptions::UnknownProgramState->throw(error => $cc[3]."()> C4::Context->interface() is not set! Don't know are you in OPAC or staff client?");
119 unless ($qotdPref =~ /$interface/) {
123 return $self->get_daily_quote(%opts);