Bug 29234: Further clean Z3950 Tests
[koha-ffzg.git] / C4 / CourseReserves.pm
index 4737536..fac8b84 100644 (file)
@@ -2,56 +2,59 @@ package C4::CourseReserves;
 
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
-require Exporter;
+use List::MoreUtils qw( any );
 
 use C4::Context;
-use C4::Items qw(GetItem ModItem);
-use C4::Biblio qw(GetBiblioFromItemNumber);
-use C4::Circulation qw(GetOpenIssue);
 
-use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG @FIELDS);
+use Koha::Courses;
+use Koha::Course::Instructors;
+use Koha::Course::Items;
+use Koha::Course::Reserves;
+use Koha::Checkouts;
 
+use vars qw(@FIELDS);
+our (@ISA, @EXPORT_OK);
 BEGIN {
-    @ISA    = qw(Exporter);
-    @EXPORT = qw(
-      &GetCourse
-      &ModCourse
-      &GetCourses
-      &DelCourse
+    require Exporter;
+    @ISA       = qw(Exporter);
+    @EXPORT_OK = qw(
+      GetCourse
+      ModCourse
+      GetCourses
+      DelCourse
 
-      &GetCourseInstructors
-      &ModCourseInstructors
+      GetCourseInstructors
+      ModCourseInstructors
 
-      &GetCourseItem
-      &ModCourseItem
+      GetCourseItem
+      ModCourseItem
 
-      &GetCourseReserve
-      &ModCourseReserve
-      &GetCourseReserves
-      &DelCourseReserve
+      GetCourseReserve
+      ModCourseReserve
+      GetCourseReserves
+      DelCourseReserve
 
-      &SearchCourses
+      SearchCourses
 
-      &GetItemReservesInfo
+      GetItemCourseReservesInfo
     );
 
-    $DEBUG = 0;
-    @FIELDS = ( 'itype', 'ccode', 'holdingbranch', 'location' );
+    @FIELDS = ( 'itype', 'ccode', 'homebranch', 'holdingbranch', 'location' );
 }
 
 =head1 NAME
@@ -76,21 +79,18 @@ This module deals with course reserves.
 
 sub GetCourse {
     my ($course_id) = @_;
-    warn whoami() . "( $course_id )" if $DEBUG;
 
-    my $query = "SELECT * FROM courses WHERE course_id = ?";
-    my $dbh   = C4::Context->dbh;
-    my $sth   = $dbh->prepare($query);
-    $sth->execute($course_id);
+    my $course = Koha::Courses->find( $course_id );
+    return unless $course;
+    $course = $course->unblessed;
 
-    my $course = $sth->fetchrow_hashref();
-
-    $query = "
+    my $dbh = C4::Context->dbh;
+    my $query = "
         SELECT b.* FROM course_instructors ci
         LEFT JOIN borrowers b ON ( ci.borrowernumber = b.borrowernumber )
         WHERE course_id =  ?
     ";
-    $sth = $dbh->prepare($query);
+    my $sth = $dbh->prepare($query);
     $sth->execute($course_id);
     $course->{'instructors'} = $sth->fetchall_arrayref( {} );
 
@@ -105,7 +105,6 @@ sub GetCourse {
 
 sub ModCourse {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $dbh = C4::Context->dbh;
 
@@ -155,15 +154,14 @@ sub ModCourse {
 
 sub GetCourses {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my @query_keys;
     my @query_values;
 
     my $query = "
-        SELECT courses.*
-        FROM courses
-        LEFT JOIN course_reserves ON course_reserves.course_id = courses.course_id
+        SELECT c.course_id, c.department, c.course_number, c.section, c.course_name, c.term, c.staff_note, c.public_note, c.students_count, c.enabled, c.timestamp
+        FROM courses c
+        LEFT JOIN course_reserves ON course_reserves.course_id = c.course_id
         LEFT JOIN course_items ON course_items.ci_id = course_reserves.ci_id
     ";
 
@@ -179,7 +177,7 @@ sub GetCourses {
         $query .= join( ' AND ', @query_keys );
     }
 
-    $query .= " GROUP BY courses.course_id ";
+    $query .= " GROUP BY c.course_id, c.department, c.course_number, c.section, c.course_name, c.term, c.staff_note, c.public_note, c.students_count, c.enabled, c.timestamp ";
 
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare($query);
@@ -238,10 +236,9 @@ sub DelCourse {
 
 sub EnableOrDisableCourseItems {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $course_id = $params{'course_id'};
-    my $enabled   = $params{'enabled'} || 0;
+    my $enabled = $params{'enabled'} || 0;
 
     my $lookfor = ( $enabled eq 'yes' ) ? 'no' : 'yes';
 
@@ -252,16 +249,13 @@ sub EnableOrDisableCourseItems {
 
     if ( $enabled eq 'yes' ) {
         foreach my $course_reserve (@$course_reserves) {
-            if (
-                CountCourseReservesForItem(
+            if (CountCourseReservesForItem(
                     ci_id   => $course_reserve->{'ci_id'},
                     enabled => 'yes'
                 )
-              )
-            {
+              ) {
                 EnableOrDisableCourseItem(
                     ci_id   => $course_reserve->{'ci_id'},
-                    enabled => 'yes',
                 );
             }
         }
@@ -273,11 +267,9 @@ sub EnableOrDisableCourseItems {
                     ci_id   => $course_reserve->{'ci_id'},
                     enabled => 'yes'
                 )
-              )
-            {
+              ) {
                 EnableOrDisableCourseItem(
                     ci_id   => $course_reserve->{'ci_id'},
-                    enabled => 'no',
                 );
             }
         }
@@ -286,30 +278,29 @@ sub EnableOrDisableCourseItems {
 
 =head2 EnableOrDisableCourseItem
 
-    EnableOrDisableCourseItem( ci_id => $ci_id, enabled => $enabled );
-
-    enabled => 'yes' to enable course items
-    enabled => 'no' to disable course items
+    EnableOrDisableCourseItem( ci_id => $ci_id );
 
 =cut
 
 sub EnableOrDisableCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id   = $params{'ci_id'};
-    my $enabled = $params{'enabled'};
 
-    return unless ( $ci_id && $enabled );
-    return unless ( $enabled eq 'yes' || $enabled eq 'no' );
+    return unless ( $ci_id );
 
     my $course_item = GetCourseItem( ci_id => $ci_id );
 
+    my $info = $course_item->{itemnumber} ? GetItemCourseReservesInfo( itemnumber => $course_item->{itemnumber} ) : GetItemCourseReservesInfo( biblionumber => $course_item->{biblionumber} );
+
+    my $enabled = any { $_->{course}->{enabled} eq 'yes' } @$info;
+    $enabled = $enabled ? 'yes' : 'no';
+
     ## We don't want to 'enable' an already enabled item,
     ## or disable and already disabled item,
     ## as that would cause the fields to swap
     if ( $course_item->{'enabled'} ne $enabled ) {
-        _SwapAllFields($ci_id);
+        _SwapAllFields($ci_id, $enabled );
 
         my $query = "
             UPDATE course_items
@@ -331,8 +322,6 @@ sub EnableOrDisableCourseItem {
 
 sub GetCourseInstructors {
     my ($course_id) = @_;
-    warn "C4::CourseReserves::GetCourseInstructors( $course_id )"
-      if $DEBUG;
 
     my $query = "
         SELECT * FROM borrowers
@@ -361,7 +350,6 @@ sub GetCourseInstructors {
 
 sub ModCourseInstructors {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $course_id       = $params{'course_id'};
     my $mode            = $params{'mode'};
@@ -376,7 +364,7 @@ sub ModCourseInstructors {
     return unless ( $cardnumbers || $borrowernumbers );
     return if ( $cardnumbers && $borrowernumbers );
 
-    my (@cardnumbers, @borrowernumbers);
+    my ( @cardnumbers, @borrowernumbers );
     @cardnumbers = @$cardnumbers if ( ref($cardnumbers) eq 'ARRAY' );
     @borrowernumbers = @$borrowernumbers
       if ( ref($borrowernumbers) eq 'ARRAY' );
@@ -387,8 +375,7 @@ sub ModCourseInstructors {
 
     my $dbh = C4::Context->dbh;
 
-    $dbh->do( "DELETE FROM course_instructors WHERE course_id = ?",
-        undef, $course_id )
+    $dbh->do( "DELETE FROM course_instructors WHERE course_id = ?", undef, $course_id )
       if ( $mode eq 'replace' );
 
     my $query;
@@ -400,8 +387,7 @@ sub ModCourseInstructors {
             FROM borrowers
             WHERE $field IN ( $placeholders )
         ";
-    }
-    else {
+    } else {
         $query = "
             DELETE FROM course_instructors
             WHERE course_id = ?
@@ -418,21 +404,34 @@ sub ModCourseInstructors {
 
 =head2 GetCourseItem {
 
-  $course_item = GetCourseItem( itemnumber => $itemnumber [, ci_id => $ci_id );
+  Given one of biblionumber, itenumber, or ci_id, returns hashref of the course_items values
+
+  $course_item = GetCourseItem( itemnumber => $itemnumber [, ci_id => $ci_id ] );
+  $course_item = GetCourseItem( biblionumber => $biblionumber [, ci_id => $ci_id ]);
+  $course_item = GetCourseItem( ci_id => $ci_id );
 
 =cut
 
 sub GetCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id      = $params{'ci_id'};
     my $itemnumber = $params{'itemnumber'};
-
-    return unless ( $itemnumber || $ci_id );
-
-    my $field = ($itemnumber) ? 'itemnumber' : 'ci_id';
-    my $value = ($itemnumber) ? $itemnumber  : $ci_id;
+    my $biblionumber = $params{'biblionumber'};
+
+    return unless ( $itemnumber xor $biblionumber xor $ci_id );
+
+    my ( $field, $value );
+    if ( $itemnumber ) {
+        $field = 'itemnumber';
+        $value = $itemnumber;
+    } elsif ( $biblionumber ) {
+        $field = 'biblionumber';
+        $value = $biblionumber;
+    } else {
+        $field = 'ci_id';
+        $value = $ci_id;
+    }
 
     my $query = "SELECT * FROM course_items WHERE $field = ?";
     my $dbh   = C4::Context->dbh;
@@ -457,23 +456,24 @@ sub GetCourseItem {
 
   ModCourseItem( %params );
 
-  Creates or modifies an existing course item.
+  Creates or modifies an existing course item. Must be passed either an itemnumber or biblionumber parameter
 
 =cut
 
 sub ModCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
-    my $itemnumber    = $params{'itemnumber'};
-    my $itype         = $params{'itype'};
-    my $ccode         = $params{'ccode'};
-    my $holdingbranch = $params{'holdingbranch'};
-    my $location      = $params{'location'};
+    my $itemnumber = $params{'itemnumber'};
+    my $biblionumber = $params{'biblionumber'};
+
+    return unless ($itemnumber xor $biblionumber);
 
-    return unless ($itemnumber);
+    my $course_item = $itemnumber ? GetCourseItem( itemnumber => $itemnumber ) : GetCourseItem( biblionumber => $biblionumber );
 
-    my $course_item = GetCourseItem( itemnumber => $itemnumber );
+    if ( $itemnumber and !$biblionumber ) {
+        $biblionumber = Koha::Items->find( $itemnumber )->biblionumber;
+        $params{biblionumber} = $biblionumber;
+    }
 
     my $ci_id;
 
@@ -485,8 +485,7 @@ sub ModCourseItem {
             course_item => $course_item,
             %params
         );
-    }
-    else {
+    } else {
         $ci_id = _AddCourseItem(%params);
     }
 
@@ -502,27 +501,23 @@ sub ModCourseItem {
 
 sub _AddCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
-    my ( @fields, @values );
+    $params{homebranch} ||= undef; # Can't be empty string, FK constraint
+    $params{holdingbranch} ||= undef; # Can't be empty string, FK constraint
 
-    push( @fields, 'itemnumber = ?' );
-    push( @values, $params{'itemnumber'} );
+    my %data = map { $_ => $params{$_} } @FIELDS;
+    my %enabled = map { $_ . "_enabled" => $params{ $_ . "_enabled" } } @FIELDS;
 
-    foreach (@FIELDS) {
-        if ( $params{$_} ) {
-            push( @fields, "$_ = ?" );
-            push( @values, $params{$_} );
+    my $ci = Koha::Course::Item->new(
+        {
+            itemnumber => $params{itemnumber},
+            biblionumber => $params{biblionumber},
+            %data,
+            %enabled,
         }
-    }
-
-    my $query = "INSERT INTO course_items SET " . join( ',', @fields );
-    my $dbh = C4::Context->dbh;
-    $dbh->do( $query, undef, @values );
-
-    my $ci_id = $dbh->last_insert_id( undef, undef, 'course_items', 'ci_id' );
+    )->store();
 
-    return $ci_id;
+    return $ci->id;
 }
 
 =head2 _UpdateCourseItem
@@ -533,98 +528,56 @@ sub _AddCourseItem {
 
 sub _UpdateCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id         = $params{'ci_id'};
     my $course_item   = $params{'course_item'};
-    my $itype         = $params{'itype'};
-    my $ccode         = $params{'ccode'};
-    my $holdingbranch = $params{'holdingbranch'};
-    my $location      = $params{'location'};
-
-    return unless ( $ci_id || $course_item );
-
-    $course_item = GetCourseItem( ci_id => $ci_id )
-      unless ($course_item);
-    $ci_id = $course_item->{'ci_id'} unless ($ci_id);
-
-    ## Revert fields that had an 'original' value, but now don't
-    ## Update the item fields to the stored values from course_items
-    ## and then set those fields in course_items to NULL
-    my @fields_to_revert;
-    foreach (@FIELDS) {
-        if ( !$params{$_} && $course_item->{$_} ) {
-            push( @fields_to_revert, $_ );
-        }
-    }
-    _RevertFields(
-        ci_id       => $ci_id,
-        fields      => \@fields_to_revert,
-        course_item => $course_item
-    ) if (@fields_to_revert);
-
-    ## Update fields that still have an original value, but it has changed
-    ## This necessitates only changing the current item values, as we still
-    ## have the original values stored in course_items
-    my %mod_params;
-    foreach (@FIELDS) {
-        if (   $params{$_}
-            && $course_item->{$_}
-            && $params{$_} ne $course_item->{$_} )
-        {
-            $mod_params{$_} = $params{$_};
-        }
-    }
-    ModItem( \%mod_params, undef, $course_item->{'itemnumber'} );
-
-    ## Update fields that didn't have an original value, but now do
-    ## We must save the original value in course_items, and also
-    ## update the item fields to the new value
-    my $item = GetItem( $course_item->{'itemnumber'} );
-    my %mod_params_new;
-    my %mod_params_old;
-    foreach (@FIELDS) {
-        if ( $params{$_} && !$course_item->{$_} ) {
-            $mod_params_new{$_} = $params{$_};
-            $mod_params_old{$_} = $item->{$_};
-        }
-    }
-    _ModStoredFields( 'ci_id' => $params{'ci_id'}, %mod_params_old );
-    ModItem( \%mod_params_new, undef, $course_item->{'itemnumber'} );
-
-}
-
-=head2 _ModStoredFields
-
-    _ModStoredFields( %params );
-
-    Updates the values for the 'original' fields in course_items
-    for a given ci_id
-
-=cut
 
-sub _ModStoredFields {
-    my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
+    $params{homebranch} ||= undef; # Can't be empty string, FK constraint
+    $params{holdingbranch} ||= undef; # Can't be empty string, FK constraint
 
-    return unless ( $params{'ci_id'} );
+    return unless ( $ci_id || $course_item );
 
-    my ( @fields_to_update, @values_to_update );
+    $course_item = Koha::Course::Items->find( $ci_id || $course_item->{ci_id} );
+
+    my %data = map { $_ => $params{$_} } @FIELDS;
+    my %enabled = map { $_ . "_enabled" => $params{ $_ . "_enabled" } } @FIELDS;
+
+    if ( $course_item->itemnumber ) {
+        # biblio-level course items don't store any of these fields
+        my $item = Koha::Items->find( $course_item->itemnumber );
+
+        # Handle updates to changed fields for a course item, both adding and removing
+        if ( $course_item->is_enabled ) {
+            my $item_fields = {};
+
+            for my $field ( @FIELDS ) {
+
+                my $field_enabled = $field . '_enabled';
+                my $field_storage = $field . '_storage';
+
+                # Find newly enabled field and add item value to storage
+                if ( $params{$field_enabled} && !$course_item->$field_enabled ) {
+                    $enabled{$field_storage} = $item->$field;
+                    $item_fields->{$field}   = $params{$field};
+                }
+                # Find newly disabled field and copy the storage value to the item, unset storage value
+                elsif ( !$params{$field_enabled} && $course_item->$field_enabled ) {
+                    $item_fields->{$field}   = $course_item->$field_storage;
+                    $enabled{$field_storage} = undef;
+                }
+                # The field was already enabled, copy the incoming value to the item.
+                # The "original" ( when not on course reserve ) value is already in the storage field
+                elsif ( $course_item->$field_enabled) {
+                    $item_fields->{$field} = $params{$field};
+                }
+            }
 
-    foreach (@FIELDS) {
-        if ( $params{$_} ) {
-            push( @fields_to_update, $_ );
-            push( @values_to_update, $params{$_} );
+            $item->set( $item_fields )->store
+                if keys %$item_fields;
         }
     }
 
-    my $query =
-        "UPDATE course_items SET "
-      . join( ',', map { "$_=?" } @fields_to_update )
-      . " WHERE ci_id = ?";
-
-    C4::Context->dbh->do( $query, undef, @values_to_update, $params{'ci_id'} )
-      if (@values_to_update);
+    $course_item->update( { %data, %enabled } );
 
 }
 
@@ -632,40 +585,37 @@ sub _ModStoredFields {
 
     _RevertFields( ci_id => $ci_id, fields => \@fields_to_revert );
 
+    Copies fields from course item storage back to the actual item
+
 =cut
 
 sub _RevertFields {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
-    my $ci_id       = $params{'ci_id'};
-    my $course_item = $params{'course_item'};
-    my $fields      = $params{'fields'};
-    my @fields      = @$fields;
+    my $ci_id = $params{'ci_id'};
 
-    return unless ($ci_id);
+    return unless $ci_id;
 
-    $course_item = GetCourseItem( ci_id => $params{'ci_id'} )
-      unless ($course_item);
+    my $course_item = Koha::Course::Items->find( $ci_id );
 
-    my $mod_item_params;
-    my @fields_to_null;
-    foreach my $field (@fields) {
-        foreach (@FIELDS) {
-            if ( $field eq $_ && $course_item->{$_} ) {
-                $mod_item_params->{$_} = $course_item->{$_};
-                push( @fields_to_null, $_ );
-            }
-        }
-    }
-    ModItem( $mod_item_params, undef, $course_item->{'itemnumber'} );
+    my $item_fields = {};
+    $item_fields->{itype}         = $course_item->itype_storage         if $course_item->itype_enabled;
+    $item_fields->{ccode}         = $course_item->ccode_storage         if $course_item->ccode_enabled;
+    $item_fields->{location}      = $course_item->location_storage      if $course_item->location_enabled;
+    $item_fields->{homebranch} = $course_item->homebranch_storage if $course_item->homebranch_enabled;
+    $item_fields->{holdingbranch} = $course_item->holdingbranch_storage if $course_item->holdingbranch_enabled;
 
-    my $query =
-        "UPDATE course_items SET "
-      . join( ',', map { "$_=NULL" } @fields_to_null )
-      . " WHERE ci_id = ?";
+    Koha::Items->find( $course_item->itemnumber )
+               ->set( $item_fields )
+               ->store
+        if keys %$item_fields;
 
-    C4::Context->dbh->do( $query, undef, $ci_id ) if (@fields_to_null);
+    $course_item->itype_storage(undef);
+    $course_item->ccode_storage(undef);
+    $course_item->location_storage(undef);
+    $course_item->homebranch_storage(undef);
+    $course_item->holdingbranch_storage(undef);
+    $course_item->store();
 }
 
 =head2 _SwapAllFields
@@ -675,23 +625,51 @@ sub _RevertFields {
 =cut
 
 sub _SwapAllFields {
-    my ($ci_id) = @_;
-    warn "C4::CourseReserves::_SwapFields( $ci_id )" if $DEBUG;
-
-    my $course_item = GetCourseItem( ci_id => $ci_id );
-    my $item = GetItem( $course_item->{'itemnumber'} );
-
-    my %course_item_fields;
-    my %item_fields;
-    foreach (@FIELDS) {
-        if ( $course_item->{$_} ) {
-            $course_item_fields{$_} = $course_item->{$_};
-            $item_fields{$_}        = $item->{$_};
-        }
+    my ( $ci_id, $enabled ) = @_;
+
+    my $course_item = Koha::Course::Items->find( $ci_id );
+    my $item = Koha::Items->find( $course_item->itemnumber );
+
+    if ( $enabled eq 'yes' ) { # Copy item fields to course item storage, course item fields to item
+        $course_item->itype_storage( $item->effective_itemtype )    if $course_item->itype_enabled;
+        $course_item->ccode_storage( $item->ccode )                 if $course_item->ccode_enabled;
+        $course_item->location_storage( $item->location )           if $course_item->location_enabled;
+        $course_item->homebranch_storage( $item->homebranch )       if $course_item->homebranch_enabled;
+        $course_item->holdingbranch_storage( $item->holdingbranch ) if $course_item->holdingbranch_enabled;
+        $course_item->store();
+
+        my $item_fields = {};
+        $item_fields->{itype}         = $course_item->itype         if $course_item->itype_enabled;
+        $item_fields->{ccode}         = $course_item->ccode         if $course_item->ccode_enabled;
+        $item_fields->{location}      = $course_item->location      if $course_item->location_enabled;
+        $item_fields->{homebranch}    = $course_item->homebranch    if $course_item->homebranch_enabled;
+        $item_fields->{holdingbranch} = $course_item->holdingbranch if $course_item->holdingbranch_enabled;
+
+        Koha::Items->find( $course_item->itemnumber )
+                   ->set( $item_fields )
+                   ->store
+            if keys %$item_fields;
+
+    } else { # Copy course item storage to item
+        my $item_fields = {};
+        $item_fields->{itype}         = $course_item->itype_storage         if $course_item->itype_enabled;
+        $item_fields->{ccode}         = $course_item->ccode_storage         if $course_item->ccode_enabled;
+        $item_fields->{location}      = $course_item->location_storage      if $course_item->location_enabled;
+        $item_fields->{homebranch}    = $course_item->homebranch_storage    if $course_item->homebranch_enabled;
+        $item_fields->{holdingbranch} = $course_item->holdingbranch_storage if $course_item->holdingbranch_enabled;
+
+        Koha::Items->find( $course_item->itemnumber )
+                   ->set( $item_fields )
+                   ->store
+            if keys %$item_fields;
+
+        $course_item->itype_storage(undef);
+        $course_item->ccode_storage(undef);
+        $course_item->location_storage(undef);
+        $course_item->homebranch_storage(undef);
+        $course_item->holdingbranch_storage(undef);
+        $course_item->store();
     }
-
-    ModItem( \%course_item_fields, undef, $course_item->{'itemnumber'} );
-    _ModStoredFields( %item_fields, ci_id => $ci_id );
 }
 
 =head2 GetCourseItems {
@@ -705,7 +683,6 @@ sub _SwapAllFields {
 
 sub GetCourseItems {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $course_id  = $params{'course_id'};
     my $itemnumber = $params{'itemnumber'};
@@ -744,13 +721,15 @@ sub GetCourseItems {
 
 sub DelCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id = $params{'ci_id'};
 
     return unless ($ci_id);
 
-    _RevertFields( ci_id => $ci_id, fields => \@FIELDS );
+    my $course_item = Koha::Course::Items->find( $ci_id );
+    return unless $course_item;
+
+    _RevertFields( ci_id => $ci_id ) if $course_item->enabled eq 'yes';
 
     my $query = "
         DELETE FROM course_items
@@ -767,7 +746,6 @@ sub DelCourseItem {
 
 sub GetCourseReserve {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $cr_id     = $params{'cr_id'};
     my $course_id = $params{'course_id'};
@@ -785,8 +763,7 @@ sub GetCourseReserve {
         ";
         $sth = $dbh->prepare($query);
         $sth->execute($cr_id);
-    }
-    else {
+    } else {
         my $query = "
             SELECT * FROM course_reserves
             WHERE course_id = ? AND ci_id = ?
@@ -807,7 +784,6 @@ sub GetCourseReserve {
 
 sub ModCourseReserve {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $course_id   = $params{'course_id'};
     my $ci_id       = $params{'ci_id'};
@@ -816,8 +792,7 @@ sub ModCourseReserve {
 
     return unless ( $course_id && $ci_id );
 
-    my $course_reserve =
-      GetCourseReserve( course_id => $course_id, ci_id => $ci_id );
+    my $course_reserve = GetCourseReserve( course_id => $course_id, ci_id => $ci_id );
     my $cr_id;
 
     my $dbh = C4::Context->dbh;
@@ -831,8 +806,7 @@ sub ModCourseReserve {
             WHERE cr_id = ?
         ";
         $dbh->do( $query, undef, $staff_note, $public_note, $cr_id );
-    }
-    else {
+    } else {
         my $query = "
             INSERT INTO course_reserves SET
             course_id = ?,
@@ -840,16 +814,12 @@ sub ModCourseReserve {
             staff_note = ?,
             public_note = ?
         ";
-        $dbh->do( $query, undef, $course_id, $ci_id, $staff_note,
-            $public_note );
-        $cr_id =
-          $dbh->last_insert_id( undef, undef, 'course_reserves', 'cr_id' );
+        $dbh->do( $query, undef, $course_id, $ci_id, $staff_note, $public_note );
+        $cr_id = $dbh->last_insert_id( undef, undef, 'course_reserves', 'cr_id' );
     }
 
-    my $course = GetCourse($course_id);
     EnableOrDisableCourseItem(
         ci_id   => $params{'ci_id'},
-        enabled => $course->{'enabled'}
     );
 
     return $cr_id;
@@ -870,12 +840,11 @@ sub ModCourseReserve {
 
 sub GetCourseReserves {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
-    my $course_id     = $params{'course_id'};
-    my $ci_id         = $params{'ci_id'};
-    my $include_items = $params{'include_items'};
-    my $include_count = $params{'include_count'};
+    my $course_id       = $params{'course_id'};
+    my $ci_id           = $params{'ci_id'};
+    my $include_items   = $params{'include_items'};
+    my $include_count   = $params{'include_count'};
     my $include_courses = $params{'include_courses'};
 
     return unless ( $course_id || $ci_id );
@@ -884,7 +853,7 @@ sub GetCourseReserves {
     my $value = ($course_id) ? $course_id  : $ci_id;
 
     my $query = "
-        SELECT cr.*, ci.itemnumber
+        SELECT cr.*, ci.itemnumber, ci.biblionumber
         FROM course_reserves cr, course_items ci
         WHERE cr.$field = ?
         AND cr.ci_id = ci.ci_id
@@ -897,23 +866,26 @@ sub GetCourseReserves {
 
     if ($include_items) {
         foreach my $cr (@$course_reserves) {
+            my $item = Koha::Items->find( $cr->{itemnumber}, { prefetch => ['biblio','biblioitem'] });
+            my $biblio = $cr->{itemnumber} ? $item->biblio : Koha::Biblios->find( $cr->{biblionumber}, { prefetch => ['biblioitem'] });
+            my $biblioitem = $biblio->biblioitem;
             $cr->{'course_item'} = GetCourseItem( ci_id => $cr->{'ci_id'} );
-            $cr->{'item'} = GetBiblioFromItemNumber( $cr->{'itemnumber'} );
-            $cr->{'issue'} = GetOpenIssue( $cr->{'itemnumber'} );
+            $cr->{'item'}        = $item;
+            $cr->{'biblio'}      = $biblio;
+            $cr->{'biblioitem'}  = $biblioitem;
+            $cr->{'issue'}       = Koha::Checkouts->find({ itemnumber => $cr->{'itemnumber'} });
         }
     }
 
     if ($include_count) {
         foreach my $cr (@$course_reserves) {
-            $cr->{'reserves_count'} =
-              CountCourseReservesForItem( ci_id => $cr->{'ci_id'} );
+            $cr->{'reserves_count'} = CountCourseReservesForItem( ci_id => $cr->{'ci_id'} );
         }
     }
 
     if ($include_courses) {
         foreach my $cr (@$course_reserves) {
-            $cr->{'courses'} =
-              GetCourses( itemnumber => $cr->{'itemnumber'} );
+            $cr->{'courses'} = $cr->{itemnumber} ? GetCourses( itemnumber => $cr->{'itemnumber'} ) : GetCourses( biblionumber => $cr->{biblionumber} );
         }
     }
 
@@ -928,7 +900,6 @@ sub GetCourseReserves {
 
 sub DelCourseReserve {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $cr_id = $params{'cr_id'};
 
@@ -946,31 +917,31 @@ sub DelCourseReserve {
 
     ## If there are no other course reserves for this item
     ## delete the course_item as well
-    unless ( CountCourseReservesForItem( ci_id => $course_reserve->{'ci_id'} ) )
-    {
+    unless ( CountCourseReservesForItem( ci_id => $course_reserve->{'ci_id'} ) ) {
         DelCourseItem( ci_id => $course_reserve->{'ci_id'} );
     }
 
 }
 
-=head2 GetReservesInfo
+=head2 GetItemCourseReservesInfo
 
-    my $arrayref = GetItemReservesInfo( itemnumber => $itemnumber );
+    my $arrayref = GetItemCourseReservesInfo( itemnumber => $itemnumber );
+    my $arrayref = GetItemCourseReservesInfo( biblionumber => $biblionumber );
 
-    For a given item, returns an arrayref of reserves hashrefs,
+    For a given itemnumber or biblionumber, returns an arrayref of reserves hashrefs,
     with a course hashref under the key 'course'
 
 =cut
 
-sub GetItemReservesInfo {
+sub GetItemCourseReservesInfo {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $itemnumber = $params{'itemnumber'};
+    my $biblionumber = $params{'biblionumber'};
 
-    return unless ($itemnumber);
+    return unless ($itemnumber xor $biblionumber);
 
-    my $course_item = GetCourseItem( itemnumber => $itemnumber );
+    my $course_item = $itemnumber ? GetCourseItem( itemnumber => $itemnumber ) : GetCourseItem( biblionumber => $biblionumber );
 
     return unless ( keys %$course_item );
 
@@ -990,6 +961,8 @@ sub GetItemReservesInfo {
     ci_id - course_item id
     OR
     itemnumber - course_item itemnumber
+    OR
+    biblionumber - course_item biblionumber
 
     enabled = 'yes' or 'no'
     Optional, if not supplied, counts reserves
@@ -999,16 +972,15 @@ sub GetItemReservesInfo {
 
 sub CountCourseReservesForItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id      = $params{'ci_id'};
     my $itemnumber = $params{'itemnumber'};
     my $enabled    = $params{'enabled'};
+    my $biblionumber = $params{'biblionumber'};
 
-    return unless ( $ci_id || $itemnumber );
+    return unless ( $ci_id xor $itemnumber xor $biblionumber );
 
-    my $course_item =
-      GetCourseItem( ci_id => $ci_id, itemnumber => $itemnumber );
+    my $course_item = GetCourseItem( ci_id => $ci_id ) || GetCourseItem( itemnumber => $itemnumber ) || GetCourseItem( biblionumber => $biblionumber );
 
     my @params = ( $course_item->{'ci_id'} );
     push( @params, $enabled ) if ($enabled);
@@ -1038,16 +1010,15 @@ sub CountCourseReservesForItem {
 
 sub SearchCourses {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $term = $params{'term'};
 
     my $enabled = $params{'enabled'} || '%';
 
     my @params;
-    my $query = "SELECT c.* FROM courses c";
-
-    $query .= "
+    my $query = "
+        SELECT c.course_id, c.department, c.course_number, c.section, c.course_name, c.term, c.staff_note, c.public_note, c.students_count, c.enabled, c.timestamp
+        FROM courses c
         LEFT JOIN course_instructors ci
             ON ( c.course_id = ci.course_id )
         LEFT JOIN borrowers b
@@ -1071,9 +1042,10 @@ sub SearchCourses {
            )
            AND
            c.enabled LIKE ?
-        GROUP BY c.course_id
+        GROUP BY c.course_id, c.department, c.course_number, c.section, c.course_name, c.term, c.staff_note, c.public_note, c.students_count, c.enabled, c.timestamp
     ";
 
+    $term //= '';
     $term   = "%$term%";
     @params = ($term) x 10;
 
@@ -1082,7 +1054,7 @@ sub SearchCourses {
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare($query);
 
-    $sth->execute(@params, $enabled);
+    $sth->execute( @params, $enabled );
 
     my $courses = $sth->fetchall_arrayref( {} );
 
@@ -1108,12 +1080,6 @@ sub stringify_params {
     return "( $string )";
 }
 
-sub identify_myself {
-    my (%params) = @_;
-
-    return whowasi() . stringify_params(%params);
-}
-
 1;
 
 =head1 AUTHOR