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

(-)a/C4/Koha.pm (-75 lines)
Lines 24-38 use Modern::Perl; Link Here
24
24
25
use C4::Context;
25
use C4::Context;
26
use Koha::Caches;
26
use Koha::Caches;
27
use Koha::DateUtils qw(dt_from_string);
28
use Koha::AuthorisedValues;
27
use Koha::AuthorisedValues;
29
use Koha::Libraries;
28
use Koha::Libraries;
30
use Koha::MarcSubfieldStructures;
29
use Koha::MarcSubfieldStructures;
31
use DateTime::Format::MySQL;
32
use Business::ISBN;
30
use Business::ISBN;
33
use Business::ISSN;
31
use Business::ISSN;
34
use autouse 'Data::cselectall_arrayref' => qw(Dumper);
32
use autouse 'Data::cselectall_arrayref' => qw(Dumper);
35
use DBI qw(:sql_types);
36
use vars qw(@ISA @EXPORT @EXPORT_OK $DEBUG);
33
use vars qw(@ISA @EXPORT @EXPORT_OK $DEBUG);
37
34
38
BEGIN {
35
BEGIN {
Lines 63-69 BEGIN { Link Here
63
		$DEBUG
60
		$DEBUG
64
	);
61
	);
65
	$DEBUG = 0;
62
	$DEBUG = 0;
66
@EXPORT_OK = qw( GetDailyQuote );
67
}
63
}
68
64
69
=head1 NAME
65
=head1 NAME
Lines 686-762 sub GetNormalizedOCLCNumber { Link Here
686
    return
682
    return
687
}
683
}
688
684
689
=head2 GetDailyQuote($opts)
690
691
Takes a hashref of options
692
693
Currently supported options are:
694
695
'id'        An exact quote id
696
'random'    Select a random quote
697
noop        When no option is passed in, this sub will return the quote timestamped for the current day
698
699
The function returns an anonymous hash following this format:
700
701
        {
702
          'source' => 'source-of-quote',
703
          'timestamp' => 'timestamp-value',
704
          'text' => 'text-of-quote',
705
          'id' => 'quote-id'
706
        };
707
708
=cut
709
710
# This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
711
# at least for default option
712
713
sub GetDailyQuote {
714
    my %opts = @_;
715
    my $dbh = C4::Context->dbh;
716
    my $query = '';
717
    my $sth = undef;
718
    my $quote = undef;
719
    if ($opts{'id'}) {
720
        $query = 'SELECT * FROM quotes WHERE id = ?';
721
        $sth = $dbh->prepare($query);
722
        $sth->execute($opts{'id'});
723
        $quote = $sth->fetchrow_hashref();
724
    }
725
    elsif ($opts{'random'}) {
726
        # Fall through... we also return a random quote as a catch-all if all else fails
727
    }
728
    else {
729
        $query = 'SELECT * FROM quotes WHERE timestamp LIKE CONCAT(CURRENT_DATE,\'%\') ORDER BY timestamp DESC LIMIT 0,1';
730
        $sth = $dbh->prepare($query);
731
        $sth->execute();
732
        $quote = $sth->fetchrow_hashref();
733
    }
734
    unless ($quote) {        # if there are not matches, choose a random quote
735
        # get a list of all available quote ids
736
        $sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;');
737
        $sth->execute;
738
        my $range = ($sth->fetchrow_array)[0];
739
        # chose a random id within that range if there is more than one quote
740
        my $offset = int(rand($range));
741
        # grab it
742
        $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
743
        $sth = C4::Context->dbh->prepare($query);
744
        # see http://www.perlmonks.org/?node_id=837422 for why
745
        # we're being verbose and using bind_param
746
        $sth->bind_param(1, $offset, SQL_INTEGER);
747
        $sth->execute();
748
        $quote = $sth->fetchrow_hashref();
749
        # update the timestamp for that quote
750
        $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
751
        $sth = C4::Context->dbh->prepare($query);
752
        $sth->execute(
753
            DateTime::Format::MySQL->format_datetime( dt_from_string() ),
754
            $quote->{'id'}
755
        );
756
    }
757
    return $quote;
758
}
759
760
sub _normalize_match_point {
685
sub _normalize_match_point {
761
    my $match_point = shift;
686
    my $match_point = shift;
762
    (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
687
    (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
(-)a/Koha/Quote.pm (+74 lines)
Lines 17-24 package Koha::Quote; Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
use Carp;
19
use Carp;
20
use DateTime::Format::MySQL;
21
use DBI qw(:sql_types);
20
22
21
use Koha::Database;
23
use Koha::Database;
24
use Koha::DateUtils qw(dt_from_string);
22
25
23
use base qw(Koha::Object);
26
use base qw(Koha::Object);
24
27
Lines 32-37 Koha::Quote - Koha Quote object class Link Here
32
35
33
=cut
36
=cut
34
37
38
=head2 get_daily_quote($opts)
39
40
Takes a hashref of options
41
42
Currently supported options are:
43
44
'id'        An exact quote id
45
'random'    Select a random quote
46
noop        When no option is passed in, this sub will return the quote timestamped for the current day
47
48
The function returns an anonymous hash following this format:
49
50
        {
51
          'source' => 'source-of-quote',
52
          'timestamp' => 'timestamp-value',
53
          'text' => 'text-of-quote',
54
          'id' => 'quote-id'
55
        };
56
57
=cut
58
59
# This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
60
# at least for default option
61
62
sub get_daily_quote {
63
    my ($self, %opts) = @_;
64
    my $dbh = C4::Context->dbh;
65
    my $query = '';
66
    my $sth = undef;
67
    my $quote = undef;
68
    if ($opts{'id'}) {
69
        $query = 'SELECT * FROM quotes WHERE id = ?';
70
        $sth = $dbh->prepare($query);
71
        $sth->execute($opts{'id'});
72
        $quote = $sth->fetchrow_hashref();
73
    }
74
    elsif ($opts{'random'}) {
75
        # Fall through... we also return a random quote as a catch-all if all else fails
76
    }
77
    else {
78
        $query = 'SELECT * FROM quotes WHERE timestamp LIKE CONCAT(CURRENT_DATE,\'%\') ORDER BY timestamp DESC LIMIT 0,1';
79
        $sth = $dbh->prepare($query);
80
        $sth->execute();
81
        $quote = $sth->fetchrow_hashref();
82
    }
83
    unless ($quote) {        # if there are not matches, choose a random quote
84
        # get a list of all available quote ids
85
        $sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;');
86
        $sth->execute;
87
        my $range = ($sth->fetchrow_array)[0];
88
        # chose a random id within that range if there is more than one quote
89
        my $offset = int(rand($range));
90
        # grab it
91
        $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
92
        $sth = C4::Context->dbh->prepare($query);
93
        # see http://www.perlmonks.org/?node_id=837422 for why
94
        # we're being verbose and using bind_param
95
        $sth->bind_param(1, $offset, SQL_INTEGER);
96
        $sth->execute();
97
        $quote = $sth->fetchrow_hashref();
98
        # update the timestamp for that quote
99
        $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
100
        $sth = C4::Context->dbh->prepare($query);
101
        $sth->execute(
102
            DateTime::Format::MySQL->format_datetime( dt_from_string() ),
103
            $quote->{'id'}
104
        );
105
    }
106
    return $quote;
107
}
108
35
=head3 _type
109
=head3 _type
36
110
37
=cut
111
=cut
(-)a/opac/opac-main.pl (-2 / +2 lines)
Lines 24-30 use C4::Auth; # get_template_and_user Link Here
24
use C4::Output;
24
use C4::Output;
25
use C4::NewsChannels;    # GetNewsToDisplay
25
use C4::NewsChannels;    # GetNewsToDisplay
26
use C4::Languages qw(getTranslatedLanguages accept_language);
26
use C4::Languages qw(getTranslatedLanguages accept_language);
27
use C4::Koha qw( GetDailyQuote );
27
use Koha::Quote;
28
use C4::Members;
28
use C4::Members;
29
use C4::Overdues;
29
use C4::Overdues;
30
use Koha::Checkouts;
30
use Koha::Checkouts;
Lines 73-79 if (defined $news_id){ Link Here
73
    @all_koha_news   = &GetNewsToDisplay( $template->lang, $homebranch);
73
    @all_koha_news   = &GetNewsToDisplay( $template->lang, $homebranch);
74
}
74
}
75
75
76
my $quote = GetDailyQuote();   # other options are to pass in an exact quote id or select a random quote each pass... see perldoc C4::Koha
76
my $quote = Koha::Quote->get_daily_quote();   # other options are to pass in an exact quote id or select a random quote each pass... see perldoc C4::Koha
77
77
78
# For dashboard
78
# For dashboard
79
my $patron = Koha::Patrons->find( $borrowernumber );
79
my $patron = Koha::Patrons->find( $borrowernumber );
(-)a/t/db_dependent/Koha/GetDailyQuote.t (-31 / +24 lines)
Lines 16-49 Link Here
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
use DateTime::Format::MySQL;
20
use Test::More tests => 12;
20
use Test::More tests => 12;
21
21
22
use C4::Koha qw( GetDailyQuote );
23
use DateTime::Format::MySQL;
24
use Koha::Database;
22
use Koha::Database;
25
use Koha::DateUtils qw(dt_from_string);
23
use Koha::DateUtils qw(dt_from_string);
24
use Koha::Quote;
25
use Koha::Quotes;
26
26
27
BEGIN {
27
BEGIN {
28
    use_ok('C4::Koha');
28
    use_ok('Koha::Quote');
29
}
29
}
30
30
31
can_ok('C4::Koha', qw( GetDailyQuote ));
31
my $quote = Koha::Quote->new();
32
isa_ok( $quote, 'Koha::Quote', 'Quote class returned' );
32
33
33
my $schema = Koha::Database->new->schema;
34
my $schema = Koha::Database->new->schema;
34
$schema->storage->txn_begin;
35
$schema->storage->txn_begin;
35
my $dbh = C4::Context->dbh;
36
my $dbh = C4::Context->dbh;
36
37
37
# Setup stage
38
$dbh->do("DELETE FROM quotes");
39
40
# Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
38
# Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
41
$dbh->do("INSERT INTO `quotes` VALUES
39
my $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string()); #???
42
(6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.',NOW()),
40
my $quote_1 = Koha::Quote->new({ id => 6, source => 'George Washington', text => 'To be prepared for war is one of the most effectual means of preserving peace.', timestamp =>  $timestamp })->store;
43
(7,'Thomas Jefferson','When angry, count ten, before you speak; if very angry, an hundred.',NOW()),
41
my $quote_2 = Koha::Quote->new({ id => 7, source => 'Thomas Jefferson', text => 'When angry, count ten, before you speak; if very angry, an hundred.', timestamp =>  $timestamp })->store;
44
(8,'Abraham Lincoln','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.',NOW()),
42
my $quote_3 = Koha::Quote->new({ id => 8, 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;
45
(9,'Abraham Lincoln','I have always found that mercy bears richer fruits than strict justice.',NOW()),
43
my $quote_4 = Koha::Quote->new({ id => 9, source => 'Abraham Lincoln', text => 'I have always found that mercy bears richer fruits than strict justice.', timestamp =>  $timestamp })->store;
46
(10,'Andrew Johnson','I feel incompetent to perform duties...which have been so unexpectedly thrown upon me.',NOW());");
44
my $quote_5 = Koha::Quote->new({ id => 10, source => 'Andrew Johnson', text => 'I feel incompetent to perform duties...which have been so unexpectedly thrown upon me.', timestamp =>  $timestamp })->store;
47
45
48
my $expected_quote = {
46
my $expected_quote = {
49
    id          => 8,
47
    id          => 8,
Lines 52-84 my $expected_quote = { Link Here
52
    timestamp   => dt_from_string,
50
    timestamp   => dt_from_string,
53
};
51
};
54
52
55
my $quote = GetDailyQuote('id'=>8);
53
$quote = Koha::Quote->get_daily_quote('id'=>$quote_3->id);
56
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
54
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
57
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
55
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
58
56
59
$quote = GetDailyQuote('random'=>1);
57
$quote = Koha::Quote->get_daily_quote('random'=>1);
60
ok($quote, "Got a random quote.");
58
ok($quote, "Got a random quote.");
61
cmp_ok($quote->{'id'}, '>', 0, 'Id is greater than 0');
59
cmp_ok($quote->{'id'}, '>', 0, 'Id is greater than 0');
62
60
63
my $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one
61
$timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one
64
my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
62
Koha::Quotes->search({ id => $expected_quote->{'id'} })->update({ timestamp => $timestamp });
65
my $sth = C4::Context->dbh->prepare($query);
66
$sth->execute( $timestamp , $expected_quote->{'id'});
67
68
$expected_quote->{'timestamp'} = $timestamp;
63
$expected_quote->{'timestamp'} = $timestamp;
69
64
70
$quote = GetDailyQuote(); # this is the "default" mode of selection
65
$quote = Koha::Quote->get_daily_quote(); # this is the "default" mode of selection
71
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
66
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
72
is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct");
67
is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct");
73
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
68
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
74
69
75
$dbh->do(q|DELETE FROM quotes|);
70
$dbh->do(q|DELETE FROM quotes|);
76
$quote = eval {GetDailyQuote();};
71
$quote = eval {Koha::Quote->get_daily_quote();};
77
is( $@, '', 'GetDailyQuote does not die if no quote exist' );
72
is( $@, '', 'get_daily_quote does not die if no quote exist' );
78
is_deeply( $quote, {}, 'GetDailyQuote return an empty hashref is no quote exist'); # Is it what we expect?
73
is_deeply( $quote, {}, 'get_daily_quote return an empty hashref is no quote exist'); # Is it what we expect?
79
$dbh->do(q|INSERT INTO `quotes` VALUES
74
80
    (6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.',NOW())
75
my $quote_6 = Koha::Quote->new({ id => 6, source => 'George Washington', text => 'To be prepared for war is one of the most effectual means of preserving peace.', timestamp =>  dt_from_string() })->store;
81
|);
82
76
83
$quote = GetDailyQuote();
77
$quote = Koha::Quote->get_daily_quote();
84
is( $quote->{id}, 6, ' GetDailyQuote returns the only existing quote' );
78
is( $quote->{id}, 6, ' get_daily_quote returns the only existing quote' );
85
- 

Return to bug 16371