File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.221: download - view: text, annotated - select for diffs
Fri Mar 26 22:27:06 2010 UTC (14 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Accommodate Communities.
- Remove html from phrases to be translated.

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

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