File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.138: download - view: text, annotated - select for diffs
Wed Jul 19 15:31:05 2006 UTC (17 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- eliminating some group info passing

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

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