Bugzilla – Attachment 63432 Details for
Bug 17499
Koha objects for messaging preferences
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 17499: Add a method for setting default messaging preferences
Bug-17499-Add-a-method-for-setting-default-messagi.patch (text/plain), 8.17 KB, created by
Lari Taskula
on 2017-05-12 13:26:48 UTC
(
hide
)
Description:
Bug 17499: Add a method for setting default messaging preferences
Filename:
MIME Type:
Creator:
Lari Taskula
Created:
2017-05-12 13:26:48 UTC
Size:
8.17 KB
patch
obsolete
>From f362c9bdabae2106d1ac3c83d65b1567220cc1ac Mon Sep 17 00:00:00 2001 >From: Lari Taskula <lari.taskula@jns.fi> >Date: Wed, 26 Oct 2016 18:06:57 +0300 >Subject: [PATCH] Bug 17499: Add a method for setting default messaging > preferences > >This patch adds a method Koha::Patron::Message::Preference->new_from_default that >can be used to add category's default messaging preferences to patron for a given >message type. > >Example call: > >Koha::Patron::Message::Preference->new_from_default({ > borrowernumber => 123, > categorycode => "ABC", > message_attribute_id => 1, >}); > >Also adds a simple method for Koha::Patron, set_default_messaging_preferences. >Usage: $patron->set_default_messaging_preferences() > >To test: >1. Run t/db_dependent/Koha/Patron/Message/Preferences.t >--- > Koha/Exceptions.pm | 4 ++ > Koha/Patron.pm | 28 +++++++++++ > Koha/Patron/Message/Preference.pm | 63 ++++++++++++++++++++++++ > t/db_dependent/Koha/Patron/Message/Preferences.t | 36 +++++++++++++- > 4 files changed, 130 insertions(+), 1 deletion(-) > >diff --git a/Koha/Exceptions.pm b/Koha/Exceptions.pm >index ff04b9e..793aafa 100644 >--- a/Koha/Exceptions.pm >+++ b/Koha/Exceptions.pm >@@ -44,6 +44,10 @@ use Exception::Class ( > isa => 'Koha::Exceptions::Exception', > description => 'General problem adding a library limit' > }, >+ 'Koha::Exceptions::UnknownObject' => { >+ isa => 'Koha::Exceptions::Exception', >+ description => 'Object cannot be found or is not known', >+ }, > # Virtualshelves exceptions > 'Koha::Exceptions::Virtualshelves::DuplicateObject' => { > isa => 'Koha::Exceptions::DuplicateObject', >diff --git a/Koha/Patron.pm b/Koha/Patron.pm >index b97bceb..d2cc7c7 100644 >--- a/Koha/Patron.pm >+++ b/Koha/Patron.pm >@@ -33,6 +33,7 @@ use Koha::Patron::Categories; > use Koha::Patron::HouseboundProfile; > use Koha::Patron::HouseboundRole; > use Koha::Patron::Images; >+use Koha::Patron::Message::Preferences; > use Koha::Patrons; > use Koha::Virtualshelves; > use Koha::Club::Enrollments; >@@ -630,6 +631,33 @@ sub get_enrollable_clubs { > return wantarray ? $e->as_list : $e; > } > >+=head3 set_default_messaging_preferences >+ >+ $patron->set_default_messaging_preferences >+ >+Sets default messaging preferences on patron. >+ >+See Koha::Patron::Message::Preference(s) for more documentation, especially on >+thrown exceptions. >+ >+=cut >+ >+sub set_default_messaging_preferences { >+ my ($self, $categorycode) = @_; >+ >+ my $options = Koha::Patron::Message::Preferences->get_options; >+ >+ foreach my $option (@$options) { >+ Koha::Patron::Message::Preference->new_from_default({ >+ borrowernumber => $self->borrowernumber, >+ categorycode => $categorycode || $self->categorycode, >+ message_attribute_id => $option->{message_attribute_id}, >+ })->store; >+ } >+ >+ return $self; >+} >+ > =head3 type > > =cut >diff --git a/Koha/Patron/Message/Preference.pm b/Koha/Patron/Message/Preference.pm >index df0d0ec..8fa1910 100644 >--- a/Koha/Patron/Message/Preference.pm >+++ b/Koha/Patron/Message/Preference.pm >@@ -23,6 +23,7 @@ use Koha::Database; > use Koha::Exceptions; > use Koha::Patron::Categories; > use Koha::Patron::Message::Preferences; >+use Koha::Patron::Message::Transport::Preferences; > use Koha::Patrons; > > use base qw(Koha::Object); >@@ -77,6 +78,68 @@ sub new { > return $self; > } > >+=head3 new_from_default >+ >+my $preference = Koha::Patron::Message::Preference->new_from_default({ >+ borrowernumber => 123, >+ categorycode => 'ABC', >+ message_attribute_id => 1, >+})->store; >+ >+Sets default messaging preference for C<categorycode> to patron for given >+C<message_attribute_id>. >+ >+Throws Koha::Exceptions::MissingParameter if any of following is missing: >+- categorycode >+- borrowernumber >+- message_attribute_id >+ >+Throws Koha::Exceptions::UnknownObject if default preferences are not found. >+ >+=cut >+ >+sub new_from_default { >+ my ($class, $params) = @_; >+ >+ my @required = qw(borrowernumber categorycode message_attribute_id); >+ foreach my $p (@required) { >+ Koha::Exceptions::MissingParameter->throw( >+ error => "Missing required parameter.", >+ parameter => $p, >+ ) unless exists $params->{$p}; >+ } >+ >+ my $default = Koha::Patron::Message::Preferences->find({ >+ categorycode => $params->{'categorycode'}, >+ message_attribute_id => $params->{'message_attribute_id'}, >+ }); >+ Koha::Exceptions::UnknownObject->throw( >+ error => "Default messaging preference for given categorycode and" >+ ." message_attribute_id cannot be found.", >+ ) unless $default; >+ $default = $default->unblessed; >+ >+ # Add a new messaging preference for patron >+ my $self = $class->SUPER::new({ >+ borrowernumber => $params->{'borrowernumber'}, >+ message_attribute_id => $default->{'message_attribute_id'}, >+ days_in_advance => $default->{'days_in_advance'}, >+ wants_digest => $default->{'wants_digest'}, >+ })->store; >+ >+ # Set default messaging transport types >+ my $default_transport_types = Koha::Patron::Message::Transport::Preferences-> >+ search({ borrower_message_preference_id => $default->{'borrower_message_preference_id'} }); >+ while (my $transport = $default_transport_types->next) { >+ Koha::Patron::Message::Transport::Preference->new({ >+ borrower_message_preference_id => $self->borrower_message_preference_id, >+ message_transport_type => $transport->message_transport_type, >+ })->store; >+ } >+ >+ return $self; >+} >+ > =head3 store > > Makes a validation before actual Koha::Object->store so that proper exceptions >diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t >index 91d77e3..07a6e04 100644 >--- a/t/db_dependent/Koha/Patron/Message/Preferences.t >+++ b/t/db_dependent/Koha/Patron/Message/Preferences.t >@@ -126,7 +126,7 @@ subtest 'Add a test messaging transport' => sub { > }; > > subtest 'Add a messaging preference to patron' => sub { >- plan tests => 4; >+ plan tests => 5; > > ok($preference = Koha::Patron::Message::Preference->new({ > borrowernumber => $patron->{'borrowernumber'}, >@@ -147,6 +147,40 @@ subtest 'Add a messaging preference to patron' => sub { > "Adding a duplicate preference" > ." => Koha::Exceptions::DuplicateObject"); > >+ subtest 'Add preferences from defaults' => sub { >+ plan tests => 4; >+ >+ my $attr = Koha::Patron::Message::Attribute->new({ >+ message_name => "default", >+ })->store; >+ ok(my $default = Koha::Patron::Message::Preference->new({ >+ categorycode => $patron->{'categorycode'}, >+ message_attribute_id => $attr->message_attribute_id, >+ wants_digest => 1, >+ days_in_advance => 1337, >+ })->store, "Created a new default preference for category."); >+ Koha::Patron::Message::Transport::Preference->new({ >+ borrower_message_preference_id => $default->borrower_message_preference_id, >+ message_transport_type => "sms", >+ })->store; >+ Koha::Patron::Message::Transport::Preference->new({ >+ borrower_message_preference_id => $default->borrower_message_preference_id, >+ message_transport_type => "email", >+ })->store; >+ ok(Koha::Patron::Message::Preference->new_from_default({ >+ borrowernumber => $patron->{'borrowernumber'}, >+ categorycode => $patron->{'categorycode'}, >+ message_attribute_id => $attr->message_attribute_id, >+ })->store, "Added a default preference to patron."); >+ ok(my $pref = Koha::Patron::Message::Preferences->find({ >+ borrowernumber => $patron->{'borrowernumber'}, >+ message_attribute_id => $attr->message_attribute_id, >+ }), "Found the default preference from patron."); >+ is(Koha::Patron::Message::Transport::Preferences->search({ >+ borrower_message_preference_id => $pref->borrower_message_preference_id >+ })->count, 2, "Found the two transport types that we set earlier"); >+ }; >+ > subtest 'Attempt to add a messaging preference with invalid parameters' => sub { > plan tests => 6; > >-- >2.7.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 17499
:
63427
|
63428
|
63429
|
63430
|
63431
|
63432
|
63433
|
63434
|
63786
|
63787
|
63788
|
63789
|
63790
|
63791
|
63792
|
63793
|
63801
|
63802
|
63803
|
63804
|
63805
|
63806
|
63807
|
63808
|
63809
|
63810
|
63811
|
63812
|
63878
|
63895
|
64035
|
64036
|
64037
|
64038
|
64039
|
64040
|
64041
|
64042
|
64043
|
64044
|
64795
|
66686
|
69434
|
69435
|
69458
|
91864
|
93863
|
93865
|
93872
|
94748
|
99479
|
99634
|
99668
|
99669
|
99670
|
103441
|
103442
|
103443
|
106653
|
106654
|
106655
|
106656
|
106657
|
107612
|
107613
|
107614
|
107615
|
107616
|
113010
|
113011
|
113127
|
113129
|
113686
|
113687
|
113765
|
113766
|
113767
|
113768
|
118452
|
118453
|
119470
|
119471
|
119472
|
119473
|
119474
|
119475
|
119476
|
119477
|
119478
|
119479
|
119480
|
119481
|
119482
|
119483
|
119484
|
119485
|
119486
|
119487
|
119488
|
119489
|
119490
|
119491
|
141408
|
141409
|
141410
|
141411
|
141412
|
141413
|
141414
|
141415
|
141416
|
141417
|
141418
|
146529
|
146530
|
146531
|
146532
|
146533
|
146534
|
146535
|
146536
|
146537
|
146538
|
146539
|
146540
|
151900
|
151901
|
151902
|
151903
|
151904
|
151905
|
151906
|
151907
|
151908
|
151909
|
151910
|
151911
|
151937
|
151938
|
151939
|
151940
|
151941
|
151942
|
151943
|
151944
|
151945
|
151946
|
151947
|
151948
|
155365
|
155366
|
155367
|
155368
|
155369
|
155370
|
155371
|
155372
|
155373
|
155374
|
155375
|
155376