File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.136: download - view: text, annotated - select for diffs
Wed Jul 19 11:43:47 2006 UTC (17 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Breadcrumbs for group portfolio files.  Link to all groups listing imcluded if originally accessed via 'Groups' link in inline menu, or GRPS button on remote.

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

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