use Modern::Perl;
use Carp;
-use DateTime::Format::MySQL;
-use DBI qw(:sql_types);
use Koha::Database;
-use Koha::DateUtils qw(dt_from_string);
-use Koha::Exceptions::UnknownProgramState;
use Koha::Quotes;
use base qw(Koha::Object);
=cut
-=head2 get_daily_quote($opts)
-
-Takes a hashref of options
-
-Currently supported options are:
-
-'id' An exact quote id
-'random' Select a random quote
-noop When no option is passed in, this sub will return the quote timestamped for the current day
-
-=cut
-
-# This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
-# at least for default option
-
-sub get_daily_quote {
- my ($self, %opts) = @_;
-
- my $quote = undef;
-
- if ($opts{'id'}) {
- $quote = Koha::Quotes->find({ id => $opts{'id'} });
- }
- elsif ($opts{'random'}) {
- # Fall through... we also return a random quote as a catch-all if all else fails
- }
- else {
- my $dt = dt_from_string()->ymd();
- $quote = Koha::Quotes->search(
- {
- timestamp => { -like => "$dt%" },
- },
- {
- order_by => { -desc => 'timestamp' },
- rows => 1,
- }
- )->single;
- }
- unless ($quote) { # if there are not matches, choose a random quote
- my $range = Koha::Quotes->search->count;
- my $offset = int(rand($range));
- $quote = Koha::Quotes->search(
- {},
- {
- order_by => 'id',
- rows => 1,
- offset => $offset,
- }
- )->single;
-
- unless($quote){
- return;
- }
-
- # update the timestamp for that quote
- my $dt = DateTime::Format::MySQL->format_datetime(dt_from_string());
- $quote->update({ timestamp => $dt });
- }
- return $quote;
-}
-
-=head2 get_daily_quote_for_interface
-
- my $quote = Koha::Quote->get_daily_quote_for_interface();
-
-Is a wrapper for get_daily_quote(), with an extra check for using the correct
-interface defined in the syspref 'QuoteOfTheDay'.
-If the current interface is not allowed to display quotes, then returns nothing.
-
-=cut
-
-sub get_daily_quote_for_interface {
- my ($self, %opts) = @_;
- my $qotdPref = C4::Context->preference('QuoteOfTheDay');
- my $interface = C4::Context->interface();
- unless ($interface) {
- my @cc = caller(3);
- Koha::Exceptions::UnknownProgramState->throw(error => $cc[3]."()> C4::Context->interface() is not set! Don't know are you in OPAC or staff client?");
- }
- unless ($qotdPref =~ /$interface/) {
- return;
- }
-
- return $self->get_daily_quote(%opts);
-}
-
=head3 _type
=cut
use Modern::Perl;
use Carp;
+use DateTime::Format::MySQL;
use Koha::Database;
+use Koha::DateUtils qw(dt_from_string);
use Koha::Quote;
use base qw(Koha::Objects);
=head1 NAME
-Koha::Quote - Koha Quote object class
+Koha::Quotes - Koha Quote object class
=head1 API
=cut
+=head2 get_daily_quote($opts)
+
+Takes a hashref of options
+
+Currently supported options are:
+
+'id' An exact quote id
+'random' Select a random quote
+noop When no option is passed in, this sub will return the quote timestamped for the current day
+
+=cut
+
+# This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
+# at least for default option
+
+sub get_daily_quote {
+ my ($self, %opts) = @_;
+
+ my $qotdPref = C4::Context->preference('QuoteOfTheDay');
+ my $interface = C4::Context->interface();
+
+ my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+
+ unless ($qotdPref =~ /$interface/) {
+ return;
+ }
+
+ my $quote = undef;
+
+ if ($opts{'id'}) {
+ $quote = $self->find({ id => $opts{'id'} });
+ }
+ elsif ($opts{'random'}) {
+ # Fall through... we also return a random quote as a catch-all if all else fails
+ }
+ else {
+ my $dt = $dtf->format_date(dt_from_string);
+ $quote = $self->search(
+ {
+ timestamp => { -between => => [ "$dt 00:00:00", "$dt 23:59:59" ] },
+ },
+ {
+ order_by => { -desc => 'timestamp' },
+ rows => 1,
+ }
+ )->single;
+ }
+ unless ($quote) { # if there are not matches, choose a random quote
+ my $range = $self->search->count;
+ my $offset = int(rand($range));
+ $quote = $self->search(
+ {},
+ {
+ order_by => 'id',
+ rows => 1,
+ offset => $offset,
+ }
+ )->single;
+
+ unless($quote){
+ return;
+ }
+
+ # update the timestamp for that quote
+ my $dt = $dtf->format_datetime(dt_from_string);
+ $quote->update({ timestamp => $dt });
+ }
+ return $quote;
+}
+
=head3 type
=cut
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
-use DateTime::Format::MySQL;
-use Test::More tests => 13;
+use Test::More tests => 15;
use Koha::Database;
use Koha::DateUtils qw(dt_from_string);
BEGIN {
use_ok('Koha::Quote');
+ use_ok('Koha::Quotes');
}
my $quote = Koha::Quote->new();
my $dbh = C4::Context->dbh;
# Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
-my $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string());
+my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+my $timestamp = $dtf->format_datetime(dt_from_string());
my $quote_1 = Koha::Quote->new({ source => 'George Washington', text => 'To be prepared for war is one of the most effectual means of preserving peace.', timestamp => $timestamp })->store;
my $quote_2 = Koha::Quote->new({ source => 'Thomas Jefferson', text => 'When angry, count ten, before you speak; if very angry, an hundred.', timestamp => $timestamp })->store;
my $quote_3 = Koha::Quote->new({ source => 'Abraham Lincoln', text => 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal', timestamp => $timestamp })->store;
timestamp => $timestamp,
};
-$quote = Koha::Quote->get_daily_quote('id'=>$quote_3->id);
+#First test with QuoteOfTheDay disabled
+t::lib::Mocks::mock_preference('QuoteOfTheDay', 0);
+
+##Set interface and get nothing because syspref is not set.
+C4::Context->interface('opac');
+$quote = Koha::Quotes->get_daily_quote(id => $quote_1->id);
+ok(not($quote), "'QuoteOfTheDay'-syspref not set so nothing returned");
+
+##Set 'QuoteOfTheDay'-syspref to not include current interface 'opac'
+t::lib::Mocks::mock_preference('QuoteOfTheDay', 'intranet');
+$quote = Koha::Quotes->get_daily_quote(id => $quote_1->id);
+ok(not($quote), "'QuoteOfTheDay'-syspref doesn't include 'opac'");
+
+##Set 'QuoteOfTheDay'-syspref to include current interface 'opac'
+t::lib::Mocks::mock_preference('QuoteOfTheDay', 'opac,intranet');
+
+$quote = Koha::Quotes->get_daily_quote('id'=>$quote_3->id);
cmp_ok($quote->id, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
-$quote = Koha::Quote->get_daily_quote('random'=>1);
+$quote = Koha::Quotes->get_daily_quote('random'=>1);
ok($quote, "Got a random quote.");
cmp_ok($quote->id, '>', 0, 'Id is greater than 0');
-$timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one
+$timestamp = $dtf->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one
Koha::Quotes->search({ id => $expected_quote->{'id'} })->update({ timestamp => $timestamp });
$expected_quote->{'timestamp'} = $timestamp;
-$quote = Koha::Quote->get_daily_quote()->unblessed; # this is the "default" mode of selection
+$quote = Koha::Quotes->get_daily_quote()->unblessed; # this is the "default" mode of selection
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct");
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
Koha::Quotes->search()->delete();
-$quote = eval {Koha::Quote->get_daily_quote();};
+$quote = eval {Koha::Quotes->get_daily_quote();};
is( $@, '', 'get_daily_quote does not die if no quote exist' );
is_deeply( $quote, undef, 'return undef if quotes do not exists'); # Is it what we expect?
my $quote_6 = Koha::Quote->new({ source => 'George Washington', text => 'To be prepared for war is one of the most effectual means of preserving peace.', timestamp => dt_from_string() })->store;
-$quote = Koha::Quote->get_daily_quote();
+$quote = Koha::Quotes->get_daily_quote();
is( $quote->id, $quote_6->id, ' get_daily_quote returns the only existing quote' );
$schema->storage->txn_rollback;
-
-subtest "get_daily_quote_for_interface" => sub {
-
- plan tests => 3;
-
- $schema->storage->txn_begin;
-
- my ($quote);
- my $quote_1 = Koha::Quote->new({ source => 'Dusk And Her Embrace', text => 'Unfurl thy limbs breathless succubus<br/>How the full embosomed fog<br/>Imparts the night to us....', timestamp => dt_from_string })->store;
-
- my $expected_quote = {
- id => $quote_1->id,
- source => 'Dusk And Her Embrace',
- text => 'Unfurl thy limbs breathless succubus<br/>How the full embosomed fog<br/>Imparts the night to us....',
- timestamp => DateTime::Format::MySQL->format_datetime(dt_from_string),
- };
-
- t::lib::Mocks::mock_preference('QuoteOfTheDay', 0);
-
- ##Set interface and get nothing because syspref is not set.
- C4::Context->interface('opac');
- $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id);
- ok(not($quote), "'QuoteOfTheDay'-syspref not set so nothing returned");
-
- ##Set 'QuoteOfTheDay'-syspref to not include current interface 'opac'
- t::lib::Mocks::mock_preference('QuoteOfTheDay', 'intranet');
- $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id);
- ok(not($quote), "'QuoteOfTheDay'-syspref doesn't include 'opac'");
-
- ##Set 'QuoteOfTheDay'-syspref to include current interface 'opac'
- t::lib::Mocks::mock_preference('QuoteOfTheDay', 'opac,intranet');
- $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id)->unblessed;
- is_deeply($quote, $expected_quote, "Got the expected quote");
-
- $schema->storage->txn_rollback;
-};