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

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

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