Bug 27926: (QA follow-up) Switch to using data-order
[koha.git] / Koha / Quotes.pm
1 package Koha::Quotes;
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
21 use Koha::Database;
22 use Koha::DateUtils qw(dt_from_string);
23 use Koha::Quote;
24
25 use base qw(Koha::Objects);
26
27 =head1 NAME
28
29 Koha::Quotes - Koha Quote object class
30
31 =head1 API
32
33 =head2 Class methods
34
35 =cut
36
37 =head2 get_daily_quote($opts)
38
39 Takes a hashref of options
40
41 Currently supported options are:
42
43 'id'        An exact quote id
44 'random'    Select a random quote
45 noop        When no option is passed in, this sub will return the quote timestamped for the current day
46
47 =cut
48
49 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
50 # at least for default option
51
52 sub get_daily_quote {
53     my ($self, %opts) = @_;
54
55     my $qotdPref = C4::Context->preference('QuoteOfTheDay');
56     my $interface = C4::Context->interface();
57
58     my $dtf  = Koha::Database->new->schema->storage->datetime_parser;
59
60     unless ($qotdPref =~ /$interface/) {
61         return;
62     }
63
64     my $quote = undef;
65
66     if ($opts{'id'}) {
67         $quote = $self->find({ id => $opts{'id'} });
68     }
69     elsif ($opts{'random'}) {
70         # Fall through... we also return a random quote as a catch-all if all else fails
71     }
72     else {
73         my $dt = $dtf->format_date(dt_from_string);
74         $quote = $self->search(
75             {
76                 timestamp => { -between => => [ "$dt 00:00:00", "$dt 23:59:59" ] },
77             },
78             {
79                 order_by => { -desc => 'timestamp' },
80                 rows => 1,
81             }
82         )->single;
83     }
84     unless ($quote) {        # if there are not matches, choose a random quote
85         my $range = $self->search->count;
86         my $offset = int(rand($range));
87         $quote = $self->search(
88             {},
89             {
90                 order_by => 'id',
91                 rows => 1,
92                 offset => $offset,
93             }
94         )->single;
95     }
96
97     return unless $quote;
98
99     # update the timestamp for that quote
100     $quote->update({timestamp => dt_from_string})->discard_changes;
101
102     return $quote;
103 }
104
105 =head3 type
106
107 =cut
108
109 sub _type {
110     return 'Quote';
111 }
112
113 =head3 object_class
114
115 =cut
116
117 sub object_class {
118     return 'Koha::Quote';
119 }
120
121 1;