File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.171: download - view: text, annotated - select for diffs
Mon Nov 6 20:02:58 2006 UTC (17 years, 6 months ago) by rezaferry
Branches: MAIN
CVS tags: HEAD
Added the help link to file actions (also added the help file)
Changed the manual (texxml file) to include help on catalog

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

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