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;
27 use base qw(Koha::Object);
31 Koha::Quote - Koha Quote object class
39 =head2 get_daily_quote($opts)
41 Takes a hashref of options
43 Currently supported options are:
45 'id' An exact quote id
46 'random' Select a random quote
47 noop When no option is passed in, this sub will return the quote timestamped for the current day
49 The function returns an anonymous hash following this format:
52 'source' => 'source-of-quote',
53 'timestamp' => 'timestamp-value',
54 'text' => 'text-of-quote',
60 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
61 # at least for default option
64 my ($self, %opts) = @_;
65 my $dbh = C4::Context->dbh;
70 $query = 'SELECT * FROM quotes WHERE id = ?';
71 $sth = $dbh->prepare($query);
72 $sth->execute($opts{'id'});
73 $quote = $sth->fetchrow_hashref();
75 elsif ($opts{'random'}) {
76 # Fall through... we also return a random quote as a catch-all if all else fails
79 $query = 'SELECT * FROM quotes WHERE timestamp LIKE CONCAT(CURRENT_DATE,\'%\') ORDER BY timestamp DESC LIMIT 0,1';
80 $sth = $dbh->prepare($query);
82 $quote = $sth->fetchrow_hashref();
84 unless ($quote) { # if there are not matches, choose a random quote
85 # get a list of all available quote ids
86 $sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;');
88 my $range = ($sth->fetchrow_array)[0];
89 # chose a random id within that range if there is more than one quote
90 my $offset = int(rand($range));
92 $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
93 $sth = C4::Context->dbh->prepare($query);
94 # see http://www.perlmonks.org/?node_id=837422 for why
95 # we're being verbose and using bind_param
96 $sth->bind_param(1, $offset, SQL_INTEGER);
98 $quote = $sth->fetchrow_hashref();
99 # update the timestamp for that quote
100 $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
101 $sth = C4::Context->dbh->prepare($query);
103 DateTime::Format::MySQL->format_datetime( dt_from_string() ),
110 =head2 get_daily_quote_for_interface
112 my $quote = Koha::Quote->get_daily_quote_for_interface();
114 Is a wrapper for get_daily_quote(), with an extra check for using the correct
115 interface defined in the syspref 'QuoteOfTheDay'.
116 If the current interface is not allowed to display quotes, then returns nothing.
120 sub get_daily_quote_for_interface {
121 my ($self, %opts) = @_;
122 my $qotdPref = C4::Context->preference('QuoteOfTheDay');
123 my $interface = C4::Context->interface();
124 unless ($interface) {
126 Koha::Exceptions::UnknownProgramState->throw(error => $cc[3]."()> C4::Context->interface() is not set! Don't know are you in OPAC or staff client?");
128 unless ($qotdPref =~ /$interface/) {
132 return $self->get_daily_quote(%opts);