From a9dbfc6614dacf12ee8c9582b46ab4de4d7a641a Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 12 Jul 2013 16:30:05 +0200 Subject: [PATCH] Bug 10389: Share a list (part 2: accept the invitation) Content-Type: text/plain; charset=utf-8 What it does: Test plan: --- C4/VirtualShelves.pm | 52 ++++++++++++++++++++ .../opac-tmpl/prog/en/modules/opac-shareshelf.tt | 7 +-- opac/opac-shareshelf.pl | 46 +++++++++++------- 3 files changed, 83 insertions(+), 22 deletions(-) diff --git a/C4/VirtualShelves.pm b/C4/VirtualShelves.pm index f464199..a4afe9c 100644 --- a/C4/VirtualShelves.pm +++ b/C4/VirtualShelves.pm @@ -434,6 +434,7 @@ ShelfPossibleAction($loggedinuser, $shelfnumber, $action); C<$loggedinuser,$shelfnumber,$action> $action can be "view", "add", "delete", "manage", "new_public", "new_private". +New additional actions are: invite, acceptshare. Note that add/delete here refers to adding/deleting entries from the list. Deleting the list itself falls under manage. new_public and new_private refers to creating a new public or private list. The distinction between deleting your own entries from the list or entries from @@ -441,6 +442,8 @@ others is made in DelFromShelf. Returns 1 if the user can do the $action in the $shelfnumber shelf. Returns 0 otherwise. +For the actions invite and acceptshare a second errorcode is returned if the +result is false. See opac-shareshelf.pl =cut @@ -490,6 +493,28 @@ sub ShelfPossibleAction { #DelFromShelf checks the situation per biblio return 1 if $user>0 && ($shelf->{allow_delete_own}==1 || $shelf->{allow_delete_other}==1); } + elsif($action eq 'invite') { + #for sharing you must be the owner and the list must be private + if( $shelf->{category}==1 ) { + return 1 if $shelf->{owner}==$user; + return (0, 4); # code 4: should be owner + } + else { + return (0, 5); # code 5: should be private list + } + } + elsif($action eq 'acceptshare') { + #the key for accepting is checked later in AcceptShare + #you must not be the owner, list must be private + if( $shelf->{category}==1 ) { + return (0, 8) if $shelf->{owner}==$user; + #code 8: should not be owner + return 1; + } + else { + return (0, 5); # code 5: should be private list + } + } elsif($action eq 'manage') { return 1 if $user && $shelf->{owner}==$user; } @@ -667,6 +692,33 @@ sub AddShare { $dbh->do($sql, undef, ($shelfnumber, $key, SHARE_INVITATION_EXPIRY_DAYS)); } +=head2 AcceptShare + + my $result= AcceptShare($shelfnumber, $key, $borrowernumber); + +Checks acceptation of a share request. +Key must be found for this shelf. Invitation must not have expired. +Returns true when accepted, false otherwise. + +=cut + +sub AcceptShare { + my ($shelfnumber, $key, $borrowernumber)= @_; + return if !$shelfnumber || !$key || !$borrowernumber; + + my $sql; + my $dbh = C4::Context->dbh; + $sql=" +UPDATE virtualshelfshares +SET invitekey=NULL, sharedate=NULL, borrowernumber=? +WHERE shelfnumber=? AND invitekey=? AND sharedate>NOW() + "; + my $i= $dbh->do($sql, undef, ($borrowernumber, $shelfnumber, $key)); + return if !defined($i) || !$i || $i eq '0E0'; #not found + return 1; +} + + # internal subs sub _shelf_count { diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-shareshelf.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-shareshelf.tt index 9adebcc..7b73400 100644 --- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-shareshelf.tt +++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-shareshelf.tt @@ -19,6 +19,8 @@ [% IF errcode==4 %]
You can only share a list if you are the owner.
[% END %] [% IF errcode==5 %]
You cannot share a public list.
[% END %] [% IF errcode==6 %]
Sorry, but you did not enter any valid email address.
[% END %] + [% IF errcode==7 %]
Sorry, but we could not accept this key. The invitation may have expired. Contact the patron who sent you the invitation.
[% END %] + [% IF errcode==8 %]
As owner of a list you cannot accept an invitation for sharing it.
[% END %] [% ELSIF op=='invite' %]
@@ -45,10 +47,7 @@

Return to your lists

[% ELSIF op=='accept' %] - [%# TODO: Replace the following two lines %] -

Thank you for testing this feature.

-

Your signoff will certainly help in finishing the remaining part!

- + [%# Nothing to do: we already display an error or we redirect. %] [% END %] [%# End of essential part %] diff --git a/opac/opac-shareshelf.pl b/opac/opac-shareshelf.pl index 6c95f0d..66e53c2 100755 --- a/opac/opac-shareshelf.pl +++ b/opac/opac-shareshelf.pl @@ -34,7 +34,7 @@ use C4::VirtualShelves; #------------------------------------------------------------------------------- my $query= new CGI; -my ($shelfname, $owner); +my $shelfname; my ($template, $loggedinuser, $cookie); my $errcode=0; my (@addr, $fail_addr, @newkey); @@ -49,17 +49,19 @@ my $key= $query->param('key')||''; check_common_errors(); load_template("opac-shareshelf.tmpl"); +(undef, $shelfname)= GetShelf($shelfnumber); if($errcode) { #nothing to do } elsif($op eq 'invite') { - show_invite(); + check_invite(); } elsif($op eq 'conf_invite') { - confirm_invite(); + check_invite(); + confirm_invite() if !$errcode; } elsif($op eq 'accept') { - show_accept(); + do_accept(); } load_template_vars(); output_html_with_http_headers $query, $cookie, $template->output; @@ -81,12 +83,14 @@ sub check_common_errors { } } -sub show_invite { - return unless check_owner_category(); +sub check_invite { + my @rv= ShelfPossibleAction($loggedinuser, $shelfnumber, 'invite'); + $errcode= $rv[1] if !$rv[0]; + #errorcode 4: should be owner + #errorcode 5: should be private list } sub confirm_invite { - return unless check_owner_category(); process_addrlist(); if(@addr) { send_invitekey(); @@ -96,8 +100,22 @@ sub confirm_invite { } } -sub show_accept { - #TODO Add some code here to accept an invitation (followup report) +sub do_accept { + my @rv= ShelfPossibleAction($loggedinuser, $shelfnumber, 'acceptshare'); + $errcode= $rv[1] if !$rv[0]; + return if $errcode; + #errorcode 5: should be private list + #errorcode 8: should not be owner + + my $dbkey= keytostring( stringtokey($key, 0), 1); + if( C4::VirtualShelves::AcceptShare($shelfnumber, $dbkey, $loggedinuser) ) { + #redirect to view of this shared list + print $query->redirect("/cgi-bin/koha/opac-shelves.pl?display=privateshelves&viewshelf=$shelfnumber"); + exit; + } + else { + $errcode= 7; #not accepted (key not found or expired) + } } sub process_addrlist { @@ -149,14 +167,6 @@ sub send_invitekey { } } -sub check_owner_category { - #FIXME candidate for a module? what held me back is: getting back the two different error codes and the shelfname - (undef,$shelfname,$owner,my $category)= GetShelf($shelfnumber); - $errcode=4 if $owner!= $loggedinuser; #should be owner - $errcode=5 if !$errcode && $category!=1; #should be private - return $errcode==0; -} - sub load_template { my ($file)= @_; ($template, $loggedinuser, $cookie)= get_template_and_user({ @@ -208,7 +218,7 @@ sub stringtokey { for(my $i=0; $i<@temp-1; $i+=2) { push @retval, $temp[$i]*10+$temp[$i+1]; } - return @retval; + return \@retval; } sub base64ord { #base64 ordinal -- 1.7.7.6