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

1.125     albertel    1: # The LearningOnline Network
                      2: # portfolio browser
                      3: #
1.145   ! banghart    4: # $Id: portfolio.pm,v 1.144 2006/08/04 20:41:32 albertel 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.145   ! banghart  297:                             $line .= '</td><td>'.'<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);
1.143     banghart  342:                         $line = '<td colspan="2">&nbsp;</td>';
1.140     banghart  343:                         if (($v_fname eq $fname)&&($v_extension eq $extension)) {
                    344: 		            &display_directory_line($r,$select_mode,$fullpath, $v_filename, $mtime, $size, 
                    345: 		                            $css_class, $line, \%access_controls, $curr_access, $now,
                    346: 		                             \%version_flag, $href_location, $url, $current_path, $access_admin_text, 1);
                    347: 		        }
                    348: 		    }
1.134     banghart  349: 		}
1.47      banghart  350:             }
                    351:         }
                    352:     }
1.77      banghart  353:     if ($select_mode eq 'true') {
                    354:         $r->print('</table>
1.127     albertel  355:             <input type="hidden" name="continue" value="true" />
                    356:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'" />
                    357:             <input type="hidden" name="mode" value="selectfile" />
1.60      banghart  358:             <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
1.48      banghart  359:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
                    360:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
1.77      banghart  361:         </form>');        
                    362:     } else {
1.120     raeburn   363:         $r->print('</table>');
                    364:         if ($can_delete) {
                    365:             $r->print('
1.77      banghart  366:         <input type="submit" name="doit" value="Delete Checked Files" />
                    367:         <input type="hidden" name="action" value="delete" />
                    368:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
1.120     raeburn   369:         </form>'
                    370:             );
                    371:         }
1.77      banghart  372:     }
1.47      banghart  373: }
1.72      banghart  374: 
1.24      albertel  375: sub open_form {
1.94      raeburn   376:     my ($r,$url)=@_;
1.65      banghart  377:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
1.102     raeburn   378:     $r->print('<form name="portform" method="post" action="'.$url.'">');
1.24      albertel  379:     $r->print('<input type="hidden" name="action" value="'.
1.82      albertel  380: 	      $env{'form.action'}.'" />');
1.24      albertel  381:     $r->print('<input type="hidden" name="confirmed" value="1" />');
1.65      banghart  382:     foreach (@files) {
                    383:         $r->print('<input type="hidden" name="selectfile" value="'.
                    384: 	      $_.'" />');
                    385:     }
1.24      albertel  386:     $r->print('<input type="hidden" name="currentpath" value="'.
1.82      albertel  387: 	      $env{'form.currentpath'}.'" />');
1.24      albertel  388: }
                    389: 
                    390: sub close_form {
1.137     albertel  391:     my ($r,$url,$button_text)=@_;
1.108     raeburn   392:     if (!defined($button_text)) {
                    393:         $button_text = {
                    394:                          'continue' => &mt('Continue'),
                    395:                          'cancel'   => &mt('Cancel'),
                    396:                        };
                    397:     }
1.138     albertel  398:     $r->print('<p><input type="submit" value="'.$button_text->{'continue'}.'" />');
1.137     albertel  399:     $r->print(&group_form_data().'</p></form>');
1.112     albertel  400:     $r->print('<form action="'.$url.'" method="post">
1.24      albertel  401:                <p>
                    402:               <input type="hidden" name="currentpath" value="'.
1.137     albertel  403: 	      $env{'form.currentpath'}.'" />'.
                    404: 	      &group_form_data());
1.108     raeburn   405:     $r->print("\n".'   <input type="submit" value="'.$button_text->{'cancel'}.'" />
1.94      raeburn   406:                </p></form>'); 
1.24      albertel  407: }
                    408: 
                    409: sub display_file {
1.27      albertel  410:     my ($path,$filename)=@_;
1.65      banghart  411:     my $display_file_text;
1.116     albertel  412:     my $file_start='<span class="LC_filename">';
                    413:     my $file_end='</span>';
1.82      albertel  414:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
1.65      banghart  415:     if (!defined($filename)) { 
1.82      albertel  416:         $filename=$env{'form.selectfile'};
1.116     albertel  417:         $display_file_text = $file_start.$path.$filename.$file_end;
1.65      banghart  418:     } elsif (ref($filename) eq "ARRAY") {
1.116     albertel  419:         foreach my $file (@$filename) {
                    420:             $display_file_text .= $file_start.$path.$file.$file_end.'<br />';
1.65      banghart  421:         }
                    422:     } elsif (ref($filename) eq "SCALAR") {
1.116     albertel  423:         $display_file_text = $file_start.$path.$$filename.$file_end;
                    424:     } else {
                    425: 	$display_file_text = $file_start.$path.$filename.$file_end;
1.65      banghart  426:     }
                    427:     return $display_file_text;
1.24      albertel  428: }
                    429: 
                    430: sub done {
1.137     albertel  431:     my ($message,$url)=@_;
1.76      banghart  432:     unless (defined $message) {
                    433:         $message='Done';
                    434:     }
1.94      raeburn   435:     my $result = '<h3><a href="'.$url.'?currentpath='.
                    436: 	         $env{'form.currentpath'}.
                    437: 	         '&fieldname='.$env{'form.fieldname'}.
1.137     albertel  438: 	         '&mode='.$env{'form.mode'}.
                    439: 		 &group_args();
1.94      raeburn   440:     $result .= '">'.&mt($message).'</a></h3>';
                    441:     return $result;
1.24      albertel  442: }
                    443: 
                    444: sub delete {
1.138     albertel  445:     my ($r,$url)=@_;
1.55      banghart  446:     my @check;
1.82      albertel  447:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
1.138     albertel  448:     $file_name = &prepend_group($file_name);
1.65      banghart  449:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
1.138     albertel  450:     my ($uname,$udom) = &get_name_dom();
1.94      raeburn   451:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.55      banghart  452:         $r->print ("The file is locked and cannot be deleted.<br />");
1.137     albertel  453:         $r->print(&done('Back',$url));
1.55      banghart  454:     } else {
1.66      banghart  455:         if (scalar(@files)) {
1.94      raeburn   456:             &open_form($r,$url);
1.66      banghart  457:             $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
1.137     albertel  458:             &close_form($r,$url);
1.66      banghart  459:         } else {
                    460:             $r->print("No file was checked to delete.<br />");
1.137     albertel  461:             $r->print(&done(undef,$url));
1.66      banghart  462:         }
1.55      banghart  463:     }
1.24      albertel  464: } 
                    465: 
                    466: sub delete_confirmed {
1.138     albertel  467:     my ($r,$url)=@_;
1.65      banghart  468:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
                    469:     my $result;
1.138     albertel  470:     my ($uname,$udom) = &get_name_dom();
                    471:     my $port_path = &get_port_path();
1.65      banghart  472:     foreach my $delete_file (@files) {
1.94      raeburn   473:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
1.82      albertel  474: 					       $env{'form.currentpath'}.
1.65      banghart  475: 					       $delete_file);
                    476:         if ($result ne 'ok') {
1.116     albertel  477: 	$r->print('<span class="LC_error"> An error occured ('.$result.
                    478: 		  ') while trying to delete '.&display_file(undef, $delete_file).'</span><br />');
1.65      banghart  479:         }
1.24      albertel  480:     }
1.137     albertel  481:     $r->print(&done(undef,$url));
1.24      albertel  482: }
                    483: 
1.30      banghart  484: sub delete_dir {
1.137     albertel  485:     my ($r,$url)=@_;
1.94      raeburn   486:     &open_form($r,$url);
1.30      banghart  487:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
1.137     albertel  488:     &close_form($r,$url);
1.30      banghart  489: } 
                    490: 
                    491: sub delete_dir_confirmed {
1.138     albertel  492:     my ($r,$url)=@_;
1.82      albertel  493:     my $directory_name = $env{'form.currentpath'};
1.81      albertel  494:     $directory_name =~ s|/$||; # remove any trailing slash
1.138     albertel  495:     my ($uname,$udom) = &get_name_dom();
                    496:     my $namespace = &get_namespace();
                    497:     my $port_path = &get_port_path();
1.94      raeburn   498:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
1.30      banghart  499: 					       $directory_name);
1.32      banghart  500: 					       
1.30      banghart  501:     if ($result ne 'ok') {
1.116     albertel  502: 	$r->print('<span class="LC_error"> An error occured (dir) ('.$result.
                    503: 		  ') while trying to delete '.$directory_name.'</span><br />');
1.32      banghart  504:     } else {
1.41      banghart  505:         # now remove from recent
                    506: #        $r->print('<br /> removing '.$directory_name.'<br /');
1.94      raeburn   507:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
1.32      banghart  508:         my @dirs = split m!/!, $directory_name;
                    509:         
                    510: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
                    511:         $directory_name='/';
                    512:         for (my $i=1; $i < (@dirs - 1); $i ++){
                    513:             $directory_name .= $dirs[$i].'/';
                    514:         }
1.82      albertel  515:         $env{'form.currentpath'} = $directory_name;
1.30      banghart  516:     }
1.137     albertel  517:     $r->print(&done(undef,$url));
1.30      banghart  518: }
                    519: 
1.24      albertel  520: sub rename {
1.138     albertel  521:     my ($r,$url)=@_;
1.82      albertel  522:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
1.138     albertel  523:     my ($uname,$udom) = &get_name_dom();
                    524:     $file_name = &prepend_group($file_name);
1.94      raeburn   525:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.55      banghart  526:         $r->print ("The file is locked and cannot be renamed.<br />");
1.137     albertel  527:         $r->print(&done(undef,$url));
1.55      banghart  528:     } else {
1.94      raeburn   529:         &open_form($r,$url);
1.55      banghart  530:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
                    531:                    <input name="filenewname" type="input" size="50" />?</p>');
1.137     albertel  532:         &close_form($r,$url);
1.55      banghart  533:     }
1.24      albertel  534: }
                    535: 
                    536: sub rename_confirmed {
1.138     albertel  537:     my ($r,$url)=@_;
1.82      albertel  538:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
1.138     albertel  539:     my ($uname,$udom) = &get_name_dom();
                    540:     my $port_path = &get_port_path();
1.27      albertel  541:     if ($filenewname eq '') {
1.116     albertel  542: 	$r->print('<span class="LC_error">'.
1.27      albertel  543: 		  &mt("Error: no valid filename was provided to rename to.").
1.116     albertel  544: 		  '</span><br />');
1.137     albertel  545: 	$r->print(&done(undef,$url));
1.27      albertel  546: 	return;
                    547:     } 
                    548:     my $result=
1.94      raeburn   549: 	&Apache::lonnet::renameuserfile($uname,$udom,
                    550:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
                    551:             $port_path.$env{'form.currentpath'}.$filenewname);
1.27      albertel  552:     if ($result ne 'ok') {
1.116     albertel  553: 	$r->print('<span class="LC_error">'.
                    554: 		  &mt('An errror occured ([_1]) while trying to rename [_2]'
                    555: 		      .' to [_3]',$result,&display_file(),
                    556: 		      &display_file('',$filenewname)).'</span><br />');
1.27      albertel  557:     }
1.82      albertel  558:     if ($filenewname ne $env{'form.filenewname'}) {
1.116     albertel  559:         $r->print(&mt("The new file name was changed from:<br />[_1] to [_2]",
                    560: 		      '<strong>'.&display_file('',$env{'form.filenewname'}).'</strong>',
                    561: 		      '<strong>'.&display_file('',$filenewname).'</strong>'));
1.66      banghart  562:     }
1.137     albertel  563:     $r->print(&done(undef,$url));
1.27      albertel  564: }
1.102     raeburn   565: 
1.104     raeburn   566: sub display_access {
1.137     albertel  567:     my ($r,$url,$group,$can_setacl,$port_path) = @_;
1.138     albertel  568:     my ($uname,$udom) = &get_name_dom();
1.104     raeburn   569:     my $file_name = $env{'form.currentpath'}.$env{'form.access'};
1.138     albertel  570:     $file_name = &prepend_group($file_name);
1.104     raeburn   571:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    572:                                                                         $uname);
                    573:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group,$file_name);
1.120     raeburn   574:     my $aclcount = keys(%access_controls);
1.142     raeburn   575:     my $header = '<h3>'.&mt('Allowing others to retrieve file: [_1]',$port_path.$env{'form.currentpath'}.$env{'form.access'}).'</h3>';
1.123     albertel  576:     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   577: 
                    578:     if ($group eq '') {
                    579:         $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>"));
                    580:     }
                    581:     
1.120     raeburn   582:     if ($can_setacl) {
                    583:         &open_form($r,$url);
                    584:         $r->print($header.$info);
                    585:         &access_setting_table($r,$access_controls{$file_name});
                    586:         my $button_text = {
1.108     raeburn   587:                         'continue' => &mt('Proceed'),
                    588:                         'cancel' => &mt('Back to directory listing'),
                    589:                       };
1.137     albertel  590:         &close_form($r,$url,$button_text);
1.120     raeburn   591:     } else {
                    592:         $r->print($header);
                    593:         if ($aclcount) {  
                    594:             $r->print($info);
                    595:         }
1.137     albertel  596:         &view_access_settings($r,$url,$access_controls{$file_name},$aclcount);
1.120     raeburn   597:     }
                    598: }
                    599: 
                    600: sub view_access_settings {
1.137     albertel  601:     my ($r,$url,$access_controls,$aclcount) = @_;
1.120     raeburn   602:     my ($showstart,$showend);
                    603:     my %todisplay;
                    604:     foreach my $key (sort(keys(%{$access_controls}))) {
                    605:         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
                    606:         $todisplay{$scope}{$key} = $$access_controls{$key};
                    607:     }
                    608:     if ($aclcount) {
                    609:         $r->print(&mt('<h4>Current access controls defined for this file:</h4>'));
                    610:         $r->print(&Apache::loncommon::start_data_table());
                    611:         $r->print(&Apache::loncommon::start_data_table_header_row());
                    612:         $r->print('<th>'.&mt('Access control').'</th><th>'.&mt('Dates available').
                    613:                   '</th><th>'.&mt('Additional information').'</th>');
                    614:         $r->print(&Apache::loncommon::end_data_table_header_row());
                    615:         my $count = 1;
                    616:         my $chg = 'none';
                    617:         &build_access_summary($r,$count,$chg,%todisplay);
                    618:         $r->print(&Apache::loncommon::end_data_table());
                    619:     } else {
                    620:         $r->print(&mt('No access control settings currently exist for this file.<br />' ));
                    621:     }
                    622:     $r->print('<br /><a href="'.$url.'?currentpath='.$env{'form.currentpath'}.
1.137     albertel  623:               &group_args().'">'.&mt('Return to directory listing').'</a>');
1.120     raeburn   624:     return;
1.104     raeburn   625: }
                    626: 
1.120     raeburn   627: sub build_access_summary {
                    628:     my ($r,$count,$chg,%todisplay) = @_; 
                    629:     my ($showstart,$showend);
                    630:     my %scope_desc = (
                    631:                       public => 'Public',
                    632:                       guest => 'Passphrase-protected',
                    633:                       domains => 'Conditional: domain-based',
                    634:                       users => 'Conditional: user-based',
                    635:                       course => 'Conditional: course-based',
                    636:                       group => 'Conditional: group-based',
                    637:                      );
                    638:     my @allscopes = ('public','guest','domains','users','course','group');
                    639:     foreach my $scope (@allscopes) {
                    640:         if ((!(exists($todisplay{$scope}))) || (ref($todisplay{$scope}) ne 'HASH')) {
                    641:             next;
                    642:         }
                    643:         foreach my $key (sort(keys(%{$todisplay{$scope}}))) {
                    644:             if ($count) {
                    645:                 $r->print(&Apache::loncommon::start_data_table_row());
                    646:             }
                    647:             my ($num,$scope,$end,$start) = &unpack_acc_key($key);
                    648:             my $content = $todisplay{$scope}{$key};
                    649:             if ($chg eq 'delete') {
                    650:                 $showstart = &mt('Deleted');
                    651:                 $showend = $showstart;
                    652:             } else {
                    653:                 $showstart = localtime($start);
                    654:                 if ($end == 0) {
                    655:                     $showend = &mt('No end date');
                    656:                 } else {
                    657:                     $showend = localtime($end);
                    658:                 }
                    659:             }
                    660:             $r->print('<td>'.&mt($scope_desc{$scope}));
                    661:             if (($scope eq 'course') || ($scope eq 'group')) {
                    662:                 if ($chg ne 'delete') {
                    663:                     my $cid = $content->{'domain'}.'_'.$content->{'number'};
                    664:                     my %course_description = &Apache::lonnet::coursedescription($cid);
                    665:                     $r->print('<br />('.$course_description{'description'}.')');
                    666:                 }
                    667:             }
                    668:             $r->print('</td><td>'.&mt('Start: ').$showstart.
                    669:                   '<br />'.&mt('End: ').$showend.'</td><td>');
                    670:             if ($chg ne 'delete') {
                    671:                 if ($scope eq 'guest') {
                    672:                     $r->print(&mt('Passphrase').': '.$content->{'password'});
                    673:                 } elsif ($scope eq 'course' || $scope eq 'group') {
                    674:                     $r->print('<table><tr>');
                    675:                     $r->print('<th>'.&mt('Roles').'</th><th>'.
                    676:                           &mt('Access').'</th><th>'.
                    677:                                           &mt('Sections').'</th>');
                    678:                     if ($scope eq 'course') {
                    679:                         $r->print('<th>'.&mt('Groups').'</th>');
                    680:                     } else {
                    681:                         $r->print('<th>'.&mt('Teams').'</th>');
                    682:                     }
                    683:                     $r->print('</tr>');
                    684:                     foreach my $id (sort(keys(%{$content->{'roles'}}))) {
                    685:                         $r->print('<tr>');
                    686:                         foreach my $item ('role','access','section','group') {
                    687:                             $r->print('<td>');
                    688:                             if ($item eq 'role') {
                    689:                                 my $ucscope = $scope;
                    690:                                 $ucscope =~ s/^(\w)/uc($1)/e;
                    691:                                 my $role_output;
                    692:                                 foreach my $role (@{$content->{'roles'}{$id}{$item}}) {
                    693:                                     if ($role eq 'all') {
                    694:                                         $role_output .= $role.',';
                    695:                                     } elsif ($role =~ /^cr/) {
                    696:                                         $role_output .= (split('/',$role))[3].',';
                    697:                                     } else {
                    698:                                         $role_output .= &Apache::lonnet::plaintext($role,$ucscope).',';
                    699:                                     }
                    700:                                 }
                    701:                                 $role_output =~ s/,$//;
                    702:                                 $r->print($role_output);
                    703:                             } else {
                    704:                                 $r->print(join(',',@{$content->{'roles'}{$id}{$item}}));
                    705:                             }
1.121     albertel  706:                             $r->print('</tr>');
1.120     raeburn   707:                         }
1.121     albertel  708: 			$r->print("</table>");
1.120     raeburn   709:                     }
1.121     albertel  710: 		    $r->print("</tr></table>");
1.120     raeburn   711:                 } elsif ($scope eq 'domains') {
                    712:                     $r->print(&mt('Domains: ').join(',',@{$content->{'dom'}}));
                    713:                 } elsif ($scope eq 'users') {
                    714:                     my $curr_user_list = &sort_users($content->{'users'});
                    715:                     $r->print(&mt('Users: ').$curr_user_list);
                    716:                 } else {
                    717:                     $r->print('&nbsp;');
                    718:                 }
                    719:             } else {
                    720:                 $r->print('&nbsp;');
                    721:             }
                    722:             $r->print('</td>');
                    723:             $r->print(&Apache::loncommon::end_data_table_row());
                    724:             $count ++;
                    725:         }
                    726:     }
                    727: }
                    728: 
                    729: 
1.104     raeburn   730: sub update_access {
1.137     albertel  731:     my ($r,$url,$group,$port_path) = @_;
1.104     raeburn   732:     my $totalprocessed = 0;
                    733:     my %processing;
                    734:     my %title  = (
1.108     raeburn   735:                          'activate' => 'New control(s) added',
                    736:                          'delete'   => 'Existing control(s) deleted',
                    737:                          'update'   => 'Existing control(s) modified',
1.104     raeburn   738:                      );
1.108     raeburn   739:     my $changes;
1.104     raeburn   740:     foreach my $chg (sort(keys(%title))) {     
                    741:         @{$processing{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
                    742:         $totalprocessed += @{$processing{$chg}};
                    743:         foreach my $num (@{$processing{$chg}}) {
                    744:             my $scope = $env{'form.scope_'.$num};
                    745:             my ($start,$end) = &get_dates_from_form($num);
                    746:             my $newkey = $num.':'.$scope.'_'.$end.'_'.$start;
                    747:             if ($chg eq 'delete') {
                    748:                 $$changes{$chg}{$newkey} = 1;
                    749:             } else {
                    750:                 $$changes{$chg}{$newkey} = 
1.108     raeburn   751:                             &build_access_record($num,$scope,$start,$end,$chg);
1.104     raeburn   752:             }
                    753:         }
                    754:     }
                    755:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
1.142     raeburn   756:     $r->print('<h3>'.&mt('Allowing others to retrieve file: [_1]',
1.133     raeburn   757:               $port_path.$file_name).'</h3>'."\n");
1.138     albertel  758:     $file_name = &prepend_group($file_name);
                    759:     my ($uname,$udom) = &get_name_dom();
1.104     raeburn   760:     my ($errors,$outcome,$deloutcome,$new_values,$translation);
                    761:     if ($totalprocessed) {
                    762:         ($outcome,$deloutcome,$new_values,$translation) =
1.108     raeburn   763:         &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
                    764:                                                 $uname);
1.104     raeburn   765:     }
1.108     raeburn   766:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    767:                                                                        $uname);
1.109     albertel  768:     my %access_controls = 
                    769: 	&Apache::lonnet::get_access_controls($current_permissions,
                    770: 					     $group,$file_name);
1.104     raeburn   771:     if ($totalprocessed) {
                    772:         if ($outcome eq 'ok') {
                    773:             my $updated_controls = $access_controls{$file_name};
                    774:             my ($showstart,$showend);
                    775:             $r->print(&Apache::loncommon::start_data_table());
1.110     albertel  776:             $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn   777:             $r->print('<th>'.&mt('Type of change').'</th><th>'.
                    778:                       &mt('Access control').'</th><th>'.&mt('Dates available').
                    779:                       '</th><th>'.&mt('Additional information').'</th>');
1.110     albertel  780:             $r->print(&Apache::loncommon::end_data_table_header_row());
1.104     raeburn   781:             foreach my $chg (sort(keys(%processing))) {
                    782:                 if (@{$processing{$chg}} > 0) {
                    783:                     if ($chg eq 'delete') {
                    784:                         if (!($deloutcome eq 'ok')) {
1.116     albertel  785:                             $errors .='<span class="LC_error">'.
                    786: 				&mt('A problem occurred deleting access controls: [_1]',$deloutcome).
                    787: 				'</span>';
1.104     raeburn   788:                             next;
                    789:                         }
                    790:                     }
                    791:                     my $numchgs = @{$processing{$chg}};
                    792:                     $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn   793:                     $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
                    794:                               '.</td>');
1.104     raeburn   795:                     my $count = 0;
1.120     raeburn   796:                     my %todisplay;
1.104     raeburn   797:                     foreach my $key (sort(keys(%{$$changes{$chg}}))) {
1.120     raeburn   798:                         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
1.104     raeburn   799:                         my $newkey = $key;
                    800:                         if ($chg eq 'activate') {
                    801:                             $newkey =~ s/^(\d+)/$$translation{$1}/;
                    802:                         }
1.120     raeburn   803:                         $todisplay{$scope}{$newkey} = $$updated_controls{$newkey};
1.104     raeburn   804:                     }
1.120     raeburn   805:                     &build_access_summary($r,$count,$chg,%todisplay);  
1.104     raeburn   806:                 }
                    807:             }
                    808:             $r->print(&Apache::loncommon::end_data_table());
                    809:         } else {
                    810:             if ((@{$processing{'activate'}} > 0) || (@{$processing{'update'}} > 0)) {
1.116     albertel  811:                 $errors .= '<span class="LC_error">'.
                    812: 		    &mt('A problem occurred storing access control settings: [_1]',$outcome).
                    813: 		    '</span>';
1.104     raeburn   814:             }
                    815:         }
                    816:         if ($errors) { 
                    817:             $r->print($errors);
                    818:         }
                    819:     }
1.108     raeburn   820:     my $allnew = 0;
                    821:     my $totalnew = 0;
                    822:     my $status = 'new';
                    823:     my ($firstitem,$lastitem);
                    824:     foreach my $newitem ('course','group','domains','users') {
                    825:         $allnew += $env{'form.new'.$newitem};
                    826:     }
                    827:     if ($allnew > 0) {
                    828:         my $now = time;
                    829:         my $then = $now + (60*60*24*180); # six months approx.
1.138     albertel  830:         &open_form($r,$url);
1.108     raeburn   831:         foreach my $newitem ('course','group','domains','users') {
                    832:             if ($env{'form.new'.$newitem} > 0) {
                    833:                 $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 />');
                    834:                 $firstitem = $totalnew;
                    835:                 $lastitem = $totalnew + $env{'form.new'.$newitem};
                    836:                 $totalnew = $lastitem;
                    837:                 my @numbers;   
                    838:                 for (my $i=$firstitem; $i<$lastitem; $i++) {
                    839:                     push (@numbers,$i);
                    840:                 }
                    841:                 &display_access_row($r,$status,$newitem,\@numbers,
                    842:                                     $access_controls{$file_name},$now,$then);
                    843:             }
                    844:         }
1.137     albertel  845:         &close_form($r,$url);
1.108     raeburn   846:     } else {
                    847:         $r->print('<br /><a href="'.$url.'?access='.$env{'form.selectfile'}.
1.138     albertel  848:                   '&amp;currentpath='.$env{'form.currentpath'}.&group_args().'">'.
1.133     raeburn   849:                    &mt('Display all access settings for this file').'</a>'.
                    850:                   '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.
                    851:                   '<a href="'.$url.'?currentpath='.$env{'form.currentpath'}.
1.137     albertel  852:                   &group_args().'">'.&mt('Return to directory listing').'</a>');
1.108     raeburn   853:     }
1.104     raeburn   854:     return;
                    855: }
                    856: 
                    857: sub build_access_record {
1.108     raeburn   858:     my ($num,$scope,$start,$end,$chg) = @_;
1.109     albertel  859:     my $record = {
                    860: 	type => $scope,
                    861: 	time => {
                    862: 	    start => $start,
                    863: 	    end   => $end
                    864: 	    },
                    865: 	    };
                    866: 		
                    867:     if ($scope eq 'guest') {	
                    868:         $record->{'password'} = $env{'form.password'};
1.108     raeburn   869:     } elsif (($scope eq 'course') || ($scope eq 'group')) {
1.109     albertel  870:         $record->{'domain'} = $env{'form.crsdom_'.$num};
                    871: 	$record->{'number'} = $env{'form.crsnum_'.$num};
1.108     raeburn   872:         my @role_ids;
1.109     albertel  873:         my @delete_role_ids =
                    874:             &Apache::loncommon::get_env_multiple('form.delete_role_'.$num);
                    875: 	my @preserves =
                    876: 	    &Apache::loncommon::get_env_multiple('form.preserve_role_'.$num);
                    877: 	if (@delete_role_ids) {
                    878: 	    foreach my $id (@preserves) {
                    879: 		if (grep {$_ = $id} (@delete_role_ids)) {
                    880: 		    next;
                    881: 		}
                    882: 		push(@role_ids,$id); 
                    883: 	    }
                    884: 	} else {
                    885: 	    push(@role_ids,@preserves);
                    886: 	}
                    887: 
                    888: 	my $next_id = $env{'form.add_role_'.$num};
                    889: 	if ($next_id) {
                    890: 	    push(@role_ids,$next_id);
                    891: 	}
                    892: 
1.108     raeburn   893:         foreach my $id (@role_ids) {
                    894:             my (@roles,@accesses,@sections,@groups);
                    895:             if (($id == $next_id) && ($chg eq 'update')) {
1.109     albertel  896:                 @roles    = split(/,/,$env{'form.role_'.$num.'_'.$next_id});
1.108     raeburn   897:                 @accesses = split(/,/,$env{'form.access_'.$num.'_'.$next_id});
                    898:                 @sections = split(/,/,$env{'form.section_'.$num.'_'.$next_id});
1.109     albertel  899:                 @groups   = split(/,/,$env{'form.group_'.$num.'_'.$next_id});
1.108     raeburn   900:             } else {
                    901:                 @roles = &Apache::loncommon::get_env_multiple('form.role_'.$num.'_'.$id);
                    902:                 @accesses = &Apache::loncommon::get_env_multiple('form.access_'.$num.'_'.$id);
                    903:                 @sections = &Apache::loncommon::get_env_multiple('form.section_'.$num.'_'.$id);
                    904:                 @groups = &Apache::loncommon::get_env_multiple('form.group_'.$num.'_'.$id);
                    905:             }
1.109     albertel  906: 	    $record->{'roles'}{$id}{'role'}    = \@roles;
                    907: 	    $record->{'roles'}{$id}{'access'}  = \@accesses;
                    908: 	    $record->{'roles'}{$id}{'section'} = \@sections;
                    909: 	    $record->{'roles'}{$id}{'group'}   = \@groups;
1.108     raeburn   910:         }
                    911:     } elsif ($scope eq 'domains') {
                    912:         my @doms = &Apache::loncommon::get_env_multiple('form.dom_'.$num);
1.109     albertel  913: 	$record->{'dom'} = \@doms;
1.108     raeburn   914:     } elsif ($scope eq 'users') {
                    915:         my $userlist = $env{'form.users_'.$num};
1.109     albertel  916:         $userlist =~ s/\s+//sg;
                    917: 	my %userhash = map { ($_,1) } (split(/,/,$userlist));
                    918:         foreach my $user (keys(%userhash)) {
1.108     raeburn   919:             my ($uname,$udom) = split(/:/,$user);
1.109     albertel  920: 	    push(@{$record->{'users'}}, {
                    921: 		'uname' => $uname,
                    922: 		'udom'  => $udom
                    923: 		});
                    924: 	}
1.108     raeburn   925:     }
1.104     raeburn   926:     return $record;
                    927: }
                    928: 
                    929: sub get_dates_from_form {
                    930:     my ($id) = @_;
                    931:     my $startdate;
                    932:     my $enddate;
                    933:     $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$id);
                    934:     $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$id);
                    935:     if ( exists ($env{'form.noend_'.$id}) ) {
                    936:         $enddate = 0;
                    937:     }
                    938:     return ($startdate,$enddate);
                    939: }
                    940: 
1.108     raeburn   941: sub sort_users {
1.109     albertel  942:     my ($users) = @_; 
                    943:     my @curr_users = map {
                    944: 	$_->{'uname'}.':'.$_->{'udom'}
                    945:     } (@{$users});
                    946:     my $curr_user_list = join(",\n",sort(@curr_users));
1.108     raeburn   947:     return $curr_user_list;
                    948: }
                    949: 
1.104     raeburn   950: sub access_setting_table {
                    951:     my ($r,$access_controls) = @_;
                    952:     my ($public,$publictext);
1.108     raeburn   953:     $publictext = &mt('Off');
1.104     raeburn   954:     my ($guest,$guesttext);
1.108     raeburn   955:     $guesttext = &mt('Off');
1.104     raeburn   956:     my @courses = ();
                    957:     my @groups = ();
                    958:     my @domains = ();
                    959:     my @users = ();
                    960:     my $now = time;
                    961:     my $then = $now + (60*60*24*180); # six months approx.
1.108     raeburn   962:     my ($num,$scope,$publicnum,$guestnum);
                    963:     my (%acl_count,%end,%start);
1.104     raeburn   964:     foreach my $key (sort(keys(%{$access_controls}))) {
1.108     raeburn   965:         ($num,$scope,$end{$key},$start{$key}) = &unpack_acc_key($key);
1.104     raeburn   966:         if ($scope eq 'public') {
                    967:             $public = $key;
1.108     raeburn   968:             $publicnum = $num;
                    969:             $publictext = &acl_status($start{$key},$end{$key},$now);
                    970:         } elsif ($scope eq 'guest') {
                    971:             $guest=$key;
                    972:             $guestnum = $num;  
                    973:             $guesttext = &acl_status($start{$key},$end{$key},$now);
                    974:         } elsif ($scope eq 'course') {
                    975:             push(@courses,$key);
                    976:         } elsif ($scope eq 'group') {
                    977:             push(@groups,$key);
                    978:         } elsif ($scope eq 'domains') {
                    979:             push(@domains,$key);
                    980:         } elsif ($scope eq 'users') {
                    981:             push(@users,$key);
1.104     raeburn   982:         }
1.108     raeburn   983:         $acl_count{$scope} ++;
1.104     raeburn   984:     }
1.108     raeburn   985:     $r->print('<table border="0"><tr><td valign="top">');
                    986:     $r->print('<h3>'.&mt('Public access:').' '.$publictext.'</h3>');
1.104     raeburn   987:     $r->print(&Apache::loncommon::start_data_table());
1.110     albertel  988:     $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn   989:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').'</th>');
1.110     albertel  990:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.104     raeburn   991:     $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn   992:     if ($public) {
                    993:         $r->print('<td>'.&actionbox('old',$publicnum,'public').'</td><td>'.
                    994:                   &dateboxes($publicnum,$start{$public},$end{$public}).'</td>');
                    995:     } else {
                    996:         $r->print('<td>'.&actionbox('new','0','public').'</td><td>'.
                    997:                   &dateboxes('0',$now,$then).'</td>');
                    998:     }
                    999:     $r->print(&Apache::loncommon::end_data_table_row());
                   1000:     $r->print(&Apache::loncommon::end_data_table());
                   1001:     $r->print('</td><td width="40">&nbsp;</td><td valign="top">');
1.120     raeburn  1002:     $r->print('<h3>'.&mt('Passphrase-protected access:').' '.$guesttext.'</h3>');
1.104     raeburn  1003:     $r->print(&Apache::loncommon::start_data_table());
1.110     albertel 1004:     $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn  1005:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').
1.120     raeburn  1006:               '</th><th>'. &mt('Passphrase').'</th>');
1.110     albertel 1007:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.108     raeburn  1008:     $r->print(&Apache::loncommon::start_data_table_row());
                   1009:     my $passwd;
                   1010:     if ($guest) {
1.109     albertel 1011:         $passwd = $$access_controls{$guest}{'password'};
1.108     raeburn  1012:         $r->print('<td>'.&actionbox('old',$guestnum,'guest').'</td><td>'.
                   1013:                   &dateboxes($guestnum,$start{$guest},$end{$guest}).'</td>');
                   1014:     } else {
                   1015:         $r->print('<td>'.&actionbox('new','1','guest').'</td><td>'.
                   1016:                   &dateboxes('1',$now,$then).'</td>');
1.104     raeburn  1017:     }
1.108     raeburn  1018:     $r->print('<td><input type="text" size="15" name="password" value="'.
                   1019:               $passwd.'" /></td>');
1.104     raeburn  1020:     $r->print(&Apache::loncommon::end_data_table_row());
                   1021:     $r->print(&Apache::loncommon::end_data_table());
1.123     albertel 1022:     $r->print('</td></tr><tr><td colspan="3">&nbsp;</td></tr><tr><td valign="top">');
                   1023:     &access_element($r,'domains',\%acl_count,\@domains,$access_controls,$now,$then);
                   1024:     $r->print('</td><td>&nbsp;</td><td valign="top">');
                   1025:     &access_element($r,'users',\%acl_count,\@users,$access_controls,$now,$then);
                   1026:     $r->print('</td></tr><tr><td colspan="3"></td></tr><tr>');
                   1027:     if (@courses > 0 || @groups > 0) {
                   1028:         $r->print('<td colspan="3" valign="top">');
                   1029:     } else {
                   1030:         $r->print('<td valign="top">');
                   1031:     }
                   1032:     &access_element($r,'course',\%acl_count,\@courses,$access_controls,$now,$then);
                   1033:     $r->print('</td>');
                   1034:     if (@courses > 0 || @groups > 0) {
                   1035:         $r->print('</tr><tr><td colspan="3">&nbsp;</td></tr><tr><td colspan="3" valign="top">');
                   1036:     } else {
                   1037:         $r->print('<td>&nbsp;</td><td valign="top">');
                   1038:     }
                   1039:     &access_element($r,'group',\%acl_count,\@groups,$access_controls,$now,$then);
1.108     raeburn  1040:     $r->print('</td></tr></table>');
                   1041: }
                   1042: 
                   1043: sub acl_status {
                   1044:     my ($start,$end,$now) = @_;
                   1045:     if ($start > $now) {
                   1046:         return &mt('Inactive');
                   1047:     }
                   1048:     if ($end && $end<$now) {
                   1049:         return &mt('Inactive');
                   1050:     }
                   1051:     return &mt('Active');
                   1052: }
                   1053: 
                   1054: sub access_element {
                   1055:     my ($r,$type,$acl_count,$items,$access_controls,$now,$then) = @_;
                   1056:     my $title = $type;
                   1057:     $title =~ s/s$//;
                   1058:     $title =~ s/^(\w)/uc($1)/e;
                   1059:     $r->print('<h3>'.&mt('[_1]-based conditional access: ',$title));
                   1060:     if ($$acl_count{$type}) {
                   1061:         $r->print($$acl_count{$type}.' ');
                   1062:         if ($$acl_count{$type} > 1) {
                   1063:             $r->print(&mt('conditions'));
                   1064:         } else {
                   1065:             $r->print(&mt('condition'));
                   1066:         }
                   1067:     } else {
                   1068:         $r->print(&mt('Off'));
                   1069:     }
                   1070:     $r->print('</h3>');
                   1071:     &display_access_row($r,'old',$type,$items,$access_controls,$now,$then);
                   1072:     return;
                   1073: }
                   1074: 
                   1075: sub display_access_row {
                   1076:     my ($r,$status,$type,$items,$access_controls,$now,$then) = @_;
                   1077:     if (@{$items} > 0) {
                   1078:         my @all_doms;
                   1079:         my $colspan = 3;
                   1080:         my $uctype = $type;
                   1081:         $uctype =~ s/^(\w)/uc($1)/e;
                   1082:         $r->print(&Apache::loncommon::start_data_table());
1.110     albertel 1083:         $r->print(&Apache::loncommon::start_data_table_header_row());
                   1084:         $r->print('<th>'.&mt('Action?').'</th><th>'.&mt($uctype).'</th><th>'.
1.108     raeburn  1085:               &mt('Dates available').'</th>');
                   1086:         if (($type eq 'course') || ($type eq 'group')) {
                   1087:             $r->print('<th>'.&mt('Allowed [_1] member affiliations',$type).
                   1088:                       '</th>');
                   1089:             $colspan ++;
                   1090:         } elsif ($type eq 'domains') {
                   1091:             @all_doms = &Apache::loncommon::get_domains();
                   1092:         }
1.110     albertel 1093:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.108     raeburn  1094:         foreach my $key (@{$items}) {
1.118     albertel 1095: 	    $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn  1096:             if (($type eq 'course') || ($type eq 'group')) {
1.118     albertel 1097:                 &course_row($r,$status,$type,$key,$access_controls,$now,$then);
1.108     raeburn  1098:             } elsif ($type eq 'domains') {
                   1099:                 &domains_row($r,$status,$key,\@all_doms,$access_controls,$now,
                   1100:                             $then);
                   1101:             } elsif ($type eq 'users') {
                   1102:                 &users_row($r,$status,$key,$access_controls,$now,$then);
                   1103:             }
1.118     albertel 1104: 	    $r->print(&Apache::loncommon::end_data_table_row());
1.108     raeburn  1105:         }
                   1106:         if ($status eq 'old') {
1.111     albertel 1107: 	    $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn  1108:             $r->print('<td colspan="',$colspan.'">'.&additional_item($type).
                   1109:                       '</td>');
1.111     albertel 1110: 	    $r->print(&Apache::loncommon::end_data_table_row());
1.108     raeburn  1111:         }
                   1112:         $r->print(&Apache::loncommon::end_data_table());
                   1113:     } else {
                   1114:         $r->print(&mt('No [_1]-based conditions defined.<br />',$type).
                   1115:                   &additional_item($type));
                   1116:     }
                   1117:     return;
                   1118: }
                   1119: 
1.110     albertel 1120: sub course_js {
                   1121:     return qq|
1.108     raeburn  1122: <script type="text/javascript">
                   1123: function setRoleOptions(caller,num,cdom,cnum,type) {
                   1124:     addIndexnum = getCallerIndex(caller);
                   1125:     updateIndexnum = getIndex('update',num);
                   1126:     if (caller.checked) {
                   1127:         document.portform.elements[updateIndexnum].checked = true;
                   1128:         var url = '/adm/portfolio?action=rolepicker&setroles='+addIndexnum+'&cnum='+cnum+'&cdom='+cdom+'&type='+type;
                   1129:         var title = 'Roles_Chooser';
                   1130:         var options = 'scrollbars=1,resizable=1,menubar=0';
                   1131:         options += ',width=700,height=600';
                   1132:         rolebrowser = open(url,title,options,'1');
                   1133:         rolebrowser.focus();
                   1134:     } else {
                   1135:         for (var j=0;j<5;j++) {
                   1136:             document.portform.elements[addIndexnum+j].value = '';
                   1137:         }
                   1138:     }
                   1139: }
                   1140: 
                   1141: function getCallerIndex(caller) {
                   1142:     for (var i=0;i<document.portform.elements.length;i++) {
                   1143:         if (document.portform.elements[i] == caller) {
                   1144:             return i;
                   1145:         }
                   1146:     }
                   1147:     return -1;
                   1148: }
                   1149: 
                   1150: function getIndex(name,value) {
                   1151:     for (var i=0;i<document.portform.elements.length;i++) {
                   1152:         if (document.portform.elements[i].name == name && document.portform.elements[i].value == value) {
                   1153:             return i;
                   1154:         }
                   1155:     }
                   1156:     return -1;
                   1157: }
                   1158: 
                   1159: </script>
1.110     albertel 1160: |;
                   1161: }
                   1162: 
                   1163: sub course_row {
1.118     albertel 1164:     my ($r,$status,$type,$item,$access_controls,$now,$then) = @_;
1.115     raeburn  1165:     my $content;
1.110     albertel 1166:     my $defdom = $env{'user.domain'};
                   1167:     if ($status eq 'old') {
1.115     raeburn  1168:         $content = $$access_controls{$item}; 
                   1169:         $defdom =  $content->{'domain'};
1.110     albertel 1170:     }
                   1171:     my $js = &Apache::loncommon::coursebrowser_javascript($defdom)
                   1172: 	.&course_js();
1.108     raeburn  1173:     my $crsgrptext = 'Groups';
                   1174:     if ($type eq 'group') {
                   1175:         $crsgrptext = 'Teams';
                   1176:     }
1.120     raeburn  1177:     my $uctype = $type;
                   1178:     $uctype =~ s/^(\w)/uc($1)/e;
1.108     raeburn  1179:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1180:                                                     $type);
1.110     albertel 1181:     $r->print('<td>'.$js.&actionbox($status,$num,$scope).'</td>');
1.108     raeburn  1182:     if ($status eq 'old') {
1.115     raeburn  1183:         my $cid = $content->{'domain'}.'_'.$content->{'number'};
1.108     raeburn  1184:         my %course_description = &Apache::lonnet::coursedescription($cid);
1.115     raeburn  1185:         $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  1186:     } elsif ($status eq 'new') {
                   1187:         $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>');
                   1188:     }
                   1189:     $r->print('<td>'.&dateboxes($num,$start,$end).'</td>');
1.118     albertel 1190:     $r->print('<td><table><tr>');
1.108     raeburn  1191:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Roles').'</th><th>'.
                   1192:               &mt('Access').'</th><th>'.&mt('Sections').'</th><th>'.
                   1193:               &mt($crsgrptext).'</th></tr>');
                   1194:     if ($status eq 'old') {
                   1195:         my $max_id = 0;
1.115     raeburn  1196:         foreach my $role_id (sort(keys(%{$content->{'roles'}}))) {
1.108     raeburn  1197:             if ($role_id > $max_id) {
                   1198:                 $max_id = $role_id;
                   1199:             }
                   1200:             $max_id ++;
1.115     raeburn  1201:             my $role_selects = &role_selectors($num,$role_id,$status,$type,$content,'display');
1.112     albertel 1202:             $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  1203:         }
1.120     raeburn  1204:         $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  1205:     } elsif ($status eq 'new') {
                   1206:         my $role_id = 1;
                   1207:         my $role_selects = &role_selectors($num,$role_id,$status,$type,undef,'display');
                   1208:         $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);
                   1209:         $r->print('</tr></table></td>');
                   1210:     }
                   1211:     return;
                   1212: }
                   1213: 
                   1214: sub domains_row {
                   1215:     my ($r,$status,$item,$all_doms,$access_controls,$now,$then) = @_;
                   1216:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1217:                                                     'domains');
1.112     albertel 1218:     my $dom_select = '<select name="dom_'.$num.'" size="4" multiple="true">'.
1.108     raeburn  1219:                      ' <option value="">'.&mt('Please select').'</option>';
                   1220:     if ($status eq 'old') {
1.109     albertel 1221:         my $content =  $$access_controls{$item};
                   1222: 	foreach my $dom (@{$all_doms}) {
                   1223:             if ((@{$content->{'dom'}} > 0) 
                   1224: 		&& (grep(/^\Q$dom\E$/,@{$content->{'dom'}}))) {
1.108     raeburn  1225:                 $dom_select .= '<option value="'.$dom.'" selected>'.
                   1226:                                $dom.'</option>';
                   1227:             } else {
                   1228:                 $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
                   1229:             }
                   1230:         }
                   1231:     } else {
                   1232:         foreach my $dom (@{$all_doms}) {
                   1233:             $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
                   1234:         }
                   1235:     }
1.112     albertel 1236:     $dom_select .= '</select>';
1.108     raeburn  1237:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.$dom_select.
                   1238:               '</td><td>'.&dateboxes($num,$start,$end).'</td>');
                   1239: }
                   1240: 
                   1241: sub users_row {
                   1242:     my ($r,$status,$item,$access_controls,$now,$then) = @_;
                   1243:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1244:                                                     'users');
                   1245:     my $curr_user_list;
                   1246:     if ($status eq 'old') {
1.109     albertel 1247:         my $content = $$access_controls{$item};
                   1248:         $curr_user_list = &sort_users($content->{'users'});
1.108     raeburn  1249:     }
                   1250:     $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>');
                   1251: }
                   1252: 
                   1253: sub additional_item {
                   1254:     my ($type) = @_;
                   1255:     my $output = &mt('Add new [_1] condition(s)?',$type).'&nbsp;'.&mt('Number to add: ').'<input type="text" name="new'.$type.'" size="3" value="0" />';
                   1256:     return $output;
                   1257: }
                   1258: 
                   1259: sub actionbox {
                   1260:     my ($status,$num,$scope) = @_;
1.112     albertel 1261:     my $output = '<span style="white-space: nowrap"><label>';
1.108     raeburn  1262:     if ($status eq 'new') {
                   1263:         $output .= '<input type="checkbox" name="activate" value="'.$num.'" />'.
                   1264:         &mt('Activate');
                   1265:     } else {
                   1266:         $output .= '<input type="checkbox" name="delete" value="'.$num.
1.112     albertel 1267:                    '" />'.&mt('Delete').'</label></span><br /><span style="white-space: nowrap">'.
1.108     raeburn  1268:                    '<label><input type="checkbox" name="update" value="'.
                   1269:                    $num.'" />'.&mt('Update');
                   1270:     }
1.112     albertel 1271:     $output .= '</label></span><input type="hidden" name="scope_'.$num.                '" value="'.$scope.'" />';
1.108     raeburn  1272:     return $output;
                   1273: }
                   1274:                                                                                    
                   1275: sub dateboxes {
                   1276:     my ($num,$start,$end) = @_;
                   1277:     my $noend;
                   1278:     if ($end == 0) {
                   1279:         $noend = 'checked="checked"';
                   1280:     }
                   1281:     my $startdate = &Apache::lonhtmlcommon::date_setter('portform',
                   1282:                            'startdate_'.$num,$start,undef,undef,undef,1,undef,
                   1283:                             undef,undef,1);
                   1284:     my $enddate = &Apache::lonhtmlcommon::date_setter('portform',
                   1285:                                'enddate_'.$num,$end,undef,undef,undef,1,undef,
1.112     albertel 1286:                                 undef,undef,1). '&nbsp;&nbsp;<span style="white-space: nowrap"><label>'.
1.108     raeburn  1287:                                 '<input type="checkbox" name="noend_'.
                   1288:                                 $num.'" '.$noend.' />'.&mt('No end date').
1.112     albertel 1289:                                 '</label></span>';
1.108     raeburn  1290:                                                                                    
                   1291:     my $output = &mt('Start: ').$startdate.'<br />'.&mt('End: ').$enddate;
                   1292:     return $output;
                   1293: }
                   1294: 
                   1295: sub unpack_acc_key {
                   1296:     my ($acc_key) = @_;
                   1297:     my ($num,$scope,$end,$start) = ($acc_key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   1298:     return ($num,$scope,$end,$start);
                   1299: }
                   1300: 
                   1301: sub set_identifiers {
                   1302:     my ($status,$item,$now,$then,$scope) = @_;
                   1303:     if ($status eq 'old') {
                   1304:         return(&unpack_acc_key($item));
                   1305:     } else {
                   1306:         return($item,$scope,$then,$now);
                   1307:     }
                   1308: } 
                   1309: 
                   1310: sub role_selectors {
                   1311:     my ($num,$role_id,$status,$type,$content,$caller) = @_;
                   1312:     my ($output,$cdom,$cnum,$longid);
                   1313:     if ($caller eq 'display') {
                   1314:         $longid = '_'.$num.'_'.$role_id;
                   1315:         if ($status eq 'new') {
                   1316:             foreach my $item ('role','access','section','group') {
                   1317:                 $output .= '<td><select name="'.$item.$longid.'">'.
                   1318:                            '<option value="">'.&mt('Pick [_1] first',$type).
                   1319:                            '</option></select></td>';
                   1320:             }
                   1321:             return $output;
                   1322:         } else {
                   1323:             $cdom = $$content{'domain'};
                   1324:             $cnum = $$content{'number'};
                   1325:         }
                   1326:     } elsif ($caller eq 'rolepicker') {
                   1327:          $cdom = $env{'form.cdom'};
                   1328:          $cnum = $env{'form.cnum'};
                   1329:     }
1.120     raeburn  1330:     my $uctype = $type;
                   1331:     $uctype =~ s/^(\w)/uc($1)/e;
1.108     raeburn  1332:     my ($sections,$groups,$allroles,$rolehash,$accesshash) =
1.120     raeburn  1333:             &Apache::loncommon::get_secgrprole_info($cdom,$cnum,1,$uctype);
1.108     raeburn  1334:     if (!@{$sections}) {
                   1335:         @{$sections} = ('none');
                   1336:     } else {
                   1337:         unshift(@{$sections},('all','none'));
                   1338:     }
                   1339:     if (!@{$groups}) {
                   1340:         @{$groups} = ('none');
                   1341:     } else {
                   1342:         unshift(@{$groups},('all','none'));
                   1343:     }
                   1344:     my @allacesses = sort(keys(%{$accesshash}));
                   1345:     my (%sectionhash,%grouphash);
                   1346:     foreach my $sec (@{$sections}) {
                   1347:         $sectionhash{$sec} = $sec;
                   1348:     }
                   1349:     foreach my $grp (@{$groups}) {
                   1350:         $grouphash{$grp} = $grp;
                   1351:     }
                   1352:     my %lookup = (
                   1353:                    'role' => $rolehash,
                   1354:                    'access' => $accesshash,
                   1355:                    'section' => \%sectionhash,
                   1356:                    'group' => \%grouphash,
                   1357:                  );
                   1358:     my @allaccesses = sort(keys(%{$accesshash}));
                   1359:     my %allitems = (
                   1360:                     'role' => $allroles,
                   1361:                     'access' => \@allaccesses,
                   1362:                     'section' => $sections,
                   1363:                     'group' => $groups, 
                   1364:                    );
                   1365:     foreach my $item ('role','access','section','group') {
                   1366:         $output .= '<td><select name="'.$item.$longid.'" multiple="true" size="4">'."\n";
                   1367:         foreach my $entry (@{$allitems{$item}}) {
                   1368:             if ($caller eq 'display') {
                   1369:                 if ((@{$$content{'roles'}{$role_id}{$item}} > 0) && 
                   1370:                     (grep(/^\Q$entry\E$/,@{$$content{'roles'}{$role_id}{$item}}))) {
                   1371:                     $output .= '  <option value="'.$entry.'" selected>'.
                   1372:                                   $lookup{$item}{$entry}.'</option>';
                   1373:                     next;
                   1374:                 }
                   1375:             }
                   1376:             $output .= '  <option value="'.$entry.'">'.
                   1377:                        $lookup{$item}{$entry}.'</option>';
                   1378:         }
                   1379:         $output .= '</select>';
                   1380:     }
                   1381:     $output .= '</td>';
                   1382:     return $output;
                   1383: }
                   1384: 
                   1385: sub role_options_window {
                   1386:     my ($r) = @_;
                   1387:     my $cdom = $env{'form.cdom'};
                   1388:     my $cnum = $env{'form.cnum'};
                   1389:     my $type = $env{'form.type'};
                   1390:     my $addindex = $env{'form.setroles'};
1.120     raeburn  1391:     my $grouptitle = 'Groups';
                   1392:     if ($type eq 'Group') {
                   1393:          $grouptitle = 'Teams';
                   1394:     } 
1.108     raeburn  1395:     my $role_selects = &role_selectors(1,1,'new',$type,undef,'rolepicker');
                   1396:     $r->print(<<"END_SCRIPT");
                   1397: <script type="text/javascript">
                   1398: function setRoles() {
                   1399:     var addidx = $addindex+1;
                   1400:     for (var i=0; i<4; i++) {
                   1401:         var copylist = '';
                   1402:         for (var j=0; j<document.rolepicker.elements[i].length; j++) {
                   1403:             if (document.rolepicker.elements[i].options[j].selected) {
                   1404:                 copylist = copylist + document.rolepicker.elements[i].options[j].value + ',';
                   1405:             }
                   1406:         }
                   1407:         copylist = copylist.substr(0,copylist.length-1);
                   1408:         opener.document.portform.elements[addidx+i].value = copylist;
                   1409:     }
                   1410:     self.close();
                   1411: }
                   1412: </script>
                   1413: END_SCRIPT
                   1414:     $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  1415:     $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  1416:     return;
1.104     raeburn  1417: }
                   1418: 
1.47      banghart 1419: sub select_files {
1.138     albertel 1420:     my ($r) = @_;
1.82      albertel 1421:     if ($env{'form.continue'} eq 'true') {
1.60      banghart 1422:         # here we update the selections for the currentpath
                   1423:         # eventually, have to handle removing those not checked, but . . . 
1.83      banghart 1424:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
                   1425:         if (scalar(@items)){
1.85      banghart 1426:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
1.83      banghart 1427:         }
1.62      banghart 1428:     } else {
                   1429:             #empty the file for a fresh start
1.83      banghart 1430:             &Apache::lonnet::clear_selected_files($env{'user.name'});
1.62      banghart 1431:     }
1.82      albertel 1432:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
1.62      banghart 1433:     my $java_files = join ",", @files;
                   1434:     if ($java_files) {
                   1435:         $java_files.=',';
1.60      banghart 1436:     }
1.63      banghart 1437:     my $javascript =(<<ENDSMP);
1.113     albertel 1438:         <script type="text/javascript">
1.48      banghart 1439:         function finishSelect() {
1.62      banghart 1440: ENDSMP
1.63      banghart 1441:     $javascript .= 'fileList = "'.$java_files.'";';
                   1442:     $javascript .= (<<ENDSMP);
1.49      banghart 1443:             for (i=0;i<document.forms.checkselect.length;i++) { 
                   1444:                 if (document.forms.checkselect[i].checked){
1.54      banghart 1445:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
1.49      banghart 1446:                 }
                   1447:             }
1.128     albertel 1448:             opener.document.forms.lonhomework.$env{'form.fieldname'}.value=fileList;
1.48      banghart 1449:             self.close();
                   1450:         }
                   1451:         </script>
                   1452: ENDSMP
1.63      banghart 1453:     $r->print($javascript);
1.47      banghart 1454:     $r->print("<h1>Select portfolio files</h1>
1.88      albertel 1455:                 Check as many as you wish in response to the problem.<br />");
                   1456:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
                   1457:     if (@otherfiles) {
                   1458: 	$r->print("<strong>Files selected from other directories:</strong><br />");
                   1459: 	foreach my $file (@otherfiles) {
                   1460: 	    $r->print($file."<br />");
                   1461: 	}
1.60      banghart 1462:     }
1.47      banghart 1463: }
1.138     albertel 1464: 
1.24      albertel 1465: sub upload {
1.137     albertel 1466:     my ($r,$url,$group)=@_;
1.82      albertel 1467:     my $fname=$env{'form.uploaddoc.filename'};
                   1468:     my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
1.38      banghart 1469:     my $disk_quota = 20000; # expressed in k
1.126     raeburn  1470:     if (defined($group)) {
                   1471:         my $grp_quota = &get_group_quota($group); # quota expressed in k 
                   1472:         if ($grp_quota ne '') {
                   1473:             $disk_quota = $grp_quota;
                   1474:         } else {
                   1475:             $disk_quota = 0;
                   1476:         }
                   1477:     }
1.34      banghart 1478:     $fname=&Apache::lonnet::clean_filename($fname);
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.144     albertel 1598:     my ($udom,$uname) = @_;
                   1599:     if (!defined($uname) || !defined($udom)) {
                   1600: 	($uname,$udom) = &get_name_dom();
                   1601:     }
1.137     albertel 1602:     my $path;
1.138     albertel 1603:     if (defined($env{'form.group'})) {
                   1604:         $path = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
1.94      raeburn  1605:     } else {
                   1606:         $path = '/userfiles/portfolio';
                   1607:     }
                   1608:     return (&Apache::loncommon::propath($udom,$uname).$path);
                   1609: }
                   1610: 
1.126     raeburn  1611: sub get_group_quota {
                   1612:     my ($group) = @_;
                   1613:     my $group_quota; 
                   1614:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1615:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1616:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
                   1617:     if (%curr_groups) {
                   1618:         my %group_info =  &Apache::longroup::get_group_settings(
                   1619:                                                     $curr_groups{$group});
                   1620:         $group_quota = $group_info{'quota'}; #expressed in Mb
                   1621:         if ($group_quota) {
                   1622:             $group_quota = 1000 * $group_quota; #expressed in k
                   1623:         } 
                   1624:     }
                   1625:     return $group_quota;
                   1626: } 
                   1627: 
1.94      raeburn  1628: sub get_dir_list {
1.138     albertel 1629:     my ($portfolio_root) = @_;
                   1630:     my ($uname,$udom) = &get_name_dom();
1.94      raeburn  1631:     return &Apache::lonnet::dirlist($env{'form.currentpath'},
                   1632:                                           $udom,$uname,$portfolio_root);
                   1633: }
                   1634: 
                   1635: sub get_name_dom {
                   1636:     my ($uname,$udom);
1.138     albertel 1637:     if (defined($env{'form.group'})) {
1.94      raeburn  1638:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1639:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1640:     } else {
                   1641:         $udom = $env{'user.domain'};
                   1642:         $uname = $env{'user.name'};
                   1643:     }
                   1644:     return ($uname,$udom);
                   1645: }
                   1646: 
1.102     raeburn  1647: sub prepend_group {
1.138     albertel 1648:     my ($filename) = @_;
                   1649:     if (defined($env{'form.group'})) {
                   1650:         $filename = $env{'form.group'}.$filename;
1.102     raeburn  1651:     }
                   1652:     return $filename;
                   1653: }
                   1654: 
1.94      raeburn  1655: sub get_namespace {
                   1656:     my $namespace = 'portfolio';
1.138     albertel 1657:     if (defined($env{'form.group'})) {
                   1658:         my ($uname,$udom) = &get_name_dom();
                   1659:         $namespace .= '_'.$udom.'_'.$uname.'_'.$env{'form.group'};
1.94      raeburn  1660:     }
                   1661:     return $namespace;
                   1662: }
                   1663: 
                   1664: sub get_port_path {
                   1665:     my $port_path;
1.138     albertel 1666:     if (defined($env{'form.group'})) {
                   1667:        $port_path = "groups/$env{'form.group'}/portfolio";
1.94      raeburn  1668:     } else {
                   1669:        $port_path = 'portfolio';
                   1670:     }
                   1671:     return $port_path;
1.24      albertel 1672: }
                   1673: 
1.120     raeburn  1674: sub missing_priv {
1.138     albertel 1675:     my ($r,$url,$priv) = @_;
1.120     raeburn  1676:     my $longtext = {
                   1677:                       upload => 'upload files',
                   1678:                       delete => 'delete files',
                   1679:                       rename => 'rename files',
                   1680:                       setacl => 'set access controls for files',
                   1681:                    };
                   1682:     my $escpath = &HTML::Entities::encode($env{'form.currentpath'},'&<>"');
                   1683:     my $rtnlink = '<a href="'.$url;
                   1684:     if ($url =~ /\?/) {
                   1685:         $rtnlink .= '&';
                   1686:     } else {
                   1687:         $rtnlink .= '?';
                   1688:     }
                   1689:     $rtnlink .= 'currentpath='.$escpath;
                   1690:     $r->print(&mt('<h3>Action disallowed</h3>'));
                   1691:     $r->print(&mt('You do not have sufficient privileges to [_1] ',
                   1692:                   $longtext->{$priv}));
1.138     albertel 1693:     if (defined($env{'form.group'})) {
1.120     raeburn  1694:         $r->print(&mt("in the group's file repository."));
1.137     albertel 1695:         $rtnlink .= &group_args()
1.120     raeburn  1696:     } else {
                   1697:         $r->print(&mt('in this portfolio.'));
                   1698:     }
                   1699:     $rtnlink .= '">'.&mt('Return to directory listing page').'</a>';
                   1700:     $r->print('<br />'.$rtnlink);
                   1701:     $r->print(&Apache::loncommon::end_page());
                   1702:     return;
                   1703: }
                   1704: 
1.132     raeburn  1705: sub coursegrp_portfolio_header {
1.137     albertel 1706:     my ($cdom,$cnum,$grp_desc)=@_;
1.132     raeburn  1707:     my $gpterm  = &Apache::loncommon::group_term();
                   1708:     my $ucgpterm = $gpterm;
                   1709:     $ucgpterm =~ s/^(\w)/uc($1)/e;
1.137     albertel 1710:     if ($env{'form.ref'}) {
1.136     raeburn  1711:         &Apache::lonhtmlcommon::add_breadcrumb
                   1712:             ({href=>"/adm/coursegroups",
                   1713:               text=>"Groups",
                   1714:               title=>"Course Groups"});
                   1715:     }
1.132     raeburn  1716:     &Apache::lonhtmlcommon::add_breadcrumb
1.138     albertel 1717:         ({href=>"/adm/$cdom/$cnum/$env{'form.group'}/smppg?ref=$env{'form.ref'}",
1.132     raeburn  1718:           text=>"$ucgpterm: $grp_desc",
                   1719:           title=>"Go to group's home page"},
1.137     albertel 1720:          {href=>"/adm/coursegrp_portfolio?".&group_args(),
1.132     raeburn  1721:           text=>"Group Portfolio",
1.136     raeburn  1722:           title=>"Display group portfolio"});
1.132     raeburn  1723:     my $output = &Apache::lonhtmlcommon::breadcrumbs(
                   1724:                          &mt('[_1] portfolio files - [_2]',$gpterm,$grp_desc));
                   1725:     return $output;
                   1726: }
                   1727: 
                   1728: 
1.24      albertel 1729: sub handler {
                   1730:     # this handles file management
                   1731:     my $r = shift;
1.73      banghart 1732:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.108     raeburn  1733:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
                   1734: 	  'fieldname','mode','rename','continue','group','access','setnum',
1.136     raeburn  1735:           'cnum','cdom','type','setroles','showversions','ref']);
1.138     albertel 1736:     my ($uname,$udom,$portfolio_root,$url,$caller,$title,$group,$grp_desc);
1.94      raeburn  1737:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
                   1738:         $url = $1.$2;
                   1739:         $caller = $2;
                   1740:     }
1.137     albertel 1741:     my ($can_modify,$can_delete,$can_upload,$can_setacl);
1.94      raeburn  1742:     if ($caller eq 'coursegrp_portfolio') {
                   1743:     #  Needs to be in a course
                   1744:         if (! ($env{'request.course.fn'})) {
                   1745:         # Not in a course
                   1746:             $env{'user.error.msg'}=
                   1747:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
                   1748:             return HTTP_NOT_ACCEPTABLE;
                   1749:         }
                   1750:         my $earlyout = 0;
1.136     raeburn  1751:         my $view_permission = 
                   1752:            &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
1.137     albertel 1753:         $env{'form.group'} =~ s/\W//g;
1.138     albertel 1754: 	$group = $env{'form.group'};
1.94      raeburn  1755:         if ($group) {
1.138     albertel 1756:             ($uname,$udom) = &get_name_dom();
1.99      raeburn  1757:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
1.98      albertel 1758: 							       $group); 
                   1759:             if (%curr_groups) {
1.132     raeburn  1760:                 my %grp_content = &Apache::longroup::get_group_settings(
                   1761:                                                          $curr_groups{$group});
                   1762:                 $grp_desc = &unescape($grp_content{'description'});
1.94      raeburn  1763:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
                   1764:                                       $env{'request.course.id'}.'/'.$group))) {
1.138     albertel 1765:                     $portfolio_root = &get_portfolio_root();
1.94      raeburn  1766:                 } else {
1.116     albertel 1767:                     $r->print('You do not have the privileges required to access the shared files space for this group.');
1.94      raeburn  1768:                     $earlyout = 1;
                   1769:                 }
                   1770:             } else {
                   1771:                 $r->print('Not a valid group for this course');
                   1772:                 $earlyout = 1;
                   1773:             }
                   1774:             $title = &mt('Group files').' for '.$group; 
                   1775:         } else {
                   1776:             $r->print('Invalid group');
                   1777:             $earlyout = 1;
                   1778:         }
                   1779:         if ($earlyout) { return OK; }
1.126     raeburn  1780:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
1.120     raeburn  1781:             $can_modify = 1;
                   1782:             $can_delete = 1;
1.126     raeburn  1783:             $can_upload = 1;
                   1784:             $can_setacl = 1;
                   1785:         } else {
                   1786:             if (&Apache::lonnet::allowed('agf',$env{'request.course.id'}.'/'.$group)) {
                   1787:                 $can_setacl = 1;
                   1788:             }
                   1789:             if (&Apache::lonnet::allowed('ugf',$env{'request.course.id'}.'/'.$group)) {
                   1790:                 $can_upload = 1;
                   1791:             }
                   1792:             if (&Apache::lonnet::allowed('mgf',$env{'request.course.id'}.'/'.$group)) {
                   1793:                 $can_modify = 1;
                   1794:             }
                   1795:             if (&Apache::lonnet::allowed('dgf',$env{'request.course.id'}.'/'.$group)) {
                   1796:                 $can_delete = 1;
                   1797:             }
1.120     raeburn  1798:         }
1.94      raeburn  1799:     } else {
                   1800:         ($uname,$udom) = &get_name_dom();
                   1801:         $portfolio_root = &get_portfolio_root();
                   1802:         $title = &mt('Portfolio Manager');
1.120     raeburn  1803:         $can_modify = 1;
                   1804:         $can_delete = 1;
                   1805:         $can_upload = 1;
                   1806:         $can_setacl = 1;
1.94      raeburn  1807:     }
                   1808: 
1.138     albertel 1809:     my $port_path = &get_port_path();
1.24      albertel 1810:     &Apache::loncommon::no_cache($r);
                   1811:     &Apache::loncommon::content_type($r,'text/html');
                   1812:     $r->send_http_header;
                   1813:     # Give the LON-CAPA page header
1.82      albertel 1814:     if ($env{"form.mode"} eq 'selectfile'){
1.96      albertel 1815:         $r->print(&Apache::loncommon::start_page($title,undef,
1.97      albertel 1816: 						 {'only_body' => 1}));
1.108     raeburn  1817:     } elsif ($env{'form.action'} eq 'rolepicker') {
                   1818:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
                   1819:                                                  {'no_nav_bar'  => 1, }));
1.74      banghart 1820:     } else {
1.97      albertel 1821:         $r->print(&Apache::loncommon::start_page($title));
1.74      banghart 1822:     }
1.24      albertel 1823:     $r->rflush();
1.88      albertel 1824: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
1.116     albertel 1825:    	    $r->print('<span class="LC_error">'.
                   1826: 		      'No file was selected to upload.'.
                   1827: 		      'To upload a file, click <strong>Browse...</strong>'.
                   1828: 		      ', select a file, then click <strong>Upload</strong>.'.
                   1829: 		      '</span>');
1.40      banghart 1830: 	}
1.82      albertel 1831:     if ($env{'form.meta'}) {
1.94      raeburn  1832:         &open_form($r,$url);
1.82      albertel 1833: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
1.70      banghart 1834:         $r->print('Edit the meta data<br />');
1.137     albertel 1835:         &close_form($r,$url);
1.70      banghart 1836:     }
1.82      albertel 1837:     if ($env{'form.store'}) {
1.70      banghart 1838:     }
                   1839: 
1.82      albertel 1840:     if ($env{'form.uploaddoc.filename'}) {
1.120     raeburn  1841:         if ($can_upload) {
1.137     albertel 1842: 	    &upload($r,$url,$group);
1.120     raeburn  1843:         } else {
1.138     albertel 1844:             &missing_priv($r,$url,'upload');
1.120     raeburn  1845:         }
1.82      albertel 1846:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
1.120     raeburn  1847:         if ($can_delete) {
1.138     albertel 1848: 	    &delete_confirmed($r,$url);
1.120     raeburn  1849:         } else {
1.138     albertel 1850:             &missing_priv($r,$url,'delete');
1.120     raeburn  1851:         }
1.82      albertel 1852:     } elsif ($env{'form.action'} eq 'delete') {
1.120     raeburn  1853:         if ($can_delete) {
1.138     albertel 1854: 	    &delete($r,$url);
1.120     raeburn  1855:         } else {
1.138     albertel 1856:             &missing_priv($r,$url,'delete');
1.120     raeburn  1857:         }
1.82      albertel 1858:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
1.120     raeburn  1859:         if ($can_delete) {
1.138     albertel 1860: 	    &delete_dir_confirmed($r,$url);
1.120     raeburn  1861:         } else {
1.138     albertel 1862:             &missing_priv($r,$url,'delete');
1.120     raeburn  1863:         }
                   1864:     } elsif ($env{'form.action'} eq 'deletedir') {
                   1865:         if ($can_delete) {
1.137     albertel 1866: 	    &delete_dir($r,$url);
1.120     raeburn  1867:         } else {
1.138     albertel 1868:             &missing_priv($r,$url,'delete');
1.120     raeburn  1869:         }
1.82      albertel 1870:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
1.120     raeburn  1871:         if ($can_modify) {
1.138     albertel 1872: 	    &rename_confirmed($r,$url);
1.120     raeburn  1873:         } else {
1.138     albertel 1874:             &missing_priv($r,$url,'rename');
1.120     raeburn  1875:         }
1.82      albertel 1876:     } elsif ($env{'form.rename'}) {
                   1877:         $env{'form.selectfile'} = $env{'form.rename'};
                   1878:         $env{'form.action'} = 'rename';
1.120     raeburn  1879:         if ($can_modify) {
1.138     albertel 1880: 	    &rename($r,$url);
1.120     raeburn  1881:         } else {
1.138     albertel 1882:             &missing_priv($r,$url,'rename');
1.120     raeburn  1883:         }
1.104     raeburn  1884:     } elsif ($env{'form.access'}) {
                   1885:         $env{'form.selectfile'} = $env{'form.access'};
                   1886:         $env{'form.action'} = 'chgaccess';
1.137     albertel 1887:         &display_access($r,$url,$group,$can_setacl,$port_path);
1.104     raeburn  1888:     } elsif ($env{'form.action'} eq 'chgaccess') {
1.120     raeburn  1889:         if ($can_setacl) {
1.137     albertel 1890:             &update_access($r,$url,$group,$port_path);
1.120     raeburn  1891:         } else {
1.138     albertel 1892:             &missing_priv($r,$url,'setacl');
1.120     raeburn  1893:         }
1.108     raeburn  1894:     } elsif ($env{'form.action'} eq 'rolepicker') {
1.120     raeburn  1895:         if ($can_setacl) { 
                   1896:             &role_options_window($r);
                   1897:         } else {
1.138     albertel 1898:             &missing_priv($r,$url,'setacl');
1.120     raeburn  1899:         }
1.82      albertel 1900:     } elsif ($env{'form.createdir'}) {
1.120     raeburn  1901:         if ($can_upload) {
1.138     albertel 1902: 	    &createdir($r,$url);
1.120     raeburn  1903:         } else {
1.138     albertel 1904:             &missing_priv($r,$url,'upload');
1.120     raeburn  1905:         }
1.82      albertel 1906:     } elsif ($env{'form.lockinfo'}) {
1.137     albertel 1907:         &lock_info($r,$url,$group);
1.24      albertel 1908:     } else {
                   1909: 	my $current_path='/';
1.82      albertel 1910: 	if ($env{'form.currentpath'}) {
                   1911: 	    $current_path = $env{'form.currentpath'};
1.24      albertel 1912: 	}
1.132     raeburn  1913:         if ($caller eq 'coursegrp_portfolio') {
                   1914:             &Apache::lonhtmlcommon::clear_breadcrumbs();
1.137     albertel 1915:             $r->print(&coursegrp_portfolio_header($udom,$uname,$grp_desc));
1.132     raeburn  1916:         }
1.138     albertel 1917:         my @dir_list=&get_dir_list($portfolio_root);
1.46      albertel 1918: 	if ($dir_list[0] eq 'no_such_dir'){
                   1919: 	    # two main reasons for this:
                   1920:             #    1) never been here, so directory structure not created
                   1921: 	    #    2) back-button navigation after deleting a directory
                   1922: 	    if ($current_path eq '/'){
1.100     albertel 1923: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
1.138     albertel 1924: 					       &get_port_path());
1.46      albertel 1925: 	    } else {
                   1926:                 # some directory that snuck in get rid of the directory
                   1927:                 # from the recent pulldown, just in case
                   1928: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
                   1929: 						      [$current_path]);
                   1930: 		$current_path = '/'; # force it back to the root        
                   1931: 	    }
                   1932: 	    # now grab the directory list again, for the first time
                   1933: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
1.94      raeburn  1934: 					    $udom,$uname,$portfolio_root);
1.43      banghart 1935:         }
1.46      albertel 1936: 	# need to know if directory is empty so it can be removed if desired
                   1937: 	my $is_empty=(@dir_list == 2);
1.137     albertel 1938: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,
                   1939: 			$can_upload);
1.138     albertel 1940:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group,
1.137     albertel 1941:                            $can_upload,$can_modify,$can_delete,$can_setacl);
1.95      albertel 1942: 	$r->print(&Apache::loncommon::end_page());
1.30      banghart 1943:     }
1.90      albertel 1944:     return OK;
1.2       banghart 1945: }
1.120     raeburn  1946: 
1.1       banghart 1947: 1;
                   1948: __END__

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