File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.141: download - view: text, annotated - select for diffs
Wed Aug 2 02:34:54 2006 UTC (17 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
&prepend_group() already added the group identifier as per FIXME.

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

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