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

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

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