Bug 14237: Database updates
[srvgit] / C4 / CourseReserves.pm
index ed12690..061c839 100644 (file)
@@ -27,7 +27,7 @@ use Koha::Course::Instructors;
 use Koha::Course::Items;
 use Koha::Course::Reserves;
 
-use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG @FIELDS);
+use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @FIELDS);
 
 BEGIN {
     require Exporter;
@@ -55,7 +55,6 @@ BEGIN {
     );
     %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
 
-    $DEBUG = 0;
     @FIELDS = ( 'itype', 'ccode', 'homebranch', 'holdingbranch', 'location' );
 }
 
@@ -81,10 +80,9 @@ This module deals with course reserves.
 
 sub GetCourse {
     my ($course_id) = @_;
-    warn whoami() . "( $course_id )" if $DEBUG;
 
     my $course = Koha::Courses->find( $course_id );
-    return undef unless $course;
+    return unless $course;
     $course = $course->unblessed;
 
     my $dbh = C4::Context->dbh;
@@ -108,7 +106,6 @@ sub GetCourse {
 
 sub ModCourse {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $dbh = C4::Context->dbh;
 
@@ -158,7 +155,6 @@ sub ModCourse {
 
 sub GetCourses {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my @query_keys;
     my @query_values;
@@ -241,7 +237,6 @@ sub DelCourse {
 
 sub EnableOrDisableCourseItems {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $course_id = $params{'course_id'};
     my $enabled = $params{'enabled'} || 0;
@@ -290,7 +285,6 @@ sub EnableOrDisableCourseItems {
 
 sub EnableOrDisableCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id   = $params{'ci_id'};
 
@@ -298,7 +292,7 @@ sub EnableOrDisableCourseItem {
 
     my $course_item = GetCourseItem( ci_id => $ci_id );
 
-    my $info = GetItemCourseReservesInfo( itemnumber => $course_item->{itemnumber} );
+    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';
@@ -329,8 +323,6 @@ sub EnableOrDisableCourseItem {
 
 sub GetCourseInstructors {
     my ($course_id) = @_;
-    warn "C4::CourseReserves::GetCourseInstructors( $course_id )"
-      if $DEBUG;
 
     my $query = "
         SELECT * FROM borrowers
@@ -359,7 +351,6 @@ sub GetCourseInstructors {
 
 sub ModCourseInstructors {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $course_id       = $params{'course_id'};
     my $mode            = $params{'mode'};
@@ -420,15 +411,24 @@ sub ModCourseInstructors {
 
 sub GetCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id      = $params{'ci_id'};
     my $itemnumber = $params{'itemnumber'};
+    my $biblionumber = $params{'biblionumber'};
 
-    return unless ( $itemnumber || $ci_id );
+    return unless ( $itemnumber || $biblionumber || $ci_id );
 
-    my $field = ($itemnumber) ? 'itemnumber' : 'ci_id';
-    my $value = ($itemnumber) ? $itemnumber  : $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;
@@ -459,13 +459,18 @@ sub GetCourseItem {
 
 sub ModCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $itemnumber = $params{'itemnumber'};
+    my $biblionumber = $params{'biblionumber'};
 
-    return unless ($itemnumber);
+    return unless ($itemnumber || $biblionumber);
 
-    my $course_item = GetCourseItem( itemnumber => $itemnumber );
+    my $course_item = $itemnumber ? GetCourseItem( itemnumber => $itemnumber ) : GetCourseItem( biblionumber => $biblionumber );
+
+    if ( $itemnumber and !$biblionumber ) {
+        $biblionumber = Koha::Items->find( $itemnumber )->biblionumber;
+        $params{biblionumber} = $biblionumber;
+    }
 
     my $ci_id;
 
@@ -493,7 +498,6 @@ sub ModCourseItem {
 
 sub _AddCourseItem {
     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
@@ -504,6 +508,7 @@ sub _AddCourseItem {
     my $ci = Koha::Course::Item->new(
         {
             itemnumber => $params{itemnumber},
+            biblionumber => $params{biblionumber},
             %data,
             %enabled,
         }
@@ -520,7 +525,6 @@ sub _AddCourseItem {
 
 sub _UpdateCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id         = $params{'ci_id'};
     my $course_item   = $params{'course_item'};
@@ -535,74 +539,39 @@ sub _UpdateCourseItem {
     my %data = map { $_ => $params{$_} } @FIELDS;
     my %enabled = map { $_ . "_enabled" => $params{ $_ . "_enabled" } } @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 = {};
-
-        # Find newly enabled field and add item value to storage
-        if ( $params{itype_enabled} && !$course_item->itype_enabled ) {
-            $enabled{itype_storage} = $item->itype;
-            $item_fields->{itype} = $params{itype};
-        }
-        # Find newly disabled field and copy the storage value to the item, unset storage value
-        elsif ( !$params{itype_enabled} && $course_item->itype_enabled ) {
-            $item_fields->{itype} = $course_item->itype_storage;
-            $enabled{itype_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->itype_enabled) {
-            $item_fields->{itype} = $params{itype};
-        }
-
-        if ( $params{ccode_enabled} && !$course_item->ccode_enabled ) {
-            $enabled{ccode_storage} = $item->ccode;
-            $item_fields->{ccode} = $params{ccode};
-        }
-        elsif ( !$params{ccode_enabled} && $course_item->ccode_enabled ) {
-            $item_fields->{ccode} = $course_item->ccode_storage;
-            $enabled{ccode_storage} = undef;
-        } elsif ( $course_item->ccode_enabled) {
-            $item_fields->{ccode} = $params{ccode};
-        }
-
-        if ( $params{location_enabled} && !$course_item->location_enabled ) {
-            $enabled{location_storage} = $item->location;
-            $item_fields->{location} = $params{location};
-        }
-        elsif ( !$params{location_enabled} && $course_item->location_enabled ) {
-            $item_fields->{location} = $course_item->location_storage;
-            $enabled{location_storage} = undef;
-        } elsif ( $course_item->location_enabled) {
-            $item_fields->{location} = $params{location};
-        }
-
-        if ( $params{homebranch_enabled} && !$course_item->homebranch_enabled ) {
-            $enabled{homebranch_storage} = $item->homebranch;
-            $item_fields->{homebranch} = $params{homebranch};
-        }
-        elsif ( !$params{homebranch_enabled} && $course_item->homebranch_enabled ) {
-            $item_fields->{homebranch} = $course_item->homebranch_storage;
-            $enabled{homebranch_storage} = undef;
-        } elsif ( $course_item->homebranch_enabled) {
-            $item_fields->{homebranch} = $params{homebranch};
-        }
+    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};
+                }
+            }
 
-        if ( $params{holdingbranch_enabled} && !$course_item->holdingbranch_enabled ) {
-            $enabled{holdingbranch_storage} = $item->holdingbranch;
-            $item_fields->{holdingbranch} = $params{holdingbranch};
-        }
-        elsif ( !$params{holdingbranch_enabled} && $course_item->holdingbranch_enabled ) {
-            $item_fields->{holdingbranch} = $course_item->holdingbranch_storage;
-            $enabled{holdingbranch_storage} = undef;
-        } elsif ( $course_item->holdingbranch_enabled) {
-            $item_fields->{holdingbranch} = $params{holdingbranch};
+            $item->set( $item_fields )->store
+                if keys %$item_fields;
         }
-
-        $item->set( $item_fields )->store
-            if keys %$item_fields;
     }
 
     $course_item->update( { %data, %enabled } );
@@ -619,7 +588,6 @@ sub _UpdateCourseItem {
 
 sub _RevertFields {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id = $params{'ci_id'};
 
@@ -655,7 +623,6 @@ sub _RevertFields {
 
 sub _SwapAllFields {
     my ( $ci_id, $enabled ) = @_;
-    warn "C4::CourseReserves::_SwapFields( $ci_id )" if $DEBUG;
 
     my $course_item = Koha::Course::Items->find( $ci_id );
     my $item = Koha::Items->find( $course_item->itemnumber );
@@ -713,7 +680,6 @@ sub _SwapAllFields {
 
 sub GetCourseItems {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $course_id  = $params{'course_id'};
     my $itemnumber = $params{'itemnumber'};
@@ -752,7 +718,6 @@ sub GetCourseItems {
 
 sub DelCourseItem {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $ci_id = $params{'ci_id'};
 
@@ -778,7 +743,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'};
@@ -817,7 +781,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'};
@@ -874,7 +837,6 @@ sub ModCourseReserve {
 
 sub GetCourseReserves {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $course_id       = $params{'course_id'};
     my $ci_id           = $params{'ci_id'};
@@ -888,7 +850,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
@@ -902,7 +864,7 @@ sub GetCourseReserves {
     if ($include_items) {
         foreach my $cr (@$course_reserves) {
             my $item = Koha::Items->find( $cr->{itemnumber} );
-            my $biblio = $item->biblio;
+            my $biblio = $cr->{itemnumber} ? $item->biblio : Koha::Biblios->find( $cr->{biblionumber} );
             my $biblioitem = $biblio->biblioitem;
             $cr->{'course_item'} = GetCourseItem( ci_id => $cr->{'ci_id'} );
             $cr->{'item'}        = $item;
@@ -920,7 +882,7 @@ sub GetCourseReserves {
 
     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} );
         }
     }
 
@@ -935,7 +897,6 @@ sub GetCourseReserves {
 
 sub DelCourseReserve {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $cr_id = $params{'cr_id'};
 
@@ -962,6 +923,7 @@ sub DelCourseReserve {
 =head2 GetItemCourseReservesInfo
 
     my $arrayref = GetItemCourseReservesInfo( itemnumber => $itemnumber );
+    my $arrayref = GetItemCourseReservesInfo( biblionumber => $biblionumber );
 
     For a given item, returns an arrayref of reserves hashrefs,
     with a course hashref under the key 'course'
@@ -970,13 +932,13 @@ sub DelCourseReserve {
 
 sub GetItemCourseReservesInfo {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $itemnumber = $params{'itemnumber'};
+    my $biblionumber = $params{'biblionumber'};
 
-    return unless ($itemnumber);
+    return unless ($itemnumber || $biblionumber);
 
-    my $course_item = GetCourseItem( itemnumber => $itemnumber );
+    my $course_item = $itemnumber ? GetCourseItem( itemnumber => $itemnumber ) : GetCourseItem( biblionumber => $biblionumber );
 
     return unless ( keys %$course_item );
 
@@ -996,6 +958,8 @@ sub GetItemCourseReservesInfo {
     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
@@ -1005,15 +969,15 @@ sub GetItemCourseReservesInfo {
 
 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 || ( $itemnumber || $biblionumber ) );
 
-    my $course_item = GetCourseItem( ci_id => $ci_id, itemnumber => $itemnumber );
+    my $course_item = $itemnumber ? GetCourseItem( ci_id => $ci_id, itemnumber => $itemnumber ) : GetCourseItem( ci_id => $ci_id, biblionumber => $biblionumber );
 
     my @params = ( $course_item->{'ci_id'} );
     push( @params, $enabled ) if ($enabled);
@@ -1043,7 +1007,6 @@ sub CountCourseReservesForItem {
 
 sub SearchCourses {
     my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
 
     my $term = $params{'term'};
 
@@ -1114,12 +1077,6 @@ sub stringify_params {
     return "( $string )";
 }
 
-sub identify_myself {
-    my (%params) = @_;
-
-    return whowasi() . stringify_params(%params);
-}
-
 1;
 
 =head1 AUTHOR