# 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 vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG @FIELDS);
BEGIN {
- @ISA = qw(Exporter);
- @EXPORT = qw(
+ require Exporter;
+ @ISA = qw(Exporter);
+ @EXPORT_OK = qw(
&GetCourse
&ModCourse
&GetCourses
&SearchCourses
- &GetItemReservesInfo
+ &GetItemCourseReservesInfo
);
+ %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
$DEBUG = 0;
@FIELDS = ( 'itype', 'ccode', 'holdingbranch', 'location' );
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';
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',
);
}
}
ci_id => $course_reserve->{'ci_id'},
enabled => 'yes'
)
- )
- {
+ ) {
EnableOrDisableCourseItem(
ci_id => $course_reserve->{'ci_id'},
- enabled => 'no',
);
}
}
=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
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
return unless ( $cardnumbers || $borrowernumbers );
return if ( $cardnumbers && $borrowernumbers );
- my @cardnumbers = @$cardnumbers if ( ref($cardnumbers) eq 'ARRAY' );
- my @borrowernumbers = @$borrowernumbers
+ my ( @cardnumbers, @borrowernumbers );
+ @cardnumbers = @$cardnumbers if ( ref($cardnumbers) eq 'ARRAY' );
+ @borrowernumbers = @$borrowernumbers
if ( ref($borrowernumbers) eq 'ARRAY' );
my $field = (@cardnumbers) ? 'cardnumber' : 'borrowernumber';
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;
FROM borrowers
WHERE $field IN ( $placeholders )
";
- }
- else {
+ } else {
$query = "
DELETE FROM course_instructors
WHERE course_id = ?
course_item => $course_item,
%params
);
- }
- else {
+ } else {
$ci_id = _AddCourseItem(%params);
}
foreach (@FIELDS) {
if ( $params{$_}
&& $course_item->{$_}
- && $params{$_} ne $course_item->{$_} )
- {
+ && $params{$_} ne $course_item->{$_} ) {
$mod_params{$_} = $params{$_};
}
}
- ModItem( \%mod_params, undef, $course_item->{'itemnumber'} );
+ 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
}
}
_ModStoredFields( 'ci_id' => $params{'ci_id'}, %mod_params_old );
- ModItem( \%mod_params_new, undef, $course_item->{'itemnumber'} );
+ ModItem( \%mod_params_new, undef, $course_item->{'itemnumber'} ) if %mod_params_new;
}
}
}
- my $query =
- "UPDATE course_items SET "
- . join( ',', map { "$_=?" } @fields_to_update )
- . " WHERE ci_id = ?";
+ 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);
}
}
}
- ModItem( $mod_item_params, undef, $course_item->{'itemnumber'} );
+ 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 = ?";
+ 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( \%course_item_fields, undef, $course_item->{'itemnumber'} );
+ ModItem( \%course_item_fields, undef, $course_item->{'itemnumber'} ) if %course_item_fields;
_ModStoredFields( %item_fields, ci_id => $ci_id );
}
";
$sth = $dbh->prepare($query);
$sth->execute($cr_id);
- }
- else {
+ } else {
my $query = "
SELECT * FROM course_reserves
WHERE course_id = ? AND ci_id = ?
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;
WHERE cr_id = ?
";
$dbh->do( $query, undef, $staff_note, $public_note, $cr_id );
- }
- else {
+ } else {
my $query = "
INSERT INTO course_reserves SET
course_id = ?,
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;
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 );
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->{'issue'} = GetOpenIssue( $cr->{'itemnumber'} );
+ $cr->{'item'} = $item;
+ $cr->{'biblio'} = $biblio;
+ $cr->{'biblioitem'} = $biblioitem;
+ $cr->{'issue'} = GetOpenIssue( $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'} = GetCourses( itemnumber => $cr->{'itemnumber'} );
}
}
## 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 );
For a given item, 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;
return unless ( $ci_id || $itemnumber );
- my $course_item =
- GetCourseItem( ci_id => $ci_id, itemnumber => $itemnumber );
+ my $course_item = GetCourseItem( ci_id => $ci_id, itemnumber => $itemnumber );
my @params = ( $course_item->{'ci_id'} );
push( @params, $enabled ) if ($enabled);
GROUP BY c.course_id
";
+ $term //= '';
$term = "%$term%";
@params = ($term) x 10;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($query);
- $sth->execute(@params, $enabled);
+ $sth->execute( @params, $enabled );
my $courses = $sth->fetchall_arrayref( {} );