From df75e06b6fae2773cde2ee15f1596a774eb71389 Mon Sep 17 00:00:00 2001 From: PerplexedTheta 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 --- Koha/Virtualshelf.pm | 37 ++++++++++++++++++- .../atomicupdate/bug_30955-add-new-notice.pl | 15 ++++++++ .../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 +- 6 files changed, 101 insertions(+), 3 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_30955-add-new-notice.pl 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/atomicupdate/bug_30955-add-new-notice.pl b/installer/data/mysql/atomicupdate/bug_30955-add-new-notice.pl new file mode 100755 index 0000000000..b4a6971236 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_30955-add-new-notice.pl @@ -0,0 +1,15 @@ +use Modern::Perl; + +return { + bug_number => "30955", + description => "Send a notice to new owner when transferring shared list", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + $dbh->do( + q{ INSERT IGNORE INTO letter (module, code, name, is_html, title, content, message_transport_type) VALUES ('lists', 'TRANSFER_OWNERSHIP', 'Transfer ownership', 1, 'A list has been transferred to you', "Dear [%- INCLUDE 'patron-title.inc' patron => new_owner -%],
\r\n
\r\nA 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 %].
\r\n
\r\nThank you
\r\n
\r\n", 'email') } + ); + say $out "Added notice TRANSFER_OWNERSHIP"; + }, +}; diff --git a/installer/data/mysql/en/mandatory/sample_notices.yml b/installer/data/mysql/en/mandatory/sample_notices.yml index 7e63d16241..58dad0731a 100644 --- a/installer/data/mysql/en/mandatory/sample_notices.yml +++ b/installer/data/mysql/en/mandatory/sample_notices.yml @@ -1282,6 +1282,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 603c735252..19385ca10c 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)
  • @@ -256,6 +257,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) @@ -418,6 +420,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.3 (Apple Git-146)