File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.198: download - view: text, annotated - select for diffs
Fri Nov 28 17:04:25 2008 UTC (15 years, 6 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Optimized screen layout for portfolio files in selection mode:
- Wrapped list of selected files from different folders in data table
- Added missing &mt() calls to button texts
- Wrapped buttons in paragraph
- Improved check files message and moved it under the selected files table
  (better place because selection does not take place in the selected files table)

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

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