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

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

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