From d293abbfab9588d0553b2517f220d4513fc54724 Mon Sep 17 00:00:00 2001 From: Nicolas Legrand Date: Fri, 27 Mar 2015 16:18:56 +0100 Subject: [PATCH] Add desk management Test plan : 1/ perl installer/data/mysql/updatedatabase.pl (create new table desks) 2/ prove t/db_dependent/Desks.t 3/ go to admin page, click on desk management, try to create, modify and delete desks http://bugs.koha-community.org/show_bug.cgi?id=13881 --- C4/Desks.pm | 183 ++++++++++++++++ admin/desks.pl | 131 ++++++++++++ .../atomicupdate/bug_13881-add_desk_management.sql | 12 ++ installer/data/mysql/kohastructure.sql | 18 +- .../prog/en/modules/admin/admin-home.tt | 2 + .../intranet-tmpl/prog/en/modules/admin/desks.tt | 220 ++++++++++++++++++++ t/db_dependent/Desks.t | 70 +++++++ 7 files changed, 635 insertions(+), 1 deletion(-) create mode 100644 C4/Desks.pm create mode 100755 admin/desks.pl create mode 100644 installer/data/mysql/atomicupdate/bug_13881-add_desk_management.sql create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/admin/desks.tt create mode 100644 t/db_dependent/Desks.t diff --git a/C4/Desks.pm b/C4/Desks.pm new file mode 100644 index 0000000..9298b9a --- /dev/null +++ b/C4/Desks.pm @@ -0,0 +1,183 @@ +# This file is part of Koha. +# +# Copyright (C) 2011 Progilone +# Copyright (C) 2015 BULAC +# +# 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. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +package C4::Desks; + +use Modern::Perl; + +use C4::Context; +use vars qw(@ISA @EXPORT); + +BEGIN { + require Exporter; + @ISA = qw(Exporter); + @EXPORT = qw( + &AddDesk + &ModDesk + &DelDesk + &GetDesk + &GetDesks + ); +} + +=head1 NAME + +C4::Desks - Desk management functions + +=head1 DESCRIPTION + +This module contains an API for manipulating issue desks in Koha. It +is used by circulation. + +=head1 HISTORICAL NOTE + +Developped by Progilone on a customised Koha 3.2 for BULAC +library. The module is ported to Koha 3.19 and up by the BULAC. + +=cut + +=head2 AddDesk + + AddDesk({ + 'deskcode' => $deskcode, + 'deskname' => $deskname, + 'deskdescription' => $deskdescription + 'branchcode' => $branchcode }); + + returns 1 on success, undef on error. A return value greater than 1 + or just -1 means something went wrong. + +=cut + +sub AddDesk { + my ($args) = @_; + C4::Context->dbh->do + ('INSERT INTO desks ' . + '(deskcode, deskname, deskdescription, branchcode) ' . + 'VALUES (?,?,?,?)' , + undef, + $args->{'deskcode'}, + $args->{'deskname'}, + $args->{'deskdescription'}, + $args->{'branchcode'}, + ); +} + +=head2 DelDesk + + DelDesk($deskcode) + + returns 1 on success, undef on error. A return value greater than 1 + or just -1 means something went wrong. + +=cut + +sub DelDesk { + my $deskcode = shift; + C4::Context->dbh->do + ('DELETE FROM desks WHERE deskcode = ?', + undef, + $deskcode + ); +} + +=head2 GetDesk + + $desk_href = GetDesk($deskcode); + + returns undef when no desk matches $deskcode, return a href + containing desk parameters: + {'deskcode' => $deskcode, + 'deskname' => $deskname, + 'deskdescription' => $deskdescription, + 'branchcode' => $branchcode } + +=cut + +sub GetDesk { + my $deskcode = shift; + my $query = ' + SELECT * + FROM desks + WHERE deskcode = ?'; + + my $sth = C4::Context->dbh->prepare($query); + $sth->execute($deskcode); + $sth->fetchrow_hashref; +} + +=head2 GetDesks + + $desk_aref = GetDesks([$branch]) + + returns an array ref containing deskcodes. If no desks are + available, the array is empty. + +=cut + +sub GetDesks { + my $branchcode = shift || ''; + my $retaref = []; + my $query = 'SELECT deskcode FROM desks'; + if ($branchcode) { + $query = $query . ' WHERE branchcode = ?'; + } + my $sth = C4::Context->dbh->prepare($query); + if ($branchcode) { + $sth->execute($branchcode); + } else { + $sth->execute(); + } + while (my $rowaref = $sth->fetchrow_arrayref()) { + push @{ $retaref }, $rowaref->[0]; + } + return $retaref; +} + +=head2 ModDesks + + $deskhref = { + 'deskcode' => $deskcode, + 'deskname' => $deskname, + 'deskdescription' => $deskdescription, + 'branchcode' => $branchcode + } + ModDesks($deskhref) + + Modify desk with $deskcode with values contained in various + $deskhref fields. Returns 1 on success, 0 if no desk were + modified, undef, -1 or an int greater than 1 if something went + terribly wrong. + +=cut + +sub ModDesk { + my ($args) = @_; + C4::Context->dbh->do('UPDATE desks SET deskname = ? , ' . + 'deskdescription = ? , ' . + 'branchcode = ? ' . + 'WHERE deskcode = ?' , + undef, + $args->{'deskname'}, + $args->{'deskdescription'}, + $args->{'branchcode'}, + $args->{'deskcode'} + ); +} + +1; diff --git a/admin/desks.pl b/admin/desks.pl new file mode 100755 index 0000000..417b631 --- /dev/null +++ b/admin/desks.pl @@ -0,0 +1,131 @@ +#!/usr/bin/perl +# +# Copyright (C) 2011 Progilone +# Copyright (C) 2015 BULAC +# +# 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 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 + +use Modern::Perl; +use CGI; +use C4::Output; +use C4::Auth; +use C4::Context; +use C4::Koha; +use C4::Branch; +use C4::Desks; + +my $script_name = "/cgi-bin/koha/admin/desks.pl"; + +my $input = new CGI; +my $deskcode = $input->param('deskcode'); +my $branchcode = $input->param('branchcode'); +my $deskname = $input->param('deskname'); +my $deskdescription = $input->param('deskdescription'); +my $op = $input->param('op') || ''; + +my @deskloop; +my $branches = GetBranches; +my @branches = keys $branches; +my @branchloop; +foreach my $thisbranch (sort keys %$branches) { + my $selected = 1 if $thisbranch eq $branchcode; + my %row =(value => $thisbranch, + selected => $selected, + branchname => $branches->{$thisbranch}->{branchname}, + ); + push @branchloop, \%row; +} + + +my ( $template, $borrowernumber, $cookie ) = + get_template_and_user( + { + template_name => "admin/desks.tt", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => { + parameters => 'parameters_remaining_permissions' + }, + debug => 1, + } + ); + +$template->param(branchloop => \@branchloop); + +$template->param(script_name => $script_name); +if ($op) { + $template->param($op => 1); +} else { + $template->param(else => 1); +} +$template->param(deskcode => $deskcode); + +my $desk; + +if ( $op eq 'add_form' || $op eq 'delete_confirm') { + $desk = GetDesk($deskcode); +} +elsif ( $op eq 'add_validate' ) { + $desk = { + 'deskcode' => $deskcode, + 'deskname' => $deskname, + 'deskdescription' => $deskdescription, + 'branchcode' => $branchcode, + }; + if ( GetDesk($deskcode) ) { + $template->param(error => 'ALREADY_EXISTS'); + print $input->redirect('desks.pl'); + exit; + } + if (AddDesk($desk) != 1) { + $template->param(error => 'CANT_ADD'); + } + print $input->redirect('desks.pl'); + exit; +} +elsif ( $op eq 'modify_validate' ) { + $desk = { + 'deskcode' => $deskcode, + 'deskname' => $deskname, + 'deskdescription' => $deskdescription, + 'branchcode' => $branchcode, + }; + if (ModDesk($desk) != 1) { + $template->param(error => 'CANT_MODIFY'); + } + print $input->redirect('desks.pl'); + exit; +} +elsif ( $op eq 'delete_confirmed' ) { + if ( DelDesk($deskcode) != 1) { + $template->param(error => 'CANT_DELETE'); + } + print $input->redirect('desks.pl'); + exit; +} +else { + my $userenv = C4::Context->userenv; + my $desksaref = GetDesks(); + foreach my $d (@$desksaref) { + push @deskloop, GetDesk($d); + } + $template->param(deskloop => \@deskloop); +} + +$template->param(desk => $desk); + +output_html_with_http_headers $input, $cookie, $template->output; diff --git a/installer/data/mysql/atomicupdate/bug_13881-add_desk_management.sql b/installer/data/mysql/atomicupdate/bug_13881-add_desk_management.sql new file mode 100644 index 0000000..eabaec8 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_13881-add_desk_management.sql @@ -0,0 +1,12 @@ +DROP TABLE IF EXISTS `desks`; +CREATE TABLE `desks` ( + `deskcode` varchar(10) NOT NULL, -- desk id + `branchcode` varchar(10) NOT NULL, -- branch id the desk is attached to + `deskname` varchar(80) NOT NULL, -- name used for OPAC or intranet printing + `deskdescription` varchar(300) NOT NULL, -- longer description of the desk + PRIMARY KEY (`deskcode`), + UNIQUE KEY `deskcode` (`deskcode`), + KEY `fk_desks_branchcode` (`branchcode`), + KEY `fk_desks_name_branchcode` (`branchcode`,`deskname`), + CONSTRAINT `fk_desks_branchcode` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 6ddba43..5758210 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -453,7 +453,6 @@ CREATE TABLE `branchtransfers` ( -- information for items that are in transit be CONSTRAINT `branchtransfers_ibfk_3` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; - -- -- Table structure for table `browser` -- @@ -957,6 +956,23 @@ CREATE TABLE `deleteditems` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- +-- Table structure for table `desks` +-- + +DROP TABLE IF EXISTS `desks`; +CREATE TABLE `desks` ( + `deskcode` varchar(10) NOT NULL, -- desk id + `branchcode` varchar(10) NOT NULL, -- branch id the desk is attached to + `deskname` varchar(80) NOT NULL, -- name used for OPAC or intranet printing + `deskdescription` varchar(300) NOT NULL, -- longer description of the desk + PRIMARY KEY (`deskcode`), + UNIQUE KEY `deskcode` (`deskcode`), + KEY `fk_desks_branchcode` (`branchcode`), + KEY `fk_desks_name_branchcode` (`branchcode`,`deskname`), + CONSTRAINT `fk_desks_branchcode` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -- Table structure for table `ethnicity` -- diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt index 02bc60a..3f6fc12 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt @@ -32,6 +32,8 @@
Libraries and groups
Define libraries and groups.
+
Desks
+
Define Desks.
Item types
Define item types used for circulation rules.
Authorized values
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/desks.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/desks.tt new file mode 100644 index 0000000..d7b443e --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/desks.tt @@ -0,0 +1,220 @@ +[% USE AuthorisedValues %] +[% INCLUDE 'doc-head-open.inc' %] +Koha › Administration › Desks [% IF ( add_form ) %]› + [% IF ( deskcode ) %] +Modify desk '[% deskcode %]' + [% ELSE %] +Add desk + [% END %] +[% END %] +[% IF ( delete_confirm ) %]› + Delete desk '[% deskcode %]'? +[% END %] +[% IF ( delete_confirmed ) %]› +Desk deleted +[% END %] + +[% INCLUDE 'doc-head-close.inc' %] + +[% INCLUDE 'datatables.inc' %] + + + + +[% INCLUDE 'header.inc' %] +[% INCLUDE 'cat-search.inc' %] + + + +
+
+
+
+ [% IF ( error ) %]$ +
+ [% IF ( CANT_ADD ) %] +

Adding Desk [% deskcode %] failed

+ [% END %] + [% IF ( ALREADY_EXISTS ) %] +

Adding Desk [% deskcode %] failed, it already exists

+ [% END %] + [% IF ( CANT_MODIFY ) %] +

Modifying Desk [% deskcode %] failed

+ [% IF ( CANT_DELETE ) %] +

Deleting Desk [% deskcode %] failed

+ [% END %] + [% END %] +
+[% END %] +[% IF ( else ) %] + +[% END %] + + +[% IF ( add_form ) %] + [% IF ( deskcode ) %] +

Modify desk

+ [% ELSE %] +

Add desk

+ [% END %] +
+ + +
+
    + [% IF ( desk.deskcode ) %] +
  1. + Desk code: + + [% desk.deskcode %] +
  2. + [% ELSE %] +
  3. + + + Required +
  4. + [% END %] +
  5. + + + Required +
  6. +
  7. + + + Required
  8. +
  9. + + +
  10. +
+
+
+ + Cancel +
+
+
+[% END %] + + +[% IF ( delete_confirm ) %] +
+

Delete Desk '[% desk.deskcode %]'?

+
+ + + + + + + + + +
Desk[% desk.deskcode %]
Name[% desk.deskname %]
Description[% desk.deskdescription %]
Branch[% desk.branchcode %]
+ +
+
+ +
+
+[% END %] + + +[% IF ( else ) %] +

Desks administration

+ [% IF ( deskloop ) %] + + + + + + + + + [% FOREACH deskloo IN deskloop %] + + + + + + + + [% END %] +
Desk codeNameDescriptionBranchcodeAction
+ + [% deskloo.deskcode %] + + [% deskloo.deskname %][% deskloo.deskdescription %][% deskloo.branchcode %] + Edit + Delete +
+ [% ELSE %] +
There are no desk defined
+ [% END %] +
[% pagination_bar %]
+[% END %] +
+
+
+[% INCLUDE 'admin-menu.inc' %] +
+
+[% INCLUDE 'intranet-bottom.inc' %] diff --git a/t/db_dependent/Desks.t b/t/db_dependent/Desks.t new file mode 100644 index 0000000..964617d --- /dev/null +++ b/t/db_dependent/Desks.t @@ -0,0 +1,70 @@ +#!/usr/bin/perl +# +# Copyright (C) 2015 BULAC +# +# 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. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 17; + +BEGIN { + use_ok('C4::Branch'); + use_ok('C4::Desks'); +} + +my $deskcode = 'MON BUREAU'; +my $deskname = 'mon bureau'; +my $deskdescription = "Le beau bureau ici."; +my $branchcode = shift [keys GetBranches]; + +my $hrdesk = { + 'deskcode' => $deskcode, + 'deskname' => $deskname, + 'deskdescription' => $deskdescription, + 'branchcode' => $branchcode + }; + +ok(AddDesk($hrdesk) == 1, 'AddDesk creates desk.'); +ok(! defined AddDesk($hrdesk) + , 'AddDesk returns undef when desk already exists'); + +my $desk = GetDesk($deskcode); +ok (ref($desk) eq 'HASH', 'GetDesk returns hashref.'); +ok($desk->{'deskcode'} eq $deskcode, 'GetDesk returns desk code'); +ok($desk->{'deskname'} eq $deskname, 'GetDesk returns desk name'); +ok($desk->{'deskdescription'} eq $deskdescription, 'GetDesk returns desk description'); +ok($desk->{'branchcode'} eq $branchcode, 'GetDesk returns branchcode'); +ok(! defined GetDesk('this desk surely not exists'), + "GetDesk returns undef when desk doesn't exist"); + +my $modifieddesk = { + 'deskcode' => $deskcode, + 'deskname' => 'mon joli bureau', + 'deskdescription' => "Celui dans l'entrée", + 'branchcode' => $branchcode + }; +ok(ModDesk($modifieddesk) == 1, 'ModDesk modifies Desk'); +$modifieddesk->{'deskcode'} = 'this desk surely not exists'; +ok(ModDesk($modifieddesk) == 0, 'ModDesk returns 0 when deskcode is wrong'); + +my $desks = GetDesks(); +ok(ref($desks) eq 'ARRAY', 'GetDesks returns an array'); +ok($deskcode ~~ @$desks, 'GetDesks returns desk codes'); +my $emptydesksset = GetDesks("this branch sureley doesn't exist"); +ok(! @$emptydesksset , 'GetDesks returns [] when no desks are found'); + +ok(DelDesk($deskcode) == 1, 'DelDesk returns 1 when successfuly deleting desk'); +ok(DelDesk('this desk surely not exists'), + "DelDesk returns 0 when no desk were deleted"); -- 1.7.10.4