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