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

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

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