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

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

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