File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.166: download - view: text, annotated - select for diffs
Thu Oct 19 21:23:22 2006 UTC (17 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
&mt() an additional error message.  Loop through keys of hash.  Stop concatenating keys when creating new access control record for a renamed file.

    1: # The LearningOnline Network
    2: # portfolio browser
    3: #
    4: # $Id: portfolio.pm,v 1.166 2006/10/19 21:23:22 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or 
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::portfolio;
   30: use strict;
   31: use Apache::Constants qw(:common :http);
   32: use Apache::loncommon;
   33: use Apache::lonnet;
   34: use Apache::lontexconvert;
   35: use Apache::lonfeedback;
   36: use Apache::lonlocal;
   37: use Apache::lonnet;
   38: use Apache::longroup;
   39: use Apache::lonhtmlcommon;
   40: use HTML::Entities;
   41: use LONCAPA;
   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">'.
  554:                      &mt('An error occured ([_1]) while trying to delete 
  555:                          [_2].',$result,&display_file(undef, $delete_file)).
  556:                       '</span><br /><br />');
  557:         } else {
  558:             $r->print(&mt('File: [_1] deleted.',
  559:                           &display_file(undef,$delete_file)));
  560:             my $file_name = $env{'form.currentpath'}.$delete_file;
  561:             $file_name = &prepend_group($file_name);
  562:             my %access_controls = 
  563:                     &Apache::lonnet::get_access_controls($current_permissions,
  564:                                                          $group,$file_name);
  565:             if (keys(%access_controls) > 0) {
  566:                 my %changes; 
  567:                 foreach my $key (keys(%{$access_controls{$file_name}})) {
  568:                     $changes{'delete'}{$key} = 1;
  569:                 }
  570:                 if (keys(%changes) > 0) {
  571:                     my ($outcome,$deloutcome,$new_values,$translation) =
  572:                     &Apache::lonnet::modify_access_controls($file_name,\%changes,
  573:                                                             $udom,$uname);
  574:                     if ($outcome ne 'ok') {
  575:                            $r->print('<br />'.&mt("An error occured ([_1]) while ".
  576:                                "trying to delete access controls for the file.",$outcome).
  577:                                '</span><br /><br />');
  578:                     } else {
  579:                         if ($deloutcome eq 'ok') {
  580:                             $r->print('<br />'.&mt('Access controls also deleted for the file.').'<br /><br />');
  581:                         } else {
  582:                             $r->print('<span class="LC_error">'.'<br />'.
  583:                                &mt("An error occured ([_1]) while ".
  584:                                    "trying to delete access controls for the file.",$deloutcome).
  585:                                    '</span><br /><br />');
  586:                         }
  587:                     }
  588:                 }
  589:             }
  590:         }
  591:     }
  592:     $r->print(&done(undef,$url));
  593: }
  594: 
  595: sub delete_dir {
  596:     my ($r,$url)=@_;
  597:     &open_form($r,$url);
  598:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
  599:     &close_form($r,$url);
  600: } 
  601: 
  602: sub delete_dir_confirmed {
  603:     my ($r,$url)=@_;
  604:     my $directory_name = $env{'form.currentpath'};
  605:     $directory_name =~ s|/$||; # remove any trailing slash
  606:     my ($uname,$udom) = &get_name_dom();
  607:     my $namespace = &get_namespace();
  608:     my $port_path = &get_port_path();
  609:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  610: 					       $directory_name);
  611: 					       
  612:     if ($result ne 'ok') {
  613: 	$r->print('<span class="LC_error"> An error occured (dir) ('.$result.
  614: 		  ') while trying to delete '.$directory_name.'</span><br />');
  615:     } else {
  616:         # now remove from recent
  617: #        $r->print('<br /> removing '.$directory_name.'<br /');
  618:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
  619:         my @dirs = split m!/!, $directory_name;
  620:         
  621: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
  622:         $directory_name='/';
  623:         for (my $i=1; $i < (@dirs - 1); $i ++){
  624:             $directory_name .= $dirs[$i].'/';
  625:         }
  626:         $env{'form.currentpath'} = $directory_name;
  627:     }
  628:     $r->print(&done(undef,$url));
  629: }
  630: 
  631: sub rename {
  632:     my ($r,$url)=@_;
  633:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
  634:     my ($uname,$udom) = &get_name_dom();
  635:     $file_name = &prepend_group($file_name);
  636:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  637:         $r->print ("The file is locked and cannot be renamed.<br />");
  638:         $r->print(&done(undef,$url));
  639:     } else {
  640:         &open_form($r,$url);
  641:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  642:                    <input name="filenewname" type="input" size="50" />?</p>');
  643:         &close_form($r,$url);
  644:     }
  645: }
  646: 
  647: sub rename_confirmed {
  648:     my ($r,$url,$group)=@_;
  649:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
  650:     my ($uname,$udom) = &get_name_dom();
  651:     my $port_path = &get_port_path();
  652:     if ($filenewname eq '') {
  653: 	$r->print('<span class="LC_error">'.
  654: 		  &mt("Error: no valid filename was provided to rename to.").
  655: 		  '</span><br />');
  656: 	$r->print(&done(undef,$url));
  657: 	return;
  658:     } 
  659:     my $chg_access;
  660:     my $result=
  661: 	&Apache::lonnet::renameuserfile($uname,$udom,
  662:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
  663:             $port_path.$env{'form.currentpath'}.$filenewname);
  664:     if ($result eq 'ok') {
  665:         $chg_access = &access_for_renamed($filenewname,$group,$udom,$uname);
  666:     } else {      
  667: 	$r->print('<span class="LC_error">'.
  668: 		  &mt('An error occured ([_1]) while trying to rename [_2]'
  669: 		      .' to [_3]',$result,&display_file(),
  670: 		      &display_file('',$filenewname)).'</span><br />');
  671:         return;
  672:     }
  673:     if ($filenewname ne $env{'form.filenewname'}) {
  674:         $r->print(&mt("The new file name was changed from:<br />[_1] to [_2]",
  675: 		      '<strong>'.&display_file('',$env{'form.filenewname'}).'</strong>',
  676: 		      '<strong>'.&display_file('',$filenewname).'</strong>'));
  677:     }
  678:     $r->print($chg_access);
  679:     $r->print(&done(undef,$url));
  680: }
  681: 
  682: sub access_for_renamed {
  683:     my ($filenewname,$group,$udom,$uname) = @_;
  684:     my $oldfile = $env{'form.currentpath'}.$env{'form.selectfile'};
  685:     $oldfile = &prepend_group($oldfile);
  686:     my $newfile = $env{'form.currentpath'}.$filenewname;
  687:     $newfile = &prepend_group($newfile);
  688:     my $current_permissions =
  689: 	&Apache::lonnet::get_portfile_permissions($udom,$uname);
  690:     my %access_controls =
  691: 	&Apache::lonnet::get_access_controls($current_permissions,
  692: 					     $group,$oldfile);
  693:     my $chg_text;
  694:     if (keys(%access_controls) > 0) {
  695:         my %change_old;
  696:         my %change_new;
  697:         foreach my $key (keys(%{$access_controls{$oldfile}})) {
  698:             $change_old{'delete'}{$key} = 1;
  699:             $change_new{'activate'}{$key} = $access_controls{$oldfile}{$key};
  700:         }
  701:         my ($outcome,$deloutcome,$new_values,$translation) =
  702:             &Apache::lonnet::modify_access_controls($oldfile,\%change_old,
  703: 						    $udom,$uname);
  704:         if ($outcome ne 'ok') {
  705:             $chg_text ='<br /><br />'.&mt("An error occured ([_1]) while ".
  706:                 "trying to delete access control records for the old name.",$outcome).
  707:                 '</span><br />';
  708:         } else {
  709:             if ($deloutcome ne 'ok') {
  710:                 $chg_text = '<br /><br /><span class="LC_error"><br />'.
  711: 		    &mt("An error occured ([_1]) while ".
  712: 			"trying to delete access control records for the old name.",$deloutcome).
  713: 			'</span><br />';
  714:             }
  715:         }
  716:         ($outcome,$deloutcome,$new_values,$translation) =
  717:             &Apache::lonnet::modify_access_controls($newfile,\%change_new,
  718:                                                     $udom,$uname);
  719:         if ($outcome ne 'ok') {
  720:             $chg_text .= '<br /><br />'.
  721: 		&mt("An error occured ([_1]) while ".
  722:                 "trying to update access control records for the new name.",$outcome).
  723:                 '</span><br />';
  724:         }
  725:         if ($chg_text eq '') {
  726:             $chg_text = '<br /><br />'.&mt('Access controls updated to reflect the name change.');
  727:         }
  728:     }
  729:     return $chg_text;
  730: }
  731: 
  732: sub display_access {
  733:     my ($r,$url,$group,$can_setacl,$port_path) = @_;
  734:     my ($uname,$udom) = &get_name_dom();
  735:     my $file_name = $env{'form.currentpath'}.$env{'form.access'};
  736:     $file_name = &prepend_group($file_name);
  737:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  738:                                                                         $uname);
  739:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group,$file_name);
  740:     my $aclcount = keys(%access_controls);
  741:     my $header = '<h3>'.&mt('Allowing others to retrieve file: [_1]',$port_path.$env{'form.currentpath'}.$env{'form.access'}).'</h3>';
  742:     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.');
  743:     $info .= '<br /><ul><li>'.&mt('Public files are available to anyone without the need for login.');
  744:     $info .= '</li><li>'.&mt('Passphrase-protected files do not require log-in, but will require the viewer to enter the passphrase you set.');
  745:     $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 />';
  746:     $info .= &mt('The conditions can include affiliation with a particular course or group, or a user account in a specific domain.').'<br />';
  747:     $info .= &mt('Alternatively access can be granted to people with specific LON-CAPA usernames and domains.').'</li></ul>';
  748:     if ($group eq '') {
  749:         $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 />"));
  750:     }
  751:     
  752:     if ($can_setacl) {
  753:         &open_form($r,$url);
  754:         $r->print($header.$info);
  755:         &access_setting_table($r,$access_controls{$file_name});
  756:         my $button_text = {
  757:                         'continue' => &mt('Proceed'),
  758:                         'cancel' => &mt('Back to directory listing'),
  759:                       };
  760:         &close_form($r,$url,$button_text);
  761:     } else {
  762:         $r->print($header);
  763:         if ($aclcount) {  
  764:             $r->print($info);
  765:         }
  766:         &view_access_settings($r,$url,$access_controls{$file_name},$aclcount);
  767:     }
  768: }
  769: 
  770: sub view_access_settings {
  771:     my ($r,$url,$access_controls,$aclcount) = @_;
  772:     my ($showstart,$showend);
  773:     my %todisplay;
  774:     foreach my $key (sort(keys(%{$access_controls}))) {
  775:         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  776:         $todisplay{$scope}{$key} = $$access_controls{$key};
  777:     }
  778:     if ($aclcount) {
  779:         $r->print(&mt('<h4>Current access controls defined for this file:</h4>'));
  780:         $r->print(&Apache::loncommon::start_data_table());
  781:         $r->print(&Apache::loncommon::start_data_table_header_row());
  782:         $r->print('<th>'.&mt('Access control').'</th><th>'.&mt('Dates available').
  783:                   '</th><th>'.&mt('Additional information').'</th>');
  784:         $r->print(&Apache::loncommon::end_data_table_header_row());
  785:         my $count = 1;
  786:         my $chg = 'none';
  787:         &build_access_summary($r,$count,$chg,%todisplay);
  788:         $r->print(&Apache::loncommon::end_data_table());
  789:     } else {
  790:         $r->print(&mt('No access control settings currently exist for this file.<br />' ));
  791:     }
  792:     my %anchor_fields = (
  793:         'currentpath' => $env{'form.currentpath'}
  794:     );
  795:     $r->print('<br />'.&make_anchor($url, \%anchor_fields, &mt('Return to directory listing')));
  796:     return;
  797: }
  798: 
  799: sub build_access_summary {
  800:     my ($r,$count,$chg,%todisplay) = @_; 
  801:     my ($showstart,$showend);
  802:     my %scope_desc = (
  803:                       public => 'Public',
  804:                       guest => 'Passphrase-protected',
  805:                       domains => 'Conditional: domain-based',
  806:                       users => 'Conditional: user-based',
  807:                       course => 'Conditional: course-based',
  808:                       group => 'Conditional: group-based',
  809:                      );
  810:     my @allscopes = ('public','guest','domains','users','course','group');
  811:     foreach my $scope (@allscopes) {
  812:         if ((!(exists($todisplay{$scope}))) || (ref($todisplay{$scope}) ne 'HASH')) {
  813:             next;
  814:         }
  815:         foreach my $key (sort(keys(%{$todisplay{$scope}}))) {
  816:             if ($count) {
  817:                 $r->print(&Apache::loncommon::start_data_table_row());
  818:             }
  819:             my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  820:             my $content = $todisplay{$scope}{$key};
  821:             if ($chg eq 'delete') {
  822:                 $showstart = &mt('Deleted');
  823:                 $showend = $showstart;
  824:             } else {
  825:                 $showstart = localtime($start);
  826:                 if ($end == 0) {
  827:                     $showend = &mt('No end date');
  828:                 } else {
  829:                     $showend = localtime($end);
  830:                 }
  831:             }
  832:             $r->print('<td>'.&mt($scope_desc{$scope}));
  833:             if (($scope eq 'course') || ($scope eq 'group')) {
  834:                 if ($chg ne 'delete') {
  835:                     my $cid = $content->{'domain'}.'_'.$content->{'number'};
  836:                     my %course_description = &Apache::lonnet::coursedescription($cid);
  837:                     $r->print('<br />('.$course_description{'description'}.')');
  838:                 }
  839:             }
  840:             $r->print('</td><td>'.&mt('Start: ').$showstart.
  841:                   '<br />'.&mt('End: ').$showend.'</td><td>');
  842:             if ($chg ne 'delete') {
  843:                 if ($scope eq 'guest') {
  844:                     $r->print(&mt('Passphrase').': '.$content->{'password'});
  845:                 } elsif ($scope eq 'course' || $scope eq 'group') {
  846:                     $r->print('<table><tr>');
  847:                     $r->print('<th>'.&mt('Roles').'</th><th>'.
  848:                           &mt('Access').'</th><th>'.
  849:                                           &mt('Sections').'</th>');
  850:                     if ($scope eq 'course') {
  851:                         $r->print('<th>'.&mt('Groups').'</th>');
  852:                     } else {
  853:                         $r->print('<th>'.&mt('Teams').'</th>');
  854:                     }
  855:                     $r->print('</tr>');
  856:                     foreach my $id (sort(keys(%{$content->{'roles'}}))) {
  857:                         $r->print('<tr>');
  858:                         foreach my $item ('role','access','section','group') {
  859:                             $r->print('<td>');
  860:                             if ($item eq 'role') {
  861:                                 my $ucscope = $scope;
  862:                                 $ucscope =~ s/^(\w)/uc($1)/e;
  863:                                 my $role_output;
  864:                                 foreach my $role (@{$content->{'roles'}{$id}{$item}}) {
  865:                                     if ($role eq 'all') {
  866:                                         $role_output .= $role.',';
  867:                                     } elsif ($role =~ /^cr/) {
  868:                                         $role_output .= (split('/',$role))[3].',';
  869:                                     } else {
  870:                                         $role_output .= &Apache::lonnet::plaintext($role,$ucscope).',';
  871:                                     }
  872:                                 }
  873:                                 $role_output =~ s/,$//;
  874:                                 $r->print($role_output);
  875:                             } else {
  876:                                 $r->print(join(',',@{$content->{'roles'}{$id}{$item}}));
  877:                             }
  878:                             $r->print('</tr>');
  879:                         }
  880: 			$r->print("</table>");
  881:                     }
  882: 		    $r->print("</tr></table>");
  883:                 } elsif ($scope eq 'domains') {
  884:                     $r->print(&mt('Domains: ').join(',',@{$content->{'dom'}}));
  885:                 } elsif ($scope eq 'users') {
  886:                     my $curr_user_list = &sort_users($content->{'users'});
  887:                     $r->print(&mt('Users: ').$curr_user_list);
  888:                 } else {
  889:                     $r->print('&nbsp;');
  890:                 }
  891:             } else {
  892:                 $r->print('&nbsp;');
  893:             }
  894:             $r->print('</td>');
  895:             $r->print(&Apache::loncommon::end_data_table_row());
  896:             $count ++;
  897:         }
  898:     }
  899: }
  900: 
  901: 
  902: sub update_access {
  903:     my ($r,$url,$group,$port_path) = @_;
  904:     my $totalprocessed = 0;
  905:     my %processing;
  906:     my %title  = (
  907:                          'activate' => 'New control(s) added',
  908:                          'delete'   => 'Existing control(s) deleted',
  909:                          'update'   => 'Existing control(s) modified',
  910:                      );
  911:     my $changes;
  912:     foreach my $chg (sort(keys(%title))) {     
  913:         @{$processing{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
  914:         $totalprocessed += @{$processing{$chg}};
  915:         foreach my $num (@{$processing{$chg}}) {
  916:             my $scope = $env{'form.scope_'.$num};
  917:             my ($start,$end) = &get_dates_from_form($num);
  918:             my $newkey = $num.':'.$scope.'_'.$end.'_'.$start;
  919:             if ($chg eq 'delete') {
  920:                 $$changes{$chg}{$newkey} = 1;
  921:             } else {
  922:                 $$changes{$chg}{$newkey} = 
  923:                             &build_access_record($num,$scope,$start,$end,$chg);
  924:             }
  925:         }
  926:     }
  927:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  928:     $r->print('<h3>'.&mt('Allowing others to retrieve file: [_1]',
  929:               $port_path.$file_name).'</h3>'."\n");
  930:     $file_name = &prepend_group($file_name);
  931:     my ($uname,$udom) = &get_name_dom();
  932:     my ($errors,$outcome,$deloutcome,$new_values,$translation);
  933:     if ($totalprocessed) {
  934:         ($outcome,$deloutcome,$new_values,$translation) =
  935:         &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
  936:                                                 $uname);
  937:     }
  938:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  939:                                                                        $uname);
  940:     my %access_controls = 
  941: 	&Apache::lonnet::get_access_controls($current_permissions,
  942: 					     $group,$file_name);
  943:     if ($totalprocessed) {
  944:         if ($outcome eq 'ok') {
  945:             my $updated_controls = $access_controls{$file_name};
  946:             my ($showstart,$showend);
  947:             $r->print(&Apache::loncommon::start_data_table());
  948:             $r->print(&Apache::loncommon::start_data_table_header_row());
  949:             $r->print('<th>'.&mt('Type of change').'</th><th>'.
  950:                       &mt('Access control').'</th><th>'.&mt('Dates available').
  951:                       '</th><th>'.&mt('Additional information').'</th>');
  952:             $r->print(&Apache::loncommon::end_data_table_header_row());
  953:             foreach my $chg (sort(keys(%processing))) {
  954:                 if (@{$processing{$chg}} > 0) {
  955:                     if ($chg eq 'delete') {
  956:                         if (!($deloutcome eq 'ok')) {
  957:                             $errors .='<span class="LC_error">'.
  958: 				&mt('A problem occurred deleting access controls: [_1]',$deloutcome).
  959: 				'</span>';
  960:                             next;
  961:                         }
  962:                     }
  963:                     my $numchgs = @{$processing{$chg}};
  964:                     $r->print(&Apache::loncommon::start_data_table_row());
  965:                     $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
  966:                               '.</td>');
  967:                     my $count = 0;
  968:                     my %todisplay;
  969:                     foreach my $key (sort(keys(%{$$changes{$chg}}))) {
  970:                         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  971:                         my $newkey = $key;
  972:                         if ($chg eq 'activate') {
  973:                             $newkey =~ s/^(\d+)/$$translation{$1}/;
  974:                         }
  975:                         $todisplay{$scope}{$newkey} = $$updated_controls{$newkey};
  976:                     }
  977:                     &build_access_summary($r,$count,$chg,%todisplay);  
  978:                 }
  979:             }
  980:             $r->print(&Apache::loncommon::end_data_table());
  981:         } else {
  982:             if ((@{$processing{'activate'}} > 0) || (@{$processing{'update'}} > 0)) {
  983:                 $errors .= '<span class="LC_error">'.
  984: 		    &mt('A problem occurred storing access control settings: [_1]',$outcome).
  985: 		    '</span>';
  986:             }
  987:         }
  988:         if ($errors) { 
  989:             $r->print($errors);
  990:         }
  991:     }
  992:     my $allnew = 0;
  993:     my $totalnew = 0;
  994:     my $status = 'new';
  995:     my ($firstitem,$lastitem);
  996:     foreach my $newitem ('course','group','domains','users') {
  997:         $allnew += $env{'form.new'.$newitem};
  998:     }
  999:     if ($allnew > 0) {
 1000:         my $now = time;
 1001:         my $then = $now + (60*60*24*180); # six months approx.
 1002:         &open_form($r,$url);
 1003:         foreach my $newitem ('course','group','domains','users') {
 1004:             if ($env{'form.new'.$newitem} > 0) {
 1005:                 $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 />');
 1006:                 $firstitem = $totalnew;
 1007:                 $lastitem = $totalnew + $env{'form.new'.$newitem};
 1008:                 $totalnew = $lastitem;
 1009:                 my @numbers;   
 1010:                 for (my $i=$firstitem; $i<$lastitem; $i++) {
 1011:                     push (@numbers,$i);
 1012:                 }
 1013:                 &display_access_row($r,$status,$newitem,\@numbers,
 1014:                                     $access_controls{$file_name},$now,$then);
 1015:             }
 1016:         }
 1017:         &close_form($r,$url);
 1018:     } else {
 1019:         my %anchor_fields = (
 1020:             'currentpath' => $env{'form.currentpath'},
 1021:             'access' => $env{'form.selectfile'}
 1022:         );
 1023:         $r->print('<br />'.&make_anchor($url, \%anchor_fields, &mt('Display all access settings for this file')));
 1024:         delete $anchor_fields{'access'};
 1025:         $r->print('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&make_anchor($url,\%anchor_fields,&mt('Return to directory listing')));
 1026:     }
 1027:     return;
 1028: }
 1029: 
 1030: sub build_access_record {
 1031:     my ($num,$scope,$start,$end,$chg) = @_;
 1032:     my $record = {
 1033: 	type => $scope,
 1034: 	time => {
 1035: 	    start => $start,
 1036: 	    end   => $end
 1037: 	    },
 1038: 	    };
 1039: 		
 1040:     if ($scope eq 'guest') {	
 1041:         $record->{'password'} = $env{'form.password'};
 1042:     } elsif (($scope eq 'course') || ($scope eq 'group')) {
 1043:         $record->{'domain'} = $env{'form.crsdom_'.$num};
 1044: 	$record->{'number'} = $env{'form.crsnum_'.$num};
 1045:         my @role_ids;
 1046:         my @delete_role_ids =
 1047:             &Apache::loncommon::get_env_multiple('form.delete_role_'.$num);
 1048: 	my @preserves =
 1049: 	    &Apache::loncommon::get_env_multiple('form.preserve_role_'.$num);
 1050: 	if (@delete_role_ids) {
 1051: 	    foreach my $id (@preserves) {
 1052: 		if (grep {$_ = $id} (@delete_role_ids)) {
 1053: 		    next;
 1054: 		}
 1055: 		push(@role_ids,$id); 
 1056: 	    }
 1057: 	} else {
 1058: 	    push(@role_ids,@preserves);
 1059: 	}
 1060: 
 1061: 	my $next_id = $env{'form.add_role_'.$num};
 1062: 	if ($next_id) {
 1063: 	    push(@role_ids,$next_id);
 1064: 	}
 1065: 
 1066:         foreach my $id (@role_ids) {
 1067:             my (@roles,@accesses,@sections,@groups);
 1068:             if (($id == $next_id) && ($chg eq 'update')) {
 1069:                 @roles    = split(/,/,$env{'form.role_'.$num.'_'.$next_id});
 1070:                 @accesses = split(/,/,$env{'form.access_'.$num.'_'.$next_id});
 1071:                 @sections = split(/,/,$env{'form.section_'.$num.'_'.$next_id});
 1072:                 @groups   = split(/,/,$env{'form.group_'.$num.'_'.$next_id});
 1073:             } else {
 1074:                 @roles = &Apache::loncommon::get_env_multiple('form.role_'.$num.'_'.$id);
 1075:                 @accesses = &Apache::loncommon::get_env_multiple('form.access_'.$num.'_'.$id);
 1076:                 @sections = &Apache::loncommon::get_env_multiple('form.section_'.$num.'_'.$id);
 1077:                 @groups = &Apache::loncommon::get_env_multiple('form.group_'.$num.'_'.$id);
 1078:             }
 1079: 	    $record->{'roles'}{$id}{'role'}    = \@roles;
 1080: 	    $record->{'roles'}{$id}{'access'}  = \@accesses;
 1081: 	    $record->{'roles'}{$id}{'section'} = \@sections;
 1082: 	    $record->{'roles'}{$id}{'group'}   = \@groups;
 1083:         }
 1084:     } elsif ($scope eq 'domains') {
 1085:         my @doms = &Apache::loncommon::get_env_multiple('form.dom_'.$num);
 1086: 	$record->{'dom'} = \@doms;
 1087:     } elsif ($scope eq 'users') {
 1088:         my $userlist = $env{'form.users_'.$num};
 1089:         $userlist =~ s/\s+//sg;
 1090: 	my %userhash = map { ($_,1) } (split(/,/,$userlist));
 1091:         foreach my $user (keys(%userhash)) {
 1092:             my ($uname,$udom) = split(/:/,$user);
 1093: 	    push(@{$record->{'users'}}, {
 1094: 		'uname' => $uname,
 1095: 		'udom'  => $udom
 1096: 		});
 1097: 	}
 1098:     }
 1099:     return $record;
 1100: }
 1101: 
 1102: sub get_dates_from_form {
 1103:     my ($id) = @_;
 1104:     my $startdate;
 1105:     my $enddate;
 1106:     $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$id);
 1107:     $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$id);
 1108:     if ( exists ($env{'form.noend_'.$id}) ) {
 1109:         $enddate = 0;
 1110:     }
 1111:     return ($startdate,$enddate);
 1112: }
 1113: 
 1114: sub sort_users {
 1115:     my ($users) = @_; 
 1116:     my @curr_users = map {
 1117: 	$_->{'uname'}.':'.$_->{'udom'}
 1118:     } (@{$users});
 1119:     my $curr_user_list = join(",\n",sort(@curr_users));
 1120:     return $curr_user_list;
 1121: }
 1122: 
 1123: sub access_setting_table {
 1124:     my ($r,$access_controls) = @_;
 1125:     my ($public,$publictext);
 1126:     $publictext = &mt('Off');
 1127:     my ($guest,$guesttext);
 1128:     $guesttext = &mt('Off');
 1129:     my @courses = ();
 1130:     my @groups = ();
 1131:     my @domains = ();
 1132:     my @users = ();
 1133:     my $now = time;
 1134:     my $then = $now + (60*60*24*180); # six months approx.
 1135:     my ($num,$scope,$publicnum,$guestnum);
 1136:     my (%acl_count,%end,%start);
 1137:     foreach my $key (sort(keys(%{$access_controls}))) {
 1138:         ($num,$scope,$end{$key},$start{$key}) = &unpack_acc_key($key);
 1139:         if ($scope eq 'public') {
 1140:             $public = $key;
 1141:             $publicnum = $num;
 1142:             $publictext = &acl_status($start{$key},$end{$key},$now);
 1143:         } elsif ($scope eq 'guest') {
 1144:             $guest=$key;
 1145:             $guestnum = $num;  
 1146:             $guesttext = &acl_status($start{$key},$end{$key},$now);
 1147:         } elsif ($scope eq 'course') {
 1148:             push(@courses,$key);
 1149:         } elsif ($scope eq 'group') {
 1150:             push(@groups,$key);
 1151:         } elsif ($scope eq 'domains') {
 1152:             push(@domains,$key);
 1153:         } elsif ($scope eq 'users') {
 1154:             push(@users,$key);
 1155:         }
 1156:         $acl_count{$scope} ++;
 1157:     }
 1158:     $r->print('<table border="0"><tr><td valign="top">');
 1159:     $r->print('<h3>'.&mt('Public access:').' '.$publictext.'</h3>');
 1160:     $r->print(&Apache::loncommon::start_data_table());
 1161:     $r->print(&Apache::loncommon::start_data_table_header_row());
 1162:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').'</th>');
 1163:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1164:     $r->print(&Apache::loncommon::start_data_table_row());
 1165:     if ($public) {
 1166:         $r->print('<td>'.&actionbox('old',$publicnum,'public').'</td><td>'.
 1167:                   &dateboxes($publicnum,$start{$public},$end{$public}).'</td>');
 1168:     } else {
 1169:         $r->print('<td>'.&actionbox('new','0','public').'</td><td>'.
 1170:                   &dateboxes('0',$now,$then).'</td>');
 1171:     }
 1172:     $r->print(&Apache::loncommon::end_data_table_row());
 1173:     $r->print(&Apache::loncommon::end_data_table());
 1174:     $r->print('</td><td width="40">&nbsp;</td><td valign="top">');
 1175:     $r->print('<h3>'.&mt('Passphrase-protected access:').' '.$guesttext.'</h3>');
 1176:     $r->print(&Apache::loncommon::start_data_table());
 1177:     $r->print(&Apache::loncommon::start_data_table_header_row());
 1178:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').
 1179:               '</th><th>'. &mt('Passphrase').'</th>');
 1180:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1181:     $r->print(&Apache::loncommon::start_data_table_row());
 1182:     my $passwd;
 1183:     if ($guest) {
 1184:         $passwd = $$access_controls{$guest}{'password'};
 1185:         $r->print('<td>'.&actionbox('old',$guestnum,'guest').'</td><td>'.
 1186:                   &dateboxes($guestnum,$start{$guest},$end{$guest}).'</td>');
 1187:     } else {
 1188:         $r->print('<td>'.&actionbox('new','1','guest').'</td><td>'.
 1189:                   &dateboxes('1',$now,$then).'</td>');
 1190:     }
 1191:     $r->print('<td><input type="text" size="15" name="password" value="'.
 1192:               $passwd.'" /></td>');
 1193:     $r->print(&Apache::loncommon::end_data_table_row());
 1194:     $r->print(&Apache::loncommon::end_data_table());
 1195:     $r->print('</td></tr><tr><td colspan="3">&nbsp;</td></tr><tr><td valign="top">');
 1196:     &access_element($r,'domains',\%acl_count,\@domains,$access_controls,$now,$then);
 1197:     $r->print('</td><td>&nbsp;</td><td valign="top">');
 1198:     &access_element($r,'users',\%acl_count,\@users,$access_controls,$now,$then);
 1199:     $r->print('</td></tr><tr><td colspan="3"></td></tr><tr>');
 1200:     if (@courses > 0 || @groups > 0) {
 1201:         $r->print('<td colspan="3" valign="top">');
 1202:     } else {
 1203:         $r->print('<td valign="top">');
 1204:     }
 1205:     &access_element($r,'course',\%acl_count,\@courses,$access_controls,$now,$then);
 1206:     $r->print('</td>');
 1207:     if (@courses > 0 || @groups > 0) {
 1208:         $r->print('</tr><tr><td colspan="3">&nbsp;</td></tr><tr><td colspan="3" valign="top">');
 1209:     } else {
 1210:         $r->print('<td>&nbsp;</td><td valign="top">');
 1211:     }
 1212:     &access_element($r,'group',\%acl_count,\@groups,$access_controls,$now,$then);
 1213:     $r->print('</td></tr></table>');
 1214: }
 1215: 
 1216: sub acl_status {
 1217:     my ($start,$end,$now) = @_;
 1218:     if ($start > $now) {
 1219:         return &mt('Inactive');
 1220:     }
 1221:     if ($end && $end<$now) {
 1222:         return &mt('Inactive');
 1223:     }
 1224:     return &mt('Active');
 1225: }
 1226: 
 1227: sub access_element {
 1228:     my ($r,$type,$acl_count,$items,$access_controls,$now,$then) = @_;
 1229:     my $title = $type;
 1230:     $title =~ s/s$//;
 1231:     $title =~ s/^(\w)/uc($1)/e;
 1232:     $r->print('<h3>'.&mt('[_1]-based conditional access: ',$title));
 1233:     if ($$acl_count{$type}) {
 1234:         $r->print($$acl_count{$type}.' ');
 1235:         if ($$acl_count{$type} > 1) {
 1236:             $r->print(&mt('conditions'));
 1237:         } else {
 1238:             $r->print(&mt('condition'));
 1239:         }
 1240:     } else {
 1241:         $r->print(&mt('Off'));
 1242:     }
 1243:     $r->print('</h3>');
 1244:     &display_access_row($r,'old',$type,$items,$access_controls,$now,$then);
 1245:     return;
 1246: }
 1247: 
 1248: sub display_access_row {
 1249:     my ($r,$status,$type,$items,$access_controls,$now,$then) = @_;
 1250:     if (@{$items} > 0) {
 1251:         my @all_doms;
 1252:         my $colspan = 3;
 1253:         my $uctype = $type;
 1254:         $uctype =~ s/^(\w)/uc($1)/e;
 1255:         $r->print(&Apache::loncommon::start_data_table());
 1256:         $r->print(&Apache::loncommon::start_data_table_header_row());
 1257:         $r->print('<th>'.&mt('Action?').'</th><th>'.&mt($uctype).'</th><th>'.
 1258:               &mt('Dates available').'</th>');
 1259:         if (($type eq 'course') || ($type eq 'group')) {
 1260:             $r->print('<th>'.&mt('Allowed [_1] member affiliations',$type).
 1261:                       '</th>');
 1262:             $colspan ++;
 1263:         } elsif ($type eq 'domains') {
 1264:             @all_doms = &Apache::loncommon::get_domains();
 1265:         }
 1266:         $r->print(&Apache::loncommon::end_data_table_header_row());
 1267:         foreach my $key (@{$items}) {
 1268: 	    $r->print(&Apache::loncommon::start_data_table_row());
 1269:             if (($type eq 'course') || ($type eq 'group')) {
 1270:                 &course_row($r,$status,$type,$key,$access_controls,$now,$then);
 1271:             } elsif ($type eq 'domains') {
 1272:                 &domains_row($r,$status,$key,\@all_doms,$access_controls,$now,
 1273:                             $then);
 1274:             } elsif ($type eq 'users') {
 1275:                 &users_row($r,$status,$key,$access_controls,$now,$then);
 1276:             }
 1277: 	    $r->print(&Apache::loncommon::end_data_table_row());
 1278:         }
 1279:         if ($status eq 'old') {
 1280: 	    $r->print(&Apache::loncommon::start_data_table_row());
 1281:             $r->print('<td colspan="',$colspan.'">'.&additional_item($type).
 1282:                       '</td>');
 1283: 	    $r->print(&Apache::loncommon::end_data_table_row());
 1284:         }
 1285:         $r->print(&Apache::loncommon::end_data_table());
 1286:     } else {
 1287:         $r->print(&mt('No [_1]-based conditions defined.<br />',$type).
 1288:                   &additional_item($type));
 1289:     }
 1290:     return;
 1291: }
 1292: 
 1293: sub course_js {
 1294:     return qq|
 1295: <script type="text/javascript">
 1296: function setRoleOptions(caller,num,cdom,cnum,type) {
 1297:     addIndexnum = getCallerIndex(caller);
 1298:     updateIndexnum = getIndex('update',num);
 1299:     if (caller.checked) {
 1300:         document.portform.elements[updateIndexnum].checked = true;
 1301:         var url = '/adm/portfolio?action=rolepicker&setroles='+addIndexnum+'&cnum='+cnum+'&cdom='+cdom+'&type='+type;
 1302:         var title = 'Roles_Chooser';
 1303:         var options = 'scrollbars=1,resizable=1,menubar=0';
 1304:         options += ',width=700,height=600';
 1305:         rolebrowser = open(url,title,options,'1');
 1306:         rolebrowser.focus();
 1307:     } else {
 1308:         for (var j=0;j<5;j++) {
 1309:             document.portform.elements[addIndexnum+j].value = '';
 1310:         }
 1311:     }
 1312: }
 1313: 
 1314: function getCallerIndex(caller) {
 1315:     for (var i=0;i<document.portform.elements.length;i++) {
 1316:         if (document.portform.elements[i] == caller) {
 1317:             return i;
 1318:         }
 1319:     }
 1320:     return -1;
 1321: }
 1322: 
 1323: function getIndex(name,value) {
 1324:     for (var i=0;i<document.portform.elements.length;i++) {
 1325:         if (document.portform.elements[i].name == name && document.portform.elements[i].value == value) {
 1326:             return i;
 1327:         }
 1328:     }
 1329:     return -1;
 1330: }
 1331: 
 1332: </script>
 1333: |;
 1334: }
 1335: 
 1336: sub course_row {
 1337:     my ($r,$status,$type,$item,$access_controls,$now,$then) = @_;
 1338:     my $content;
 1339:     my $defdom = $env{'user.domain'};
 1340:     if ($status eq 'old') {
 1341:         $content = $$access_controls{$item}; 
 1342:         $defdom =  $content->{'domain'};
 1343:     }
 1344:     my $js = &Apache::loncommon::coursebrowser_javascript($defdom)
 1345: 	.&course_js();
 1346:     my $crsgrptext = 'Groups';
 1347:     if ($type eq 'group') {
 1348:         $crsgrptext = 'Teams';
 1349:     }
 1350:     my $uctype = $type;
 1351:     $uctype =~ s/^(\w)/uc($1)/e;
 1352:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1353:                                                     $type);
 1354:     $r->print('<td>'.$js.&actionbox($status,$num,$scope).'</td>');
 1355:     if ($status eq 'old') {
 1356:         my $cid = $content->{'domain'}.'_'.$content->{'number'};
 1357:         my %course_description = &Apache::lonnet::coursedescription($cid);
 1358:         $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>');
 1359:     } elsif ($status eq 'new') {
 1360:         $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>');
 1361:     }
 1362:     $r->print('<td>'.&dateboxes($num,$start,$end).'</td>');
 1363:     $r->print('<td><table><tr>');
 1364:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Roles').'</th><th>'.
 1365:               &mt('Access').'</th><th>'.&mt('Sections').'</th><th>'.
 1366:               &mt($crsgrptext).'</th></tr>');
 1367:     if ($status eq 'old') {
 1368:         my $max_id = 0;
 1369:         foreach my $role_id (sort(keys(%{$content->{'roles'}}))) {
 1370:             if ($role_id > $max_id) {
 1371:                 $max_id = $role_id;
 1372:             }
 1373:             $max_id ++;
 1374:             my $role_selects = &role_selectors($num,$role_id,$status,$type,$content,'display');
 1375:             $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>');
 1376:         }
 1377:         $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>');
 1378:     } elsif ($status eq 'new') {
 1379:         my $role_id = 1;
 1380:         my $role_selects = &role_selectors($num,$role_id,$status,$type,undef,'display');
 1381:         $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);
 1382:         $r->print('</tr></table></td>');
 1383:     }
 1384:     return;
 1385: }
 1386: 
 1387: sub domains_row {
 1388:     my ($r,$status,$item,$all_doms,$access_controls,$now,$then) = @_;
 1389:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1390:                                                     'domains');
 1391:     my $dom_select = '<select name="dom_'.$num.'" size="4" multiple="true">'.
 1392:                      ' <option value="">'.&mt('Please select').'</option>';
 1393:     if ($status eq 'old') {
 1394:         my $content =  $$access_controls{$item};
 1395: 	foreach my $dom (@{$all_doms}) {
 1396:             if ((@{$content->{'dom'}} > 0) 
 1397: 		&& (grep(/^\Q$dom\E$/,@{$content->{'dom'}}))) {
 1398:                 $dom_select .= '<option value="'.$dom.'" selected>'.
 1399:                                $dom.'</option>';
 1400:             } else {
 1401:                 $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
 1402:             }
 1403:         }
 1404:     } else {
 1405:         foreach my $dom (@{$all_doms}) {
 1406:             $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
 1407:         }
 1408:     }
 1409:     $dom_select .= '</select>';
 1410:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.$dom_select.
 1411:               '</td><td>'.&dateboxes($num,$start,$end).'</td>');
 1412: }
 1413: 
 1414: sub users_row {
 1415:     my ($r,$status,$item,$access_controls,$now,$then) = @_;
 1416:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1417:                                                     'users');
 1418:     my $curr_user_list;
 1419:     if ($status eq 'old') {
 1420:         my $content = $$access_controls{$item};
 1421:         $curr_user_list = &sort_users($content->{'users'});
 1422:     }
 1423:     $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>');
 1424: }
 1425: 
 1426: sub additional_item {
 1427:     my ($type) = @_;
 1428:     my $output = &mt('Add new [_1] condition(s)?',$type).'&nbsp;'.&mt('Number to add: ').'<input type="text" name="new'.$type.'" size="3" value="0" />';
 1429:     return $output;
 1430: }
 1431: 
 1432: sub actionbox {
 1433:     my ($status,$num,$scope) = @_;
 1434:     my $output = '<span style="white-space: nowrap"><label>';
 1435:     if ($status eq 'new') {
 1436:         $output .= '<input type="checkbox" name="activate" value="'.$num.'" />'.
 1437:         &mt('Activate');
 1438:     } else {
 1439:         $output .= '<input type="checkbox" name="delete" value="'.$num.
 1440:                    '" />'.&mt('Delete').'</label></span><br /><span style="white-space: nowrap">'.
 1441:                    '<label><input type="checkbox" name="update" value="'.
 1442:                    $num.'" />'.&mt('Update');
 1443:     }
 1444:     $output .= '</label></span><input type="hidden" name="scope_'.$num.                '" value="'.$scope.'" />';
 1445:     return $output;
 1446: }
 1447:                                                                                    
 1448: sub dateboxes {
 1449:     my ($num,$start,$end) = @_;
 1450:     my $noend;
 1451:     if ($end == 0) {
 1452:         $noend = 'checked="checked"';
 1453:     }
 1454:     my $startdate = &Apache::lonhtmlcommon::date_setter('portform',
 1455:                            'startdate_'.$num,$start,undef,undef,undef,1,undef,
 1456:                             undef,undef,1);
 1457:     my $enddate = &Apache::lonhtmlcommon::date_setter('portform',
 1458:                                'enddate_'.$num,$end,undef,undef,undef,1,undef,
 1459:                                 undef,undef,1). '&nbsp;&nbsp;<span style="white-space: nowrap"><label>'.
 1460:                                 '<input type="checkbox" name="noend_'.
 1461:                                 $num.'" '.$noend.' />'.&mt('No end date').
 1462:                                 '</label></span>';
 1463:                                                                                    
 1464:     my $output = &mt('Start: ').$startdate.'<br />'.&mt('End: ').$enddate;
 1465:     return $output;
 1466: }
 1467: 
 1468: sub unpack_acc_key {
 1469:     my ($acc_key) = @_;
 1470:     my ($num,$scope,$end,$start) = ($acc_key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 1471:     return ($num,$scope,$end,$start);
 1472: }
 1473: 
 1474: sub set_identifiers {
 1475:     my ($status,$item,$now,$then,$scope) = @_;
 1476:     if ($status eq 'old') {
 1477:         return(&unpack_acc_key($item));
 1478:     } else {
 1479:         return($item,$scope,$then,$now);
 1480:     }
 1481: } 
 1482: 
 1483: sub role_selectors {
 1484:     my ($num,$role_id,$status,$type,$content,$caller) = @_;
 1485:     my ($output,$cdom,$cnum,$longid);
 1486:     if ($caller eq 'display') {
 1487:         $longid = '_'.$num.'_'.$role_id;
 1488:         if ($status eq 'new') {
 1489:             foreach my $item ('role','access','section','group') {
 1490:                 $output .= '<td><select name="'.$item.$longid.'">'.
 1491:                            '<option value="">'.&mt('Pick [_1] first',$type).
 1492:                            '</option></select></td>';
 1493:             }
 1494:             return $output;
 1495:         } else {
 1496:             $cdom = $$content{'domain'};
 1497:             $cnum = $$content{'number'};
 1498:         }
 1499:     } elsif ($caller eq 'rolepicker') {
 1500:          $cdom = $env{'form.cdom'};
 1501:          $cnum = $env{'form.cnum'};
 1502:     }
 1503:     my $uctype = $type;
 1504:     $uctype =~ s/^(\w)/uc($1)/e;
 1505:     my ($sections,$groups,$allroles,$rolehash,$accesshash) =
 1506:             &Apache::loncommon::get_secgrprole_info($cdom,$cnum,1,$uctype);
 1507:     if (!@{$sections}) {
 1508:         @{$sections} = ('none');
 1509:     } else {
 1510:         unshift(@{$sections},('all','none'));
 1511:     }
 1512:     if (!@{$groups}) {
 1513:         @{$groups} = ('none');
 1514:     } else {
 1515:         unshift(@{$groups},('all','none'));
 1516:     }
 1517:     my @allacesses = sort(keys(%{$accesshash}));
 1518:     my (%sectionhash,%grouphash);
 1519:     foreach my $sec (@{$sections}) {
 1520:         $sectionhash{$sec} = $sec;
 1521:     }
 1522:     foreach my $grp (@{$groups}) {
 1523:         $grouphash{$grp} = $grp;
 1524:     }
 1525:     my %lookup = (
 1526:                    'role' => $rolehash,
 1527:                    'access' => $accesshash,
 1528:                    'section' => \%sectionhash,
 1529:                    'group' => \%grouphash,
 1530:                  );
 1531:     my @allaccesses = sort(keys(%{$accesshash}));
 1532:     my %allitems = (
 1533:                     'role' => $allroles,
 1534:                     'access' => \@allaccesses,
 1535:                     'section' => $sections,
 1536:                     'group' => $groups, 
 1537:                    );
 1538:     foreach my $item ('role','access','section','group') {
 1539:         $output .= '<td><select name="'.$item.$longid.'" multiple="true" size="4">'."\n";
 1540:         foreach my $entry (@{$allitems{$item}}) {
 1541:             if ($caller eq 'display') {
 1542:                 if ((@{$$content{'roles'}{$role_id}{$item}} > 0) && 
 1543:                     (grep(/^\Q$entry\E$/,@{$$content{'roles'}{$role_id}{$item}}))) {
 1544:                     $output .= '  <option value="'.$entry.'" selected>'.
 1545:                                   $lookup{$item}{$entry}.'</option>';
 1546:                     next;
 1547:                 }
 1548:             }
 1549:             $output .= '  <option value="'.$entry.'">'.
 1550:                        $lookup{$item}{$entry}.'</option>';
 1551:         }
 1552:         $output .= '</select>';
 1553:     }
 1554:     $output .= '</td>';
 1555:     return $output;
 1556: }
 1557: 
 1558: sub role_options_window {
 1559:     my ($r) = @_;
 1560:     my $cdom = $env{'form.cdom'};
 1561:     my $cnum = $env{'form.cnum'};
 1562:     my $type = $env{'form.type'};
 1563:     my $addindex = $env{'form.setroles'};
 1564:     my $grouptitle = 'Groups';
 1565:     if ($type eq 'Group') {
 1566:          $grouptitle = 'Teams';
 1567:     } 
 1568:     my $role_selects = &role_selectors(1,1,'new',$type,undef,'rolepicker');
 1569:     $r->print(<<"END_SCRIPT");
 1570: <script type="text/javascript">
 1571: function setRoles() {
 1572:     var addidx = $addindex+1;
 1573:     for (var i=0; i<4; i++) {
 1574:         var copylist = '';
 1575:         for (var j=0; j<document.rolepicker.elements[i].length; j++) {
 1576:             if (document.rolepicker.elements[i].options[j].selected) {
 1577:                 copylist = copylist + document.rolepicker.elements[i].options[j].value + ',';
 1578:             }
 1579:         }
 1580:         copylist = copylist.substr(0,copylist.length-1);
 1581:         opener.document.portform.elements[addidx+i].value = copylist;
 1582:     }
 1583:     self.close();
 1584: }
 1585: </script>
 1586: END_SCRIPT
 1587:     $r->print(&mt('Select roles, course status, section(s) and group(s) for users who will be able to access the portfolio file.'));
 1588:     $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()" />');
 1589:     return;
 1590: }
 1591: 
 1592: sub select_files {
 1593:     my ($r) = @_;
 1594:     if ($env{'form.continue'} eq 'true') {
 1595:         # here we update the selections for the currentpath
 1596:         # eventually, have to handle removing those not checked, but . . . 
 1597:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
 1598:         if (scalar(@items)){
 1599:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
 1600:         }
 1601:     } else {
 1602:             #empty the file for a fresh start
 1603:             &Apache::lonnet::clear_selected_files($env{'user.name'});
 1604:     }
 1605:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
 1606:     my $java_files = join ",", @files;
 1607:     if ($java_files) {
 1608:         $java_files.=',';
 1609:     }
 1610:     my $javascript =(<<ENDSMP);
 1611:         <script type="text/javascript">
 1612:         function finishSelect() {
 1613: ENDSMP
 1614:     $javascript .= 'fileList = "'.$java_files.'";';
 1615:     $javascript .= (<<ENDSMP);
 1616:             for (i=0;i<document.forms.checkselect.length;i++) { 
 1617:                 if (document.forms.checkselect[i].checked){
 1618:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
 1619:                 }
 1620:             }
 1621:             opener.document.forms.lonhomework.$env{'form.fieldname'}.value=fileList;
 1622:             self.close();
 1623:         }
 1624:         </script>
 1625: ENDSMP
 1626:     $r->print($javascript);
 1627:     $r->print("<h1>Select portfolio files</h1>
 1628:                 Check as many as you wish in response to the problem.<br />");
 1629:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
 1630:     if (@otherfiles) {
 1631: 	$r->print("<strong>Files selected from other directories:</strong><br />");
 1632: 	foreach my $file (@otherfiles) {
 1633: 	    $r->print($file."<br />");
 1634: 	}
 1635:     }
 1636: }
 1637: 
 1638: sub upload {
 1639:     my ($r,$url,$group)=@_;
 1640:     my $fname=$env{'form.uploaddoc.filename'};
 1641:     my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
 1642:     my $disk_quota = 20000; # expressed in k
 1643:     if (defined($group)) {
 1644:         my $grp_quota = &get_group_quota($group); # quota expressed in k 
 1645:         if ($grp_quota ne '') {
 1646:             $disk_quota = $grp_quota;
 1647:         } else {
 1648:             $disk_quota = 0;
 1649:         }
 1650:     }
 1651:     $fname=&Apache::lonnet::clean_filename($fname);
 1652: 
 1653:     my $portfolio_root=&get_portfolio_root();
 1654:     my ($uname,$udom) = &get_name_dom();
 1655:     my $port_path = &get_port_path();
 1656:     # Fixme --- Move the checking for existing file to LOND error return
 1657:     my @dir_list=&get_dir_list($portfolio_root);
 1658:     my $found_file = 0;
 1659:     my $locked_file = 0;
 1660:     foreach my $line (@dir_list) {
 1661:         my ($file_name)=split(/\&/,$line,2);
 1662:         if ($file_name eq $fname){
 1663:             $file_name = $env{'form.currentpath'}.$file_name;
 1664:             $file_name = &prepend_group($file_name);
 1665:             $found_file = 1;
 1666:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
 1667:                 $locked_file = 1;
 1668:             } 
 1669:         }
 1670:     }
 1671:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root);
 1672:     if (($current_disk_usage + $filesize) > $disk_quota){
 1673:         $r->print('<span class="LC_error">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes)</strong>. Disk quota will be exceeded.</span>'.
 1674:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
 1675:         $r->print(&done('Back',$url));
 1676:     } 
 1677:     elsif ($found_file){
 1678:         if ($locked_file){
 1679:             $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>'.
 1680:                   '<br />You will be able to rename or delete existing '.$fname.' after a grade has been assigned.');
 1681:             $r->print(&done('Back',$url));      
 1682:         } else {   
 1683:             $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>'.
 1684:                   '<br />To upload, rename or delete existing '.$fname.' in '.$port_path.$env{'form.currentpath'});
 1685:             $r->print(&done('Back',$url));
 1686:         }
 1687:     } else {
 1688:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
 1689: 	        	 $port_path.$env{'form.currentpath'});
 1690:         if ($result !~ m|^/uploaded/|) {
 1691:             $r->print('<span class="LC_error">'.'An errror occured ('.$result.
 1692: 	              ') while trying to upload '.&display_file().'</span><br />');
 1693: 	    $r->print(&done('Back',$url));
 1694:         } else {
 1695:             $r->print(&done(undef,$url));
 1696:         }
 1697:     }
 1698: }
 1699: 
 1700: sub lock_info {
 1701:     my ($r,$url,$group) = @_;
 1702:     my ($uname,$udom) = &get_name_dom();
 1703:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
 1704:                                                                        $uname);
 1705:     my $file_name = $env{'form.lockinfo'};
 1706:     $file_name = &prepend_group($file_name);
 1707:     if (defined($file_name) && defined($$current_permissions{$file_name})) {
 1708:         foreach my $array_item (@{$$current_permissions{$file_name}}) {
 1709:             next if (ref($array_item) ne 'ARRAY');
 1710: 
 1711: 	    my $filetext;
 1712: 	    if (defined($group)) {
 1713: 		$filetext = '<strong>'.$env{'form.lockinfo'}.
 1714: 		    '</strong> (group: '.$group.')'; 
 1715: 	    } else {
 1716: 		$filetext = '<strong>'.$file_name.'</strong>';
 1717: 	    } 
 1718: 	    
 1719: 	    my $title ='<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
 1720: 		'</strong><br />';
 1721: 	    if ($$array_item[-1] eq 'graded') {
 1722: 		$r->print(&mt('[_1] was submitted in response to problem: [_2]',
 1723:                               $filetext,$title));
 1724: 	    } elsif ($$array_item[-1] eq 'handback') {
 1725: 		$r->print(&mt('[_1] was handed back in response to problem: [_2]',
 1726:                               $filetext,$title));
 1727: 	    } else {
 1728: 		# submission style lock
 1729: 		$r->print(&mt('[_1] was submitted in response to problem: [_2]',
 1730:                               $filetext,$title));
 1731: 	    }
 1732: 	    my %course_description = 
 1733: 		&Apache::lonnet::coursedescription($$array_item[1]);
 1734: 	    if ( $course_description{'description'} ne '') {
 1735: 		$r->print(&mt('In the course: <strong>[_1]</strong><br />',
 1736: 			      $course_description{'description'}));
 1737: 	    }
 1738:         }
 1739:     }
 1740:     $r->print(&done('Back',$url));
 1741:     return 'ok';
 1742: }
 1743: 
 1744: sub createdir {
 1745:     my ($r,$url)=@_;
 1746:     my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
 1747:     if ($newdir eq '') {
 1748:     	$r->print('<span class="LC_error">'.
 1749: 	    	  &mt("Error: no directory name was provided.").
 1750: 		      '</span><br />');
 1751: 	    $r->print(&done(undef,$url));
 1752: 	    return;
 1753:     }
 1754:     my $portfolio_root = &get_portfolio_root(); 
 1755:     my @dir_list=&get_dir_list($portfolio_root);
 1756:     my $found_file = 0;
 1757:     foreach my $line (@dir_list) {
 1758:         my ($filename)=split(/\&/,$line,2);
 1759:         if ($filename eq $newdir){
 1760:             $found_file = 1;
 1761:         }
 1762:     }
 1763:     if ($found_file){
 1764:     	    $r->print('<span class="LC_error">'.'Unable to create a directory named <strong>'.$newdir.
 1765:     	            ' </strong>a file or directory by that name already exists.</span><br />');
 1766:     } else {
 1767:         my ($uname,$udom) = &get_name_dom();
 1768:         my $port_path = &get_port_path();
 1769:         my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
 1770: 	         $port_path.$env{'form.currentpath'}.$newdir);
 1771:         if ($result ne 'ok') {
 1772:     	    $r->print('<span class="LC_error">'.'An errror occured ('.$result.
 1773: 	    	      ') while trying to create a new directory '.&display_file().'</span><br />');
 1774:         }
 1775:     }
 1776:     if ($newdir ne $env{'form.newdir'}) {
 1777:         $r->print("The new directory name was changed from:<br /><strong>".$env{'form.newdir'}."</strong> to <strong>$newdir </strong>");  
 1778:     }
 1779:     $r->print(&done(undef,$url));
 1780: }
 1781: 
 1782: sub get_portfolio_root {
 1783:     my ($udom,$uname,$group) = @_;
 1784:     if (!(defined($udom)) || !(defined($uname))) {
 1785:         ($uname,$udom) = &get_name_dom();
 1786:     }
 1787:     my $path = '/userfiles/portfolio';
 1788:     if (!defined($group)) { 
 1789:         if (defined($env{'form.group'})) {
 1790:             $group = $env{'form.group'};      
 1791:         }
 1792:     }
 1793:     if (defined($group)) {
 1794:         $path = '/userfiles/groups/'.$group.'/portfolio';
 1795:     } 
 1796:     return (&Apache::loncommon::propath($udom,$uname).$path);
 1797: }
 1798: 
 1799: sub get_group_quota {
 1800:     my ($group) = @_;
 1801:     my $group_quota; 
 1802:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1803:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1804:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
 1805:     if (%curr_groups) {
 1806:         my %group_info =  &Apache::longroup::get_group_settings(
 1807:                                                     $curr_groups{$group});
 1808:         $group_quota = $group_info{'quota'}; #expressed in Mb
 1809:         if ($group_quota) {
 1810:             $group_quota = 1000 * $group_quota; #expressed in k
 1811:         } 
 1812:     }
 1813:     return $group_quota;
 1814: } 
 1815: 
 1816: sub get_dir_list {
 1817:     my ($portfolio_root) = @_;
 1818:     my ($uname,$udom) = &get_name_dom();
 1819:     return &Apache::lonnet::dirlist($env{'form.currentpath'},
 1820:                                           $udom,$uname,$portfolio_root);
 1821: }
 1822: 
 1823: sub get_name_dom {
 1824:     my ($uname,$udom);
 1825:     if (defined($env{'form.group'})) {
 1826:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1827:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
 1828:     } else {
 1829:         $udom = $env{'user.domain'};
 1830:         $uname = $env{'user.name'};
 1831:     }
 1832:     return ($uname,$udom);
 1833: }
 1834: 
 1835: sub prepend_group {
 1836:     my ($filename) = @_;
 1837:     if (defined($env{'form.group'})) {
 1838:         $filename = $env{'form.group'}.$filename;
 1839:     }
 1840:     return $filename;
 1841: }
 1842: 
 1843: sub get_namespace {
 1844:     my $namespace = 'portfolio';
 1845:     if (defined($env{'form.group'})) {
 1846:         my ($uname,$udom) = &get_name_dom();
 1847:         $namespace .= '_'.$udom.'_'.$uname.'_'.$env{'form.group'};
 1848:     }
 1849:     return $namespace;
 1850: }
 1851: 
 1852: sub get_port_path {
 1853:     my $port_path;
 1854:     if (defined($env{'form.group'})) {
 1855:        $port_path = "groups/$env{'form.group'}/portfolio";
 1856:     } else {
 1857:        $port_path = 'portfolio';
 1858:     }
 1859:     return $port_path;
 1860: }
 1861: 
 1862: sub missing_priv {
 1863:     my ($r,$url,$priv) = @_;
 1864:     my $longtext = {
 1865:                       upload => 'upload files',
 1866:                       delete => 'delete files',
 1867:                       rename => 'rename files',
 1868:                       setacl => 'set access controls for files',
 1869:                    };
 1870:     my $escpath = &HTML::Entities::encode($env{'form.currentpath'},'&<>"');
 1871:     my $rtnlink = '<a href="'.$url;
 1872:     if ($url =~ /\?/) {
 1873:         $rtnlink .= '&';
 1874:     } else {
 1875:         $rtnlink .= '?';
 1876:     }
 1877:     $rtnlink .= 'currentpath='.$escpath;
 1878:     $r->print(&mt('<h3>Action disallowed</h3>'));
 1879:     $r->print(&mt('You do not have sufficient privileges to [_1] ',
 1880:                   $longtext->{$priv}));
 1881:     if (defined($env{'form.group'})) {
 1882:         $r->print(&mt("in the group's file repository."));
 1883:         $rtnlink .= &group_args()
 1884:     } else {
 1885:         $r->print(&mt('in this portfolio.'));
 1886:     }
 1887:     $rtnlink .= '">'.&mt('Return to directory listing page').'</a>';
 1888:     $r->print('<br />'.$rtnlink);
 1889:     $r->print(&Apache::loncommon::end_page());
 1890:     return;
 1891: }
 1892: 
 1893: sub coursegrp_portfolio_header {
 1894:     my ($cdom,$cnum,$grp_desc)=@_;
 1895:     my $gpterm  = &Apache::loncommon::group_term();
 1896:     my $ucgpterm = $gpterm;
 1897:     $ucgpterm =~ s/^(\w)/uc($1)/e;
 1898:     if ($env{'form.ref'}) {
 1899:         &Apache::lonhtmlcommon::add_breadcrumb
 1900:             ({href=>"/adm/coursegroups",
 1901:               text=>"Groups",
 1902:               title=>"Course Groups"});
 1903:     }
 1904:     &Apache::lonhtmlcommon::add_breadcrumb
 1905:         ({href=>"/adm/$cdom/$cnum/$env{'form.group'}/smppg?ref=$env{'form.ref'}",
 1906:           text=>"$ucgpterm: $grp_desc",
 1907:           title=>"Go to group's home page"},
 1908:          {href=>"/adm/coursegrp_portfolio?".&group_args(),
 1909:           text=>"Group Portfolio",
 1910:           title=>"Display group portfolio"});
 1911:     my $output = &Apache::lonhtmlcommon::breadcrumbs(
 1912:                          &mt('[_1] portfolio files - [_2]',$gpterm,$grp_desc));
 1913:     return $output;
 1914: }
 1915: 
 1916: 
 1917: sub handler {
 1918:     # this handles file management
 1919:     my $r = shift;
 1920:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1921:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
 1922: 	  'fieldname','mode','rename','continue','group','access','setnum',
 1923:           'cnum','cdom','type','setroles','showversions','ref']);
 1924:     my ($uname,$udom,$portfolio_root,$url,$caller,$title,$group,$grp_desc);
 1925:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
 1926:         $url = $1.$2;
 1927:         $caller = $2;
 1928:     }
 1929:     my ($can_modify,$can_delete,$can_upload,$can_setacl);
 1930:     if ($caller eq 'coursegrp_portfolio') {
 1931:     #  Needs to be in a course
 1932:         if (! ($env{'request.course.fn'})) {
 1933:         # Not in a course
 1934:             $env{'user.error.msg'}=
 1935:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
 1936:             return HTTP_NOT_ACCEPTABLE;
 1937:         }
 1938:         my $earlyout = 0;
 1939:         my $view_permission = 
 1940:            &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
 1941:         $env{'form.group'} =~ s/\W//g;
 1942: 	$group = $env{'form.group'};
 1943:         if ($group) {
 1944:             ($uname,$udom) = &get_name_dom();
 1945:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
 1946: 							       $group); 
 1947:             if (%curr_groups) {
 1948:                 my %grp_content = &Apache::longroup::get_group_settings(
 1949:                                                          $curr_groups{$group});
 1950:                 $grp_desc = &unescape($grp_content{'description'});
 1951:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
 1952:                                       $env{'request.course.id'}.'/'.$group))) {
 1953:                     $portfolio_root = &get_portfolio_root();
 1954:                 } else {
 1955:                     $r->print('You do not have the privileges required to access the shared files space for this group.');
 1956:                     $earlyout = 1;
 1957:                 }
 1958:             } else {
 1959:                 $r->print('Not a valid group for this course');
 1960:                 $earlyout = 1;
 1961:             }
 1962:             $title = &mt('Group files').' for '.$group; 
 1963:         } else {
 1964:             $r->print('Invalid group');
 1965:             $earlyout = 1;
 1966:         }
 1967:         if ($earlyout) { return OK; }
 1968:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
 1969:             $can_modify = 1;
 1970:             $can_delete = 1;
 1971:             $can_upload = 1;
 1972:             $can_setacl = 1;
 1973:         } else {
 1974:             if (&Apache::lonnet::allowed('agf',$env{'request.course.id'}.'/'.$group)) {
 1975:                 $can_setacl = 1;
 1976:             }
 1977:             if (&Apache::lonnet::allowed('ugf',$env{'request.course.id'}.'/'.$group)) {
 1978:                 $can_upload = 1;
 1979:             }
 1980:             if (&Apache::lonnet::allowed('mgf',$env{'request.course.id'}.'/'.$group)) {
 1981:                 $can_modify = 1;
 1982:             }
 1983:             if (&Apache::lonnet::allowed('dgf',$env{'request.course.id'}.'/'.$group)) {
 1984:                 $can_delete = 1;
 1985:             }
 1986:         }
 1987:     } else {
 1988:         ($uname,$udom) = &get_name_dom();
 1989:         $portfolio_root = &get_portfolio_root();
 1990:         $title = &mt('Portfolio Manager');
 1991:         $can_modify = 1;
 1992:         $can_delete = 1;
 1993:         $can_upload = 1;
 1994:         $can_setacl = 1;
 1995:     }
 1996: 
 1997:     my $port_path = &get_port_path();
 1998:     &Apache::loncommon::no_cache($r);
 1999:     &Apache::loncommon::content_type($r,'text/html');
 2000:     $r->send_http_header;
 2001:     # Give the LON-CAPA page header
 2002:     if ($env{"form.mode"} eq 'selectfile'){
 2003:         $r->print(&Apache::loncommon::start_page($title,undef,
 2004: 						 {'only_body' => 1}));
 2005:     } elsif ($env{'form.action'} eq 'rolepicker') {
 2006:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
 2007:                                                  {'no_nav_bar'  => 1, }));
 2008:     } else {
 2009:         $r->print(&Apache::loncommon::start_page($title));
 2010:     }
 2011:     $r->rflush();
 2012: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
 2013:    	    $r->print('<span class="LC_error">'.
 2014: 		      'No file was selected to upload.'.
 2015: 		      'To upload a file, click <strong>Browse...</strong>'.
 2016: 		      ', select a file, then click <strong>Upload</strong>.'.
 2017: 		      '</span>');
 2018: 	}
 2019:     if ($env{'form.meta'}) {
 2020:         &open_form($r,$url);
 2021: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
 2022:         $r->print('Edit the meta data<br />');
 2023:         &close_form($r,$url);
 2024:     }
 2025:     if ($env{'form.store'}) {
 2026:     }
 2027: 
 2028:     if ($env{'form.uploaddoc.filename'}) {
 2029:         if ($can_upload) {
 2030: 	    &upload($r,$url,$group);
 2031:         } else {
 2032:             &missing_priv($r,$url,'upload');
 2033:         }
 2034:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
 2035:         if ($can_delete) {
 2036: 	    &delete_confirmed($r,$url,$group);
 2037:         } else {
 2038:             &missing_priv($r,$url,'delete');
 2039:         }
 2040:     } elsif ($env{'form.action'} eq 'delete') {
 2041:         if ($can_delete) {
 2042: 	    &delete($r,$url);
 2043:         } else {
 2044:             &missing_priv($r,$url,'delete');
 2045:         }
 2046:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
 2047:         if ($can_delete) {
 2048: 	    &delete_dir_confirmed($r,$url);
 2049:         } else {
 2050:             &missing_priv($r,$url,'delete');
 2051:         }
 2052:     } elsif ($env{'form.action'} eq 'deletedir') {
 2053:         if ($can_delete) {
 2054: 	    &delete_dir($r,$url);
 2055:         } else {
 2056:             &missing_priv($r,$url,'delete');
 2057:         }
 2058:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
 2059:         if ($can_modify) {
 2060: 	    &rename_confirmed($r,$url,$group);
 2061:         } else {
 2062:             &missing_priv($r,$url,'rename');
 2063:         }
 2064:     } elsif ($env{'form.rename'}) {
 2065:         $env{'form.selectfile'} = $env{'form.rename'};
 2066:         $env{'form.action'} = 'rename';
 2067:         if ($can_modify) {
 2068: 	    &rename($r,$url);
 2069:         } else {
 2070:             &missing_priv($r,$url,'rename');
 2071:         }
 2072:     } elsif ($env{'form.access'}) {
 2073:         $env{'form.selectfile'} = $env{'form.access'};
 2074:         $env{'form.action'} = 'chgaccess';
 2075:         &display_access($r,$url,$group,$can_setacl,$port_path);
 2076:     } elsif ($env{'form.action'} eq 'chgaccess') {
 2077:         if ($can_setacl) {
 2078:             &update_access($r,$url,$group,$port_path);
 2079:         } else {
 2080:             &missing_priv($r,$url,'setacl');
 2081:         }
 2082:     } elsif ($env{'form.action'} eq 'rolepicker') {
 2083:         if ($can_setacl) { 
 2084:             &role_options_window($r);
 2085:         } else {
 2086:             &missing_priv($r,$url,'setacl');
 2087:         }
 2088:     } elsif ($env{'form.createdir'}) {
 2089:         if ($can_upload) {
 2090: 	    &createdir($r,$url);
 2091:         } else {
 2092:             &missing_priv($r,$url,'upload');
 2093:         }
 2094:     } elsif ($env{'form.lockinfo'}) {
 2095:         &lock_info($r,$url,$group);
 2096:     } else {
 2097: 	my $current_path='/';
 2098: 	if ($env{'form.currentpath'}) {
 2099: 	    $current_path = $env{'form.currentpath'};
 2100: 	}
 2101:         if ($caller eq 'coursegrp_portfolio') {
 2102:             &Apache::lonhtmlcommon::clear_breadcrumbs();
 2103:             $r->print(&coursegrp_portfolio_header($udom,$uname,$grp_desc));
 2104:         }
 2105:         my @dir_list=&get_dir_list($portfolio_root);
 2106: 	if ($dir_list[0] eq 'no_such_dir'){
 2107: 	    # two main reasons for this:
 2108:             #    1) never been here, so directory structure not created
 2109: 	    #    2) back-button navigation after deleting a directory
 2110: 	    if ($current_path eq '/'){
 2111: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
 2112: 					       &get_port_path());
 2113: 	    } else {
 2114:                 # some directory that snuck in get rid of the directory
 2115:                 # from the recent pulldown, just in case
 2116: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
 2117: 						      [$current_path]);
 2118: 		$current_path = '/'; # force it back to the root        
 2119: 	    }
 2120: 	    # now grab the directory list again, for the first time
 2121: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
 2122: 					    $udom,$uname,$portfolio_root);
 2123:         }
 2124: 	# need to know if directory is empty so it can be removed if desired
 2125: 	my $is_empty=(@dir_list == 2);
 2126: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,
 2127: 			$can_upload);
 2128:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group,
 2129:                            $can_upload,$can_modify,$can_delete,$can_setacl);
 2130: 	$r->print(&Apache::loncommon::end_page());
 2131:     }
 2132:     return OK;
 2133: }
 2134: 
 2135: 1;
 2136: __END__

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