Bug 26922: Regression tests
[koha.git] / Koha / Quotes.pm
blob746fff0bc1ee466704ecac7d051fb2a708145343
1 package Koha::Quotes;
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>.
18 use Modern::Perl;
19 use Carp;
21 use Koha::Database;
22 use Koha::DateUtils qw(dt_from_string);
23 use Koha::Quote;
25 use base qw(Koha::Objects);
27 =head1 NAME
29 Koha::Quotes - Koha Quote object class
31 =head1 API
33 =head2 Class methods
35 =cut
37 =head2 get_daily_quote($opts)
39 Takes a hashref of options
41 Currently supported options are:
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
47 =cut
49 # This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
50 # at least for default option
52 sub get_daily_quote {
53 my ($self, %opts) = @_;
55 my $qotdPref = C4::Context->preference('QuoteOfTheDay');
56 my $interface = C4::Context->interface();
58 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
60 unless ($qotdPref =~ /$interface/) {
61 return;
64 my $quote = undef;
66 if ($opts{'id'}) {
67 $quote = $self->find({ id => $opts{'id'} });
69 elsif ($opts{'random'}) {
70 # Fall through... we also return a random quote as a catch-all if all else fails
72 else {
73 my $dt = $dtf->format_date(dt_from_string);
74 $quote = $self->search(
76 timestamp => { -between => => [ "$dt 00:00:00", "$dt 23:59:59" ] },
79 order_by => { -desc => 'timestamp' },
80 rows => 1,
82 )->single;
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 {},
90 order_by => 'id',
91 rows => 1,
92 offset => $offset,
94 )->single;
97 return unless $quote;
99 # update the timestamp for that quote
100 $quote->update({timestamp => dt_from_string})->discard_changes;
102 return $quote;
105 =head3 type
107 =cut
109 sub _type {
110 return 'Quote';
113 =head3 object_class
115 =cut
117 sub object_class {
118 return 'Koha::Quote';