File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.154: download - view: text, annotated - select for diffs
Sat Aug 12 06:18:44 2006 UTC (17 years, 10 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Way stupid, but it shows handback info for versioned files.
	Saving work in progress.

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

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