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

1.125     albertel    1: # The LearningOnline Network
                      2: # portfolio browser
                      3: #
1.155   ! banghart    4: # $Id: portfolio.pm,v 1.154 2006/08/12 06:18:44 banghart Exp $
1.125     albertel    5: #
1.3       banghart    6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or 
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
1.1       banghart   29: package Apache::portfolio;
                     30: use strict;
                     31: use Apache::Constants qw(:common :http);
1.2       banghart   32: use Apache::loncommon;
1.1       banghart   33: use Apache::lonnet;
1.2       banghart   34: use Apache::lontexconvert;
                     35: use Apache::lonfeedback;
                     36: use Apache::lonlocal;
1.82      albertel   37: use Apache::lonnet;
1.99      raeburn    38: use Apache::longroup;
1.132     raeburn    39: use Apache::lonhtmlcommon;
1.113     albertel   40: use HTML::Entities;
1.101     www        41: use LONCAPA;
1.16      banghart   42: 
1.137     albertel   43: sub group_args {
                     44:     my $output;
                     45:     if (defined($env{'form.group'})) {
1.138     albertel   46:         $output .= '&group='.$env{'form.group'};
1.137     albertel   47: 	if (defined($env{'form.ref'})) {
                     48: 	    $output .= '&ref='.$env{'form.ref'};
                     49: 	}
                     50:     }
                     51:     return $output;
                     52: }
                     53: 
                     54: sub group_form_data {
                     55:     my $output;
                     56:     if (defined($env{'form.group'})) {
1.138     albertel   57: 	$output = '<input type="hidden" name="group" value="'.$env{'form.group'}.'" />';
1.137     albertel   58: 	if (exists($env{'form.ref'})) {
                     59: 	    $output .= '<input type="hidden" name="ref" value="'.
                     60: 		$env{'form.ref'}.'" />';
                     61: 	}
                     62:     }
1.139     albertel   63:     return $output;
1.137     albertel   64: } 
                     65: 
1.16      banghart   66: # receives a file name and path stub from username/userfiles/portfolio/
                     67: # returns an anchor tag consisting encoding filename and currentpath
1.23      albertel   68: sub make_anchor {
1.149     banghart   69:     my ($url, $anchor_fields, $inner_text) = @_;
                     70:     if ($$anchor_fields{'continue'} ne 'true') {$$anchor_fields{'continue'} = 'false'};
                     71:     my $anchor = '<a href="'.$url.'?';
                     72:     foreach my $field_name (keys(%$anchor_fields)) {
                     73:         $anchor .= $field_name.'='.$$anchor_fields{$field_name}.'&amp;';
                     74:     }
1.151     banghart   75:     $anchor =~ s/&amp;$//;
1.138     albertel   76:     $anchor .= &group_args();
1.149     banghart   77:     $anchor .= '">'.$inner_text.'</a>';
1.8       albertel   78:     return $anchor;
1.6       banghart   79: }
1.138     albertel   80: 
1.24      albertel   81: my $dirptr=16384;
1.48      banghart   82: sub display_common {
1.137     albertel   83:     my ($r,$url,$current_path,$is_empty,$dir_list,$can_upload)=@_;
1.138     albertel   84:     my $namespace = &get_namespace();
                     85:     my $port_path = &get_port_path();
1.120     raeburn    86:     if ($can_upload) {
1.137     albertel   87:         my $groupitem = &group_form_data();
                     88: 
1.120     raeburn    89:         my $iconpath= $r->dir_config('lonIconsURL') . "/";
                     90:         my %text=&Apache::lonlocal::texthash(
                     91: 					 'upload' => 'Upload',
1.88      albertel   92: 					 'upload_label' =>  
                     93: 					 'Upload file to current directory:',
                     94: 					 'createdir' => 'Create Subdirectory',
                     95: 					 'createdir_label' => 
                     96: 					 'Create subdirectory in current directory:');
1.120     raeburn    97:         my $escuri = &HTML::Entities::encode($r->uri,'&<>"');
                     98:         $r->print(<<"TABLE"); 
1.114     albertel   99: <table id="LC_portfolio_actions">
                    100:   <tr id="LC_portfolio_upload">
                    101:     <td class="LC_label">
1.113     albertel  102:       $text{'upload_label'}
                    103:     </td>
1.120     raeburn   104:     <td class="LC_value">
1.113     albertel  105:       <form method="post" enctype="multipart/form-data" action="$escuri">
1.120     raeburn   106:         $groupitem 
1.88      albertel  107:         <input name="uploaddoc" type="file" />
                    108: 	<input type="hidden" name="currentpath" value="$current_path" />
                    109: 	<input type="hidden" name="action" value="$env{"form.action"}" />
                    110: 	<input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
                    111: 	<input type="hidden" name="mode" value="$env{"form.mode"}" />
                    112: 	<input type="submit" name="storeupl" value="$text{'upload'}" />
1.113     albertel  113:       </form>
                    114:     </td>
                    115:   </tr>
1.114     albertel  116:   <tr id="LC_portfolio_createdir">
                    117:     <td class="LC_label">
1.113     albertel  118:       $text{'createdir_label'}
                    119:     </td>
1.114     albertel  120:     <td class="LC_value">
1.113     albertel  121:       <form method="post" action="$escuri">
1.94      raeburn   122:         <input name="newdir" type="input" />$groupitem
1.88      albertel  123:         <input type="hidden" name="currentpath" value="$current_path" />
                    124:         <input type="hidden" name="action" value="$env{"form.action"}" />
                    125:         <input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
                    126:         <input type="hidden" name="mode" value="$env{"form.mode"}" />
                    127:         <input type="submit" name="createdir" value="$text{'createdir'}" />
1.113     albertel  128:       </form>
                    129:     </td>
                    130:   </tr>
1.88      albertel  131: </table>
                    132: TABLE
1.120     raeburn   133:     }
1.24      albertel  134:     my @tree = split (/\//,$current_path);
1.150     banghart  135:     my %anchor_fields = (
1.149     banghart  136:         'selectfile'    => $port_path,
                    137:         'currentpath'   => '/',
                    138:         'mode'          => $env{"form.mode"},
                    139:         'fieldname'     => $env{"form.fieldname"},
                    140:         'continue'      => $env{"form.continue"}
                    141:     );
                    142:     $r->print('<span class="LC_current_location">'.&make_anchor($url,\%anchor_fields,$port_path).'/');
1.19      banghart  143:     if (@tree > 1){
                    144:         my $newCurrentPath = '';
                    145:         for (my $i = 1; $i< @tree; $i++){
                    146:             $newCurrentPath .= $tree[$i].'/';
1.150     banghart  147:             my %anchor_fields = (
1.149     banghart  148:                 'selectfile' => $tree[$i],
                    149:                 'currentpath' => '/',
                    150:                 'mode' => $env{"form.mode"},
                    151:                 'fieldname' => $env{"form.fieldname"},
                    152:                 'continue' => $env{"form.continue"}
                    153:             );
                    154:             $r->print(&make_anchor($url,\%anchor_fields,$tree[$i]).'/');
1.19      banghart  155:         }
                    156:     }
1.117     albertel  157:     $r->print('</span>');
1.94      raeburn   158:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
1.137     albertel  159:     $r->print('<br /><form method="post" action="'.$url.'?mode='.$env{"form.mode"}.'&amp;fieldname='.$env{"form.fieldname"}.&group_args());
1.94      raeburn   160:     $r->print('">'.
                    161: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
1.22      albertel  162: 						    'this.form.submit();'));
1.21      banghart  163:     $r->print("</form>");
1.48      banghart  164: }
1.137     albertel  165: 
1.134     banghart  166: sub display_directory_line {
1.137     albertel  167:     my ($r,$select_mode,$fullpath, $filename, $mtime, $size, $css_class,
                    168: 	$line, $access_controls, $curr_access, $now, $version_flag,
1.140     banghart  169: 	$href_location, $url, $current_path, $access_admin_text, $versions)=@_;
1.135     banghart  170:     # my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
1.137     albertel  171:     $r->print('<tr class="'.$css_class.'">');
1.153     banghart  172:     $r->print($line); # contains first two cells of table
1.155   ! banghart  173:     $filename = $filename;
        !           174:     $filename = &prepend_group($filename);
        !           175:     my $lock_info;
1.153     banghart  176:     if ($$version_flag{$filename}) { # versioned can't be versioned, so TRUE when root file
                    177:         $r->print('<td><img alt="" src="'.&Apache::loncommon::icon($filename).'" /></td>');
                    178:         $r->print('<td>'.$$version_flag{$filename}.'</td>');
                    179:     } else { # this is a graded or handed back file
1.154     banghart  180:         my ($user,$domain) = &get_name_dom();
                    181:         my $permissions_hash = &Apache::lonnet::get_portfile_permissions($domain,$user);
1.155   ! banghart  182:         if (defined($filename) && defined($$permissions_hash{'/'.$filename})) {
        !           183:             foreach my $array_item (@{$$permissions_hash{'/'.$filename}}) {
        !           184:                 if (ref($array_item) eq 'ARRAY') {
        !           185:                     if ($$array_item[(@$array_item - 1)] eq 'handback') {
        !           186:                         $lock_info = 'Handback';
        !           187:                     } elsif ($$array_item[(@$array_item - 1)] eq 'graded') {
        !           188:                         $lock_info = 'Graded';
1.154     banghart  189:                     }
1.155   ! banghart  190:                  }
1.154     banghart  191:             }
                    192:         }
1.155   ! banghart  193:             if ($lock_info) {
        !           194:                 my %anchor_fields = (
        !           195:                     'lockinfo' => '/'.$filename
        !           196:                 );
        !           197:                 $lock_info = &make_anchor(undef,\%anchor_fields,$lock_info);
        !           198:             }
        !           199:             $r->print('<td colspan="2">'.$lock_info.'</td>');
1.153     banghart  200:     }
                    201:     # $r->print('<td>'.$$version_flag{$filename}.'</td><td>');
                    202:     $r->print('<td>'.&make_anchor($href_location.$filename,undef,$filename).'</td>'); 
1.137     albertel  203:     $r->print('<td>'.$size.'</td>');
                    204:     $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
                    205:     if ($select_mode ne 'true') {
                    206: 	$r->print('<td><span style="white-space: nowrap">'.
                    207: 		  &mt($curr_access).'&nbsp;&nbsp;&nbsp;');
1.152     albertel  208:         my %anchor_fields = (
1.150     banghart  209:             'access' => $filename,
                    210:             'currentpath' => $current_path
                    211:         );
                    212: 	$r->print(&make_anchor($url, \%anchor_fields, $access_admin_text).'</span></td>');
1.137     albertel  213:     }
                    214:     $r->print('</tr>'.$/);
1.134     banghart  215: }
1.137     albertel  216: 
1.48      banghart  217: sub display_directory {
1.138     albertel  218:     my ($r,$url,$current_path,$is_empty,$dir_list,$group,$can_upload,
1.137     albertel  219:         $can_modify,$can_delete,$can_setacl)=@_;
1.48      banghart  220:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
                    221:     my $display_out;
1.77      banghart  222:     my $select_mode;
                    223:     my $checked_files;
1.138     albertel  224:     my $port_path = &get_port_path();
                    225:     my ($uname,$udom) = &get_name_dom();
1.120     raeburn   226:     my $access_admin_text = &mt('View Status');
                    227:     if ($can_setacl) {
                    228:         $access_admin_text = &mt('View/Change Status');
                    229:     }
                    230: 
1.102     raeburn   231:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    232:                                                                         $uname);
                    233:     my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash(
                    234:                                                   $current_permissions,$group);
                    235:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group);
                    236:     my $now = time;
1.82      albertel  237:     if ($env{"form.mode"} eq 'selectfile'){
1.77      banghart  238: 	&select_files($r);
1.94      raeburn   239: 	$checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
1.77      banghart  240: 	$select_mode = 'true';
                    241:     } 
1.120     raeburn   242:     if ($is_empty && ($current_path ne '/') && $can_delete) {
1.137     albertel  243:         $display_out = '<form method="post" action="'.$url.'">'.
                    244: 	    &group_form_data().
1.30      banghart  245:         '<input type="hidden" name="action" value="deletedir" />'.
                    246:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
                    247:         '<input type="hidden" name="selectfile" value="" />'.
                    248:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
                    249:         '</form>';
                    250:         
1.48      banghart  251:         $r->print($display_out);
1.31      albertel  252: 	return;
                    253:     }
1.77      banghart  254:     if ($select_mode eq 'true') {
1.113     albertel  255:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
1.119     albertel  256:         $r->print('<table id="LC_browser">'.
1.146     banghart  257:             '<tr><th>Select</th><th>&nbsp;</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
1.77      banghart  258:     } else {
1.113     albertel  259:         $r->print('<form method="post" action="'.$url.'">');
1.119     albertel  260:         $r->print('<table id="LC_browser">'.
1.146     banghart  261:             '<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   262:     }
1.137     albertel  263:     $r->print("\n".&group_form_data()."\n");
                    264: 
1.94      raeburn   265:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
                    266:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
1.105     banghart  267:     my @dir_lines;
1.131     banghart  268:     my @version_lines;
1.105     banghart  269:     my %versioned;
1.140     banghart  270:     foreach my $dir_line (sort 
1.26      albertel  271: 		      { 
                    272: 			  my ($afile)=split('&',$a,2);
                    273: 			  my ($bfile)=split('&',$b,2);
                    274: 			  return (lc($afile) cmp lc($bfile));
                    275: 		      } (@$dir_list)) {
1.18      banghart  276:     	#$strip holds directory/file name
                    277:     	#$dom 
1.140     banghart  278:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$dir_line,16); 
1.77      banghart  279:     	$filename =~ s/\s+$//;
1.105     banghart  280:     	my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
                    281:     	if ($version) {
1.131     banghart  282:     	    push(@version_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
1.129     banghart  283:     	    $versioned{$fname.'.'.$extension} .= $version.",";
1.131     banghart  284:     	} else {
                    285:     	    push(@dir_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
1.105     banghart  286:     	}
                    287:     }
1.140     banghart  288:     foreach my $dir_line (@dir_lines) {
                    289:         my ($filename,$dom,$testdir,$size,$mtime,$obs) = @$dir_line;
1.105     banghart  290:         my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
1.93      albertel  291:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
1.130     banghart  292:     	    my %version_flag;
1.131     banghart  293:     	    my $show_versions;
                    294:     	    if ($env{'form.showversions'} eq $filename) {
                    295:     	        $show_versions = 'true';
                    296:     	    }
1.129     banghart  297:     	    if (exists($versioned{$filename})) {
1.152     albertel  298:     	        my %anchor_fields = (
1.150     banghart  299:     	            'selectfile' => $filename,
                    300:     	            'continue' => 'false',
                    301:     	            'currentpath' => $current_path,
                    302:     	        );
1.134     banghart  303:     	        if ($show_versions) {
1.150     banghart  304:                     $version_flag{$filename} = &make_anchor('portfolio',\%anchor_fields,
                    305:                         '<img class="LC_icon" alt="'.&mt('opened folder').'" src="'.$iconpath.'folder_pointer_opened.gif" />');
1.134     banghart  306:     	        } else {
1.150     banghart  307:     	            $anchor_fields{'showversions'} = $filename;
                    308:                     $version_flag{$filename} = &make_anchor('portfolio',\%anchor_fields,
                    309:                         '<img class="LC_icon" alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_pointer_closed.gif" />');
1.134     banghart  310:                 }
1.129     banghart  311:     	    } else {
1.146     banghart  312:     	        $version_flag{$filename} = '&nbsp;';
1.129     banghart  313:     	    }
1.23      albertel  314:             if ($dirptr&$testdir) {
1.116     albertel  315: 		my $colspan='colspan="2"';
1.77      banghart  316:                 if ($select_mode eq 'true'){
1.116     albertel  317: 		    undef($colspan);
1.64      banghart  318:                 }
1.116     albertel  319: 		$r->print('<tr class="LC_browser_folder"><td '.$colspan.'><img alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_closed.gif" /></td>');
1.47      banghart  320:                 $r->print('<td>Go to ...</td>');
1.152     albertel  321:                 my %anchor_fields = (
1.149     banghart  322:                     'selectfile'    => $filename.'/',
                    323:                     'currentpath'   => $current_path.$filename.'/',
                    324:                     'mode'          => $env{"form.mode"},
                    325:                     'fieldname'     => $env{"form.fieldname"},
                    326:                     'continue'      => $env{"form.continue"}
                    327:                 );  
                    328:                 $r->print('<td>'.$version_flag{$filename}.'</td><td>'.&make_anchor($url,\%anchor_fields,$filename.'/').'</td>'); 
1.47      banghart  329:                 $r->print('</tr>'); 
                    330:             } else {
1.116     albertel  331: 		my $css_class = 'LC_browser_file';
                    332: 		my $line;
1.129     banghart  333:                 my $fullpath = $current_path.$filename;
1.138     albertel  334:                 $fullpath = &prepend_group($fullpath);
1.124     albertel  335:                 if ($select_mode eq 'true') {
1.116     albertel  336:                     $line='<td><input type="checkbox" name="checkfile" value="'.$filename.'"';
                    337: 		    if ($$checked_files{$filename} eq 'selected') {
                    338:                         $line.=" checked ";
1.77      banghart  339:                     }
1.116     albertel  340: 		    $line.=' /></td>';
1.77      banghart  341:                 } else {
1.103     raeburn   342:                     if (exists $locked_files{$fullpath}) {
1.152     albertel  343:                         my %anchor_fields = (
1.150     banghart  344:                             'lockinfo' => $current_path.$filename
                    345:                         );
                    346:                         $line.='<td colspan="2">'.&make_anchor($url,\%anchor_fields,'Locked').'</td>';
1.116     albertel  347: 			$css_class= 'LC_browser_file_locked';
1.77      banghart  348:                     } else {
1.120     raeburn   349:                         if (!$can_modify) {
                    350:                             $line .= '<td colspan="2">';
                    351:                         } else {
                    352:                             $line .= '<td>';
                    353:                         }
                    354:                         if ($can_delete) {
                    355:                             $line .= '<input type="checkbox" name="selectfile" value="'.$filename.'" />';
                    356:                         }
                    357:                         if ($can_modify) {
1.146     banghart  358:                             my $cat='<img class="LC_icon" alt="'.&mt('Catalog Information').
1.120     raeburn   359:                             '" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
1.152     albertel  360:                             my %anchor_fields = (
1.150     banghart  361:                                 'rename' => $filename,
                    362:                                 currentpath => $current_path
                    363:                             );
                    364:                             $line .= &make_anchor($url,\%anchor_fields,'Rename');
                    365:                             $line .= '</td><td>'.&make_anchor($href_edit_location.$filename.'.meta',undef,$cat);
                    366:                             # '<a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>';
1.120     raeburn   367:                         }
                    368:                         $line .= '</td>';
1.77      banghart  369:                     }
1.61      banghart  370:                 }
1.124     albertel  371: 		my $curr_access;
                    372: 		if ($select_mode ne 'true') {
                    373: 		    my $pub_access = 0;
                    374: 		    my $guest_access = 0;
                    375: 		    my $cond_access = 0;
                    376: 		    foreach my $key (sort(keys(%{$access_controls{$fullpath}}))) {
                    377: 			my ($num,$scope,$end,$start) = &unpack_acc_key($key);
                    378: 			if (($now > $start) && (!$end || $end > $now)) {
                    379: 			    if ($scope eq 'public')  {
                    380: 				$pub_access = 1;
                    381: 			    } elsif ($scope eq 'guest') {
                    382: 				$guest_access = 1;
                    383: 			    } else {
                    384: 				$cond_access = 1;
                    385: 			    }
                    386: 			}
                    387: 		    }
                    388: 		    if (!$pub_access && !$guest_access && !$cond_access) {
                    389: 			$curr_access = &mt('Private');
                    390: 		    } else {
                    391: 			my @allaccesses; 
                    392: 			if ($pub_access) {
                    393: 			    push(@allaccesses,&mt('Public'));
                    394: 			}
                    395: 			if ($guest_access) {
                    396: 			    push(@allaccesses,&mt('Passphrase-protected'));
                    397: 			}
                    398: 			if ($cond_access) {
                    399: 			    push(@allaccesses,&mt('Conditional'));
                    400: 			}
                    401: 			$curr_access = join('+ ',@allaccesses);
                    402: 		    }
                    403: 		}
1.140     banghart  404:                 &display_directory_line($r,$select_mode,$fullpath, $filename, $mtime, $size, $css_class, $line, 
                    405:                                         \%access_controls, $curr_access,$now, \%version_flag, $href_location, 
                    406:                                         $url, $current_path, $access_admin_text);
1.134     banghart  407: 		if ($show_versions) {
1.140     banghart  408: 		    foreach my $dir_line (@version_lines) {
                    409: 		        my ($v_filename,$dom,$testdir,$size,$mtime,$obs) = @$dir_line;
                    410:                         my ($v_fname,$version,$v_extension) = &Apache::grades::file_name_version_ext($v_filename);
1.143     banghart  411:                         $line = '<td colspan="2">&nbsp;</td>';
1.140     banghart  412:                         if (($v_fname eq $fname)&&($v_extension eq $extension)) {
                    413: 		            &display_directory_line($r,$select_mode,$fullpath, $v_filename, $mtime, $size, 
                    414: 		                            $css_class, $line, \%access_controls, $curr_access, $now,
                    415: 		                             \%version_flag, $href_location, $url, $current_path, $access_admin_text, 1);
                    416: 		        }
                    417: 		    }
1.134     banghart  418: 		}
1.47      banghart  419:             }
                    420:         }
                    421:     }
1.77      banghart  422:     if ($select_mode eq 'true') {
                    423:         $r->print('</table>
1.127     albertel  424:             <input type="hidden" name="continue" value="true" />
                    425:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'" />
                    426:             <input type="hidden" name="mode" value="selectfile" />
1.60      banghart  427:             <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
1.48      banghart  428:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
                    429:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
1.77      banghart  430:         </form>');        
                    431:     } else {
1.120     raeburn   432:         $r->print('</table>');
                    433:         if ($can_delete) {
                    434:             $r->print('
1.77      banghart  435:         <input type="submit" name="doit" value="Delete Checked Files" />
                    436:         <input type="hidden" name="action" value="delete" />
                    437:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
1.120     raeburn   438:         </form>'
                    439:             );
                    440:         }
1.77      banghart  441:     }
1.47      banghart  442: }
1.72      banghart  443: 
1.24      albertel  444: sub open_form {
1.94      raeburn   445:     my ($r,$url)=@_;
1.65      banghart  446:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
1.102     raeburn   447:     $r->print('<form name="portform" method="post" action="'.$url.'">');
1.24      albertel  448:     $r->print('<input type="hidden" name="action" value="'.
1.82      albertel  449: 	      $env{'form.action'}.'" />');
1.24      albertel  450:     $r->print('<input type="hidden" name="confirmed" value="1" />');
1.65      banghart  451:     foreach (@files) {
                    452:         $r->print('<input type="hidden" name="selectfile" value="'.
                    453: 	      $_.'" />');
                    454:     }
1.24      albertel  455:     $r->print('<input type="hidden" name="currentpath" value="'.
1.82      albertel  456: 	      $env{'form.currentpath'}.'" />');
1.24      albertel  457: }
                    458: 
                    459: sub close_form {
1.137     albertel  460:     my ($r,$url,$button_text)=@_;
1.108     raeburn   461:     if (!defined($button_text)) {
                    462:         $button_text = {
                    463:                          'continue' => &mt('Continue'),
                    464:                          'cancel'   => &mt('Cancel'),
                    465:                        };
                    466:     }
1.138     albertel  467:     $r->print('<p><input type="submit" value="'.$button_text->{'continue'}.'" />');
1.137     albertel  468:     $r->print(&group_form_data().'</p></form>');
1.112     albertel  469:     $r->print('<form action="'.$url.'" method="post">
1.24      albertel  470:                <p>
                    471:               <input type="hidden" name="currentpath" value="'.
1.137     albertel  472: 	      $env{'form.currentpath'}.'" />'.
                    473: 	      &group_form_data());
1.108     raeburn   474:     $r->print("\n".'   <input type="submit" value="'.$button_text->{'cancel'}.'" />
1.94      raeburn   475:                </p></form>'); 
1.24      albertel  476: }
                    477: 
                    478: sub display_file {
1.27      albertel  479:     my ($path,$filename)=@_;
1.65      banghart  480:     my $display_file_text;
1.116     albertel  481:     my $file_start='<span class="LC_filename">';
                    482:     my $file_end='</span>';
1.82      albertel  483:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
1.65      banghart  484:     if (!defined($filename)) { 
1.82      albertel  485:         $filename=$env{'form.selectfile'};
1.116     albertel  486:         $display_file_text = $file_start.$path.$filename.$file_end;
1.65      banghart  487:     } elsif (ref($filename) eq "ARRAY") {
1.116     albertel  488:         foreach my $file (@$filename) {
                    489:             $display_file_text .= $file_start.$path.$file.$file_end.'<br />';
1.65      banghart  490:         }
                    491:     } elsif (ref($filename) eq "SCALAR") {
1.116     albertel  492:         $display_file_text = $file_start.$path.$$filename.$file_end;
                    493:     } else {
                    494: 	$display_file_text = $file_start.$path.$filename.$file_end;
1.65      banghart  495:     }
                    496:     return $display_file_text;
1.24      albertel  497: }
                    498: 
                    499: sub done {
1.137     albertel  500:     my ($message,$url)=@_;
1.76      banghart  501:     unless (defined $message) {
                    502:         $message='Done';
                    503:     }
1.153     banghart  504:     my %anchor_fields = (
                    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.155   ! banghart  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.');
        !           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 />';
1.153     banghart  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 '') {
                    652:         $info .= (&mt("Direct others to the 'Display file listing' link (shown when there are viewable files) on your personal information page:<br /><a href='/adm/$udom/$uname/aboutme'>http://$ENV{'SERVER_NAME'}/adm/$udom/$uname/aboutme</a>"));
                    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.123     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}}) {
                   1612:             if (ref($array_item) eq 'ARRAY') {
                   1613:                 my $filetext;
                   1614:                 if (defined($group)) {
                   1615:                     $filetext = '<strong>'.$env{'form.lockinfo'}.
                   1616:                                     '</strong> (group: '.$group.')'; 
                   1617:                 } else {
                   1618:                     $filetext = '<strong>'.$file_name.'</strong>';
                   1619:                 } 
1.155   ! banghart 1620:                 if ($$array_item[(@$array_item - 1)] eq 'graded') {
        !          1621:                     $r->print(&mt('[_1] was submitted in response to problem: ',
1.102     raeburn  1622:                               $filetext).
                   1623:                           '<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
                   1624:                           '</strong><br />');
1.155   ! banghart 1625:                     my %course_description = &Apache::lonnet::coursedescription($$array_item[1]);
        !          1626:                     $r->print(&mt('In the course: <strong>[_1]</strong><br />',
1.102     raeburn  1627:                               $course_description{'description'}));
1.155   ! banghart 1628:                 } elsif ($$array_item[(@$array_item - 1)] eq 'handback') {
        !          1629:                     $r->print(&mt('[_1] was handed back in response to problem: ',
        !          1630:                               $filetext).
        !          1631:                           '<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
        !          1632:                           '</strong><br />');
        !          1633:                     my %course_description = &Apache::lonnet::coursedescription($$array_item[1]);
        !          1634:                     $r->print(&mt('In the course: <strong>[_1]</strong><br />',
        !          1635:                               $course_description{'description'}));
        !          1636:                     
        !          1637:                 }
1.102     raeburn  1638:                 # $r->print('the third is '.$$array_item[2].'<br>');
                   1639:                 # $r->print("item is $$array_item[0]<br> and $$array_item[0]");
1.85      banghart 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.146     banghart 1686:     my ($uname,$udom) = &get_name_dom();
1.137     albertel 1687:     my $path;
1.138     albertel 1688:     if (defined($env{'form.group'})) {
                   1689:         $path = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
1.94      raeburn  1690:     } else {
                   1691:         $path = '/userfiles/portfolio';
                   1692:     }
                   1693:     return (&Apache::loncommon::propath($udom,$uname).$path);
                   1694: }
                   1695: 
1.126     raeburn  1696: sub get_group_quota {
                   1697:     my ($group) = @_;
                   1698:     my $group_quota; 
                   1699:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1700:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1701:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
                   1702:     if (%curr_groups) {
                   1703:         my %group_info =  &Apache::longroup::get_group_settings(
                   1704:                                                     $curr_groups{$group});
                   1705:         $group_quota = $group_info{'quota'}; #expressed in Mb
                   1706:         if ($group_quota) {
                   1707:             $group_quota = 1000 * $group_quota; #expressed in k
                   1708:         } 
                   1709:     }
                   1710:     return $group_quota;
                   1711: } 
                   1712: 
1.94      raeburn  1713: sub get_dir_list {
1.138     albertel 1714:     my ($portfolio_root) = @_;
                   1715:     my ($uname,$udom) = &get_name_dom();
1.94      raeburn  1716:     return &Apache::lonnet::dirlist($env{'form.currentpath'},
                   1717:                                           $udom,$uname,$portfolio_root);
                   1718: }
                   1719: 
                   1720: sub get_name_dom {
                   1721:     my ($uname,$udom);
1.138     albertel 1722:     if (defined($env{'form.group'})) {
1.94      raeburn  1723:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1724:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1725:     } else {
                   1726:         $udom = $env{'user.domain'};
                   1727:         $uname = $env{'user.name'};
                   1728:     }
                   1729:     return ($uname,$udom);
                   1730: }
                   1731: 
1.102     raeburn  1732: sub prepend_group {
1.138     albertel 1733:     my ($filename) = @_;
                   1734:     if (defined($env{'form.group'})) {
                   1735:         $filename = $env{'form.group'}.$filename;
1.102     raeburn  1736:     }
                   1737:     return $filename;
                   1738: }
                   1739: 
1.94      raeburn  1740: sub get_namespace {
                   1741:     my $namespace = 'portfolio';
1.138     albertel 1742:     if (defined($env{'form.group'})) {
                   1743:         my ($uname,$udom) = &get_name_dom();
                   1744:         $namespace .= '_'.$udom.'_'.$uname.'_'.$env{'form.group'};
1.94      raeburn  1745:     }
                   1746:     return $namespace;
                   1747: }
                   1748: 
                   1749: sub get_port_path {
                   1750:     my $port_path;
1.138     albertel 1751:     if (defined($env{'form.group'})) {
                   1752:        $port_path = "groups/$env{'form.group'}/portfolio";
1.94      raeburn  1753:     } else {
                   1754:        $port_path = 'portfolio';
                   1755:     }
                   1756:     return $port_path;
1.24      albertel 1757: }
                   1758: 
1.120     raeburn  1759: sub missing_priv {
1.138     albertel 1760:     my ($r,$url,$priv) = @_;
1.120     raeburn  1761:     my $longtext = {
                   1762:                       upload => 'upload files',
                   1763:                       delete => 'delete files',
                   1764:                       rename => 'rename files',
                   1765:                       setacl => 'set access controls for files',
                   1766:                    };
                   1767:     my $escpath = &HTML::Entities::encode($env{'form.currentpath'},'&<>"');
                   1768:     my $rtnlink = '<a href="'.$url;
                   1769:     if ($url =~ /\?/) {
                   1770:         $rtnlink .= '&';
                   1771:     } else {
                   1772:         $rtnlink .= '?';
                   1773:     }
                   1774:     $rtnlink .= 'currentpath='.$escpath;
                   1775:     $r->print(&mt('<h3>Action disallowed</h3>'));
                   1776:     $r->print(&mt('You do not have sufficient privileges to [_1] ',
                   1777:                   $longtext->{$priv}));
1.138     albertel 1778:     if (defined($env{'form.group'})) {
1.120     raeburn  1779:         $r->print(&mt("in the group's file repository."));
1.137     albertel 1780:         $rtnlink .= &group_args()
1.120     raeburn  1781:     } else {
                   1782:         $r->print(&mt('in this portfolio.'));
                   1783:     }
                   1784:     $rtnlink .= '">'.&mt('Return to directory listing page').'</a>';
                   1785:     $r->print('<br />'.$rtnlink);
                   1786:     $r->print(&Apache::loncommon::end_page());
                   1787:     return;
                   1788: }
                   1789: 
1.132     raeburn  1790: sub coursegrp_portfolio_header {
1.137     albertel 1791:     my ($cdom,$cnum,$grp_desc)=@_;
1.132     raeburn  1792:     my $gpterm  = &Apache::loncommon::group_term();
                   1793:     my $ucgpterm = $gpterm;
                   1794:     $ucgpterm =~ s/^(\w)/uc($1)/e;
1.137     albertel 1795:     if ($env{'form.ref'}) {
1.136     raeburn  1796:         &Apache::lonhtmlcommon::add_breadcrumb
                   1797:             ({href=>"/adm/coursegroups",
                   1798:               text=>"Groups",
                   1799:               title=>"Course Groups"});
                   1800:     }
1.132     raeburn  1801:     &Apache::lonhtmlcommon::add_breadcrumb
1.138     albertel 1802:         ({href=>"/adm/$cdom/$cnum/$env{'form.group'}/smppg?ref=$env{'form.ref'}",
1.132     raeburn  1803:           text=>"$ucgpterm: $grp_desc",
                   1804:           title=>"Go to group's home page"},
1.137     albertel 1805:          {href=>"/adm/coursegrp_portfolio?".&group_args(),
1.132     raeburn  1806:           text=>"Group Portfolio",
1.136     raeburn  1807:           title=>"Display group portfolio"});
1.132     raeburn  1808:     my $output = &Apache::lonhtmlcommon::breadcrumbs(
                   1809:                          &mt('[_1] portfolio files - [_2]',$gpterm,$grp_desc));
                   1810:     return $output;
                   1811: }
                   1812: 
                   1813: 
1.24      albertel 1814: sub handler {
                   1815:     # this handles file management
                   1816:     my $r = shift;
1.73      banghart 1817:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.108     raeburn  1818:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
                   1819: 	  'fieldname','mode','rename','continue','group','access','setnum',
1.136     raeburn  1820:           'cnum','cdom','type','setroles','showversions','ref']);
1.138     albertel 1821:     my ($uname,$udom,$portfolio_root,$url,$caller,$title,$group,$grp_desc);
1.94      raeburn  1822:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
                   1823:         $url = $1.$2;
                   1824:         $caller = $2;
                   1825:     }
1.137     albertel 1826:     my ($can_modify,$can_delete,$can_upload,$can_setacl);
1.94      raeburn  1827:     if ($caller eq 'coursegrp_portfolio') {
                   1828:     #  Needs to be in a course
                   1829:         if (! ($env{'request.course.fn'})) {
                   1830:         # Not in a course
                   1831:             $env{'user.error.msg'}=
                   1832:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
                   1833:             return HTTP_NOT_ACCEPTABLE;
                   1834:         }
                   1835:         my $earlyout = 0;
1.136     raeburn  1836:         my $view_permission = 
                   1837:            &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
1.137     albertel 1838:         $env{'form.group'} =~ s/\W//g;
1.138     albertel 1839: 	$group = $env{'form.group'};
1.94      raeburn  1840:         if ($group) {
1.138     albertel 1841:             ($uname,$udom) = &get_name_dom();
1.99      raeburn  1842:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
1.98      albertel 1843: 							       $group); 
                   1844:             if (%curr_groups) {
1.132     raeburn  1845:                 my %grp_content = &Apache::longroup::get_group_settings(
                   1846:                                                          $curr_groups{$group});
                   1847:                 $grp_desc = &unescape($grp_content{'description'});
1.94      raeburn  1848:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
                   1849:                                       $env{'request.course.id'}.'/'.$group))) {
1.138     albertel 1850:                     $portfolio_root = &get_portfolio_root();
1.94      raeburn  1851:                 } else {
1.116     albertel 1852:                     $r->print('You do not have the privileges required to access the shared files space for this group.');
1.94      raeburn  1853:                     $earlyout = 1;
                   1854:                 }
                   1855:             } else {
                   1856:                 $r->print('Not a valid group for this course');
                   1857:                 $earlyout = 1;
                   1858:             }
                   1859:             $title = &mt('Group files').' for '.$group; 
                   1860:         } else {
                   1861:             $r->print('Invalid group');
                   1862:             $earlyout = 1;
                   1863:         }
                   1864:         if ($earlyout) { return OK; }
1.126     raeburn  1865:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
1.120     raeburn  1866:             $can_modify = 1;
                   1867:             $can_delete = 1;
1.126     raeburn  1868:             $can_upload = 1;
                   1869:             $can_setacl = 1;
                   1870:         } else {
                   1871:             if (&Apache::lonnet::allowed('agf',$env{'request.course.id'}.'/'.$group)) {
                   1872:                 $can_setacl = 1;
                   1873:             }
                   1874:             if (&Apache::lonnet::allowed('ugf',$env{'request.course.id'}.'/'.$group)) {
                   1875:                 $can_upload = 1;
                   1876:             }
                   1877:             if (&Apache::lonnet::allowed('mgf',$env{'request.course.id'}.'/'.$group)) {
                   1878:                 $can_modify = 1;
                   1879:             }
                   1880:             if (&Apache::lonnet::allowed('dgf',$env{'request.course.id'}.'/'.$group)) {
                   1881:                 $can_delete = 1;
                   1882:             }
1.120     raeburn  1883:         }
1.94      raeburn  1884:     } else {
                   1885:         ($uname,$udom) = &get_name_dom();
                   1886:         $portfolio_root = &get_portfolio_root();
                   1887:         $title = &mt('Portfolio Manager');
1.120     raeburn  1888:         $can_modify = 1;
                   1889:         $can_delete = 1;
                   1890:         $can_upload = 1;
                   1891:         $can_setacl = 1;
1.94      raeburn  1892:     }
                   1893: 
1.138     albertel 1894:     my $port_path = &get_port_path();
1.24      albertel 1895:     &Apache::loncommon::no_cache($r);
                   1896:     &Apache::loncommon::content_type($r,'text/html');
                   1897:     $r->send_http_header;
                   1898:     # Give the LON-CAPA page header
1.82      albertel 1899:     if ($env{"form.mode"} eq 'selectfile'){
1.96      albertel 1900:         $r->print(&Apache::loncommon::start_page($title,undef,
1.97      albertel 1901: 						 {'only_body' => 1}));
1.108     raeburn  1902:     } elsif ($env{'form.action'} eq 'rolepicker') {
                   1903:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
                   1904:                                                  {'no_nav_bar'  => 1, }));
1.74      banghart 1905:     } else {
1.97      albertel 1906:         $r->print(&Apache::loncommon::start_page($title));
1.74      banghart 1907:     }
1.24      albertel 1908:     $r->rflush();
1.88      albertel 1909: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
1.116     albertel 1910:    	    $r->print('<span class="LC_error">'.
                   1911: 		      'No file was selected to upload.'.
                   1912: 		      'To upload a file, click <strong>Browse...</strong>'.
                   1913: 		      ', select a file, then click <strong>Upload</strong>.'.
                   1914: 		      '</span>');
1.40      banghart 1915: 	}
1.82      albertel 1916:     if ($env{'form.meta'}) {
1.94      raeburn  1917:         &open_form($r,$url);
1.82      albertel 1918: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
1.70      banghart 1919:         $r->print('Edit the meta data<br />');
1.137     albertel 1920:         &close_form($r,$url);
1.70      banghart 1921:     }
1.82      albertel 1922:     if ($env{'form.store'}) {
1.70      banghart 1923:     }
                   1924: 
1.82      albertel 1925:     if ($env{'form.uploaddoc.filename'}) {
1.120     raeburn  1926:         if ($can_upload) {
1.137     albertel 1927: 	    &upload($r,$url,$group);
1.120     raeburn  1928:         } else {
1.138     albertel 1929:             &missing_priv($r,$url,'upload');
1.120     raeburn  1930:         }
1.82      albertel 1931:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
1.120     raeburn  1932:         if ($can_delete) {
1.138     albertel 1933: 	    &delete_confirmed($r,$url);
1.120     raeburn  1934:         } else {
1.138     albertel 1935:             &missing_priv($r,$url,'delete');
1.120     raeburn  1936:         }
1.82      albertel 1937:     } elsif ($env{'form.action'} eq 'delete') {
1.120     raeburn  1938:         if ($can_delete) {
1.138     albertel 1939: 	    &delete($r,$url);
1.120     raeburn  1940:         } else {
1.138     albertel 1941:             &missing_priv($r,$url,'delete');
1.120     raeburn  1942:         }
1.82      albertel 1943:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
1.120     raeburn  1944:         if ($can_delete) {
1.138     albertel 1945: 	    &delete_dir_confirmed($r,$url);
1.120     raeburn  1946:         } else {
1.138     albertel 1947:             &missing_priv($r,$url,'delete');
1.120     raeburn  1948:         }
                   1949:     } elsif ($env{'form.action'} eq 'deletedir') {
                   1950:         if ($can_delete) {
1.137     albertel 1951: 	    &delete_dir($r,$url);
1.120     raeburn  1952:         } else {
1.138     albertel 1953:             &missing_priv($r,$url,'delete');
1.120     raeburn  1954:         }
1.82      albertel 1955:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
1.120     raeburn  1956:         if ($can_modify) {
1.138     albertel 1957: 	    &rename_confirmed($r,$url);
1.120     raeburn  1958:         } else {
1.138     albertel 1959:             &missing_priv($r,$url,'rename');
1.120     raeburn  1960:         }
1.82      albertel 1961:     } elsif ($env{'form.rename'}) {
                   1962:         $env{'form.selectfile'} = $env{'form.rename'};
                   1963:         $env{'form.action'} = 'rename';
1.120     raeburn  1964:         if ($can_modify) {
1.138     albertel 1965: 	    &rename($r,$url);
1.120     raeburn  1966:         } else {
1.138     albertel 1967:             &missing_priv($r,$url,'rename');
1.120     raeburn  1968:         }
1.104     raeburn  1969:     } elsif ($env{'form.access'}) {
                   1970:         $env{'form.selectfile'} = $env{'form.access'};
                   1971:         $env{'form.action'} = 'chgaccess';
1.137     albertel 1972:         &display_access($r,$url,$group,$can_setacl,$port_path);
1.104     raeburn  1973:     } elsif ($env{'form.action'} eq 'chgaccess') {
1.120     raeburn  1974:         if ($can_setacl) {
1.137     albertel 1975:             &update_access($r,$url,$group,$port_path);
1.120     raeburn  1976:         } else {
1.138     albertel 1977:             &missing_priv($r,$url,'setacl');
1.120     raeburn  1978:         }
1.108     raeburn  1979:     } elsif ($env{'form.action'} eq 'rolepicker') {
1.120     raeburn  1980:         if ($can_setacl) { 
                   1981:             &role_options_window($r);
                   1982:         } else {
1.138     albertel 1983:             &missing_priv($r,$url,'setacl');
1.120     raeburn  1984:         }
1.82      albertel 1985:     } elsif ($env{'form.createdir'}) {
1.120     raeburn  1986:         if ($can_upload) {
1.138     albertel 1987: 	    &createdir($r,$url);
1.120     raeburn  1988:         } else {
1.138     albertel 1989:             &missing_priv($r,$url,'upload');
1.120     raeburn  1990:         }
1.82      albertel 1991:     } elsif ($env{'form.lockinfo'}) {
1.137     albertel 1992:         &lock_info($r,$url,$group);
1.24      albertel 1993:     } else {
                   1994: 	my $current_path='/';
1.82      albertel 1995: 	if ($env{'form.currentpath'}) {
                   1996: 	    $current_path = $env{'form.currentpath'};
1.24      albertel 1997: 	}
1.132     raeburn  1998:         if ($caller eq 'coursegrp_portfolio') {
                   1999:             &Apache::lonhtmlcommon::clear_breadcrumbs();
1.137     albertel 2000:             $r->print(&coursegrp_portfolio_header($udom,$uname,$grp_desc));
1.132     raeburn  2001:         }
1.138     albertel 2002:         my @dir_list=&get_dir_list($portfolio_root);
1.46      albertel 2003: 	if ($dir_list[0] eq 'no_such_dir'){
                   2004: 	    # two main reasons for this:
                   2005:             #    1) never been here, so directory structure not created
                   2006: 	    #    2) back-button navigation after deleting a directory
                   2007: 	    if ($current_path eq '/'){
1.100     albertel 2008: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
1.138     albertel 2009: 					       &get_port_path());
1.46      albertel 2010: 	    } else {
                   2011:                 # some directory that snuck in get rid of the directory
                   2012:                 # from the recent pulldown, just in case
                   2013: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
                   2014: 						      [$current_path]);
                   2015: 		$current_path = '/'; # force it back to the root        
                   2016: 	    }
                   2017: 	    # now grab the directory list again, for the first time
                   2018: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
1.94      raeburn  2019: 					    $udom,$uname,$portfolio_root);
1.43      banghart 2020:         }
1.46      albertel 2021: 	# need to know if directory is empty so it can be removed if desired
                   2022: 	my $is_empty=(@dir_list == 2);
1.137     albertel 2023: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,
                   2024: 			$can_upload);
1.138     albertel 2025:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group,
1.137     albertel 2026:                            $can_upload,$can_modify,$can_delete,$can_setacl);
1.95      albertel 2027: 	$r->print(&Apache::loncommon::end_page());
1.30      banghart 2028:     }
1.90      albertel 2029:     return OK;
1.2       banghart 2030: }
1.120     raeburn  2031: 
1.1       banghart 2032: 1;
                   2033: __END__

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