From c37e1329c011869433a8a11ae7db3ab7ad912aa3 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 Signed-off-by: Cab Vinton --- 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 | 40 +++++++++++++++++++++- 6 files changed, 79 insertions(+), 2 deletions(-) diff --git a/C4/Utils/DataTables/Members.pm b/C4/Utils/DataTables/Members.pm index 1e3f642..030c78c 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 ) = @_; @@ -183,6 +184,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} ) { # FIXME We should not format the date here, do it in template-side instead diff --git a/Koha/Patron/Attribute.pm b/Koha/Patron/Attribute.pm index 906fc6d..577110f 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 3e2a672..3195dce 100644 --- a/admin/columns_settings.yml +++ b/admin/columns_settings.yml @@ -222,6 +222,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 1a765a6..432f4ce 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt @@ -108,6 +108,7 @@ Expires on OD/Checkouts Fines + Extended attributes Circ note   @@ -481,6 +482,7 @@ { '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' } ], 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 6799c31..d4a0ea7 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 @@ -30,6 +30,7 @@ "[% IF data.overdues %][% data.overdues | html %][% ELSE %][% data.overdues | html %][% END %] / [% data.issues | html %]", "dt_fines": "[% IF data.fines < 0 %][% data.fines | $Price | html %] [% ELSIF data.fines > 0 %] [% data.fines | $Price | html %] [% ELSE %] [% data.fines | $Price | html %] [% END %]", + "dt_extended_attributes": "", "dt_borrowernotes": "[% data.borrowernotes.replace('\\\\' , '\\\\') |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 044f317..fb406c3 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,41 @@ 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.1.4