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

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

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