Annotation of loncom/interface/portfolio.pm, revision 1.142

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

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