From 85ba989f82093ea79bac2ba98c117486f01acecb Mon Sep 17 00:00:00 2001 From: Jake Deery Date: Wed, 10 Jul 2024 16:18:26 +0100 Subject: [PATCH] Bug 30955: Sends notice to new owner at transfer This patch adds a new notice, TRANSFER_OWNERSHIP, under a new module, Lists. When a list is transferred to a new owner, this notice is triggered, containing a short paragraph detailing the list name. TO TEST: a) log in as a koha superlibrarian b) go to the lists module, create a public list, and make a note of the title used c) on the lists module, transfer the list to some other user, and make a note of the user used d) go to the patron notices page for the user in step c e) ensure that a notice has been generated 1) check the contents of the notice, it should contain - the new owner's name - the list's title - the old owner's name & (if set) email f) under tools > notices, modify the notice for TRANSFER_OWNERSHIP g) repeat steps c-e 1) ensure the modifications you've made are now visible in the notice Signed-off-by: Roman Dolny Signed-off-by: Lucas Gass Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/Virtualshelf.pm | 37 ++++++++++++++++++- .../mysql/en/mandatory/sample_notices.yml | 16 ++++++++ .../prog/en/modules/tools/letter.tt | 7 ++++ t/db_dependent/Koha/Virtualshelf.t | 27 +++++++++++++- virtualshelves/shelves.pl | 2 +- 5 files changed, 86 insertions(+), 3 deletions(-) diff --git a/Koha/Virtualshelf.pm b/Koha/Virtualshelf.pm index 930c4d6239..1cb82a1c5c 100644 --- a/Koha/Virtualshelf.pm +++ b/Koha/Virtualshelf.pm @@ -360,8 +360,43 @@ sub transfer_ownership { Koha::Exceptions::MissingParameter->throw("Mandatory parameter 'patron' missing") unless $patron_id; + ## before we change the owner, collect some details + my $old_owner = Koha::Patrons->find( $self->owner ); + my $new_owner = Koha::Patrons->find($patron_id); + my $library = $new_owner->library->unblessed; + + ## first we change the owner $self->remove_share($patron_id) if $self->is_private; - return $self->set( { owner => $patron_id } )->store; + $self->set( { owner => $patron_id } )->store; + + ## now we message the new owner + my $letter = C4::Letters::GetPreparedLetter( + module => 'lists', + letter_code => 'TRANSFER_OWNERSHIP', + branchcode => $library->{branchcode}, + lang => $new_owner->lang || 'default', + objects => { + old_owner => $old_owner, + new_owner => $new_owner, + shelf => $self, + }, + tables => { + 'branches' => $library->{branchcode}, + }, + message_transport_type => 'email', + ); + + if ($letter) { + my $message_id = C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $patron_id, + message_transport_type => 'email', + } + ) or warn "can't enqueue letter $letter"; + } + + return $self; } =head2 Internal methods diff --git a/installer/data/mysql/en/mandatory/sample_notices.yml b/installer/data/mysql/en/mandatory/sample_notices.yml index bbbd7417f6..3ca1a9aea5 100644 --- a/installer/data/mysql/en/mandatory/sample_notices.yml +++ b/installer/data/mysql/en/mandatory/sample_notices.yml @@ -1329,6 +1329,22 @@ tables: - "[% branch.branchillemail %]" - "[% branch.branchemail %]" + - module: lists + code: TRANSFER_OWNERSHIP + branchcode: "" + name: "Transfer ownership" + is_html: 1 + title: "A list has been transferred to you" + message_transport_type: email + lang: default + content: + - "Dear [%- INCLUDE 'patron-title.inc' patron => new_owner -%],
" + - "
" + - "A public list, titled [% shelf.shelfname | html %], has been transferred to you[% IF ( old_owner ) %] by [%- INCLUDE 'patron-title.inc' patron => old_owner%][% IF ( old_owner.email ) %] ([% old_owner.email | html %])[% END %][% END %].
" + - "
" + - "Thank you
" + - "
" + - module: members code: DISCHARGE branchcode: "" diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt index d24fa422b3..784d15d0f8 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt @@ -194,6 +194,7 @@
  • Claim serial issue
  • Holds
  • Interlibrary loans
  • +
  • Lists
  • Order acquisition
  • Patrons
  • Patrons (custom slip)
  • @@ -257,6 +258,7 @@ [% CASE 'claimissues' %]Claim serial issue [% CASE 'reserves' %]Holds [% CASE 'ill' %]Interlibrary loans + [% CASE 'lists' %]Lists [% CASE 'members' %]Patrons [% CASE 'patron_slip' %]Patrons (custom slip) [% CASE 'add_message' %]Patrons (custom message) @@ -420,6 +422,11 @@ [% IF ( module == "ill" ) %] [% ELSE %] + [% IF ( module == "lists" ) %] + + [% ELSE %] + + [% END %] [% END %] [% IF ( module == "members" ) %] diff --git a/t/db_dependent/Koha/Virtualshelf.t b/t/db_dependent/Koha/Virtualshelf.t index af6c2e1b32..c7061cc7b9 100755 --- a/t/db_dependent/Koha/Virtualshelf.t +++ b/t/db_dependent/Koha/Virtualshelf.t @@ -30,7 +30,7 @@ my $builder = t::lib::TestBuilder->new; subtest 'transfer_ownership() tests' => sub { - plan tests => 8; + plan tests => 13; $schema->storage->txn_begin; @@ -95,5 +95,30 @@ subtest 'transfer_ownership() tests' => sub { is( $private_list_shares->count, 1, 'Count is correct' ); is( $private_list_shares->next->borrowernumber, $patron_3->id, "Private lists get the share for the new owner removed" ); + my %params; + my $mocked_letters = Test::MockModule->new('C4::Letters'); + $mocked_letters->mock( + 'GetPreparedLetter', + sub { + %params = @_; + return 1; + } + ); + $mocked_letters->mock( + 'EnqueueLetter', + sub { + return 1; + } + ); + + $public_list->transfer_ownership( $patron_1->id ); + $public_list->discard_changes; + + is( $params{module}, "lists", "Enqueued letter with module lists correctly" ); + is( $params{letter_code}, "TRANSFER_OWNERSHIP", "Enqueued letter with code TRANSFER_OWNERSHIP correctly" ); + is( $params{objects}->{old_owner}->borrowernumber, $patron_2->borrowernumber, "old_owner passed to enqueue letter correctly" ); + is( $params{objects}->{new_owner}->borrowernumber, $patron_1->borrowernumber, "new_owner passed to enqueue letter correctly" ); + is( $params{objects}->{shelf}->shelfnumber, $public_list->shelfnumber, "shelf passed to enqueue letter correctly" ); + $schema->storage->txn_rollback; }; diff --git a/virtualshelves/shelves.pl b/virtualshelves/shelves.pl index e3e7a868e4..c60038fddd 100755 --- a/virtualshelves/shelves.pl +++ b/virtualshelves/shelves.pl @@ -266,7 +266,7 @@ if ( $op eq 'add_form' ) { push @messages, { type => 'error', code => $error_code }; $op = 'list'; } else { - $shelf->owner($new_owner)->store; + $shelf->transfer_ownership($new_owner); $op = 'list'; } } -- 2.39.5