244aea0673a505c0bd4398fc835174efa9b96973
[koha.git] / Koha / Quote.pm
1 package Koha::Quote;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use Carp;
20 use DateTime::Format::MySQL;
21 use DBI qw(:sql_types);
22
23 use Koha::Database;
24 use Koha::DateUtils qw(dt_from_string);
25 use Koha::Exceptions::UnknownProgramState;
26
27 use base qw(Koha::Object);
28
29 =head1 NAME
30
31 Koha::Quote - Koha Quote object class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =cut
38
39 =head2 get_daily_quote($opts)
40
41 Takes a hashref of options
42
43 Currently supported options are:
44
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
48
49 The function returns an anonymous hash following this format:
50
51         {
52           'source' => 'source-of-quote',
53           'timestamp' => 'timestamp-value',
54           'text' => 'text-of-quote',
55           'id' => 'quote-id'
56         };
57
58 =cut
59
60 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
61 # at least for default option
62
63 sub get_daily_quote {
64     my ($self, %opts) = @_;
65     my $dbh = C4::Context->dbh;
66     my $query = '';
67     my $sth = undef;
68     my $quote = undef;
69     if ($opts{'id'}) {
70         $query = 'SELECT * FROM quotes WHERE id = ?';
71         $sth = $dbh->prepare($query);
72         $sth->execute($opts{'id'});
73         $quote = $sth->fetchrow_hashref();
74     }
75     elsif ($opts{'random'}) {
76         # Fall through... we also return a random quote as a catch-all if all else fails
77     }
78     else {
79         $query = 'SELECT * FROM quotes WHERE timestamp LIKE CONCAT(CURRENT_DATE,\'%\') ORDER BY timestamp DESC LIMIT 0,1';
80         $sth = $dbh->prepare($query);
81         $sth->execute();
82         $quote = $sth->fetchrow_hashref();
83     }
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;');
87         $sth->execute;
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));
91         # grab it
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);
97         $sth->execute();
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);
102         $sth->execute(
103             DateTime::Format::MySQL->format_datetime( dt_from_string() ),
104             $quote->{'id'}
105         );
106     }
107     return $quote;
108 }
109
110 =head2 get_daily_quote_for_interface
111
112     my $quote = Koha::Quote->get_daily_quote_for_interface();
113
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.
117
118 =cut
119
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) {
125         my @cc = caller(3);
126         Koha::Exceptions::UnknownProgramState->throw(error => $cc[3]."()> C4::Context->interface() is not set! Don't know are you in OPAC or staff client?");
127     }
128     unless ($qotdPref =~ /$interface/) {
129         return;
130     }
131
132     return $self->get_daily_quote(%opts);
133 }
134
135 =head3 _type
136
137 =cut
138
139 sub _type {
140     return 'Quote';
141 }
142
143 1;