From 3d624637ae52860eaf58e44511f5d5c961664743 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 3 Jan 2018 11:53:59 +0100 Subject: [PATCH] Bug 19909: Show attributes in patron search results Add a new column (hidden by default) in the results table which displays all attributes associated to the corresponding patron. New subroutine Koha::Patron::Attribute::display_value that returns the authorised value description and defaults to the raw value if the attribute's type is not associated to an authorised value category Test plan: 1. Create some patron attribute types, with and without an associated authorised value category 2. Choose an existing patron and set a value for each attribute 3. Go to patron search and do a search that will return your patron (and some others to avoid the redirection) 4. Show column 'Extended attributes' by clicking on 'Column visibility' button 5. Note that all attributes are displayed correctly 6. prove t/db_dependent/Koha/Patron/Attributes.t --- C4/Utils/DataTables/Members.pm | 2 ++ Koha/Patron/Attribute.pm | 33 ++++++++++++++++- admin/columns_settings.yml | 3 ++ .../prog/en/modules/members/member.tt | 2 ++ .../en/modules/members/tables/members_results.tt | 1 + t/db_dependent/Koha/Patron/Attributes.t | 41 +++++++++++++++++++++- 6 files changed, 80 insertions(+), 2 deletions(-) diff --git a/C4/Utils/DataTables/Members.pm b/C4/Utils/DataTables/Members.pm index b2e4963a73..caa6fd6b6d 100644 --- a/C4/Utils/DataTables/Members.pm +++ b/C4/Utils/DataTables/Members.pm @@ -5,6 +5,7 @@ use C4::Context; use C4::Utils::DataTables; use Koha::DateUtils; use C4::Members::Attributes qw(SearchIdMatchingAttribute ); +use Koha::Patron::Attributes; sub search { my ( $params ) = @_; @@ -175,6 +176,7 @@ sub search { my $balance = $patron_object->account->balance; # FIXME Should be formatted from the template $patron->{fines} = sprintf("%.2f", $balance); + $patron->{attributes} = Koha::Patron::Attributes->search({ borrowernumber => $patron_object->borrowernumber }); if($patron->{dateexpiry} and $patron->{dateexpiry} ne '0000-00-00') { $patron->{dateexpiry} = output_pref( { dt => dt_from_string( $patron->{dateexpiry}, 'iso'), dateonly => 1} ); diff --git a/Koha/Patron/Attribute.pm b/Koha/Patron/Attribute.pm index 906fc6d3c9..577110f740 100644 --- a/Koha/Patron/Attribute.pm +++ b/Koha/Patron/Attribute.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Koha::Database; use Koha::Exceptions::Patron::Attribute; use Koha::Patron::Attribute::Types; +use Koha::AuthorisedValues; use base qw(Koha::Object); @@ -91,7 +92,37 @@ sub type { my $self = shift; - return Koha::Patron::Attribute::Types->find( $self->code ); + return scalar Koha::Patron::Attribute::Types->find( $self->code ); +} + +=head3 display_value + + my $display_value = $attribute->display_value; + +Returns the authorised value description corresponding to the attribute if its +type is associated to an authorised value category. + +Returns the raw attribute's value otherwise. + +=cut + +sub display_value { + my $self = shift; + + my $value = $self->attribute; + + my $type = $self->type; + if ($type->authorised_value_category) { + my $authorised_values = Koha::AuthorisedValues->search({ + category => $type->authorised_value_category, + authorised_value => $value, + }); + if ($authorised_values->count) { + $value = $authorised_values->next->lib; + } + } + + return $value; } =head2 Internal methods diff --git a/admin/columns_settings.yml b/admin/columns_settings.yml index 61fa864b38..f3839ec07c 100644 --- a/admin/columns_settings.yml +++ b/admin/columns_settings.yml @@ -199,6 +199,9 @@ modules: - columnname: fines - + columnname: extended_attributes + is_hidden: 1 + - columnname: circ_notes - columnname: actions diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt index 0c5e044d86..6316a1b0e5 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt @@ -235,6 +235,7 @@ $(document).ready(function() { { 'mDataProp': 'dt_dateexpiry' }, { 'mDataProp': 'dt_od_checkouts', 'bSortable': false }, { 'mDataProp': 'dt_fines', 'bSortable': false }, + { 'mDataProp': 'dt_extended_attributes', 'bSortable': false }, { 'mDataProp': 'dt_borrowernotes' }, { 'mDataProp': 'dt_action', 'bSortable': false, 'sClass': 'actions' } ], @@ -403,6 +404,7 @@ function filterByFirstLetterSurname(letter) { Expires on OD/Checkouts Fines + Extended attributes Circ note   diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/tables/members_results.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/tables/members_results.tt index 666b59c7f5..5aad2dd7c5 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/tables/members_results.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/tables/members_results.tt @@ -29,6 +29,7 @@ "[% IF data.overdues %][% data.overdues %][% ELSE %][% data.overdues %][% END %] / [% data.issues %]", "dt_fines": "[% IF data.fines < 0 %][% data.fines | $Price %] [% ELSIF data.fines > 0 %] [% data.fines | $Price %] [% ELSE %] [% data.fines | $Price %] [% END %]", + "dt_extended_attributes": "", "dt_borrowernotes": "[% data.borrowernotes.replace('\\\\' , '\\\\') |html |html_line_break |collapse %]", "dt_action": diff --git a/t/db_dependent/Koha/Patron/Attributes.t b/t/db_dependent/Koha/Patron/Attributes.t index 044f317aa2..79a3f650b1 100644 --- a/t/db_dependent/Koha/Patron/Attributes.t +++ b/t/db_dependent/Koha/Patron/Attributes.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use t::lib::TestBuilder; use Test::Exception; @@ -275,3 +275,42 @@ subtest 'type() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'display_value() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $borrower = $builder->build({ source => 'Borrower' }); + + { + my $category = $builder->build({ source => 'AuthorisedValueCategory' }); + my $attr_type = $builder->build({ + source => 'BorrowerAttributeType', + value => { authorised_value_category => $category->{category_name} }, + }); + my $authorisedvalue = $builder->build({ + source => 'AuthorisedValue', + value => { category => $category->{category_name} }, + }); + my $attribute = Koha::Patron::Attribute->new( + { borrowernumber => $borrower->{borrowernumber}, + code => $attr_type->{code}, + attribute => $authorisedvalue->{authorised_value}, + } + )->store; + + is($attribute->display_value, $authorisedvalue->{lib}, 'display_value() returns the authorised value description'); + } + + { + my $attr_type = $builder->build({ source => 'BorrowerAttributeType' }); + my $attr = $builder->build({ source => 'BorrowerAttribute' }); + my $attribute = Koha::Patron::Attributes->find($attr->{id}); + + is($attribute->display_value, $attr->{attribute}, "display_value() returns the raw attribute's value"); + } + + $schema->storage->txn_rollback; +}; + -- 2.11.0