File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.124: download - view: text, annotated - select for diffs
Wed Jun 28 19:52:43 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- select mode was broken BUG#4845

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

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