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

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

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