From 8bbc9bbe5b428ce1471297fc59b5047e0373d52b Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Wed, 26 Oct 2016 18:06:57 +0300 Subject: [PATCH] [SIGNED-OFF] Bug 17499: Add a method for setting default messaging preferences MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Marc VĂ©ron Signed-off-by: Josef Moravec --- 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 to patron for given +C. + +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.1.4