File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.271: download - view: text, annotated - select for diffs
Sat Sep 7 19:13:00 2024 UTC (5 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Help link to right of "Current Access Status" column header points at
  Portfolio_ShareFile_Group.hlp in group context, regardless of privileges.

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

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