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

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

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