View | Details | Raw Unified | Return to bug 14544
Collapse All | Expand All

(-)a/C4/VirtualShelves.pm (-84 / +2 lines)
Lines 41-49 BEGIN { Link Here
41
    @ISA    = qw(Exporter);
41
    @ISA    = qw(Exporter);
42
    @EXPORT = qw(
42
    @EXPORT = qw(
43
            &GetShelves &GetShelfContents
43
            &GetShelves &GetShelfContents
44
            &AddToShelf
45
            &ShelfPossibleAction
44
            &ShelfPossibleAction
46
            &DelFromShelf
47
            &GetBibliosShelves
45
            &GetBibliosShelves
48
    );
46
    );
49
        @EXPORT_OK = qw(
47
        @EXPORT_OK = qw(
Lines 234-274 sub GetShelfContents { Link Here
234
    # or newer, for your version of DBI.
232
    # or newer, for your version of DBI.
235
}
233
}
236
234
237
=head2 AddToShelf
238
239
  &AddToShelf($biblionumber, $shelfnumber, $borrower);
240
241
Adds bib number C<$biblionumber> to virtual virtualshelves number
242
C<$shelfnumber>, unless that bib is already on that shelf.
243
244
=cut
245
246
sub AddToShelf {
247
    my ($biblionumber, $shelfnumber, $borrowernumber) = @_;
248
    return unless $biblionumber;
249
    my $dbh = C4::Context->dbh;
250
    my $query = qq(
251
        SELECT *
252
        FROM   virtualshelfcontents
253
        WHERE  shelfnumber=? AND biblionumber=?
254
    );
255
    my $sth = $dbh->prepare($query);
256
257
    $sth->execute( $shelfnumber, $biblionumber );
258
    ($sth->rows) and return; # already on shelf
259
    $query = qq(
260
        INSERT INTO virtualshelfcontents
261
            (shelfnumber, biblionumber, flags, borrowernumber)
262
        VALUES (?, ?, 0, ?));
263
    $sth = $dbh->prepare($query);
264
    $sth->execute( $shelfnumber, $biblionumber, $borrowernumber);
265
    $query = qq(UPDATE virtualshelves
266
                SET lastmodified = CURRENT_TIMESTAMP
267
                WHERE shelfnumber = ?);
268
    $sth = $dbh->prepare($query);
269
    $sth->execute( $shelfnumber );
270
}
271
272
=head2 ShelfPossibleAction
235
=head2 ShelfPossibleAction
273
236
274
ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
237
ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
Lines 280-286 New additional actions are: invite, acceptshare. Link Here
280
Note that add/delete here refers to adding/deleting entries from the list. Deleting the list itself falls under manage.
243
Note that add/delete here refers to adding/deleting entries from the list. Deleting the list itself falls under manage.
281
new_public and new_private refers to creating a new public or private list.
244
new_public and new_private refers to creating a new public or private list.
282
The distinction between deleting your own entries from the list or entries from
245
The distinction between deleting your own entries from the list or entries from
283
others is made in DelFromShelf.
246
others is made when deleting a content from the shelf.
284
247
285
Returns 1 if the user can do the $action in the $shelfnumber shelf.
248
Returns 1 if the user can do the $action in the $shelfnumber shelf.
286
Returns 0 otherwise.
249
Returns 0 otherwise.
Lines 339-345 sub ShelfPossibleAction { Link Here
339
        #this answer is just diplomatic: it says that you may be able to delete
302
        #this answer is just diplomatic: it says that you may be able to delete
340
        #some items from that shelf
303
        #some items from that shelf
341
        #it does not answer the question about a specific biblio
304
        #it does not answer the question about a specific biblio
342
        #DelFromShelf checks the situation per biblio
305
        #Koha::Virtualshelf->remove_biblios checks the situation per biblio
343
        return 1 if $user>0 && ($shelf->{allow_delete_own}==1 || $shelf->{allow_delete_other}==1);
306
        return 1 if $user>0 && ($shelf->{allow_delete_own}==1 || $shelf->{allow_delete_other}==1);
344
    }
307
    }
345
    elsif($action eq 'invite') {
308
    elsif($action eq 'invite') {
Lines 370-420 sub ShelfPossibleAction { Link Here
370
    return 0;
333
    return 0;
371
}
334
}
372
335
373
=head2 DelFromShelf
374
375
    $result= &DelFromShelf( $bibref, $shelfnumber, $user);
376
377
Removes biblionumbers in passed arrayref from shelf C<$shelfnumber>.
378
If the bib wasn't on that virtualshelves to begin with, nothing happens.
379
380
Returns 0 if no items have been deleted.
381
382
=cut
383
384
sub DelFromShelf {
385
    my ($bibref, $shelfnumber, $user) = @_;
386
    my $dbh = C4::Context->dbh;
387
    my $query = qq(SELECT allow_delete_own, allow_delete_other FROM virtualshelves WHERE shelfnumber=?);
388
    my $sth= $dbh->prepare($query);
389
    $sth->execute($shelfnumber);
390
    my ($del_own, $del_oth)= $sth->fetchrow;
391
    my $r; my $t=0;
392
393
    if($del_own) {
394
        $query = qq(DELETE FROM virtualshelfcontents
395
            WHERE shelfnumber=? AND biblionumber=? AND borrowernumber=?);
396
        $sth= $dbh->prepare($query);
397
        foreach my $biblionumber (@$bibref) {
398
            $sth->execute($shelfnumber, $biblionumber, $user);
399
            $r= $sth->rows; #Expect -1, 0 or 1 (-1 means Don't know; count as 1)
400
            $t+= ($r==-1)? 1: $r;
401
        }
402
    }
403
    if($del_oth) {
404
        #includes a check if borrowernumber is null (deleted patron)
405
        $query = qq/DELETE FROM virtualshelfcontents
406
            WHERE shelfnumber=? AND biblionumber=? AND
407
            (borrowernumber IS NULL OR borrowernumber<>?)/;
408
        $sth= $dbh->prepare($query);
409
        foreach my $biblionumber (@$bibref) {
410
            $sth->execute($shelfnumber, $biblionumber, $user);
411
            $r= $sth->rows;
412
            $t+= ($r==-1)? 1: $r;
413
        }
414
    }
415
    return $t;
416
}
417
418
=head2 GetBibliosShelves
336
=head2 GetBibliosShelves
419
337
420
This finds all the public lists that this bib record is in.
338
This finds all the public lists that this bib record is in.
(-)a/C4/VirtualShelves/Page.pm (-5 / +5 lines)
Lines 106-112 sub shelfpage { Link Here
106
                    $item = GetItem( 0, $barcode);
106
                    $item = GetItem( 0, $barcode);
107
                    if (defined $item && $item->{'itemnumber'}) {
107
                    if (defined $item && $item->{'itemnumber'}) {
108
                        $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'} );
108
                        $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'} );
109
                        AddToShelf( $biblio->{'biblionumber'}, $shelfnumber, $loggedinuser)
109
                        Koha::Virtualshelves->find( $shelfnumber )->add_biblio( $biblio->{biblionumber}, $loggedinuser )
110
                          or push @paramsloop, { duplicatebiblio => $barcode };
110
                          or push @paramsloop, { duplicatebiblio => $barcode };
111
                    }
111
                    }
112
                    else {
112
                    else {
Lines 120-136 sub shelfpage { Link Here
120
            elsif(grep { /REM-(\d+)/ } $query->param) {
120
            elsif(grep { /REM-(\d+)/ } $query->param) {
121
            #remove item(s) from shelf
121
            #remove item(s) from shelf
122
                if(ShelfPossibleAction($loggedinuser, $shelfnumber, 'delete')) {
122
                if(ShelfPossibleAction($loggedinuser, $shelfnumber, 'delete')) {
123
                #This is just a general okay; DelFromShelf checks further
124
                    my @bib;
123
                    my @bib;
125
                    foreach($query->param) {
124
                    foreach($query->param) {
126
                        /REM-(\d+)/ or next;
125
                        /REM-(\d+)/ or next;
127
                        push @bib, $1; #$1 is biblionumber
126
                        push @bib, $1; #$1 is biblionumber
128
                    }
127
                    }
129
                    my $t= DelFromShelf(\@bib, $shelfnumber, $loggedinuser);
128
                    my $shelf = Koha::Virtualshelves->find( $shelfnumber );
130
                    if($t==0) {
129
                    my $number_of_biblios_removed = $shelf->remove_biblios( { biblionumbers => \@bib, borrowernumber => $loggedinuser } );
130
                    if( $number_of_biblios_removed == 0) {
131
                        push @paramsloop, {nothingdeleted => $shelfnumber};
131
                        push @paramsloop, {nothingdeleted => $shelfnumber};
132
                    }
132
                    }
133
                    elsif($t<@bib) {
133
                    elsif( $number_of_biblios_removed < @bib ) {
134
                        push @paramsloop, {somedeleted => $shelfnumber};
134
                        push @paramsloop, {somedeleted => $shelfnumber};
135
                    }
135
                    }
136
                }
136
                }
(-)a/Koha/Virtualshelf.pm (+64 lines)
Lines 24-29 use Koha::DateUtils qw( dt_from_string ); Link Here
24
use Koha::Exceptions;
24
use Koha::Exceptions;
25
use Koha::Virtualshelfshare;
25
use Koha::Virtualshelfshare;
26
use Koha::Virtualshelfshares;
26
use Koha::Virtualshelfshares;
27
use Koha::Virtualshelfcontent;
27
28
28
use base qw(Koha::Object);
29
use base qw(Koha::Object);
29
30
Lines 100-105 sub get_shares { Link Here
100
    return $self->{_result}->virtualshelfshares;
101
    return $self->{_result}->virtualshelfshares;
101
}
102
}
102
103
104
sub get_contents {
105
    my ( $self ) = @_;
106
    return $self->{_result}->virtualshelfcontents;
107
}
108
103
sub share {
109
sub share {
104
    my ( $self, $key ) = @_;
110
    my ( $self, $key ) = @_;
105
    unless ( $key ) {
111
    unless ( $key ) {
Lines 137-142 sub remove_share { Link Here
137
    return $shelves->next->delete;
143
    return $shelves->next->delete;
138
}
144
}
139
145
146
sub add_biblio {
147
    my ( $self, $biblionumber, $borrowernumber ) = @_;
148
    return unless $biblionumber;
149
    my $already_exists = $self->get_contents->search(
150
        {
151
            biblionumber => $biblionumber,
152
        }
153
    )->count;
154
    return if $already_exists;
155
    my $content = Koha::Virtualshelfcontent->new(
156
        {
157
            shelfnumber => $self->shelfnumber,
158
            biblionumber => $biblionumber,
159
            borrowernumber => $borrowernumber,
160
        }
161
    )->store;
162
    $self->lastmodified(dt_from_string);
163
    $self->store;
164
165
    return $content;
166
}
167
168
sub remove_biblios {
169
    my ( $self, $params ) = @_;
170
    my $biblionumbers = $params->{biblionumbers} || [];
171
    my $borrowernumber = $params->{borrowernumber};
172
    return unless @$biblionumbers;
173
174
    my $number_removed = 0;
175
    for my $biblionumber ( @$biblionumbers ) {
176
        if ( $self->allow_delete_own ) {
177
            $number_removed += $self->get_contents->search(
178
                {
179
                    biblionumber => $biblionumber,
180
                    borrowernumber => $borrowernumber,
181
                }
182
            )->delete;
183
        }
184
        if ( $self->allow_delete_other ) {
185
            $number_removed += $self->get_contents->search(
186
                {
187
                    biblionumber => $biblionumber,
188
                    # FIXME
189
                    # This does not make sense, but it's has been backported from DelFromShelf.
190
                    # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
191
                    borrowernumber => {
192
                        -or => {
193
                            '!=' => $borrowernumber,
194
                            '=' => undef
195
                        }
196
                    },
197
                }
198
            )->delete;
199
        }
200
    }
201
    return $number_removed;
202
}
203
140
sub type {
204
sub type {
141
    return 'Virtualshelve';
205
    return 'Virtualshelve';
142
}
206
}
(-)a/Koha/Virtualshelfcontent.pm (+45 lines)
Line 0 Link Here
1
package Koha::Virtualshelfcontent;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Carp;
21
22
use Koha::Database;
23
use Koha::Exceptions;
24
25
use base qw(Koha::Object);
26
27
=head1 NAME
28
29
Koha::Virtualshelfcontent - Koha Virtualshelfcontent Object class
30
31
=head1 API
32
33
=head2 Class Methods
34
35
=cut
36
37
=head3 type
38
39
=cut
40
41
sub type {
42
    return 'Virtualshelfcontent';
43
}
44
45
1;
(-)a/Koha/Virtualshelfcontents.pm (+50 lines)
Line 0 Link Here
1
package Koha::Virtualshelfcontents;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Carp;
21
22
use Koha::Database;
23
24
use Koha::Virtualshelf;
25
26
use base qw(Koha::Objects);
27
28
=head1 NAME
29
30
Koha::Virtualshelfcontents - Koha Virtualshelfcontents Object class
31
32
=head1 API
33
34
=head2 Class Methods
35
36
=cut
37
38
=head3 type
39
40
=cut
41
42
sub type {
43
    return 'Virtualshelfcontent';
44
}
45
46
sub object_class {
47
    return 'Koha::Virtualshelfcontent';
48
}
49
50
1;
(-)a/opac/opac-addbybiblionumber.pl (-1 / +2 lines)
Lines 81-87 sub AddBibliosToShelf { Link Here
81
        @biblionumber = (split /\//,$biblionumber[0]);
81
        @biblionumber = (split /\//,$biblionumber[0]);
82
    }
82
    }
83
    for my $bib (@biblionumber) {
83
    for my $bib (@biblionumber) {
84
        AddToShelf($bib, $shelfnumber, $loggedinuser);
84
        my $shelf = Koha::Virtualshelves->find( $shelfnumber );
85
        $shelf->add_biblio( $bib, $loggedinuser );
85
    }
86
    }
86
}
87
}
87
88
(-)a/t/db_dependent/Utils/Datatables_Virtualshelves.t (-18 / +18 lines)
Lines 108-118 my $biblionumber2 = _add_biblio('title 2'); Link Here
108
my $biblionumber3 = _add_biblio('title 3');
108
my $biblionumber3 = _add_biblio('title 3');
109
my $biblionumber4 = _add_biblio('title 4');
109
my $biblionumber4 = _add_biblio('title 4');
110
my $biblionumber5 = _add_biblio('title 5');
110
my $biblionumber5 = _add_biblio('title 5');
111
C4::VirtualShelves::AddToShelf( $biblionumber1, $shelf2->shelfnumber, $john_doe{borrowernumber} );
111
$shelf2->add_biblio( $biblionumber1, $john_doe{borrowernumber} );
112
C4::VirtualShelves::AddToShelf( $biblionumber2, $shelf2->shelfnumber, $john_doe{borrowernumber} );
112
$shelf2->add_biblio( $biblionumber2, $john_doe{borrowernumber} );
113
C4::VirtualShelves::AddToShelf( $biblionumber3, $shelf2->shelfnumber, $john_doe{borrowernumber} );
113
$shelf2->add_biblio( $biblionumber3, $john_doe{borrowernumber} );
114
C4::VirtualShelves::AddToShelf( $biblionumber4, $shelf2->shelfnumber, $john_doe{borrowernumber} );
114
$shelf2->add_biblio( $biblionumber4, $john_doe{borrowernumber} );
115
C4::VirtualShelves::AddToShelf( $biblionumber5, $shelf2->shelfnumber, $john_doe{borrowernumber} );
115
$shelf2->add_biblio( $biblionumber5, $john_doe{borrowernumber} );
116
116
117
my $shelf3 = Koha::Virtualshelf->new(
117
my $shelf3 = Koha::Virtualshelf->new(
118
    {
118
    {
Lines 125-133 my $shelf3 = Koha::Virtualshelf->new( Link Here
125
my $biblionumber6 = _add_biblio('title 6');
125
my $biblionumber6 = _add_biblio('title 6');
126
my $biblionumber7 = _add_biblio('title 7');
126
my $biblionumber7 = _add_biblio('title 7');
127
my $biblionumber8 = _add_biblio('title 8');
127
my $biblionumber8 = _add_biblio('title 8');
128
C4::VirtualShelves::AddToShelf( $biblionumber6, $shelf3->shelfnumber, $jane_doe{borrowernumber} );
128
$shelf3->add_biblio( $biblionumber6, $jane_doe{borrowernumber} );
129
C4::VirtualShelves::AddToShelf( $biblionumber7, $shelf3->shelfnumber, $jane_doe{borrowernumber} );
129
$shelf3->add_biblio( $biblionumber7, $jane_doe{borrowernumber} );
130
C4::VirtualShelves::AddToShelf( $biblionumber8, $shelf3->shelfnumber, $jane_doe{borrowernumber} );
130
$shelf3->add_biblio( $biblionumber8, $jane_doe{borrowernumber} );
131
131
132
my $shelf4 = Koha::Virtualshelf->new(
132
my $shelf4 = Koha::Virtualshelf->new(
133
    {
133
    {
Lines 141-150 my $biblionumber9 = _add_biblio('title 9'); Link Here
141
my $biblionumber10 = _add_biblio('title 10');
141
my $biblionumber10 = _add_biblio('title 10');
142
my $biblionumber11 = _add_biblio('title 11');
142
my $biblionumber11 = _add_biblio('title 11');
143
my $biblionumber12 = _add_biblio('title 12');
143
my $biblionumber12 = _add_biblio('title 12');
144
C4::VirtualShelves::AddToShelf( $biblionumber9,  $shelf4->shelfnumber, $jane_doe{borrowernumber} );
144
$shelf3->add_biblio( $biblionumber9, $jane_doe{borrowernumber} );
145
C4::VirtualShelves::AddToShelf( $biblionumber10, $shelf4->shelfnumber, $jane_doe{borrowernumber} );
145
$shelf3->add_biblio( $biblionumber10, $jane_doe{borrowernumber} );
146
C4::VirtualShelves::AddToShelf( $biblionumber11, $shelf4->shelfnumber, $jane_doe{borrowernumber} );
146
$shelf3->add_biblio( $biblionumber11, $jane_doe{borrowernumber} );
147
C4::VirtualShelves::AddToShelf( $biblionumber12, $shelf4->shelfnumber, $jane_doe{borrowernumber} );
147
$shelf3->add_biblio( $biblionumber12, $jane_doe{borrowernumber} );
148
148
149
my $shelf5 = Koha::Virtualshelf->new(
149
my $shelf5 = Koha::Virtualshelf->new(
150
    {
150
    {
Lines 160-171 my $biblionumber15 = _add_biblio('title 15'); Link Here
160
my $biblionumber16 = _add_biblio('title 16');
160
my $biblionumber16 = _add_biblio('title 16');
161
my $biblionumber17 = _add_biblio('title 17');
161
my $biblionumber17 = _add_biblio('title 17');
162
my $biblionumber18 = _add_biblio('title 18');
162
my $biblionumber18 = _add_biblio('title 18');
163
C4::VirtualShelves::AddToShelf( $biblionumber13, $shelf5->shelfnumber, $jane_doe{borrowernumber} );
163
$shelf5->add_biblio( $biblionumber13, $jane_doe{borrowernumber} );
164
C4::VirtualShelves::AddToShelf( $biblionumber14, $shelf5->shelfnumber, $jane_doe{borrowernumber} );
164
$shelf5->add_biblio( $biblionumber14, $jane_doe{borrowernumber} );
165
C4::VirtualShelves::AddToShelf( $biblionumber15, $shelf5->shelfnumber, $jane_doe{borrowernumber} );
165
$shelf5->add_biblio( $biblionumber15, $jane_doe{borrowernumber} );
166
C4::VirtualShelves::AddToShelf( $biblionumber16, $shelf5->shelfnumber, $jane_doe{borrowernumber} );
166
$shelf5->add_biblio( $biblionumber16, $jane_doe{borrowernumber} );
167
C4::VirtualShelves::AddToShelf( $biblionumber17, $shelf5->shelfnumber, $jane_doe{borrowernumber} );
167
$shelf5->add_biblio( $biblionumber17, $jane_doe{borrowernumber} );
168
C4::VirtualShelves::AddToShelf( $biblionumber18, $shelf5->shelfnumber, $jane_doe{borrowernumber} );
168
$shelf5->add_biblio( $biblionumber18, $jane_doe{borrowernumber} );
169
169
170
for my $i ( 6 .. 15 ) {
170
for my $i ( 6 .. 15 ) {
171
    Koha::Virtualshelf->new(
171
    Koha::Virtualshelf->new(
(-)a/t/db_dependent/VirtualShelves.t (-135 lines)
Lines 1-135 Link Here
1
#!/usr/bin/perl
2
3
# This file is a test script for C4::VirtualShelves.pm
4
# Author : Antoine Farnault, antoine@koha-fr.org
5
# Larger modifications by Jonathan Druart and Marcel de Rooy
6
7
use Modern::Perl;
8
use Test::More tests => 56;
9
use MARC::Record;
10
11
use C4::Biblio qw( AddBiblio DelBiblio );
12
use C4::Context;
13
use C4::Members qw( AddMember );
14
15
use Koha::Virtualshelves;
16
17
18
my $dbh = C4::Context->dbh;
19
$dbh->{RaiseError} = 1;
20
$dbh->{AutoCommit} = 0;
21
22
# Create some borrowers
23
my @borrowernumbers;
24
for my $i ( 1 .. 10 ) {
25
    my $borrowernumber = AddMember(
26
        firstname =>  'my firstname',
27
        surname => 'my surname ' . $i,
28
        categorycode => 'S',
29
        branchcode => 'CPL',
30
    );
31
    push @borrowernumbers, $borrowernumber;
32
}
33
34
# Creating some biblios
35
my @biblionumbers;
36
foreach(0..9) {
37
    my ($biblionumber)= AddBiblio(MARC::Record->new, '');
38
        #item number ignored
39
    push @biblionumbers, $biblionumber;
40
}
41
42
#----------------------------------------------------------------------#
43
#
44
#           TESTS START HERE
45
#
46
#----------------------------------------------------------------------#
47
48
use_ok('C4::VirtualShelves');
49
50
#-----------------------TEST Virtualshelf constructor------------------------#
51
52
# creating shelves (could be <10 when names are not unique)
53
my @shelves;
54
for my $i (0..9){
55
    my $name= randomname();
56
    my $catg= int(rand(2))+1;
57
    my $shelf = eval {
58
        Koha::Virtualshelf->new(
59
            {
60
                shelfname => $name,
61
                category  => $catg,
62
                owner     =>$borrowernumbers[$i],
63
            }
64
        )->store;
65
    };
66
    if ( $@ or not $shelf ) {
67
        my $valid_name = Koha::Virtualshelf->new(
68
            {
69
                shelfname => $name,
70
                category  => $catg,
71
                owner     =>$borrowernumbers[$i],
72
            }
73
        )->is_shelfname_valid;
74
        is( $valid_name, 0, 'If the insert has failed, it should be caused by an invalid shelfname (or maybe not?)' );
75
    } else {
76
        ok($shelf->shelfnumber > -1, "The shelf $i should have been inserted");
77
    }
78
    push @shelves, {
79
        number => $shelf->shelfnumber,
80
        name   => $shelf->shelfname,
81
        catg   => $shelf->category,
82
        owner  => $borrowernumbers[$i],
83
    };
84
}
85
86
#-----------TEST AddToShelf & GetShelfContents &  DelFromShelf functions--------------#
87
# usage : &AddToShelf($biblionumber, $shelfnumber);
88
# usage : $biblist = &GetShelfContents($shelfnumber);
89
# usage : $biblist = GetShelfContents($shelfnumber);
90
91
my %used = ();
92
for my $i (0..9){
93
    my $bib = $biblionumbers[int(rand(9))];
94
    my $shelfnumber = $shelves[int(rand(9))]->{number};
95
    if($shelfnumber<0) {
96
        ok(1, 'skip add to list-test for shelf -1');
97
        ok(1, 'skip counting list entries for shelf -1');
98
        next;
99
    }
100
101
    my $key = "$bib\t$shelfnumber";
102
    my $should_fail = exists($used{$key}) ? 1 : 0;
103
    #FIXME We assume here that we have permission to add..
104
    #The different permissions could be tested too.
105
106
    my ($biblistBefore,$countbefore) = GetShelfContents($shelfnumber);
107
    my $status = AddToShelf($bib,$shelfnumber,$borrowernumbers[$i]);
108
    my ($biblistAfter,$countafter) = GetShelfContents($shelfnumber);
109
110
    if ($should_fail) {
111
        ok(!defined($status), 'failed to add to list when we should');
112
    } else {
113
        ok(defined($status), 'added to list when we should');
114
    }
115
116
    if (defined $status) {
117
        is($countbefore, $countafter - 1, 'added bib to list');  # the bib has been successfully added.
118
    } else {
119
        is($countbefore, $countafter, 'did not add duplicate bib to list');
120
    }
121
122
    $used{$key}++;
123
}
124
125
#----------------------- SOME SUBS --------------------------------------------#
126
127
sub randomname {
128
    my $rv='';
129
    for(0..19) {
130
        $rv.= ('a'..'z','A'..'Z',0..9) [int(rand()*62)];
131
    }
132
    return $rv;
133
}
134
135
$dbh->rollback;
(-)a/t/db_dependent/Virtualshelves.t (-2 / +90 lines)
Lines 1-20 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use Test::More tests => 2;
4
use Test::More tests => 3;
5
use DateTime::Duration;
5
6
6
use C4::Context;
7
use C4::Context;
7
use Koha::DateUtils;
8
use Koha::DateUtils;
8
use Koha::Virtualshelves;
9
use Koha::Virtualshelves;
9
use Koha::Virtualshelfshares;
10
use Koha::Virtualshelfshares;
11
use Koha::Virtualshelfcontents;
10
12
11
use t::lib::TestBuilder;
13
use t::lib::TestBuilder;
12
14
13
my $dbh = C4::Context->dbh;
15
my $dbh = C4::Context->dbh;
14
$dbh->{AutoCommit} = 0;
16
$dbh->{AutoCommit} = 0;
15
17
16
$dbh->do(q|DELETE FROM virtualshelves|);
17
$dbh->do(q|DELETE FROM virtualshelfshares|);
18
$dbh->do(q|DELETE FROM virtualshelfshares|);
19
$dbh->do(q|DELETE FROM virtualshelfcontents|);
20
$dbh->do(q|DELETE FROM virtualshelves|);
18
21
19
my $builder = t::lib::TestBuilder->new;
22
my $builder = t::lib::TestBuilder->new;
20
23
Lines 147-149 subtest 'Sharing' => sub { Link Here
147
    $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
150
    $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
148
    is( $number_of_shelves_shared, 1, 'To be sure the share has been removed' );
151
    is( $number_of_shelves_shared, 1, 'To be sure the share has been removed' );
149
};
152
};
153
154
subtest 'Shelf content' => sub {
155
156
    plan tests => 18;
157
    my $patron1 = $builder->build( { source => 'Borrower', } );
158
    my $patron2 = $builder->build( { source => 'Borrower', } );
159
    my $biblio1 = $builder->build( { source => 'Biblio', } );
160
    my $biblio2 = $builder->build( { source => 'Biblio', } );
161
    my $biblio3 = $builder->build( { source => 'Biblio', } );
162
    my $biblio4 = $builder->build( { source => 'Biblio', } );
163
    my $number_of_contents = Koha::Virtualshelfcontents->search->count;
164
165
    is( $number_of_contents, 0, 'No content should exist' );
166
167
    my $dt_yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) );
168
    my $shelf = Koha::Virtualshelf->new(
169
        {   shelfname    => "my first shelf",
170
            owner        => $patron1->{borrowernumber},
171
            category     => 1,
172
            lastmodified => $dt_yesterday,
173
        }
174
    )->store;
175
176
    $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
177
    is( output_pref( dt_from_string $shelf->lastmodified ), output_pref($dt_yesterday), 'The lastmodified has been set to yesterday, will be useful for another test later' );
178
    my $content1 = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
179
    is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' );
180
    $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
181
    is( output_pref( dt_from_string( $shelf->lastmodified ) ), output_pref(dt_from_string), 'Adding a biblio to a shelf should update the lastmodified for the shelf' );
182
    my $content2 = $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
183
    $number_of_contents = Koha::Virtualshelfcontents->search->count;
184
    is( $number_of_contents, 2, '2 biblio should have been inserted' );
185
186
    my $content1_bis = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
187
    is( $content1_bis, undef, 'add_biblio should return undef on duplicate' );    # Or an exception ?
188
    $number_of_contents = Koha::Virtualshelfcontents->search->count;
189
    is( $number_of_contents, 2, 'The biblio should not have been duplicated' );
190
191
    $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
192
    my $contents = $shelf->get_contents;
193
    is( $contents->count, 2, 'There are 2 biblios on this shelf' );
194
195
    # Patron 2 will try to remove a content
196
    # allow_add = 0, allow_delete_own = 1, allow_delete_other = 0 => Default values
197
    my $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
198
    is( $number_of_deleted_biblios, 0, );
199
    $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
200
    is( $number_of_deleted_biblios, 1, );
201
202
    $number_of_contents = Koha::Virtualshelfcontents->search->count;
203
    is( $number_of_contents, 1, 'To be sure the content has been deleted' );
204
205
    # allow_delete_own = 0
206
    $shelf->allow_delete_own(0);
207
    $shelf->store;
208
    $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
209
    is( $number_of_deleted_biblios, 0, );
210
    $number_of_contents = Koha::Virtualshelfcontents->search->count;
211
    is( $number_of_contents, 1, 'The biblio should not have been deleted to the shelf by the patron, even if it is his own content (allow_delete_own=0)' );
212
213
    # allow_add = 1, allow_delete_own = 1
214
    $shelf->allow_add(1);
215
    $shelf->allow_delete_own(1);
216
    $shelf->store;
217
218
    my $content3 = $shelf->add_biblio( $biblio3->{biblionumber}, $patron2->{borrowernumber} );
219
    my $content4 = $shelf->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
220
221
    $number_of_contents = Koha::Virtualshelfcontents->search->count;
222
    is( $number_of_contents, 3, 'The biblio should have been added to the shelf by the patron 2' );
223
224
    $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio3->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
225
    is( $number_of_deleted_biblios, 1, );
226
    $number_of_contents = Koha::Virtualshelfcontents->search->count;
227
    is( $number_of_contents, 2, 'The biblio should have been deleted to the shelf by the patron, it is his own content (allow_delete_own=1) ' );
228
229
    # allow_add = 1, allow_delete_own = 1, allow_delete_other = 1
230
    $shelf->allow_delete_other(1);
231
    $shelf->store;
232
233
    $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
234
    is( $number_of_deleted_biblios, 1, );
235
    $number_of_contents = Koha::Virtualshelfcontents->search->count;
236
    is( $number_of_contents, 1, 'The biblio should have been deleted to the shelf by the patron 2, even if it is not his own content (allow_delete_other=1)' );
237
};
(-)a/virtualshelves/addbybiblionumber.pl (-2 / +1 lines)
Lines 127-133 sub HandleBiblioPars { Link Here
127
sub AddBibliosToShelf {
127
sub AddBibliosToShelf {
128
    my ($shelfnumber, @biblionumber)=@_;
128
    my ($shelfnumber, @biblionumber)=@_;
129
    for my $bib (@biblionumber){
129
    for my $bib (@biblionumber){
130
        AddToShelf($bib, $shelfnumber, $loggedinuser);
130
        Koha::Virtualshelves->find( $shelfnumber )->add_biblio( $bib, $loggedinuser );
131
    }
131
    }
132
}
132
}
133
133
134
- 

Return to bug 14544