File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.165: download - view: text, annotated - select for diffs
Thu Oct 19 20:36:12 2006 UTC (17 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- style
- correcting mt usage
- only cycle through the keys in the has

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

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