File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.223: download - view: text, annotated - select for diffs
Mon Apr 12 17:27:24 2010 UTC (14 years, 2 months ago) by bisitz
Branches: MAIN
CVS tags: PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD
- Optimized portfolio file list:
Also display header for empty directories and show empty message.
This should avoid confusion about a screen with many functions but no portfolio files.
- Optimized spacing for "Portfolio FileList" help link
- Optimized "Delete Directory" button:
    - Moved button from above to below the list, right below the empty message (see above)
    - Removed unnecessary variable

    1: # The LearningOnline Network
    2: # portfolio browser
    3: #
    4: # $Id: portfolio.pm,v 1.223 2010/04/12 17:27:24 bisitz 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 qw(:DEFAULT :match);
   42: 
   43: sub group_args {
   44:     my $output;
   45:     if (defined($env{'form.group'})) {
   46:         $output .= '&group='.$env{'form.group'};
   47: 	if (defined($env{'form.ref'})) {
   48: 	    $output .= '&ref='.$env{'form.ref'};
   49: 	}
   50:     }
   51:     return $output;
   52: }
   53: 
   54: sub group_form_data {
   55:     my $output;
   56:     if (defined($env{'form.group'})) {
   57: 	$output = '<input type="hidden" name="group" value="'.$env{'form.group'}.'" />';
   58: 	if (exists($env{'form.ref'})) {
   59: 	    $output .= '<input type="hidden" name="ref" value="'.
   60: 		$env{'form.ref'}.'" />';
   61: 	}
   62:     }
   63:     return $output;
   64: } 
   65: 
   66: # receives a file name and path stub from username/userfiles/portfolio/
   67: # returns an anchor tag consisting encoding filename and currentpath
   68: sub make_anchor {
   69:     my ($url, $anchor_fields, $inner_text) = @_;
   70:     if ($$anchor_fields{'continue'} ne 'true') {$$anchor_fields{'continue'} = 'false'};
   71:     my $anchor = '<a href="'.$url.'?';
   72:     foreach my $field_name (keys(%$anchor_fields)) {
   73:         $anchor .= $field_name.'='.$$anchor_fields{$field_name}.'&amp;';
   74:     }
   75:     $anchor =~ s/&amp;$//;
   76:     $anchor .= &group_args();
   77:     $anchor .= '">'.$inner_text.'</a>';
   78:     return $anchor;
   79: }
   80: 
   81: my $dirptr=16384;
   82: sub display_common {
   83:     my ($r,$url,$current_path,$is_empty,$dir_list,$can_upload,$group)=@_;
   84:     my $namespace = &get_namespace();
   85:     my $port_path = &get_port_path();
   86:     if ($can_upload) {
   87:         my $groupitem = &group_form_data();
   88: 
   89:         my $iconpath= $r->dir_config('lonIconsURL') . "/";
   90:         my %lt=&Apache::lonlocal::texthash(
   91:                    'upload'          => 'Upload',
   92:                    'upload_label'    => 'Upload file to current directory',
   93:                    'createdir'       => 'Create Subdirectory',
   94:                    'createdir_label' => 'Create subdirectory in current directory',
   95:                    'parse'           => 'Upload embedded images/multimedia/css/linked files if HTML file',
   96:                );
   97:         my $escuri = &HTML::Entities::encode($r->uri,'&<>"');
   98: 	my $help_fileupload = &Apache::loncommon::help_open_topic('Portfolio AddFiles');
   99: 	my $help_createdir = &Apache::loncommon::help_open_topic('Portfolio CreateDirectory');
  100:         my $help_portfolio = &Apache::loncommon::help_open_topic('Portfolio About', &mt('Help on the portfolio'));
  101:         $r->print(&display_usage($group));
  102:         my $parse_check;
  103:         if (!&suppress_embed_prompt()) {
  104:             $parse_check = <<"END";
  105:         <br />
  106:         <span class="LC_nobreak">
  107:          <label>
  108:           <input type="checkbox" name="parserflag" checked="checked" />
  109:           $lt{'parse'}
  110:          </label>
  111:         </span>
  112: END
  113:         }
  114: 
  115:         $r->print('<div>'.$help_portfolio);
  116:         # Upload File
  117:         $r->print('<div class="LC_left_float">'
  118:                  .'<form method="post" enctype="multipart/form-data" action="'.$escuri.'">'
  119:                  .'<fieldset>'
  120:                  .'<legend>'.$lt{'upload_label'}.'</legend>'
  121:                  .$groupitem 
  122:                  .'<input name="uploaddoc" type="file" />'
  123:                  .'<input type="hidden" name="currentpath" value="'.$current_path.'" />'
  124:                  .'<input type="hidden" name="action" value="'.$env{"form.action"}.'" />'
  125:                  .'<input type="hidden" name="fieldname" value="'.$env{"form.fieldname"}.'" />'
  126:                  .'<input type="hidden" name="mode" value="'.$env{"form.mode"}.'" />'
  127:                  .'<input type="submit" name="storeupl" value="'.$lt{'upload'}.'" />'
  128:                  .$help_fileupload
  129:                  .$parse_check
  130:                  .'</fieldset>'
  131:                  .'</form>'
  132:                  .'</div>'
  133:         );
  134:         # Create Subdirectory
  135:         $r->print('<div class="LC_left_float">'
  136:                  .'<form method="post" action="'.$escuri.'">'
  137:                  .'<fieldset>'
  138:                  .'<legend>'.$lt{'createdir_label'}.'</legend>'
  139:                  .'<input name="newdir" type="text" />'.$groupitem
  140:                  .'<input type="hidden" name="currentpath" value="'.$current_path.'" />'
  141:                  .'<input type="hidden" name="action" value="'.$env{"form.action"}.'" />'
  142:                  .'<input type="hidden" name="fieldname" value="'.$env{"form.fieldname"}.'" />'
  143:                  .'<input type="hidden" name="mode" value="'.$env{"form.mode"}.'" />'
  144:                  .'<input type="submit" name="createdir" value="'.$lt{'createdir'}.'" />'
  145:                  .$help_createdir
  146:                  .'</fieldset>'
  147:                  .'</form>'
  148:                  .'</div>'
  149:         );
  150:         $r->print('</div>');
  151:     } # end "if can_upload"
  152: 
  153:     my @tree = split (/\//,$current_path);
  154:     my %anchor_fields = (
  155:         'selectfile'    => $port_path,
  156:         'currentpath'   => '/',
  157:         'mode'          => $env{"form.mode"},
  158:         'fieldname'     => $env{"form.fieldname"},
  159:         'continue'      => $env{"form.continue"}
  160:     );
  161:     $r->print('<br clear="all" />');
  162:     $r->print('<span class="LC_current_location">'.&make_anchor($url,\%anchor_fields,$port_path).'/');
  163:     if (@tree > 1){
  164:         my $newCurrentPath = '/';
  165:         for (my $i = 1; $i< @tree; $i++){
  166:             $newCurrentPath .= $tree[$i].'/';
  167:             my %anchor_fields = (
  168:                 'selectfile' => $tree[$i],
  169:                 'currentpath' => $newCurrentPath,
  170:                 'mode' => $env{"form.mode"},
  171:                 'fieldname' => $env{"form.fieldname"},
  172:                 'continue' => $env{"form.continue"}
  173:             );
  174:             $r->print(&make_anchor($url,\%anchor_fields,$tree[$i]).'/');
  175:         }
  176:     }
  177:     $r->print('</span>');
  178:     $r->print(&Apache::loncommon::help_open_topic('Portfolio ChangeDirectory'));
  179:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
  180:     $r->print('<br /><form method="post" action="'.$url.'?mode='.$env{"form.mode"}.'&amp;fieldname='.$env{"form.fieldname"}.&group_args());
  181:     $r->print('">'.
  182: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
  183: 						    'this.form.submit();'));
  184:     $r->print("</form>");
  185: }
  186: 
  187: sub display_usage {
  188:     my ($group) = @_;
  189:     my $disk_quota = &get_quota($group);
  190:     my $getpropath = 1;
  191:     my $portfolio_root = &get_portfolio_root();
  192:     my ($uname,$udom) = &get_name_dom($group);
  193:     my $current_disk_usage =
  194:          &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
  195:     my $usage = $current_disk_usage/1000;
  196:     my $quota = $disk_quota/1000;
  197:     my $percent;
  198:     if ($disk_quota == 0) {
  199:         $percent = 100.0;
  200:     } else {
  201:         $percent = 100*($current_disk_usage/$disk_quota);
  202:     }
  203:     $usage = sprintf("%.2f",$usage);
  204:     $quota = sprintf("%.2f",$quota);
  205:     $percent = sprintf("%.0f",$percent);
  206:     my ($color,$cssclass);
  207:     if ($percent <= 60) {
  208:         $color = '#00A000';
  209:     } elsif ($percent > 60 && $percent < 90) {
  210:         $color = '#FFD300';
  211:         $cssclass = 'class="LC_warning"';
  212:     } elsif( $percent >= 90) {
  213:         $color = '#FF0000';
  214:         $cssclass = 'class="LC_error"';
  215:     }
  216:     my $prog_width = $percent;
  217:     if ($prog_width > 100) {
  218:         $prog_width = 100;
  219:     }
  220:     my $disk_meter = '
  221:   <div id="meter1" align="left" '.$cssclass.'>'.&mt('Currently using [_1] of the [_2] available.',$usage.' MB <span style="font-weight:bold;">('.$percent.'%)</span>',$quota.' MB')."\n".
  222: '   <div id="meter2" style="display:block; margin-top:5px; margin-bottom:5px; margin-left:0px; margin-right:0px; width:400px; border:1px solid #000000; height:10px;">'."\n".
  223: '    <div id="meter3" style="display:block; background-color:'.$color.'; width:'.$prog_width.'%; height:10px; color:#000000; margin:0px;"></div>'."\n".
  224: '   </div>'."\n".
  225: '  </div>';
  226:     return &Apache::loncommon::head_subbox($disk_meter);
  227: }
  228: 
  229: sub display_directory_line {
  230:     my ($r,$select_mode, $filename, $mtime, $size, $css_class,
  231: 	$line, $access_controls, $curr_access, $now, $version_flag,
  232: 	$href_location, $url, $current_path, $access_admin_text, $versions)=@_;
  233: 
  234:     my $fullpath =  &prepend_group($current_path.$filename);
  235:     $r->print(&Apache::loncommon::start_data_table_row());
  236:     $r->print($line); # contains first two cells of table
  237:     my $lock_info;
  238:     if ($version_flag) { # versioned can't be versioned, so TRUE when root file
  239:         $r->print('<td><img alt="" src="'.&Apache::loncommon::icon($filename).'" class="LC_fileicon" /></td>');
  240:         $r->print('<td>'.$version_flag.'</td>');
  241:     } else { # this is a graded or handed back file
  242:         my ($user,$domain) = &get_name_dom($env{'form.group'});
  243:         my $permissions_hash = &Apache::lonnet::get_portfile_permissions($domain,$user);
  244:         if (defined($$permissions_hash{$fullpath})) {
  245:             foreach my $array_item (@{$$permissions_hash{$fullpath}}) {
  246:                 if (ref($array_item) eq 'ARRAY') {
  247:                     if ($$array_item[-1] eq 'handback') {
  248:                         $lock_info = 'Handback';
  249:                     } elsif ($$array_item[-1] eq 'graded') {
  250:                         $lock_info = 'Graded';
  251:                     }
  252:                  }
  253:             }
  254:         }
  255: 	if ($lock_info) {
  256: 	    my %anchor_fields = ('lockinfo' => $fullpath);
  257: 	    if ($versions) { # hold the folder open
  258: 	        my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($fullpath);
  259: 	        $fname =~ s|^/||;
  260: 	        $anchor_fields{'showversions'} = $fname.'.'.$extension;
  261: 	    }
  262: 	    $lock_info = &make_anchor(undef,\%anchor_fields,$lock_info);
  263: 	}
  264: 	$r->print('<td colspan="2">'.$lock_info.'</td>');
  265:     }
  266:     # $r->print('<td>'.$$version_flag{$filename}.'</td><td>');
  267:     $r->print('<td>'.&make_anchor($href_location.$filename,undef,$filename).'</td>'); 
  268:     $r->print('<td>'.$size.'</td>');
  269:     $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
  270:     if ($select_mode ne 'true') {
  271:         $r->print('<td class="'.$css_class.'">&nbsp;&nbsp;</td>'); # Display status
  272:         $r->print('<td><span class="LC_nobreak">'
  273:                  .&mt($curr_access).'&nbsp;&nbsp;&nbsp;'
  274:        );
  275:         my %anchor_fields = (
  276:             'access' => $filename,
  277:             'currentpath' => $current_path
  278:         );
  279: 	$r->print(&make_anchor($url, \%anchor_fields, $access_admin_text).'</span></td>');
  280:     } else {
  281:         $r->print('<td class="'.$css_class.'">&nbsp;&nbsp;</td>'); # Display status
  282:     }
  283:     $r->print(&Apache::loncommon::end_data_table_row().$/);
  284: }
  285: 
  286: sub display_directory {
  287:     my ($r,$url,$current_path,$is_empty,$dir_list,$group,$can_upload,
  288:         $can_modify,$can_delete,$can_setacl)=@_;
  289:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
  290:     my $select_mode;
  291:     my $checked_files;
  292:     my $port_path = &get_port_path();
  293:     my ($uname,$udom) = &get_name_dom($group);
  294:     my $access_admin_text = &mt('View Status');
  295:     if ($can_setacl) {
  296:         $access_admin_text = &mt('View/Change Status');
  297:     }
  298: 
  299:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  300:                                                                         $uname);
  301:     my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash(
  302:                                                   $current_permissions,$group);
  303:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group);
  304:     my $now = time;
  305:     if ($env{"form.mode"} eq 'selectfile') {
  306:         &select_files($r);
  307:         $checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
  308:         $select_mode = 'true';
  309:     }
  310:     if ($select_mode eq 'true') {
  311:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
  312:         $r->print(&Apache::loncommon::start_data_table()
  313:                  .&Apache::loncommon::start_data_table_header_row()
  314:                  .'<th>'.&mt('Select').'</th>'
  315:                  .'<th>&nbsp;</th>'
  316:                  .'<th>&nbsp;</th>'
  317:                  .'<th>'.&mt('Name').'</th>'
  318:                  .'<th>'.&mt('Size').'</th>'
  319:                  .'<th>'.&mt('Last Modified').'</th>'
  320:                  .'<th>&nbsp;</th>'
  321:                  .&Apache::loncommon::end_data_table_header_row()
  322:         );
  323:     } else {
  324:         $r->print('<form method="post" action="'.$url.'">');
  325:         $r->print(
  326:             '<p>'
  327:            .&Apache::loncommon::help_open_topic(
  328:                 'Portfolio FileList',
  329:                 &mt('Using the portfolio file list'))
  330:            .'</p>'
  331:         );
  332:         $r->print(&Apache::loncommon::start_data_table()
  333:                  .&Apache::loncommon::start_data_table_header_row()
  334:                  .'<th colspan="2">'.&mt('Actions'). &Apache::loncommon::help_open_topic('Portfolio FileAction').'</th>'
  335:                  .'<th>&nbsp;</th>'
  336:                  .'<th>&nbsp;</th>'
  337:                  .'<th>'.&mt('Name').&Apache::loncommon::help_open_topic('Portfolio OpenFile').'</th>'
  338:                  .'<th>'.&mt('Size').'</th>'
  339:                  .'<th>'.&mt('Last Modified').'</th>'
  340:                  .'<th>&nbsp;</th>'
  341:                  .'<th>'.&mt('Current Access Status').&Apache::loncommon::help_open_topic('Portfolio ShareFile').'</th>'
  342:                  .&Apache::loncommon::end_data_table_header_row());
  343:     }
  344: 
  345:     # Empty directory?
  346:     if ($is_empty && ($current_path ne '/') && $can_delete) {
  347:         my $cols = ($select_mode eq 'true') ? 7 : 9;
  348:         # Empty message
  349:         $r->print(
  350:             &Apache::loncommon::start_data_table_row()
  351:            .'<td colspan="'.$cols.'">'
  352:            .'<p class="LC_info">'
  353:            .&mt('This directory is empty.')
  354:            .'</p>'
  355:            .'</td>'
  356:            .&Apache::loncommon::end_data_table_row()
  357:            .&Apache::loncommon::end_data_table()
  358:            .'</form>'
  359:         );
  360:         # Delete button
  361:         $r->print(
  362:             '<form method="post" action="'.$url.'">'.
  363:             &group_form_data().
  364:             '<input type="hidden" name="action" value="deletedir" />'.
  365:             '<p>'.
  366:             '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
  367:             '</p>'.
  368:             '<input type="hidden" name="selectfile" value="" />'.
  369:             '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
  370:             '</form>'
  371:         );
  372:         # Directory is empty, so nothing else to display
  373:         return;
  374:     }
  375: 
  376:     $r->print("\n".&group_form_data()."\n");
  377: 
  378:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
  379:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
  380:     my @dir_lines;
  381:     my %versioned;
  382:     foreach my $dir_line (sort 
  383: 		      { 
  384: 			  my ($afile)=split('&',$a,2);
  385: 			  my ($bfile)=split('&',$b,2);
  386: 			  return (lc($afile) cmp lc($bfile));
  387: 		      } (@$dir_list)) {
  388:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$dir_line,16); 
  389:     	$filename =~ s/\s+$//;
  390:     	my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
  391:     	if ($version) {
  392: 	    my $fullpath = &prepend_group($current_path.$fname.'.'.$extension);
  393:     	    push(@{ $versioned{$fullpath} },
  394: 		 [$filename,$dom,$testdir,$size,$mtime,$obs,]);
  395:     	} else {
  396:     	    push(@dir_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
  397:     	}
  398:     }
  399:     foreach my $dir_line (@dir_lines) {
  400:         my ($filename,$dom,$testdir,$size,$mtime,$obs) = @$dir_line;
  401:         my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
  402:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
  403:     	    my $version_flag;
  404:     	    my $show_versions;
  405: 	    my $fullpath =  &prepend_group($current_path.$filename);
  406:     	    if ($env{'form.showversions'} =~ /$filename/) {
  407:     	        $show_versions = 'true';
  408:     	    }
  409:     	    if (exists($versioned{$fullpath})) {
  410:     	        my %anchor_fields = (
  411:     	            'selectfile' => $fullpath,
  412:     	            'continue' => 'false',
  413:     	            'currentpath' => $current_path,
  414:     	        );
  415:     	        if ($show_versions) {
  416:     	            # Must preserve other possible showversion files
  417:     	            my $version_remainder = $env{'form.showversions'};
  418:     	            $version_remainder =~ s/$filename//g;    	            
  419:     	            $anchor_fields{'showversions'} = $version_remainder;
  420:                     $version_flag = &make_anchor('portfolio',\%anchor_fields,
  421:                         '<img class="LC_icon" alt="'.&mt('opened folder').'" src="'.$iconpath.'folder_pointer_opened.gif" />');
  422:     	        } else {
  423:     	            # allow multiple files to show versioned
  424:     	            $anchor_fields{'showversions'} = $env{'form.showversions'}.','.$filename;
  425:                     $version_flag = &make_anchor('portfolio',\%anchor_fields,
  426:                         '<img class="LC_icon" alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_pointer_closed.gif" />');
  427:                 }
  428:     	    } else {
  429:     	        $version_flag = '&nbsp;';
  430:     	    }
  431:             if ($dirptr&$testdir) {
  432: 		my $colspan_folder='';
  433: 		my $colspan_fill='';
  434:                 if ($select_mode eq 'true'){
  435:                     $colspan_fill=' colspan="3"';
  436:                 } else {
  437:                     $colspan_folder=' colspan="2"';
  438:                     $colspan_fill=' colspan="4"';
  439:                 }
  440: 		$r->print('<tr class="LC_browser_folder">');
  441:                 $r->print('<td'.$colspan_folder.'><img alt="'.&mt('closed folder').'" src="'.$iconpath.'navmap.folder.closed.gif" class="LC_fileicon" /></td>'
  442:                          .'<td>'.&mt('Go to ...').'</td>');
  443:                 my %anchor_fields = (
  444:                     'selectfile'    => $filename.'/',
  445:                     'currentpath'   => $current_path.$filename.'/',
  446:                     'mode'          => $env{"form.mode"},
  447:                     'fieldname'     => $env{"form.fieldname"},
  448:                     'continue'      => $env{"form.continue"}
  449:                 );  
  450:                 $r->print('<td>'.$version_flag.'</td>'
  451:                          .'<td>'.&make_anchor($url,\%anchor_fields,$filename.'/').'</td>'); 
  452:                 $r->print('<td'.$colspan_fill.'>&nbsp;</td>');
  453:                 $r->print('</tr>'); 
  454:             } else {
  455: 		my $css_class = 'LC_browser_file';
  456: 		my $line;
  457:                 if ($select_mode eq 'true') {
  458:                     $line='<td><input type="checkbox" name="checkfile" value="'.$filename.'"';
  459: 		    if ($$checked_files{$filename} eq 'selected') {
  460:                         $line.=' checked="checked" ';
  461:                     }
  462: 		    $line.=' /></td>';
  463:                 } else {
  464:                     if (exists $locked_files{$fullpath}) {
  465:                         my %anchor_fields = (
  466:                             'lockinfo' => $fullpath
  467:                         );
  468:                         $line.='<td colspan="2">'.&make_anchor($url,\%anchor_fields,&mt('Locked')).'</td>';
  469: 			$css_class= 'LC_browser_file_locked';
  470:                     } else {
  471:                         if (!$can_modify) {
  472:                             $line .= '<td colspan="2">';
  473:                         } else {
  474:                             $line .= '<td>';
  475:                         }
  476:                         if ($can_delete) {
  477:                             $line .= '<input type="checkbox" name="selectfile" value="'.$filename.'" />';
  478:                         }
  479:                         if ($can_modify) {
  480:                             my $cat='<img class="LC_icon" alt="'.&mt('Metadata').'" title="'.&mt('Metadata').'" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.png').'" />';
  481:                             my %anchor_fields = (
  482:                                 'rename' => $filename,
  483:                                 currentpath => $current_path
  484:                             );
  485:                             $line .= &make_anchor($url,\%anchor_fields,&mt('Rename'));
  486:                             $line .= '</td><td>'.&make_anchor($href_edit_location.$filename.'.meta',\%anchor_fields,$cat);
  487:                             # '<a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>';
  488:                         }
  489:                         $line .= '</td>';
  490:                     }
  491:                 }
  492: 		my $curr_access;
  493: 		if ($select_mode ne 'true') {
  494: 		    my $pub_access = 0;
  495: 		    my $guest_access = 0;
  496: 		    my $cond_access = 0;
  497: 		    foreach my $key (sort(keys(%{$access_controls{$fullpath}}))) {
  498: 			my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  499: 			if (($now > $start) && (!$end || $end > $now)) {
  500: 			    if ($scope eq 'public')  {
  501: 				$pub_access = 1;
  502: 			    } elsif ($scope eq 'guest') {
  503: 				$guest_access = 1;
  504: 			    } else {
  505: 				$cond_access = 1;
  506: 			    }
  507: 			}
  508: 		    }
  509: 		    if (!$pub_access && !$guest_access && !$cond_access) {
  510: 			$curr_access = &mt('Private');
  511: 		    } else {
  512: 			my @allaccesses; 
  513: 			if ($pub_access) {
  514: 			    push(@allaccesses,&mt('Public'));
  515: 			}
  516: 			if ($guest_access) {
  517: 			    push(@allaccesses,&mt('Passphrase-protected'));
  518: 			}
  519: 			if ($cond_access) {
  520: 			    push(@allaccesses,&mt('Conditional'));
  521: 			}
  522: 			$curr_access = join('+ ',@allaccesses);
  523: 		    }
  524: 		}
  525:                 &display_directory_line($r,$select_mode, $filename, $mtime, $size, $css_class, $line, 
  526:                                         \%access_controls, $curr_access,$now, $version_flag, $href_location, 
  527:                                         $url, $current_path, $access_admin_text);
  528: 		if ($show_versions) {
  529: 		    foreach my $dir_line (@{ $versioned{$fullpath} }) {
  530: 		        my ($v_filename,$dom,$testdir,$size,$mtime,$obs) =
  531: 			    @$dir_line;
  532:                         $line = '<td colspan="2">&nbsp;</td>';
  533: 			&display_directory_line($r,$select_mode, $v_filename, $mtime, $size, 
  534: 						$css_class, $line, \%access_controls, $curr_access, $now,
  535: 						undef, $href_location, $url, $current_path, $access_admin_text, 1);
  536: 		    }
  537: 		}
  538:             }
  539:         }
  540:     }
  541:     if ($select_mode eq 'true') {
  542:         $r->print(&Apache::loncommon::end_data_table().'
  543:             <input type="hidden" name="continue" value="true" />
  544:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'" />
  545:             <input type="hidden" name="mode" value="selectfile" />
  546:             <p>
  547:             <input type="submit" name="submit" value="'.&mt('Select checked files, and continue selecting').'" /><br />
  548:             <input type="button" name="doit" onClick="finishSelect();" value="'.&mt('Select checked files, and close window').'" />
  549:             </p>
  550:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
  551:         </form>');        
  552:     } else {
  553:         $r->print(&Apache::loncommon::end_data_table());
  554:         if ($can_delete) {
  555:             $r->print('
  556:         <p>
  557:         <input type="submit" name="doit" value="'.&mt('Delete Selected').'" />'.
  558: 	&Apache::loncommon::help_open_topic('Portfolio DeleteFile').'
  559:         </p>
  560:         <input type="hidden" name="action" value="delete" />
  561:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
  562:         </form>'
  563:             );
  564:         }
  565:     }
  566: }
  567: 
  568: sub open_form {
  569:     my ($r,$url)=@_;
  570:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  571:     $r->print('<form name="portform" method="post" action="'.$url.'">');
  572:     $r->print('<input type="hidden" name="action" value="'.
  573: 	      $env{'form.action'}.'" />');
  574:     $r->print('<input type="hidden" name="confirmed" value="1" />');
  575:     foreach (@files) {
  576:         $r->print('<input type="hidden" name="selectfile" value="'.
  577: 	      $_.'" />');
  578:     }
  579:     $r->print('<input type="hidden" name="currentpath" value="'.
  580: 	      $env{'form.currentpath'}.'" />');
  581: }
  582: 
  583: sub close_form {
  584:     my ($r,$url,$button_text)=@_;
  585:     if (!defined($button_text)) {
  586:         $button_text = {
  587:                          'continue' => &mt('Continue'),
  588:                          'cancel'   => &mt('Cancel'),
  589:                        };
  590:     }
  591:     $r->print('<p><input type="submit" value="'.$button_text->{'continue'}.'" />');
  592:     $r->print(&group_form_data().'</p></form>');
  593:     $r->print('<form action="'.$url.'" method="post">
  594:                <p>
  595:               <input type="hidden" name="currentpath" value="'.
  596: 	      $env{'form.currentpath'}.'" />'.
  597: 	      &group_form_data());
  598:     $r->print("\n".'   <input type="submit" value="'.$button_text->{'cancel'}.'" />
  599:                </p></form>'); 
  600: }
  601: 
  602: sub display_file {
  603:     my ($path,$filename)=@_;
  604:     my $display_file_text;
  605:     my $file_start='<span class="LC_filename">';
  606:     my $file_end='</span>';
  607:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
  608:     if (!defined($filename)) { 
  609:         $filename=$env{'form.selectfile'};
  610:         $display_file_text = $file_start.$path.$filename.$file_end;
  611:     } elsif (ref($filename) eq "ARRAY") {
  612:         foreach my $file (@$filename) {
  613:             $display_file_text .= $file_start.$path.$file.$file_end.'<br />';
  614:         }
  615:     } elsif (ref($filename) eq "SCALAR") {
  616:         $display_file_text = $file_start.$path.$$filename.$file_end;
  617:     } else {
  618: 	$display_file_text = $file_start.$path.$filename.$file_end;
  619:     }
  620:     return $display_file_text;
  621: }
  622: 
  623: sub done {
  624:     my ($message,$url)=@_;
  625:     unless (defined $message) {
  626:         $message='Done';
  627:     }
  628:     my %anchor_fields = (
  629:         'showversions' => $env{'form.showversions'},
  630:         'currentpath' => $env{'form.currentpath'},
  631:         'fieldname' => $env{'form.fieldname'},
  632:         'mode'      => $env{'form.mode'}
  633:     );
  634:     my $result = '<h3>'.&make_anchor($url,\%anchor_fields,&mt($message)).'</h3>';
  635:     return $result;
  636: }
  637: 
  638: sub delete {
  639:     my ($r,$url,$group)=@_;
  640:     my @check;
  641:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  642:     $file_name = &prepend_group($file_name);
  643:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  644:     my ($uname,$udom) = &get_name_dom($group);
  645:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  646:         $r->print(&mt('The file is locked and cannot be deleted.').'<br />');
  647:         $r->print(&done('Back',$url));
  648:     } else {
  649:         if (scalar(@files)) {
  650:             &open_form($r,$url);
  651:             $r->print('<p>'.&mt('Delete [_1]?',&display_file(undef,\@files)).'</p>');
  652:             &close_form($r,$url);
  653:         } else {
  654:             $r->print("No file was checked to delete.<br />");
  655:             $r->print(&done(undef,$url));
  656:         }
  657:     }
  658: } 
  659: 
  660: sub delete_confirmed {
  661:     my ($r,$url,$group)=@_;
  662:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  663:     my $result;
  664:     my ($uname,$udom) = &get_name_dom($group);
  665:     my $port_path = &get_port_path();
  666:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  667:                                                                         $uname);
  668:     foreach my $delete_file (@files) {
  669:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  670: 					       $env{'form.currentpath'}.
  671: 					       $delete_file);
  672:         if ($result ne 'ok') {
  673: 	    $r->print('<span class="LC_error">'
  674:                       .&mt('An error occurred ([_1]) while trying to delete [_2].'
  675:                          ,$result,&display_file(undef, $delete_file))
  676:                       .'</span><br /><br />');
  677:         } else {
  678:             $r->print(&mt('File: [_1] deleted.',
  679:                           &display_file(undef,$delete_file)));
  680:             my $file_name = $env{'form.currentpath'}.$delete_file;
  681:             $file_name = &prepend_group($file_name);
  682:             my %access_controls = 
  683:                     &Apache::lonnet::get_access_controls($current_permissions,
  684:                                                          $group,$file_name);
  685:             if (keys(%access_controls) > 0) {
  686:                 my %changes; 
  687:                 foreach my $key (keys(%{$access_controls{$file_name}})) {
  688:                     $changes{'delete'}{$key} = 1;
  689:                 }
  690:                 if (keys(%changes) > 0) {
  691:                     my ($outcome,$deloutcome,$new_values,$translation) =
  692:                     &Apache::lonnet::modify_access_controls($file_name,\%changes,
  693:                                                             $udom,$uname);
  694:                     if ($outcome ne 'ok') {
  695:                            $r->print('<br />'.&mt("An error occurred ([_1]) while ".
  696:                                "trying to delete access controls for the file.",$outcome).
  697:                                '</span><br /><br />');
  698:                     } else {
  699:                         if ($deloutcome eq 'ok') {
  700:                             $r->print('<br />'.&mt('Access controls also deleted for the file.').'<br /><br />');
  701:                         } else {
  702:                             $r->print('<span class="LC_error">'.'<br />'.
  703:                                &mt("An error occurred ([_1]) while ".
  704:                                    "trying to delete access controls for the file.",$deloutcome).
  705:                                    '</span><br /><br />');
  706:                         }
  707:                     }
  708:                 }
  709:             }
  710:         }
  711:     }
  712:     $r->print(&done(undef,$url));
  713: }
  714: 
  715: sub delete_dir {
  716:     my ($r,$url)=@_;
  717:     &open_form($r,$url);
  718:      $r->print('<p>'.&mt('Delete [_1]?',&display_file()).'</p>');
  719:     &close_form($r,$url);
  720: } 
  721: 
  722: sub delete_dir_confirmed {
  723:     my ($r,$url,$group)=@_;
  724:     my $directory_name = $env{'form.currentpath'};
  725:     $directory_name =~ s|/$||; # remove any trailing slash
  726:     my ($uname,$udom) = &get_name_dom($group);
  727:     my $namespace = &get_namespace();
  728:     my $port_path = &get_port_path();
  729:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  730: 					       $directory_name);
  731: 					       
  732:     if ($result ne 'ok') {
  733: 	$r->print('<span class="LC_error">'
  734:                   .&mt('An error occurred (dir) ([_1]) while trying to delete [_2].'
  735:                        ,$result,$directory_name)
  736:                   .'</span><br />');
  737:     } else {
  738:         # now remove from recent
  739:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
  740:         my @dirs = split m!/!, $directory_name;
  741:         $directory_name='/';
  742:         for (my $i=1; $i < (@dirs - 1); $i ++){
  743:             $directory_name .= $dirs[$i].'/';
  744:         }
  745:         $env{'form.currentpath'} = $directory_name;
  746:     }
  747:     $r->print(&done(undef,$url));
  748: }
  749: 
  750: sub rename {
  751:     my ($r,$url,$group)=@_;
  752:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
  753:     my ($uname,$udom) = &get_name_dom($group);
  754:     $file_name = &prepend_group($file_name);
  755:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  756:         $r->print("The file is locked and cannot be renamed.<br />");
  757:         $r->print(&done(undef,$url));
  758:     } else {
  759:         &open_form($r,$url);
  760:         $r->print('<p>'.&mt('Rename [_1] to [_2]?', &display_file()
  761:                   , '<input name="filenewname" type="text" size="50" />').'</p>');
  762:         &close_form($r,$url);
  763:     }
  764: }
  765: 
  766: sub rename_confirmed {
  767:     my ($r,$url,$group)=@_;
  768:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
  769:     my ($uname,$udom) = &get_name_dom($group);
  770:     my $port_path = &get_port_path();
  771:     if ($filenewname eq '') {
  772: 	$r->print('<span class="LC_error">'.
  773: 		  &mt("Error: no valid filename was provided to rename to.").
  774: 		  '</span><br />');
  775: 	$r->print(&done(undef,$url));
  776: 	return;
  777:     } 
  778:     my $chg_access;
  779:     my $result=
  780: 	&Apache::lonnet::renameuserfile($uname,$udom,
  781:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
  782:             $port_path.$env{'form.currentpath'}.$filenewname);
  783:     if ($result eq 'ok') {
  784:         $chg_access = &access_for_renamed($filenewname,$group,$udom,$uname);
  785:     } else {      
  786: 	$r->print('<span class="LC_error">'.
  787: 		  &mt('An error occurred ([_1]) while trying to rename [_2] to [_3].'
  788:                       ,$result,&display_file(),&display_file('',$filenewname))
  789:                   .'</span><br />');
  790:         return;
  791:     }
  792:     if ($filenewname ne $env{'form.filenewname'}) {
  793:         $r->print(&mt("The new file name was changed from:<br />[_1] to [_2]",
  794: 		      '<strong>'.&display_file('',$env{'form.filenewname'}).'</strong>',
  795: 		      '<strong>'.&display_file('',$filenewname).'</strong>'));
  796:     }
  797:     $r->print($chg_access);
  798:     $r->print(&done(undef,$url));
  799: }
  800: 
  801: sub access_for_renamed {
  802:     my ($filenewname,$group,$udom,$uname) = @_;
  803:     my $oldfile = $env{'form.currentpath'}.$env{'form.selectfile'};
  804:     $oldfile = &prepend_group($oldfile);
  805:     my $newfile = $env{'form.currentpath'}.$filenewname;
  806:     $newfile = &prepend_group($newfile);
  807:     my $current_permissions =
  808: 	&Apache::lonnet::get_portfile_permissions($udom,$uname);
  809:     my %access_controls =
  810: 	&Apache::lonnet::get_access_controls($current_permissions,
  811: 					     $group,$oldfile);
  812:     my $chg_text;
  813:     if (keys(%access_controls) > 0) {
  814:         my %change_old;
  815:         my %change_new;
  816:         foreach my $key (keys(%{$access_controls{$oldfile}})) {
  817:             $change_old{'delete'}{$key} = 1;
  818:             $change_new{'activate'}{$key} = $access_controls{$oldfile}{$key};
  819:         }
  820:         my ($outcome,$deloutcome,$new_values,$translation) =
  821:             &Apache::lonnet::modify_access_controls($oldfile,\%change_old,
  822: 						    $udom,$uname);
  823:         if ($outcome ne 'ok') {
  824:             $chg_text ='<br /><br />'.&mt("An error occurred ([_1]) while ".
  825:                 "trying to delete access control records for the old name.",$outcome).
  826:                 '</span><br />';
  827:         } else {
  828:             if ($deloutcome ne 'ok') {
  829:                 $chg_text = '<br /><br /><span class="LC_error"><br />'.
  830: 		    &mt("An error occurred ([_1]) while ".
  831: 			"trying to delete access control records for the old name.",$deloutcome).
  832: 			'</span><br />';
  833:             }
  834:         }
  835:         ($outcome,$deloutcome,$new_values,$translation) =
  836:             &Apache::lonnet::modify_access_controls($newfile,\%change_new,
  837:                                                     $udom,$uname);
  838:         if ($outcome ne 'ok') {
  839:             $chg_text .= '<br /><br />'.
  840: 		&mt("An error occurred ([_1]) while ".
  841:                 "trying to update access control records for the new name.",$outcome).
  842:                 '</span><br />';
  843:         }
  844:         if ($chg_text eq '') {
  845:             $chg_text = '<br /><br />'.&mt('Access controls updated to reflect the name change.');
  846:         }
  847:     }
  848:     return $chg_text;
  849: }
  850: 
  851: sub display_access {
  852:     my ($r,$url,$group,$can_setacl,$port_path,$action) = @_;
  853:     my ($uname,$udom) = &get_name_dom($group);
  854:     my $file_name = $env{'form.currentpath'}.$env{'form.access'};
  855:     $file_name = &prepend_group($file_name);
  856:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  857:                                                                         $uname);
  858:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group,$file_name);
  859:     my $aclcount = keys(%access_controls);
  860:     my ($header,$info);
  861:     if ($action eq 'chgaccess') {
  862:         $header = '<h3>'.&mt('Allowing others to retrieve file: [_1]',$port_path.$env{'form.currentpath'}.$env{'form.access'}).'</h3>';
  863:         $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.');
  864:         $info .= '<br /><ul><li>'.&mt('Public files are available to anyone without the need for login.');
  865:         $info .= '</li><li>'.&mt('Passphrase-protected files do not require log-in, but will require the viewer to enter the passphrase you set.');
  866:         $info .= '</li><li>'.&explain_conditionals();
  867:         $info .= '</li></ul>'.
  868:                   &mt('A listing of files viewable without log-in is available at: ')."<a href=\"/adm/$udom/$uname/aboutme/portfolio\">".&Apache::lonnet::absolute_url($ENV{'SERVER_NAME'})."/adm/$udom/$uname/aboutme/portfolio</a>.<br />";
  869:         if ($group eq '') {
  870:             $info .= &mt("For logged in users a 'Display file listing' link will also appear (when there are viewable files) on your personal information page:");
  871:         } else {
  872:             $info .= &mt("For logged in users a 'Display file listing' link will also appear (when there are viewable files) on the course information page:");
  873:         }
  874:         $info .= "<br /><a href=\"/adm/$udom/$uname/aboutme\">".&Apache::lonnet::absolute_url($ENV{'SERVER_NAME'})."/adm/$udom/$uname/aboutme</a><br />";
  875:         if ($group ne '') {
  876:             $info .= &mt("Users with privileges to edit course contents may add a course information page to a course using the 'Course Info' button in DOCS").'<br />';
  877:         }
  878:     } else {
  879:         $header = '<h3>'.&mt('Conditional access controls for file: [_1]',$port_path.$env{'form.currentpath'}.$env{'form.access'}).'</h3>'.
  880:                   &explain_conditionals().'<br />';
  881:     }
  882:     if ($can_setacl) {
  883:         &open_form($r,$url);
  884:         $r->print($header.$info);
  885: 	$r->print('<br />'.&Apache::loncommon::help_open_topic('Portfolio ShareFile SetAccess', &mt('Help on setting up share access')));
  886: 	$r->print(&Apache::loncommon::help_open_topic('Portfolio ShareFile ChangeSetting', &mt('Help on changing settings')));
  887: 	$r->print(&Apache::loncommon::help_open_topic('Portfolio ShareFile StopAccess', &mt('Help on removing share access')));
  888:         &access_setting_table($r,$url,$file_name,$access_controls{$file_name},
  889:                               $action);
  890:         my $button_text = {
  891:                         'continue' => &mt('Proceed'),
  892:                         'cancel' => &mt('Return to directory'),
  893:                       };
  894:         &close_form($r,$url,$button_text);
  895:     } else {
  896:         $r->print($header);
  897:         if ($aclcount) {  
  898:             $r->print($info);
  899:         }
  900:         &view_access_settings($r,$url,$access_controls{$file_name},$aclcount);
  901:     }
  902: }
  903: 
  904: sub explain_conditionals {
  905:     return
  906:         &mt('Conditional files are accessible to logged-in users with accounts in the LON-CAPA network, who satisfy the conditions you set.').'<br />'."\n".
  907:         &mt('The conditions can include affiliation with a particular course or community, or a user account in a specific domain.').'<br />'."\n".
  908:         &mt('Alternatively access can be granted to people with specific LON-CAPA usernames and domains.');
  909: }
  910: 
  911: sub view_access_settings {
  912:     my ($r,$url,$access_controls,$aclcount) = @_;
  913:     my ($showstart,$showend);
  914:     my %todisplay;
  915:     foreach my $key (sort(keys(%{$access_controls}))) {
  916:         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  917:         $todisplay{$scope}{$key} = $$access_controls{$key};
  918:     }
  919:     if ($aclcount) {
  920:         $r->print('<h4>'.&mt('Current access controls defined for this file:').'</h4>');
  921:         $r->print(&Apache::loncommon::start_data_table());
  922:         $r->print(&Apache::loncommon::start_data_table_header_row());
  923:         $r->print('<th>'.&mt('Access control').'</th><th>'.&mt('Dates available').
  924:                   '</th><th>'.&mt('Additional information').'</th>');
  925:         $r->print(&Apache::loncommon::end_data_table_header_row());
  926:         my $count = 1;
  927:         my $chg = 'none';
  928:         &build_access_summary($r,$count,$chg,%todisplay);
  929:         $r->print(&Apache::loncommon::end_data_table());
  930:     } else {
  931:         $r->print(&mt('No access control settings currently exist for this file.').'<br />');
  932:     }
  933:     my %anchor_fields = (
  934:         'currentpath' => $env{'form.currentpath'}
  935:     );
  936:     $r->print('<br />'.&make_anchor($url, \%anchor_fields, &mt('Return to directory')));
  937:     return;
  938: }
  939: 
  940: sub build_access_summary {
  941:     my ($r,$count,$chg,%todisplay) = @_; 
  942:     my ($showstart,$showend);
  943:     my %scope_desc = (
  944:                       public => 'Public',
  945:                       guest => 'Passphrase-protected',
  946:                       domains => 'Conditional: domain-based',
  947:                       users => 'Conditional: user-based',
  948:                       course => 'Conditional: course/community-based',
  949:                      );
  950:     my @allscopes = ('public','guest','domains','users','course');
  951:     foreach my $scope (@allscopes) {
  952:         if ((!(exists($todisplay{$scope}))) || (ref($todisplay{$scope}) ne 'HASH')) {
  953:             next;
  954:         }
  955:         foreach my $key (sort(keys(%{$todisplay{$scope}}))) {
  956:             if ($count) {
  957:                 $r->print(&Apache::loncommon::start_data_table_row());
  958:             }
  959:             my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  960:             my $content = $todisplay{$scope}{$key};
  961:             if ($chg eq 'delete') {
  962:                 $showstart = &mt('Deleted');
  963:                 $showend = $showstart;
  964:             } else {
  965:                 $showstart = localtime($start);
  966:                 if ($end == 0) {
  967:                     $showend = &mt('No end date');
  968:                 } else {
  969:                     $showend = localtime($end);
  970:                 }
  971:             }
  972:             $r->print('<td>'.&mt($scope_desc{$scope}));
  973:             my $crstype;
  974:             if ($scope eq 'course') {
  975:                 if ($chg ne 'delete') {
  976:                     my $cid = $content->{'domain'}.'_'.$content->{'number'};
  977:                     my %course_description = &Apache::lonnet::coursedescription($cid);
  978:                     $r->print('<br />('.$course_description{'description'}.')');
  979:                     $crstype = 'Course';
  980:                     if ($course_description{'type'} ne '') {
  981:                         $crstype = $course_description{'type'};
  982:                     }
  983:                 }
  984:             }
  985:             $r->print('</td><td>'.&mt('Start: ').$showstart.
  986:                   '<br />'.&mt('End: ').$showend.'</td><td>');
  987:             if ($chg ne 'delete') {
  988:                 if ($scope eq 'guest') {
  989:                     $r->print(&mt('Passphrase').': '.$content->{'password'});
  990:                 } elsif ($scope eq 'course') {
  991:                     $r->print('<table width="100%"><tr>');
  992:                     $r->print('<th>'.&mt('Roles').'</th><th>'.
  993:                           &mt('Access').'</th><th>'.
  994:                                           &mt('Sections').'</th>');
  995:                     $r->print('<th>'.&mt('Groups').'</th>');
  996:                     $r->print('</tr>');
  997:                     foreach my $id (sort(keys(%{$content->{'roles'}}))) {
  998:                         $r->print('<tr>');
  999:                         foreach my $item ('role','access','section','group') {
 1000:                             $r->print('<td>');
 1001:                             if ($item eq 'role') {
 1002:                                 my $role_output;
 1003:                                 foreach my $role (@{$content->{'roles'}{$id}{$item}}) {
 1004:                                     if ($role eq 'all') {
 1005:                                         $role_output .= $role.',';
 1006:                                     } elsif ($role =~ /^cr/) {
 1007:                                         $role_output .= (split('/',$role))[3].',';
 1008:                                     } else {
 1009:                                         $role_output .= &Apache::lonnet::plaintext($role,$crstype).',';
 1010:                                     }
 1011:                                 }
 1012:                                 $role_output =~ s/,$//;
 1013:                                 $r->print($role_output);
 1014:                             } else {
 1015:                                 $r->print(join(',',@{$content->{'roles'}{$id}{$item}}));
 1016:                             }
 1017:                             $r->print('</td>');
 1018:                         }
 1019: 			$r->print('</tr>');
 1020:                     }
 1021: 		    $r->print('</table>');
 1022:                 } elsif ($scope eq 'domains') {
 1023:                     $r->print(&mt('Domains: ').join(',',@{$content->{'dom'}}));
 1024:                 } elsif ($scope eq 'users') {
 1025:                     my $curr_user_list = &sort_users($content->{'users'});
 1026:                     $r->print(&mt('Users: ').$curr_user_list);
 1027:                 } else {
 1028:                     $r->print('&nbsp;');
 1029:                 }
 1030:             } else {
 1031:                 $r->print('&nbsp;');
 1032:             }
 1033:             $r->print('</td>');
 1034:             $r->print(&Apache::loncommon::end_data_table_row());
 1035:             $count ++;
 1036:         }
 1037:     }
 1038: }
 1039: 
 1040: 
 1041: sub update_access {
 1042:     my ($r,$url,$group,$port_path) = @_;
 1043:     my $totalprocessed = 0;
 1044:     my %processing;
 1045:     my %title  = (
 1046:                          'activate' => 'New control(s) added',
 1047:                          'delete'   => 'Existing control(s) deleted',
 1048:                          'update'   => 'Existing control(s) modified',
 1049:                      );
 1050:     my $changes;
 1051:     foreach my $chg (sort(keys(%title))) {     
 1052:         @{$processing{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
 1053:         $totalprocessed += @{$processing{$chg}};
 1054:         foreach my $num (@{$processing{$chg}}) {
 1055:             my $scope = $env{'form.scope_'.$num};
 1056:             my ($start,$end) = &get_dates_from_form($num);
 1057:             my $newkey = $num.':'.$scope.'_'.$end.'_'.$start;
 1058:             if ($chg eq 'delete') {
 1059:                 $$changes{$chg}{$newkey} = 1;
 1060:             } else {
 1061:                 $$changes{$chg}{$newkey} = 
 1062:                             &build_access_record($num,$scope,$start,$end,$chg);
 1063:             }
 1064:         }
 1065:     }
 1066:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
 1067:     $r->print('<h3>'.&mt('Allowing others to retrieve file: [_1]',
 1068:               $port_path.$file_name).'</h3>'."\n");
 1069:     $file_name = &prepend_group($file_name);
 1070:     my ($uname,$udom) = &get_name_dom($group);
 1071:     my ($errors,$outcome,$deloutcome,$new_values,$translation);
 1072:     if ($totalprocessed) {
 1073:         ($outcome,$deloutcome,$new_values,$translation) =
 1074:         &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
 1075:                                                 $uname);
 1076:     }
 1077:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
 1078:                                                                        $uname);
 1079:     my %access_controls = 
 1080: 	&Apache::lonnet::get_access_controls($current_permissions,
 1081: 					     $group,$file_name);
 1082:     if ($totalprocessed) {
 1083:         if ($outcome eq 'ok') {
 1084:             my $updated_controls = $access_controls{$file_name};
 1085:             my ($showstart,$showend);
 1086:             $r->print(&Apache::loncommon::start_data_table());
 1087:             $r->print(&Apache::loncommon::start_data_table_header_row());
 1088:             $r->print('<th>'.&mt('Type of change').'</th><th>'.
 1089:                       &mt('Access control').'</th><th>'.&mt('Dates available').
 1090:                       '</th><th>'.&mt('Additional information').'</th>');
 1091:             $r->print(&Apache::loncommon::end_data_table_header_row());
 1092:             foreach my $chg (sort(keys(%processing))) {
 1093:                 if (@{$processing{$chg}} > 0) {
 1094:                     if ($chg eq 'delete') {
 1095:                         if (!($deloutcome eq 'ok')) {
 1096:                             $errors .='<span class="LC_error">'.
 1097: 				&mt('A problem occurred deleting access controls: [_1]',$deloutcome).
 1098: 				'</span>';
 1099:                             next;
 1100:                         }
 1101:                     }
 1102:                     my $numchgs = @{$processing{$chg}};
 1103:                     $r->print(&Apache::loncommon::start_data_table_row());
 1104:                     $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
 1105:                               '.</td>');
 1106:                     my $count = 0;
 1107:                     my %todisplay;
 1108:                     foreach my $key (sort(keys(%{$$changes{$chg}}))) {
 1109:                         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
 1110:                         my $newkey = $key;
 1111:                         if ($chg eq 'activate') {
 1112:                             $newkey =~ s/^(\d+)/$$translation{$1}/;
 1113:                         }
 1114:                         $todisplay{$scope}{$newkey} = $$updated_controls{$newkey};
 1115:                     }
 1116:                     &build_access_summary($r,$count,$chg,%todisplay);  
 1117:                 }
 1118:             }
 1119:             $r->print(&Apache::loncommon::end_data_table());
 1120:         } else {
 1121:             if ((@{$processing{'activate'}} > 0) || (@{$processing{'update'}} > 0)) {
 1122:                 $errors .= '<span class="LC_error">'.
 1123: 		    &mt('A problem occurred saving access control settings: [_1]',$outcome).
 1124: 		    '</span>';
 1125:             }
 1126:         }
 1127:         if ($errors) { 
 1128:             $r->print($errors);
 1129:         }
 1130:     }
 1131:     my $allnew = 0;
 1132:     my $totalnew = 0;
 1133:     my $status = 'new';
 1134:     my ($firstitem,$lastitem);
 1135:     my @types = ('course','domains','users');
 1136:     foreach my $newitem (@types) {
 1137:         $allnew += $env{'form.new'.$newitem};
 1138:     }
 1139:     if ($allnew > 0) {
 1140:         my $now = time;
 1141:         my $then = $now + (60*60*24*180); # six months approx.
 1142:         &open_form($r,$url);
 1143:         my %showtypes = &Apache::lonlocal::texthash (
 1144:            course  => 'course/community',
 1145:            domains => 'domain',
 1146:            users   => 'user',
 1147:         );
 1148:         foreach my $newitem (@types) {
 1149:             if ($env{'form.new'.$newitem} > 0) {
 1150:                 $r->print('<br />'.&mt('Add new [_1]-based[_2] access control for portfolio file: [_3]','<b>'.$showtypes{$newitem},'</b>','<b>'.$env{'form.currentpath'}.$env{'form.selectfile'}.'</b>').'<br /><br />');
 1151:                 $firstitem = $totalnew;
 1152:                 $lastitem = $totalnew + $env{'form.new'.$newitem};
 1153:                 $totalnew = $lastitem;
 1154:                 my @numbers;   
 1155:                 for (my $i=$firstitem; $i<$lastitem; $i++) {
 1156:                     push (@numbers,$i);
 1157:                 }
 1158:                 &display_access_row($r,$status,$newitem,\@numbers,
 1159:                                     $access_controls{$file_name},$now,$then);
 1160:             }
 1161:         }
 1162:         &close_form($r,$url);
 1163:     } else {
 1164:         my %anchor_fields = (
 1165:             'currentpath' => $env{'form.currentpath'},
 1166:             'access' => $env{'form.selectfile'}
 1167:         );
 1168:         $r->print('<br />'.&make_anchor($url, \%anchor_fields, &mt('Display all access settings for this file')));
 1169:         delete $anchor_fields{'access'};
 1170:         $r->print('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&make_anchor($url,\%anchor_fields,&mt('Return to directory')));
 1171:     }
 1172:     return;
 1173: }
 1174: 
 1175: sub build_access_record {
 1176:     my ($num,$scope,$start,$end,$chg) = @_;
 1177:     my $record = {
 1178: 	type => $scope,
 1179: 	time => {
 1180: 	    start => $start,
 1181: 	    end   => $end
 1182: 	    },
 1183: 	    };
 1184: 		
 1185:     if ($scope eq 'guest') {	
 1186:         $record->{'password'} = $env{'form.password'};
 1187:     } elsif ($scope eq 'course') {
 1188:         $record->{'domain'} = $env{'form.crsdom_'.$num};
 1189: 	$record->{'number'} = $env{'form.crsnum_'.$num};
 1190:         my @role_ids;
 1191:         my @delete_role_ids =
 1192:             &Apache::loncommon::get_env_multiple('form.delete_role_'.$num);
 1193: 	my @preserves =
 1194: 	    &Apache::loncommon::get_env_multiple('form.preserve_role_'.$num);
 1195: 	if (@delete_role_ids) {
 1196: 	    foreach my $id (@preserves) {
 1197: 		if (grep {$_ = $id} (@delete_role_ids)) {
 1198: 		    next;
 1199: 		}
 1200: 		push(@role_ids,$id); 
 1201: 	    }
 1202: 	} else {
 1203: 	    push(@role_ids,@preserves);
 1204: 	}
 1205: 
 1206: 	my $next_id = $env{'form.add_role_'.$num};
 1207: 	if ($next_id) {
 1208: 	    push(@role_ids,$next_id);
 1209: 	}
 1210: 
 1211:         foreach my $id (@role_ids) {
 1212:             my (@roles,@accesses,@sections,@groups);
 1213:             if (($id == $next_id) && ($chg eq 'update')) {
 1214:                 @roles    = split(/,/,$env{'form.role_'.$num.'_'.$next_id});
 1215:                 @accesses = split(/,/,$env{'form.access_'.$num.'_'.$next_id});
 1216:                 @sections = split(/,/,$env{'form.section_'.$num.'_'.$next_id});
 1217:                 @groups   = split(/,/,$env{'form.group_'.$num.'_'.$next_id});
 1218:             } else {
 1219:                 @roles = &Apache::loncommon::get_env_multiple('form.role_'.$num.'_'.$id);
 1220:                 @accesses = &Apache::loncommon::get_env_multiple('form.access_'.$num.'_'.$id);
 1221:                 @sections = &Apache::loncommon::get_env_multiple('form.section_'.$num.'_'.$id);
 1222:                 @groups = &Apache::loncommon::get_env_multiple('form.group_'.$num.'_'.$id);
 1223:             }
 1224: 	    $record->{'roles'}{$id}{'role'}    = \@roles;
 1225: 	    $record->{'roles'}{$id}{'access'}  = \@accesses;
 1226: 	    $record->{'roles'}{$id}{'section'} = \@sections;
 1227: 	    $record->{'roles'}{$id}{'group'}   = \@groups;
 1228:         }
 1229:     } elsif ($scope eq 'domains') {
 1230:         my @doms = &Apache::loncommon::get_env_multiple('form.dom_'.$num);
 1231: 	$record->{'dom'} = \@doms;
 1232:     } elsif ($scope eq 'users') {
 1233:         my $userlist = $env{'form.users_'.$num};
 1234:         $userlist =~ s/\s+//sg;
 1235: 	my %userhash = map { ($_,1) } (split(/,/,$userlist));
 1236:         foreach my $user (keys(%userhash)) {
 1237:             my ($uname,$udom) = split(/:/,$user);
 1238: 	    push(@{$record->{'users'}}, {
 1239: 		'uname' => $uname,
 1240: 		'udom'  => $udom
 1241: 		});
 1242: 	}
 1243:     }
 1244:     return $record;
 1245: }
 1246: 
 1247: sub get_dates_from_form {
 1248:     my ($id) = @_;
 1249:     my $startdate;
 1250:     my $enddate;
 1251:     $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$id);
 1252:     $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$id);
 1253:     if ( exists ($env{'form.noend_'.$id}) ) {
 1254:         $enddate = 0;
 1255:     }
 1256:     return ($startdate,$enddate);
 1257: }
 1258: 
 1259: sub sort_users {
 1260:     my ($users) = @_; 
 1261:     my @curr_users = map {
 1262: 	$_->{'uname'}.':'.$_->{'udom'}
 1263:     } (@{$users});
 1264:     my $curr_user_list = join(",\n",sort(@curr_users));
 1265:     return $curr_user_list;
 1266: }
 1267: 
 1268: sub access_setting_table {
 1269:     my ($r,$url,$filename,$access_controls,$action) = @_;
 1270:     my ($public,$publictext);
 1271:     $publictext ='Off';
 1272:     my ($guest,$guesttext);
 1273:     $guesttext = 'Off';
 1274:     my @courses = ();
 1275:     my @domains = ();
 1276:     my @users = ();
 1277:     my $now = time;
 1278:     my $then = $now + (60*60*24*180); # six months approx.
 1279:     my ($num,$scope,$publicnum,$guestnum);
 1280:     my (%acl_count,%end,%start,%conditionals);
 1281:     foreach my $key (sort(keys(%{$access_controls}))) {
 1282:         ($num,$scope,$end{$key},$start{$key}) = &unpack_acc_key($key);
 1283:         if ($scope eq 'public') {
 1284:             $public = $key;
 1285:             $publicnum = $num;
 1286:             $publictext = &acl_status($start{$key},$end{$key},$now);
 1287:         } elsif ($scope eq 'guest') {
 1288:             $guest=$key;
 1289:             $guestnum = $num;  
 1290:             $guesttext = &acl_status($start{$key},$end{$key},$now);
 1291:         } else {
 1292:             $conditionals{$scope}{$key} = $$access_controls{$key};
 1293:             if ($scope eq 'course') {
 1294:                 push(@courses,$key);
 1295:             } elsif ($scope eq 'domains') {
 1296:                 push(@domains,$key);
 1297:             } elsif ($scope eq 'users') {
 1298:                 push(@users,$key);
 1299:             }
 1300:         }
 1301:         $acl_count{$scope} ++;
 1302:     }
 1303:     $r->print('<table border="0"><tr><td valign="top">');
 1304:     if ($action eq 'chgaccess') {
 1305:         &standard_settings($r,$now,$then,$url,$filename,\%acl_count,\%start,
 1306:                            \%end,$public,$publicnum,$publictext,$guest,$guestnum,
 1307:                            $guesttext,$access_controls,%conditionals);
 1308:     } else {
 1309:         &condition_setting($r,$access_controls,$now,$then,\%acl_count,
 1310:                            \@domains,\@users,\@courses);
 1311:     }
 1312:     $r->print('</td></tr></table>');
 1313: }
 1314: 
 1315: sub standard_settings {
 1316:     my ($r,$now,$then,$url,$filename,$acl_count,$start,$end,$public,$publicnum,
 1317:       $publictext,$guest,$guestnum,$guesttext,$access_controls,%conditionals)=@_;
 1318:     $r->print('<h3>'.&mt('Public access: [_1]',&mt($publictext)).'</h3>');
 1319:     $r->print(&Apache::loncommon::start_data_table());
 1320:     $r->print(&Apache::loncommon::start_data_table_header_row());
 1321:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').'</th>');
 1322:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1323:     $r->print(&Apache::loncommon::start_data_table_row());
 1324:     if ($public) {
 1325:         $r->print('<td>'.&actionbox('old',$publicnum,'public').'</td><td>'.
 1326:              &dateboxes($publicnum,$start->{$public},$end->{$public}).'</td>');
 1327:     } else {
 1328:         $r->print('<td>'.&actionbox('new','0','public').'</td><td>'.
 1329:                   &dateboxes('0',$now,$then).'</td>');
 1330:     }
 1331:     $r->print(&Apache::loncommon::end_data_table_row());
 1332:     $r->print(&Apache::loncommon::end_data_table());
 1333:     $r->print('</td><td width="40">&nbsp;</td><td valign="top">');
 1334:     $r->print('<h3>'.&mt('Passphrase-protected access: [_1]',&mt($guesttext)).'</h3>');
 1335:     $r->print(&Apache::loncommon::start_data_table());
 1336:     $r->print(&Apache::loncommon::start_data_table_header_row());
 1337:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').
 1338:               '</th><th>'. &mt('Passphrase').'</th>');
 1339:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1340:     $r->print(&Apache::loncommon::start_data_table_row());
 1341:     my $passwd;
 1342:     if ($guest) {
 1343:         $passwd = $$access_controls{$guest}{'password'};
 1344:         $r->print('<td>'.&actionbox('old',$guestnum,'guest').'</td><td>'.
 1345:               &dateboxes($guestnum,$start->{$guest},$end->{$guest}).'</td>');
 1346:     } else {
 1347:         $r->print('<td>'.&actionbox('new','1','guest').'</td><td>'.
 1348:                   &dateboxes('1',$now,$then).'</td>');
 1349:     }
 1350:     $r->print('<td><input type="text" size="15" name="password" value="'.
 1351:               $passwd.'" /></td>');
 1352:     $r->print(&Apache::loncommon::end_data_table_row());
 1353:     $r->print(&Apache::loncommon::end_data_table());
 1354:     $r->print('</td></tr><tr><td colspan="3">&nbsp;</td></tr>'.
 1355:               '<tr><td colspan="3">');
 1356:     my $numconditionals = 0;
 1357:     my $conditionstext;
 1358:     my %cond_status;
 1359:     foreach my $scope ('domains','users','course') {
 1360:         $numconditionals += $acl_count->{$scope}; 
 1361:         if ($acl_count->{$scope} > 0) {
 1362:             if ($conditionstext ne 'Active') { 
 1363:                 foreach my $key (keys(%{$conditionals{$scope}})) {
 1364:                     $conditionstext = &acl_status($start->{$key},$end->{$key},$now);
 1365:                     if ($conditionstext eq 'Active') {
 1366:                        last;
 1367:                     }
 1368:                 }
 1369:             }
 1370:         }
 1371:     }
 1372:     if ($conditionstext eq '') {
 1373:         $conditionstext = 'Off';
 1374:     }
 1375:     my %anchor_fields = (
 1376:             'access' => $env{'form.selectfile'},
 1377:             'action' => 'chgconditions',
 1378:             'currentpath' => $env{'form.currentpath'},
 1379:         );
 1380:     $r->print('<h3>'.&mt('Conditional access: [_1]',&mt($conditionstext)).'</h3>');
 1381:     if ($numconditionals > 0) {
 1382:         my $count = 1;
 1383:         my $chg = 'none';
 1384:         $r->print(&mt('You have previously set [_1] conditional access controls.',$numconditionals).' '.&make_anchor($url,\%anchor_fields,&mt('Change Conditions')).'<br /><br />');
 1385:         $r->print(&Apache::loncommon::start_data_table());
 1386:         $r->print(&Apache::loncommon::start_data_table_header_row());
 1387:         $r->print('<th>'.&mt('Access control').'</th><th>'.&mt('Dates available').
 1388:                   '</th><th>'.&mt('Additional information').'</th>');
 1389:         $r->print(&Apache::loncommon::end_data_table_header_row());
 1390:         &build_access_summary($r,$count,$chg,%conditionals);
 1391:         $r->print(&Apache::loncommon::end_data_table());
 1392:     } else {
 1393:         $r->print(&make_anchor($url,\%anchor_fields,&mt('Add conditional access')).' '.&mt('based on domain, username, or course/community affiliation.'));
 1394:     }
 1395: }
 1396: 
 1397: sub condition_setting {
 1398:     my ($r,$access_controls,$now,$then,$acl_count,$domains,$users,$courses) = @_;
 1399:     $r->print('<tr><td valign="top">');
 1400:     &access_element($r,'domains',$acl_count,$domains,$access_controls,$now,$then);
 1401:     $r->print('</td><td>&nbsp;</td><td valign="top">');
 1402:     &access_element($r,'users',$acl_count,$users,$access_controls,$now,$then);
 1403:     $r->print('</td></tr><tr><td colspan="3"></td></tr><tr>');
 1404:     if ($acl_count->{course} > 0) {
 1405:         $r->print('<td colspan="3" valign="top">');
 1406:     } else {
 1407:         $r->print('<td valign="top">');
 1408:     }
 1409:     &access_element($r,'course',$acl_count,$courses,$access_controls,$now,$then);
 1410:     $r->print('</td>');
 1411:     $r->print('</td></tr></table>');
 1412: }
 1413: 
 1414: sub acl_status {
 1415:     my ($start,$end,$now) = @_;
 1416:     if ($start > $now) {
 1417:         return 'Inactive';
 1418:     }
 1419:     if ($end && $end<$now) {
 1420:         return 'Inactive';
 1421:     }
 1422:     return 'Active';
 1423: }
 1424: 
 1425: sub access_element {
 1426:     my ($r,$type,$acl_count,$items,$access_controls,$now,$then) = @_;
 1427:     my %typetext = &Apache::lonlocal::texthash(
 1428:         domains => 'Domain',
 1429:         users   => 'User',
 1430:         course  => 'Course/Community'
 1431:     );
 1432:     $r->print('<h3>'.&mt('[_1]-based conditional access: ',$typetext{$type}));
 1433:     if ($$acl_count{$type}) {
 1434:         $r->print($$acl_count{$type}.' ');
 1435:         if ($$acl_count{$type} > 1) {
 1436:             $r->print(&mt('conditions'));
 1437:         } else {
 1438:             $r->print(&mt('condition'));
 1439:         }
 1440:     } else {
 1441:         $r->print(&mt('Off'));
 1442:     }
 1443:     $r->print('</h3>');
 1444:     &display_access_row($r,'old',$type,$items,$access_controls,$now,$then);
 1445:     return;
 1446: }
 1447: 
 1448: sub display_access_row {
 1449:     my ($r,$status,$type,$items,$access_controls,$now,$then) = @_;
 1450:     my $showtype;
 1451:     if ($type eq 'course') {
 1452:         $showtype = &mt('Courses/Communities');
 1453:     } elsif ($type eq 'domains') {
 1454:         $showtype = &mt('Domains');
 1455:     } elsif ($type eq 'users') {
 1456:         $showtype = &mt('Users');
 1457:     }
 1458:     if (@{$items} > 0) {
 1459:         my @all_doms;
 1460:         my $colspan = 3;
 1461:         $r->print(&Apache::loncommon::start_data_table());
 1462:         $r->print(&Apache::loncommon::start_data_table_header_row());
 1463:         $r->print('<th>'.&mt('Action?').'</th><th>'.$showtype.'</th><th>'.
 1464:               &mt('Dates available').'</th>');
 1465:         if ($type eq 'course' && $status eq 'old') {
 1466:             
 1467:             $r->print('<th>'.&mt('Allowed course/community affiliations').
 1468:                       '</th>');
 1469:             $colspan ++;
 1470:         } elsif ($type eq 'domains') {
 1471:             @all_doms = sort(&Apache::lonnet::all_domains());
 1472:         }
 1473:         $r->print(&Apache::loncommon::end_data_table_header_row());
 1474:         foreach my $key (@{$items}) {
 1475: 	    $r->print(&Apache::loncommon::start_data_table_row());
 1476:             if ($type eq 'course') {
 1477:                 &course_row($r,$status,$type,$key,$access_controls,$now,$then);
 1478:             } elsif ($type eq 'domains') {
 1479:                 &domains_row($r,$status,$key,\@all_doms,$access_controls,$now,
 1480:                             $then);
 1481:             } elsif ($type eq 'users') {
 1482:                 &users_row($r,$status,$key,$access_controls,$now,$then);
 1483:             }
 1484: 	    $r->print(&Apache::loncommon::end_data_table_row());
 1485:         }
 1486:         if ($status eq 'old') {
 1487: 	    $r->print(&Apache::loncommon::start_data_table_row());
 1488:             $r->print('<td colspan="',$colspan.'">'.&additional_item($type).
 1489:                       '</td>');
 1490: 	    $r->print(&Apache::loncommon::end_data_table_row());
 1491:         }
 1492:         $r->print(&Apache::loncommon::end_data_table());
 1493:     } else {
 1494:         $r->print(&mt('No [_1]-based conditions defined.',$showtype).'<br />'.
 1495:                   &additional_item($type));
 1496:     }
 1497:     return;
 1498: }
 1499: 
 1500: sub course_js {
 1501:     return qq|
 1502: <script type="text/javascript">
 1503: function setRoleOptions(num,roleid,cdom,cnum,type) {
 1504:     updateIndexNum = getIndexByValue('update',num);
 1505:     var addItem = 'add_role_'+num;
 1506:     var addIndexNum = getIndexByName(addItem);
 1507:     if (document.portform.elements[addItem].checked) {
 1508:         document.portform.elements[updateIndexNum].checked = true;
 1509:         var url = '/adm/portfolio?action=rolepicker&setroles='+num+'_'+roleid+'&cnum='+cnum+'&cdom='+cdom+'&type='+type;
 1510:         var title = 'Roles_Chooser';
 1511:         var options = 'scrollbars=1,resizable=1,menubar=0';
 1512:         options += ',width=700,height=600';
 1513:         rolebrowser = open(url,title,options,'1');
 1514:         rolebrowser.focus();
 1515:     } else {
 1516:         addArray = new Array ('role','access','section','group');
 1517:         for (var j=0;j<addArray.length;j++) {
 1518:             var itemIndex = getIndexByName(addArray[j]+'_'+num+'_'+roleid);
 1519:             document.portform.elements[itemIndex].value = '';
 1520:         }
 1521:     }
 1522: }
 1523: 
 1524: function getIndexByName(item) {
 1525:     for (var i=0;i<document.portform.elements.length;i++) {
 1526:         if (document.portform.elements[i].name == item) {
 1527:             return i;
 1528:         }
 1529:     }
 1530:     return -1;
 1531: }
 1532: 
 1533: function getIndexByValue(name,value) {
 1534:     for (var i=0;i<document.portform.elements.length;i++) {
 1535:         if (document.portform.elements[i].name == name && document.portform.elements[i].value == value) {
 1536:             return i;
 1537:         }
 1538:     }
 1539:     return -1;
 1540: }
 1541: 
 1542: </script>
 1543: |;
 1544: }
 1545: 
 1546: sub course_row {
 1547:     my ($r,$status,$type,$item,$access_controls,$now,$then) = @_;
 1548:     my $content;
 1549:     my $defdom = $env{'user.domain'};
 1550:     if ($status eq 'old') {
 1551:         $content = $$access_controls{$item}; 
 1552:         $defdom =  $content->{'domain'};
 1553:     }
 1554:     my $js = &Apache::loncommon::coursebrowser_javascript($defdom)
 1555: 	.&course_js();
 1556:     my $showtype = &mt('Course/Community');
 1557:     my $crstype = 'Course';
 1558:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1559:                                                     $type);
 1560:     $r->print('<td>'.$js.&actionbox($status,$num,$scope).'</td>');
 1561:     if ($status eq 'old') {
 1562:         my $cid = $content->{'domain'}.'_'.$content->{'number'};
 1563:         my %course_description = &Apache::lonnet::coursedescription($cid);
 1564:         if ($course_description{'type'} ne '') {
 1565:             $crstype = $course_description{'type'};
 1566:         }
 1567:         $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>');
 1568:     } elsif ($status eq 'new') {
 1569:         $r->print('<td>'.&Apache::loncommon::selectcourse_link('portform','crsnum_'.$num,'crsdom_'.$num,'description_'.$num,$num.'_1',undef,$showtype).'&nbsp;&nbsp;<input type="text" name="description_'.$num.'" size="30" /><input type="hidden" name="crsdom_'.$num.'" /><input type="hidden" name="crsnum_'.$num.'" /></td>');
 1570:     }
 1571:     $r->print('<td>'.&dateboxes($num,$start,$end));
 1572:     my $newrole_id = 1;
 1573:     if ($status eq 'old') {
 1574:         $r->print('</td><td>');
 1575:         my $max_id = 0;
 1576:         if (keys(%{$content->{'roles'}}) > 0) {
 1577:             $r->print('<table><tr><th>'.&mt('Action').'</th>'.
 1578:                       '<th>'.&mt('Roles').'</th>'.
 1579:                       '<th>'.&mt('Access').'</th>'.
 1580:                       '<th>'.&mt('Sections').'</th>'.
 1581:                       '<th>'.&mt('Groups').'</th></tr>');
 1582:             foreach my $role_id (sort(keys(%{$content->{'roles'}}))) {
 1583:                 if ($role_id > $max_id) {
 1584:                     $max_id = $role_id;
 1585:                 }
 1586:                 $max_id ++;
 1587:                 my $role_selects = &role_selectors($num,$role_id,$crstype,$content,'display');
 1588:                 $r->print('<tr><td><span class="LC_nobreak"><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>');
 1589:             }
 1590:             $r->print('</table>');
 1591:         }
 1592:         $r->print('<br />'.&mt('Add a roles-based condition').
 1593:                   '&nbsp;<input type="checkbox" name="add_role_'.
 1594:                   $num.'" onClick="javascript:setRoleOptions('."'$num',
 1595:                   '$max_id','$content->{'domain'}','$content->{'number'}',
 1596:                   '$showtype'".')" value="" />');
 1597:         $newrole_id = $max_id;
 1598:     } else {
 1599:         $r->print('<input type="hidden" name="add_role_'.$num.'" value="" />');
 1600:     }
 1601:     $r->print(&add_course_role($num,$newrole_id));
 1602:     $r->print('</td>');
 1603:     return;
 1604: }
 1605: 
 1606: sub add_course_role {
 1607:     my ($num,$max_id) = @_;
 1608:     my $output;
 1609:     $output .='<input type="hidden" name="role_'.$num.'_'.$max_id.'" />'.
 1610:               '<input type="hidden" name="access_'.$num.'_'.$max_id.'" />'.
 1611:               '<input type="hidden" name="section_'.$num.'_'.$max_id.'" />'.
 1612:               '<input type="hidden" name="group_'.$num.'_'.$max_id.'" />';
 1613:     return $output;
 1614: }
 1615: 
 1616: sub domains_row {
 1617:     my ($r,$status,$item,$all_doms,$access_controls,$now,$then) = @_;
 1618:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1619:                                                     'domains');
 1620:     my $dom_select = '<select name="dom_'.$num.'" size="4" multiple="multiple">'.
 1621:                      ' <option value="">'.&mt('Please select').'</option>';
 1622:     if ($status eq 'old') {
 1623:         my $content =  $$access_controls{$item};
 1624: 	foreach my $dom (@{$all_doms}) {
 1625:             if ((@{$content->{'dom'}} > 0) 
 1626: 		&& (grep(/^\Q$dom\E$/,@{$content->{'dom'}}))) {
 1627:                 $dom_select .= '<option value="'.$dom.'" selected="selected">'.
 1628:                                $dom.'</option>';
 1629:             } else {
 1630:                 $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
 1631:             }
 1632:         }
 1633:     } else {
 1634:         foreach my $dom (@{$all_doms}) {
 1635:             $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
 1636:         }
 1637:     }
 1638:     $dom_select .= '</select>';
 1639:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.$dom_select.
 1640:               '</td><td>'.&dateboxes($num,$start,$end).'</td>');
 1641: }
 1642: 
 1643: sub users_row {
 1644:     my ($r,$status,$item,$access_controls,$now,$then) = @_;
 1645:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1646:                                                     'users');
 1647:     my $curr_user_list;
 1648:     if ($status eq 'old') {
 1649:         my $content = $$access_controls{$item};
 1650:         $curr_user_list = &sort_users($content->{'users'});
 1651:     }
 1652:     $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>');
 1653: }
 1654: 
 1655: sub additional_item {
 1656:     my ($type) = @_;
 1657:     my $showtype;
 1658:     if ($type eq 'course') {
 1659:         $showtype = &mt('course/community');
 1660:     } elsif ($type eq 'domains') {
 1661:         $showtype = &mt('domains');
 1662:     } elsif ($type eq 'users') {
 1663:         $showtype = &mt('users');
 1664:     }
 1665:     my $output = &mt('Add new [_1] condition(s)?',$showtype).'&nbsp;'.&mt('Number to add: ').'<input type="text" name="new'.$type.'" size="3" value="0" />';
 1666:     return $output;
 1667: }
 1668: 
 1669: sub actionbox {
 1670:     my ($status,$num,$scope) = @_;
 1671:     my $output = '<span class="LC_nobreak"><label>';
 1672:     if ($status eq 'new') {
 1673:         my $checkstate;
 1674:         if ($scope eq 'domains' || $scope eq 'users' || $scope eq 'course') {
 1675:             $checkstate = 'checked="checked"';
 1676:         }
 1677:         $output .= '<input type="checkbox" name="activate" value="'.$num.'" '.
 1678:                    $checkstate.'  />'.
 1679:         &mt('Activate');
 1680:     } else {
 1681:         $output .= '<input type="checkbox" name="delete" value="'.$num.
 1682:                    '" />'.&mt('Delete').'</label></span><br /><span class="LC_nobreak">'.
 1683:                    '<label><input type="checkbox" name="update" value="'.
 1684:                    $num.'" />'.&mt('Update');
 1685:     }
 1686:     $output .= '</label></span><input type="hidden" name="scope_'.$num.                '" value="'.$scope.'" />';
 1687:     return $output;
 1688: }
 1689:                                                                                    
 1690: sub dateboxes {
 1691:     my ($num,$start,$end) = @_;
 1692:     my $noend;
 1693:     if ($end == 0) {
 1694:         $noend = 'checked="checked"';
 1695:     }
 1696:     my $startdate = &Apache::lonhtmlcommon::date_setter('portform',
 1697:                            'startdate_'.$num,$start,undef,undef,undef,1,undef,
 1698:                             undef,undef,1);
 1699:     my $enddate = &Apache::lonhtmlcommon::date_setter('portform',
 1700:                                'enddate_'.$num,$end,undef,undef,undef,1,undef,
 1701:                                 undef,undef,1). '&nbsp;&nbsp;<span class="LC_nobreak"><label>'.
 1702:                                 '<input type="checkbox" name="noend_'.
 1703:                                 $num.'" '.$noend.' />'.&mt('No end date').
 1704:                                 '</label></span>';
 1705:                                                                                    
 1706:     my $output = &mt('Start: ').$startdate.'<br />'.&mt('End: ').$enddate;
 1707:     return $output;
 1708: }
 1709: 
 1710: sub unpack_acc_key {
 1711:     my ($acc_key) = @_;
 1712:     my ($num,$scope,$end,$start) = ($acc_key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 1713:     return ($num,$scope,$end,$start);
 1714: }
 1715: 
 1716: sub set_identifiers {
 1717:     my ($status,$item,$now,$then,$scope) = @_;
 1718:     if ($status eq 'old') {
 1719:         return(&unpack_acc_key($item));
 1720:     } else {
 1721:         return($item,$scope,$then,$now);
 1722:     }
 1723: } 
 1724: 
 1725: sub role_selectors {
 1726:     my ($num,$role_id,$type,$content,$caller) = @_;
 1727:     my ($output,$cdom,$cnum,$longid);
 1728:     if ($caller eq 'display') {
 1729:         $longid = '_'.$num.'_'.$role_id;
 1730:         $cdom = $$content{'domain'};
 1731:         $cnum = $$content{'number'};
 1732:     } elsif ($caller eq 'rolepicker') {
 1733:          $cdom = $env{'form.cdom'};
 1734:          $cnum = $env{'form.cnum'};
 1735:     }
 1736:     my $crstype = 'Course';
 1737:     if ($cnum =~ /^$match_community$/) {
 1738:         $crstype = 'Community'
 1739:     }
 1740:     my ($sections,$groups,$allroles,$rolehash,$accesshash) =
 1741:             &Apache::loncommon::get_secgrprole_info($cdom,$cnum,1,$crstype);
 1742:     if (!@{$sections}) {
 1743:         @{$sections} = ('none');
 1744:     } else {
 1745:         unshift(@{$sections},('all','none'));
 1746:     }
 1747:     if (!@{$groups}) {
 1748:         @{$groups} = ('none');
 1749:     } else {
 1750:         unshift(@{$groups},('all','none'));
 1751:     }
 1752:     my @allacesses = sort(keys(%{$accesshash}));
 1753:     my (%sectionhash,%grouphash);
 1754:     foreach my $sec (@{$sections}) {
 1755:         $sectionhash{$sec} = $sec;
 1756:     }
 1757:     foreach my $grp (@{$groups}) {
 1758:         $grouphash{$grp} = $grp;
 1759:     }
 1760:     my %lookup = (
 1761:                    'role' => $rolehash,
 1762:                    'access' => $accesshash,
 1763:                    'section' => \%sectionhash,
 1764:                    'group' => \%grouphash,
 1765:                  );
 1766:     my @allaccesses = sort(keys(%{$accesshash}));
 1767:     my %allitems = (
 1768:                     'role' => $allroles,
 1769:                     'access' => \@allaccesses,
 1770:                     'section' => $sections,
 1771:                     'group' => $groups,
 1772:                    );
 1773:     foreach my $item ('role','access','section','group') {
 1774:         $output .= '<td><select name="'.$item.$longid.'" multiple="multiple" size="4">'."\n";
 1775:         foreach my $entry (@{$allitems{$item}}) {
 1776:             if ($caller eq 'display') {
 1777:                 if ((@{$$content{'roles'}{$role_id}{$item}} > 0) && 
 1778:                     (grep(/^\Q$entry\E$/,@{$$content{'roles'}{$role_id}{$item}}))) {
 1779:                     $output .= '  <option value="'.$entry.'" selected="selected">'.
 1780:                                   $lookup{$item}{$entry}.'</option>';
 1781:                     next;
 1782:                 }
 1783:             }
 1784:             $output .= '  <option value="'.$entry.'">'.
 1785:                        $lookup{$item}{$entry}.'</option>';
 1786:         }
 1787:         $output .= '</select>';
 1788:     }
 1789:     $output .= '</td>';
 1790:     return $output;
 1791: }
 1792: 
 1793: sub role_options_window {
 1794:     my ($r) = @_;
 1795:     my $type = $env{'form.type'};
 1796:     my $rolenum = $env{'form.setroles'};
 1797:     my ($num,$role_id) = ($rolenum =~ /^([\d_]+)_(\d+)$/);
 1798:     my $role_elements;
 1799:     foreach my $item ('role','access','section','group') {
 1800:         $role_elements .= "'".$item.'_'.$rolenum."',";
 1801:     }
 1802:     $role_elements =~ s/,$//; 
 1803:     my $role_selects = &role_selectors($num,$role_id,$type,undef,
 1804:                                        'rolepicker');
 1805:     $r->print(<<"END_SCRIPT");
 1806: <script type="text/javascript">
 1807: function setRoles() {
 1808:     var role_elements = new Array($role_elements);
 1809:     for (var i=0; i<role_elements.length; i++) {
 1810:         var copylist = '';
 1811:         for (var j=0; j<document.rolepicker.elements[i].length; j++) {
 1812:             if (document.rolepicker.elements[i].options[j].selected) {
 1813:                 copylist = copylist + document.rolepicker.elements[i].options[j].value + ',';
 1814:             }
 1815:         }
 1816:         copylist = copylist.substr(0,copylist.length-1);
 1817:         var openerItem = getIndexByName(role_elements[i]);
 1818:         opener.document.portform.elements[openerItem].value = copylist; 
 1819:     }
 1820:     var roleAdder = getIndexByName('add_role_$num');
 1821:     opener.document.portform.elements[roleAdder].value = '$role_id';
 1822:     self.close();
 1823: }
 1824: 
 1825: function getIndexByName(item) {
 1826:     for (var i=0;i<opener.document.portform.elements.length;i++) {
 1827:         if (opener.document.portform.elements[i].name == item) {
 1828:             return i;
 1829:         }
 1830:     }
 1831:     return -1;
 1832: }
 1833: 
 1834: </script>
 1835: END_SCRIPT
 1836:     $r->print(
 1837:         '<p>'
 1838:        .&mt('Select roles, '.lc($type).' status, section(s) and group(s) for users'
 1839:            .' who will be able to access the portfolio file.')
 1840:        .'</p>'
 1841:     );
 1842:     $r->print(
 1843:         '<form name="rolepicker" action="/adm/portfolio" method="post">'
 1844:        .'<table><tr>'
 1845:        .'<th>'.&mt('Roles').'</th>'
 1846:        .'<th>'.&mt("$type status").'</th>'
 1847:        .'<th>'.&mt('Sections').'</th>'
 1848:        .'<th>'.&mt('Groups').'</th>'
 1849:        .'</tr><tr>'.$role_selects.'</tr>'
 1850:        .'</table><br />'
 1851:        .'<input type="button" name="rolepickbutton" value="'.&mt('Save').'" onclick="setRoles()" />'
 1852:     );
 1853:     return;
 1854: }
 1855: 
 1856: sub select_files {
 1857:     my ($r) = @_;
 1858:     if ($env{'form.continue'} eq 'true') {
 1859:         # here we update the selections for the currentpath
 1860:         # eventually, have to handle removing those not checked, but . . . 
 1861:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
 1862:         if (scalar(@items)){
 1863:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
 1864:         }
 1865:     } else {
 1866:             #empty the file for a fresh start
 1867:             &Apache::lonnet::clear_selected_files($env{'user.name'});
 1868:     }
 1869:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
 1870:     my $java_files = join ",", @files;
 1871:     if ($java_files) {
 1872:         $java_files.=',';
 1873:     }
 1874:     my $javascript =(<<ENDSMP);
 1875:         <script type="text/javascript">
 1876:         function finishSelect() {
 1877: ENDSMP
 1878:     $javascript .= 'fileList = "'.$java_files.'";';
 1879:     $javascript .= (<<ENDSMP);
 1880:             for (i=0;i<document.forms.checkselect.length;i++) { 
 1881:                 if (document.forms.checkselect[i].checked){
 1882:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
 1883:                 }
 1884:             }
 1885:             var hwfield = opener.document.getElementsByName('$env{'form.fieldname'}');
 1886:             hwfield[0].value = fileList;
 1887:             self.close();
 1888:         }
 1889:         </script>
 1890: ENDSMP
 1891:     $r->print($javascript);
 1892:     $r->print("<h1>".&mt('Select portfolio files')."</h1>");
 1893:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
 1894:     if (@otherfiles) {
 1895: 	$r->print(&Apache::loncommon::start_data_table()
 1896:                  .&Apache::loncommon::start_data_table_header_row()
 1897:                  .'<th>'.&mt('Files selected from other directories:')."</th>"
 1898:                  .&Apache::loncommon::end_data_table_header_row()
 1899:         );
 1900: 	foreach my $file (@otherfiles) {
 1901: 	    $r->print(&Apache::loncommon::start_data_table_row()
 1902:                      .'<td>'.$file."</td>"
 1903:                      .&Apache::loncommon::end_data_table_row()
 1904:             );
 1905: 	}
 1906:         $r->print(&Apache::loncommon::end_data_table()
 1907:                  .'<br />'
 1908:         );
 1909:     }
 1910:     $r->print('<div>'
 1911:              .&mt('Check as many files as you wish in response to the problem:')
 1912:              .'</div>'
 1913:     );
 1914: }
 1915: 
 1916: 
 1917: sub check_for_upload {
 1918:     my ($path,$fname,$group,$element) = @_;
 1919:     my $disk_quota = &get_quota($group);
 1920:     my $filesize = (length($env{'form.'.$element})) / 1000; #express in k (1024?)
 1921:     my $portfolio_root = &get_portfolio_root();
 1922:     my $port_path = &get_port_path();
 1923:     my ($uname,$udom) = &get_name_dom($group);
 1924:     # Fixme --- Move the checking for existing file to LOND error return
 1925:     my @dir_list=&get_dir_list($portfolio_root,$path,$group);
 1926:     my $found_file = 0;
 1927:     my $locked_file = 0;
 1928:     foreach my $line (@dir_list) {
 1929:         my ($file_name)=split(/\&/,$line,2);
 1930:         if ($file_name eq $fname){
 1931:             $file_name = $path.$file_name;
 1932:             $file_name = &prepend_group($file_name);
 1933:             $found_file = 1;
 1934:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
 1935:                 $locked_file = 1;
 1936:             } 
 1937:         }
 1938:     }
 1939:     my $getpropath = 1;
 1940:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
 1941: 
 1942:     if (($current_disk_usage + $filesize) > $disk_quota){
 1943:         my $msg = '<span class="LC_error">'.
 1944:                 &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</span>'.
 1945:                   '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage);
 1946: 	return ('will_exceed_quota',$msg);
 1947:     } elsif ($found_file) {
 1948:         if ($locked_file) {
 1949:             my $msg = '<span class="LC_error">';
 1950:             $msg .= &mt('Unable to upload [_1]. A locked file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>','<span class="LC_filename">'.$port_path.$env{'form.currentpath'}.'</span>');
 1951:             $msg .= '</span><br />';
 1952:             $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
 1953: 	    return ('file_locked',$msg);
 1954: 	} else {
 1955:             my $msg = '<span class="LC_error">';
 1956:             $msg .= &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
 1957:             $msg .= '</span>';
 1958:             $msg .= '<br />';
 1959:             $msg .= &mt('To upload, rename or delete existing [_1] in [_2].','<span class="LC_filename">'.$fname.'</span>', $port_path.$env{'form.currentpath'});
 1960: 	    return ('file_exists',$msg);
 1961: 	}
 1962:     }
 1963: }
 1964: 
 1965: sub upload {
 1966:     my ($r,$url,$group)=@_;
 1967:     my $fname=&Apache::lonnet::clean_filename($env{'form.uploaddoc.filename'});
 1968:     my $disk_quota = &get_quota($group);
 1969:     my $portfolio_root = &get_portfolio_root();
 1970:     my $port_path = &get_port_path();
 1971:     my ($uname,$udom) = &get_name_dom($group);
 1972:     my $getpropath = 1;
 1973:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
 1974:     my ($state,$msg) = 
 1975:         &Apache::loncommon::check_for_upload($env{'form.currentpath'},$fname,
 1976: 		                             $group,'uploaddoc',$portfolio_root,
 1977:                                              $port_path,$disk_quota,
 1978:                                              $current_disk_usage,$uname,$udom);
 1979:     if ($state eq 'will_exceed_quota'
 1980: 	|| $state eq 'file_locked'
 1981: 	|| $state eq 'file_exists' ) {
 1982: 	$r->print($msg.&done('Back',$url));
 1983: 	return;
 1984:     }
 1985: 
 1986:     my (%allfiles,%codebase,$mode);
 1987:     if ($env{'form.uploaddoc.filename'} =~ m/(\.htm|\.html|\.shtml)$/i) {
 1988:         if ($env{'form.parserflag'}) {
 1989: 	    $mode = 'parse';
 1990:         }
 1991:     }
 1992:     my $result=
 1993: 	&Apache::lonnet::userfileupload('uploaddoc','',
 1994: 					$port_path.$env{'form.currentpath'},
 1995: 					$mode,\%allfiles,\%codebase);
 1996:     if ($result !~ m|^/uploaded/|) {
 1997: 	$r->print('<span class="LC_error">'.&mt('An error occurred ([_1]) while trying to upload [_2].'
 1998:                   ,$result,&display_file()).'</span><br />');
 1999: 	$r->print(&done('Back',$url));
 2000:     } else {
 2001: 	if (%allfiles) {
 2002:             if (!&suppress_embed_prompt()) {
 2003: 	        my $state = <<STATE;
 2004:     <input type="hidden" name="action"      value="upload_embedded" />
 2005:     <input type="hidden" name="currentpath" value="$env{'form.currentpath'}" />
 2006:     <input type="hidden" name="fieldname"   value="$env{'form.fieldname'}" />
 2007:     <input type="hidden" name="mode"        value="$env{'form.mode'}" />
 2008: STATE
 2009:                 $r->print("<h2>".&mt("Reference Warning")."</h2>");
 2010:                 $r->print("<p>".&mt("Completed upload of the file. This file contained references to other files. You must upload the referenced files or else the uploaded file may not work properly.")."</p>");
 2011:                 $r->print("<p>".&mt("Please select the locations from which the referenced files are to be uploaded.")."</p>");
 2012: 	        $r->print(&Apache::loncommon::ask_for_embedded_content('/adm/portfolio',$state,\%allfiles,\%codebase,
 2013: 				      {'error_on_invalid_names'   => 1,
 2014: 				       'ignore_remote_references' => 1,}));
 2015: 	        $r->print('<p>Or '.&done('Return to directory',$url).'</p>');
 2016:             }
 2017: 	} else {
 2018: 	    $r->print(&done(undef,$url));
 2019: 	}
 2020:     }
 2021: }
 2022: 
 2023: sub lock_info {
 2024:     my ($r,$url,$group) = @_;
 2025:     my ($uname,$udom) = &get_name_dom($group);
 2026:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
 2027:                                                                        $uname);
 2028:     my $file_name = $env{'form.lockinfo'};
 2029:     $file_name = &prepend_group($file_name);
 2030:     if (defined($file_name) && defined($$current_permissions{$file_name})) {
 2031:         foreach my $array_item (@{$$current_permissions{$file_name}}) {
 2032:             next if (ref($array_item) ne 'ARRAY');
 2033: 
 2034: 	    my $filetext;
 2035: 	    if (defined($group)) {
 2036: 		$filetext = '<strong>'.$env{'form.lockinfo'}.
 2037: 		    '</strong> (group: '.$group.')'; 
 2038: 	    } else {
 2039: 		$filetext = '<strong>'.$file_name.'</strong>';
 2040: 	    } 
 2041: 	    
 2042: 	    my $title ='<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
 2043: 		'</strong><br />';
 2044: 	    if ($$array_item[-1] eq 'graded') {
 2045: 		$r->print(&mt('[_1] was submitted in response to problem: [_2]',
 2046:                               $filetext,$title));
 2047: 	    } elsif ($$array_item[-1] eq 'handback') {
 2048: 		$r->print(&mt('[_1] was handed back in response to problem: [_2]',
 2049:                               $filetext,$title));
 2050: 	    } else {
 2051: 		# submission style lock
 2052: 		$r->print(&mt('[_1] was submitted in response to problem: [_2]',
 2053:                               $filetext,$title));
 2054: 	    }
 2055: 	    my %course_description = 
 2056: 		&Apache::lonnet::coursedescription($$array_item[1]);
 2057: 	    if ( $course_description{'description'} ne '') {
 2058: 		$r->print(&mt('In the course:').' <strong>'.$course_description{'description'}.'</strong><br />');
 2059: 	    }
 2060:         }
 2061:     }
 2062:     $r->print(&done(&mt('Back'),$url));
 2063:     return 'ok';
 2064: }
 2065: 
 2066: sub createdir {
 2067:     my ($r,$url,$group)=@_;
 2068:     my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
 2069:     if ($newdir eq '') {
 2070:     	$r->print('<span class="LC_error">'.
 2071: 	    	  &mt("Error: no directory name was provided.").
 2072: 		      '</span><br />');
 2073: 	    $r->print(&done(undef,$url));
 2074: 	    return;
 2075:     }
 2076:     my $portfolio_root = &get_portfolio_root(); 
 2077:     my @dir_list=&get_dir_list($portfolio_root,undef,$group);
 2078:     my $found_file = 0;
 2079:     foreach my $line (@dir_list) {
 2080:         my ($filename)=split(/\&/,$line,2);
 2081:         if ($filename eq $newdir){
 2082:             $found_file = 1;
 2083:         }
 2084:     }
 2085:     if ($found_file){
 2086:     	    $r->print('<span class="LC_error">'
 2087:                       .&mt('Unable to create a directory named [_1].','<strong>'.$newdir.'</strong>')
 2088:                       .' '.&mt('A file or directory by that name already exists.').'</span><br />');
 2089:     } else {
 2090:         my ($uname,$udom) = &get_name_dom($group);
 2091:         my $port_path = &get_port_path();
 2092:         my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
 2093: 	         $port_path.$env{'form.currentpath'}.$newdir);
 2094:         if ($result ne 'ok') {
 2095:     	    $r->print('<span class="LC_error">'
 2096:                       .&mt('An error occurred ([_1]) while trying to create a new directory [_2].'
 2097:                            ,$result,&display_file())
 2098:                       .'</span><br />');
 2099:         }
 2100:     }
 2101:     if ($newdir ne $env{'form.newdir'}) {
 2102:         $r->print(&mt('The new directory name was changed from [_1] to [_2].'
 2103:                       ,'<strong>'.$env{'form.newdir'}.'</strong>','<strong>'.$newdir.'</strong>'));  
 2104:     }
 2105:     $r->print(&done(undef,$url));
 2106: }
 2107: 
 2108: sub get_portfolio_root {
 2109:     my ($udom,$uname,$group) = @_;
 2110:     if (!(defined($udom)) || !(defined($uname))) {
 2111:         ($uname,$udom) = &get_name_dom($group);
 2112:     }
 2113:     my $path = '/userfiles/portfolio';
 2114:     if (!defined($group)) { 
 2115:         if (defined($env{'form.group'})) {
 2116:             $group = $env{'form.group'};      
 2117:         }
 2118:     }
 2119:     if (defined($group)) {
 2120:         $path = '/userfiles/groups/'.$group.'/portfolio';
 2121:     } 
 2122:     return $path;
 2123: }
 2124: 
 2125: sub get_group_quota {
 2126:     my ($group) = @_;
 2127:     my $group_quota; 
 2128:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2129:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 2130:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
 2131:     if (%curr_groups) {
 2132:         my %group_info =  &Apache::longroup::get_group_settings(
 2133:                                                     $curr_groups{$group});
 2134:         $group_quota = $group_info{'quota'}; #expressed in Mb
 2135:         if ($group_quota) {
 2136:             $group_quota = 1000 * $group_quota; #expressed in k
 2137:         }
 2138:     }
 2139:     return $group_quota;
 2140: }
 2141: 
 2142: sub get_dir_list {
 2143:     my ($portfolio_root,$path,$group) = @_;
 2144:     $path ||= $env{'form.currentpath'};
 2145:     my ($uname,$udom) = &get_name_dom($group);
 2146:     my $getpropath = 1;
 2147:     return &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,$getpropath);
 2148: }
 2149: 
 2150: sub get_name_dom {
 2151:     my ($group) = @_;
 2152:     my ($uname,$udom);
 2153:     if (defined($group)) {
 2154:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2155:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
 2156:     } else {
 2157:         $udom = $env{'user.domain'};
 2158:         $uname = $env{'user.name'};
 2159:     }
 2160:     return ($uname,$udom);
 2161: }
 2162: 
 2163: sub prepend_group {
 2164:     my ($filename) = @_;
 2165:     if (defined($env{'form.group'})) {
 2166:         $filename = $env{'form.group'}.$filename;
 2167:     }
 2168:     return $filename;
 2169: }
 2170: 
 2171: sub get_namespace {
 2172:     my $namespace = 'portfolio';
 2173:     if (defined($env{'form.group'})) {
 2174:         my ($uname,$udom) = &get_name_dom($env{'form.group'});
 2175:         $namespace .= '_'.$udom.'_'.$uname.'_'.$env{'form.group'};
 2176:     }
 2177:     return $namespace;
 2178: }
 2179: 
 2180: sub get_port_path {
 2181:     my $port_path;
 2182:     if (defined($env{'form.group'})) {
 2183:        $port_path = "groups/$env{'form.group'}/portfolio";
 2184:     } else {
 2185:        $port_path = 'portfolio';
 2186:     }
 2187:     return $port_path;
 2188: }
 2189: 
 2190: sub missing_priv {
 2191:     my ($r,$url,$priv) = @_;
 2192:     my $longtext = {
 2193:                       upload => 'upload files',
 2194:                       delete => 'delete files',
 2195:                       rename => 'rename files',
 2196:                       setacl => 'set access controls for files',
 2197:                    };
 2198:     my $escpath = &HTML::Entities::encode($env{'form.currentpath'},'&<>"');
 2199:     my $rtnlink = '<a href="'.$url;
 2200:     if ($url =~ /\?/) {
 2201:         $rtnlink .= '&';
 2202:     } else {
 2203:         $rtnlink .= '?';
 2204:     }
 2205:     $rtnlink .= 'currentpath='.$escpath;
 2206:     $r->print('<h3>'.&mt('Action disallowed').'</h3>');
 2207:     $r->print(&mt('You do not have sufficient privileges to [_1] ',
 2208:                   $longtext->{$priv}));
 2209:     if (defined($env{'form.group'})) {
 2210:         $r->print(&mt("in the group's group portfolio."));
 2211:         $rtnlink .= &group_args()
 2212:     } else {
 2213:         $r->print(&mt('in this portfolio.'));
 2214:     }
 2215:     $rtnlink .= '">'.&mt('Return to directory').'</a>';
 2216:     $r->print('<br />'.$rtnlink);
 2217:     $r->print(&Apache::loncommon::end_page());
 2218:     return;
 2219: }
 2220: 
 2221: sub coursegrp_portfolio_header {
 2222:     my ($cdom,$cnum,$grp_desc)=@_;
 2223:     my $gpterm  = &Apache::loncommon::group_term();
 2224:     my $ucgpterm = $gpterm;
 2225:     $ucgpterm =~ s/^(\w)/uc($1)/e;
 2226:     if ($env{'form.ref'}) {
 2227:         &Apache::lonhtmlcommon::add_breadcrumb
 2228:             ({href=>"/adm/coursegroups",
 2229:               text=>"Groups",
 2230:               title=>"Course Groups"});
 2231:     }
 2232:     &Apache::lonhtmlcommon::add_breadcrumb
 2233:         ({href=>"/adm/$cdom/$cnum/$env{'form.group'}/smppg?ref=$env{'form.ref'}",
 2234:           text=>"$ucgpterm: $grp_desc",
 2235:           title=>"Go to group's home page"},
 2236:          {href=>"/adm/coursegrp_portfolio?".&group_args(),
 2237:           text=>"Group Portfolio",
 2238:           title=>"Display group portfolio"});
 2239:     my $output = &Apache::lonhtmlcommon::breadcrumbs(
 2240:                          &mt('[_1] portfolio files - [_2]',$gpterm,$grp_desc));
 2241:     return $output;
 2242: }
 2243: 
 2244: sub get_quota {
 2245:     my ($group) = @_;
 2246:     my $disk_quota;
 2247:     if (defined($group)) {
 2248:         my $grp_quota = &get_group_quota($group); # quota expressed in k
 2249:         if ($grp_quota ne '') {
 2250:             $disk_quota = $grp_quota;
 2251:         } else {
 2252:             $disk_quota = 0;
 2253:         }
 2254:     } else {
 2255:         $disk_quota = &Apache::loncommon::get_user_quota($env{'user.name'},
 2256:                                     $env{'user.domain'}); #expressed in Mb
 2257:         $disk_quota = 1000 * $disk_quota; # convert from Mb to kb
 2258:     }
 2259:     return $disk_quota;
 2260: }
 2261: 
 2262: sub suppress_embed_prompt {
 2263:     my $suppress_prompt = 0;
 2264:     if (($env{'request.role'} =~ /^st/) && ($env{'request.course.id'} ne '')) {
 2265:         if ($env{'course.'.$env{'request.course.id'}.'.suppress_embed_prompt'} eq 'yes') {
 2266:             $suppress_prompt = 1;
 2267:         }
 2268:     }
 2269:     return $suppress_prompt;
 2270: }
 2271: 
 2272: 
 2273: sub handler {
 2274:     # this handles file management
 2275:     my $r = shift;
 2276:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 2277:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
 2278: 	  'fieldname','mode','rename','continue','group','access','setnum',
 2279:           'cnum','cdom','type','setroles','showversions','ref']);
 2280:     my ($uname,$udom,$portfolio_root,$url,$caller,$title,$group,$grp_desc);
 2281:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
 2282:         $url = $1.$2;
 2283:         $caller = $2;
 2284:     }
 2285:     my ($can_modify,$can_delete,$can_upload,$can_setacl);
 2286:     if ($caller eq 'coursegrp_portfolio') {
 2287:     #  Needs to be in a course
 2288:         if (! ($env{'request.course.fn'})) {
 2289:         # Not in a course
 2290:             $env{'user.error.msg'}=
 2291:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
 2292:             return HTTP_NOT_ACCEPTABLE;
 2293:         }
 2294:         my $earlyout = 0;
 2295:         my $view_permission = 
 2296:            &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
 2297:         $env{'form.group'} =~ s/\W//g;
 2298: 	$group = $env{'form.group'};
 2299:         if ($group ne '') {
 2300:             ($uname,$udom) = &get_name_dom($group);
 2301:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
 2302: 							       $group); 
 2303:             if (%curr_groups) {
 2304:                 my %grp_content = &Apache::longroup::get_group_settings(
 2305:                                                          $curr_groups{$group});
 2306:                 $grp_desc = &unescape($grp_content{'description'});
 2307:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
 2308:                                       $env{'request.course.id'}.'/'.$group))) {
 2309:                     $portfolio_root = &get_portfolio_root();
 2310:                 } else {
 2311:                     $r->print(&mt('You do not have the privileges required to access the shared files space for this group.'));
 2312:                     $earlyout = 1;
 2313:                 }
 2314:             } else {
 2315:                 $r->print(&mt('Not a valid group for this course'));
 2316:                 $earlyout = 1;
 2317:             }
 2318:             $title = &mt('Group portfolio for [_1]', $group); 
 2319:         } else {
 2320:             $r->print(&mt('Invalid group'));
 2321:             $earlyout = 1;
 2322:         }
 2323:         if ($earlyout) { return OK; }
 2324:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
 2325:             $can_modify = 1;
 2326:             $can_delete = 1;
 2327:             $can_upload = 1;
 2328:             $can_setacl = 1;
 2329:         } else {
 2330:             if (&Apache::lonnet::allowed('agf',$env{'request.course.id'}.'/'.$group)) {
 2331:                 $can_setacl = 1;
 2332:             }
 2333:             if (&Apache::lonnet::allowed('ugf',$env{'request.course.id'}.'/'.$group)) {
 2334:                 $can_upload = 1;
 2335:             }
 2336:             if (&Apache::lonnet::allowed('mgf',$env{'request.course.id'}.'/'.$group)) {
 2337:                 $can_modify = 1;
 2338:             }
 2339:             if (&Apache::lonnet::allowed('dgf',$env{'request.course.id'}.'/'.$group)) {
 2340:                 $can_delete = 1;
 2341:             }
 2342:         }
 2343:     } else {
 2344:         ($uname,$udom) = &get_name_dom();
 2345:         $portfolio_root = &get_portfolio_root();
 2346:         $title = &mt('My Space');
 2347:         $can_modify = 1;
 2348:         $can_delete = 1;
 2349:         $can_upload = 1;
 2350:         $can_setacl = 1;
 2351:     }
 2352: 
 2353:     my $port_path = &get_port_path();
 2354:     &Apache::loncommon::no_cache($r);
 2355:     &Apache::loncommon::content_type($r,'text/html');
 2356:     $r->send_http_header;
 2357:     # Give the LON-CAPA page header
 2358:     my $brcrum = [{href=>"/adm/portfolio",text=>"Portfolio Manager"}];
 2359: 
 2360:     if ($env{"form.mode"} eq 'selectfile'){
 2361:         $r->print(&Apache::loncommon::start_page($title,undef,
 2362: 						 {'only_body' => 1}));
 2363:     } elsif ($env{'form.action'} eq 'rolepicker') {
 2364:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
 2365:                                                  {'no_nav_bar'  => 1, }));
 2366:     } elsif ($caller eq 'coursegrp_portfolio') {
 2367:         $r->print(&Apache::loncommon::start_page($title));
 2368:     } else {
 2369:         $r->print(&Apache::loncommon::start_page($title,undef,
 2370:                                                  {'bread_crumbs' => $brcrum}));
 2371:         if (!&Apache::lonnet::usertools_access($uname,$udom,'portfolio')) {
 2372:             $r->print('<h2>'.&mt('No user portfolio available') .'</h2>'.
 2373:                       &mt('This is a result of one of the following:').'<ul>'.
 2374:                       '<li>'.&mt('The administrator of this domain has disabled portfolio functionality for this specific user.').'</li>'.
 2375:                       '<li>'.&mt('The domain has been configured to disable, by default, portfolio functionality for all users in the domain.').'</li>'.
 2376:                       '</ul>');
 2377:             $r->print(&Apache::loncommon::end_page());
 2378:             return OK;
 2379:         }
 2380:     }
 2381:     $r->rflush();
 2382:     my ($blocked,$blocktext) = 
 2383:         &Apache::loncommon::blocking_status('port',$uname,$udom);
 2384:     if ($blocked) {
 2385:          $r->print($blocktext);
 2386:          $r->print(&Apache::loncommon::end_page());
 2387:          return OK;
 2388:     }
 2389: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
 2390:    	    $r->print('<span class="LC_error">');
 2391:    	    $r->print(&mt('No file was selected to upload.').' ');
 2392: 	    $r->print(&mt('To upload a file, click <strong>Browse...</strong> and select a file, then click <strong>Upload</strong>.'));
 2393: 	    $r->print('</span>');
 2394: 	}
 2395:     if ($env{'form.meta'}) {
 2396:         &open_form($r,$url);
 2397:         $r->print(&mt('Edit the meta data').'<br />');
 2398:         &close_form($r,$url);
 2399:     }
 2400:     if ($env{'form.store'}) {
 2401:     }
 2402: 
 2403:     if ($env{'form.uploaddoc.filename'}) {
 2404:         if ($can_upload) {
 2405: 	    &upload($r,$url,$group);
 2406:         } else {
 2407:             &missing_priv($r,$url,'upload');
 2408:         }
 2409:     } elsif ($env{'form.action'} eq 'upload_embedded') {
 2410: 	if ($can_upload) {
 2411:             my $disk_quota = &get_quota($group);
 2412:             my $getpropath = 1;
 2413:             my $current_disk_usage = 
 2414:                 &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
 2415: 	    $r->print(
 2416:                 &Apache::loncommon::upload_embedded('portfolio',$port_path,$uname,$udom,
 2417:                     $group,$portfolio_root,$group,$disk_quota,$current_disk_usage));
 2418:             $r->print(&done(undef,$url));
 2419:         } else {
 2420:             &missing_priv($r,$url,'upload');
 2421:         }
 2422:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
 2423:         if ($can_delete) {
 2424: 	    &delete_confirmed($r,$url,$group);
 2425:         } else {
 2426:             &missing_priv($r,$url,'delete');
 2427:         }
 2428:     } elsif ($env{'form.action'} eq 'delete') {
 2429:         if ($can_delete) {
 2430: 	    &delete($r,$url,$group);
 2431:         } else {
 2432:             &missing_priv($r,$url,'delete');
 2433:         }
 2434:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
 2435:         if ($can_delete) {
 2436: 	    &delete_dir_confirmed($r,$url,$group);
 2437:         } else {
 2438:             &missing_priv($r,$url,'delete');
 2439:         }
 2440:     } elsif ($env{'form.action'} eq 'deletedir') {
 2441:         if ($can_delete) {
 2442: 	    &delete_dir($r,$url);
 2443:         } else {
 2444:             &missing_priv($r,$url,'delete');
 2445:         }
 2446:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
 2447:         if ($can_modify) {
 2448: 	    &rename_confirmed($r,$url,$group);
 2449:         } else {
 2450:             &missing_priv($r,$url,'rename');
 2451:         }
 2452:     } elsif ($env{'form.rename'}) {
 2453:         $env{'form.selectfile'} = $env{'form.rename'};
 2454:         $env{'form.action'} = 'rename';
 2455:         if ($can_modify) {
 2456: 	    &rename($r,$url,$group);
 2457:         } else {
 2458:             &missing_priv($r,$url,'rename');
 2459:         }
 2460:     } elsif ($env{'form.access'}) {
 2461:         $env{'form.selectfile'} = $env{'form.access'};
 2462:         if (!defined($env{'form.action'})) { 
 2463:             $env{'form.action'} = 'chgaccess';
 2464:         }
 2465:         &display_access($r,$url,$group,$can_setacl,$port_path,$env{'form.action'});
 2466:     } elsif (($env{'form.action'} eq 'chgaccess') || 
 2467:              ($env{'form.action'} eq 'chgconditions')) {
 2468:         if ($can_setacl) {
 2469:             &update_access($r,$url,$group,$port_path);
 2470:         } else {
 2471:             &missing_priv($r,$url,'setacl');
 2472:         }
 2473:     } elsif ($env{'form.action'} eq 'rolepicker') {
 2474:         if ($can_setacl) { 
 2475:             &role_options_window($r);
 2476:         } else {
 2477:             &missing_priv($r,$url,'setacl');
 2478:         }
 2479:     } elsif ($env{'form.createdir'}) {
 2480:         if ($can_upload) {
 2481: 	    &createdir($r,$url,$group);
 2482:         } else {
 2483:             &missing_priv($r,$url,'upload');
 2484:         }
 2485:     } elsif ($env{'form.lockinfo'}) {
 2486:         &lock_info($r,$url,$group);
 2487:     } else {
 2488: 	my $current_path='/';
 2489: 	if ($env{'form.currentpath'}) {
 2490: 	    $current_path = $env{'form.currentpath'};
 2491: 	}
 2492:         if ($caller eq 'coursegrp_portfolio') {
 2493:             &Apache::lonhtmlcommon::clear_breadcrumbs();
 2494:             $r->print(&coursegrp_portfolio_header($udom,$uname,$grp_desc));
 2495:         }
 2496:         my @dir_list=&get_dir_list($portfolio_root,$current_path,$group);
 2497: 	if ($dir_list[0] eq 'no_such_dir'){
 2498: 	    # two main reasons for this:
 2499:             #    1) never been here, so directory structure not created
 2500: 	    #    2) back-button navigation after deleting a directory
 2501: 	    if ($current_path eq '/'){
 2502: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
 2503: 					       &get_port_path());
 2504: 	    } else {
 2505:                 # some directory that snuck in get rid of the directory
 2506:                 # from the recent pulldown, just in case
 2507: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
 2508: 						      [$current_path]);
 2509: 		$current_path = '/'; # force it back to the root        
 2510: 	    }
 2511: 	    # now grab the directory list again, for the first time
 2512:             @dir_list=&get_dir_list($portfolio_root,$current_path,$group);
 2513:         }
 2514: 	# need to know if directory is empty so it can be removed if desired
 2515: 	my $is_empty=(@dir_list == 2);
 2516: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,
 2517: 			$can_upload,$group);
 2518:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group,
 2519:                            $can_upload,$can_modify,$can_delete,$can_setacl);
 2520: 	$r->print(&Apache::loncommon::end_page());
 2521:     }
 2522:     return OK;
 2523: }
 2524: 
 2525: 1;
 2526: __END__

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