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

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

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