File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.131: download - view: text, annotated - select for diffs
Wed Jul 5 22:52:53 2006 UTC (17 years, 11 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Saving work in progress. Closer to showing versioned files.

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

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