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

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

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