File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.139: download - view: text, annotated - select for diffs
Wed Jul 19 21:58:16 2006 UTC (17 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- tested the group and ref canges, seem to work

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

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