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

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

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