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

(-)a/Koha/Exceptions.pm (-4 lines)
Lines 50-59 use Exception::Class ( Link Here
50
        isa => 'Koha::Exceptions::Exception',
50
        isa => 'Koha::Exceptions::Exception',
51
        description => 'Koha is under maintenance.'
51
        description => 'Koha is under maintenance.'
52
    },
52
    },
53
    'Koha::Exceptions::UnknownProgramState' => {
54
        isa => 'Koha::Exceptions::Exception',
55
        description => 'The running program has done something terribly unpredicatable',
56
    },
57
    # Virtualshelves exceptions
53
    # Virtualshelves exceptions
58
    'Koha::Exceptions::Virtualshelves::DuplicateObject' => {
54
    'Koha::Exceptions::Virtualshelves::DuplicateObject' => {
59
        isa => 'Koha::Exceptions::DuplicateObject',
55
        isa => 'Koha::Exceptions::DuplicateObject',
(-)a/Koha/Quote.pm (-90 lines)
Lines 17-28 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);
22
20
23
use Koha::Database;
21
use Koha::Database;
24
use Koha::DateUtils qw(dt_from_string);
25
use Koha::Exceptions::UnknownProgramState;
26
use Koha::Quotes;
22
use Koha::Quotes;
27
23
28
use base qw(Koha::Object);
24
use base qw(Koha::Object);
Lines 37-128 Koha::Quote - Koha Quote object class Link Here
37
33
38
=cut
34
=cut
39
35
40
=head2 get_daily_quote($opts)
41
42
Takes a hashref of options
43
44
Currently supported options are:
45
46
'id'        An exact quote id
47
'random'    Select a random quote
48
noop        When no option is passed in, this sub will return the quote timestamped for the current day
49
50
=cut
51
52
# This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
53
# at least for default option
54
55
sub get_daily_quote {
56
    my ($self, %opts) = @_;
57
58
    my $quote = undef;
59
60
    if ($opts{'id'}) {
61
        $quote = Koha::Quotes->find({ id => $opts{'id'} });
62
    }
63
    elsif ($opts{'random'}) {
64
        # Fall through... we also return a random quote as a catch-all if all else fails
65
    }
66
    else {
67
        my $dt = dt_from_string()->ymd();
68
        $quote = Koha::Quotes->search(
69
            {
70
                timestamp => { -like => "$dt%" },
71
            },
72
            {
73
                order_by => { -desc => 'timestamp' },
74
                rows => 1,
75
            }
76
        )->single;
77
    }
78
    unless ($quote) {        # if there are not matches, choose a random quote
79
        my $range = Koha::Quotes->search->count;
80
        my $offset = int(rand($range));
81
        $quote = Koha::Quotes->search(
82
            {},
83
            {
84
                order_by => 'id',
85
                rows => 1,
86
                offset => $offset,
87
            }
88
        )->single;
89
90
        unless($quote){
91
            return;
92
        }
93
94
        # update the timestamp for that quote
95
        my $dt = DateTime::Format::MySQL->format_datetime(dt_from_string());
96
        $quote->update({ timestamp => $dt });
97
    }
98
    return $quote;
99
}
100
101
=head2 get_daily_quote_for_interface
102
103
    my $quote = Koha::Quote->get_daily_quote_for_interface();
104
105
Is a wrapper for get_daily_quote(), with an extra check for using the correct
106
interface defined in the syspref 'QuoteOfTheDay'.
107
If the current interface is not allowed to display quotes, then returns nothing.
108
109
=cut
110
111
sub get_daily_quote_for_interface {
112
    my ($self, %opts) = @_;
113
    my $qotdPref = C4::Context->preference('QuoteOfTheDay');
114
    my $interface = C4::Context->interface();
115
    unless ($interface) {
116
        my @cc = caller(3);
117
        Koha::Exceptions::UnknownProgramState->throw(error => $cc[3]."()> C4::Context->interface() is not set! Don't know are you in OPAC or staff client?");
118
    }
119
    unless ($qotdPref =~ /$interface/) {
120
        return;
121
    }
122
123
    return $self->get_daily_quote(%opts);
124
}
125
126
=head3 _type
36
=head3 _type
127
37
128
=cut
38
=cut
(-)a/Koha/Quotes.pm (-1 / +73 lines)
Lines 17-31 package Koha::Quotes; Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
use Carp;
19
use Carp;
20
use DateTime::Format::MySQL;
20
21
21
use Koha::Database;
22
use Koha::Database;
23
use Koha::DateUtils qw(dt_from_string);
22
use Koha::Quote;
24
use Koha::Quote;
23
25
24
use base qw(Koha::Objects);
26
use base qw(Koha::Objects);
25
27
26
=head1 NAME
28
=head1 NAME
27
29
28
Koha::Quote - Koha Quote object class
30
Koha::Quotes - Koha Quote object class
29
31
30
=head1 API
32
=head1 API
31
33
Lines 33-38 Koha::Quote - Koha Quote object class Link Here
33
35
34
=cut
36
=cut
35
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
=cut
49
50
# This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues...
51
# at least for default option
52
53
sub get_daily_quote {
54
    my ($self, %opts) = @_;
55
56
    my $qotdPref = C4::Context->preference('QuoteOfTheDay');
57
    my $interface = C4::Context->interface();
58
59
    my $dtf  = Koha::Database->new->schema->storage->datetime_parser;
60
61
    unless ($qotdPref =~ /$interface/) {
62
        return;
63
    }
64
65
    my $quote = undef;
66
67
    if ($opts{'id'}) {
68
        $quote = $self->find({ id => $opts{'id'} });
69
    }
70
    elsif ($opts{'random'}) {
71
        # Fall through... we also return a random quote as a catch-all if all else fails
72
    }
73
    else {
74
        my $dt = $dtf->format_date(dt_from_string);
75
        $quote = $self->search(
76
            {
77
                timestamp => { -between => => [ "$dt 00:00:00", "$dt 23:59:59" ] },
78
            },
79
            {
80
                order_by => { -desc => 'timestamp' },
81
                rows => 1,
82
            }
83
        )->single;
84
    }
85
    unless ($quote) {        # if there are not matches, choose a random quote
86
        my $range = $self->search->count;
87
        my $offset = int(rand($range));
88
        $quote = $self->search(
89
            {},
90
            {
91
                order_by => 'id',
92
                rows => 1,
93
                offset => $offset,
94
            }
95
        )->single;
96
97
        unless($quote){
98
            return;
99
        }
100
101
        # update the timestamp for that quote
102
        my $dt = $dtf->format_datetime(dt_from_string);
103
        $quote->update({ timestamp => $dt });
104
    }
105
    return $quote;
106
}
107
36
=head3 type
108
=head3 type
37
109
38
=cut
110
=cut
(-)a/mainpage.pl (-2 / +2 lines)
Lines 32-38 use Koha::Patron::Discharge; Link Here
32
use Koha::Reviews;
32
use Koha::Reviews;
33
use Koha::ArticleRequests;
33
use Koha::ArticleRequests;
34
use Koha::ProblemReports;
34
use Koha::ProblemReports;
35
use Koha::Quote;
35
use Koha::Quotes;
36
36
37
my $query = new CGI;
37
my $query = new CGI;
38
38
Lines 56-62 my $koha_news_count = scalar @$all_koha_news; Link Here
56
$template->param(
56
$template->param(
57
    koha_news       => $all_koha_news,
57
    koha_news       => $all_koha_news,
58
    koha_news_count => $koha_news_count,
58
    koha_news_count => $koha_news_count,
59
    daily_quote     => Koha::Quote->get_daily_quote_for_interface(),
59
    daily_quote     => Koha::Quotes->get_daily_quote(),
60
);
60
);
61
61
62
my $branch =
62
my $branch =
(-)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 Koha::Quote;
27
use Koha::Quotes;
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 99-105 if ( $patron ) { Link Here
99
$template->param(
99
$template->param(
100
    koha_news           => @all_koha_news,
100
    koha_news           => @all_koha_news,
101
    branchcode          => $homebranch,
101
    branchcode          => $homebranch,
102
    daily_quote         => Koha::Quote->get_daily_quote_for_interface(),
102
    daily_quote         => Koha::Quotes->get_daily_quote(),
103
);
103
);
104
104
105
# If GoogleIndicTransliteration system preference is On Set parameter to load Google's javascript in OPAC search screens
105
# If GoogleIndicTransliteration system preference is On Set parameter to load Google's javascript in OPAC search screens
(-)a/t/db_dependent/Koha/Quotes.t (-46 / +26 lines)
Lines 16-23 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
use DateTime::Format::MySQL;
19
use Test::More tests => 15;
20
use Test::More tests => 13;
21
20
22
use Koha::Database;
21
use Koha::Database;
23
use Koha::DateUtils qw(dt_from_string);
22
use Koha::DateUtils qw(dt_from_string);
Lines 29-34 use t::lib::Mocks; Link Here
29
28
30
BEGIN {
29
BEGIN {
31
    use_ok('Koha::Quote');
30
    use_ok('Koha::Quote');
31
    use_ok('Koha::Quotes');
32
}
32
}
33
33
34
my $quote = Koha::Quote->new();
34
my $quote = Koha::Quote->new();
Lines 39-45 $schema->storage->txn_begin; Link Here
39
my $dbh = C4::Context->dbh;
39
my $dbh = C4::Context->dbh;
40
40
41
# Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
41
# Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
42
my $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string());
42
my $dtf = Koha::Database->new->schema->storage->datetime_parser;
43
my $timestamp = $dtf->format_datetime(dt_from_string());
43
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;
44
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;
44
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;
45
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;
45
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;
46
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;
Lines 53-119 my $expected_quote = { Link Here
53
    timestamp   => $timestamp,
54
    timestamp   => $timestamp,
54
};
55
};
55
56
56
$quote = Koha::Quote->get_daily_quote('id'=>$quote_3->id);
57
#First test with QuoteOfTheDay disabled
58
t::lib::Mocks::mock_preference('QuoteOfTheDay', 0);
59
60
##Set interface and get nothing because syspref is not set.
61
C4::Context->interface('opac');
62
$quote = Koha::Quotes->get_daily_quote(id => $quote_1->id);
63
ok(not($quote), "'QuoteOfTheDay'-syspref not set so nothing returned");
64
65
##Set 'QuoteOfTheDay'-syspref to not include current interface 'opac'
66
t::lib::Mocks::mock_preference('QuoteOfTheDay', 'intranet');
67
$quote = Koha::Quotes->get_daily_quote(id => $quote_1->id);
68
ok(not($quote), "'QuoteOfTheDay'-syspref doesn't include 'opac'");
69
70
##Set 'QuoteOfTheDay'-syspref to include current interface 'opac'
71
t::lib::Mocks::mock_preference('QuoteOfTheDay', 'opac,intranet');
72
73
$quote = Koha::Quotes->get_daily_quote('id'=>$quote_3->id);
57
cmp_ok($quote->id, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
74
cmp_ok($quote->id, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
58
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
75
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
59
76
60
$quote = Koha::Quote->get_daily_quote('random'=>1);
77
$quote = Koha::Quotes->get_daily_quote('random'=>1);
61
ok($quote, "Got a random quote.");
78
ok($quote, "Got a random quote.");
62
cmp_ok($quote->id, '>', 0, 'Id is greater than 0');
79
cmp_ok($quote->id, '>', 0, 'Id is greater than 0');
63
80
64
$timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one
81
$timestamp = $dtf->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 });
82
Koha::Quotes->search({ id => $expected_quote->{'id'} })->update({ timestamp => $timestamp });
66
$expected_quote->{'timestamp'} = $timestamp;
83
$expected_quote->{'timestamp'} = $timestamp;
67
84
68
$quote = Koha::Quote->get_daily_quote()->unblessed; # this is the "default" mode of selection
85
$quote = Koha::Quotes->get_daily_quote()->unblessed; # this is the "default" mode of selection
69
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
86
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
70
is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct");
87
is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct");
71
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
88
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
72
89
73
Koha::Quotes->search()->delete();
90
Koha::Quotes->search()->delete();
74
$quote = eval {Koha::Quote->get_daily_quote();};
91
$quote = eval {Koha::Quotes->get_daily_quote();};
75
is( $@, '', 'get_daily_quote does not die if no quote exist' );
92
is( $@, '', 'get_daily_quote does not die if no quote exist' );
76
is_deeply( $quote, undef, 'return undef if quotes do not exists'); # Is it what we expect?
93
is_deeply( $quote, undef, 'return undef if quotes do not exists'); # Is it what we expect?
77
94
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;
95
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
96
80
$quote = Koha::Quote->get_daily_quote();
97
$quote = Koha::Quotes->get_daily_quote();
81
is( $quote->id, $quote_6->id, ' get_daily_quote returns the only existing quote' );
98
is( $quote->id, $quote_6->id, ' get_daily_quote returns the only existing quote' );
82
99
83
$schema->storage->txn_rollback;
100
$schema->storage->txn_rollback;
84
85
subtest "get_daily_quote_for_interface" => sub {
86
87
    plan tests => 3;
88
89
    $schema->storage->txn_begin;
90
91
    my ($quote);
92
    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;
93
94
    my $expected_quote = {
95
        id          => $quote_1->id,
96
        source      => 'Dusk And Her Embrace',
97
        text        => 'Unfurl thy limbs breathless succubus<br/>How the full embosomed fog<br/>Imparts the night to us....',
98
        timestamp   => DateTime::Format::MySQL->format_datetime(dt_from_string),
99
    };
100
101
    t::lib::Mocks::mock_preference('QuoteOfTheDay', 0);
102
103
    ##Set interface and get nothing because syspref is not set.
104
    C4::Context->interface('opac');
105
    $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id);
106
    ok(not($quote), "'QuoteOfTheDay'-syspref not set so nothing returned");
107
108
    ##Set 'QuoteOfTheDay'-syspref to not include current interface 'opac'
109
    t::lib::Mocks::mock_preference('QuoteOfTheDay', 'intranet');
110
    $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id);
111
    ok(not($quote), "'QuoteOfTheDay'-syspref doesn't include 'opac'");
112
113
    ##Set 'QuoteOfTheDay'-syspref to include current interface 'opac'
114
    t::lib::Mocks::mock_preference('QuoteOfTheDay', 'opac,intranet');
115
    $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id)->unblessed;
116
    is_deeply($quote, $expected_quote, "Got the expected quote");
117
118
    $schema->storage->txn_rollback;
119
};
120
- 

Return to bug 16371