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

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

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