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

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

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