Modified log reporting to allow multiple modules to be selected.
[koha.git] / C4 / Members / Attributes.pm
blobe01232024003fb2abfd2a1a90d79ba8cbcba1181
1 package C4::Members::Attributes;
3 # Copyright (C) 2008 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use C4::Context;
22 use C4::Members::AttributeTypes;
24 use vars qw($VERSION);
26 BEGIN {
27 # set the version for version checking
28 $VERSION = 3.00;
31 =head1 NAME
33 C4::Members::Attribute - manage extend patron attributes
35 =head1 SYNOPSIS
37 =over 4
39 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
41 =back
43 =head1 FUNCTIONS
45 =head2 GetBorrowerAttributes
47 =over 4
49 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber[, $opac_only]);
51 =back
53 Retrieve an arrayref of extended attributes associated with the
54 patron specified by C<$borrowernumber>. Each entry in the arrayref
55 is a hashref containing the following keys:
57 code (attribute type code)
58 description (attribute type description)
59 value (attribute value)
60 value_description (attribute value description (if associated with an authorised value))
61 password (password, if any, associated with attribute
63 If the C<$opac_only> parameter is present and has a true value, only the attributes
64 marked for OPAC display are returned.
66 =cut
68 sub GetBorrowerAttributes {
69 my $borrowernumber = shift;
70 my $opac_only = @_ ? shift : 0;
72 my $dbh = C4::Context->dbh();
73 my $query = "SELECT code, description, attribute, lib, password
74 FROM borrower_attributes
75 JOIN borrower_attribute_types USING (code)
76 LEFT JOIN authorised_values ON (category = authorised_value_category AND attribute = authorised_value)
77 WHERE borrowernumber = ?";
78 $query .= "\nAND opac_display = 1" if $opac_only;
79 $query .= "\nORDER BY code, attribute";
80 my $sth = $dbh->prepare_cached($query);
81 $sth->execute($borrowernumber);
82 my @results = ();
83 while (my $row = $sth->fetchrow_hashref()) {
84 push @results, {
85 code => $row->{'code'},
86 description => $row->{'description'},
87 value => $row->{'attribute'},
88 value_description => $row->{'lib'},
89 password => $row->{'password'},
92 return \@results;
95 =head2 CheckUniqueness
97 =over 4
99 my $ok = CheckUniqueness($code, $value[, $borrowernumber]);
101 =back
103 Given an attribute type and value, verify if would violate
104 a unique_id restriction if added to the patron. The
105 optional C<$borrowernumber> is the patron that the attribute
106 value would be added to, if known.
108 Returns false if the C<$code> is not valid or the
109 value would violate the uniqueness constraint.
111 =cut
113 sub CheckUniqueness {
114 my $code = shift;
115 my $value = shift;
116 my $borrowernumber = @_ ? shift : undef;
118 my $attr_type = C4::Members::AttributeTypes->fetch($code);
120 return 0 unless defined $attr_type;
121 return 1 unless $attr_type->unique_id();
123 my $dbh = C4::Context->dbh;
124 my $sth;
125 if (defined($borrowernumber)) {
126 $sth = $dbh->prepare("SELECT COUNT(*)
127 FROM borrower_attributes
128 WHERE code = ?
129 AND attribute = ?
130 AND borrowernumber <> ?");
131 $sth->execute($code, $value, $borrowernumber);
132 } else {
133 $sth = $dbh->prepare("SELECT COUNT(*)
134 FROM borrower_attributes
135 WHERE code = ?
136 AND attribute = ?");
137 $sth->execute($code, $value);
139 my ($count) = $sth->fetchrow_array;
140 $sth->finish();
141 return ($count == 0);
144 =head2 SetBorrowerAttributes
146 =over 4
148 SetBorrowerAttributes($borrowernumber, [ { code => 'CODE', value => 'value', password => 'password' }, ... ] );
150 =back
152 Set patron attributes for the patron identified by C<$borrowernumber>,
153 replacing any that existed previously.
155 =cut
157 sub SetBorrowerAttributes {
158 my $borrowernumber = shift;
159 my $attr_list = shift;
161 my $dbh = C4::Context->dbh;
162 my $delsth = $dbh->prepare("DELETE FROM borrower_attributes WHERE borrowernumber = ?");
163 $delsth->execute($borrowernumber);
165 my $sth = $dbh->prepare("INSERT INTO borrower_attributes (borrowernumber, code, attribute, password)
166 VALUES (?, ?, ?, ?)");
167 foreach my $attr (@$attr_list) {
168 $attr->{password} = undef unless exists $attr->{password};
169 $sth->execute($borrowernumber, $attr->{code}, $attr->{value}, $attr->{password});
173 =head1 AUTHOR
175 Koha Development Team <info@koha.org>
177 Galen Charlton <galen.charlton@liblime.com>
179 =cut