Bug 22330: (QA follow-up) Remove duplicate use lines, combine and sort remaning lines
[koha.git] / C4 / CourseReserves.pm
index c3e25e6..428d7e9 100644 (file)
@@ -2,27 +2,28 @@ 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;
 
+use List::MoreUtils qw(any);
+
 use C4::Context;
-use C4::Items qw(GetItem ModItem);
-use C4::Biblio qw(GetBiblioFromItemNumber);
+use C4::Items qw(ModItem);
 use C4::Circulation qw(GetOpenIssue);
 
-use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG @FIELDS);
+use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG @FIELDS);
 
 BEGIN {
     require Exporter;
@@ -161,9 +162,9 @@ sub GetCourses {
     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 +180,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);
@@ -259,7 +260,6 @@ sub EnableOrDisableCourseItems {
               ) {
                 EnableOrDisableCourseItem(
                     ci_id   => $course_reserve->{'ci_id'},
-                    enabled => 'yes',
                 );
             }
         }
@@ -274,7 +274,6 @@ sub EnableOrDisableCourseItems {
               ) {
                 EnableOrDisableCourseItem(
                     ci_id   => $course_reserve->{'ci_id'},
-                    enabled => 'no',
                 );
             }
         }
@@ -283,10 +282,7 @@ 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
 
@@ -295,13 +291,16 @@ sub EnableOrDisableCourseItem {
     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 = GetItemCourseReservesInfo( itemnumber => $course_item->{itemnumber} );
+
+    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
@@ -460,11 +459,7 @@ 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'};
 
     return unless ($itemnumber);
 
@@ -504,10 +499,8 @@ sub _AddCourseItem {
     push( @values, $params{'itemnumber'} );
 
     foreach (@FIELDS) {
-        if ( $params{$_} ) {
-            push( @fields, "$_ = ?" );
-            push( @values, $params{$_} );
-        }
+        push( @fields, "$_ = ?" );
+        push( @values, $params{$_} || undef );
     }
 
     my $query = "INSERT INTO course_items SET " . join( ',', @fields );
@@ -531,10 +524,6 @@ sub _UpdateCourseItem {
 
     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 );
 
@@ -542,49 +531,13 @@ sub _UpdateCourseItem {
       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{$_};
-        }
+        $mod_params{$_} = $params{$_};
     }
-    ModItem( \%mod_params, undef, $course_item->{'itemnumber'} ) if %mod_params;
-
-    ## 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'} ) if %mod_params_new;
 
+    ModItem( \%mod_params, undef, $course_item->{'itemnumber'} );
 }
 
 =head2 _ModStoredFields
@@ -605,7 +558,7 @@ sub _ModStoredFields {
     my ( @fields_to_update, @values_to_update );
 
     foreach (@FIELDS) {
-        if ( $params{$_} ) {
+        if ( defined($params{$_}) ) {
             push( @fields_to_update, $_ );
             push( @values_to_update, $params{$_} );
         }
@@ -628,31 +581,18 @@ 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);
 
-    $course_item = GetCourseItem( ci_id => $params{'ci_id'} )
-      unless ($course_item);
+    my $course_item = GetCourseItem( ci_id => $params{'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, $_ );
-            }
-        }
+    foreach my $field ( @FIELDS ) {
+        $mod_item_params->{$field} = $course_item->{$field};
     }
-    ModItem( $mod_item_params, undef, $course_item->{'itemnumber'} ) if $mod_item_params && %$mod_item_params;
-
-    my $query = "UPDATE course_items SET " . join( ',', map { "$_=NULL" } @fields_to_null ) . " WHERE ci_id = ?";
 
-    C4::Context->dbh->do( $query, undef, $ci_id ) if (@fields_to_null);
+    ModItem( $mod_item_params, undef, $course_item->{'itemnumber'} ) if $mod_item_params && %$mod_item_params;
 }
 
 =head2 _SwapAllFields
@@ -666,14 +606,14 @@ sub _SwapAllFields {
     warn "C4::CourseReserves::_SwapFields( $ci_id )" if $DEBUG;
 
     my $course_item = GetCourseItem( ci_id => $ci_id );
-    my $item = GetItem( $course_item->{'itemnumber'} );
+    my $item = Koha::Items->find($course_item->{'itemnumber'});
 
     my %course_item_fields;
     my %item_fields;
     foreach (@FIELDS) {
-        if ( $course_item->{$_} ) {
+        if ( defined( $course_item->{$_} ) ) {
             $course_item_fields{$_} = $course_item->{$_};
-            $item_fields{$_}        = $item->{$_};
+            $item_fields{$_}        = $item->$_ || q{};
         }
     }
 
@@ -737,7 +677,7 @@ sub DelCourseItem {
 
     return unless ($ci_id);
 
-    _RevertFields( ci_id => $ci_id, fields => \@FIELDS );
+    _RevertFields( ci_id => $ci_id );
 
     my $query = "
         DELETE FROM course_items
@@ -828,10 +768,8 @@ sub ModCourseReserve {
         $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;
@@ -879,8 +817,13 @@ sub GetCourseReserves {
 
     if ($include_items) {
         foreach my $cr (@$course_reserves) {
+            my $item = Koha::Items->find( $cr->{itemnumber} );
+            my $biblio = $item->biblio;
+            my $biblioitem = $biblio->biblioitem;
             $cr->{'course_item'} = GetCourseItem( ci_id => $cr->{'ci_id'} );
-            $cr->{'item'}        = GetBiblioFromItemNumber( $cr->{'itemnumber'} );
+            $cr->{'item'}        = $item;
+            $cr->{'biblio'}      = $biblio;
+            $cr->{'biblioitem'}  = $biblioitem;
             $cr->{'issue'}       = GetOpenIssue( $cr->{'itemnumber'} );
         }
     }
@@ -932,7 +875,7 @@ sub DelCourseReserve {
 
 }
 
-=head2 GetReservesInfo
+=head2 GetItemCourseReservesInfo
 
     my $arrayref = GetItemCourseReservesInfo( itemnumber => $itemnumber );
 
@@ -1023,9 +966,9 @@ sub SearchCourses {
     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
@@ -1049,9 +992,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;