File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.132: download - view: text, annotated - select for diffs
Sat Jul 8 01:20:40 2006 UTC (18 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Adding breadcrumbs for course group portfolio files directory listing screen.

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

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