View | Details | Raw Unified | Return to bug 16371
Collapse All | Expand All

(-)a/Koha/Quote.pm (-39 / +30 lines)
Lines 23-28 use DBI qw(:sql_types); Link Here
23
use Koha::Database;
23
use Koha::Database;
24
use Koha::DateUtils qw(dt_from_string);
24
use Koha::DateUtils qw(dt_from_string);
25
use Koha::Exceptions::UnknownProgramState;
25
use Koha::Exceptions::UnknownProgramState;
26
use Koha::Quotes;
26
27
27
use base qw(Koha::Object);
28
use base qw(Koha::Object);
28
29
Lines 46-60 Currently supported options are: Link Here
46
'random'    Select a random quote
47
'random'    Select a random quote
47
noop        When no option is passed in, this sub will return the quote timestamped for the current day
48
noop        When no option is passed in, this sub will return the quote timestamped for the current day
48
49
49
The function returns an anonymous hash following this format:
50
51
        {
52
          'source' => 'source-of-quote',
53
          'timestamp' => 'timestamp-value',
54
          'text' => 'text-of-quote',
55
          'id' => 'quote-id'
56
        };
57
58
=cut
50
=cut
59
51
60
# This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
52
# This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
Lines 62-108 The function returns an anonymous hash following this format: Link Here
62
54
63
sub get_daily_quote {
55
sub get_daily_quote {
64
    my ($self, %opts) = @_;
56
    my ($self, %opts) = @_;
65
    my $dbh = C4::Context->dbh;
57
66
    my $query = '';
67
    my $sth = undef;
68
    my $quote = undef;
58
    my $quote = undef;
59
69
    if ($opts{'id'}) {
60
    if ($opts{'id'}) {
70
        $query = 'SELECT * FROM quotes WHERE id = ?';
61
        $quote = Koha::Quotes->find({ id => $opts{'id'} });
71
        $sth = $dbh->prepare($query);
72
        $sth->execute($opts{'id'});
73
        $quote = $sth->fetchrow_hashref();
74
    }
62
    }
75
    elsif ($opts{'random'}) {
63
    elsif ($opts{'random'}) {
76
        # Fall through... we also return a random quote as a catch-all if all else fails
64
        # Fall through... we also return a random quote as a catch-all if all else fails
77
    }
65
    }
78
    else {
66
    else {
79
        $query = 'SELECT * FROM quotes WHERE timestamp LIKE CONCAT(CURRENT_DATE,\'%\') ORDER BY timestamp DESC LIMIT 0,1';
67
        my $dt = dt_from_string()->ymd();
80
        $sth = $dbh->prepare($query);
68
        $quote = Koha::Quotes->search(
81
        $sth->execute();
69
            {
82
        $quote = $sth->fetchrow_hashref();
70
                timestamp => { -like => "$dt%" },
71
            },
72
            {
73
                order_by => { -desc => 'timestamp' },
74
                rows => 1,
75
            }
76
        )->single;
83
    }
77
    }
84
    unless ($quote) {        # if there are not matches, choose a random quote
78
    unless ($quote) {        # if there are not matches, choose a random quote
85
        # get a list of all available quote ids
79
        my $range = Koha::Quotes->search->count;
86
        $sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;');
87
        $sth->execute;
88
        my $range = ($sth->fetchrow_array)[0];
89
        # chose a random id within that range if there is more than one quote
90
        my $offset = int(rand($range));
80
        my $offset = int(rand($range));
91
        # grab it
81
        $quote = Koha::Quotes->search(
92
        $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
82
            {},
93
        $sth = C4::Context->dbh->prepare($query);
83
            {
94
        # see http://www.perlmonks.org/?node_id=837422 for why
84
                order_by => 'id',
95
        # we're being verbose and using bind_param
85
                rows => 1,
96
        $sth->bind_param(1, $offset, SQL_INTEGER);
86
                offset => $offset,
97
        $sth->execute();
87
            }
98
        $quote = $sth->fetchrow_hashref();
88
        )->single;
89
90
        unless($quote){
91
            return;
92
        }
93
        
99
        # update the timestamp for that quote
94
        # update the timestamp for that quote
100
        $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
95
        my $dt = DateTime::Format::MySQL->format_datetime(dt_from_string());
101
        $sth = C4::Context->dbh->prepare($query);
96
        $quote->update({ timestamp => $dt });
102
        $sth->execute(
103
            DateTime::Format::MySQL->format_datetime( dt_from_string() ),
104
            $quote->{'id'}
105
        );
106
    }
97
    }
107
    return $quote;
98
    return $quote;
108
}
99
}
(-)a/t/db_dependent/Koha/Quotes.t (-9 / +8 lines)
Lines 50-84 my $expected_quote = { Link Here
50
    id          => $quote_3->id,
50
    id          => $quote_3->id,
51
    source      => 'Abraham Lincoln',
51
    source      => 'Abraham Lincoln',
52
    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.',
52
    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.',
53
    timestamp   => dt_from_string,
53
    timestamp   => $timestamp,
54
};
54
};
55
55
56
$quote = Koha::Quote->get_daily_quote('id'=>$quote_3->id);
56
$quote = Koha::Quote->get_daily_quote('id'=>$quote_3->id);
57
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
57
cmp_ok($quote->id, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
58
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
58
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
59
59
60
$quote = Koha::Quote->get_daily_quote('random'=>1);
60
$quote = Koha::Quote->get_daily_quote('random'=>1);
61
ok($quote, "Got a random quote.");
61
ok($quote, "Got a random quote.");
62
cmp_ok($quote->{'id'}, '>', 0, 'Id is greater than 0');
62
cmp_ok($quote->id, '>', 0, 'Id is greater than 0');
63
63
64
$timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one
64
$timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one
65
Koha::Quotes->search({ id => $expected_quote->{'id'} })->update({ timestamp => $timestamp });
65
Koha::Quotes->search({ id => $expected_quote->{'id'} })->update({ timestamp => $timestamp });
66
$expected_quote->{'timestamp'} = $timestamp;
66
$expected_quote->{'timestamp'} = $timestamp;
67
67
68
$quote = Koha::Quote->get_daily_quote(); # this is the "default" mode of selection
68
$quote = Koha::Quote->get_daily_quote()->unblessed; # this is the "default" mode of selection
69
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
69
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
70
is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct");
70
is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct");
71
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
71
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
72
72
73
$dbh->do(q|DELETE FROM quotes|);
73
Koha::Quotes->search()->delete();
74
$quote = eval {Koha::Quote->get_daily_quote();};
74
$quote = eval {Koha::Quote->get_daily_quote();};
75
is( $@, '', 'get_daily_quote does not die if no quote exist' );
75
is( $@, '', 'get_daily_quote does not die if no quote exist' );
76
is_deeply( $quote, {}, 'get_daily_quote return an empty hashref is no quote exist'); # Is it what we expect?
76
is_deeply( $quote, undef, 'return undef if quotes do not exists'); # Is it what we expect?
77
77
78
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;
78
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;
79
79
80
$quote = Koha::Quote->get_daily_quote();
80
$quote = Koha::Quote->get_daily_quote();
81
is( $quote->{id}, $quote_6->id, ' get_daily_quote returns the only existing quote' );
81
is( $quote->id, $quote_6->id, ' get_daily_quote returns the only existing quote' );
82
82
83
$schema->storage->txn_rollback;
83
$schema->storage->txn_rollback;
84
84
Lines 112-118 subtest "get_daily_quote_for_interface" => sub { Link Here
112
112
113
    ##Set 'QuoteOfTheDay'-syspref to include current interface 'opac'
113
    ##Set 'QuoteOfTheDay'-syspref to include current interface 'opac'
114
    t::lib::Mocks::mock_preference('QuoteOfTheDay', 'opac,intranet');
114
    t::lib::Mocks::mock_preference('QuoteOfTheDay', 'opac,intranet');
115
    $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id);
115
    $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id)->unblessed;
116
    is_deeply($quote, $expected_quote, "Got the expected quote");
116
    is_deeply($quote, $expected_quote, "Got the expected quote");
117
117
118
    $schema->storage->txn_rollback;
118
    $schema->storage->txn_rollback;
119
- 

Return to bug 16371