--- loncom/interface/portfolio.pm	2006/08/24 18:28:43	1.162
+++ loncom/interface/portfolio.pm	2006/10/19 21:23:22	1.166
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # portfolio browser
 #
-# $Id: portfolio.pm,v 1.162 2006/08/24 18:28:43 banghart Exp $
+# $Id: portfolio.pm,v 1.166 2006/10/19 21:23:22 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -538,18 +538,55 @@ sub delete {
 } 
 
 sub delete_confirmed {
-    my ($r,$url)=@_;
+    my ($r,$url,$group)=@_;
     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
     my $result;
     my ($uname,$udom) = &get_name_dom();
     my $port_path = &get_port_path();
+    my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
+                                                                        $uname);
     foreach my $delete_file (@files) {
         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
 					       $env{'form.currentpath'}.
 					       $delete_file);
         if ($result ne 'ok') {
-	$r->print('<span class="LC_error"> An error occured ('.$result.
-		  ') while trying to delete '.&display_file(undef, $delete_file).'</span><br />');
+	    $r->print('<span class="LC_error">'.
+                     &mt('An error occured ([_1]) while trying to delete 
+                         [_2].',$result,&display_file(undef, $delete_file)).
+                      '</span><br /><br />');
+        } else {
+            $r->print(&mt('File: [_1] deleted.',
+                          &display_file(undef,$delete_file)));
+            my $file_name = $env{'form.currentpath'}.$delete_file;
+            $file_name = &prepend_group($file_name);
+            my %access_controls = 
+                    &Apache::lonnet::get_access_controls($current_permissions,
+                                                         $group,$file_name);
+            if (keys(%access_controls) > 0) {
+                my %changes; 
+                foreach my $key (keys(%{$access_controls{$file_name}})) {
+                    $changes{'delete'}{$key} = 1;
+                }
+                if (keys(%changes) > 0) {
+                    my ($outcome,$deloutcome,$new_values,$translation) =
+                    &Apache::lonnet::modify_access_controls($file_name,\%changes,
+                                                            $udom,$uname);
+                    if ($outcome ne 'ok') {
+                           $r->print('<br />'.&mt("An error occured ([_1]) while ".
+                               "trying to delete access controls for the file.",$outcome).
+                               '</span><br /><br />');
+                    } else {
+                        if ($deloutcome eq 'ok') {
+                            $r->print('<br />'.&mt('Access controls also deleted for the file.').'<br /><br />');
+                        } else {
+                            $r->print('<span class="LC_error">'.'<br />'.
+                               &mt("An error occured ([_1]) while ".
+                                   "trying to delete access controls for the file.",$deloutcome).
+                                   '</span><br /><br />');
+                        }
+                    }
+                }
+            }
         }
     }
     $r->print(&done(undef,$url));
@@ -608,7 +645,7 @@ sub rename {
 }
 
 sub rename_confirmed {
-    my ($r,$url)=@_;
+    my ($r,$url,$group)=@_;
     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
     my ($uname,$udom) = &get_name_dom();
     my $port_path = &get_port_path();
@@ -619,24 +656,79 @@ sub rename_confirmed {
 	$r->print(&done(undef,$url));
 	return;
     } 
+    my $chg_access;
     my $result=
 	&Apache::lonnet::renameuserfile($uname,$udom,
             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
             $port_path.$env{'form.currentpath'}.$filenewname);
-    if ($result ne 'ok') {
+    if ($result eq 'ok') {
+        $chg_access = &access_for_renamed($filenewname,$group,$udom,$uname);
+    } else {      
 	$r->print('<span class="LC_error">'.
-		  &mt('An errror occured ([_1]) while trying to rename [_2]'
+		  &mt('An error occured ([_1]) while trying to rename [_2]'
 		      .' to [_3]',$result,&display_file(),
 		      &display_file('',$filenewname)).'</span><br />');
+        return;
     }
     if ($filenewname ne $env{'form.filenewname'}) {
         $r->print(&mt("The new file name was changed from:<br />[_1] to [_2]",
 		      '<strong>'.&display_file('',$env{'form.filenewname'}).'</strong>',
 		      '<strong>'.&display_file('',$filenewname).'</strong>'));
     }
+    $r->print($chg_access);
     $r->print(&done(undef,$url));
 }
 
+sub access_for_renamed {
+    my ($filenewname,$group,$udom,$uname) = @_;
+    my $oldfile = $env{'form.currentpath'}.$env{'form.selectfile'};
+    $oldfile = &prepend_group($oldfile);
+    my $newfile = $env{'form.currentpath'}.$filenewname;
+    $newfile = &prepend_group($newfile);
+    my $current_permissions =
+	&Apache::lonnet::get_portfile_permissions($udom,$uname);
+    my %access_controls =
+	&Apache::lonnet::get_access_controls($current_permissions,
+					     $group,$oldfile);
+    my $chg_text;
+    if (keys(%access_controls) > 0) {
+        my %change_old;
+        my %change_new;
+        foreach my $key (keys(%{$access_controls{$oldfile}})) {
+            $change_old{'delete'}{$key} = 1;
+            $change_new{'activate'}{$key} = $access_controls{$oldfile}{$key};
+        }
+        my ($outcome,$deloutcome,$new_values,$translation) =
+            &Apache::lonnet::modify_access_controls($oldfile,\%change_old,
+						    $udom,$uname);
+        if ($outcome ne 'ok') {
+            $chg_text ='<br /><br />'.&mt("An error occured ([_1]) while ".
+                "trying to delete access control records for the old name.",$outcome).
+                '</span><br />';
+        } else {
+            if ($deloutcome ne 'ok') {
+                $chg_text = '<br /><br /><span class="LC_error"><br />'.
+		    &mt("An error occured ([_1]) while ".
+			"trying to delete access control records for the old name.",$deloutcome).
+			'</span><br />';
+            }
+        }
+        ($outcome,$deloutcome,$new_values,$translation) =
+            &Apache::lonnet::modify_access_controls($newfile,\%change_new,
+                                                    $udom,$uname);
+        if ($outcome ne 'ok') {
+            $chg_text .= '<br /><br />'.
+		&mt("An error occured ([_1]) while ".
+                "trying to update access control records for the new name.",$outcome).
+                '</span><br />';
+        }
+        if ($chg_text eq '') {
+            $chg_text = '<br /><br />'.&mt('Access controls updated to reflect the name change.');
+        }
+    }
+    return $chg_text;
+}
+
 sub display_access {
     my ($r,$url,$group,$can_setacl,$port_path) = @_;
     my ($uname,$udom) = &get_name_dom();
@@ -1688,16 +1780,19 @@ sub createdir {
 }
 
 sub get_portfolio_root {
-    my ($udom,$uname) = @_;
+    my ($udom,$uname,$group) = @_;
     if (!(defined($udom)) || !(defined($uname))) {
         ($uname,$udom) = &get_name_dom();
     }
-    my $path;
-    if (defined($env{'form.group'})) {
-        $path = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
-    } else {
-        $path = '/userfiles/portfolio';
+    my $path = '/userfiles/portfolio';
+    if (!defined($group)) { 
+        if (defined($env{'form.group'})) {
+            $group = $env{'form.group'};      
+        }
     }
+    if (defined($group)) {
+        $path = '/userfiles/groups/'.$group.'/portfolio';
+    } 
     return (&Apache::loncommon::propath($udom,$uname).$path);
 }
 
@@ -1938,7 +2033,7 @@ sub handler {
         }
     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
         if ($can_delete) {
-	    &delete_confirmed($r,$url);
+	    &delete_confirmed($r,$url,$group);
         } else {
             &missing_priv($r,$url,'delete');
         }
@@ -1962,7 +2057,7 @@ sub handler {
         }
     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
         if ($can_modify) {
-	    &rename_confirmed($r,$url);
+	    &rename_confirmed($r,$url,$group);
         } else {
             &missing_priv($r,$url,'rename');
         }