From 1d56d8eefb702971bf5e8183edc056fdefc2505a Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 16 Jul 2015 07:58:26 -0400 Subject: [PATCH] Bug 8483 - Borrower reading history should include deleted items When showing a borrower's reading history, it'd be good to be able to include the deleted items also, as we have that information available. Test Plan: 1) Apply this patch 2) prove t/db_dependent/Members/GetAllIssues.t 3) Check out and return an item for a patron 4) Delete that item 5) Verify the record still shows in the patron's reading history 6) Delete the record entirely 7) Verify the record still shows in the patron's reading history, but that the title is now just text instead of a hyperlink. Signed-off-by: Heather Braum --- C4/Members.pm | 55 ++++++++++++-------- Koha/Borrower.pm | 55 ++++++++++++++++++++ Koha/Object.pm | 12 +++++ Koha/Objects.pm | 14 +++++ installer/data/mysql/atomicupdate/bug_8483.sql | 1 + installer/data/mysql/kohastructure.sql | 4 +- .../prog/en/modules/members/readingrec.tt | 8 ++- 7 files changed, 124 insertions(+), 25 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_8483.sql diff --git a/C4/Members.pm b/C4/Members.pm index 0b8e7d7..0770451 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -42,6 +42,8 @@ use Text::Unaccent qw( unac_string ); use Koha::AuthUtils qw(hash_password); use Koha::Database; use Module::Load; +use Koha::Borrowers; + if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) { load Koha::NorwegianPatronDB, qw( NLUpdateHashedPIN NLEncryptPIN NLSync ); } @@ -1122,31 +1124,42 @@ sub GetAllIssues { my ( $borrowernumber, $order, $limit ) = @_; return unless $borrowernumber; + $order = 'date_due desc' unless $order; + my ( $column, $sort ) = split( / /, $order ); + $column ||= 'date_due'; + $sort ||= 'desc'; - my $dbh = C4::Context->dbh; - my $query = -'SELECT *, issues.timestamp as issuestimestamp, issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp - FROM issues - LEFT JOIN items on items.itemnumber=issues.itemnumber - LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber - LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber - WHERE borrowernumber=? - UNION ALL - SELECT *, old_issues.timestamp as issuestimestamp, old_issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp - FROM old_issues - LEFT JOIN items on items.itemnumber=old_issues.itemnumber - LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber - LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber - WHERE borrowernumber=? AND old_issues.itemnumber IS NOT NULL - order by ' . $order; - if ($limit) { - $query .= " limit $limit"; + my $borrower = Koha::Borrowers->find( $borrowernumber ); + my $checkouts = $borrower->all_checkouts( { order => { column => $column, sort => $sort } , limit => $limit } ); + + my @results; + foreach my $checkout (@$checkouts) { + my $item = $checkout->item( { deleted => 1 } ); + next unless $item; + + my $biblio = $item->biblio( { deleted => 1 } ); + next unless $biblio; + + my $biblioitem = $item->biblioitem( { deleted => 1 } ); + next unless $biblioitem; + + my $checkout_hashref = $checkout->unblessed(); + my $item_hashref = $item->unblessed(); + my $biblio_hashref = $biblio->unblessed(); + my $biblioitem_hashref = $biblioitem->unblessed(); + + my $hashref = { %$checkout_hashref, %$item_hashref, %$biblio_hashref, %$biblioitem_hashref }; + $hashref->{issuestimestamp} = $checkout->timestamp(); + $hashref->{renewals} = $checkout->renewals(); + $hashref->{totalrenewals} = $item->renewals(); + $hashref->{itemstimestamp} = $item->timestamp(); + $hashref->{record_is_deleted} = ref($biblio) eq 'Koha::Deleted::Biblio'; + + push( @results, $hashref ); } - my $sth = $dbh->prepare($query); - $sth->execute( $borrowernumber, $borrowernumber ); - return $sth->fetchall_arrayref( {} ); + return \@results; } diff --git a/Koha/Borrower.pm b/Koha/Borrower.pm index 93426bf..a210dc1 100644 --- a/Koha/Borrower.pm +++ b/Koha/Borrower.pm @@ -23,6 +23,9 @@ use Carp; use Koha::Database; +use Koha::Checkouts; +use Koha::Old::Checkouts; + use base qw(Koha::Object); =head1 NAME @@ -35,6 +38,58 @@ Koha::Borrower - Koha Borrower Object class =cut +=head3 all_checkouts + +=cut + +sub all_checkouts { + my ( $self, $params ) = @_; + + my $limit = $params->{limit}; + + my $order_by_column = $params->{order} ? $params->{order}->{column} : 'date_due'; + my $order_by_sort = $params->{order} ? $params->{order}->{sort} : 'desc'; + + my @results; + my $results_found = 0; + + my $issues = Koha::Checkouts->search( + { + borrowernumber => $self->borrowernumber(), + }, + { + order_by => { "-$order_by_sort" => $order_by_column } + } + ); + + while ( my $i = $issues->next() ) { + push( @results, $i ); + $results_found++; + last if ( $limit && $results_found >= $limit ); + } + + if ( !$limit || $results_found < $limit ) { + + $issues = Koha::Old::Checkouts->search( + { + borrowernumber => $self->borrowernumber(), + }, + { + order_by => { "-$order_by_sort" => $order_by_column } + } + ); + + while ( my $i = $issues->next() ) { + push( @results, $i ); + $results_found++; + last if ( $limit && $results_found >= $limit ); + } + + } + + return wantarray ? @results : \@results; +} + =head3 type =cut diff --git a/Koha/Object.pm b/Koha/Object.pm index a77eb5c..504872c 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -266,6 +266,18 @@ sub AUTOLOAD { return; } +=head3 $object->unblessed(); + +Returns an unblessed representation of object. + +=cut + +sub unblessed { + my ($self) = @_; + + return { $self->_result->get_columns }; +} + =head3 type This method must be defined in the child class. The value is the name of the DBIC resultset. diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 8ef6390..f6add21 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -195,6 +195,20 @@ sub _wrap { return @objects; } +=head3 Koha::Objects->unblessed + +Returns an unblessed representation of objects. + +=cut + +sub unblessed { + my ($self) = @_; + + return [ map { $_->unblessed } $self->as_list ]; +} + + + =head3 Koha::Objects->_resultset Returns the internal resultset or creates it if undefined diff --git a/installer/data/mysql/atomicupdate/bug_8483.sql b/installer/data/mysql/atomicupdate/bug_8483.sql new file mode 100644 index 0000000..d706468 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_8483.sql @@ -0,0 +1 @@ +ALTER TABLE `old_issues` DROP FOREIGN KEY `old_issues_ibfk_2` ; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index dab7538..deb78b4 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1164,7 +1164,7 @@ CREATE TABLE `issues` ( -- information related to check outs or issues KEY `branchcode_idx` (`branchcode`), KEY `bordate` (`borrowernumber`,`timestamp`), CONSTRAINT `issues_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE RESTRICT ON UPDATE CASCADE, - CONSTRAINT `issues_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE RESTRICT ON UPDATE CASCADE + CONSTRAINT `issues_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE RESTRICT ON UPDATE CASCADE, ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- @@ -1640,8 +1640,6 @@ CREATE TABLE `old_issues` ( -- lists items that were checked out and have been r KEY `branchcode_idx` (`branchcode`), KEY `old_bordate` (`borrowernumber`,`timestamp`), CONSTRAINT `old_issues_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) - ON DELETE SET NULL ON UPDATE SET NULL, - CONSTRAINT `old_issues_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE SET NULL ON UPDATE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/readingrec.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/readingrec.tt index 6fb109e..b6929c6 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/readingrec.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/readingrec.tt @@ -94,7 +94,13 @@ [% issue.issuestimestamp | $KohaDates with_hours => 1 %] - [% issue.title |html %] + + [% IF issue.record_is_deleted %] + [% issue.title |html %] + [% ELSE %] + [% issue.title |html %] + [% END %] + [% issue.author %] -- 1.7.10.4