File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.200: download - view: text, annotated - select for diffs
Fri Dec 5 10:23:56 2008 UTC (15 years, 6 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Replaced all hardcoded nowrap styles with LC_nobreak classes

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

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