Skip to content

Commit

Permalink
Bug 16960 - Patron::Modifications should be fixed
Browse files Browse the repository at this point in the history
The changes from opac-memberentry do not reach the table, since the
Patron::Modifications object does not work well.

Test Plan:
1) Apply this patch
2) Create some patron modification requests
3) Ensure you can approve and deny modifications
4) Ensure patron self registration works

Signed-off-by: Bob Birchall <[email protected]>

Signed-off-by: Katrin Fischer  <[email protected]>

Signed-off-by: Kyle M Hall <[email protected]>
  • Loading branch information
kylemhall committed Aug 22, 2016
1 parent 29b4ee5 commit 1016139
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 322 deletions.
36 changes: 33 additions & 3 deletions Koha/Patron/Modification.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,44 @@ use base qw(Koha::Object);

=head1 NAME
Koha::Item - Koha Item object class
=head1 API
Koha::Patron::Modification - Class represents a requrest to modify or create a patron
=head2 Class Methods
=cut

=head2 approve
$m->approve();
Commits the pending modifications to the borrower record and removes
them from the modifications table.
=cut

sub approve {
my ($self) = @_;

my $data = $self->unblessed();

delete $data->{timestamp};
delete $data->{verification_token};

foreach my $key ( keys %$data ) {
delete $data->{$key} unless ( defined( $data->{$key} ) );
}

my $patron = Koha::Patrons->find( $self->borrowernumber );

return unless $patron;

$patron->set($data);

if ( $patron->store() ) {
return $self->delete();
}
}

=head3 type
=cut
Expand Down
218 changes: 10 additions & 208 deletions Koha/Patron/Modifications.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,80 +25,20 @@ Koha::Patron::Modifications
use Modern::Perl;

use C4::Context;
use C4::Debug;

use base qw(Koha::Objects);

=head2 AddModifications
Koha::Patron::Modifications->AddModifications( $data );
Adds or updates modifications for a patron
Requires either the key borrowernumber, or verification_token
to be part of the passed in hash.
=cut

sub AddModifications {
my ( $self, $data ) = @_;

delete $data->{borrowernumber};
if( $self->{borrowernumber} ) {
return if( not keys %$data );
$data->{borrowernumber} = $self->{borrowernumber};
$data->{verification_token} = '';
}
elsif( $self->{verification_token} ) {
$data->{verification_token} = $self->{verification_token};
$data->{borrowernumber} = 0;
}
else {
return;
}
use Koha::Patron::Modification;

my $rs = Koha::Database->new()->schema->resultset('BorrowerModification');
return $rs->update_or_create($data, { key => 'primary' } );
}

=head2 Verify
$verified = Koha::Patron::Modifications->Verify( $verification_token );
Returns true if the passed in token is valid.
=cut

sub Verify {
my ( $self, $verification_token ) = @_;

$verification_token =
($verification_token)
? $verification_token
: $self->{'verification_token'};

my $dbh = C4::Context->dbh;
my $query = "
SELECT COUNT(*) AS count
FROM borrower_modifications
WHERE verification_token = ?
";
my $sth = $dbh->prepare($query);
$sth->execute($verification_token);
my $result = $sth->fetchrow_hashref();

return $result->{'count'};
}
use base qw(Koha::Objects);

=head2 GetPendingModificationsCount
=head2 pending_count
$count = Koha::Patron::Modifications->GetPendingModificationsCount();
$count = Koha::Patron::Modifications->pending_count();
Returns the number of pending modifications for existing patron.
Returns the number of pending modifications for existing patrons.
=cut

sub GetPendingModificationsCount {
sub pending_count {
my ( $self, $branchcode ) = @_;

my $dbh = C4::Context->dbh;
Expand All @@ -119,18 +59,18 @@ sub GetPendingModificationsCount {
$sth->execute(@params);
my $result = $sth->fetchrow_hashref();

return $result->{'count'};
return $result->{count};
}

=head2 GetPendingModifications
=head2 pending
$arrayref = Koha::Patron::Modifications->GetPendingModifications();
$arrayref = Koha::Patron::Modifications->pending();
Returns an arrayref of hashrefs for all pending modifications for existing patrons.
=cut

sub GetPendingModifications {
sub pending {
my ( $self, $branchcode ) = @_;

my $dbh = C4::Context->dbh;
Expand Down Expand Up @@ -162,144 +102,6 @@ sub GetPendingModifications {
return \@m;
}

=head2 ApproveModifications
Koha::Patron::Modifications->ApproveModifications( $borrowernumber );
Commits the pending modifications to the borrower record and removes
them from the modifications table.
=cut

sub ApproveModifications {
my ( $self, $borrowernumber ) = @_;

$borrowernumber =
($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};

return unless $borrowernumber;

my $data = $self->GetModifications( { borrowernumber => $borrowernumber } );
delete $data->{timestamp};
delete $data->{verification_token};

my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({
borrowernumber => $data->{borrowernumber},
});
if( $rs->update($data) ) {
$self->DelModifications( { borrowernumber => $borrowernumber } );
}
}

=head2 DenyModifications
Koha::Patron::Modifications->DenyModifications( $borrowernumber );
Removes the modifications from the table for the given patron,
without committing the changes to the patron record.
=cut

sub DenyModifications {
my ( $self, $borrowernumber ) = @_;

$borrowernumber =
($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};

return unless $borrowernumber;

return $self->DelModifications( { borrowernumber => $borrowernumber } );
}

=head2 DelModifications
Koha::Patron::Modifications->DelModifications({
[ borrowernumber => $borrowernumber ],
[ verification_token => $verification_token ]
});
Deletes the modifications for the given borrowernumber or verification token.
=cut

sub DelModifications {
my ( $self, $params ) = @_;

my ( $field, $value );

if ( $params->{'borrowernumber'} ) {
$field = 'borrowernumber';
$value = $params->{'borrowernumber'};
}
elsif ( $params->{'verification_token'} ) {
$field = 'verification_token';
$value = $params->{'verification_token'};
}

return unless $value;

my $dbh = C4::Context->dbh;

$field = $dbh->quote_identifier($field);

my $query = "
DELETE
FROM borrower_modifications
WHERE $field = ?
";

my $sth = $dbh->prepare($query);
return $sth->execute($value);
}

=head2 GetModifications
$hashref = Koha::Patron::Modifications->GetModifications({
[ borrowernumber => $borrowernumber ],
[ verification_token => $verification_token ]
});
Gets the modifications for the given borrowernumber or verification token.
=cut

sub GetModifications {
my ( $self, $params ) = @_;

my ( $field, $value );

if ( defined( $params->{'borrowernumber'} ) ) {
$field = 'borrowernumber';
$value = $params->{'borrowernumber'};
}
elsif ( defined( $params->{'verification_token'} ) ) {
$field = 'verification_token';
$value = $params->{'verification_token'};
}

return unless $value;

my $dbh = C4::Context->dbh;

$field = $dbh->quote_identifier($field);

my $query = "
SELECT *
FROM borrower_modifications
WHERE $field = ?
";

my $sth = $dbh->prepare($query);
$sth->execute($value);
my $data = $sth->fetchrow_hashref();

foreach my $key ( keys %$data ) {
delete $data->{$key} unless ( defined( $data->{$key} ) );
}

return $data;
}

sub _type {
return 'BorrowerModification';
}
Expand Down
3 changes: 1 addition & 2 deletions mainpage.pl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
my $pendingcomments = numberofreviews(0);
my $pendingtags = get_count_by_tag_status(0);
my $pendingsuggestions = CountSuggestion("ASKED");
my $pending_borrower_modifications =
Koha::Patron::Modifications->GetPendingModificationsCount( $branch );
my $pending_borrower_modifications = Koha::Patron::Modifications->pending_count( $branch );
my $pending_discharge_requests = Koha::Patron::Discharge::count({ pending => 1 });

$template->param(
Expand Down
2 changes: 1 addition & 1 deletion members/members-home.pl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@


my $pending_borrower_modifications =
Koha::Patron::Modifications->GetPendingModificationsCount( $branch );
Koha::Patron::Modifications->pending_count( $branch );

$template->param(
no_add => $no_add,
Expand Down
10 changes: 5 additions & 5 deletions members/members-update-do.pl
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
my $action = $query->param($param);

if ( $action eq 'approve' ) {
Koha::Patron::Modifications->ApproveModifications( $borrowernumber );
my $m = Koha::Patron::Modifications->find( { borrowernumber => $borrowernumber } );
$m->approve() if $m;
}
elsif ( $action eq 'deny' ) {
Koha::Patron::Modifications->DenyModifications( $borrowernumber );
}
elsif ( $action eq 'ignore' ) {

my $m = Koha::Patron::Modifications->find( { borrowernumber => $borrowernumber } );
$m->delete() if $m;
}
# elsif ( $action eq 'ignore' ) { }
}
}

Expand Down
2 changes: 1 addition & 1 deletion members/members-update.pl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
: undef;

my $pending_modifications =
Koha::Patron::Modifications->GetPendingModifications($branch);
Koha::Patron::Modifications->pending($branch);

my $borrowers;
foreach my $pm (@$pending_modifications) {
Expand Down
17 changes: 8 additions & 9 deletions opac/opac-memberentry.pl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use C4::Members;
use C4::Form::MessagingPreferences;
use Koha::Patrons;
use Koha::Patron::Modification;
use Koha::Patron::Modifications;
use C4::Branch qw(GetBranchesLoop);
use C4::Scrubber;
Expand Down Expand Up @@ -127,11 +128,11 @@
$template->param( 'email' => $borrower{'email'} );

my $verification_token = md5_hex( \%borrower );
$borrower{'password'} = random_string("..........");

Koha::Patron::Modifications->new(
verification_token => $verification_token )
->AddModifications(\%borrower);
$borrower{password} = random_string("..........");
$borrower{verification_token} = $verification_token;

Koha::Patron::Modification->new( \%borrower )->store();

#Send verification email
my $letter = C4::Letters::GetPreparedLetter(
Expand Down Expand Up @@ -224,12 +225,10 @@
}
);

my $m =
Koha::Patron::Modifications->new(
borrowernumber => $borrowernumber );
$borrower_changes{borrowernumber} = $borrowernumber;

my $m = Koha::Patron::Modification->new( \%borrower_changes )->store();

$m->DelModifications;
$m->AddModifications(\%borrower_changes);
$template->param(
borrower => GetMember( borrowernumber => $borrowernumber ),
);
Expand Down
Loading

0 comments on commit 1016139

Please sign in to comment.