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

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

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