File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.232: download - view: text, annotated - select for diffs
Fri May 27 18:55:54 2011 UTC (12 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_1, version_2_10_0, loncapaMITrelate_1, HEAD
- Duplicate declaration.

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

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