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

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

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