Bugzilla – Attachment 63791 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), 11.26 KB, created by
Lari Taskula
on 2017-05-29 09:57:36 UTC
(
hide
)
Description:
Bug 17499: Add a method for setting default messaging preferences
Filename:
MIME Type:
Creator:
Lari Taskula
Created:
2017-05-29 09:57:36 UTC
Size:
11.26 KB
patch
obsolete
>From 8e9ef3d7ded606e8aa9bbe78910d21c5ae463b43 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 | 35 ++++++++ > Koha/Patron/Message/Preference.pm | 74 ++++++++++++++++ > t/db_dependent/Koha/Patron/Message/Preferences.t | 106 ++++++++++++++++++++++- > 4 files changed, 218 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 015cb41..1704844 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; >@@ -653,6 +654,40 @@ sub account_locked { > and $self->login_attempts >= $FailedLoginAttempts )? 1 : 0; > } > >+=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) { >+ # Check that this option has preference configuration for this category >+ unless (Koha::Patron::Message::Preferences->search({ >+ message_attribute_id => $option->{message_attribute_id}, >+ categorycode => $categorycode || $self->categorycode, >+ })->count) { >+ next; >+ } >+ 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 447919e..9f63782 100644 >--- a/Koha/Patron/Message/Preference.pm >+++ b/Koha/Patron/Message/Preference.pm >@@ -23,6 +23,8 @@ use Koha::Database; > use Koha::Exceptions; > use Koha::Patron::Categories; > use Koha::Patron::Message::Attributes; >+use Koha::Patron::Message::Preferences; >+use Koha::Patron::Message::Transport::Preferences; > use Koha::Patron::Message::Transports; > use Koha::Patrons; > >@@ -70,6 +72,78 @@ sub new { > return $self; > } > >+=head3 new_from_default >+ >+my $preference = Koha::Patron::Message::Preference->new_from_default({ >+ borrowernumber => 123, >+ categorycode => 'ABC', # if not given, patron's categorycode will be used >+ message_attribute_id => 1, >+}); >+ >+NOTE: This subroutine initializes and STORES the object (in order to set >+message transport types for the preference), so no need to call ->store when >+preferences are initialized via this method. >+ >+Stores default messaging preference for C<categorycode> to patron for given >+C<message_attribute_id>. >+ >+Throws Koha::Exceptions::MissingParameter if any of following is missing: >+- 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 message_attribute_id); >+ foreach my $p (@required) { >+ Koha::Exceptions::MissingParameter->throw( >+ error => 'Missing required parameter.', >+ parameter => $p, >+ ) unless exists $params->{$p}; >+ } >+ unless ($params->{'categorycode'}) { >+ my $patron = Koha::Patrons->find($params->{borrowernumber}); >+ $params->{'categorycode'} = $patron->categorycode; >+ } >+ >+ 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 2f20039..8f09826 100644 >--- a/t/db_dependent/Koha/Patron/Message/Preferences.t >+++ b/t/db_dependent/Koha/Patron/Message/Preferences.t >@@ -19,7 +19,7 @@ > > use Modern::Perl; > >-use Test::More tests => 4; >+use Test::More tests => 5; > > use t::lib::Mocks; > use t::lib::TestBuilder; >@@ -148,6 +148,31 @@ subtest 'Test Koha::Patron::Message::Preferences->get_options' => sub { > }; > }; > >+subtest 'Add preferences from defaults' => sub { >+ plan tests => 3; >+ >+ $schema->storage->txn_begin; >+ >+ my $patron = build_a_test_patron(); >+ my ($default, $mtt1, $mtt2) = build_a_test_category_preference({ >+ patron => $patron, >+ }); >+ ok(Koha::Patron::Message::Preference->new_from_default({ >+ borrowernumber => $patron->borrowernumber, >+ categorycode => $patron->categorycode, >+ message_attribute_id => $default->message_attribute_id, >+ })->store, 'Added a default preference to patron.'); >+ ok(my $pref = Koha::Patron::Message::Preferences->find({ >+ borrowernumber => $patron->borrowernumber, >+ message_attribute_id => $default->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'); >+ >+ $schema->storage->txn_rollback; >+}; >+ > subtest 'Test adding a new preference with invalid parameters' => sub { > plan tests => 4; > >@@ -280,6 +305,28 @@ sub build_a_test_category { > return Koha::Patron::Categories->find($categorycode); > } > >+sub build_a_test_letter { >+ my ($params) = @_; >+ >+ my $mtt = $params->{mtt} ? $params->{mtt} : 'email'; >+ my $branchcode = $builder->build({ >+ source => 'Branch' })->{branchcode}; >+ my $letter = $builder->build({ >+ source => 'Letter', >+ value => { >+ branchcode => '', >+ is_html => 0, >+ message_transport_type => $mtt >+ } >+ }); >+ >+ return Koha::Notice::Templates->find({ >+ module => $letter->{module}, >+ code => $letter->{code}, >+ branchcode => $letter->{branchcode}, >+ }); >+} >+ > sub build_a_test_patron { > my $categorycode = $builder->build({ > source => 'Category' })->{categorycode}; >@@ -291,4 +338,61 @@ sub build_a_test_patron { > return Koha::Patrons->find($borrowernumber); > } > >+sub build_a_test_transport_type { >+ my $mtt = $builder->build({ >+ source => 'MessageTransportType' }); >+ >+ return Koha::Patron::Message::Transport::Types->find( >+ $mtt->{message_transport_type} >+ ); >+} >+ >+sub build_a_test_category_preference { >+ my ($params) = @_; >+ >+ my $patron = $params->{patron}; >+ my $attr = $params->{attr} >+ ? $params->{attr} >+ : build_a_test_attribute($params->{days_in_advance}); >+ >+ my $letter = $params->{letter} ? $params->{letter} : build_a_test_letter(); >+ my $mtt1 = $params->{mtt1} ? $params->{mtt1} : build_a_test_transport_type(); >+ my $mtt2 = $params->{mtt2} ? $params->{mtt2} : build_a_test_transport_type(); >+ >+ Koha::Patron::Message::Transport->new({ >+ message_attribute_id => $attr->message_attribute_id, >+ message_transport_type => $mtt1->message_transport_type, >+ is_digest => $params->{digest} ? 1 : 0, >+ letter_module => $letter->module, >+ letter_code => $letter->code, >+ })->store; >+ >+ Koha::Patron::Message::Transport->new({ >+ message_attribute_id => $attr->message_attribute_id, >+ message_transport_type => $mtt2->message_transport_type, >+ is_digest => $params->{digest} ? 1 : 0, >+ letter_module => $letter->module, >+ letter_code => $letter->code, >+ })->store; >+ >+ my $default = Koha::Patron::Message::Preference->new({ >+ categorycode => $patron->categorycode, >+ message_attribute_id => $attr->message_attribute_id, >+ wants_digest => $params->{digest} ? 1 : 0, >+ days_in_advance => $params->{days_in_advance} >+ ? $params->{days_in_advance} : 0, >+ })->store; >+ >+ Koha::Patron::Message::Transport::Preference->new({ >+ borrower_message_preference_id => $default->borrower_message_preference_id, >+ message_transport_type => $mtt1->message_transport_type, >+ })->store; >+ Koha::Patron::Message::Transport::Preference->new({ >+ borrower_message_preference_id => $default->borrower_message_preference_id, >+ message_transport_type => $mtt2->message_transport_type, >+ })->store; >+ >+ return ($default, $mtt1, $mtt2); >+} >+ > 1; >-- >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