--- loncom/interface/portfolio.pm	2006/08/14 21:25:42	1.156
+++ 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.156 2006/08/14 21:25:42 albertel Exp $
+# $Id: portfolio.pm,v 1.166 2006/10/19 21:23:22 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -191,6 +191,11 @@ sub display_directory_line {
         }
 	if ($lock_info) {
 	    my %anchor_fields = ('lockinfo' => $fullpath);
+	    if ($versions) { # hold the folder open
+	        my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($fullpath);
+	        $fname =~ s|^/||;
+	        $anchor_fields{'showversions'} = $fname.'.'.$extension;
+	    }
 	    $lock_info = &make_anchor(undef,\%anchor_fields,$lock_info);
 	}
 	$r->print('<td colspan="2">'.$lock_info.'</td>');
@@ -289,7 +294,7 @@ sub display_directory {
     	    my $version_flag;
     	    my $show_versions;
 	    my $fullpath =  &prepend_group($current_path.$filename);
-    	    if ($env{'form.showversions'} eq $filename) {
+    	    if ($env{'form.showversions'} =~ /$filename/) {
     	        $show_versions = 'true';
     	    }
     	    if (exists($versioned{$fullpath})) {
@@ -299,10 +304,15 @@ sub display_directory {
     	            'currentpath' => $current_path,
     	        );
     	        if ($show_versions) {
+    	            # Must preserve other possible showversion files
+    	            my $version_remainder = $env{'form.showversions'};
+    	            $version_remainder =~ s/$filename//g;    	            
+    	            $anchor_fields{'showversions'} = $version_remainder;
                     $version_flag = &make_anchor('portfolio',\%anchor_fields,
                         '<img class="LC_icon" alt="'.&mt('opened folder').'" src="'.$iconpath.'folder_pointer_opened.gif" />');
     	        } else {
-    	            $anchor_fields{'showversions'} = $filename;
+    	            # allow multiple files to show versioned
+    	            $anchor_fields{'showversions'} = $env{'form.showversions'}.','.$filename;
                     $version_flag = &make_anchor('portfolio',\%anchor_fields,
                         '<img class="LC_icon" alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_pointer_closed.gif" />');
                 }
@@ -358,7 +368,7 @@ sub display_directory {
                                 currentpath => $current_path
                             );
                             $line .= &make_anchor($url,\%anchor_fields,'Rename');
-                            $line .= '</td><td>'.&make_anchor($href_edit_location.$filename.'.meta',undef,$cat);
+                            $line .= '</td><td>'.&make_anchor($href_edit_location.$filename.'.meta',\%anchor_fields,$cat);
                             # '<a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>';
                         }
                         $line .= '</td>';
@@ -496,6 +506,7 @@ sub done {
         $message='Done';
     }
     my %anchor_fields = (
+        'showversions' => $env{'form.showversions'},
         'currentpath' => $env{'form.currentpath'},
         'fieldname' => $env{'form.fieldname'},
         'mode'      => $env{'form.mode'}
@@ -527,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));
@@ -597,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();
@@ -608,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();
@@ -643,7 +746,7 @@ sub display_access {
     $info .= &mt('The conditions can include affiliation with a particular course or group, or a user account in a specific domain.').'<br />';
     $info .= &mt('Alternatively access can be granted to people with specific LON-CAPA usernames and domains.').'</li></ul>';
     if ($group eq '') {
-        $info .= (&mt("Direct others to the 'Display file listing' link (shown when there are viewable files) on your personal information page:<br /><a href='/adm/$udom/$uname/aboutme'>http://$ENV{'SERVER_NAME'}/adm/$udom/$uname/aboutme</a>"));
+        $info .= (&mt("A listing of files viewable without log-in is available at: <a href='/adm/$udom/$uname/aboutme/portfolio'>http://$ENV{'SERVER_NAME'}/adm/$udom/$uname/aboutme/portfolio</a>.<br />For logged in users a 'Display file listing' link will also appear (when there are viewable files) on your personal information page:<br /><a href='/adm/$udom/$uname/aboutme'>http://$ENV{'SERVER_NAME'}/adm/$udom/$uname/aboutme</a><br />"));
     }
     
     if ($can_setacl) {
@@ -1677,13 +1780,19 @@ sub createdir {
 }
 
 sub get_portfolio_root {
-    my ($uname,$udom) = &get_name_dom();
-    my $path;
-    if (defined($env{'form.group'})) {
-        $path = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
-    } else {
-        $path = '/userfiles/portfolio';
+    my ($udom,$uname,$group) = @_;
+    if (!(defined($udom)) || !(defined($uname))) {
+        ($uname,$udom) = &get_name_dom();
     }
+    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);
 }
 
@@ -1924,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');
         }
@@ -1948,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');
         }