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

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

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