Bug 16371: Rewrite get_daily_quote
[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 use Koha::Quotes;
27
28 use base qw(Koha::Object);
29
30 =head1 NAME
31
32 Koha::Quote - Koha Quote object class
33
34 =head1 API
35
36 =head2 Class methods
37
38 =cut
39
40 =head2 get_daily_quote($opts)
41
42 Takes a hashref of options
43
44 Currently supported options are:
45
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
49
50 =cut
51
52 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
53 # at least for default option
54
55 sub get_daily_quote {
56     my ($self, %opts) = @_;
57
58     my $quote = undef;
59
60     if ($opts{'id'}) {
61         $quote = Koha::Quotes->find({ id => $opts{'id'} });
62     }
63     elsif ($opts{'random'}) {
64         # Fall through... we also return a random quote as a catch-all if all else fails
65     }
66     else {
67         my $dt = dt_from_string()->ymd();
68         $quote = Koha::Quotes->search(
69             {
70                 timestamp => { -like => "$dt%" },
71             },
72             {
73                 order_by => { -desc => 'timestamp' },
74                 rows => 1,
75             }
76         )->single;
77     }
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(
82             {},
83             {
84                 order_by => 'id',
85                 rows => 1,
86                 offset => $offset,
87             }
88         )->single;
89
90         unless($quote){
91             return;
92         }
93
94         # update the timestamp for that quote
95         my $dt = DateTime::Format::MySQL->format_datetime(dt_from_string());
96         $quote->update({ timestamp => $dt });
97     }
98     return $quote;
99 }
100
101 =head2 get_daily_quote_for_interface
102
103     my $quote = Koha::Quote->get_daily_quote_for_interface();
104
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.
108
109 =cut
110
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) {
116         my @cc = caller(3);
117         Koha::Exceptions::UnknownProgramState->throw(error => $cc[3]."()> C4::Context->interface() is not set! Don't know are you in OPAC or staff client?");
118     }
119     unless ($qotdPref =~ /$interface/) {
120         return;
121     }
122
123     return $self->get_daily_quote(%opts);
124 }
125
126 =head3 _type
127
128 =cut
129
130 sub _type {
131     return 'Quote';
132 }
133
134 1;