View | Details | Raw Unified | Return to bug 20443
Collapse All | Expand All

(-)a/C4/Members/Attributes.pm (-23 / +1 lines)
Lines 29-35 our ($csv, $AttributeTypes); Link Here
29
29
30
BEGIN {
30
BEGIN {
31
    @ISA = qw(Exporter);
31
    @ISA = qw(Exporter);
32
    @EXPORT_OK = qw(GetBorrowerAttributes GetBorrowerAttributeValue CheckUniqueness SetBorrowerAttributes
32
    @EXPORT_OK = qw(GetBorrowerAttributes CheckUniqueness SetBorrowerAttributes
33
                    DeleteBorrowerAttribute UpdateBorrowerAttribute
33
                    DeleteBorrowerAttribute UpdateBorrowerAttribute
34
                    extended_attributes_code_value_arrayref extended_attributes_merge
34
                    extended_attributes_code_value_arrayref extended_attributes_merge
35
                    SearchIdMatchingAttribute);
35
                    SearchIdMatchingAttribute);
Lines 95-122 sub GetBorrowerAttributes { Link Here
95
    return \@results;
95
    return \@results;
96
}
96
}
97
97
98
=head2 GetBorrowerAttributeValue
99
100
  my $value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attribute_code);
101
102
Retrieve the value of an extended attribute C<$attribute_code> associated with the
103
patron specified by C<$borrowernumber>.
104
105
=cut
106
107
sub GetBorrowerAttributeValue {
108
    my $borrowernumber = shift;
109
    my $code = shift;
110
111
    my $dbh = C4::Context->dbh();
112
    my $query = "SELECT attribute
113
                 FROM borrower_attributes
114
                 WHERE borrowernumber = ?
115
                 AND code = ?";
116
    my $value = $dbh->selectrow_array($query, undef, $borrowernumber, $code);
117
    return $value;
118
}
119
120
=head2 SearchIdMatchingAttribute
98
=head2 SearchIdMatchingAttribute
121
99
122
  my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
100
  my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
(-)a/Koha/Patron.pm (+24 lines)
Lines 1557-1562 sub add_guarantor { Link Here
1557
    )->store();
1557
    )->store();
1558
}
1558
}
1559
1559
1560
=head3 get_extended_attribute_value
1561
1562
my $attribute_value = $patron->get_extended_attribute_value( $code );
1563
1564
Return the attribute's value for the code passed in parameter.
1565
1566
It not exist it returns undef
1567
1568
Note that this will not work for repeatable attribute types.
1569
1570
Maybe you certainly not want to use this method, it is actually only used for SHOW_BARCODE
1571
(which should be a real patron's attribute (not extended)
1572
1573
=cut
1574
1575
sub get_extended_attribute_value {
1576
    my ( $self, $code ) = @_;
1577
    my $rs = $self->_result->borrower_attributes;
1578
    return unless $rs;
1579
    my $attribute = $rs->search( { code => $code } );
1580
    return unless $attribute->count;
1581
    return $attribute->next->attribute;
1582
}
1583
1560
=head3 to_api
1584
=head3 to_api
1561
1585
1562
    my $json = $patron->to_api;
1586
    my $json = $patron->to_api;
(-)a/opac/opac-user.pl (-3 / +2 lines)
Lines 28-34 use C4::External::BakerTaylor qw( image_url link_url ); Link Here
28
use C4::Reserves;
28
use C4::Reserves;
29
use C4::Members;
29
use C4::Members;
30
use C4::Members::AttributeTypes;
30
use C4::Members::AttributeTypes;
31
use C4::Members::Attributes qw/GetBorrowerAttributeValue/;
32
use C4::Output;
31
use C4::Output;
33
use C4::Biblio;
32
use C4::Biblio;
34
use C4::Items;
33
use C4::Items;
Lines 287-296 $template->param( canrenew => $canrenew ); Link Here
287
$template->param( OVERDUES       => \@overdues );
286
$template->param( OVERDUES       => \@overdues );
288
$template->param( overdues_count => $overdues_count );
287
$template->param( overdues_count => $overdues_count );
289
288
290
my $show_barcode = Koha::Patron::Attribute::Types->search(
289
my $show_barcode = Koha::Patron::Attribute::Types->search( # FIXME we should not need this search
291
    { code => ATTRIBUTE_SHOW_BARCODE } )->count;
290
    { code => ATTRIBUTE_SHOW_BARCODE } )->count;
292
if ($show_barcode) {
291
if ($show_barcode) {
293
    my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
292
    my $patron_show_barcode = $patron->get_extended_attribute_value(ATTRIBUTE_SHOW_BARCODE);
294
    undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
293
    undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
295
}
294
}
296
$template->param( show_barcode => 1 ) if $show_barcode;
295
$template->param( show_barcode => 1 ) if $show_barcode;
(-)a/t/db_dependent/Auth_with_ldap.t (-2 / +4 lines)
Lines 117-122 $builder->build( Link Here
117
    }
117
    }
118
);
118
);
119
119
120
my $patron = Koha::Patrons->find($borrower->{borrowernumber});
121
120
# C4::Auth_with_ldap needs several stuff set first ^^^
122
# C4::Auth_with_ldap needs several stuff set first ^^^
121
use_ok('C4::Auth_with_ldap');
123
use_ok('C4::Auth_with_ldap');
122
can_ok(
124
can_ok(
Lines 212-224 subtest 'checkpw_ldap tests' => sub { Link Here
212
            'Extended attributes are not deleted'
214
            'Extended attributes are not deleted'
213
        );
215
        );
214
216
215
        is( C4::Members::Attributes::GetBorrowerAttributeValue($borrower->{borrowernumber}, $attr_type2->{code}), 'BAR', 'Mapped attribute is BAR' );
217
        is( $patron->get_extended_attribute_value( $attr_type2->{code} ), 'BAR', 'Mapped attribute is BAR' );
216
        $auth->unmock('update_local');
218
        $auth->unmock('update_local');
217
        $auth->unmock('ldap_entry_2_hash');
219
        $auth->unmock('ldap_entry_2_hash');
218
220
219
        $update               = 0;
221
        $update               = 0;
220
        $desired_count_result = 0;    # user auth problem
222
        $desired_count_result = 0;    # user auth problem
221
        Koha::Patrons->find( $borrower->{borrowernumber} )->delete;
223
        $patron->delete;
222
        reload_ldap_module();
224
        reload_ldap_module();
223
        is(
225
        is(
224
            C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' ),
226
            C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' ),
(-)a/t/db_dependent/Koha/Patrons.t (+76 lines)
Lines 2018-2020 subtest 'anonymize' => sub { Link Here
2018
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
2018
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
2019
};
2019
};
2020
$schema->storage->txn_rollback;
2020
$schema->storage->txn_rollback;
2021
2022
subtest 'get_extended_attributes' => sub {
2023
    plan tests => 7;
2024
    my $schema = Koha::Database->new->schema;
2025
    $schema->storage->txn_begin;
2026
2027
    my $patron_1 = $builder->build_object({class=> 'Koha::Patrons'});
2028
    my $patron_2 = $builder->build_object({class=> 'Koha::Patrons'});
2029
2030
    set_logged_in_user($patron_1);
2031
2032
    my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1');
2033
    $attribute_type1->unique_id(1);
2034
    $attribute_type1->store();
2035
2036
    my $attribute_type2 = C4::Members::AttributeTypes->new('my code2', 'my description2');
2037
    $attribute_type2->opac_display(1);
2038
    $attribute_type2->staff_searchable(1);
2039
    $attribute_type2->store();
2040
2041
    my $new_library = $builder->build( { source => 'Branch' } );
2042
    my $attribute_type_limited = C4::Members::AttributeTypes->new('my code3', 'my description3');
2043
    $attribute_type_limited->branches([ $new_library->{branchcode} ]);
2044
    $attribute_type_limited->store;
2045
2046
    my $attributes_for_1 = [
2047
        {
2048
            value => 'my attribute1',
2049
            code => $attribute_type1->code(),
2050
        },
2051
        {
2052
            value => 'my attribute2',
2053
            code => $attribute_type2->code(),
2054
        },
2055
        {
2056
            value => 'my attribute limited',
2057
            code => $attribute_type_limited->code(),
2058
        }
2059
    ];
2060
2061
    my $attributes_for_2 = [
2062
        {
2063
            value => 'my attribute12',
2064
            code => $attribute_type1->code(),
2065
        },
2066
        {
2067
            value => 'my attribute limited 2',
2068
            code => $attribute_type_limited->code(),
2069
        }
2070
    ];
2071
2072
    my $extended_attributes = $patron_1->get_extended_attributes;
2073
    is( ref($extended_attributes), 'Koha::Patron::Attributes', 'Koha::Patron->get_extended_attributes must return a Koha::Patron::Attribute set' );
2074
    is( $extended_attributes->count, 0, 'There should not be attribute yet');
2075
2076
    C4::Members::Attributes::SetBorrowerAttributes($patron_1->borrowernumber, $attributes_for_1);
2077
    C4::Members::Attributes::SetBorrowerAttributes($patron_2->borrowernumber, $attributes_for_2);
2078
2079
    my $extended_attributes_for_1 = $patron_1->get_extended_attributes;
2080
    is( $extended_attributes_for_1->count, 3, 'There should be 3 attributes now for patron 1');
2081
2082
    my $extended_attributes_for_2 = $patron_2->get_extended_attributes;
2083
    is( $extended_attributes_for_2->count, 2, 'There should be 2 attributes now for patron 2');
2084
2085
    my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code });
2086
    is( $attribute_12->next->attribute, 'my attribute12', 'search by code should return the correct attribute' );
2087
2088
    $attribute_12 = $patron_2->get_extended_attribute_value( $attribute_type1->code );
2089
    is( $attribute_12, 'my attribute12', 'Koha::Patron->get_extended_attribute_value should return the correct attribute value' );
2090
2091
    # TODO - What about multiple?
2092
    my $non_existent = $patron_2->get_extended_attribute_value( 'not_exist' );
2093
    is( $non_existent, undef, 'Koha::Patron->get_extended_attribute_value must return undef if the attribute does not exist' );
2094
2095
    $schema->storage->txn_rollback;
2096
};
(-)a/t/db_dependent/Members/Attributes.t (-18 / +16 lines)
Lines 26-32 use Koha::Database; Link Here
26
use t::lib::TestBuilder;
26
use t::lib::TestBuilder;
27
use t::lib::Mocks;
27
use t::lib::Mocks;
28
28
29
use Test::More tests => 48;
29
use Test::More tests => 46;
30
30
31
use_ok('C4::Members::Attributes');
31
use_ok('C4::Members::Attributes');
32
32
Lines 71-79 $attribute_type_limited->branches([ $new_library->{branchcode} ]); Link Here
71
$attribute_type_limited->store;
71
$attribute_type_limited->store;
72
72
73
my $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
73
my $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
74
is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
74
ok(1);
75
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
75
#is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
76
is( @$borrower_attributes, 0, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
76
$patron = Koha::Patrons->find($borrowernumber);
77
$borrower_attributes = $patron->get_extended_attributes;
78
is( $borrower_attributes->count, 0, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
77
79
78
my $attributes = [
80
my $attributes = [
79
    {
81
    {
Lines 131-149 is( @$borrower_attributes, 3, 'SetBorrowerAttributes should not have removed the Link Here
131
#$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited');
133
#$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited');
132
#is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' );
134
#is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' );
133
135
134
my $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue();
136
$patron = Koha::Patrons->find($borrowernumber);
135
is( $attribute_value, undef, 'GetBorrowerAttributeValue without arguments returns undef' );
137
my $extended_attributes = $patron->get_extended_attributes;
136
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber);
138
my $attribute_value = $extended_attributes->search({ code => 'my invalid code' });
137
is( $attribute_value, undef, 'GetBorrowerAttributeValue without the attribute code returns undef' );
139
is( $attribute_value->count, 0, 'non existent attribute should return empty result set');
138
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue(undef, $attributes->[0]->{code});
140
$attribute_value = $patron->get_extended_attribute_value('my invalid code');
139
is( $attribute_value, undef, 'GetBorrowerAttributeValue with a undef borrower number returns undef' );
141
is( $attribute_value, undef, 'non existent attribute should undef');
140
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, 'my invalid code');
141
is( $attribute_value, undef, 'GetBorrowerAttributeValue with an invalid code retuns undef' );
142
142
143
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[0]->{code});
143
$attribute_value = $patron->get_extended_attribute_value($attributes->[0]->{code});
144
is( $attribute_value, $attributes->[0]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
144
is( $attribute_value, $attributes->[0]->{value}, 'get_extended_attribute_value returns the correct attribute value' );
145
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
145
$attribute_value = $patron->get_extended_attribute_value($attributes->[1]->{code});
146
is( $attribute_value, $attributes->[1]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
146
is( $attribute_value, $attributes->[1]->{value}, 'get_extended_attribute_value returns the correct attribute value' );
147
147
148
148
149
my $attribute = {
149
my $attribute = {
Lines 166-172 $check_uniqueness = C4::Members::Attributes::CheckUniqueness(undef, $attribute-> Link Here
166
is( $check_uniqueness, 0, 'CheckUniqueness without the argument code returns false' );
166
is( $check_uniqueness, 0, 'CheckUniqueness without the argument code returns false' );
167
$check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code');
167
$check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code');
168
is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns false' );
168
is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns false' );
169
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
170
$check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', $attribute->{attribute});
169
$check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', $attribute->{attribute});
171
is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns fale' );
170
is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns fale' );
172
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, 'new value');
171
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, 'new value');
173
- 

Return to bug 20443