File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.148: download - view: text, annotated - select for diffs
Sun Aug 6 17:23:56 2006 UTC (17 years, 10 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Make show versions work in subdirectories.

    1: # The LearningOnline Network
    2: # portfolio browser
    3: #
    4: # $Id: portfolio.pm,v 1.148 2006/08/06 17:23:56 banghart Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or 
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::portfolio;
   30: use strict;
   31: use Apache::Constants qw(:common :http);
   32: use Apache::loncommon;
   33: use Apache::lonnet;
   34: use Apache::lontexconvert;
   35: use Apache::lonfeedback;
   36: use Apache::lonlocal;
   37: use Apache::lonnet;
   38: use Apache::longroup;
   39: use Apache::lonhtmlcommon;
   40: use HTML::Entities;
   41: use LONCAPA;
   42: 
   43: sub group_args {
   44:     my $output;
   45:     if (defined($env{'form.group'})) {
   46:         $output .= '&group='.$env{'form.group'};
   47: 	if (defined($env{'form.ref'})) {
   48: 	    $output .= '&ref='.$env{'form.ref'};
   49: 	}
   50:     }
   51:     return $output;
   52: }
   53: 
   54: sub group_form_data {
   55:     my $output;
   56:     if (defined($env{'form.group'})) {
   57: 	$output = '<input type="hidden" name="group" value="'.$env{'form.group'}.'" />';
   58: 	if (exists($env{'form.ref'})) {
   59: 	    $output .= '<input type="hidden" name="ref" value="'.
   60: 		$env{'form.ref'}.'" />';
   61: 	}
   62:     }
   63:     return $output;
   64: } 
   65: 
   66: # receives a file name and path stub from username/userfiles/portfolio/
   67: # returns an anchor tag consisting encoding filename and currentpath
   68: sub make_anchor {
   69:     my ($url, $filename, $current_path, $current_mode, $field_name,
   70:         $continue_select) = @_;
   71:     if ($continue_select ne 'true') {$continue_select = 'false'};
   72:     my $anchor = '<a href="'.$url.'?selectfile='.$filename.'&amp;currentpath='.$current_path.'&amp;mode='.$current_mode.'&amp;continue='.$continue_select.'&amp;fieldname='.$field_name;
   73:     $anchor .= &group_args();
   74:     $anchor .= '">'.$filename.'</a>';
   75:     return $anchor;
   76: }
   77: 
   78: my $dirptr=16384;
   79: sub display_common {
   80:     my ($r,$url,$current_path,$is_empty,$dir_list,$can_upload)=@_;
   81:     my $namespace = &get_namespace();
   82:     my $port_path = &get_port_path();
   83:     if ($can_upload) {
   84:         my $groupitem = &group_form_data();
   85: 
   86:         my $iconpath= $r->dir_config('lonIconsURL') . "/";
   87:         my %text=&Apache::lonlocal::texthash(
   88: 					 'upload' => 'Upload',
   89: 					 'upload_label' =>  
   90: 					 'Upload file to current directory:',
   91: 					 'createdir' => 'Create Subdirectory',
   92: 					 'createdir_label' => 
   93: 					 'Create subdirectory in current directory:');
   94:         my $escuri = &HTML::Entities::encode($r->uri,'&<>"');
   95:         $r->print(<<"TABLE"); 
   96: <table id="LC_portfolio_actions">
   97:   <tr id="LC_portfolio_upload">
   98:     <td class="LC_label">
   99:       $text{'upload_label'}
  100:     </td>
  101:     <td class="LC_value">
  102:       <form method="post" enctype="multipart/form-data" action="$escuri">
  103:         $groupitem 
  104:         <input name="uploaddoc" type="file" />
  105: 	<input type="hidden" name="currentpath" value="$current_path" />
  106: 	<input type="hidden" name="action" value="$env{"form.action"}" />
  107: 	<input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
  108: 	<input type="hidden" name="mode" value="$env{"form.mode"}" />
  109: 	<input type="submit" name="storeupl" value="$text{'upload'}" />
  110:       </form>
  111:     </td>
  112:   </tr>
  113:   <tr id="LC_portfolio_createdir">
  114:     <td class="LC_label">
  115:       $text{'createdir_label'}
  116:     </td>
  117:     <td class="LC_value">
  118:       <form method="post" action="$escuri">
  119:         <input name="newdir" type="input" />$groupitem
  120:         <input type="hidden" name="currentpath" value="$current_path" />
  121:         <input type="hidden" name="action" value="$env{"form.action"}" />
  122:         <input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
  123:         <input type="hidden" name="mode" value="$env{"form.mode"}" />
  124:         <input type="submit" name="createdir" value="$text{'createdir'}" />
  125:       </form>
  126:     </td>
  127:   </tr>
  128: </table>
  129: TABLE
  130:     }
  131:     my @tree = split (/\//,$current_path);
  132:     $r->print('<span class="LC_current_location">'.&make_anchor($url,$port_path,'/',$env{"form.mode"},$env{"form.fieldname"},$env{"form.continue"}).'/');
  133:     if (@tree > 1){
  134:         my $newCurrentPath = '';
  135:         for (my $i = 1; $i< @tree; $i++){
  136:             $newCurrentPath .= $tree[$i].'/';
  137:             $r->print(&make_anchor($url,$tree[$i],'/'.$newCurrentPath, $env{"form.mode"},$env{"form.fieldname"}, $env{"form.continue"}).'/');
  138:         }
  139:     }
  140:     $r->print('</span>');
  141:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
  142:     $r->print('<br /><form method="post" action="'.$url.'?mode='.$env{"form.mode"}.'&amp;fieldname='.$env{"form.fieldname"}.&group_args());
  143:     $r->print('">'.
  144: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
  145: 						    'this.form.submit();'));
  146:     $r->print("</form>");
  147: }
  148: 
  149: sub display_directory_line {
  150:     my ($r,$select_mode,$fullpath, $filename, $mtime, $size, $css_class,
  151: 	$line, $access_controls, $curr_access, $now, $version_flag,
  152: 	$href_location, $url, $current_path, $access_admin_text, $versions)=@_;
  153:     # my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
  154:     $r->print('<tr class="'.$css_class.'">');
  155:     $r->print($line);
  156:     $r->print('<td><img alt="" src="'.&Apache::loncommon::icon($filename).'" /></td>');
  157:     $r->print('<td>'.$$version_flag{$filename}.'</td><td><a href="'.$href_location.$filename.'">'.
  158: 	      $filename.'</a></td>'); 
  159:     $r->print('<td>'.$size.'</td>');
  160:     $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
  161:     if ($select_mode ne 'true') {
  162: 	$r->print('<td><span style="white-space: nowrap">'.
  163: 		  &mt($curr_access).'&nbsp;&nbsp;&nbsp;');
  164: 	$r->print('<a href="'.$url.'?access='.$filename.
  165: 		  '&amp;currentpath='.$current_path.&group_args().
  166: 		  '">'.$access_admin_text.'</a></span></td>');
  167:     }
  168:     $r->print('</tr>'.$/);
  169: }
  170: 
  171: sub display_directory {
  172:     my ($r,$url,$current_path,$is_empty,$dir_list,$group,$can_upload,
  173:         $can_modify,$can_delete,$can_setacl)=@_;
  174:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
  175:     my $display_out;
  176:     my $select_mode;
  177:     my $checked_files;
  178:     my $port_path = &get_port_path();
  179:     my ($uname,$udom) = &get_name_dom();
  180:     my $access_admin_text = &mt('View Status');
  181:     if ($can_setacl) {
  182:         $access_admin_text = &mt('View/Change Status');
  183:     }
  184: 
  185:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  186:                                                                         $uname);
  187:     my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash(
  188:                                                   $current_permissions,$group);
  189:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group);
  190:     my $now = time;
  191:     if ($env{"form.mode"} eq 'selectfile'){
  192: 	&select_files($r);
  193: 	$checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
  194: 	$select_mode = 'true';
  195:     } 
  196:     if ($is_empty && ($current_path ne '/') && $can_delete) {
  197:         $display_out = '<form method="post" action="'.$url.'">'.
  198: 	    &group_form_data().
  199:         '<input type="hidden" name="action" value="deletedir" />'.
  200:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
  201:         '<input type="hidden" name="selectfile" value="" />'.
  202:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
  203:         '</form>';
  204:         
  205:         $r->print($display_out);
  206: 	return;
  207:     }
  208:     if ($select_mode eq 'true') {
  209:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
  210:         $r->print('<table id="LC_browser">'.
  211:             '<tr><th>Select</th><th>&nbsp;</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
  212:     } else {
  213:         $r->print('<form method="post" action="'.$url.'">');
  214:         $r->print('<table id="LC_browser">'.
  215:             '<tr><th colspan="2">Actions</th><th>&nbsp;</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th><th>Current Access Status</th></tr>');
  216:     }
  217:     $r->print("\n".&group_form_data()."\n");
  218: 
  219:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
  220:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
  221:     my @dir_lines;
  222:     my @version_lines;
  223:     my %versioned;
  224:     foreach my $dir_line (sort 
  225: 		      { 
  226: 			  my ($afile)=split('&',$a,2);
  227: 			  my ($bfile)=split('&',$b,2);
  228: 			  return (lc($afile) cmp lc($bfile));
  229: 		      } (@$dir_list)) {
  230:     	#$strip holds directory/file name
  231:     	#$dom 
  232:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$dir_line,16); 
  233:     	$filename =~ s/\s+$//;
  234:     	my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
  235:     	if ($version) {
  236:     	    push(@version_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
  237:     	    $versioned{$fname.'.'.$extension} .= $version.",";
  238:     	} else {
  239:     	    push(@dir_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
  240:     	}
  241:     }
  242:     foreach my $dir_line (@dir_lines) {
  243:         my ($filename,$dom,$testdir,$size,$mtime,$obs) = @$dir_line;
  244:         my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
  245:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
  246:     	    my %version_flag;
  247:     	    my $show_versions;
  248:     	    if ($env{'form.showversions'} eq $filename) {
  249:     	        $show_versions = 'true';
  250:     	    }
  251:     	    if (exists($versioned{$filename})) {
  252:     	        if ($show_versions) {
  253:                     $version_flag{$filename} = '<a href="portfolio?selectfile='.$filename.'&amp;continue=false&amp;currentpath='.$current_path.'"><img class="LC_icon" alt="'.&mt('opened folder').'" src="'.$iconpath.'folder_pointer_opened.gif" /></a>';
  254:     	        } else {
  255:                     $version_flag{$filename} = '<a href="portfolio?selectfile='.$filename.'&amp;continue=false&amp;currentpath='.$current_path.'&amp;showversions='.$filename.'"><img class="LC_icon" alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_pointer_closed.gif" /></a>';
  256:                 }
  257:     	    } else {
  258:     	        $version_flag{$filename} = '&nbsp;';
  259:     	    }
  260:             if ($dirptr&$testdir) {
  261: 		my $colspan='colspan="2"';
  262:                 if ($select_mode eq 'true'){
  263: 		    undef($colspan);
  264:                 }
  265: 		$r->print('<tr class="LC_browser_folder"><td '.$colspan.'><img alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_closed.gif" /></td>');
  266:                 $r->print('<td>Go to ...</td>');
  267:                 $r->print('<td>'.$version_flag{$filename}.'</td><td>'.&make_anchor($url,$filename.'/',$current_path.$filename.'/',$env{'form.mode'},$env{"form.fieldname"},$env{'form.continue'}).'</td>'); 
  268:                 $r->print('</tr>'); 
  269:             } else {
  270: 		my $css_class = 'LC_browser_file';
  271: 		my $line;
  272:                 my $fullpath = $current_path.$filename;
  273:                 $fullpath = &prepend_group($fullpath);
  274:                 if ($select_mode eq 'true') {
  275:                     $line='<td><input type="checkbox" name="checkfile" value="'.$filename.'"';
  276: 		    if ($$checked_files{$filename} eq 'selected') {
  277:                         $line.=" checked ";
  278:                     }
  279: 		    $line.=' /></td>';
  280:                 } else {
  281:                     if (exists $locked_files{$fullpath}) {
  282:                         $line.='<td colspan="2"><a href="'.$url.'?lockinfo='.$current_path.$filename.&group_args().'">Locked</a></td>';
  283: 			$css_class= 'LC_browser_file_locked';
  284:                     } else {
  285:                         if (!$can_modify) {
  286:                             $line .= '<td colspan="2">';
  287:                         } else {
  288:                             $line .= '<td>';
  289:                         }
  290:                         if ($can_delete) {
  291:                             $line .= '<input type="checkbox" name="selectfile" value="'.$filename.'" />';
  292:                         }
  293:                         if ($can_modify) {
  294:                             my $cat='<img class="LC_icon" alt="'.&mt('Catalog Information').
  295:                             '" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
  296:                             $line .= '<a href="'.$url.'?rename='.$filename.'&amp;currentpath='.$current_path.&group_args().'">Rename</a>';
  297:                             $line .= '</td><td>'.'<a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>';
  298:                         }
  299:                         $line .= '</td>';
  300:                     }
  301:                 }
  302: 		my $curr_access;
  303: 		if ($select_mode ne 'true') {
  304: 		    my $pub_access = 0;
  305: 		    my $guest_access = 0;
  306: 		    my $cond_access = 0;
  307: 		    foreach my $key (sort(keys(%{$access_controls{$fullpath}}))) {
  308: 			my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  309: 			if (($now > $start) && (!$end || $end > $now)) {
  310: 			    if ($scope eq 'public')  {
  311: 				$pub_access = 1;
  312: 			    } elsif ($scope eq 'guest') {
  313: 				$guest_access = 1;
  314: 			    } else {
  315: 				$cond_access = 1;
  316: 			    }
  317: 			}
  318: 		    }
  319: 		    if (!$pub_access && !$guest_access && !$cond_access) {
  320: 			$curr_access = &mt('Private');
  321: 		    } else {
  322: 			my @allaccesses; 
  323: 			if ($pub_access) {
  324: 			    push(@allaccesses,&mt('Public'));
  325: 			}
  326: 			if ($guest_access) {
  327: 			    push(@allaccesses,&mt('Passphrase-protected'));
  328: 			}
  329: 			if ($cond_access) {
  330: 			    push(@allaccesses,&mt('Conditional'));
  331: 			}
  332: 			$curr_access = join('+ ',@allaccesses);
  333: 		    }
  334: 		}
  335:                 &display_directory_line($r,$select_mode,$fullpath, $filename, $mtime, $size, $css_class, $line, 
  336:                                         \%access_controls, $curr_access,$now, \%version_flag, $href_location, 
  337:                                         $url, $current_path, $access_admin_text);
  338: 		if ($show_versions) {
  339: 		    foreach my $dir_line (@version_lines) {
  340: 		        my ($v_filename,$dom,$testdir,$size,$mtime,$obs) = @$dir_line;
  341:                         my ($v_fname,$version,$v_extension) = &Apache::grades::file_name_version_ext($v_filename);
  342:                         $line = '<td colspan="2">&nbsp;</td>';
  343:                         if (($v_fname eq $fname)&&($v_extension eq $extension)) {
  344: 		            &display_directory_line($r,$select_mode,$fullpath, $v_filename, $mtime, $size, 
  345: 		                            $css_class, $line, \%access_controls, $curr_access, $now,
  346: 		                             \%version_flag, $href_location, $url, $current_path, $access_admin_text, 1);
  347: 		        }
  348: 		    }
  349: 		}
  350:             }
  351:         }
  352:     }
  353:     if ($select_mode eq 'true') {
  354:         $r->print('</table>
  355:             <input type="hidden" name="continue" value="true" />
  356:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'" />
  357:             <input type="hidden" name="mode" value="selectfile" />
  358:             <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
  359:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
  360:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
  361:         </form>');        
  362:     } else {
  363:         $r->print('</table>');
  364:         if ($can_delete) {
  365:             $r->print('
  366:         <input type="submit" name="doit" value="Delete Checked Files" />
  367:         <input type="hidden" name="action" value="delete" />
  368:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
  369:         </form>'
  370:             );
  371:         }
  372:     }
  373: }
  374: 
  375: sub open_form {
  376:     my ($r,$url)=@_;
  377:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  378:     $r->print('<form name="portform" method="post" action="'.$url.'">');
  379:     $r->print('<input type="hidden" name="action" value="'.
  380: 	      $env{'form.action'}.'" />');
  381:     $r->print('<input type="hidden" name="confirmed" value="1" />');
  382:     foreach (@files) {
  383:         $r->print('<input type="hidden" name="selectfile" value="'.
  384: 	      $_.'" />');
  385:     }
  386:     $r->print('<input type="hidden" name="currentpath" value="'.
  387: 	      $env{'form.currentpath'}.'" />');
  388: }
  389: 
  390: sub close_form {
  391:     my ($r,$url,$button_text)=@_;
  392:     if (!defined($button_text)) {
  393:         $button_text = {
  394:                          'continue' => &mt('Continue'),
  395:                          'cancel'   => &mt('Cancel'),
  396:                        };
  397:     }
  398:     $r->print('<p><input type="submit" value="'.$button_text->{'continue'}.'" />');
  399:     $r->print(&group_form_data().'</p></form>');
  400:     $r->print('<form action="'.$url.'" method="post">
  401:                <p>
  402:               <input type="hidden" name="currentpath" value="'.
  403: 	      $env{'form.currentpath'}.'" />'.
  404: 	      &group_form_data());
  405:     $r->print("\n".'   <input type="submit" value="'.$button_text->{'cancel'}.'" />
  406:                </p></form>'); 
  407: }
  408: 
  409: sub display_file {
  410:     my ($path,$filename)=@_;
  411:     my $display_file_text;
  412:     my $file_start='<span class="LC_filename">';
  413:     my $file_end='</span>';
  414:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
  415:     if (!defined($filename)) { 
  416:         $filename=$env{'form.selectfile'};
  417:         $display_file_text = $file_start.$path.$filename.$file_end;
  418:     } elsif (ref($filename) eq "ARRAY") {
  419:         foreach my $file (@$filename) {
  420:             $display_file_text .= $file_start.$path.$file.$file_end.'<br />';
  421:         }
  422:     } elsif (ref($filename) eq "SCALAR") {
  423:         $display_file_text = $file_start.$path.$$filename.$file_end;
  424:     } else {
  425: 	$display_file_text = $file_start.$path.$filename.$file_end;
  426:     }
  427:     return $display_file_text;
  428: }
  429: 
  430: sub done {
  431:     my ($message,$url)=@_;
  432:     unless (defined $message) {
  433:         $message='Done';
  434:     }
  435:     my $result = '<h3><a href="'.$url.'?currentpath='.
  436: 	         $env{'form.currentpath'}.
  437: 	         '&fieldname='.$env{'form.fieldname'}.
  438: 	         '&mode='.$env{'form.mode'}.
  439: 		 &group_args();
  440:     $result .= '">'.&mt($message).'</a></h3>';
  441:     return $result;
  442: }
  443: 
  444: sub delete {
  445:     my ($r,$url)=@_;
  446:     my @check;
  447:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  448:     $file_name = &prepend_group($file_name);
  449:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  450:     my ($uname,$udom) = &get_name_dom();
  451:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  452:         $r->print ("The file is locked and cannot be deleted.<br />");
  453:         $r->print(&done('Back',$url));
  454:     } else {
  455:         if (scalar(@files)) {
  456:             &open_form($r,$url);
  457:             $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
  458:             &close_form($r,$url);
  459:         } else {
  460:             $r->print("No file was checked to delete.<br />");
  461:             $r->print(&done(undef,$url));
  462:         }
  463:     }
  464: } 
  465: 
  466: sub delete_confirmed {
  467:     my ($r,$url)=@_;
  468:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  469:     my $result;
  470:     my ($uname,$udom) = &get_name_dom();
  471:     my $port_path = &get_port_path();
  472:     foreach my $delete_file (@files) {
  473:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  474: 					       $env{'form.currentpath'}.
  475: 					       $delete_file);
  476:         if ($result ne 'ok') {
  477: 	$r->print('<span class="LC_error"> An error occured ('.$result.
  478: 		  ') while trying to delete '.&display_file(undef, $delete_file).'</span><br />');
  479:         }
  480:     }
  481:     $r->print(&done(undef,$url));
  482: }
  483: 
  484: sub delete_dir {
  485:     my ($r,$url)=@_;
  486:     &open_form($r,$url);
  487:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
  488:     &close_form($r,$url);
  489: } 
  490: 
  491: sub delete_dir_confirmed {
  492:     my ($r,$url)=@_;
  493:     my $directory_name = $env{'form.currentpath'};
  494:     $directory_name =~ s|/$||; # remove any trailing slash
  495:     my ($uname,$udom) = &get_name_dom();
  496:     my $namespace = &get_namespace();
  497:     my $port_path = &get_port_path();
  498:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  499: 					       $directory_name);
  500: 					       
  501:     if ($result ne 'ok') {
  502: 	$r->print('<span class="LC_error"> An error occured (dir) ('.$result.
  503: 		  ') while trying to delete '.$directory_name.'</span><br />');
  504:     } else {
  505:         # now remove from recent
  506: #        $r->print('<br /> removing '.$directory_name.'<br /');
  507:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
  508:         my @dirs = split m!/!, $directory_name;
  509:         
  510: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
  511:         $directory_name='/';
  512:         for (my $i=1; $i < (@dirs - 1); $i ++){
  513:             $directory_name .= $dirs[$i].'/';
  514:         }
  515:         $env{'form.currentpath'} = $directory_name;
  516:     }
  517:     $r->print(&done(undef,$url));
  518: }
  519: 
  520: sub rename {
  521:     my ($r,$url)=@_;
  522:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
  523:     my ($uname,$udom) = &get_name_dom();
  524:     $file_name = &prepend_group($file_name);
  525:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  526:         $r->print ("The file is locked and cannot be renamed.<br />");
  527:         $r->print(&done(undef,$url));
  528:     } else {
  529:         &open_form($r,$url);
  530:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  531:                    <input name="filenewname" type="input" size="50" />?</p>');
  532:         &close_form($r,$url);
  533:     }
  534: }
  535: 
  536: sub rename_confirmed {
  537:     my ($r,$url)=@_;
  538:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
  539:     my ($uname,$udom) = &get_name_dom();
  540:     my $port_path = &get_port_path();
  541:     if ($filenewname eq '') {
  542: 	$r->print('<span class="LC_error">'.
  543: 		  &mt("Error: no valid filename was provided to rename to.").
  544: 		  '</span><br />');
  545: 	$r->print(&done(undef,$url));
  546: 	return;
  547:     } 
  548:     my $result=
  549: 	&Apache::lonnet::renameuserfile($uname,$udom,
  550:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
  551:             $port_path.$env{'form.currentpath'}.$filenewname);
  552:     if ($result ne 'ok') {
  553: 	$r->print('<span class="LC_error">'.
  554: 		  &mt('An errror occured ([_1]) while trying to rename [_2]'
  555: 		      .' to [_3]',$result,&display_file(),
  556: 		      &display_file('',$filenewname)).'</span><br />');
  557:     }
  558:     if ($filenewname ne $env{'form.filenewname'}) {
  559:         $r->print(&mt("The new file name was changed from:<br />[_1] to [_2]",
  560: 		      '<strong>'.&display_file('',$env{'form.filenewname'}).'</strong>',
  561: 		      '<strong>'.&display_file('',$filenewname).'</strong>'));
  562:     }
  563:     $r->print(&done(undef,$url));
  564: }
  565: 
  566: sub display_access {
  567:     my ($r,$url,$group,$can_setacl,$port_path) = @_;
  568:     my ($uname,$udom) = &get_name_dom();
  569:     my $file_name = $env{'form.currentpath'}.$env{'form.access'};
  570:     $file_name = &prepend_group($file_name);
  571:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  572:                                                                         $uname);
  573:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group,$file_name);
  574:     my $aclcount = keys(%access_controls);
  575:     my $header = '<h3>'.&mt('Allowing others to retrieve file: [_1]',$port_path.$env{'form.currentpath'}.$env{'form.access'}).'</h3>';
  576:     my $info .= &mt('Access to this file by others can be set to be one or more of the following types: public, passphrase-protected or conditional.').'<br /><ul><li>'.&mt('Public files are available to anyone without the need for login.').'</li><li>'.&mt('Passphrase-protected files do not require log-in, but will require the viewer to enter the passphrase you set.').'</li><li>'.&mt('Conditional files are accessible to logged-in users with accounts in the LON-CAPA network, who satisfy the conditions you set.').'<br />'.&mt('The conditions can include affiliation with a particular course or group, or a user account in a specific domain.').'<br />'.&mt('Alternatively access can be granted to people with specific LON-CAPA usernames and domains.').'</li></ul>';
  577: 
  578:     if ($group eq '') {
  579:         $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>"));
  580:     }
  581:     
  582:     if ($can_setacl) {
  583:         &open_form($r,$url);
  584:         $r->print($header.$info);
  585:         &access_setting_table($r,$access_controls{$file_name});
  586:         my $button_text = {
  587:                         'continue' => &mt('Proceed'),
  588:                         'cancel' => &mt('Back to directory listing'),
  589:                       };
  590:         &close_form($r,$url,$button_text);
  591:     } else {
  592:         $r->print($header);
  593:         if ($aclcount) {  
  594:             $r->print($info);
  595:         }
  596:         &view_access_settings($r,$url,$access_controls{$file_name},$aclcount);
  597:     }
  598: }
  599: 
  600: sub view_access_settings {
  601:     my ($r,$url,$access_controls,$aclcount) = @_;
  602:     my ($showstart,$showend);
  603:     my %todisplay;
  604:     foreach my $key (sort(keys(%{$access_controls}))) {
  605:         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  606:         $todisplay{$scope}{$key} = $$access_controls{$key};
  607:     }
  608:     if ($aclcount) {
  609:         $r->print(&mt('<h4>Current access controls defined for this file:</h4>'));
  610:         $r->print(&Apache::loncommon::start_data_table());
  611:         $r->print(&Apache::loncommon::start_data_table_header_row());
  612:         $r->print('<th>'.&mt('Access control').'</th><th>'.&mt('Dates available').
  613:                   '</th><th>'.&mt('Additional information').'</th>');
  614:         $r->print(&Apache::loncommon::end_data_table_header_row());
  615:         my $count = 1;
  616:         my $chg = 'none';
  617:         &build_access_summary($r,$count,$chg,%todisplay);
  618:         $r->print(&Apache::loncommon::end_data_table());
  619:     } else {
  620:         $r->print(&mt('No access control settings currently exist for this file.<br />' ));
  621:     }
  622:     $r->print('<br /><a href="'.$url.'?currentpath='.$env{'form.currentpath'}.
  623:               &group_args().'">'.&mt('Return to directory listing').'</a>');
  624:     return;
  625: }
  626: 
  627: sub build_access_summary {
  628:     my ($r,$count,$chg,%todisplay) = @_; 
  629:     my ($showstart,$showend);
  630:     my %scope_desc = (
  631:                       public => 'Public',
  632:                       guest => 'Passphrase-protected',
  633:                       domains => 'Conditional: domain-based',
  634:                       users => 'Conditional: user-based',
  635:                       course => 'Conditional: course-based',
  636:                       group => 'Conditional: group-based',
  637:                      );
  638:     my @allscopes = ('public','guest','domains','users','course','group');
  639:     foreach my $scope (@allscopes) {
  640:         if ((!(exists($todisplay{$scope}))) || (ref($todisplay{$scope}) ne 'HASH')) {
  641:             next;
  642:         }
  643:         foreach my $key (sort(keys(%{$todisplay{$scope}}))) {
  644:             if ($count) {
  645:                 $r->print(&Apache::loncommon::start_data_table_row());
  646:             }
  647:             my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  648:             my $content = $todisplay{$scope}{$key};
  649:             if ($chg eq 'delete') {
  650:                 $showstart = &mt('Deleted');
  651:                 $showend = $showstart;
  652:             } else {
  653:                 $showstart = localtime($start);
  654:                 if ($end == 0) {
  655:                     $showend = &mt('No end date');
  656:                 } else {
  657:                     $showend = localtime($end);
  658:                 }
  659:             }
  660:             $r->print('<td>'.&mt($scope_desc{$scope}));
  661:             if (($scope eq 'course') || ($scope eq 'group')) {
  662:                 if ($chg ne 'delete') {
  663:                     my $cid = $content->{'domain'}.'_'.$content->{'number'};
  664:                     my %course_description = &Apache::lonnet::coursedescription($cid);
  665:                     $r->print('<br />('.$course_description{'description'}.')');
  666:                 }
  667:             }
  668:             $r->print('</td><td>'.&mt('Start: ').$showstart.
  669:                   '<br />'.&mt('End: ').$showend.'</td><td>');
  670:             if ($chg ne 'delete') {
  671:                 if ($scope eq 'guest') {
  672:                     $r->print(&mt('Passphrase').': '.$content->{'password'});
  673:                 } elsif ($scope eq 'course' || $scope eq 'group') {
  674:                     $r->print('<table><tr>');
  675:                     $r->print('<th>'.&mt('Roles').'</th><th>'.
  676:                           &mt('Access').'</th><th>'.
  677:                                           &mt('Sections').'</th>');
  678:                     if ($scope eq 'course') {
  679:                         $r->print('<th>'.&mt('Groups').'</th>');
  680:                     } else {
  681:                         $r->print('<th>'.&mt('Teams').'</th>');
  682:                     }
  683:                     $r->print('</tr>');
  684:                     foreach my $id (sort(keys(%{$content->{'roles'}}))) {
  685:                         $r->print('<tr>');
  686:                         foreach my $item ('role','access','section','group') {
  687:                             $r->print('<td>');
  688:                             if ($item eq 'role') {
  689:                                 my $ucscope = $scope;
  690:                                 $ucscope =~ s/^(\w)/uc($1)/e;
  691:                                 my $role_output;
  692:                                 foreach my $role (@{$content->{'roles'}{$id}{$item}}) {
  693:                                     if ($role eq 'all') {
  694:                                         $role_output .= $role.',';
  695:                                     } elsif ($role =~ /^cr/) {
  696:                                         $role_output .= (split('/',$role))[3].',';
  697:                                     } else {
  698:                                         $role_output .= &Apache::lonnet::plaintext($role,$ucscope).',';
  699:                                     }
  700:                                 }
  701:                                 $role_output =~ s/,$//;
  702:                                 $r->print($role_output);
  703:                             } else {
  704:                                 $r->print(join(',',@{$content->{'roles'}{$id}{$item}}));
  705:                             }
  706:                             $r->print('</tr>');
  707:                         }
  708: 			$r->print("</table>");
  709:                     }
  710: 		    $r->print("</tr></table>");
  711:                 } elsif ($scope eq 'domains') {
  712:                     $r->print(&mt('Domains: ').join(',',@{$content->{'dom'}}));
  713:                 } elsif ($scope eq 'users') {
  714:                     my $curr_user_list = &sort_users($content->{'users'});
  715:                     $r->print(&mt('Users: ').$curr_user_list);
  716:                 } else {
  717:                     $r->print('&nbsp;');
  718:                 }
  719:             } else {
  720:                 $r->print('&nbsp;');
  721:             }
  722:             $r->print('</td>');
  723:             $r->print(&Apache::loncommon::end_data_table_row());
  724:             $count ++;
  725:         }
  726:     }
  727: }
  728: 
  729: 
  730: sub update_access {
  731:     my ($r,$url,$group,$port_path) = @_;
  732:     my $totalprocessed = 0;
  733:     my %processing;
  734:     my %title  = (
  735:                          'activate' => 'New control(s) added',
  736:                          'delete'   => 'Existing control(s) deleted',
  737:                          'update'   => 'Existing control(s) modified',
  738:                      );
  739:     my $changes;
  740:     foreach my $chg (sort(keys(%title))) {     
  741:         @{$processing{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
  742:         $totalprocessed += @{$processing{$chg}};
  743:         foreach my $num (@{$processing{$chg}}) {
  744:             my $scope = $env{'form.scope_'.$num};
  745:             my ($start,$end) = &get_dates_from_form($num);
  746:             my $newkey = $num.':'.$scope.'_'.$end.'_'.$start;
  747:             if ($chg eq 'delete') {
  748:                 $$changes{$chg}{$newkey} = 1;
  749:             } else {
  750:                 $$changes{$chg}{$newkey} = 
  751:                             &build_access_record($num,$scope,$start,$end,$chg);
  752:             }
  753:         }
  754:     }
  755:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  756:     $r->print('<h3>'.&mt('Allowing others to retrieve file: [_1]',
  757:               $port_path.$file_name).'</h3>'."\n");
  758:     $file_name = &prepend_group($file_name);
  759:     my ($uname,$udom) = &get_name_dom();
  760:     my ($errors,$outcome,$deloutcome,$new_values,$translation);
  761:     if ($totalprocessed) {
  762:         ($outcome,$deloutcome,$new_values,$translation) =
  763:         &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
  764:                                                 $uname);
  765:     }
  766:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  767:                                                                        $uname);
  768:     my %access_controls = 
  769: 	&Apache::lonnet::get_access_controls($current_permissions,
  770: 					     $group,$file_name);
  771:     if ($totalprocessed) {
  772:         if ($outcome eq 'ok') {
  773:             my $updated_controls = $access_controls{$file_name};
  774:             my ($showstart,$showend);
  775:             $r->print(&Apache::loncommon::start_data_table());
  776:             $r->print(&Apache::loncommon::start_data_table_header_row());
  777:             $r->print('<th>'.&mt('Type of change').'</th><th>'.
  778:                       &mt('Access control').'</th><th>'.&mt('Dates available').
  779:                       '</th><th>'.&mt('Additional information').'</th>');
  780:             $r->print(&Apache::loncommon::end_data_table_header_row());
  781:             foreach my $chg (sort(keys(%processing))) {
  782:                 if (@{$processing{$chg}} > 0) {
  783:                     if ($chg eq 'delete') {
  784:                         if (!($deloutcome eq 'ok')) {
  785:                             $errors .='<span class="LC_error">'.
  786: 				&mt('A problem occurred deleting access controls: [_1]',$deloutcome).
  787: 				'</span>';
  788:                             next;
  789:                         }
  790:                     }
  791:                     my $numchgs = @{$processing{$chg}};
  792:                     $r->print(&Apache::loncommon::start_data_table_row());
  793:                     $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
  794:                               '.</td>');
  795:                     my $count = 0;
  796:                     my %todisplay;
  797:                     foreach my $key (sort(keys(%{$$changes{$chg}}))) {
  798:                         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  799:                         my $newkey = $key;
  800:                         if ($chg eq 'activate') {
  801:                             $newkey =~ s/^(\d+)/$$translation{$1}/;
  802:                         }
  803:                         $todisplay{$scope}{$newkey} = $$updated_controls{$newkey};
  804:                     }
  805:                     &build_access_summary($r,$count,$chg,%todisplay);  
  806:                 }
  807:             }
  808:             $r->print(&Apache::loncommon::end_data_table());
  809:         } else {
  810:             if ((@{$processing{'activate'}} > 0) || (@{$processing{'update'}} > 0)) {
  811:                 $errors .= '<span class="LC_error">'.
  812: 		    &mt('A problem occurred storing access control settings: [_1]',$outcome).
  813: 		    '</span>';
  814:             }
  815:         }
  816:         if ($errors) { 
  817:             $r->print($errors);
  818:         }
  819:     }
  820:     my $allnew = 0;
  821:     my $totalnew = 0;
  822:     my $status = 'new';
  823:     my ($firstitem,$lastitem);
  824:     foreach my $newitem ('course','group','domains','users') {
  825:         $allnew += $env{'form.new'.$newitem};
  826:     }
  827:     if ($allnew > 0) {
  828:         my $now = time;
  829:         my $then = $now + (60*60*24*180); # six months approx.
  830:         &open_form($r,$url);
  831:         foreach my $newitem ('course','group','domains','users') {
  832:             if ($env{'form.new'.$newitem} > 0) {
  833:                 $r->print('<br />'.&mt('Add new <b>[_1]-based</b> access control for portfolio file: <b>[_2]</b>',$newitem,$env{'form.currentpath'}.$env{'form.selectfile'}).'<br /><br />');
  834:                 $firstitem = $totalnew;
  835:                 $lastitem = $totalnew + $env{'form.new'.$newitem};
  836:                 $totalnew = $lastitem;
  837:                 my @numbers;   
  838:                 for (my $i=$firstitem; $i<$lastitem; $i++) {
  839:                     push (@numbers,$i);
  840:                 }
  841:                 &display_access_row($r,$status,$newitem,\@numbers,
  842:                                     $access_controls{$file_name},$now,$then);
  843:             }
  844:         }
  845:         &close_form($r,$url);
  846:     } else {
  847:         $r->print('<br /><a href="'.$url.'?access='.$env{'form.selectfile'}.
  848:                   '&amp;currentpath='.$env{'form.currentpath'}.&group_args().'">'.
  849:                    &mt('Display all access settings for this file').'</a>'.
  850:                   '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.
  851:                   '<a href="'.$url.'?currentpath='.$env{'form.currentpath'}.
  852:                   &group_args().'">'.&mt('Return to directory listing').'</a>');
  853:     }
  854:     return;
  855: }
  856: 
  857: sub build_access_record {
  858:     my ($num,$scope,$start,$end,$chg) = @_;
  859:     my $record = {
  860: 	type => $scope,
  861: 	time => {
  862: 	    start => $start,
  863: 	    end   => $end
  864: 	    },
  865: 	    };
  866: 		
  867:     if ($scope eq 'guest') {	
  868:         $record->{'password'} = $env{'form.password'};
  869:     } elsif (($scope eq 'course') || ($scope eq 'group')) {
  870:         $record->{'domain'} = $env{'form.crsdom_'.$num};
  871: 	$record->{'number'} = $env{'form.crsnum_'.$num};
  872:         my @role_ids;
  873:         my @delete_role_ids =
  874:             &Apache::loncommon::get_env_multiple('form.delete_role_'.$num);
  875: 	my @preserves =
  876: 	    &Apache::loncommon::get_env_multiple('form.preserve_role_'.$num);
  877: 	if (@delete_role_ids) {
  878: 	    foreach my $id (@preserves) {
  879: 		if (grep {$_ = $id} (@delete_role_ids)) {
  880: 		    next;
  881: 		}
  882: 		push(@role_ids,$id); 
  883: 	    }
  884: 	} else {
  885: 	    push(@role_ids,@preserves);
  886: 	}
  887: 
  888: 	my $next_id = $env{'form.add_role_'.$num};
  889: 	if ($next_id) {
  890: 	    push(@role_ids,$next_id);
  891: 	}
  892: 
  893:         foreach my $id (@role_ids) {
  894:             my (@roles,@accesses,@sections,@groups);
  895:             if (($id == $next_id) && ($chg eq 'update')) {
  896:                 @roles    = split(/,/,$env{'form.role_'.$num.'_'.$next_id});
  897:                 @accesses = split(/,/,$env{'form.access_'.$num.'_'.$next_id});
  898:                 @sections = split(/,/,$env{'form.section_'.$num.'_'.$next_id});
  899:                 @groups   = split(/,/,$env{'form.group_'.$num.'_'.$next_id});
  900:             } else {
  901:                 @roles = &Apache::loncommon::get_env_multiple('form.role_'.$num.'_'.$id);
  902:                 @accesses = &Apache::loncommon::get_env_multiple('form.access_'.$num.'_'.$id);
  903:                 @sections = &Apache::loncommon::get_env_multiple('form.section_'.$num.'_'.$id);
  904:                 @groups = &Apache::loncommon::get_env_multiple('form.group_'.$num.'_'.$id);
  905:             }
  906: 	    $record->{'roles'}{$id}{'role'}    = \@roles;
  907: 	    $record->{'roles'}{$id}{'access'}  = \@accesses;
  908: 	    $record->{'roles'}{$id}{'section'} = \@sections;
  909: 	    $record->{'roles'}{$id}{'group'}   = \@groups;
  910:         }
  911:     } elsif ($scope eq 'domains') {
  912:         my @doms = &Apache::loncommon::get_env_multiple('form.dom_'.$num);
  913: 	$record->{'dom'} = \@doms;
  914:     } elsif ($scope eq 'users') {
  915:         my $userlist = $env{'form.users_'.$num};
  916:         $userlist =~ s/\s+//sg;
  917: 	my %userhash = map { ($_,1) } (split(/,/,$userlist));
  918:         foreach my $user (keys(%userhash)) {
  919:             my ($uname,$udom) = split(/:/,$user);
  920: 	    push(@{$record->{'users'}}, {
  921: 		'uname' => $uname,
  922: 		'udom'  => $udom
  923: 		});
  924: 	}
  925:     }
  926:     return $record;
  927: }
  928: 
  929: sub get_dates_from_form {
  930:     my ($id) = @_;
  931:     my $startdate;
  932:     my $enddate;
  933:     $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$id);
  934:     $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$id);
  935:     if ( exists ($env{'form.noend_'.$id}) ) {
  936:         $enddate = 0;
  937:     }
  938:     return ($startdate,$enddate);
  939: }
  940: 
  941: sub sort_users {
  942:     my ($users) = @_; 
  943:     my @curr_users = map {
  944: 	$_->{'uname'}.':'.$_->{'udom'}
  945:     } (@{$users});
  946:     my $curr_user_list = join(",\n",sort(@curr_users));
  947:     return $curr_user_list;
  948: }
  949: 
  950: sub access_setting_table {
  951:     my ($r,$access_controls) = @_;
  952:     my ($public,$publictext);
  953:     $publictext = &mt('Off');
  954:     my ($guest,$guesttext);
  955:     $guesttext = &mt('Off');
  956:     my @courses = ();
  957:     my @groups = ();
  958:     my @domains = ();
  959:     my @users = ();
  960:     my $now = time;
  961:     my $then = $now + (60*60*24*180); # six months approx.
  962:     my ($num,$scope,$publicnum,$guestnum);
  963:     my (%acl_count,%end,%start);
  964:     foreach my $key (sort(keys(%{$access_controls}))) {
  965:         ($num,$scope,$end{$key},$start{$key}) = &unpack_acc_key($key);
  966:         if ($scope eq 'public') {
  967:             $public = $key;
  968:             $publicnum = $num;
  969:             $publictext = &acl_status($start{$key},$end{$key},$now);
  970:         } elsif ($scope eq 'guest') {
  971:             $guest=$key;
  972:             $guestnum = $num;  
  973:             $guesttext = &acl_status($start{$key},$end{$key},$now);
  974:         } elsif ($scope eq 'course') {
  975:             push(@courses,$key);
  976:         } elsif ($scope eq 'group') {
  977:             push(@groups,$key);
  978:         } elsif ($scope eq 'domains') {
  979:             push(@domains,$key);
  980:         } elsif ($scope eq 'users') {
  981:             push(@users,$key);
  982:         }
  983:         $acl_count{$scope} ++;
  984:     }
  985:     $r->print('<table border="0"><tr><td valign="top">');
  986:     $r->print('<h3>'.&mt('Public access:').' '.$publictext.'</h3>');
  987:     $r->print(&Apache::loncommon::start_data_table());
  988:     $r->print(&Apache::loncommon::start_data_table_header_row());
  989:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').'</th>');
  990:     $r->print(&Apache::loncommon::end_data_table_header_row());
  991:     $r->print(&Apache::loncommon::start_data_table_row());
  992:     if ($public) {
  993:         $r->print('<td>'.&actionbox('old',$publicnum,'public').'</td><td>'.
  994:                   &dateboxes($publicnum,$start{$public},$end{$public}).'</td>');
  995:     } else {
  996:         $r->print('<td>'.&actionbox('new','0','public').'</td><td>'.
  997:                   &dateboxes('0',$now,$then).'</td>');
  998:     }
  999:     $r->print(&Apache::loncommon::end_data_table_row());
 1000:     $r->print(&Apache::loncommon::end_data_table());
 1001:     $r->print('</td><td width="40">&nbsp;</td><td valign="top">');
 1002:     $r->print('<h3>'.&mt('Passphrase-protected access:').' '.$guesttext.'</h3>');
 1003:     $r->print(&Apache::loncommon::start_data_table());
 1004:     $r->print(&Apache::loncommon::start_data_table_header_row());
 1005:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').
 1006:               '</th><th>'. &mt('Passphrase').'</th>');
 1007:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1008:     $r->print(&Apache::loncommon::start_data_table_row());
 1009:     my $passwd;
 1010:     if ($guest) {
 1011:         $passwd = $$access_controls{$guest}{'password'};
 1012:         $r->print('<td>'.&actionbox('old',$guestnum,'guest').'</td><td>'.
 1013:                   &dateboxes($guestnum,$start{$guest},$end{$guest}).'</td>');
 1014:     } else {
 1015:         $r->print('<td>'.&actionbox('new','1','guest').'</td><td>'.
 1016:                   &dateboxes('1',$now,$then).'</td>');
 1017:     }
 1018:     $r->print('<td><input type="text" size="15" name="password" value="'.
 1019:               $passwd.'" /></td>');
 1020:     $r->print(&Apache::loncommon::end_data_table_row());
 1021:     $r->print(&Apache::loncommon::end_data_table());
 1022:     $r->print('</td></tr><tr><td colspan="3">&nbsp;</td></tr><tr><td valign="top">');
 1023:     &access_element($r,'domains',\%acl_count,\@domains,$access_controls,$now,$then);
 1024:     $r->print('</td><td>&nbsp;</td><td valign="top">');
 1025:     &access_element($r,'users',\%acl_count,\@users,$access_controls,$now,$then);
 1026:     $r->print('</td></tr><tr><td colspan="3"></td></tr><tr>');
 1027:     if (@courses > 0 || @groups > 0) {
 1028:         $r->print('<td colspan="3" valign="top">');
 1029:     } else {
 1030:         $r->print('<td valign="top">');
 1031:     }
 1032:     &access_element($r,'course',\%acl_count,\@courses,$access_controls,$now,$then);
 1033:     $r->print('</td>');
 1034:     if (@courses > 0 || @groups > 0) {
 1035:         $r->print('</tr><tr><td colspan="3">&nbsp;</td></tr><tr><td colspan="3" valign="top">');
 1036:     } else {
 1037:         $r->print('<td>&nbsp;</td><td valign="top">');
 1038:     }
 1039:     &access_element($r,'group',\%acl_count,\@groups,$access_controls,$now,$then);
 1040:     $r->print('</td></tr></table>');
 1041: }
 1042: 
 1043: sub acl_status {
 1044:     my ($start,$end,$now) = @_;
 1045:     if ($start > $now) {
 1046:         return &mt('Inactive');
 1047:     }
 1048:     if ($end && $end<$now) {
 1049:         return &mt('Inactive');
 1050:     }
 1051:     return &mt('Active');
 1052: }
 1053: 
 1054: sub access_element {
 1055:     my ($r,$type,$acl_count,$items,$access_controls,$now,$then) = @_;
 1056:     my $title = $type;
 1057:     $title =~ s/s$//;
 1058:     $title =~ s/^(\w)/uc($1)/e;
 1059:     $r->print('<h3>'.&mt('[_1]-based conditional access: ',$title));
 1060:     if ($$acl_count{$type}) {
 1061:         $r->print($$acl_count{$type}.' ');
 1062:         if ($$acl_count{$type} > 1) {
 1063:             $r->print(&mt('conditions'));
 1064:         } else {
 1065:             $r->print(&mt('condition'));
 1066:         }
 1067:     } else {
 1068:         $r->print(&mt('Off'));
 1069:     }
 1070:     $r->print('</h3>');
 1071:     &display_access_row($r,'old',$type,$items,$access_controls,$now,$then);
 1072:     return;
 1073: }
 1074: 
 1075: sub display_access_row {
 1076:     my ($r,$status,$type,$items,$access_controls,$now,$then) = @_;
 1077:     if (@{$items} > 0) {
 1078:         my @all_doms;
 1079:         my $colspan = 3;
 1080:         my $uctype = $type;
 1081:         $uctype =~ s/^(\w)/uc($1)/e;
 1082:         $r->print(&Apache::loncommon::start_data_table());
 1083:         $r->print(&Apache::loncommon::start_data_table_header_row());
 1084:         $r->print('<th>'.&mt('Action?').'</th><th>'.&mt($uctype).'</th><th>'.
 1085:               &mt('Dates available').'</th>');
 1086:         if (($type eq 'course') || ($type eq 'group')) {
 1087:             $r->print('<th>'.&mt('Allowed [_1] member affiliations',$type).
 1088:                       '</th>');
 1089:             $colspan ++;
 1090:         } elsif ($type eq 'domains') {
 1091:             @all_doms = &Apache::loncommon::get_domains();
 1092:         }
 1093:         $r->print(&Apache::loncommon::end_data_table_header_row());
 1094:         foreach my $key (@{$items}) {
 1095: 	    $r->print(&Apache::loncommon::start_data_table_row());
 1096:             if (($type eq 'course') || ($type eq 'group')) {
 1097:                 &course_row($r,$status,$type,$key,$access_controls,$now,$then);
 1098:             } elsif ($type eq 'domains') {
 1099:                 &domains_row($r,$status,$key,\@all_doms,$access_controls,$now,
 1100:                             $then);
 1101:             } elsif ($type eq 'users') {
 1102:                 &users_row($r,$status,$key,$access_controls,$now,$then);
 1103:             }
 1104: 	    $r->print(&Apache::loncommon::end_data_table_row());
 1105:         }
 1106:         if ($status eq 'old') {
 1107: 	    $r->print(&Apache::loncommon::start_data_table_row());
 1108:             $r->print('<td colspan="',$colspan.'">'.&additional_item($type).
 1109:                       '</td>');
 1110: 	    $r->print(&Apache::loncommon::end_data_table_row());
 1111:         }
 1112:         $r->print(&Apache::loncommon::end_data_table());
 1113:     } else {
 1114:         $r->print(&mt('No [_1]-based conditions defined.<br />',$type).
 1115:                   &additional_item($type));
 1116:     }
 1117:     return;
 1118: }
 1119: 
 1120: sub course_js {
 1121:     return qq|
 1122: <script type="text/javascript">
 1123: function setRoleOptions(caller,num,cdom,cnum,type) {
 1124:     addIndexnum = getCallerIndex(caller);
 1125:     updateIndexnum = getIndex('update',num);
 1126:     if (caller.checked) {
 1127:         document.portform.elements[updateIndexnum].checked = true;
 1128:         var url = '/adm/portfolio?action=rolepicker&setroles='+addIndexnum+'&cnum='+cnum+'&cdom='+cdom+'&type='+type;
 1129:         var title = 'Roles_Chooser';
 1130:         var options = 'scrollbars=1,resizable=1,menubar=0';
 1131:         options += ',width=700,height=600';
 1132:         rolebrowser = open(url,title,options,'1');
 1133:         rolebrowser.focus();
 1134:     } else {
 1135:         for (var j=0;j<5;j++) {
 1136:             document.portform.elements[addIndexnum+j].value = '';
 1137:         }
 1138:     }
 1139: }
 1140: 
 1141: function getCallerIndex(caller) {
 1142:     for (var i=0;i<document.portform.elements.length;i++) {
 1143:         if (document.portform.elements[i] == caller) {
 1144:             return i;
 1145:         }
 1146:     }
 1147:     return -1;
 1148: }
 1149: 
 1150: function getIndex(name,value) {
 1151:     for (var i=0;i<document.portform.elements.length;i++) {
 1152:         if (document.portform.elements[i].name == name && document.portform.elements[i].value == value) {
 1153:             return i;
 1154:         }
 1155:     }
 1156:     return -1;
 1157: }
 1158: 
 1159: </script>
 1160: |;
 1161: }
 1162: 
 1163: sub course_row {
 1164:     my ($r,$status,$type,$item,$access_controls,$now,$then) = @_;
 1165:     my $content;
 1166:     my $defdom = $env{'user.domain'};
 1167:     if ($status eq 'old') {
 1168:         $content = $$access_controls{$item}; 
 1169:         $defdom =  $content->{'domain'};
 1170:     }
 1171:     my $js = &Apache::loncommon::coursebrowser_javascript($defdom)
 1172: 	.&course_js();
 1173:     my $crsgrptext = 'Groups';
 1174:     if ($type eq 'group') {
 1175:         $crsgrptext = 'Teams';
 1176:     }
 1177:     my $uctype = $type;
 1178:     $uctype =~ s/^(\w)/uc($1)/e;
 1179:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1180:                                                     $type);
 1181:     $r->print('<td>'.$js.&actionbox($status,$num,$scope).'</td>');
 1182:     if ($status eq 'old') {
 1183:         my $cid = $content->{'domain'}.'_'.$content->{'number'};
 1184:         my %course_description = &Apache::lonnet::coursedescription($cid);
 1185:         $r->print('<td><input type="hidden" name="crsdom_'.$num.'" value="'.$content->{'domain'}.'" /><input type="hidden" name="crsnum_'.$num.'" value="'.$content->{'number'}.'" />'.$course_description{'description'}.'</td>');
 1186:     } elsif ($status eq 'new') {
 1187:         $r->print('<td>'.&Apache::loncommon::selectcourse_link('portform','crsnum_'.$num,'crsdom_'.$num,'description_'.$num,undef,undef,$uctype).'&nbsp;&nbsp;<input type="text" name="description_'.$num.'" size="30" /><input type="hidden" name="crsdom_'.$num.'" /><input type="hidden" name="crsnum_'.$num.'" /></td>');
 1188:     }
 1189:     $r->print('<td>'.&dateboxes($num,$start,$end).'</td>');
 1190:     $r->print('<td><table><tr>');
 1191:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Roles').'</th><th>'.
 1192:               &mt('Access').'</th><th>'.&mt('Sections').'</th><th>'.
 1193:               &mt($crsgrptext).'</th></tr>');
 1194:     if ($status eq 'old') {
 1195:         my $max_id = 0;
 1196:         foreach my $role_id (sort(keys(%{$content->{'roles'}}))) {
 1197:             if ($role_id > $max_id) {
 1198:                 $max_id = $role_id;
 1199:             }
 1200:             $max_id ++;
 1201:             my $role_selects = &role_selectors($num,$role_id,$status,$type,$content,'display');
 1202:             $r->print('<tr><td><span style="white-space: nowrap"><label><input type="checkbox" name="delete_role_'.$num.'" value="'.$role_id.'" />'.&mt('Delete').'</label></span><br /><input type="hidden" name="preserve_role_'.$num.'" value="'.$role_id.'" /></td>'.$role_selects.'</tr>');
 1203:         }
 1204:         $r->print('</table><br />'.&mt('Add a roles-based condition').'&nbsp;<input type="checkbox" name ="add_role_'.$num.'" onClick="javascript:setRoleOptions(this,'."'$num','$content->{'domain'}','$content->{'number'}','$uctype'".')" value="'.$max_id.'" /><input type="hidden" name="role_'.$num.'_'.$max_id.'" /><input type="hidden" name="access_'.$num.'_'.$max_id.'" /><input type="hidden" name="section_'.$num.'_'.$max_id.'" /><input type="hidden" name="group_'.$num.'_'.$max_id.'" /></td>');
 1205:     } elsif ($status eq 'new') {
 1206:         my $role_id = 1;
 1207:         my $role_selects = &role_selectors($num,$role_id,$status,$type,undef,'display');
 1208:         $r->print('<tr><td><input type="checkbox" name="add_role_'.$num.'" value="'.$role_id.'" checked="checked" />'.&mt('Add').'<input type="hidden" name="grplist_'.$num.'_'.$role_id.'" /></td>'.$role_selects);
 1209:         $r->print('</tr></table></td>');
 1210:     }
 1211:     return;
 1212: }
 1213: 
 1214: sub domains_row {
 1215:     my ($r,$status,$item,$all_doms,$access_controls,$now,$then) = @_;
 1216:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1217:                                                     'domains');
 1218:     my $dom_select = '<select name="dom_'.$num.'" size="4" multiple="true">'.
 1219:                      ' <option value="">'.&mt('Please select').'</option>';
 1220:     if ($status eq 'old') {
 1221:         my $content =  $$access_controls{$item};
 1222: 	foreach my $dom (@{$all_doms}) {
 1223:             if ((@{$content->{'dom'}} > 0) 
 1224: 		&& (grep(/^\Q$dom\E$/,@{$content->{'dom'}}))) {
 1225:                 $dom_select .= '<option value="'.$dom.'" selected>'.
 1226:                                $dom.'</option>';
 1227:             } else {
 1228:                 $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
 1229:             }
 1230:         }
 1231:     } else {
 1232:         foreach my $dom (@{$all_doms}) {
 1233:             $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
 1234:         }
 1235:     }
 1236:     $dom_select .= '</select>';
 1237:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.$dom_select.
 1238:               '</td><td>'.&dateboxes($num,$start,$end).'</td>');
 1239: }
 1240: 
 1241: sub users_row {
 1242:     my ($r,$status,$item,$access_controls,$now,$then) = @_;
 1243:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1244:                                                     'users');
 1245:     my $curr_user_list;
 1246:     if ($status eq 'old') {
 1247:         my $content = $$access_controls{$item};
 1248:         $curr_user_list = &sort_users($content->{'users'});
 1249:     }
 1250:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.&mt("Format for users' username:domain information:").'<br /><tt>sparty:msu,illini:uiuc  ... etc.</tt><br /><textarea name="users_'.$num.'" cols="30"  rows="5">'.$curr_user_list.'</textarea></td><td>'.&dateboxes($num,$start,$end).'</td>');
 1251: }
 1252: 
 1253: sub additional_item {
 1254:     my ($type) = @_;
 1255:     my $output = &mt('Add new [_1] condition(s)?',$type).'&nbsp;'.&mt('Number to add: ').'<input type="text" name="new'.$type.'" size="3" value="0" />';
 1256:     return $output;
 1257: }
 1258: 
 1259: sub actionbox {
 1260:     my ($status,$num,$scope) = @_;
 1261:     my $output = '<span style="white-space: nowrap"><label>';
 1262:     if ($status eq 'new') {
 1263:         $output .= '<input type="checkbox" name="activate" value="'.$num.'" />'.
 1264:         &mt('Activate');
 1265:     } else {
 1266:         $output .= '<input type="checkbox" name="delete" value="'.$num.
 1267:                    '" />'.&mt('Delete').'</label></span><br /><span style="white-space: nowrap">'.
 1268:                    '<label><input type="checkbox" name="update" value="'.
 1269:                    $num.'" />'.&mt('Update');
 1270:     }
 1271:     $output .= '</label></span><input type="hidden" name="scope_'.$num.                '" value="'.$scope.'" />';
 1272:     return $output;
 1273: }
 1274:                                                                                    
 1275: sub dateboxes {
 1276:     my ($num,$start,$end) = @_;
 1277:     my $noend;
 1278:     if ($end == 0) {
 1279:         $noend = 'checked="checked"';
 1280:     }
 1281:     my $startdate = &Apache::lonhtmlcommon::date_setter('portform',
 1282:                            'startdate_'.$num,$start,undef,undef,undef,1,undef,
 1283:                             undef,undef,1);
 1284:     my $enddate = &Apache::lonhtmlcommon::date_setter('portform',
 1285:                                'enddate_'.$num,$end,undef,undef,undef,1,undef,
 1286:                                 undef,undef,1). '&nbsp;&nbsp;<span style="white-space: nowrap"><label>'.
 1287:                                 '<input type="checkbox" name="noend_'.
 1288:                                 $num.'" '.$noend.' />'.&mt('No end date').
 1289:                                 '</label></span>';
 1290:                                                                                    
 1291:     my $output = &mt('Start: ').$startdate.'<br />'.&mt('End: ').$enddate;
 1292:     return $output;
 1293: }
 1294: 
 1295: sub unpack_acc_key {
 1296:     my ($acc_key) = @_;
 1297:     my ($num,$scope,$end,$start) = ($acc_key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 1298:     return ($num,$scope,$end,$start);
 1299: }
 1300: 
 1301: sub set_identifiers {
 1302:     my ($status,$item,$now,$then,$scope) = @_;
 1303:     if ($status eq 'old') {
 1304:         return(&unpack_acc_key($item));
 1305:     } else {
 1306:         return($item,$scope,$then,$now);
 1307:     }
 1308: } 
 1309: 
 1310: sub role_selectors {
 1311:     my ($num,$role_id,$status,$type,$content,$caller) = @_;
 1312:     my ($output,$cdom,$cnum,$longid);
 1313:     if ($caller eq 'display') {
 1314:         $longid = '_'.$num.'_'.$role_id;
 1315:         if ($status eq 'new') {
 1316:             foreach my $item ('role','access','section','group') {
 1317:                 $output .= '<td><select name="'.$item.$longid.'">'.
 1318:                            '<option value="">'.&mt('Pick [_1] first',$type).
 1319:                            '</option></select></td>';
 1320:             }
 1321:             return $output;
 1322:         } else {
 1323:             $cdom = $$content{'domain'};
 1324:             $cnum = $$content{'number'};
 1325:         }
 1326:     } elsif ($caller eq 'rolepicker') {
 1327:          $cdom = $env{'form.cdom'};
 1328:          $cnum = $env{'form.cnum'};
 1329:     }
 1330:     my $uctype = $type;
 1331:     $uctype =~ s/^(\w)/uc($1)/e;
 1332:     my ($sections,$groups,$allroles,$rolehash,$accesshash) =
 1333:             &Apache::loncommon::get_secgrprole_info($cdom,$cnum,1,$uctype);
 1334:     if (!@{$sections}) {
 1335:         @{$sections} = ('none');
 1336:     } else {
 1337:         unshift(@{$sections},('all','none'));
 1338:     }
 1339:     if (!@{$groups}) {
 1340:         @{$groups} = ('none');
 1341:     } else {
 1342:         unshift(@{$groups},('all','none'));
 1343:     }
 1344:     my @allacesses = sort(keys(%{$accesshash}));
 1345:     my (%sectionhash,%grouphash);
 1346:     foreach my $sec (@{$sections}) {
 1347:         $sectionhash{$sec} = $sec;
 1348:     }
 1349:     foreach my $grp (@{$groups}) {
 1350:         $grouphash{$grp} = $grp;
 1351:     }
 1352:     my %lookup = (
 1353:                    'role' => $rolehash,
 1354:                    'access' => $accesshash,
 1355:                    'section' => \%sectionhash,
 1356:                    'group' => \%grouphash,
 1357:                  );
 1358:     my @allaccesses = sort(keys(%{$accesshash}));
 1359:     my %allitems = (
 1360:                     'role' => $allroles,
 1361:                     'access' => \@allaccesses,
 1362:                     'section' => $sections,
 1363:                     'group' => $groups, 
 1364:                    );
 1365:     foreach my $item ('role','access','section','group') {
 1366:         $output .= '<td><select name="'.$item.$longid.'" multiple="true" size="4">'."\n";
 1367:         foreach my $entry (@{$allitems{$item}}) {
 1368:             if ($caller eq 'display') {
 1369:                 if ((@{$$content{'roles'}{$role_id}{$item}} > 0) && 
 1370:                     (grep(/^\Q$entry\E$/,@{$$content{'roles'}{$role_id}{$item}}))) {
 1371:                     $output .= '  <option value="'.$entry.'" selected>'.
 1372:                                   $lookup{$item}{$entry}.'</option>';
 1373:                     next;
 1374:                 }
 1375:             }
 1376:             $output .= '  <option value="'.$entry.'">'.
 1377:                        $lookup{$item}{$entry}.'</option>';
 1378:         }
 1379:         $output .= '</select>';
 1380:     }
 1381:     $output .= '</td>';
 1382:     return $output;
 1383: }
 1384: 
 1385: sub role_options_window {
 1386:     my ($r) = @_;
 1387:     my $cdom = $env{'form.cdom'};
 1388:     my $cnum = $env{'form.cnum'};
 1389:     my $type = $env{'form.type'};
 1390:     my $addindex = $env{'form.setroles'};
 1391:     my $grouptitle = 'Groups';
 1392:     if ($type eq 'Group') {
 1393:          $grouptitle = 'Teams';
 1394:     } 
 1395:     my $role_selects = &role_selectors(1,1,'new',$type,undef,'rolepicker');
 1396:     $r->print(<<"END_SCRIPT");
 1397: <script type="text/javascript">
 1398: function setRoles() {
 1399:     var addidx = $addindex+1;
 1400:     for (var i=0; i<4; i++) {
 1401:         var copylist = '';
 1402:         for (var j=0; j<document.rolepicker.elements[i].length; j++) {
 1403:             if (document.rolepicker.elements[i].options[j].selected) {
 1404:                 copylist = copylist + document.rolepicker.elements[i].options[j].value + ',';
 1405:             }
 1406:         }
 1407:         copylist = copylist.substr(0,copylist.length-1);
 1408:         opener.document.portform.elements[addidx+i].value = copylist;
 1409:     }
 1410:     self.close();
 1411: }
 1412: </script>
 1413: END_SCRIPT
 1414:     $r->print(&mt('Select roles, course status, section(s) and group(s) for users who will be able to access the portfolio file.'));
 1415:     $r->print('<form name="rolepicker" action="/adm/portfolio" method="post"><table><tr><th>'.&mt('Roles').'</th><th>'.&mt('[_1] status',$type).'</th><th>'.&mt('Sections').'</th><th>'.&mt($grouptitle).'</th></tr><tr>'.$role_selects.'</tr></table><br /><input type="button" name="rolepickbutton" value="Save selections" onclick="setRoles()" />');
 1416:     return;
 1417: }
 1418: 
 1419: sub select_files {
 1420:     my ($r) = @_;
 1421:     if ($env{'form.continue'} eq 'true') {
 1422:         # here we update the selections for the currentpath
 1423:         # eventually, have to handle removing those not checked, but . . . 
 1424:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
 1425:         if (scalar(@items)){
 1426:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
 1427:         }
 1428:     } else {
 1429:             #empty the file for a fresh start
 1430:             &Apache::lonnet::clear_selected_files($env{'user.name'});
 1431:     }
 1432:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
 1433:     my $java_files = join ",", @files;
 1434:     if ($java_files) {
 1435:         $java_files.=',';
 1436:     }
 1437:     my $javascript =(<<ENDSMP);
 1438:         <script type="text/javascript">
 1439:         function finishSelect() {
 1440: ENDSMP
 1441:     $javascript .= 'fileList = "'.$java_files.'";';
 1442:     $javascript .= (<<ENDSMP);
 1443:             for (i=0;i<document.forms.checkselect.length;i++) { 
 1444:                 if (document.forms.checkselect[i].checked){
 1445:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
 1446:                 }
 1447:             }
 1448:             opener.document.forms.lonhomework.$env{'form.fieldname'}.value=fileList;
 1449:             self.close();
 1450:         }
 1451:         </script>
 1452: ENDSMP
 1453:     $r->print($javascript);
 1454:     $r->print("<h1>Select portfolio files</h1>
 1455:                 Check as many as you wish in response to the problem.<br />");
 1456:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
 1457:     if (@otherfiles) {
 1458: 	$r->print("<strong>Files selected from other directories:</strong><br />");
 1459: 	foreach my $file (@otherfiles) {
 1460: 	    $r->print($file."<br />");
 1461: 	}
 1462:     }
 1463: }
 1464: 
 1465: sub upload {
 1466:     my ($r,$url,$group)=@_;
 1467:     my $fname=$env{'form.uploaddoc.filename'};
 1468:     my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
 1469:     my $disk_quota = 20000; # expressed in k
 1470:     if (defined($group)) {
 1471:         my $grp_quota = &get_group_quota($group); # quota expressed in k 
 1472:         if ($grp_quota ne '') {
 1473:             $disk_quota = $grp_quota;
 1474:         } else {
 1475:             $disk_quota = 0;
 1476:         }
 1477:     }
 1478:     $fname=&Apache::lonnet::clean_filename($fname);
 1479: 
 1480:     my $portfolio_root=&get_portfolio_root();
 1481:     my ($uname,$udom) = &get_name_dom();
 1482:     my $port_path = &get_port_path();
 1483:     # Fixme --- Move the checking for existing file to LOND error return
 1484:     my @dir_list=&get_dir_list($portfolio_root);
 1485:     my $found_file = 0;
 1486:     my $locked_file = 0;
 1487:     foreach my $line (@dir_list) {
 1488:         my ($file_name)=split(/\&/,$line,2);
 1489:         if ($file_name eq $fname){
 1490:             $file_name = $env{'form.currentpath'}.$file_name;
 1491:             $file_name = &prepend_group($file_name);
 1492:             $found_file = 1;
 1493:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
 1494:                 $locked_file = 1;
 1495:             } 
 1496:         }
 1497:     }
 1498:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root);
 1499:     if (($current_disk_usage + $filesize) > $disk_quota){
 1500:         $r->print('<span class="LC_error">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes)</strong>. Disk quota will be exceeded.</span>'.
 1501:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
 1502:         $r->print(&done('Back',$url));
 1503:     } 
 1504:     elsif ($found_file){
 1505:         if ($locked_file){
 1506:             $r->print('<span class="LC_error">'.'Unable to upload <strong>'.$fname.'</strong>, a <strong>locked</strong> file by that name was found in <strong>'.$port_path.$env{'form.currentpath'}.'</strong></span>'.
 1507:                   '<br />You will be able to rename or delete existing '.$fname.' after a grade has been assigned.');
 1508:             $r->print(&done('Back',$url));      
 1509:         } else {   
 1510:             $r->print('<span class="LC_error">'.'Unable to upload <strong>'.$fname.'</strong>, a file by that name was found in <strong>'.$port_path.$env{'form.currentpath'}.'</strong></span>'.
 1511:                   '<br />To upload, rename or delete existing '.$fname.' in '.$port_path.$env{'form.currentpath'});
 1512:             $r->print(&done('Back',$url));
 1513:         }
 1514:     } else {
 1515:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
 1516: 	        	 $port_path.$env{'form.currentpath'});
 1517:         if ($result !~ m|^/uploaded/|) {
 1518:             $r->print('<span class="LC_error">'.'An errror occured ('.$result.
 1519: 	              ') while trying to upload '.&display_file().'</span><br />');
 1520: 	    $r->print(&done('Back',$url));
 1521:         } else {
 1522:             $r->print(&done(undef,$url));
 1523:         }
 1524:     }
 1525: }
 1526: 
 1527: sub lock_info {
 1528:     my ($r,$url,$group) = @_;
 1529:     my ($uname,$udom) = &get_name_dom();
 1530:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
 1531:                                                                        $uname);
 1532:     my $file_name = $env{'form.lockinfo'};
 1533:     $file_name = &prepend_group($file_name);
 1534:     if (defined($file_name) && defined($$current_permissions{$file_name})) {
 1535:         foreach my $array_item (@{$$current_permissions{$file_name}}) {
 1536:             if (ref($array_item) eq 'ARRAY') {
 1537:                 my $filetext;
 1538:                 if (defined($group)) {
 1539:                     $filetext = '<strong>'.$env{'form.lockinfo'}.
 1540:                                     '</strong> (group: '.$group.')'; 
 1541:                 } else {
 1542:                     $filetext = '<strong>'.$file_name.'</strong>';
 1543:                 } 
 1544:                 $r->print(&mt('[_1] was submitted in response to problem: ',
 1545:                               $filetext).
 1546:                           '<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
 1547:                           '</strong><br />');
 1548:                 my %course_description = &Apache::lonnet::coursedescription($$array_item[1]);
 1549:                 $r->print(&mt('In the course: <strong>[_1]</strong><br />',
 1550:                               $course_description{'description'}));
 1551:                 # $r->print('the third is '.$$array_item[2].'<br>');
 1552:                 # $r->print("item is $$array_item[0]<br> and $$array_item[0]");
 1553:             }
 1554:         }
 1555:     }
 1556:     $r->print(&done('Back',$url));
 1557:     return 'ok';
 1558: }
 1559: 
 1560: sub createdir {
 1561:     my ($r,$url)=@_;
 1562:     my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
 1563:     if ($newdir eq '') {
 1564:     	$r->print('<span class="LC_error">'.
 1565: 	    	  &mt("Error: no directory name was provided.").
 1566: 		      '</span><br />');
 1567: 	    $r->print(&done(undef,$url));
 1568: 	    return;
 1569:     }
 1570:     my $portfolio_root = &get_portfolio_root(); 
 1571:     my @dir_list=&get_dir_list($portfolio_root);
 1572:     my $found_file = 0;
 1573:     foreach my $line (@dir_list) {
 1574:         my ($filename)=split(/\&/,$line,2);
 1575:         if ($filename eq $newdir){
 1576:             $found_file = 1;
 1577:         }
 1578:     }
 1579:     if ($found_file){
 1580:     	    $r->print('<span class="LC_error">'.'Unable to create a directory named <strong>'.$newdir.
 1581:     	            ' </strong>a file or directory by that name already exists.</span><br />');
 1582:     } else {
 1583:         my ($uname,$udom) = &get_name_dom();
 1584:         my $port_path = &get_port_path();
 1585:         my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
 1586: 	         $port_path.$env{'form.currentpath'}.$newdir);
 1587:         if ($result ne 'ok') {
 1588:     	    $r->print('<span class="LC_error">'.'An errror occured ('.$result.
 1589: 	    	      ') while trying to create a new directory '.&display_file().'</span><br />');
 1590:         }
 1591:     }
 1592:     if ($newdir ne $env{'form.newdir'}) {
 1593:         $r->print("The new directory name was changed from:<br /><strong>".$env{'form.newdir'}."</strong> to <strong>$newdir </strong>");  
 1594:     }
 1595:     $r->print(&done(undef,$url));
 1596: }
 1597: 
 1598: sub get_portfolio_root {
 1599:     my ($uname,$udom) = &get_name_dom();
 1600:     my $path;
 1601:     if (defined($env{'form.group'})) {
 1602:         $path = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
 1603:     } else {
 1604:         $path = '/userfiles/portfolio';
 1605:     }
 1606:     return (&Apache::loncommon::propath($udom,$uname).$path);
 1607: }
 1608: 
 1609: sub get_group_quota {
 1610:     my ($group) = @_;
 1611:     my $group_quota; 
 1612:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1613:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1614:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
 1615:     if (%curr_groups) {
 1616:         my %group_info =  &Apache::longroup::get_group_settings(
 1617:                                                     $curr_groups{$group});
 1618:         $group_quota = $group_info{'quota'}; #expressed in Mb
 1619:         if ($group_quota) {
 1620:             $group_quota = 1000 * $group_quota; #expressed in k
 1621:         } 
 1622:     }
 1623:     return $group_quota;
 1624: } 
 1625: 
 1626: sub get_dir_list {
 1627:     my ($portfolio_root) = @_;
 1628:     my ($uname,$udom) = &get_name_dom();
 1629:     return &Apache::lonnet::dirlist($env{'form.currentpath'},
 1630:                                           $udom,$uname,$portfolio_root);
 1631: }
 1632: 
 1633: sub get_name_dom {
 1634:     my ($uname,$udom);
 1635:     if (defined($env{'form.group'})) {
 1636:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1637:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
 1638:     } else {
 1639:         $udom = $env{'user.domain'};
 1640:         $uname = $env{'user.name'};
 1641:     }
 1642:     return ($uname,$udom);
 1643: }
 1644: 
 1645: sub prepend_group {
 1646:     my ($filename) = @_;
 1647:     if (defined($env{'form.group'})) {
 1648:         $filename = $env{'form.group'}.$filename;
 1649:     }
 1650:     return $filename;
 1651: }
 1652: 
 1653: sub get_namespace {
 1654:     my $namespace = 'portfolio';
 1655:     if (defined($env{'form.group'})) {
 1656:         my ($uname,$udom) = &get_name_dom();
 1657:         $namespace .= '_'.$udom.'_'.$uname.'_'.$env{'form.group'};
 1658:     }
 1659:     return $namespace;
 1660: }
 1661: 
 1662: sub get_port_path {
 1663:     my $port_path;
 1664:     if (defined($env{'form.group'})) {
 1665:        $port_path = "groups/$env{'form.group'}/portfolio";
 1666:     } else {
 1667:        $port_path = 'portfolio';
 1668:     }
 1669:     return $port_path;
 1670: }
 1671: 
 1672: sub missing_priv {
 1673:     my ($r,$url,$priv) = @_;
 1674:     my $longtext = {
 1675:                       upload => 'upload files',
 1676:                       delete => 'delete files',
 1677:                       rename => 'rename files',
 1678:                       setacl => 'set access controls for files',
 1679:                    };
 1680:     my $escpath = &HTML::Entities::encode($env{'form.currentpath'},'&<>"');
 1681:     my $rtnlink = '<a href="'.$url;
 1682:     if ($url =~ /\?/) {
 1683:         $rtnlink .= '&';
 1684:     } else {
 1685:         $rtnlink .= '?';
 1686:     }
 1687:     $rtnlink .= 'currentpath='.$escpath;
 1688:     $r->print(&mt('<h3>Action disallowed</h3>'));
 1689:     $r->print(&mt('You do not have sufficient privileges to [_1] ',
 1690:                   $longtext->{$priv}));
 1691:     if (defined($env{'form.group'})) {
 1692:         $r->print(&mt("in the group's file repository."));
 1693:         $rtnlink .= &group_args()
 1694:     } else {
 1695:         $r->print(&mt('in this portfolio.'));
 1696:     }
 1697:     $rtnlink .= '">'.&mt('Return to directory listing page').'</a>';
 1698:     $r->print('<br />'.$rtnlink);
 1699:     $r->print(&Apache::loncommon::end_page());
 1700:     return;
 1701: }
 1702: 
 1703: sub coursegrp_portfolio_header {
 1704:     my ($cdom,$cnum,$grp_desc)=@_;
 1705:     my $gpterm  = &Apache::loncommon::group_term();
 1706:     my $ucgpterm = $gpterm;
 1707:     $ucgpterm =~ s/^(\w)/uc($1)/e;
 1708:     if ($env{'form.ref'}) {
 1709:         &Apache::lonhtmlcommon::add_breadcrumb
 1710:             ({href=>"/adm/coursegroups",
 1711:               text=>"Groups",
 1712:               title=>"Course Groups"});
 1713:     }
 1714:     &Apache::lonhtmlcommon::add_breadcrumb
 1715:         ({href=>"/adm/$cdom/$cnum/$env{'form.group'}/smppg?ref=$env{'form.ref'}",
 1716:           text=>"$ucgpterm: $grp_desc",
 1717:           title=>"Go to group's home page"},
 1718:          {href=>"/adm/coursegrp_portfolio?".&group_args(),
 1719:           text=>"Group Portfolio",
 1720:           title=>"Display group portfolio"});
 1721:     my $output = &Apache::lonhtmlcommon::breadcrumbs(
 1722:                          &mt('[_1] portfolio files - [_2]',$gpterm,$grp_desc));
 1723:     return $output;
 1724: }
 1725: 
 1726: 
 1727: sub handler {
 1728:     # this handles file management
 1729:     my $r = shift;
 1730:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1731:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
 1732: 	  'fieldname','mode','rename','continue','group','access','setnum',
 1733:           'cnum','cdom','type','setroles','showversions','ref']);
 1734:     my ($uname,$udom,$portfolio_root,$url,$caller,$title,$group,$grp_desc);
 1735:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
 1736:         $url = $1.$2;
 1737:         $caller = $2;
 1738:     }
 1739:     my ($can_modify,$can_delete,$can_upload,$can_setacl);
 1740:     if ($caller eq 'coursegrp_portfolio') {
 1741:     #  Needs to be in a course
 1742:         if (! ($env{'request.course.fn'})) {
 1743:         # Not in a course
 1744:             $env{'user.error.msg'}=
 1745:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
 1746:             return HTTP_NOT_ACCEPTABLE;
 1747:         }
 1748:         my $earlyout = 0;
 1749:         my $view_permission = 
 1750:            &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
 1751:         $env{'form.group'} =~ s/\W//g;
 1752: 	$group = $env{'form.group'};
 1753:         if ($group) {
 1754:             ($uname,$udom) = &get_name_dom();
 1755:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
 1756: 							       $group); 
 1757:             if (%curr_groups) {
 1758:                 my %grp_content = &Apache::longroup::get_group_settings(
 1759:                                                          $curr_groups{$group});
 1760:                 $grp_desc = &unescape($grp_content{'description'});
 1761:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
 1762:                                       $env{'request.course.id'}.'/'.$group))) {
 1763:                     $portfolio_root = &get_portfolio_root();
 1764:                 } else {
 1765:                     $r->print('You do not have the privileges required to access the shared files space for this group.');
 1766:                     $earlyout = 1;
 1767:                 }
 1768:             } else {
 1769:                 $r->print('Not a valid group for this course');
 1770:                 $earlyout = 1;
 1771:             }
 1772:             $title = &mt('Group files').' for '.$group; 
 1773:         } else {
 1774:             $r->print('Invalid group');
 1775:             $earlyout = 1;
 1776:         }
 1777:         if ($earlyout) { return OK; }
 1778:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
 1779:             $can_modify = 1;
 1780:             $can_delete = 1;
 1781:             $can_upload = 1;
 1782:             $can_setacl = 1;
 1783:         } else {
 1784:             if (&Apache::lonnet::allowed('agf',$env{'request.course.id'}.'/'.$group)) {
 1785:                 $can_setacl = 1;
 1786:             }
 1787:             if (&Apache::lonnet::allowed('ugf',$env{'request.course.id'}.'/'.$group)) {
 1788:                 $can_upload = 1;
 1789:             }
 1790:             if (&Apache::lonnet::allowed('mgf',$env{'request.course.id'}.'/'.$group)) {
 1791:                 $can_modify = 1;
 1792:             }
 1793:             if (&Apache::lonnet::allowed('dgf',$env{'request.course.id'}.'/'.$group)) {
 1794:                 $can_delete = 1;
 1795:             }
 1796:         }
 1797:     } else {
 1798:         ($uname,$udom) = &get_name_dom();
 1799:         $portfolio_root = &get_portfolio_root();
 1800:         $title = &mt('Portfolio Manager');
 1801:         $can_modify = 1;
 1802:         $can_delete = 1;
 1803:         $can_upload = 1;
 1804:         $can_setacl = 1;
 1805:     }
 1806: 
 1807:     my $port_path = &get_port_path();
 1808:     &Apache::loncommon::no_cache($r);
 1809:     &Apache::loncommon::content_type($r,'text/html');
 1810:     $r->send_http_header;
 1811:     # Give the LON-CAPA page header
 1812:     if ($env{"form.mode"} eq 'selectfile'){
 1813:         $r->print(&Apache::loncommon::start_page($title,undef,
 1814: 						 {'only_body' => 1}));
 1815:     } elsif ($env{'form.action'} eq 'rolepicker') {
 1816:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
 1817:                                                  {'no_nav_bar'  => 1, }));
 1818:     } else {
 1819:         $r->print(&Apache::loncommon::start_page($title));
 1820:     }
 1821:     $r->rflush();
 1822: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
 1823:    	    $r->print('<span class="LC_error">'.
 1824: 		      'No file was selected to upload.'.
 1825: 		      'To upload a file, click <strong>Browse...</strong>'.
 1826: 		      ', select a file, then click <strong>Upload</strong>.'.
 1827: 		      '</span>');
 1828: 	}
 1829:     if ($env{'form.meta'}) {
 1830:         &open_form($r,$url);
 1831: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
 1832:         $r->print('Edit the meta data<br />');
 1833:         &close_form($r,$url);
 1834:     }
 1835:     if ($env{'form.store'}) {
 1836:     }
 1837: 
 1838:     if ($env{'form.uploaddoc.filename'}) {
 1839:         if ($can_upload) {
 1840: 	    &upload($r,$url,$group);
 1841:         } else {
 1842:             &missing_priv($r,$url,'upload');
 1843:         }
 1844:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
 1845:         if ($can_delete) {
 1846: 	    &delete_confirmed($r,$url);
 1847:         } else {
 1848:             &missing_priv($r,$url,'delete');
 1849:         }
 1850:     } elsif ($env{'form.action'} eq 'delete') {
 1851:         if ($can_delete) {
 1852: 	    &delete($r,$url);
 1853:         } else {
 1854:             &missing_priv($r,$url,'delete');
 1855:         }
 1856:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
 1857:         if ($can_delete) {
 1858: 	    &delete_dir_confirmed($r,$url);
 1859:         } else {
 1860:             &missing_priv($r,$url,'delete');
 1861:         }
 1862:     } elsif ($env{'form.action'} eq 'deletedir') {
 1863:         if ($can_delete) {
 1864: 	    &delete_dir($r,$url);
 1865:         } else {
 1866:             &missing_priv($r,$url,'delete');
 1867:         }
 1868:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
 1869:         if ($can_modify) {
 1870: 	    &rename_confirmed($r,$url);
 1871:         } else {
 1872:             &missing_priv($r,$url,'rename');
 1873:         }
 1874:     } elsif ($env{'form.rename'}) {
 1875:         $env{'form.selectfile'} = $env{'form.rename'};
 1876:         $env{'form.action'} = 'rename';
 1877:         if ($can_modify) {
 1878: 	    &rename($r,$url);
 1879:         } else {
 1880:             &missing_priv($r,$url,'rename');
 1881:         }
 1882:     } elsif ($env{'form.access'}) {
 1883:         $env{'form.selectfile'} = $env{'form.access'};
 1884:         $env{'form.action'} = 'chgaccess';
 1885:         &display_access($r,$url,$group,$can_setacl,$port_path);
 1886:     } elsif ($env{'form.action'} eq 'chgaccess') {
 1887:         if ($can_setacl) {
 1888:             &update_access($r,$url,$group,$port_path);
 1889:         } else {
 1890:             &missing_priv($r,$url,'setacl');
 1891:         }
 1892:     } elsif ($env{'form.action'} eq 'rolepicker') {
 1893:         if ($can_setacl) { 
 1894:             &role_options_window($r);
 1895:         } else {
 1896:             &missing_priv($r,$url,'setacl');
 1897:         }
 1898:     } elsif ($env{'form.createdir'}) {
 1899:         if ($can_upload) {
 1900: 	    &createdir($r,$url);
 1901:         } else {
 1902:             &missing_priv($r,$url,'upload');
 1903:         }
 1904:     } elsif ($env{'form.lockinfo'}) {
 1905:         &lock_info($r,$url,$group);
 1906:     } else {
 1907: 	my $current_path='/';
 1908: 	if ($env{'form.currentpath'}) {
 1909: 	    $current_path = $env{'form.currentpath'};
 1910: 	}
 1911:         if ($caller eq 'coursegrp_portfolio') {
 1912:             &Apache::lonhtmlcommon::clear_breadcrumbs();
 1913:             $r->print(&coursegrp_portfolio_header($udom,$uname,$grp_desc));
 1914:         }
 1915:         my @dir_list=&get_dir_list($portfolio_root);
 1916: 	if ($dir_list[0] eq 'no_such_dir'){
 1917: 	    # two main reasons for this:
 1918:             #    1) never been here, so directory structure not created
 1919: 	    #    2) back-button navigation after deleting a directory
 1920: 	    if ($current_path eq '/'){
 1921: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
 1922: 					       &get_port_path());
 1923: 	    } else {
 1924:                 # some directory that snuck in get rid of the directory
 1925:                 # from the recent pulldown, just in case
 1926: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
 1927: 						      [$current_path]);
 1928: 		$current_path = '/'; # force it back to the root        
 1929: 	    }
 1930: 	    # now grab the directory list again, for the first time
 1931: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
 1932: 					    $udom,$uname,$portfolio_root);
 1933:         }
 1934: 	# need to know if directory is empty so it can be removed if desired
 1935: 	my $is_empty=(@dir_list == 2);
 1936: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,
 1937: 			$can_upload);
 1938:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group,
 1939:                            $can_upload,$can_modify,$can_delete,$can_setacl);
 1940: 	$r->print(&Apache::loncommon::end_page());
 1941:     }
 1942:     return OK;
 1943: }
 1944: 
 1945: 1;
 1946: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>