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

1.125     albertel    1: # The LearningOnline Network
                      2: # portfolio browser
                      3: #
1.209   ! weissno     4: # $Id: portfolio.pm,v 1.209 2009/02/16 21:12:13 weiss 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.206     raeburn    83:     my ($r,$url,$current_path,$is_empty,$dir_list,$can_upload,$group)=@_;
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') . "/";
1.199     bisitz     90:         my %lt=&Apache::lonlocal::texthash(
1.120     raeburn    91: 					 'upload' => 'Upload',
1.199     bisitz     92: 					 'upload_label' => 'Upload file to current directory',
1.88      albertel   93: 					 'createdir' => 'Create Subdirectory',
                     94: 					 'createdir_label' => 
1.199     bisitz     95: 					 'Create subdirectory in current directory',
1.193     raeburn    96:                                          'parse' => 'If HTML file, upload embedded images/multimedia/css/linked files'
                     97:                                             );
1.120     raeburn    98:         my $escuri = &HTML::Entities::encode($r->uri,'&<>"');
1.168     albertel   99: 	my $help_fileupload = &Apache::loncommon::help_open_topic('Portfolio AddFiles');
                    100: 	my $help_createdir = &Apache::loncommon::help_open_topic('Portfolio CreateDirectory');
1.206     raeburn   101:         my $help_portfolio = &Apache::loncommon::help_open_topic('Portfolio About', &mt('Help on the portfolio'));
                    102:         $r->print(&display_usage($group));
1.195     raeburn   103:         my $parse_check;
                    104:         if (!&suppress_embed_prompt()) {
                    105:             $parse_check = <<"END";
                    106:         <br />
                    107:         <span class="LC_nobreak">
1.199     bisitz    108:          <label>$lt{'parse'}
1.195     raeburn   109:          <input type="checkbox" name="parserflag" checked="checked" />
                    110:          </label>
                    111:         </span>
                    112: END
                    113:         }
1.199     bisitz    114: 
1.206     raeburn   115:         $r->print('<div>'.$help_portfolio);
1.199     bisitz    116:         # Upload File
                    117:         $r->print('<div class="LC_left_float">'
                    118:                  .'<form method="post" enctype="multipart/form-data" action="'.$escuri.'">'
                    119:                  .'<fieldset>'
                    120:                  .'<legend>'.$lt{'upload_label'}.'</legend>'
                    121:                  .$groupitem 
                    122:                  .'<input name="uploaddoc" type="file" />'
                    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="storeupl" value="'.$lt{'upload'}.'" />'
                    128:                  .$help_fileupload
                    129:                  .$parse_check
                    130:                  .'</fieldset>'
                    131:                  .'</form>'
                    132:                  .'</div>'
                    133:         );
                    134:         # Create Subdirectory
                    135:         $r->print('<div class="LC_left_float">'
                    136:                  .'<form method="post" action="'.$escuri.'">'
                    137:                  .'<fieldset>'
                    138:                  .'<legend>'.$lt{'createdir_label'}.'</legend>'
1.204     raeburn   139:                  .'<input name="newdir" type="text" />'.$groupitem
1.199     bisitz    140:                  .'<input type="hidden" name="currentpath" value="'.$current_path.'" />'
                    141:                  .'<input type="hidden" name="action" value="'.$env{"form.action"}.'" />'
                    142:                  .'<input type="hidden" name="fieldname" value="'.$env{"form.fieldname"}.'" />'
                    143:                  .'<input type="hidden" name="mode" value="'.$env{"form.mode"}.'" />'
                    144:                  .'<input type="submit" name="createdir" value="'.$lt{'createdir'}.'" />'
                    145:                  .$help_createdir
                    146:                  .'</fieldset>'
                    147:                  .'</form>'
                    148:                  .'</div>'
                    149:         );
                    150:         $r->print('</div>');
                    151:     } # end "if can_upload"
                    152: 
1.24      albertel  153:     my @tree = split (/\//,$current_path);
1.150     banghart  154:     my %anchor_fields = (
1.149     banghart  155:         'selectfile'    => $port_path,
                    156:         'currentpath'   => '/',
                    157:         'mode'          => $env{"form.mode"},
                    158:         'fieldname'     => $env{"form.fieldname"},
                    159:         'continue'      => $env{"form.continue"}
                    160:     );
                    161:     $r->print('<span class="LC_current_location">'.&make_anchor($url,\%anchor_fields,$port_path).'/');
1.19      banghart  162:     if (@tree > 1){
1.176     albertel  163:         my $newCurrentPath = '/';
1.19      banghart  164:         for (my $i = 1; $i< @tree; $i++){
                    165:             $newCurrentPath .= $tree[$i].'/';
1.150     banghart  166:             my %anchor_fields = (
1.149     banghart  167:                 'selectfile' => $tree[$i],
1.176     albertel  168:                 'currentpath' => $newCurrentPath,
1.149     banghart  169:                 'mode' => $env{"form.mode"},
                    170:                 'fieldname' => $env{"form.fieldname"},
                    171:                 'continue' => $env{"form.continue"}
                    172:             );
                    173:             $r->print(&make_anchor($url,\%anchor_fields,$tree[$i]).'/');
1.19      banghart  174:         }
                    175:     }
1.117     albertel  176:     $r->print('</span>');
1.168     albertel  177:     $r->print(&Apache::loncommon::help_open_topic('Portfolio ChangeDirectory'));
1.94      raeburn   178:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
1.137     albertel  179:     $r->print('<br /><form method="post" action="'.$url.'?mode='.$env{"form.mode"}.'&amp;fieldname='.$env{"form.fieldname"}.&group_args());
1.94      raeburn   180:     $r->print('">'.
                    181: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
1.22      albertel  182: 						    'this.form.submit();'));
1.21      banghart  183:     $r->print("</form>");
1.48      banghart  184: }
1.137     albertel  185: 
1.206     raeburn   186: sub display_usage {
                    187:     my ($group) = @_;
                    188:     my $disk_quota = &get_quota($group);
                    189:     my $getpropath = 1;
                    190:     my $portfolio_root = &get_portfolio_root();
                    191:     my ($uname,$udom) = &get_name_dom($group);
                    192:     my $current_disk_usage =
                    193:          &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
                    194:     my $usage = $current_disk_usage/1000;
                    195:     my $quota = $disk_quota/1000;
                    196:     my $percent;
                    197:     if ($disk_quota == 0) {
                    198:         $percent = 100.0;
                    199:     } else {
                    200:         $percent = 100*($current_disk_usage/$disk_quota);
                    201:     }
                    202:     $usage = sprintf("%.2f",$usage);
                    203:     $quota = sprintf("%.2f",$quota);
                    204:     $percent = sprintf("%.0f",$percent);
                    205:     my ($color,$cssclass);
                    206:     if ($percent <= 60) {
                    207:         $color = '#00A000';
                    208:     } elsif ($percent > 60 && $percent < 90) {
                    209:         $color = '#FFD300';
                    210:         $cssclass = 'class="LC_warning"';
                    211:     } elsif( $percent >= 90) {
                    212:         $color = '#FF0000';
                    213:         $cssclass = 'class="LC_error"';
                    214:     }
                    215:     my $prog_width = $percent;
                    216:     if ($prog_width > 100) {
                    217:         $prog_width = 100;
                    218:     }
                    219:     my $disk_meter = '
                    220:   <div id="meter1" align="left" '.$cssclass.'>'.&mt('Currently using [_1] of the [_2] available.',$usage.' MB <span style="font-weight:bold;">('.$percent.'%)</span>',$quota.' MB')."\n".
                    221: '   <div id="meter2" style="display:block; margin-top:5px; margin-bottom:5px; margin-left:0px; margin-right:0px; width:400px; border:1px solid #000000; height:10px;">'."\n".
                    222: '    <div id="meter3" style="display:block; background-color:'.$color.'; width:'.$prog_width.'%; height:10px; color:#000000; margin:0px;"></div>'."\n".
                    223: '   </div>'."\n".
                    224: '  </div>';
                    225:     return $disk_meter;
                    226: }
                    227: 
1.134     banghart  228: sub display_directory_line {
1.156     albertel  229:     my ($r,$select_mode, $filename, $mtime, $size, $css_class,
1.137     albertel  230: 	$line, $access_controls, $curr_access, $now, $version_flag,
1.140     banghart  231: 	$href_location, $url, $current_path, $access_admin_text, $versions)=@_;
1.156     albertel  232: 
                    233:     my $fullpath =  &prepend_group($current_path.$filename);
1.197     bisitz    234:     $r->print(&Apache::loncommon::start_data_table_row());
1.153     banghart  235:     $r->print($line); # contains first two cells of table
1.155     banghart  236:     my $lock_info;
1.156     albertel  237:     if ($version_flag) { # versioned can't be versioned, so TRUE when root file
1.153     banghart  238:         $r->print('<td><img alt="" src="'.&Apache::loncommon::icon($filename).'" /></td>');
1.156     albertel  239:         $r->print('<td>'.$version_flag.'</td>');
1.153     banghart  240:     } else { # this is a graded or handed back file
1.191     raeburn   241:         my ($user,$domain) = &get_name_dom($env{'form.group'});
1.154     banghart  242:         my $permissions_hash = &Apache::lonnet::get_portfile_permissions($domain,$user);
1.156     albertel  243:         if (defined($$permissions_hash{$fullpath})) {
                    244:             foreach my $array_item (@{$$permissions_hash{$fullpath}}) {
1.155     banghart  245:                 if (ref($array_item) eq 'ARRAY') {
1.156     albertel  246:                     if ($$array_item[-1] eq 'handback') {
1.155     banghart  247:                         $lock_info = 'Handback';
1.156     albertel  248:                     } elsif ($$array_item[-1] eq 'graded') {
1.155     banghart  249:                         $lock_info = 'Graded';
1.154     banghart  250:                     }
1.155     banghart  251:                  }
1.154     banghart  252:             }
                    253:         }
1.156     albertel  254: 	if ($lock_info) {
                    255: 	    my %anchor_fields = ('lockinfo' => $fullpath);
1.161     banghart  256: 	    if ($versions) { # hold the folder open
                    257: 	        my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($fullpath);
                    258: 	        $fname =~ s|^/||;
                    259: 	        $anchor_fields{'showversions'} = $fname.'.'.$extension;
                    260: 	    }
1.156     albertel  261: 	    $lock_info = &make_anchor(undef,\%anchor_fields,$lock_info);
                    262: 	}
                    263: 	$r->print('<td colspan="2">'.$lock_info.'</td>');
1.153     banghart  264:     }
                    265:     # $r->print('<td>'.$$version_flag{$filename}.'</td><td>');
                    266:     $r->print('<td>'.&make_anchor($href_location.$filename,undef,$filename).'</td>'); 
1.137     albertel  267:     $r->print('<td>'.$size.'</td>');
                    268:     $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
                    269:     if ($select_mode ne 'true') {
1.197     bisitz    270:         $r->print('<td class="'.$css_class.'">&nbsp;&nbsp;</td>'); # Display status
                    271:         $r->print('<td><span class="LC_nobreak">'
                    272:                  .&mt($curr_access).'&nbsp;&nbsp;&nbsp;'
                    273:        );
1.152     albertel  274:         my %anchor_fields = (
1.150     banghart  275:             'access' => $filename,
                    276:             'currentpath' => $current_path
                    277:         );
                    278: 	$r->print(&make_anchor($url, \%anchor_fields, $access_admin_text).'</span></td>');
1.197     bisitz    279:     } else {
                    280:         $r->print('<td class="'.$css_class.'">&nbsp;&nbsp;</td>'); # Display status
1.137     albertel  281:     }
1.197     bisitz    282:     $r->print(&Apache::loncommon::end_data_table_row().$/);
1.134     banghart  283: }
1.137     albertel  284: 
1.48      banghart  285: sub display_directory {
1.138     albertel  286:     my ($r,$url,$current_path,$is_empty,$dir_list,$group,$can_upload,
1.137     albertel  287:         $can_modify,$can_delete,$can_setacl)=@_;
1.48      banghart  288:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
                    289:     my $display_out;
1.77      banghart  290:     my $select_mode;
                    291:     my $checked_files;
1.138     albertel  292:     my $port_path = &get_port_path();
1.191     raeburn   293:     my ($uname,$udom) = &get_name_dom($group);
1.120     raeburn   294:     my $access_admin_text = &mt('View Status');
                    295:     if ($can_setacl) {
                    296:         $access_admin_text = &mt('View/Change Status');
                    297:     }
                    298: 
1.102     raeburn   299:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    300:                                                                         $uname);
                    301:     my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash(
                    302:                                                   $current_permissions,$group);
                    303:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group);
                    304:     my $now = time;
1.170     raeburn   305:     if ($env{"form.mode"} eq 'selectfile') {
1.77      banghart  306: 	&select_files($r);
1.94      raeburn   307: 	$checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
1.77      banghart  308: 	$select_mode = 'true';
                    309:     } 
1.120     raeburn   310:     if ($is_empty && ($current_path ne '/') && $can_delete) {
1.137     albertel  311:         $display_out = '<form method="post" action="'.$url.'">'.
                    312: 	    &group_form_data().
1.30      banghart  313:         '<input type="hidden" name="action" value="deletedir" />'.
1.199     bisitz    314:         '<p>'.
1.30      banghart  315:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
1.199     bisitz    316:         '</p>'.
1.30      banghart  317:         '<input type="hidden" name="selectfile" value="" />'.
                    318:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
                    319:         '</form>';
                    320:         
1.48      banghart  321:         $r->print($display_out);
1.31      albertel  322: 	return;
                    323:     }
1.77      banghart  324:     if ($select_mode eq 'true') {
1.113     albertel  325:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
1.197     bisitz    326:         $r->print(&Apache::loncommon::start_data_table()
                    327:                  .&Apache::loncommon::start_data_table_header_row()
                    328:                  .'<th>'.&mt('Select').'</th>'
                    329:                  .'<th>&nbsp;</th><th>&nbsp;</th>'
                    330:                  .'<th>'.&mt('Name').'</th>'
                    331:                  .'<th>'.&mt('Size').'</th>'
                    332:                  .'<th>'.&mt('Last Modified').'</th>'
                    333:                  .'<th>&nbsp;</th>'
                    334:                  .&Apache::loncommon::end_data_table_header_row()
                    335:         );
1.77      banghart  336:     } else {
1.113     albertel  337:         $r->print('<form method="post" action="'.$url.'">');
1.168     albertel  338: 	$r->print(&Apache::loncommon::help_open_topic('Portfolio FileList',
1.187     bisitz    339: 						      &mt('Using the portfolio file list')));
1.197     bisitz    340:         $r->print(&Apache::loncommon::start_data_table()
                    341:                  .&Apache::loncommon::start_data_table_header_row()
1.187     bisitz    342:                  .'<th colspan="2">'.&mt('Actions'). &Apache::loncommon::help_open_topic('Portfolio FileAction').'</th>'
                    343:                  .'<th>&nbsp;</th><th>&nbsp;</th>'
                    344:                  .'<th>'.&mt('Name').&Apache::loncommon::help_open_topic('Portfolio OpenFile').'</th>'
                    345:                  .'<th>'.&mt('Size').'</th>'
                    346:                  .'<th>'.&mt('Last Modified').'</th>'
1.197     bisitz    347:                  .'<th>&nbsp;</th>'
1.187     bisitz    348:                  .'<th>'.&mt('Current Access Status').&Apache::loncommon::help_open_topic('Portfolio ShareFile').'</th>'
1.197     bisitz    349:                  .&Apache::loncommon::end_data_table_header_row());
1.94      raeburn   350:     }
1.137     albertel  351:     $r->print("\n".&group_form_data()."\n");
                    352: 
1.94      raeburn   353:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
                    354:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
1.105     banghart  355:     my @dir_lines;
                    356:     my %versioned;
1.140     banghart  357:     foreach my $dir_line (sort 
1.26      albertel  358: 		      { 
                    359: 			  my ($afile)=split('&',$a,2);
                    360: 			  my ($bfile)=split('&',$b,2);
                    361: 			  return (lc($afile) cmp lc($bfile));
                    362: 		      } (@$dir_list)) {
1.140     banghart  363:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$dir_line,16); 
1.77      banghart  364:     	$filename =~ s/\s+$//;
1.105     banghart  365:     	my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
                    366:     	if ($version) {
1.156     albertel  367: 	    my $fullpath = &prepend_group($current_path.$fname.'.'.$extension);
                    368:     	    push(@{ $versioned{$fullpath} },
                    369: 		 [$filename,$dom,$testdir,$size,$mtime,$obs,]);
1.131     banghart  370:     	} else {
                    371:     	    push(@dir_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
1.105     banghart  372:     	}
                    373:     }
1.140     banghart  374:     foreach my $dir_line (@dir_lines) {
                    375:         my ($filename,$dom,$testdir,$size,$mtime,$obs) = @$dir_line;
1.105     banghart  376:         my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
1.93      albertel  377:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
1.156     albertel  378:     	    my $version_flag;
1.131     banghart  379:     	    my $show_versions;
1.156     albertel  380: 	    my $fullpath =  &prepend_group($current_path.$filename);
1.162     banghart  381:     	    if ($env{'form.showversions'} =~ /$filename/) {
1.131     banghart  382:     	        $show_versions = 'true';
                    383:     	    }
1.156     albertel  384:     	    if (exists($versioned{$fullpath})) {
1.152     albertel  385:     	        my %anchor_fields = (
1.156     albertel  386:     	            'selectfile' => $fullpath,
1.150     banghart  387:     	            'continue' => 'false',
                    388:     	            'currentpath' => $current_path,
                    389:     	        );
1.134     banghart  390:     	        if ($show_versions) {
1.162     banghart  391:     	            # Must preserve other possible showversion files
                    392:     	            my $version_remainder = $env{'form.showversions'};
                    393:     	            $version_remainder =~ s/$filename//g;    	            
                    394:     	            $anchor_fields{'showversions'} = $version_remainder;
1.156     albertel  395:                     $version_flag = &make_anchor('portfolio',\%anchor_fields,
1.150     banghart  396:                         '<img class="LC_icon" alt="'.&mt('opened folder').'" src="'.$iconpath.'folder_pointer_opened.gif" />');
1.134     banghart  397:     	        } else {
1.162     banghart  398:     	            # allow multiple files to show versioned
                    399:     	            $anchor_fields{'showversions'} = $env{'form.showversions'}.','.$filename;
1.156     albertel  400:                     $version_flag = &make_anchor('portfolio',\%anchor_fields,
1.150     banghart  401:                         '<img class="LC_icon" alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_pointer_closed.gif" />');
1.134     banghart  402:                 }
1.129     banghart  403:     	    } else {
1.156     albertel  404:     	        $version_flag = '&nbsp;';
1.129     banghart  405:     	    }
1.23      albertel  406:             if ($dirptr&$testdir) {
1.197     bisitz    407: 		my $colspan_folder='';
                    408: 		my $colspan_fill='';
1.77      banghart  409:                 if ($select_mode eq 'true'){
1.197     bisitz    410:                     $colspan_fill=' colspan="3"';
                    411:                 } else {
                    412:                     $colspan_folder=' colspan="2"';
                    413:                     $colspan_fill=' colspan="4"';
1.64      banghart  414:                 }
1.197     bisitz    415: 		$r->print('<tr class="LC_browser_folder">');
                    416:                 $r->print('<td'.$colspan_folder.'><img alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_closed.gif" /></td>'
                    417:                          .'<td>'.&mt('Go to ...').'</td>');
1.152     albertel  418:                 my %anchor_fields = (
1.149     banghart  419:                     'selectfile'    => $filename.'/',
                    420:                     'currentpath'   => $current_path.$filename.'/',
                    421:                     'mode'          => $env{"form.mode"},
                    422:                     'fieldname'     => $env{"form.fieldname"},
                    423:                     'continue'      => $env{"form.continue"}
                    424:                 );  
1.197     bisitz    425:                 $r->print('<td>'.$version_flag.'</td>'
                    426:                          .'<td>'.&make_anchor($url,\%anchor_fields,$filename.'/').'</td>'); 
                    427:                 $r->print('<td'.$colspan_fill.'>&nbsp;</td>');
1.47      banghart  428:                 $r->print('</tr>'); 
                    429:             } else {
1.116     albertel  430: 		my $css_class = 'LC_browser_file';
                    431: 		my $line;
1.124     albertel  432:                 if ($select_mode eq 'true') {
1.116     albertel  433:                     $line='<td><input type="checkbox" name="checkfile" value="'.$filename.'"';
                    434: 		    if ($$checked_files{$filename} eq 'selected') {
1.197     bisitz    435:                         $line.=' checked="checked" ';
1.77      banghart  436:                     }
1.116     albertel  437: 		    $line.=' /></td>';
1.77      banghart  438:                 } else {
1.103     raeburn   439:                     if (exists $locked_files{$fullpath}) {
1.152     albertel  440:                         my %anchor_fields = (
1.156     albertel  441:                             'lockinfo' => $fullpath
1.150     banghart  442:                         );
1.188     bisitz    443:                         $line.='<td colspan="2">'.&make_anchor($url,\%anchor_fields,&mt('Locked')).'</td>';
1.116     albertel  444: 			$css_class= 'LC_browser_file_locked';
1.77      banghart  445:                     } else {
1.120     raeburn   446:                         if (!$can_modify) {
                    447:                             $line .= '<td colspan="2">';
                    448:                         } else {
                    449:                             $line .= '<td>';
                    450:                         }
                    451:                         if ($can_delete) {
                    452:                             $line .= '<input type="checkbox" name="selectfile" value="'.$filename.'" />';
                    453:                         }
                    454:                         if ($can_modify) {
1.208     schafran  455:                             my $cat='<img class="LC_icon" alt="'.&mt('Metadata').'"/>';
                    456: # 	src="'.#&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
1.152     albertel  457:                             my %anchor_fields = (
1.150     banghart  458:                                 'rename' => $filename,
                    459:                                 currentpath => $current_path
                    460:                             );
1.188     bisitz    461:                             $line .= &make_anchor($url,\%anchor_fields,&mt('Rename'));
1.159     banghart  462:                             $line .= '</td><td>'.&make_anchor($href_edit_location.$filename.'.meta',\%anchor_fields,$cat);
1.150     banghart  463:                             # '<a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>';
1.120     raeburn   464:                         }
                    465:                         $line .= '</td>';
1.77      banghart  466:                     }
1.61      banghart  467:                 }
1.124     albertel  468: 		my $curr_access;
                    469: 		if ($select_mode ne 'true') {
                    470: 		    my $pub_access = 0;
                    471: 		    my $guest_access = 0;
                    472: 		    my $cond_access = 0;
                    473: 		    foreach my $key (sort(keys(%{$access_controls{$fullpath}}))) {
                    474: 			my ($num,$scope,$end,$start) = &unpack_acc_key($key);
                    475: 			if (($now > $start) && (!$end || $end > $now)) {
                    476: 			    if ($scope eq 'public')  {
                    477: 				$pub_access = 1;
                    478: 			    } elsif ($scope eq 'guest') {
                    479: 				$guest_access = 1;
                    480: 			    } else {
                    481: 				$cond_access = 1;
                    482: 			    }
                    483: 			}
                    484: 		    }
                    485: 		    if (!$pub_access && !$guest_access && !$cond_access) {
                    486: 			$curr_access = &mt('Private');
                    487: 		    } else {
                    488: 			my @allaccesses; 
                    489: 			if ($pub_access) {
                    490: 			    push(@allaccesses,&mt('Public'));
                    491: 			}
                    492: 			if ($guest_access) {
                    493: 			    push(@allaccesses,&mt('Passphrase-protected'));
                    494: 			}
                    495: 			if ($cond_access) {
                    496: 			    push(@allaccesses,&mt('Conditional'));
                    497: 			}
                    498: 			$curr_access = join('+ ',@allaccesses);
                    499: 		    }
                    500: 		}
1.156     albertel  501:                 &display_directory_line($r,$select_mode, $filename, $mtime, $size, $css_class, $line, 
                    502:                                         \%access_controls, $curr_access,$now, $version_flag, $href_location, 
1.140     banghart  503:                                         $url, $current_path, $access_admin_text);
1.134     banghart  504: 		if ($show_versions) {
1.156     albertel  505: 		    foreach my $dir_line (@{ $versioned{$fullpath} }) {
                    506: 		        my ($v_filename,$dom,$testdir,$size,$mtime,$obs) =
                    507: 			    @$dir_line;
1.143     banghart  508:                         $line = '<td colspan="2">&nbsp;</td>';
1.156     albertel  509: 			&display_directory_line($r,$select_mode, $v_filename, $mtime, $size, 
                    510: 						$css_class, $line, \%access_controls, $curr_access, $now,
                    511: 						undef, $href_location, $url, $current_path, $access_admin_text, 1);
1.140     banghart  512: 		    }
1.134     banghart  513: 		}
1.47      banghart  514:             }
                    515:         }
                    516:     }
1.77      banghart  517:     if ($select_mode eq 'true') {
1.197     bisitz    518:         $r->print(&Apache::loncommon::end_data_table().'
1.127     albertel  519:             <input type="hidden" name="continue" value="true" />
                    520:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'" />
                    521:             <input type="hidden" name="mode" value="selectfile" />
1.198     bisitz    522:             <p>
                    523:             <input type="submit" name="submit" value="'.&mt('Select checked files, and continue selecting').'" /><br />
                    524:             <input type="button" name="doit" onClick= "finishSelect();" value="'.&mt('Select checked files, and close window').'" />
                    525:             </p>
1.48      banghart  526:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
1.77      banghart  527:         </form>');        
                    528:     } else {
1.197     bisitz    529:         $r->print(&Apache::loncommon::end_data_table());
1.120     raeburn   530:         if ($can_delete) {
                    531:             $r->print('
1.199     bisitz    532:         <p>
1.196     schafran  533:         <input type="submit" name="doit" value="'.&mt('Delete Selected').'" />'.
1.168     albertel  534: 	&Apache::loncommon::help_open_topic('Portfolio DeleteFile').'
1.199     bisitz    535:         </p>
1.77      banghart  536:         <input type="hidden" name="action" value="delete" />
                    537:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
1.120     raeburn   538:         </form>'
                    539:             );
                    540:         }
1.77      banghart  541:     }
1.47      banghart  542: }
1.72      banghart  543: 
1.24      albertel  544: sub open_form {
1.94      raeburn   545:     my ($r,$url)=@_;
1.65      banghart  546:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
1.102     raeburn   547:     $r->print('<form name="portform" method="post" action="'.$url.'">');
1.24      albertel  548:     $r->print('<input type="hidden" name="action" value="'.
1.82      albertel  549: 	      $env{'form.action'}.'" />');
1.24      albertel  550:     $r->print('<input type="hidden" name="confirmed" value="1" />');
1.65      banghart  551:     foreach (@files) {
                    552:         $r->print('<input type="hidden" name="selectfile" value="'.
                    553: 	      $_.'" />');
                    554:     }
1.24      albertel  555:     $r->print('<input type="hidden" name="currentpath" value="'.
1.82      albertel  556: 	      $env{'form.currentpath'}.'" />');
1.24      albertel  557: }
                    558: 
                    559: sub close_form {
1.137     albertel  560:     my ($r,$url,$button_text)=@_;
1.108     raeburn   561:     if (!defined($button_text)) {
                    562:         $button_text = {
                    563:                          'continue' => &mt('Continue'),
                    564:                          'cancel'   => &mt('Cancel'),
                    565:                        };
                    566:     }
1.138     albertel  567:     $r->print('<p><input type="submit" value="'.$button_text->{'continue'}.'" />');
1.137     albertel  568:     $r->print(&group_form_data().'</p></form>');
1.112     albertel  569:     $r->print('<form action="'.$url.'" method="post">
1.24      albertel  570:                <p>
                    571:               <input type="hidden" name="currentpath" value="'.
1.137     albertel  572: 	      $env{'form.currentpath'}.'" />'.
                    573: 	      &group_form_data());
1.108     raeburn   574:     $r->print("\n".'   <input type="submit" value="'.$button_text->{'cancel'}.'" />
1.94      raeburn   575:                </p></form>'); 
1.24      albertel  576: }
                    577: 
                    578: sub display_file {
1.27      albertel  579:     my ($path,$filename)=@_;
1.65      banghart  580:     my $display_file_text;
1.116     albertel  581:     my $file_start='<span class="LC_filename">';
                    582:     my $file_end='</span>';
1.82      albertel  583:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
1.65      banghart  584:     if (!defined($filename)) { 
1.82      albertel  585:         $filename=$env{'form.selectfile'};
1.116     albertel  586:         $display_file_text = $file_start.$path.$filename.$file_end;
1.65      banghart  587:     } elsif (ref($filename) eq "ARRAY") {
1.116     albertel  588:         foreach my $file (@$filename) {
                    589:             $display_file_text .= $file_start.$path.$file.$file_end.'<br />';
1.65      banghart  590:         }
                    591:     } elsif (ref($filename) eq "SCALAR") {
1.116     albertel  592:         $display_file_text = $file_start.$path.$$filename.$file_end;
                    593:     } else {
                    594: 	$display_file_text = $file_start.$path.$filename.$file_end;
1.65      banghart  595:     }
                    596:     return $display_file_text;
1.24      albertel  597: }
                    598: 
                    599: sub done {
1.137     albertel  600:     my ($message,$url)=@_;
1.76      banghart  601:     unless (defined $message) {
                    602:         $message='Done';
                    603:     }
1.153     banghart  604:     my %anchor_fields = (
1.161     banghart  605:         'showversions' => $env{'form.showversions'},
1.153     banghart  606:         'currentpath' => $env{'form.currentpath'},
                    607:         'fieldname' => $env{'form.fieldname'},
                    608:         'mode'      => $env{'form.mode'}
                    609:     );
                    610:     my $result = '<h3>'.&make_anchor($url,\%anchor_fields,&mt($message)).'</h3>';
1.94      raeburn   611:     return $result;
1.24      albertel  612: }
                    613: 
                    614: sub delete {
1.191     raeburn   615:     my ($r,$url,$group)=@_;
1.55      banghart  616:     my @check;
1.82      albertel  617:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
1.138     albertel  618:     $file_name = &prepend_group($file_name);
1.65      banghart  619:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
1.191     raeburn   620:     my ($uname,$udom) = &get_name_dom($group);
1.94      raeburn   621:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.190     raeburn   622:         $r->print(&mt('The file is locked and cannot be deleted.').'<br />');
1.137     albertel  623:         $r->print(&done('Back',$url));
1.55      banghart  624:     } else {
1.66      banghart  625:         if (scalar(@files)) {
1.94      raeburn   626:             &open_form($r,$url);
1.188     bisitz    627:             $r->print('<p>'.&mt('Delete [_1]?',&display_file(undef,\@files)).'</p>');
1.137     albertel  628:             &close_form($r,$url);
1.66      banghart  629:         } else {
                    630:             $r->print("No file was checked to delete.<br />");
1.137     albertel  631:             $r->print(&done(undef,$url));
1.66      banghart  632:         }
1.55      banghart  633:     }
1.24      albertel  634: } 
                    635: 
                    636: sub delete_confirmed {
1.164     raeburn   637:     my ($r,$url,$group)=@_;
1.65      banghart  638:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
                    639:     my $result;
1.191     raeburn   640:     my ($uname,$udom) = &get_name_dom($group);
1.138     albertel  641:     my $port_path = &get_port_path();
1.164     raeburn   642:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    643:                                                                         $uname);
1.65      banghart  644:     foreach my $delete_file (@files) {
1.94      raeburn   645:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
1.82      albertel  646: 					       $env{'form.currentpath'}.
1.65      banghart  647: 					       $delete_file);
                    648:         if ($result ne 'ok') {
1.187     bisitz    649: 	    $r->print('<span class="LC_error">'
                    650:                       .&mt('An error occurred ([_1]) while trying to delete [_2].'
                    651:                          ,$result,&display_file(undef, $delete_file))
                    652:                       .'</span><br /><br />');
1.164     raeburn   653:         } else {
                    654:             $r->print(&mt('File: [_1] deleted.',
                    655:                           &display_file(undef,$delete_file)));
                    656:             my $file_name = $env{'form.currentpath'}.$delete_file;
                    657:             $file_name = &prepend_group($file_name);
                    658:             my %access_controls = 
                    659:                     &Apache::lonnet::get_access_controls($current_permissions,
                    660:                                                          $group,$file_name);
                    661:             if (keys(%access_controls) > 0) {
                    662:                 my %changes; 
1.166     raeburn   663:                 foreach my $key (keys(%{$access_controls{$file_name}})) {
1.164     raeburn   664:                     $changes{'delete'}{$key} = 1;
                    665:                 }
                    666:                 if (keys(%changes) > 0) {
                    667:                     my ($outcome,$deloutcome,$new_values,$translation) =
                    668:                     &Apache::lonnet::modify_access_controls($file_name,\%changes,
                    669:                                                             $udom,$uname);
                    670:                     if ($outcome ne 'ok') {
1.182     albertel  671:                            $r->print('<br />'.&mt("An error occurred ([_1]) while ".
1.165     albertel  672:                                "trying to delete access controls for the file.",$outcome).
1.166     raeburn   673:                                '</span><br /><br />');
1.164     raeburn   674:                     } else {
                    675:                         if ($deloutcome eq 'ok') {
1.166     raeburn   676:                             $r->print('<br />'.&mt('Access controls also deleted for the file.').'<br /><br />');
1.164     raeburn   677:                         } else {
1.165     albertel  678:                             $r->print('<span class="LC_error">'.'<br />'.
1.182     albertel  679:                                &mt("An error occurred ([_1]) while ".
1.165     albertel  680:                                    "trying to delete access controls for the file.",$deloutcome).
1.166     raeburn   681:                                    '</span><br /><br />');
1.164     raeburn   682:                         }
                    683:                     }
                    684:                 }
                    685:             }
1.65      banghart  686:         }
1.24      albertel  687:     }
1.137     albertel  688:     $r->print(&done(undef,$url));
1.24      albertel  689: }
                    690: 
1.30      banghart  691: sub delete_dir {
1.137     albertel  692:     my ($r,$url)=@_;
1.94      raeburn   693:     &open_form($r,$url);
1.188     bisitz    694:      $r->print('<p>'.&mt('Delete [_1]?',&display_file()).'</p>');
1.137     albertel  695:     &close_form($r,$url);
1.30      banghart  696: } 
                    697: 
                    698: sub delete_dir_confirmed {
1.191     raeburn   699:     my ($r,$url,$group)=@_;
1.82      albertel  700:     my $directory_name = $env{'form.currentpath'};
1.81      albertel  701:     $directory_name =~ s|/$||; # remove any trailing slash
1.191     raeburn   702:     my ($uname,$udom) = &get_name_dom($group);
1.138     albertel  703:     my $namespace = &get_namespace();
                    704:     my $port_path = &get_port_path();
1.94      raeburn   705:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
1.30      banghart  706: 					       $directory_name);
1.32      banghart  707: 					       
1.30      banghart  708:     if ($result ne 'ok') {
1.188     bisitz    709: 	$r->print('<span class="LC_error">'
                    710:                   .&mt('An error occurred (dir) ([_1]) while trying to delete [_2].'
                    711:                        ,$result,$directory_name)
                    712:                   .'</span><br />');
1.32      banghart  713:     } else {
1.41      banghart  714:         # now remove from recent
1.94      raeburn   715:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
1.32      banghart  716:         my @dirs = split m!/!, $directory_name;
                    717:         $directory_name='/';
                    718:         for (my $i=1; $i < (@dirs - 1); $i ++){
                    719:             $directory_name .= $dirs[$i].'/';
                    720:         }
1.82      albertel  721:         $env{'form.currentpath'} = $directory_name;
1.30      banghart  722:     }
1.137     albertel  723:     $r->print(&done(undef,$url));
1.30      banghart  724: }
                    725: 
1.24      albertel  726: sub rename {
1.191     raeburn   727:     my ($r,$url,$group)=@_;
1.82      albertel  728:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
1.191     raeburn   729:     my ($uname,$udom) = &get_name_dom($group);
1.138     albertel  730:     $file_name = &prepend_group($file_name);
1.94      raeburn   731:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.169     albertel  732:         $r->print("The file is locked and cannot be renamed.<br />");
1.137     albertel  733:         $r->print(&done(undef,$url));
1.55      banghart  734:     } else {
1.94      raeburn   735:         &open_form($r,$url);
1.188     bisitz    736:         $r->print('<p>'.&mt('Rename [_1] to [_2]?', &display_file()
1.204     raeburn   737:                   , '<input name="filenewname" type="text" size="50" />').'</p>');
1.137     albertel  738:         &close_form($r,$url);
1.55      banghart  739:     }
1.24      albertel  740: }
                    741: 
                    742: sub rename_confirmed {
1.164     raeburn   743:     my ($r,$url,$group)=@_;
1.82      albertel  744:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
1.191     raeburn   745:     my ($uname,$udom) = &get_name_dom($group);
1.138     albertel  746:     my $port_path = &get_port_path();
1.27      albertel  747:     if ($filenewname eq '') {
1.116     albertel  748: 	$r->print('<span class="LC_error">'.
1.27      albertel  749: 		  &mt("Error: no valid filename was provided to rename to.").
1.116     albertel  750: 		  '</span><br />');
1.137     albertel  751: 	$r->print(&done(undef,$url));
1.27      albertel  752: 	return;
                    753:     } 
1.164     raeburn   754:     my $chg_access;
1.27      albertel  755:     my $result=
1.94      raeburn   756: 	&Apache::lonnet::renameuserfile($uname,$udom,
                    757:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
                    758:             $port_path.$env{'form.currentpath'}.$filenewname);
1.164     raeburn   759:     if ($result eq 'ok') {
                    760:         $chg_access = &access_for_renamed($filenewname,$group,$udom,$uname);
                    761:     } else {      
1.116     albertel  762: 	$r->print('<span class="LC_error">'.
1.189     raeburn   763: 		  &mt('An error occurred ([_1]) while trying to rename [_2] to [_3].'
1.188     bisitz    764:                       ,$result,&display_file(),&display_file('',$filenewname))
                    765:                   .'</span><br />');
1.164     raeburn   766:         return;
1.27      albertel  767:     }
1.82      albertel  768:     if ($filenewname ne $env{'form.filenewname'}) {
1.116     albertel  769:         $r->print(&mt("The new file name was changed from:<br />[_1] to [_2]",
                    770: 		      '<strong>'.&display_file('',$env{'form.filenewname'}).'</strong>',
                    771: 		      '<strong>'.&display_file('',$filenewname).'</strong>'));
1.66      banghart  772:     }
1.164     raeburn   773:     $r->print($chg_access);
1.137     albertel  774:     $r->print(&done(undef,$url));
1.27      albertel  775: }
1.102     raeburn   776: 
1.164     raeburn   777: sub access_for_renamed {
                    778:     my ($filenewname,$group,$udom,$uname) = @_;
                    779:     my $oldfile = $env{'form.currentpath'}.$env{'form.selectfile'};
                    780:     $oldfile = &prepend_group($oldfile);
                    781:     my $newfile = $env{'form.currentpath'}.$filenewname;
                    782:     $newfile = &prepend_group($newfile);
                    783:     my $current_permissions =
1.165     albertel  784: 	&Apache::lonnet::get_portfile_permissions($udom,$uname);
1.164     raeburn   785:     my %access_controls =
1.165     albertel  786: 	&Apache::lonnet::get_access_controls($current_permissions,
                    787: 					     $group,$oldfile);
1.164     raeburn   788:     my $chg_text;
                    789:     if (keys(%access_controls) > 0) {
                    790:         my %change_old;
                    791:         my %change_new;
1.165     albertel  792:         foreach my $key (keys(%{$access_controls{$oldfile}})) {
1.164     raeburn   793:             $change_old{'delete'}{$key} = 1;
                    794:             $change_new{'activate'}{$key} = $access_controls{$oldfile}{$key};
                    795:         }
                    796:         my ($outcome,$deloutcome,$new_values,$translation) =
                    797:             &Apache::lonnet::modify_access_controls($oldfile,\%change_old,
1.165     albertel  798: 						    $udom,$uname);
1.164     raeburn   799:         if ($outcome ne 'ok') {
1.182     albertel  800:             $chg_text ='<br /><br />'.&mt("An error occurred ([_1]) while ".
1.165     albertel  801:                 "trying to delete access control records for the old name.",$outcome).
1.164     raeburn   802:                 '</span><br />';
                    803:         } else {
                    804:             if ($deloutcome ne 'ok') {
1.165     albertel  805:                 $chg_text = '<br /><br /><span class="LC_error"><br />'.
1.182     albertel  806: 		    &mt("An error occurred ([_1]) while ".
1.165     albertel  807: 			"trying to delete access control records for the old name.",$deloutcome).
                    808: 			'</span><br />';
1.164     raeburn   809:             }
                    810:         }
                    811:         ($outcome,$deloutcome,$new_values,$translation) =
                    812:             &Apache::lonnet::modify_access_controls($newfile,\%change_new,
                    813:                                                     $udom,$uname);
                    814:         if ($outcome ne 'ok') {
1.165     albertel  815:             $chg_text .= '<br /><br />'.
1.182     albertel  816: 		&mt("An error occurred ([_1]) while ".
1.165     albertel  817:                 "trying to update access control records for the new name.",$outcome).
1.164     raeburn   818:                 '</span><br />';
                    819:         }
                    820:         if ($chg_text eq '') {
                    821:             $chg_text = '<br /><br />'.&mt('Access controls updated to reflect the name change.');
                    822:         }
                    823:     }
                    824:     return $chg_text;
                    825: }
                    826: 
1.104     raeburn   827: sub display_access {
1.170     raeburn   828:     my ($r,$url,$group,$can_setacl,$port_path,$action) = @_;
1.191     raeburn   829:     my ($uname,$udom) = &get_name_dom($group);
1.104     raeburn   830:     my $file_name = $env{'form.currentpath'}.$env{'form.access'};
1.138     albertel  831:     $file_name = &prepend_group($file_name);
1.104     raeburn   832:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    833:                                                                         $uname);
                    834:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group,$file_name);
1.120     raeburn   835:     my $aclcount = keys(%access_controls);
1.170     raeburn   836:     my ($header,$info);
                    837:     if ($action eq 'chgaccess') {
                    838:         $header = '<h3>'.&mt('Allowing others to retrieve file: [_1]',$port_path.$env{'form.currentpath'}.$env{'form.access'}).'</h3>';
                    839:         $info .= &mt('Access to this file by others can be set to be one or more of the following types: public, passphrase-protected or conditional.');
                    840:         $info .= '<br /><ul><li>'.&mt('Public files are available to anyone without the need for login.');
                    841:         $info .= '</li><li>'.&mt('Passphrase-protected files do not require log-in, but will require the viewer to enter the passphrase you set.');
                    842:         $info .= '</li><li>'.&explain_conditionals();
1.174     raeburn   843:         $info .= '</li></ul>'.
1.201     raeburn   844:                   &mt('A listing of files viewable without log-in is available at: ')."<a href=\"/adm/$udom/$uname/aboutme/portfolio\">".&Apache::lonnet::absolute_url($ENV{'SERVER_NAME'})."/adm/$udom/$uname/aboutme/portfolio</a>.<br />";
1.170     raeburn   845:         if ($group eq '') {
1.209   ! weissno   846:             $info .= &mt("For logged in users a 'Display file listing' link will also appear (when there 
        !           847: are viewable files) on your personal homepage:");
1.174     raeburn   848:         } else {
                    849:             $info .= &mt("For logged in users a 'Display file listing' link will also appear (when there are viewable files) on the course information page:");
                    850:         }
1.201     raeburn   851:         $info .= "<br /><a href=\"/adm/$udom/$uname/aboutme\">".&Apache::lonnet::absolute_url($ENV{'SERVER_NAME'})."/adm/$udom/$uname/aboutme</a><br />";
1.174     raeburn   852:         if ($group ne '') {
                    853:             $info .= &mt("Users with privileges to edit course contents may add a course information page to a course using the 'Course Info' button in DOCS").'<br />';
1.170     raeburn   854:         }
                    855:     } else {
                    856:         $header = '<h3>'.&mt('Conditional access controls for file: [_1]',$port_path.$env{'form.currentpath'}.$env{'form.access'}).'</h3>'.
                    857:                   &explain_conditionals().'<br />';
1.142     raeburn   858:     }
1.120     raeburn   859:     if ($can_setacl) {
                    860:         &open_form($r,$url);
                    861:         $r->print($header.$info);
1.187     bisitz    862: 	$r->print('<br />'.&Apache::loncommon::help_open_topic('Portfolio ShareFile SetAccess', &mt('Help on setting up share access')));
                    863: 	$r->print(&Apache::loncommon::help_open_topic('Portfolio ShareFile ChangeSetting', &mt('Help on changing settings')));
                    864: 	$r->print(&Apache::loncommon::help_open_topic('Portfolio ShareFile StopAccess', &mt('Help on removing share access')));
1.170     raeburn   865:         &access_setting_table($r,$url,$file_name,$access_controls{$file_name},
                    866:                               $action);
1.120     raeburn   867:         my $button_text = {
1.108     raeburn   868:                         'continue' => &mt('Proceed'),
1.188     bisitz    869:                         'cancel' => &mt('Return to directory'),
1.108     raeburn   870:                       };
1.137     albertel  871:         &close_form($r,$url,$button_text);
1.120     raeburn   872:     } else {
                    873:         $r->print($header);
                    874:         if ($aclcount) {  
                    875:             $r->print($info);
                    876:         }
1.137     albertel  877:         &view_access_settings($r,$url,$access_controls{$file_name},$aclcount);
1.120     raeburn   878:     }
                    879: }
                    880: 
1.170     raeburn   881: sub explain_conditionals {
                    882:     return
                    883:         &mt('Conditional files are accessible to logged-in users with accounts in the LON-CAPA network, who satisfy the conditions you set.').'<br />'."\n".
                    884:         &mt('The conditions can include affiliation with a particular course, or a user account in a specific domain.').'<br />'."\n".
                    885:         &mt('Alternatively access can be granted to people with specific LON-CAPA usernames and domains.');
                    886: }
                    887: 
1.120     raeburn   888: sub view_access_settings {
1.137     albertel  889:     my ($r,$url,$access_controls,$aclcount) = @_;
1.120     raeburn   890:     my ($showstart,$showend);
                    891:     my %todisplay;
                    892:     foreach my $key (sort(keys(%{$access_controls}))) {
                    893:         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
                    894:         $todisplay{$scope}{$key} = $$access_controls{$key};
                    895:     }
                    896:     if ($aclcount) {
1.188     bisitz    897:         $r->print('<h4>'.&mt('Current access controls defined for this file:').'</h4>');
1.120     raeburn   898:         $r->print(&Apache::loncommon::start_data_table());
                    899:         $r->print(&Apache::loncommon::start_data_table_header_row());
                    900:         $r->print('<th>'.&mt('Access control').'</th><th>'.&mt('Dates available').
                    901:                   '</th><th>'.&mt('Additional information').'</th>');
                    902:         $r->print(&Apache::loncommon::end_data_table_header_row());
                    903:         my $count = 1;
                    904:         my $chg = 'none';
                    905:         &build_access_summary($r,$count,$chg,%todisplay);
                    906:         $r->print(&Apache::loncommon::end_data_table());
                    907:     } else {
1.189     raeburn   908:         $r->print(&mt('No access control settings currently exist for this file.').'<br />');
1.120     raeburn   909:     }
1.153     banghart  910:     my %anchor_fields = (
                    911:         'currentpath' => $env{'form.currentpath'}
                    912:     );
1.188     bisitz    913:     $r->print('<br />'.&make_anchor($url, \%anchor_fields, &mt('Return to directory')));
1.120     raeburn   914:     return;
1.104     raeburn   915: }
                    916: 
1.120     raeburn   917: sub build_access_summary {
                    918:     my ($r,$count,$chg,%todisplay) = @_; 
                    919:     my ($showstart,$showend);
                    920:     my %scope_desc = (
                    921:                       public => 'Public',
                    922:                       guest => 'Passphrase-protected',
                    923:                       domains => 'Conditional: domain-based',
                    924:                       users => 'Conditional: user-based',
                    925:                       course => 'Conditional: course-based',
                    926:                      );
1.170     raeburn   927:     my @allscopes = ('public','guest','domains','users','course');
1.120     raeburn   928:     foreach my $scope (@allscopes) {
                    929:         if ((!(exists($todisplay{$scope}))) || (ref($todisplay{$scope}) ne 'HASH')) {
                    930:             next;
                    931:         }
                    932:         foreach my $key (sort(keys(%{$todisplay{$scope}}))) {
                    933:             if ($count) {
                    934:                 $r->print(&Apache::loncommon::start_data_table_row());
                    935:             }
                    936:             my ($num,$scope,$end,$start) = &unpack_acc_key($key);
                    937:             my $content = $todisplay{$scope}{$key};
                    938:             if ($chg eq 'delete') {
                    939:                 $showstart = &mt('Deleted');
                    940:                 $showend = $showstart;
                    941:             } else {
                    942:                 $showstart = localtime($start);
                    943:                 if ($end == 0) {
                    944:                     $showend = &mt('No end date');
                    945:                 } else {
                    946:                     $showend = localtime($end);
                    947:                 }
                    948:             }
                    949:             $r->print('<td>'.&mt($scope_desc{$scope}));
1.170     raeburn   950:             if ($scope eq 'course') {
1.120     raeburn   951:                 if ($chg ne 'delete') {
                    952:                     my $cid = $content->{'domain'}.'_'.$content->{'number'};
                    953:                     my %course_description = &Apache::lonnet::coursedescription($cid);
                    954:                     $r->print('<br />('.$course_description{'description'}.')');
                    955:                 }
                    956:             }
                    957:             $r->print('</td><td>'.&mt('Start: ').$showstart.
                    958:                   '<br />'.&mt('End: ').$showend.'</td><td>');
                    959:             if ($chg ne 'delete') {
                    960:                 if ($scope eq 'guest') {
                    961:                     $r->print(&mt('Passphrase').': '.$content->{'password'});
1.170     raeburn   962:                 } elsif ($scope eq 'course') {
1.172     raeburn   963:                     $r->print('<table width="100%"><tr>');
1.120     raeburn   964:                     $r->print('<th>'.&mt('Roles').'</th><th>'.
                    965:                           &mt('Access').'</th><th>'.
                    966:                                           &mt('Sections').'</th>');
1.170     raeburn   967:                     $r->print('<th>'.&mt('Groups').'</th>');
1.120     raeburn   968:                     $r->print('</tr>');
                    969:                     foreach my $id (sort(keys(%{$content->{'roles'}}))) {
                    970:                         $r->print('<tr>');
                    971:                         foreach my $item ('role','access','section','group') {
                    972:                             $r->print('<td>');
                    973:                             if ($item eq 'role') {
                    974:                                 my $ucscope = $scope;
                    975:                                 $ucscope =~ s/^(\w)/uc($1)/e;
                    976:                                 my $role_output;
                    977:                                 foreach my $role (@{$content->{'roles'}{$id}{$item}}) {
                    978:                                     if ($role eq 'all') {
                    979:                                         $role_output .= $role.',';
                    980:                                     } elsif ($role =~ /^cr/) {
                    981:                                         $role_output .= (split('/',$role))[3].',';
                    982:                                     } else {
                    983:                                         $role_output .= &Apache::lonnet::plaintext($role,$ucscope).',';
                    984:                                     }
                    985:                                 }
                    986:                                 $role_output =~ s/,$//;
                    987:                                 $r->print($role_output);
                    988:                             } else {
                    989:                                 $r->print(join(',',@{$content->{'roles'}{$id}{$item}}));
                    990:                             }
1.170     raeburn   991:                             $r->print('</td>');
1.120     raeburn   992:                         }
1.170     raeburn   993: 			$r->print('</tr>');
1.120     raeburn   994:                     }
1.170     raeburn   995: 		    $r->print('</table>');
1.120     raeburn   996:                 } elsif ($scope eq 'domains') {
                    997:                     $r->print(&mt('Domains: ').join(',',@{$content->{'dom'}}));
                    998:                 } elsif ($scope eq 'users') {
                    999:                     my $curr_user_list = &sort_users($content->{'users'});
                   1000:                     $r->print(&mt('Users: ').$curr_user_list);
                   1001:                 } else {
                   1002:                     $r->print('&nbsp;');
                   1003:                 }
                   1004:             } else {
                   1005:                 $r->print('&nbsp;');
                   1006:             }
                   1007:             $r->print('</td>');
                   1008:             $r->print(&Apache::loncommon::end_data_table_row());
                   1009:             $count ++;
                   1010:         }
                   1011:     }
                   1012: }
                   1013: 
                   1014: 
1.104     raeburn  1015: sub update_access {
1.137     albertel 1016:     my ($r,$url,$group,$port_path) = @_;
1.104     raeburn  1017:     my $totalprocessed = 0;
                   1018:     my %processing;
                   1019:     my %title  = (
1.108     raeburn  1020:                          'activate' => 'New control(s) added',
                   1021:                          'delete'   => 'Existing control(s) deleted',
                   1022:                          'update'   => 'Existing control(s) modified',
1.104     raeburn  1023:                      );
1.108     raeburn  1024:     my $changes;
1.104     raeburn  1025:     foreach my $chg (sort(keys(%title))) {     
                   1026:         @{$processing{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
                   1027:         $totalprocessed += @{$processing{$chg}};
                   1028:         foreach my $num (@{$processing{$chg}}) {
                   1029:             my $scope = $env{'form.scope_'.$num};
                   1030:             my ($start,$end) = &get_dates_from_form($num);
                   1031:             my $newkey = $num.':'.$scope.'_'.$end.'_'.$start;
                   1032:             if ($chg eq 'delete') {
                   1033:                 $$changes{$chg}{$newkey} = 1;
                   1034:             } else {
                   1035:                 $$changes{$chg}{$newkey} = 
1.108     raeburn  1036:                             &build_access_record($num,$scope,$start,$end,$chg);
1.104     raeburn  1037:             }
                   1038:         }
                   1039:     }
                   1040:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
1.142     raeburn  1041:     $r->print('<h3>'.&mt('Allowing others to retrieve file: [_1]',
1.133     raeburn  1042:               $port_path.$file_name).'</h3>'."\n");
1.138     albertel 1043:     $file_name = &prepend_group($file_name);
1.191     raeburn  1044:     my ($uname,$udom) = &get_name_dom($group);
1.104     raeburn  1045:     my ($errors,$outcome,$deloutcome,$new_values,$translation);
                   1046:     if ($totalprocessed) {
                   1047:         ($outcome,$deloutcome,$new_values,$translation) =
1.108     raeburn  1048:         &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
                   1049:                                                 $uname);
1.104     raeburn  1050:     }
1.108     raeburn  1051:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                   1052:                                                                        $uname);
1.109     albertel 1053:     my %access_controls = 
                   1054: 	&Apache::lonnet::get_access_controls($current_permissions,
                   1055: 					     $group,$file_name);
1.104     raeburn  1056:     if ($totalprocessed) {
                   1057:         if ($outcome eq 'ok') {
                   1058:             my $updated_controls = $access_controls{$file_name};
                   1059:             my ($showstart,$showend);
                   1060:             $r->print(&Apache::loncommon::start_data_table());
1.110     albertel 1061:             $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn  1062:             $r->print('<th>'.&mt('Type of change').'</th><th>'.
                   1063:                       &mt('Access control').'</th><th>'.&mt('Dates available').
                   1064:                       '</th><th>'.&mt('Additional information').'</th>');
1.110     albertel 1065:             $r->print(&Apache::loncommon::end_data_table_header_row());
1.104     raeburn  1066:             foreach my $chg (sort(keys(%processing))) {
                   1067:                 if (@{$processing{$chg}} > 0) {
                   1068:                     if ($chg eq 'delete') {
                   1069:                         if (!($deloutcome eq 'ok')) {
1.116     albertel 1070:                             $errors .='<span class="LC_error">'.
                   1071: 				&mt('A problem occurred deleting access controls: [_1]',$deloutcome).
                   1072: 				'</span>';
1.104     raeburn  1073:                             next;
                   1074:                         }
                   1075:                     }
                   1076:                     my $numchgs = @{$processing{$chg}};
                   1077:                     $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn  1078:                     $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
                   1079:                               '.</td>');
1.104     raeburn  1080:                     my $count = 0;
1.120     raeburn  1081:                     my %todisplay;
1.104     raeburn  1082:                     foreach my $key (sort(keys(%{$$changes{$chg}}))) {
1.120     raeburn  1083:                         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
1.104     raeburn  1084:                         my $newkey = $key;
                   1085:                         if ($chg eq 'activate') {
                   1086:                             $newkey =~ s/^(\d+)/$$translation{$1}/;
                   1087:                         }
1.120     raeburn  1088:                         $todisplay{$scope}{$newkey} = $$updated_controls{$newkey};
1.104     raeburn  1089:                     }
1.120     raeburn  1090:                     &build_access_summary($r,$count,$chg,%todisplay);  
1.104     raeburn  1091:                 }
                   1092:             }
                   1093:             $r->print(&Apache::loncommon::end_data_table());
                   1094:         } else {
                   1095:             if ((@{$processing{'activate'}} > 0) || (@{$processing{'update'}} > 0)) {
1.116     albertel 1096:                 $errors .= '<span class="LC_error">'.
1.179     albertel 1097: 		    &mt('A problem occurred saving access control settings: [_1]',$outcome).
1.116     albertel 1098: 		    '</span>';
1.104     raeburn  1099:             }
                   1100:         }
                   1101:         if ($errors) { 
                   1102:             $r->print($errors);
                   1103:         }
                   1104:     }
1.108     raeburn  1105:     my $allnew = 0;
                   1106:     my $totalnew = 0;
                   1107:     my $status = 'new';
                   1108:     my ($firstitem,$lastitem);
1.170     raeburn  1109:     foreach my $newitem ('course','domains','users') {
1.108     raeburn  1110:         $allnew += $env{'form.new'.$newitem};
                   1111:     }
                   1112:     if ($allnew > 0) {
                   1113:         my $now = time;
                   1114:         my $then = $now + (60*60*24*180); # six months approx.
1.138     albertel 1115:         &open_form($r,$url);
1.170     raeburn  1116:         foreach my $newitem ('course','domains','users') {
1.108     raeburn  1117:             if ($env{'form.new'.$newitem} > 0) {
1.188     bisitz   1118:                 $r->print('<br />'.&mt('Add new <b>[_1]-based</b> access control for portfolio file: <b>[_2]</b>',&mt($newitem),$env{'form.currentpath'}.$env{'form.selectfile'}).'<br /><br />');
1.108     raeburn  1119:                 $firstitem = $totalnew;
                   1120:                 $lastitem = $totalnew + $env{'form.new'.$newitem};
                   1121:                 $totalnew = $lastitem;
                   1122:                 my @numbers;   
                   1123:                 for (my $i=$firstitem; $i<$lastitem; $i++) {
                   1124:                     push (@numbers,$i);
                   1125:                 }
                   1126:                 &display_access_row($r,$status,$newitem,\@numbers,
                   1127:                                     $access_controls{$file_name},$now,$then);
                   1128:             }
                   1129:         }
1.137     albertel 1130:         &close_form($r,$url);
1.108     raeburn  1131:     } else {
1.153     banghart 1132:         my %anchor_fields = (
                   1133:             'currentpath' => $env{'form.currentpath'},
                   1134:             'access' => $env{'form.selectfile'}
                   1135:         );
                   1136:         $r->print('<br />'.&make_anchor($url, \%anchor_fields, &mt('Display all access settings for this file')));
                   1137:         delete $anchor_fields{'access'};
1.188     bisitz   1138:         $r->print('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&make_anchor($url,\%anchor_fields,&mt('Return to directory')));
1.108     raeburn  1139:     }
1.104     raeburn  1140:     return;
                   1141: }
                   1142: 
                   1143: sub build_access_record {
1.108     raeburn  1144:     my ($num,$scope,$start,$end,$chg) = @_;
1.109     albertel 1145:     my $record = {
                   1146: 	type => $scope,
                   1147: 	time => {
                   1148: 	    start => $start,
                   1149: 	    end   => $end
                   1150: 	    },
                   1151: 	    };
                   1152: 		
                   1153:     if ($scope eq 'guest') {	
                   1154:         $record->{'password'} = $env{'form.password'};
1.170     raeburn  1155:     } elsif ($scope eq 'course') {
1.109     albertel 1156:         $record->{'domain'} = $env{'form.crsdom_'.$num};
                   1157: 	$record->{'number'} = $env{'form.crsnum_'.$num};
1.108     raeburn  1158:         my @role_ids;
1.109     albertel 1159:         my @delete_role_ids =
                   1160:             &Apache::loncommon::get_env_multiple('form.delete_role_'.$num);
                   1161: 	my @preserves =
                   1162: 	    &Apache::loncommon::get_env_multiple('form.preserve_role_'.$num);
                   1163: 	if (@delete_role_ids) {
                   1164: 	    foreach my $id (@preserves) {
                   1165: 		if (grep {$_ = $id} (@delete_role_ids)) {
                   1166: 		    next;
                   1167: 		}
                   1168: 		push(@role_ids,$id); 
                   1169: 	    }
                   1170: 	} else {
                   1171: 	    push(@role_ids,@preserves);
                   1172: 	}
                   1173: 
                   1174: 	my $next_id = $env{'form.add_role_'.$num};
                   1175: 	if ($next_id) {
                   1176: 	    push(@role_ids,$next_id);
                   1177: 	}
                   1178: 
1.108     raeburn  1179:         foreach my $id (@role_ids) {
                   1180:             my (@roles,@accesses,@sections,@groups);
                   1181:             if (($id == $next_id) && ($chg eq 'update')) {
1.109     albertel 1182:                 @roles    = split(/,/,$env{'form.role_'.$num.'_'.$next_id});
1.108     raeburn  1183:                 @accesses = split(/,/,$env{'form.access_'.$num.'_'.$next_id});
                   1184:                 @sections = split(/,/,$env{'form.section_'.$num.'_'.$next_id});
1.109     albertel 1185:                 @groups   = split(/,/,$env{'form.group_'.$num.'_'.$next_id});
1.108     raeburn  1186:             } else {
                   1187:                 @roles = &Apache::loncommon::get_env_multiple('form.role_'.$num.'_'.$id);
                   1188:                 @accesses = &Apache::loncommon::get_env_multiple('form.access_'.$num.'_'.$id);
                   1189:                 @sections = &Apache::loncommon::get_env_multiple('form.section_'.$num.'_'.$id);
                   1190:                 @groups = &Apache::loncommon::get_env_multiple('form.group_'.$num.'_'.$id);
                   1191:             }
1.109     albertel 1192: 	    $record->{'roles'}{$id}{'role'}    = \@roles;
                   1193: 	    $record->{'roles'}{$id}{'access'}  = \@accesses;
                   1194: 	    $record->{'roles'}{$id}{'section'} = \@sections;
                   1195: 	    $record->{'roles'}{$id}{'group'}   = \@groups;
1.108     raeburn  1196:         }
                   1197:     } elsif ($scope eq 'domains') {
                   1198:         my @doms = &Apache::loncommon::get_env_multiple('form.dom_'.$num);
1.109     albertel 1199: 	$record->{'dom'} = \@doms;
1.108     raeburn  1200:     } elsif ($scope eq 'users') {
                   1201:         my $userlist = $env{'form.users_'.$num};
1.109     albertel 1202:         $userlist =~ s/\s+//sg;
                   1203: 	my %userhash = map { ($_,1) } (split(/,/,$userlist));
                   1204:         foreach my $user (keys(%userhash)) {
1.108     raeburn  1205:             my ($uname,$udom) = split(/:/,$user);
1.109     albertel 1206: 	    push(@{$record->{'users'}}, {
                   1207: 		'uname' => $uname,
                   1208: 		'udom'  => $udom
                   1209: 		});
                   1210: 	}
1.108     raeburn  1211:     }
1.104     raeburn  1212:     return $record;
                   1213: }
                   1214: 
                   1215: sub get_dates_from_form {
                   1216:     my ($id) = @_;
                   1217:     my $startdate;
                   1218:     my $enddate;
                   1219:     $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$id);
                   1220:     $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$id);
                   1221:     if ( exists ($env{'form.noend_'.$id}) ) {
                   1222:         $enddate = 0;
                   1223:     }
                   1224:     return ($startdate,$enddate);
                   1225: }
                   1226: 
1.108     raeburn  1227: sub sort_users {
1.109     albertel 1228:     my ($users) = @_; 
                   1229:     my @curr_users = map {
                   1230: 	$_->{'uname'}.':'.$_->{'udom'}
                   1231:     } (@{$users});
                   1232:     my $curr_user_list = join(",\n",sort(@curr_users));
1.108     raeburn  1233:     return $curr_user_list;
                   1234: }
                   1235: 
1.104     raeburn  1236: sub access_setting_table {
1.170     raeburn  1237:     my ($r,$url,$filename,$access_controls,$action) = @_;
1.104     raeburn  1238:     my ($public,$publictext);
1.170     raeburn  1239:     $publictext ='Off';
1.104     raeburn  1240:     my ($guest,$guesttext);
1.170     raeburn  1241:     $guesttext = 'Off';
1.104     raeburn  1242:     my @courses = ();
                   1243:     my @domains = ();
                   1244:     my @users = ();
                   1245:     my $now = time;
                   1246:     my $then = $now + (60*60*24*180); # six months approx.
1.108     raeburn  1247:     my ($num,$scope,$publicnum,$guestnum);
1.170     raeburn  1248:     my (%acl_count,%end,%start,%conditionals);
1.104     raeburn  1249:     foreach my $key (sort(keys(%{$access_controls}))) {
1.108     raeburn  1250:         ($num,$scope,$end{$key},$start{$key}) = &unpack_acc_key($key);
1.104     raeburn  1251:         if ($scope eq 'public') {
                   1252:             $public = $key;
1.108     raeburn  1253:             $publicnum = $num;
                   1254:             $publictext = &acl_status($start{$key},$end{$key},$now);
                   1255:         } elsif ($scope eq 'guest') {
                   1256:             $guest=$key;
                   1257:             $guestnum = $num;  
                   1258:             $guesttext = &acl_status($start{$key},$end{$key},$now);
1.170     raeburn  1259:         } else {
                   1260:             $conditionals{$scope}{$key} = $$access_controls{$key};
                   1261:             if ($scope eq 'course') {
                   1262:                 push(@courses,$key);
                   1263:             } elsif ($scope eq 'domains') {
                   1264:                 push(@domains,$key);
                   1265:             } elsif ($scope eq 'users') {
                   1266:                 push(@users,$key);
                   1267:             }
1.104     raeburn  1268:         }
1.108     raeburn  1269:         $acl_count{$scope} ++;
1.104     raeburn  1270:     }
1.108     raeburn  1271:     $r->print('<table border="0"><tr><td valign="top">');
1.170     raeburn  1272:     if ($action eq 'chgaccess') {
                   1273:         &standard_settings($r,$now,$then,$url,$filename,\%acl_count,\%start,
                   1274:                            \%end,$public,$publicnum,$publictext,$guest,$guestnum,
                   1275:                            $guesttext,$access_controls,%conditionals);
                   1276:     } else {
                   1277:         &condition_setting($r,$access_controls,$now,$then,\%acl_count,
                   1278:                            \@domains,\@users,\@courses);
                   1279:     }
                   1280:     $r->print('</td></tr></table>');
                   1281: }
                   1282: 
                   1283: sub standard_settings {
                   1284:     my ($r,$now,$then,$url,$filename,$acl_count,$start,$end,$public,$publicnum,
                   1285:       $publictext,$guest,$guestnum,$guesttext,$access_controls,%conditionals)=@_;
1.187     bisitz   1286:     $r->print('<h3>'.&mt('Public access: [_1]',&mt($publictext)).'</h3>');
1.104     raeburn  1287:     $r->print(&Apache::loncommon::start_data_table());
1.110     albertel 1288:     $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn  1289:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').'</th>');
1.110     albertel 1290:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.104     raeburn  1291:     $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn  1292:     if ($public) {
                   1293:         $r->print('<td>'.&actionbox('old',$publicnum,'public').'</td><td>'.
1.170     raeburn  1294:              &dateboxes($publicnum,$start->{$public},$end->{$public}).'</td>');
1.108     raeburn  1295:     } else {
                   1296:         $r->print('<td>'.&actionbox('new','0','public').'</td><td>'.
                   1297:                   &dateboxes('0',$now,$then).'</td>');
                   1298:     }
                   1299:     $r->print(&Apache::loncommon::end_data_table_row());
                   1300:     $r->print(&Apache::loncommon::end_data_table());
                   1301:     $r->print('</td><td width="40">&nbsp;</td><td valign="top">');
1.187     bisitz   1302:     $r->print('<h3>'.&mt('Passphrase-protected access: [_1]',&mt($guesttext)).'</h3>');
1.104     raeburn  1303:     $r->print(&Apache::loncommon::start_data_table());
1.110     albertel 1304:     $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn  1305:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').
1.120     raeburn  1306:               '</th><th>'. &mt('Passphrase').'</th>');
1.110     albertel 1307:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.108     raeburn  1308:     $r->print(&Apache::loncommon::start_data_table_row());
                   1309:     my $passwd;
                   1310:     if ($guest) {
1.109     albertel 1311:         $passwd = $$access_controls{$guest}{'password'};
1.108     raeburn  1312:         $r->print('<td>'.&actionbox('old',$guestnum,'guest').'</td><td>'.
1.170     raeburn  1313:               &dateboxes($guestnum,$start->{$guest},$end->{$guest}).'</td>');
1.108     raeburn  1314:     } else {
                   1315:         $r->print('<td>'.&actionbox('new','1','guest').'</td><td>'.
                   1316:                   &dateboxes('1',$now,$then).'</td>');
1.104     raeburn  1317:     }
1.108     raeburn  1318:     $r->print('<td><input type="text" size="15" name="password" value="'.
                   1319:               $passwd.'" /></td>');
1.104     raeburn  1320:     $r->print(&Apache::loncommon::end_data_table_row());
                   1321:     $r->print(&Apache::loncommon::end_data_table());
1.170     raeburn  1322:     $r->print('</td></tr><tr><td colspan="3">&nbsp;</td></tr>'.
                   1323:               '<tr><td colspan="3">');
                   1324:     my $numconditionals = 0;
                   1325:     my $conditionstext;
                   1326:     my %cond_status;
                   1327:     foreach my $scope ('domains','users','course') {
                   1328:         $numconditionals += $acl_count->{$scope}; 
                   1329:         if ($acl_count->{$scope} > 0) {
                   1330:             if ($conditionstext ne 'Active') { 
                   1331:                 foreach my $key (keys(%{$conditionals{$scope}})) {
                   1332:                     $conditionstext = &acl_status($start->{$key},$end->{$key},$now);
                   1333:                     if ($conditionstext eq 'Active') {
                   1334:                        last;
                   1335:                     }
                   1336:                 }
                   1337:             }
                   1338:         }
                   1339:     }
                   1340:     if ($conditionstext eq '') {
                   1341:         $conditionstext = 'Off';
                   1342:     }
                   1343:     my %anchor_fields = (
                   1344:             'access' => $env{'form.selectfile'},
                   1345:             'action' => 'chgconditions',
                   1346:             'currentpath' => $env{'form.currentpath'},
                   1347:         );
1.187     bisitz   1348:     $r->print('<h3>'.&mt('Conditional access: [_1]',&mt($conditionstext)).'</h3>');
1.170     raeburn  1349:     if ($numconditionals > 0) {
                   1350:         my $count = 1;
                   1351:         my $chg = 'none';
                   1352:         $r->print(&mt('You have previously set [_1] conditional access controls.',$numconditionals).' '.&make_anchor($url,\%anchor_fields,&mt('Change Conditions')).'<br /><br />');
                   1353:         $r->print(&Apache::loncommon::start_data_table());
                   1354:         $r->print(&Apache::loncommon::start_data_table_header_row());
                   1355:         $r->print('<th>'.&mt('Access control').'</th><th>'.&mt('Dates available').
                   1356:                   '</th><th>'.&mt('Additional information').'</th>');
                   1357:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   1358:         &build_access_summary($r,$count,$chg,%conditionals);
                   1359:         $r->print(&Apache::loncommon::end_data_table());
                   1360:     } else {
                   1361:         $r->print(&make_anchor($url,\%anchor_fields,&mt('Add conditional access')).' '.&mt('based on domain, username, or course affiliation.'));
                   1362:     }
                   1363: }
                   1364: 
                   1365: sub condition_setting {
                   1366:     my ($r,$access_controls,$now,$then,$acl_count,$domains,$users,$courses) = @_;
                   1367:     $r->print('<tr><td valign="top">');
                   1368:     &access_element($r,'domains',$acl_count,$domains,$access_controls,$now,$then);
1.158     albertel 1369:     $r->print('</td><td>&nbsp;</td><td valign="top">');
1.170     raeburn  1370:     &access_element($r,'users',$acl_count,$users,$access_controls,$now,$then);
1.158     albertel 1371:     $r->print('</td></tr><tr><td colspan="3"></td></tr><tr>');
1.170     raeburn  1372:     if ($acl_count->{course} > 0) {
1.158     albertel 1373:         $r->print('<td colspan="3" valign="top">');
                   1374:     } else {
                   1375:         $r->print('<td valign="top">');
                   1376:     }
1.170     raeburn  1377:     &access_element($r,'course',$acl_count,$courses,$access_controls,$now,$then);
1.158     albertel 1378:     $r->print('</td>');
1.108     raeburn  1379:     $r->print('</td></tr></table>');
                   1380: }
                   1381: 
                   1382: sub acl_status {
                   1383:     my ($start,$end,$now) = @_;
                   1384:     if ($start > $now) {
1.170     raeburn  1385:         return 'Inactive';
1.108     raeburn  1386:     }
                   1387:     if ($end && $end<$now) {
1.170     raeburn  1388:         return 'Inactive';
1.108     raeburn  1389:     }
1.170     raeburn  1390:     return 'Active';
1.108     raeburn  1391: }
                   1392: 
                   1393: sub access_element {
                   1394:     my ($r,$type,$acl_count,$items,$access_controls,$now,$then) = @_;
                   1395:     my $title = $type;
                   1396:     $title =~ s/s$//;
                   1397:     $title =~ s/^(\w)/uc($1)/e;
1.188     bisitz   1398:     $r->print('<h3>'.&mt('[_1]-based conditional access: ',&mt($title)));
1.108     raeburn  1399:     if ($$acl_count{$type}) {
                   1400:         $r->print($$acl_count{$type}.' ');
                   1401:         if ($$acl_count{$type} > 1) {
                   1402:             $r->print(&mt('conditions'));
                   1403:         } else {
                   1404:             $r->print(&mt('condition'));
                   1405:         }
                   1406:     } else {
                   1407:         $r->print(&mt('Off'));
                   1408:     }
                   1409:     $r->print('</h3>');
                   1410:     &display_access_row($r,'old',$type,$items,$access_controls,$now,$then);
                   1411:     return;
                   1412: }
                   1413: 
                   1414: sub display_access_row {
                   1415:     my ($r,$status,$type,$items,$access_controls,$now,$then) = @_;
                   1416:     if (@{$items} > 0) {
                   1417:         my @all_doms;
                   1418:         my $colspan = 3;
                   1419:         my $uctype = $type;
                   1420:         $uctype =~ s/^(\w)/uc($1)/e;
                   1421:         $r->print(&Apache::loncommon::start_data_table());
1.110     albertel 1422:         $r->print(&Apache::loncommon::start_data_table_header_row());
                   1423:         $r->print('<th>'.&mt('Action?').'</th><th>'.&mt($uctype).'</th><th>'.
1.108     raeburn  1424:               &mt('Dates available').'</th>');
1.172     raeburn  1425:         if ($type eq 'course' && $status eq 'old') {
1.108     raeburn  1426:             $r->print('<th>'.&mt('Allowed [_1] member affiliations',$type).
                   1427:                       '</th>');
                   1428:             $colspan ++;
                   1429:         } elsif ($type eq 'domains') {
1.178     albertel 1430:             @all_doms = sort(&Apache::lonnet::all_domains());
1.108     raeburn  1431:         }
1.110     albertel 1432:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.108     raeburn  1433:         foreach my $key (@{$items}) {
1.118     albertel 1434: 	    $r->print(&Apache::loncommon::start_data_table_row());
1.170     raeburn  1435:             if ($type eq 'course') {
1.118     albertel 1436:                 &course_row($r,$status,$type,$key,$access_controls,$now,$then);
1.108     raeburn  1437:             } elsif ($type eq 'domains') {
                   1438:                 &domains_row($r,$status,$key,\@all_doms,$access_controls,$now,
                   1439:                             $then);
                   1440:             } elsif ($type eq 'users') {
                   1441:                 &users_row($r,$status,$key,$access_controls,$now,$then);
                   1442:             }
1.118     albertel 1443: 	    $r->print(&Apache::loncommon::end_data_table_row());
1.108     raeburn  1444:         }
                   1445:         if ($status eq 'old') {
1.111     albertel 1446: 	    $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn  1447:             $r->print('<td colspan="',$colspan.'">'.&additional_item($type).
                   1448:                       '</td>');
1.111     albertel 1449: 	    $r->print(&Apache::loncommon::end_data_table_row());
1.108     raeburn  1450:         }
                   1451:         $r->print(&Apache::loncommon::end_data_table());
                   1452:     } else {
1.188     bisitz   1453:         $r->print(&mt('No [_1]-based conditions defined.',&mt($type)).'<br />'
1.187     bisitz   1454:                   .&additional_item($type));
1.108     raeburn  1455:     }
                   1456:     return;
                   1457: }
                   1458: 
1.110     albertel 1459: sub course_js {
                   1460:     return qq|
1.108     raeburn  1461: <script type="text/javascript">
1.172     raeburn  1462: function setRoleOptions(num,roleid,cdom,cnum,type) {
                   1463:     updateIndexNum = getIndexByValue('update',num);
                   1464:     var addItem = 'add_role_'+num;
                   1465:     var addIndexNum = getIndexByName(addItem);
                   1466:     if (document.portform.elements[addItem].checked) {
                   1467:         document.portform.elements[updateIndexNum].checked = true;
                   1468:         var url = '/adm/portfolio?action=rolepicker&setroles='+num+'_'+roleid+'&cnum='+cnum+'&cdom='+cdom+'&type='+type;
1.108     raeburn  1469:         var title = 'Roles_Chooser';
                   1470:         var options = 'scrollbars=1,resizable=1,menubar=0';
                   1471:         options += ',width=700,height=600';
                   1472:         rolebrowser = open(url,title,options,'1');
                   1473:         rolebrowser.focus();
                   1474:     } else {
1.172     raeburn  1475:         addArray = new Array ('role','access','section','group');
                   1476:         for (var j=0;j<addArray.length;j++) {
                   1477:             var itemIndex = getIndexByName(addArray[j]+'_'+num+'_'+roleid);
                   1478:             document.portform.elements[itemIndex].value = '';
1.108     raeburn  1479:         }
                   1480:     }
                   1481: }
                   1482: 
1.172     raeburn  1483: function getIndexByName(item) {
1.108     raeburn  1484:     for (var i=0;i<document.portform.elements.length;i++) {
1.172     raeburn  1485:         if (document.portform.elements[i].name == item) {
1.108     raeburn  1486:             return i;
                   1487:         }
                   1488:     }
                   1489:     return -1;
                   1490: }
                   1491: 
1.172     raeburn  1492: function getIndexByValue(name,value) {
1.108     raeburn  1493:     for (var i=0;i<document.portform.elements.length;i++) {
                   1494:         if (document.portform.elements[i].name == name && document.portform.elements[i].value == value) {
                   1495:             return i;
                   1496:         }
                   1497:     }
                   1498:     return -1;
                   1499: }
                   1500: 
                   1501: </script>
1.110     albertel 1502: |;
                   1503: }
                   1504: 
                   1505: sub course_row {
1.118     albertel 1506:     my ($r,$status,$type,$item,$access_controls,$now,$then) = @_;
1.115     raeburn  1507:     my $content;
1.110     albertel 1508:     my $defdom = $env{'user.domain'};
                   1509:     if ($status eq 'old') {
1.115     raeburn  1510:         $content = $$access_controls{$item}; 
                   1511:         $defdom =  $content->{'domain'};
1.110     albertel 1512:     }
                   1513:     my $js = &Apache::loncommon::coursebrowser_javascript($defdom)
                   1514: 	.&course_js();
1.120     raeburn  1515:     my $uctype = $type;
                   1516:     $uctype =~ s/^(\w)/uc($1)/e;
1.108     raeburn  1517:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1518:                                                     $type);
1.110     albertel 1519:     $r->print('<td>'.$js.&actionbox($status,$num,$scope).'</td>');
1.108     raeburn  1520:     if ($status eq 'old') {
1.115     raeburn  1521:         my $cid = $content->{'domain'}.'_'.$content->{'number'};
1.108     raeburn  1522:         my %course_description = &Apache::lonnet::coursedescription($cid);
1.115     raeburn  1523:         $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  1524:     } elsif ($status eq 'new') {
1.172     raeburn  1525:         $r->print('<td>'.&Apache::loncommon::selectcourse_link('portform','crsnum_'.$num,'crsdom_'.$num,'description_'.$num,$num.'_1',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>');
1.108     raeburn  1526:     }
1.172     raeburn  1527:     $r->print('<td>'.&dateboxes($num,$start,$end));
                   1528:     my $newrole_id = 1;
1.108     raeburn  1529:     if ($status eq 'old') {
1.172     raeburn  1530:         $r->print('</td><td>');
1.108     raeburn  1531:         my $max_id = 0;
1.172     raeburn  1532:         if (keys(%{$content->{'roles'}}) > 0) {
                   1533:             $r->print('<table><tr><th>'.&mt('Action').'</th>'.
                   1534:                       '<th>'.&mt('Roles').'</th>'.
                   1535:                       '<th>'.&mt('Access').'</th>'.
                   1536:                       '<th>'.&mt('Sections').'</th>'.
                   1537:                       '<th>'.&mt('Groups').'</th></tr>');
                   1538:             foreach my $role_id (sort(keys(%{$content->{'roles'}}))) {
                   1539:                 if ($role_id > $max_id) {
                   1540:                     $max_id = $role_id;
                   1541:                 }
                   1542:                 $max_id ++;
                   1543:                 my $role_selects = &role_selectors($num,$role_id,$type,$content,'display');
1.200     bisitz   1544:                 $r->print('<tr><td><span class="LC_nobreak"><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.172     raeburn  1545:             }
                   1546:             $r->print('</table>');
                   1547:         }
                   1548:         $r->print('<br />'.&mt('Add a roles-based condition').
                   1549:                   '&nbsp;<input type="checkbox" name ="add_role_'.
                   1550:                   $num.'" onClick="javascript:setRoleOptions('."'$num',
                   1551:                   '$max_id','$content->{'domain'}','$content->{'number'}',
                   1552:                   '$uctype'".')" value="" />');
                   1553:         $newrole_id = $max_id;
                   1554:     } else {
                   1555:         $r->print('<input type="hidden" name ="add_role_'.$num.'" value="" />');
1.108     raeburn  1556:     }
1.172     raeburn  1557:     $r->print(&add_course_role($num,$newrole_id));
                   1558:     $r->print('</td>');
1.108     raeburn  1559:     return;
                   1560: }
                   1561: 
1.172     raeburn  1562: sub add_course_role {
                   1563:     my ($num,$max_id) = @_;
                   1564:     my $output;
                   1565:     $output .='<input type="hidden" name="role_'.$num.'_'.$max_id.'" />'.
                   1566:               '<input type="hidden" name="access_'.$num.'_'.$max_id.'" />'.
                   1567:               '<input type="hidden" name="section_'.$num.'_'.$max_id.'" />'.
                   1568:               '<input type="hidden" name="group_'.$num.'_'.$max_id.'" />';
                   1569:     return $output;
                   1570: }
                   1571: 
1.108     raeburn  1572: sub domains_row {
                   1573:     my ($r,$status,$item,$all_doms,$access_controls,$now,$then) = @_;
                   1574:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1575:                                                     'domains');
1.207     bisitz   1576:     my $dom_select = '<select name="dom_'.$num.'" size="4" multiple="multiple">'.
1.108     raeburn  1577:                      ' <option value="">'.&mt('Please select').'</option>';
                   1578:     if ($status eq 'old') {
1.109     albertel 1579:         my $content =  $$access_controls{$item};
                   1580: 	foreach my $dom (@{$all_doms}) {
                   1581:             if ((@{$content->{'dom'}} > 0) 
                   1582: 		&& (grep(/^\Q$dom\E$/,@{$content->{'dom'}}))) {
1.108     raeburn  1583:                 $dom_select .= '<option value="'.$dom.'" selected>'.
                   1584:                                $dom.'</option>';
                   1585:             } else {
                   1586:                 $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
                   1587:             }
                   1588:         }
                   1589:     } else {
                   1590:         foreach my $dom (@{$all_doms}) {
                   1591:             $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
                   1592:         }
                   1593:     }
1.112     albertel 1594:     $dom_select .= '</select>';
1.108     raeburn  1595:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.$dom_select.
                   1596:               '</td><td>'.&dateboxes($num,$start,$end).'</td>');
                   1597: }
                   1598: 
                   1599: sub users_row {
                   1600:     my ($r,$status,$item,$access_controls,$now,$then) = @_;
                   1601:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1602:                                                     'users');
                   1603:     my $curr_user_list;
                   1604:     if ($status eq 'old') {
1.109     albertel 1605:         my $content = $$access_controls{$item};
                   1606:         $curr_user_list = &sort_users($content->{'users'});
1.108     raeburn  1607:     }
                   1608:     $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>');
                   1609: }
                   1610: 
                   1611: sub additional_item {
                   1612:     my ($type) = @_;
1.188     bisitz   1613:     my $output = &mt('Add new [_1] condition(s)?',&mt($type)).'&nbsp;'.&mt('Number to add: ').'<input type="text" name="new'.$type.'" size="3" value="0" />';
1.108     raeburn  1614:     return $output;
                   1615: }
                   1616: 
                   1617: sub actionbox {
                   1618:     my ($status,$num,$scope) = @_;
1.200     bisitz   1619:     my $output = '<span class="LC_nobreak"><label>';
1.108     raeburn  1620:     if ($status eq 'new') {
1.170     raeburn  1621:         my $checkstate;
                   1622:         if ($scope eq 'domains' || $scope eq 'users' || $scope eq 'course') {
                   1623:             $checkstate = 'checked="checked"';
                   1624:         }
                   1625:         $output .= '<input type="checkbox" name="activate" value="'.$num.'" '.
                   1626:                    $checkstate.'  />'.
1.108     raeburn  1627:         &mt('Activate');
                   1628:     } else {
                   1629:         $output .= '<input type="checkbox" name="delete" value="'.$num.
1.200     bisitz   1630:                    '" />'.&mt('Delete').'</label></span><br /><span class="LC_nobreak">'.
1.108     raeburn  1631:                    '<label><input type="checkbox" name="update" value="'.
                   1632:                    $num.'" />'.&mt('Update');
                   1633:     }
1.112     albertel 1634:     $output .= '</label></span><input type="hidden" name="scope_'.$num.                '" value="'.$scope.'" />';
1.108     raeburn  1635:     return $output;
                   1636: }
                   1637:                                                                                    
                   1638: sub dateboxes {
                   1639:     my ($num,$start,$end) = @_;
                   1640:     my $noend;
                   1641:     if ($end == 0) {
                   1642:         $noend = 'checked="checked"';
                   1643:     }
                   1644:     my $startdate = &Apache::lonhtmlcommon::date_setter('portform',
                   1645:                            'startdate_'.$num,$start,undef,undef,undef,1,undef,
                   1646:                             undef,undef,1);
                   1647:     my $enddate = &Apache::lonhtmlcommon::date_setter('portform',
                   1648:                                'enddate_'.$num,$end,undef,undef,undef,1,undef,
1.200     bisitz   1649:                                 undef,undef,1). '&nbsp;&nbsp;<span class="LC_nobreak"><label>'.
1.108     raeburn  1650:                                 '<input type="checkbox" name="noend_'.
                   1651:                                 $num.'" '.$noend.' />'.&mt('No end date').
1.112     albertel 1652:                                 '</label></span>';
1.108     raeburn  1653:                                                                                    
                   1654:     my $output = &mt('Start: ').$startdate.'<br />'.&mt('End: ').$enddate;
                   1655:     return $output;
                   1656: }
                   1657: 
                   1658: sub unpack_acc_key {
                   1659:     my ($acc_key) = @_;
                   1660:     my ($num,$scope,$end,$start) = ($acc_key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   1661:     return ($num,$scope,$end,$start);
                   1662: }
                   1663: 
                   1664: sub set_identifiers {
                   1665:     my ($status,$item,$now,$then,$scope) = @_;
                   1666:     if ($status eq 'old') {
                   1667:         return(&unpack_acc_key($item));
                   1668:     } else {
                   1669:         return($item,$scope,$then,$now);
                   1670:     }
                   1671: } 
                   1672: 
                   1673: sub role_selectors {
1.172     raeburn  1674:     my ($num,$role_id,$type,$content,$caller) = @_;
1.108     raeburn  1675:     my ($output,$cdom,$cnum,$longid);
                   1676:     if ($caller eq 'display') {
                   1677:         $longid = '_'.$num.'_'.$role_id;
1.172     raeburn  1678:         $cdom = $$content{'domain'};
                   1679:         $cnum = $$content{'number'};
1.108     raeburn  1680:     } elsif ($caller eq 'rolepicker') {
                   1681:          $cdom = $env{'form.cdom'};
                   1682:          $cnum = $env{'form.cnum'};
                   1683:     }
1.120     raeburn  1684:     my $uctype = $type;
                   1685:     $uctype =~ s/^(\w)/uc($1)/e;
1.108     raeburn  1686:     my ($sections,$groups,$allroles,$rolehash,$accesshash) =
1.120     raeburn  1687:             &Apache::loncommon::get_secgrprole_info($cdom,$cnum,1,$uctype);
1.108     raeburn  1688:     if (!@{$sections}) {
                   1689:         @{$sections} = ('none');
                   1690:     } else {
                   1691:         unshift(@{$sections},('all','none'));
                   1692:     }
                   1693:     if (!@{$groups}) {
                   1694:         @{$groups} = ('none');
                   1695:     } else {
                   1696:         unshift(@{$groups},('all','none'));
                   1697:     }
                   1698:     my @allacesses = sort(keys(%{$accesshash}));
                   1699:     my (%sectionhash,%grouphash);
                   1700:     foreach my $sec (@{$sections}) {
                   1701:         $sectionhash{$sec} = $sec;
                   1702:     }
                   1703:     foreach my $grp (@{$groups}) {
                   1704:         $grouphash{$grp} = $grp;
                   1705:     }
                   1706:     my %lookup = (
                   1707:                    'role' => $rolehash,
                   1708:                    'access' => $accesshash,
                   1709:                    'section' => \%sectionhash,
                   1710:                    'group' => \%grouphash,
                   1711:                  );
                   1712:     my @allaccesses = sort(keys(%{$accesshash}));
                   1713:     my %allitems = (
                   1714:                     'role' => $allroles,
                   1715:                     'access' => \@allaccesses,
                   1716:                     'section' => $sections,
1.172     raeburn  1717:                     'group' => $groups,
1.108     raeburn  1718:                    );
                   1719:     foreach my $item ('role','access','section','group') {
1.207     bisitz   1720:         $output .= '<td><select name="'.$item.$longid.'" multiple="multiple" size="4">'."\n";
1.108     raeburn  1721:         foreach my $entry (@{$allitems{$item}}) {
                   1722:             if ($caller eq 'display') {
                   1723:                 if ((@{$$content{'roles'}{$role_id}{$item}} > 0) && 
                   1724:                     (grep(/^\Q$entry\E$/,@{$$content{'roles'}{$role_id}{$item}}))) {
                   1725:                     $output .= '  <option value="'.$entry.'" selected>'.
                   1726:                                   $lookup{$item}{$entry}.'</option>';
                   1727:                     next;
                   1728:                 }
                   1729:             }
                   1730:             $output .= '  <option value="'.$entry.'">'.
                   1731:                        $lookup{$item}{$entry}.'</option>';
                   1732:         }
                   1733:         $output .= '</select>';
                   1734:     }
                   1735:     $output .= '</td>';
                   1736:     return $output;
                   1737: }
                   1738: 
                   1739: sub role_options_window {
                   1740:     my ($r) = @_;
                   1741:     my $type = $env{'form.type'};
1.172     raeburn  1742:     my $rolenum = $env{'form.setroles'};
                   1743:     my ($num,$role_id) = ($rolenum =~ /^([\d_]+)_(\d+)$/);
                   1744:     my $role_elements;
                   1745:     foreach my $item ('role','access','section','group') {
                   1746:         $role_elements .= "'".$item.'_'.$rolenum."',";
                   1747:     }
                   1748:     $role_elements =~ s/,$//; 
                   1749:     my $role_selects = &role_selectors($num,$role_id,$type,undef,
                   1750:                                        'rolepicker');
1.108     raeburn  1751:     $r->print(<<"END_SCRIPT");
                   1752: <script type="text/javascript">
                   1753: function setRoles() {
1.172     raeburn  1754:     var role_elements = new Array($role_elements);
                   1755:     for (var i=0; i<role_elements.length; i++) {
1.108     raeburn  1756:         var copylist = '';
                   1757:         for (var j=0; j<document.rolepicker.elements[i].length; j++) {
                   1758:             if (document.rolepicker.elements[i].options[j].selected) {
                   1759:                 copylist = copylist + document.rolepicker.elements[i].options[j].value + ',';
                   1760:             }
                   1761:         }
                   1762:         copylist = copylist.substr(0,copylist.length-1);
1.172     raeburn  1763:         var openerItem = getIndexByName(role_elements[i]);
                   1764:         opener.document.portform.elements[openerItem].value = copylist; 
1.108     raeburn  1765:     }
1.172     raeburn  1766:     var roleAdder = getIndexByName('add_role_$num');
                   1767:     opener.document.portform.elements[roleAdder].value = '$role_id';
1.108     raeburn  1768:     self.close();
                   1769: }
1.172     raeburn  1770: 
                   1771: function getIndexByName(item) {
                   1772:     for (var i=0;i<opener.document.portform.elements.length;i++) {
                   1773:         if (opener.document.portform.elements[i].name == item) {
                   1774:             return i;
                   1775:         }
                   1776:     }
                   1777:     return -1;
                   1778: }
                   1779: 
1.108     raeburn  1780: </script>
                   1781: END_SCRIPT
                   1782:     $r->print(&mt('Select roles, course status, section(s) and group(s) for users who will be able to access the portfolio file.'));
1.170     raeburn  1783:     $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('Groups').'</th></tr><tr>'.$role_selects.'</tr></table><br /><input type="button" name="rolepickbutton" value="Save selections" onclick="setRoles()" />');
1.108     raeburn  1784:     return;
1.104     raeburn  1785: }
                   1786: 
1.47      banghart 1787: sub select_files {
1.138     albertel 1788:     my ($r) = @_;
1.82      albertel 1789:     if ($env{'form.continue'} eq 'true') {
1.60      banghart 1790:         # here we update the selections for the currentpath
                   1791:         # eventually, have to handle removing those not checked, but . . . 
1.83      banghart 1792:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
                   1793:         if (scalar(@items)){
1.85      banghart 1794:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
1.83      banghart 1795:         }
1.62      banghart 1796:     } else {
                   1797:             #empty the file for a fresh start
1.83      banghart 1798:             &Apache::lonnet::clear_selected_files($env{'user.name'});
1.62      banghart 1799:     }
1.82      albertel 1800:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
1.62      banghart 1801:     my $java_files = join ",", @files;
                   1802:     if ($java_files) {
                   1803:         $java_files.=',';
1.60      banghart 1804:     }
1.63      banghart 1805:     my $javascript =(<<ENDSMP);
1.113     albertel 1806:         <script type="text/javascript">
1.48      banghart 1807:         function finishSelect() {
1.62      banghart 1808: ENDSMP
1.63      banghart 1809:     $javascript .= 'fileList = "'.$java_files.'";';
                   1810:     $javascript .= (<<ENDSMP);
1.49      banghart 1811:             for (i=0;i<document.forms.checkselect.length;i++) { 
                   1812:                 if (document.forms.checkselect[i].checked){
1.54      banghart 1813:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
1.49      banghart 1814:                 }
                   1815:             }
1.186     albertel 1816:             var hwfield = opener.document.getElementsByName('$env{'form.fieldname'}');
                   1817:             hwfield[0].value = fileList;
1.48      banghart 1818:             self.close();
                   1819:         }
                   1820:         </script>
                   1821: ENDSMP
1.63      banghart 1822:     $r->print($javascript);
1.198     bisitz   1823:     $r->print("<h1>".&mt('Select portfolio files')."</h1>");
1.88      albertel 1824:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
                   1825:     if (@otherfiles) {
1.198     bisitz   1826: 	$r->print(&Apache::loncommon::start_data_table()
                   1827:                  .&Apache::loncommon::start_data_table_header_row()
                   1828:                  .'<th>'.&mt('Files selected from other directories:')."</th>"
                   1829:                  .&Apache::loncommon::end_data_table_header_row()
                   1830:         );
1.88      albertel 1831: 	foreach my $file (@otherfiles) {
1.198     bisitz   1832: 	    $r->print(&Apache::loncommon::start_data_table_row()
                   1833:                      .'<td>'.$file."</td>"
                   1834:                      .&Apache::loncommon::end_data_table_row()
                   1835:             );
1.88      albertel 1836: 	}
1.198     bisitz   1837:         $r->print(&Apache::loncommon::end_data_table()
                   1838:                  .'<br />'
                   1839:         );
1.60      banghart 1840:     }
1.198     bisitz   1841:     $r->print('<div>'
                   1842:              .&mt('Check as many files as you wish in response to the problem:')
                   1843:              .'</div>'
                   1844:     );
1.47      banghart 1845: }
1.138     albertel 1846: 
1.176     albertel 1847: 
                   1848: sub check_for_upload {
                   1849:     my ($path,$fname,$group,$element) = @_;
1.174     raeburn  1850:     my $disk_quota = &get_quota($group);
1.176     albertel 1851:     my $filesize = (length($env{'form.'.$element})) / 1000; #express in k (1024?)
                   1852:     my $portfolio_root = &get_portfolio_root();
                   1853:     my $port_path = &get_port_path();
1.191     raeburn  1854:     my ($uname,$udom) = &get_name_dom($group);
1.38      banghart 1855:     # Fixme --- Move the checking for existing file to LOND error return
1.191     raeburn  1856:     my @dir_list=&get_dir_list($portfolio_root,$path,$group);
1.34      banghart 1857:     my $found_file = 0;
1.76      banghart 1858:     my $locked_file = 0;
1.33      banghart 1859:     foreach my $line (@dir_list) {
1.76      banghart 1860:         my ($file_name)=split(/\&/,$line,2);
                   1861:         if ($file_name eq $fname){
1.176     albertel 1862:             $file_name = $path.$file_name;
1.138     albertel 1863:             $file_name = &prepend_group($file_name);
1.33      banghart 1864:             $found_file = 1;
1.102     raeburn  1865:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.76      banghart 1866:                 $locked_file = 1;
                   1867:             } 
1.33      banghart 1868:         }
                   1869:     }
1.191     raeburn  1870:     my $getpropath = 1;
                   1871:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
1.176     albertel 1872: 
1.87      albertel 1873:     if (($current_disk_usage + $filesize) > $disk_quota){
1.185     banghart 1874:         my $msg = '<span class="LC_error">'.
                   1875:                 &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</span>'.
                   1876:                   '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage);
1.176     albertel 1877: 	return ('will_exceed_quota',$msg);
                   1878:     } elsif ($found_file) {
                   1879:         if ($locked_file) {
1.185     banghart 1880:             my $msg = '<span class="LC_error">';
1.188     bisitz   1881:             $msg .= &mt('Unable to upload [_1]. A locked file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>','<span class="LC_filename">'.$port_path.$env{'form.currentpath'}.'</span>');
1.185     banghart 1882:             $msg .= '</span><br />';
                   1883:             $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
1.176     albertel 1884: 	    return ('file_locked',$msg);
                   1885: 	} else {
1.185     banghart 1886:             my $msg = '<span class="LC_error">';
                   1887:             $msg .= &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
                   1888:             $msg .= '</span>';
                   1889:             $msg .= '<br />';
                   1890:             $msg .= &mt('To upload, rename or delete existing [_1] in [_2].','<span class="LC_filename">'.$fname.'</span>', $port_path.$env{'form.currentpath'});
1.176     albertel 1891: 	    return ('file_exists',$msg);
                   1892: 	}
                   1893:     }
                   1894: }
                   1895: 
                   1896: sub upload {
                   1897:     my ($r,$url,$group)=@_;
                   1898:     my $fname=&Apache::lonnet::clean_filename($env{'form.uploaddoc.filename'});
1.195     raeburn  1899:     my $disk_quota = &get_quota($group);
                   1900:     my $portfolio_root = &get_portfolio_root();
                   1901:     my $port_path = &get_port_path();
                   1902:     my ($uname,$udom) = &get_name_dom($group);
                   1903:     my $getpropath = 1;
                   1904:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
                   1905:     my ($state,$msg) = 
                   1906:         &Apache::loncommon::check_for_upload($env{'form.currentpath'},$fname,
                   1907: 		                             $group,'uploaddoc',$portfolio_root,
                   1908:                                              $port_path,$disk_quota,
                   1909:                                              $current_disk_usage,$uname,$udom);
1.176     albertel 1910:     if ($state eq 'will_exceed_quota'
                   1911: 	|| $state eq 'file_locked'
                   1912: 	|| $state eq 'file_exists' ) {
                   1913: 	$r->print($msg.&done('Back',$url));
                   1914: 	return;
                   1915:     }
                   1916: 
                   1917:     my (%allfiles,%codebase,$mode);
                   1918:     if ($env{'form.uploaddoc.filename'} =~ m/(\.htm|\.html|\.shtml)$/i) {
1.193     raeburn  1919:         if ($env{'form.parserflag'}) {
                   1920: 	    $mode = 'parse';
                   1921:         }
1.176     albertel 1922:     }
                   1923:     my $result=
                   1924: 	&Apache::lonnet::userfileupload('uploaddoc','',
                   1925: 					$port_path.$env{'form.currentpath'},
                   1926: 					$mode,\%allfiles,\%codebase);
                   1927:     if ($result !~ m|^/uploaded/|) {
1.188     bisitz   1928: 	$r->print('<span class="LC_error">'.&mt('An error occurred ([_1]) while trying to upload [_2].'
                   1929:                   ,$result,&display_file()).'</span><br />');
1.176     albertel 1930: 	$r->print(&done('Back',$url));
                   1931:     } else {
                   1932: 	if (%allfiles) {
1.195     raeburn  1933:             if (!&suppress_embed_prompt()) {
                   1934: 	        my $state = <<STATE;
1.176     albertel 1935:     <input type="hidden" name="action"      value="upload_embedded" />
                   1936:     <input type="hidden" name="currentpath" value="$env{'form.currentpath'}" />
                   1937:     <input type="hidden" name="fieldname"   value="$env{'form.fieldname'}" />
                   1938:     <input type="hidden" name="mode"        value="$env{'form.mode'}" />
                   1939: STATE
1.195     raeburn  1940:                 $r->print("<h2>".&mt("Reference Warning")."</h2>");
                   1941:                 $r->print("<p>".&mt("Completed upload of the file. This file contained references to other files. You must upload the referenced files or else the uploaded file may not work properly.")."</p>");
                   1942:                 $r->print("<p>".&mt("Please select the locations from which the referenced files are to be uploaded.")."</p>");
                   1943: 	        $r->print(&Apache::loncommon::ask_for_embedded_content('/adm/portfolio',$state,\%allfiles,\%codebase,
1.180     albertel 1944: 				      {'error_on_invalid_names'   => 1,
                   1945: 				       'ignore_remote_references' => 1,}));
1.195     raeburn  1946: 	        $r->print('<p>Or '.&done('Return to directory',$url).'</p>');
                   1947:             }
1.176     albertel 1948: 	} else {
                   1949: 	    $r->print(&done(undef,$url));
                   1950: 	}
                   1951:     }
                   1952: }
                   1953: 
1.80      banghart 1954: sub lock_info {
1.137     albertel 1955:     my ($r,$url,$group) = @_;
1.191     raeburn  1956:     my ($uname,$udom) = &get_name_dom($group);
1.102     raeburn  1957:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                   1958:                                                                        $uname);
1.84      banghart 1959:     my $file_name = $env{'form.lockinfo'};
1.138     albertel 1960:     $file_name = &prepend_group($file_name);
1.102     raeburn  1961:     if (defined($file_name) && defined($$current_permissions{$file_name})) {
                   1962:         foreach my $array_item (@{$$current_permissions{$file_name}}) {
1.156     albertel 1963:             next if (ref($array_item) ne 'ARRAY');
                   1964: 
                   1965: 	    my $filetext;
                   1966: 	    if (defined($group)) {
                   1967: 		$filetext = '<strong>'.$env{'form.lockinfo'}.
                   1968: 		    '</strong> (group: '.$group.')'; 
                   1969: 	    } else {
                   1970: 		$filetext = '<strong>'.$file_name.'</strong>';
                   1971: 	    } 
                   1972: 	    
                   1973: 	    my $title ='<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
                   1974: 		'</strong><br />';
                   1975: 	    if ($$array_item[-1] eq 'graded') {
                   1976: 		$r->print(&mt('[_1] was submitted in response to problem: [_2]',
                   1977:                               $filetext,$title));
                   1978: 	    } elsif ($$array_item[-1] eq 'handback') {
                   1979: 		$r->print(&mt('[_1] was handed back in response to problem: [_2]',
                   1980:                               $filetext,$title));
                   1981: 	    } else {
                   1982: 		# submission style lock
                   1983: 		$r->print(&mt('[_1] was submitted in response to problem: [_2]',
                   1984:                               $filetext,$title));
                   1985: 	    }
                   1986: 	    my %course_description = 
                   1987: 		&Apache::lonnet::coursedescription($$array_item[1]);
                   1988: 	    if ( $course_description{'description'} ne '') {
1.188     bisitz   1989: 		$r->print(&mt('In the course:').' <strong>'.$course_description{'description'}.'</strong><br />');
1.156     albertel 1990: 	    }
1.102     raeburn  1991:         }
1.84      banghart 1992:     }
1.185     banghart 1993:     $r->print(&done(&mt('Back'),$url));
1.80      banghart 1994:     return 'ok';
                   1995: }
1.138     albertel 1996: 
1.25      albertel 1997: sub createdir {
1.191     raeburn  1998:     my ($r,$url,$group)=@_;
1.82      albertel 1999:     my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
1.28      albertel 2000:     if ($newdir eq '') {
1.116     albertel 2001:     	$r->print('<span class="LC_error">'.
1.37      banghart 2002: 	    	  &mt("Error: no directory name was provided.").
1.116     albertel 2003: 		      '</span><br />');
1.138     albertel 2004: 	    $r->print(&done(undef,$url));
1.37      banghart 2005: 	    return;
1.94      raeburn  2006:     }
1.138     albertel 2007:     my $portfolio_root = &get_portfolio_root(); 
1.191     raeburn  2008:     my @dir_list=&get_dir_list($portfolio_root,undef,$group);
1.37      banghart 2009:     my $found_file = 0;
                   2010:     foreach my $line (@dir_list) {
                   2011:         my ($filename)=split(/\&/,$line,2);
                   2012:         if ($filename eq $newdir){
                   2013:             $found_file = 1;
                   2014:         }
                   2015:     }
                   2016:     if ($found_file){
1.188     bisitz   2017:     	    $r->print('<span class="LC_error">'
                   2018:                       .&mt('Unable to create a directory named [_1].','<strong>'.$newdir.'</strong>')
                   2019:                       .' '.&mt('A file or directory by that name already exists.').'</span><br />');
1.37      banghart 2020:     } else {
1.191     raeburn  2021:         my ($uname,$udom) = &get_name_dom($group);
1.138     albertel 2022:         my $port_path = &get_port_path();
1.94      raeburn  2023:         my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
                   2024: 	         $port_path.$env{'form.currentpath'}.$newdir);
1.37      banghart 2025:         if ($result ne 'ok') {
1.188     bisitz   2026:     	    $r->print('<span class="LC_error">'
                   2027:                       .&mt('An error occurred ([_1]) while trying to create a new directory [_2].'
                   2028:                            ,$result,&display_file())
                   2029:                       .'</span><br />');
1.37      banghart 2030:         }
1.24      albertel 2031:     }
1.82      albertel 2032:     if ($newdir ne $env{'form.newdir'}) {
1.188     bisitz   2033:         $r->print(&mt('The new directory name was changed from [_1] to [_2].'
                   2034:                       ,'<strong>'.$env{'form.newdir'}.'</strong>','<strong>'.$newdir.'</strong>'));  
1.67      banghart 2035:     }
1.137     albertel 2036:     $r->print(&done(undef,$url));
1.94      raeburn  2037: }
                   2038: 
                   2039: sub get_portfolio_root {
1.163     raeburn  2040:     my ($udom,$uname,$group) = @_;
1.160     raeburn  2041:     if (!(defined($udom)) || !(defined($uname))) {
1.191     raeburn  2042:         ($uname,$udom) = &get_name_dom($group);
1.160     raeburn  2043:     }
1.163     raeburn  2044:     my $path = '/userfiles/portfolio';
                   2045:     if (!defined($group)) { 
                   2046:         if (defined($env{'form.group'})) {
                   2047:             $group = $env{'form.group'};      
                   2048:         }
1.94      raeburn  2049:     }
1.163     raeburn  2050:     if (defined($group)) {
                   2051:         $path = '/userfiles/groups/'.$group.'/portfolio';
                   2052:     } 
1.191     raeburn  2053:     return $path;
1.94      raeburn  2054: }
                   2055: 
1.126     raeburn  2056: sub get_group_quota {
                   2057:     my ($group) = @_;
                   2058:     my $group_quota; 
                   2059:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2060:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   2061:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
                   2062:     if (%curr_groups) {
                   2063:         my %group_info =  &Apache::longroup::get_group_settings(
                   2064:                                                     $curr_groups{$group});
                   2065:         $group_quota = $group_info{'quota'}; #expressed in Mb
                   2066:         if ($group_quota) {
                   2067:             $group_quota = 1000 * $group_quota; #expressed in k
1.191     raeburn  2068:         }
1.126     raeburn  2069:     }
                   2070:     return $group_quota;
1.191     raeburn  2071: }
1.126     raeburn  2072: 
1.94      raeburn  2073: sub get_dir_list {
1.191     raeburn  2074:     my ($portfolio_root,$path,$group) = @_;
1.176     albertel 2075:     $path ||= $env{'form.currentpath'};
1.191     raeburn  2076:     my ($uname,$udom) = &get_name_dom($group);
                   2077:     my $getpropath = 1;
                   2078:     return &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,$getpropath);
1.94      raeburn  2079: }
                   2080: 
                   2081: sub get_name_dom {
1.191     raeburn  2082:     my ($group) = @_;
1.94      raeburn  2083:     my ($uname,$udom);
1.191     raeburn  2084:     if (defined($group)) {
1.94      raeburn  2085:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2086:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
                   2087:     } else {
                   2088:         $udom = $env{'user.domain'};
                   2089:         $uname = $env{'user.name'};
                   2090:     }
                   2091:     return ($uname,$udom);
                   2092: }
                   2093: 
1.102     raeburn  2094: sub prepend_group {
1.138     albertel 2095:     my ($filename) = @_;
                   2096:     if (defined($env{'form.group'})) {
                   2097:         $filename = $env{'form.group'}.$filename;
1.102     raeburn  2098:     }
                   2099:     return $filename;
                   2100: }
                   2101: 
1.94      raeburn  2102: sub get_namespace {
                   2103:     my $namespace = 'portfolio';
1.138     albertel 2104:     if (defined($env{'form.group'})) {
1.191     raeburn  2105:         my ($uname,$udom) = &get_name_dom($env{'form.group'});
1.138     albertel 2106:         $namespace .= '_'.$udom.'_'.$uname.'_'.$env{'form.group'};
1.94      raeburn  2107:     }
                   2108:     return $namespace;
                   2109: }
                   2110: 
                   2111: sub get_port_path {
                   2112:     my $port_path;
1.138     albertel 2113:     if (defined($env{'form.group'})) {
                   2114:        $port_path = "groups/$env{'form.group'}/portfolio";
1.94      raeburn  2115:     } else {
                   2116:        $port_path = 'portfolio';
                   2117:     }
                   2118:     return $port_path;
1.24      albertel 2119: }
                   2120: 
1.120     raeburn  2121: sub missing_priv {
1.138     albertel 2122:     my ($r,$url,$priv) = @_;
1.120     raeburn  2123:     my $longtext = {
                   2124:                       upload => 'upload files',
                   2125:                       delete => 'delete files',
                   2126:                       rename => 'rename files',
                   2127:                       setacl => 'set access controls for files',
                   2128:                    };
                   2129:     my $escpath = &HTML::Entities::encode($env{'form.currentpath'},'&<>"');
                   2130:     my $rtnlink = '<a href="'.$url;
                   2131:     if ($url =~ /\?/) {
                   2132:         $rtnlink .= '&';
                   2133:     } else {
                   2134:         $rtnlink .= '?';
                   2135:     }
                   2136:     $rtnlink .= 'currentpath='.$escpath;
1.188     bisitz   2137:     $r->print('<h3>'.&mt('Action disallowed').'</h3>');
1.120     raeburn  2138:     $r->print(&mt('You do not have sufficient privileges to [_1] ',
                   2139:                   $longtext->{$priv}));
1.138     albertel 2140:     if (defined($env{'form.group'})) {
1.120     raeburn  2141:         $r->print(&mt("in the group's file repository."));
1.137     albertel 2142:         $rtnlink .= &group_args()
1.120     raeburn  2143:     } else {
                   2144:         $r->print(&mt('in this portfolio.'));
                   2145:     }
1.188     bisitz   2146:     $rtnlink .= '">'.&mt('Return to directory').'</a>';
1.120     raeburn  2147:     $r->print('<br />'.$rtnlink);
                   2148:     $r->print(&Apache::loncommon::end_page());
                   2149:     return;
                   2150: }
                   2151: 
1.132     raeburn  2152: sub coursegrp_portfolio_header {
1.137     albertel 2153:     my ($cdom,$cnum,$grp_desc)=@_;
1.132     raeburn  2154:     my $gpterm  = &Apache::loncommon::group_term();
                   2155:     my $ucgpterm = $gpterm;
                   2156:     $ucgpterm =~ s/^(\w)/uc($1)/e;
1.137     albertel 2157:     if ($env{'form.ref'}) {
1.136     raeburn  2158:         &Apache::lonhtmlcommon::add_breadcrumb
                   2159:             ({href=>"/adm/coursegroups",
                   2160:               text=>"Groups",
                   2161:               title=>"Course Groups"});
                   2162:     }
1.132     raeburn  2163:     &Apache::lonhtmlcommon::add_breadcrumb
1.138     albertel 2164:         ({href=>"/adm/$cdom/$cnum/$env{'form.group'}/smppg?ref=$env{'form.ref'}",
1.132     raeburn  2165:           text=>"$ucgpterm: $grp_desc",
                   2166:           title=>"Go to group's home page"},
1.137     albertel 2167:          {href=>"/adm/coursegrp_portfolio?".&group_args(),
1.132     raeburn  2168:           text=>"Group Portfolio",
1.136     raeburn  2169:           title=>"Display group portfolio"});
1.132     raeburn  2170:     my $output = &Apache::lonhtmlcommon::breadcrumbs(
                   2171:                          &mt('[_1] portfolio files - [_2]',$gpterm,$grp_desc));
                   2172:     return $output;
                   2173: }
                   2174: 
1.174     raeburn  2175: sub get_quota {
                   2176:     my ($group) = @_;
                   2177:     my $disk_quota;
                   2178:     if (defined($group)) {
                   2179:         my $grp_quota = &get_group_quota($group); # quota expressed in k
                   2180:         if ($grp_quota ne '') {
                   2181:             $disk_quota = $grp_quota;
                   2182:         } else {
                   2183:             $disk_quota = 0;
                   2184:         }
                   2185:     } else {
                   2186:         $disk_quota = &Apache::loncommon::get_user_quota($env{'user.name'},
                   2187:                                     $env{'user.domain'}); #expressed in Mb
                   2188:         $disk_quota = 1000 * $disk_quota; # convert from Mb to kb
                   2189:     }
                   2190:     return $disk_quota;
                   2191: }
                   2192: 
1.195     raeburn  2193: sub suppress_embed_prompt {
                   2194:     my $suppress_prompt = 0;
                   2195:     if (($env{'request.role'} =~ /^st/) && ($env{'request.course.id'} ne '')) {
                   2196:         if ($env{'course.'.$env{'request.course.id'}.'.suppress_embed_prompt'} eq 'yes') {
                   2197:             $suppress_prompt = 1;
                   2198:         }
                   2199:     }
                   2200:     return $suppress_prompt;
                   2201: }
                   2202: 
                   2203: 
1.24      albertel 2204: sub handler {
                   2205:     # this handles file management
                   2206:     my $r = shift;
1.73      banghart 2207:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.108     raeburn  2208:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
                   2209: 	  'fieldname','mode','rename','continue','group','access','setnum',
1.136     raeburn  2210:           'cnum','cdom','type','setroles','showversions','ref']);
1.138     albertel 2211:     my ($uname,$udom,$portfolio_root,$url,$caller,$title,$group,$grp_desc);
1.94      raeburn  2212:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
                   2213:         $url = $1.$2;
                   2214:         $caller = $2;
                   2215:     }
1.137     albertel 2216:     my ($can_modify,$can_delete,$can_upload,$can_setacl);
1.94      raeburn  2217:     if ($caller eq 'coursegrp_portfolio') {
                   2218:     #  Needs to be in a course
                   2219:         if (! ($env{'request.course.fn'})) {
                   2220:         # Not in a course
                   2221:             $env{'user.error.msg'}=
                   2222:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
                   2223:             return HTTP_NOT_ACCEPTABLE;
                   2224:         }
                   2225:         my $earlyout = 0;
1.136     raeburn  2226:         my $view_permission = 
                   2227:            &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
1.137     albertel 2228:         $env{'form.group'} =~ s/\W//g;
1.138     albertel 2229: 	$group = $env{'form.group'};
1.191     raeburn  2230:         if ($group ne '') {
                   2231:             ($uname,$udom) = &get_name_dom($group);
1.99      raeburn  2232:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
1.98      albertel 2233: 							       $group); 
                   2234:             if (%curr_groups) {
1.132     raeburn  2235:                 my %grp_content = &Apache::longroup::get_group_settings(
                   2236:                                                          $curr_groups{$group});
                   2237:                 $grp_desc = &unescape($grp_content{'description'});
1.94      raeburn  2238:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
                   2239:                                       $env{'request.course.id'}.'/'.$group))) {
1.138     albertel 2240:                     $portfolio_root = &get_portfolio_root();
1.94      raeburn  2241:                 } else {
1.185     banghart 2242:                     $r->print(&mt('You do not have the privileges required to access the shared files space for this group.'));
1.94      raeburn  2243:                     $earlyout = 1;
                   2244:                 }
                   2245:             } else {
1.185     banghart 2246:                 $r->print(&mt('Not a valid group for this course'));
1.94      raeburn  2247:                 $earlyout = 1;
                   2248:             }
1.188     bisitz   2249:             $title = &mt('Group files for [_1]', $group); 
1.94      raeburn  2250:         } else {
1.185     banghart 2251:             $r->print(&mt('Invalid group'));
1.94      raeburn  2252:             $earlyout = 1;
                   2253:         }
                   2254:         if ($earlyout) { return OK; }
1.126     raeburn  2255:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
1.120     raeburn  2256:             $can_modify = 1;
                   2257:             $can_delete = 1;
1.126     raeburn  2258:             $can_upload = 1;
                   2259:             $can_setacl = 1;
                   2260:         } else {
                   2261:             if (&Apache::lonnet::allowed('agf',$env{'request.course.id'}.'/'.$group)) {
                   2262:                 $can_setacl = 1;
                   2263:             }
                   2264:             if (&Apache::lonnet::allowed('ugf',$env{'request.course.id'}.'/'.$group)) {
                   2265:                 $can_upload = 1;
                   2266:             }
                   2267:             if (&Apache::lonnet::allowed('mgf',$env{'request.course.id'}.'/'.$group)) {
                   2268:                 $can_modify = 1;
                   2269:             }
                   2270:             if (&Apache::lonnet::allowed('dgf',$env{'request.course.id'}.'/'.$group)) {
                   2271:                 $can_delete = 1;
                   2272:             }
1.120     raeburn  2273:         }
1.94      raeburn  2274:     } else {
                   2275:         ($uname,$udom) = &get_name_dom();
                   2276:         $portfolio_root = &get_portfolio_root();
                   2277:         $title = &mt('Portfolio Manager');
1.120     raeburn  2278:         $can_modify = 1;
                   2279:         $can_delete = 1;
                   2280:         $can_upload = 1;
                   2281:         $can_setacl = 1;
1.94      raeburn  2282:     }
                   2283: 
1.138     albertel 2284:     my $port_path = &get_port_path();
1.24      albertel 2285:     &Apache::loncommon::no_cache($r);
                   2286:     &Apache::loncommon::content_type($r,'text/html');
                   2287:     $r->send_http_header;
                   2288:     # Give the LON-CAPA page header
1.203     raeburn  2289:     my $brcrum = [{href=>"/adm/portfolio",text=>"Portfolio Manager"}];
                   2290: 
1.82      albertel 2291:     if ($env{"form.mode"} eq 'selectfile'){
1.96      albertel 2292:         $r->print(&Apache::loncommon::start_page($title,undef,
1.97      albertel 2293: 						 {'only_body' => 1}));
1.108     raeburn  2294:     } elsif ($env{'form.action'} eq 'rolepicker') {
                   2295:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
                   2296:                                                  {'no_nav_bar'  => 1, }));
1.205     raeburn  2297:     } elsif ($caller eq 'coursegrp_portfolio') {
                   2298:         $r->print(&Apache::loncommon::start_page($title));
1.74      banghart 2299:     } else {
1.203     raeburn  2300:         $r->print(&Apache::loncommon::start_page($title,undef,
                   2301:                                                  {'bread_crumbs' => $brcrum}));
1.205     raeburn  2302:         if (!&Apache::lonnet::usertools_access($uname,$udom,'portfolio')) {
                   2303:             $r->print('<h2>'.&mt('No user portfolio available') .'</h2>'.
                   2304:                       &mt('This is a result of one of the following:').'<ul>'.
                   2305:                       '<li>'.&mt('The administrator of this domain has disabled portfolio functionality for this specific user.').'</li>'.
                   2306:                       '<li>'.&mt('The domain has been configured to disable, by default, portfolio functionality for all users in the domain.').'</li>'.
                   2307:                       '</ul>');
                   2308:             $r->print(&Apache::loncommon::end_page());
                   2309:             return OK;
                   2310:         }
1.74      banghart 2311:     }
1.24      albertel 2312:     $r->rflush();
1.175     raeburn  2313:     my ($blocked,$blocktext) = 
                   2314:         &Apache::loncommon::blocking_status('port',$uname,$udom);
                   2315:     if ($blocked) {
                   2316:          $r->print($blocktext);
                   2317:          $r->print(&Apache::loncommon::end_page());
                   2318:          return OK;
                   2319:     }
1.88      albertel 2320: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
1.185     banghart 2321:    	    $r->print('<span class="LC_error">');
1.188     bisitz   2322:    	    $r->print(&mt('No file was selected to upload.').' ');
                   2323: 	    $r->print(&mt('To upload a file, click <strong>Browse...</strong> and select a file, then click <strong>Upload</strong>.'));
1.185     banghart 2324: 	    $r->print('</span>');
1.40      banghart 2325: 	}
1.82      albertel 2326:     if ($env{'form.meta'}) {
1.94      raeburn  2327:         &open_form($r,$url);
1.185     banghart 2328:         $r->print(&mt('Edit the meta data').'<br />');
1.137     albertel 2329:         &close_form($r,$url);
1.70      banghart 2330:     }
1.82      albertel 2331:     if ($env{'form.store'}) {
1.70      banghart 2332:     }
                   2333: 
1.82      albertel 2334:     if ($env{'form.uploaddoc.filename'}) {
1.120     raeburn  2335:         if ($can_upload) {
1.137     albertel 2336: 	    &upload($r,$url,$group);
1.120     raeburn  2337:         } else {
1.138     albertel 2338:             &missing_priv($r,$url,'upload');
1.120     raeburn  2339:         }
1.176     albertel 2340:     } elsif ($env{'form.action'} eq 'upload_embedded') {
                   2341: 	if ($can_upload) {
1.195     raeburn  2342:             my $disk_quota = &get_quota($group);
                   2343:             my $getpropath = 1;
                   2344:             my $current_disk_usage = 
                   2345:                 &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
                   2346: 	    $r->print(
                   2347:                 &Apache::loncommon::upload_embedded('portfolio',$port_path,$uname,$udom,
                   2348:                     $group,$portfolio_root,$group,$disk_quota,$current_disk_usage));
                   2349:             $r->print(&done(undef,$url));
1.176     albertel 2350:         } else {
                   2351:             &missing_priv($r,$url,'upload');
                   2352:         }
1.82      albertel 2353:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
1.120     raeburn  2354:         if ($can_delete) {
1.164     raeburn  2355: 	    &delete_confirmed($r,$url,$group);
1.120     raeburn  2356:         } else {
1.138     albertel 2357:             &missing_priv($r,$url,'delete');
1.120     raeburn  2358:         }
1.82      albertel 2359:     } elsif ($env{'form.action'} eq 'delete') {
1.120     raeburn  2360:         if ($can_delete) {
1.191     raeburn  2361: 	    &delete($r,$url,$group);
1.120     raeburn  2362:         } else {
1.138     albertel 2363:             &missing_priv($r,$url,'delete');
1.120     raeburn  2364:         }
1.82      albertel 2365:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
1.120     raeburn  2366:         if ($can_delete) {
1.191     raeburn  2367: 	    &delete_dir_confirmed($r,$url,$group);
1.120     raeburn  2368:         } else {
1.138     albertel 2369:             &missing_priv($r,$url,'delete');
1.120     raeburn  2370:         }
                   2371:     } elsif ($env{'form.action'} eq 'deletedir') {
                   2372:         if ($can_delete) {
1.137     albertel 2373: 	    &delete_dir($r,$url);
1.120     raeburn  2374:         } else {
1.138     albertel 2375:             &missing_priv($r,$url,'delete');
1.120     raeburn  2376:         }
1.82      albertel 2377:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
1.120     raeburn  2378:         if ($can_modify) {
1.164     raeburn  2379: 	    &rename_confirmed($r,$url,$group);
1.120     raeburn  2380:         } else {
1.138     albertel 2381:             &missing_priv($r,$url,'rename');
1.120     raeburn  2382:         }
1.82      albertel 2383:     } elsif ($env{'form.rename'}) {
                   2384:         $env{'form.selectfile'} = $env{'form.rename'};
                   2385:         $env{'form.action'} = 'rename';
1.120     raeburn  2386:         if ($can_modify) {
1.191     raeburn  2387: 	    &rename($r,$url,$group);
1.120     raeburn  2388:         } else {
1.138     albertel 2389:             &missing_priv($r,$url,'rename');
1.120     raeburn  2390:         }
1.104     raeburn  2391:     } elsif ($env{'form.access'}) {
                   2392:         $env{'form.selectfile'} = $env{'form.access'};
1.170     raeburn  2393:         if (!defined($env{'form.action'})) { 
                   2394:             $env{'form.action'} = 'chgaccess';
                   2395:         }
                   2396:         &display_access($r,$url,$group,$can_setacl,$port_path,$env{'form.action'});
                   2397:     } elsif (($env{'form.action'} eq 'chgaccess') || 
                   2398:              ($env{'form.action'} eq 'chgconditions')) {
1.120     raeburn  2399:         if ($can_setacl) {
1.137     albertel 2400:             &update_access($r,$url,$group,$port_path);
1.120     raeburn  2401:         } else {
1.138     albertel 2402:             &missing_priv($r,$url,'setacl');
1.120     raeburn  2403:         }
1.108     raeburn  2404:     } elsif ($env{'form.action'} eq 'rolepicker') {
1.120     raeburn  2405:         if ($can_setacl) { 
                   2406:             &role_options_window($r);
                   2407:         } else {
1.138     albertel 2408:             &missing_priv($r,$url,'setacl');
1.120     raeburn  2409:         }
1.82      albertel 2410:     } elsif ($env{'form.createdir'}) {
1.120     raeburn  2411:         if ($can_upload) {
1.191     raeburn  2412: 	    &createdir($r,$url,$group);
1.120     raeburn  2413:         } else {
1.138     albertel 2414:             &missing_priv($r,$url,'upload');
1.120     raeburn  2415:         }
1.82      albertel 2416:     } elsif ($env{'form.lockinfo'}) {
1.137     albertel 2417:         &lock_info($r,$url,$group);
1.24      albertel 2418:     } else {
                   2419: 	my $current_path='/';
1.82      albertel 2420: 	if ($env{'form.currentpath'}) {
                   2421: 	    $current_path = $env{'form.currentpath'};
1.24      albertel 2422: 	}
1.132     raeburn  2423:         if ($caller eq 'coursegrp_portfolio') {
                   2424:             &Apache::lonhtmlcommon::clear_breadcrumbs();
1.137     albertel 2425:             $r->print(&coursegrp_portfolio_header($udom,$uname,$grp_desc));
1.132     raeburn  2426:         }
1.192     raeburn  2427:         my @dir_list=&get_dir_list($portfolio_root,$current_path,$group);
1.46      albertel 2428: 	if ($dir_list[0] eq 'no_such_dir'){
                   2429: 	    # two main reasons for this:
                   2430:             #    1) never been here, so directory structure not created
                   2431: 	    #    2) back-button navigation after deleting a directory
                   2432: 	    if ($current_path eq '/'){
1.100     albertel 2433: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
1.138     albertel 2434: 					       &get_port_path());
1.46      albertel 2435: 	    } else {
                   2436:                 # some directory that snuck in get rid of the directory
                   2437:                 # from the recent pulldown, just in case
                   2438: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
                   2439: 						      [$current_path]);
                   2440: 		$current_path = '/'; # force it back to the root        
                   2441: 	    }
                   2442: 	    # now grab the directory list again, for the first time
1.192     raeburn  2443:             @dir_list=&get_dir_list($portfolio_root,$current_path,$group);
1.43      banghart 2444:         }
1.46      albertel 2445: 	# need to know if directory is empty so it can be removed if desired
                   2446: 	my $is_empty=(@dir_list == 2);
1.137     albertel 2447: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,
1.206     raeburn  2448: 			$can_upload,$group);
1.138     albertel 2449:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group,
1.137     albertel 2450:                            $can_upload,$can_modify,$can_delete,$can_setacl);
1.95      albertel 2451: 	$r->print(&Apache::loncommon::end_page());
1.30      banghart 2452:     }
1.90      albertel 2453:     return OK;
1.2       banghart 2454: }
1.120     raeburn  2455: 
1.1       banghart 2456: 1;
                   2457: __END__

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