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

1.3       banghart    1: # Copyright Michigan State University Board of Trustees
                      2: #
                      3: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      4: #
                      5: # LON-CAPA is free software; you can redistribute it and/or modify
                      6: # it under the terms of the GNU General Public License as published by
                      7: # the Free Software Foundation; either version 2 of the License, or 
                      8: # (at your option) any later version.
                      9: #
                     10: # LON-CAPA is distributed in the hope that it will be useful,
                     11: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     12: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     13: # GNU General Public License for more details.
                     14: #
                     15: # You should have received a copy of the GNU General Public License
                     16: # along with LON-CAPA; if not, write to the Free Software
                     17: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     18: #
                     19: # /home/httpd/html/adm/gpl.txt
                     20: #
                     21: # http://www.lon-capa.org/
                     22: #
                     23: 
1.1       banghart   24: package Apache::portfolio;
                     25: use strict;
                     26: use Apache::Constants qw(:common :http);
1.2       banghart   27: use Apache::loncommon;
1.1       banghart   28: use Apache::lonnet;
1.2       banghart   29: use Apache::lontexconvert;
                     30: use Apache::lonfeedback;
                     31: use Apache::lonlocal;
1.82      albertel   32: use Apache::lonnet;
1.99      raeburn    33: use Apache::longroup;
1.101     www        34: use lib '/home/httpd/lib/perl';
                     35: use LONCAPA;
1.16      banghart   36: 
                     37: # receives a file name and path stub from username/userfiles/portfolio/
                     38: # returns an anchor tag consisting encoding filename and currentpath
1.23      albertel   39: sub make_anchor {
1.94      raeburn    40:     my ($url, $filename, $current_path, $current_mode, $field_name,
                     41:         $continue_select,$group) = @_;
1.83      banghart   42:     if ($continue_select ne 'true') {$continue_select = 'false'};
1.94      raeburn    43:     my $anchor = '<a href="'.$url.'?selectfile='.$filename.'&currentpath='.$current_path.'&mode='.$current_mode.'&continue='.$continue_select.'&fieldname='.$field_name;
                     44:     if (defined($group)) {
                     45:         $anchor .= '&group='.$group;
                     46:     }
                     47:     $anchor .= '">'.$filename.'</a>';
1.8       albertel   48:     return $anchor;
1.6       banghart   49: }
1.24      albertel   50: my $dirptr=16384;
1.48      banghart   51: sub display_common {
1.94      raeburn    52:     my ($r,$url,$current_path,$is_empty,$dir_list,$group)=@_;
                     53:     my $groupitem;
                     54:     my $namespace = &get_namespace($group);
                     55:     my $port_path = &get_port_path($group);
                     56:     if (defined($group)) {
                     57:         $groupitem = '<input type="hidden" name="group" value="'.$group.'" />';
                     58:     } 
1.18      banghart   59:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.88      albertel   60:     my %text=&Apache::lonlocal::texthash('upload' => 'Upload',
                     61: 					 'upload_label' =>  
                     62: 					 'Upload file to current directory:',
                     63: 					 'createdir' => 'Create Subdirectory',
                     64: 					 'createdir_label' => 
                     65: 					 'Create subdirectory in current directory:');
                     66:     $r->print(<<"TABLE"); 
                     67: <table border="0" cellspacing="2" cellpadding="2">
                     68:   <form method="post" enctype="multipart/form-data">
                     69:     <tr valign="middle">
                     70:       <td bgcolor="#ccddaa" align="right">
                     71:         $text{'upload_label'}
                     72:       </td>
1.94      raeburn    73:       <td bgcolor="#ccddaa" align="left">$groupitem
1.88      albertel   74:         <input name="uploaddoc" type="file" />
                     75: 	<input type="hidden" name="currentpath" value="$current_path" />
                     76: 	<input type="hidden" name="action" value="$env{"form.action"}" />
                     77: 	<input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
                     78: 	<input type="hidden" name="mode" value="$env{"form.mode"}" />
                     79: 	<input type="submit" name="storeupl" value="$text{'upload'}" />
                     80:       </td>
                     81:     </tr>
                     82:   </form>
                     83:   <form method="post">
                     84:     <tr>
                     85:       <td bgcolor="#ccddaa" align="right">
                     86:         $text{'createdir_label'}
                     87:       </td>
                     88:       <td bgcolor="#ccddaa" align="left">
1.94      raeburn    89:         <input name="newdir" type="input" />$groupitem
1.88      albertel   90:         <input type="hidden" name="currentpath" value="$current_path" />
                     91:         <input type="hidden" name="action" value="$env{"form.action"}" />
                     92:         <input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
                     93:         <input type="hidden" name="mode" value="$env{"form.mode"}" />
                     94:         <input type="submit" name="createdir" value="$text{'createdir'}" />
                     95:       </td>
                     96:     </tr>
                     97:   </form>
                     98: </table>
                     99: TABLE
1.24      albertel  100:     my @tree = split (/\//,$current_path);
1.94      raeburn   101:     $r->print('<font size="+2">'.&make_anchor($url,$port_path,'/',$env{"form.mode"},$env{"form.fieldname"},$env{"form.continue"},$group).'/');
1.19      banghart  102:     if (@tree > 1){
                    103:         my $newCurrentPath = '';
                    104:         for (my $i = 1; $i< @tree; $i++){
                    105:             $newCurrentPath .= $tree[$i].'/';
1.94      raeburn   106:             $r->print(&make_anchor($url,$tree[$i],'/'.$newCurrentPath, $env{"form.mode"},$env{"form.fieldname"}, $env{"form.continue"},$group).'/');
1.19      banghart  107:         }
                    108:     }
                    109:     $r->print('</font>');
1.94      raeburn   110:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
                    111:     $r->print('<br /><form method=post action="'.$url.'?mode='.$env{"form.mode"}.'&fieldname='.$env{"form.fieldname"});
                    112:     if (defined($group)) {
                    113:         $r->print('&group='.$group);
                    114:     }
                    115:     $r->print('">'.
                    116: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
1.22      albertel  117: 						    'this.form.submit();'));
1.21      banghart  118:     $r->print("</form>");
1.48      banghart  119: }
                    120: sub display_directory {
1.94      raeburn   121:     my ($r,$url,$current_path,$is_empty,$dir_list,$group)=@_;
1.48      banghart  122:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.94      raeburn   123:     my ($groupitem,$groupecho);
1.48      banghart  124:     my $display_out;
1.77      banghart  125:     my $select_mode;
                    126:     my $checked_files;
1.94      raeburn   127:     my $port_path = &get_port_path($group);
                    128:     my ($uname,$udom) = &get_name_dom($group);
                    129:     if (defined($group)) {
                    130:        $groupitem = '<input type="hidden" name="group" value="'.$group.'" />'; 
                    131:        $groupecho = '&amp;group='.$group;
                    132:     }
1.102     raeburn   133:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    134:                                                                         $uname);
                    135:     my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash(
                    136:                                                   $current_permissions,$group);
                    137:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group);
                    138:     my $now = time;
1.82      albertel  139:     if ($env{"form.mode"} eq 'selectfile'){
1.77      banghart  140: 	&select_files($r);
1.94      raeburn   141: 	$checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
1.77      banghart  142: 	$select_mode = 'true';
                    143:     } 
1.45      banghart  144:     if ($is_empty && ($current_path ne '/')) {
1.94      raeburn   145:         $display_out = '<form method="post" action="'.$url.'">'.$groupitem.
1.30      banghart  146:         '<input type="hidden" name="action" value="deletedir" />'.
                    147:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
                    148:         '<input type="hidden" name="selectfile" value="" />'.
                    149:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
                    150:         '</form>';
                    151:         
1.48      banghart  152:         $r->print($display_out);
1.31      albertel  153: 	return;
                    154:     }
1.77      banghart  155:     if ($select_mode eq 'true') {
                    156:         $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
                    157:             '<tr><th>Select</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
1.94      raeburn   158:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
1.77      banghart  159:     } else {
                    160:         $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
1.102     raeburn   161:             '<tr><th colspan="2">Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th><th>Current Access Status</th></tr>');
1.94      raeburn   162:         $r->print('<form method="post" action="'.$url.'">');
                    163:     }
                    164:     if (defined($group)) {
                    165:         $r->print("\n".$groupitem."\n");
1.77      banghart  166:     }
1.94      raeburn   167:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
                    168:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
1.105     banghart  169:     my @dir_lines;
                    170:     my %versioned;
1.26      albertel  171:     foreach my $line (sort 
                    172: 		      { 
                    173: 			  my ($afile)=split('&',$a,2);
                    174: 			  my ($bfile)=split('&',$b,2);
                    175: 			  return (lc($afile) cmp lc($bfile));
                    176: 		      } (@$dir_list)) {
1.18      banghart  177:     	#$strip holds directory/file name
                    178:     	#$dom 
1.23      albertel  179:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
1.77      banghart  180:     	$filename =~ s/\s+$//;
1.105     banghart  181:     	my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
                    182:     	if ($version) {
                    183:     	    $versioned{$fname} .= $version.",";
                    184:     	}
1.106     albertel  185:         push(@dir_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
1.105     banghart  186:     }
1.106     albertel  187:     foreach my $line (@dir_lines) {
1.105     banghart  188:         my ($filename,$dom,$testdir,$size,$mtime,$obs) = @$line;
                    189:         my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
1.93      albertel  190:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
1.23      albertel  191:             if ($dirptr&$testdir) {
1.77      banghart  192:                 if ($select_mode eq 'true'){
                    193:                     $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
1.64      banghart  194:                 } else {
1.77      banghart  195:                     $r->print('<tr bgcolor="#FFAA99"><td colspan="2"><img src="'.$iconpath.'folder_closed.gif"></td>');
1.64      banghart  196:                 }
1.47      banghart  197:                 $r->print('<td>Go to ...</td>');
1.94      raeburn   198:                 $r->print('<td>'.&make_anchor($url,$filename.'/',$current_path.$filename.'/',$env{'form.mode'},$env{"form.fieldname"},$env{'form.continue'},$group).'</td>'); 
1.47      banghart  199:                 $r->print('</tr>'); 
                    200:             } else {
                    201:                 $r->print('<tr bgcolor="#CCCCFF">');
1.105     banghart  202:                 my $version_flag;
                    203:                 if (exists($versioned{$fname})) {
                    204:                    $version_flag = "*";
                    205:                 } else {
                    206:                     $version_flag = "";
                    207:                 }
                    208:                my $fullpath = $current_path.$filename;
1.103     raeburn   209:                 $fullpath = &prepend_group($fullpath,$group);
1.77      banghart  210:                 if ($select_mode eq 'true'){
1.102     raeburn   211:                     $r->print('<td><input type="checkbox" name="checkfile" value="'.$filename.'"');
1.77      banghart  212:                     if ($$checked_files{$filename} eq 'selected') {
                    213:                         $r->print("CHECKED");
                    214:                     }
                    215:                     $r->print('></td>');
                    216:                 } else {
1.103     raeburn   217:                     if (exists $locked_files{$fullpath}) {
1.94      raeburn   218:                         $r->print('<td colspan="2"><a href="'.$url.'?lockinfo='.$current_path.$filename.$groupecho.'">Locked</a></td>');
1.77      banghart  219:                     } else {
1.89      albertel  220: 			my $cat='<img alt="'.&mt('Catalog Information').
                    221: 			    '" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
1.77      banghart  222:                         $r->print('<td><input type="checkbox" name="selectfile" value="'.$filename.'" />
1.94      raeburn   223:                             <a href="'.$url.'?rename='.$filename.'&amp;currentpath='.$current_path.$groupecho.'">Rename</a></td>
1.89      albertel  224:                             <td><a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>
1.77      banghart  225:                             </td>');
                    226:                     }
1.61      banghart  227:                 }
1.103     raeburn   228:                 my $curr_access;
                    229:                 my $pub_access = 0;
1.108     raeburn   230:                 my $guest_access = 0;
                    231:                 my $cond_access = 0;
1.103     raeburn   232:                 foreach my $key (sort(keys(%{$access_controls{$fullpath}}))) {
1.108     raeburn   233:                     my ($num,$scope,$end,$start) = &unpack_acc_key($key);
1.103     raeburn   234:                     if (($now > $start) && (!$end || $end > $now)) {
                    235:                         if ($scope eq 'public')  {
                    236:                             $pub_access = 1;
1.108     raeburn   237:                         } elsif ($scope eq 'guest') {
                    238:                             $guest_access = 1;
                    239:                         } else {
                    240:                             $cond_access = 1;
1.103     raeburn   241:                         }
                    242:                     }
                    243:                 }
1.108     raeburn   244:                 if (!$pub_access && !$guest_access && !$cond_access) {
                    245:                     $curr_access = &mt('Private');
1.104     raeburn   246:                 } else {
1.108     raeburn   247:                     my @allaccesses; 
                    248:                     if ($pub_access) {
                    249:                         push(@allaccesses,&mt('Public'));
                    250:                     }
                    251:                     if ($guest_access) {
                    252:                         push(@allaccesses,&mt('Password-protected'));
                    253:                     }
                    254:                     if ($cond_access) {
                    255:                         push(@allaccesses,&mt('Conditional'));
                    256:                     }
                    257:                     $curr_access = join('+ ',@allaccesses);
1.103     raeburn   258:                 }
1.91      albertel  259:                 $r->print('<td><img src="'.&Apache::loncommon::icon($filename).'"></td>');
1.47      banghart  260:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
                    261: 			    $filename.'</a></td>'); 
                    262:                 $r->print('<td>'.$size.'</td>');
                    263:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
1.112   ! albertel  264:                 $r->print('<td><span style="white-space: nowrap">'.&mt($curr_access).'&nbsp;&nbsp;&nbsp;'.
1.102     raeburn   265:                           '<a href="'.$url.'?access='.$filename.
                    266:                           '&amp;currentpath='.$current_path.$groupecho.
1.112   ! albertel  267:                           '">'.&mt('View/Change').'</a></span></td>');
1.47      banghart  268:                 $r->print('</tr>'); 
                    269:             }
                    270:         }
                    271:     }
1.77      banghart  272:     if ($select_mode eq 'true') {
                    273:         $r->print('</table>
1.60      banghart  274:             <input type="hidden" name="continue" value="true">
1.82      albertel  275:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'">
1.60      banghart  276:             <input type="hidden" name="mode" value="selectfile">
                    277:             <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
1.48      banghart  278:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
                    279:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
1.77      banghart  280:         </form>');        
                    281:     } else {
                    282:         $r->print('</table>
                    283:         <input type="submit" name="doit" value="Delete Checked Files" />
                    284:         <input type="hidden" name="action" value="delete" />
                    285:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
                    286:         </form>');
                    287:     }
1.47      banghart  288: }
1.72      banghart  289: 
1.24      albertel  290: sub open_form {
1.94      raeburn   291:     my ($r,$url)=@_;
1.65      banghart  292:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
1.102     raeburn   293:     $r->print('<form name="portform" method="post" action="'.$url.'">');
1.24      albertel  294:     $r->print('<input type="hidden" name="action" value="'.
1.82      albertel  295: 	      $env{'form.action'}.'" />');
1.24      albertel  296:     $r->print('<input type="hidden" name="confirmed" value="1" />');
1.65      banghart  297:     foreach (@files) {
                    298:         $r->print('<input type="hidden" name="selectfile" value="'.
                    299: 	      $_.'" />');
                    300:     }
1.24      albertel  301:     $r->print('<input type="hidden" name="currentpath" value="'.
1.82      albertel  302: 	      $env{'form.currentpath'}.'" />');
1.24      albertel  303: }
                    304: 
                    305: sub close_form {
1.108     raeburn   306:     my ($r,$url,$group,$button_text)=@_;
                    307:     if (!defined($button_text)) {
                    308:         $button_text = {
                    309:                          'continue' => &mt('Continue'),
                    310:                          'cancel'   => &mt('Cancel'),
                    311:                        };
                    312:     }
                    313:     $r->print('<p><input type="submit" value="'.$button_text->{'continue'}.'" />');
1.94      raeburn   314:     if (defined($group)) {
                    315:        $r->print("\n".'<input type="hidden" name="group" value="'.
                    316:               $group.'" />');
                    317:     }
                    318:     $r->print('</p></form>');
1.112   ! albertel  319:     $r->print('<form action="'.$url.'" method="post">
1.24      albertel  320:                <p>
                    321:               <input type="hidden" name="currentpath" value="'.
1.94      raeburn   322: 	      $env{'form.currentpath'}.'" />');
                    323:     if (defined($group)) {
                    324:        $r->print("\n".'<input type="hidden" name="group" value="'.
                    325:               $group.'" />');
                    326:     }
1.108     raeburn   327:     $r->print("\n".'   <input type="submit" value="'.$button_text->{'cancel'}.'" />
1.94      raeburn   328:                </p></form>'); 
1.24      albertel  329: }
                    330: 
                    331: sub display_file {
1.27      albertel  332:     my ($path,$filename)=@_;
1.65      banghart  333:     my $display_file_text;
1.82      albertel  334:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
1.65      banghart  335:     if (!defined($filename)) { 
1.82      albertel  336:         $filename=$env{'form.selectfile'};
1.65      banghart  337:         $display_file_text = '<tt>'.$path.$filename.'</tt>';
                    338:     } elsif (ref($filename) eq "ARRAY") {
                    339:         foreach (@$filename) {
1.66      banghart  340:             $display_file_text .= '<tt>'.$path.$_.'</tt><br />';
1.65      banghart  341:         }
                    342:     } elsif (ref($filename) eq "SCALAR") {
                    343:         $display_file_text = '<tt>'.$path.$filename.'</tt>';        
                    344:     }
                    345:     return $display_file_text;
1.24      albertel  346: }
                    347: 
                    348: sub done {
1.94      raeburn   349:     my ($message,$url,$group)=@_;
1.76      banghart  350:     unless (defined $message) {
                    351:         $message='Done';
                    352:     }
1.94      raeburn   353:     my $result = '<h3><a href="'.$url.'?currentpath='.
                    354: 	         $env{'form.currentpath'}.
                    355: 	         '&fieldname='.$env{'form.fieldname'}.
                    356: 	         '&mode='.$env{'form.mode'};
                    357:     if (defined($group)) {
                    358:         $result .= '&group='.$group;
                    359:     }
                    360:     $result .= '">'.&mt($message).'</a></h3>';
                    361:     return $result;
1.24      albertel  362: }
                    363: 
                    364: sub delete {
1.94      raeburn   365:     my ($r,$url,$group)=@_;
1.55      banghart  366:     my @check;
1.82      albertel  367:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
1.102     raeburn   368:     $file_name = &prepend_group($file_name,$group);
1.65      banghart  369:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
1.94      raeburn   370:     my ($uname,$udom) = &get_name_dom($group);
                    371:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.55      banghart  372:         $r->print ("The file is locked and cannot be deleted.<br />");
1.94      raeburn   373:         $r->print(&done('Back',$url,$group));
1.55      banghart  374:     } else {
1.66      banghart  375:         if (scalar(@files)) {
1.94      raeburn   376:             &open_form($r,$url);
1.66      banghart  377:             $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
1.94      raeburn   378:             &close_form($r,$url,$group);
1.66      banghart  379:         } else {
                    380:             $r->print("No file was checked to delete.<br />");
1.94      raeburn   381:             $r->print(&done(undef,$url,$group));
1.66      banghart  382:         }
1.55      banghart  383:     }
1.24      albertel  384: } 
                    385: 
                    386: sub delete_confirmed {
1.94      raeburn   387:     my ($r,$url,$group)=@_;
1.65      banghart  388:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
                    389:     my $result;
1.94      raeburn   390:     my ($uname,$udom) = &get_name_dom($group);
                    391:     my $port_path = &get_port_path($group);
1.65      banghart  392:     foreach my $delete_file (@files) {
1.94      raeburn   393:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
1.82      albertel  394: 					       $env{'form.currentpath'}.
1.65      banghart  395: 					       $delete_file);
                    396:         if ($result ne 'ok') {
1.30      banghart  397: 	$r->print('<font color="red"> An error occured ('.$result.
1.65      banghart  398: 		  ') while trying to delete '.&display_file(undef, $delete_file).'</font><br />');
                    399:         }
1.24      albertel  400:     }
1.94      raeburn   401:     $r->print(&done(undef,$url,$group));
1.24      albertel  402: }
                    403: 
1.30      banghart  404: sub delete_dir {
1.94      raeburn   405:     my ($r,$url,$group)=@_;
                    406:     &open_form($r,$url);
1.30      banghart  407:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
1.94      raeburn   408:     &close_form($r,$url,$group);
1.30      banghart  409: } 
                    410: 
                    411: sub delete_dir_confirmed {
1.94      raeburn   412:     my ($r,$url,$group)=@_;
1.82      albertel  413:     my $directory_name = $env{'form.currentpath'};
1.81      albertel  414:     $directory_name =~ s|/$||; # remove any trailing slash
1.94      raeburn   415:     my ($uname,$udom) = &get_name_dom($group);
                    416:     my $namespace = &get_namespace($group);
                    417:     my $port_path = &get_port_path($group);
                    418:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
1.30      banghart  419: 					       $directory_name);
1.32      banghart  420: 					       
1.30      banghart  421:     if ($result ne 'ok') {
                    422: 	$r->print('<font color="red"> An error occured (dir) ('.$result.
                    423: 		  ') while trying to delete '.$directory_name.'</font><br />');
1.32      banghart  424:     } else {
1.41      banghart  425:         # now remove from recent
                    426: #        $r->print('<br /> removing '.$directory_name.'<br /');
1.94      raeburn   427:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
1.32      banghart  428:         my @dirs = split m!/!, $directory_name;
                    429:         
                    430: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
                    431:         $directory_name='/';
                    432:         for (my $i=1; $i < (@dirs - 1); $i ++){
                    433:             $directory_name .= $dirs[$i].'/';
                    434:         }
1.82      albertel  435:         $env{'form.currentpath'} = $directory_name;
1.30      banghart  436:     }
1.94      raeburn   437:     $r->print(&done(undef,$url,$group));
1.30      banghart  438: }
                    439: 
1.24      albertel  440: sub rename {
1.94      raeburn   441:     my ($r,$url,$group)=@_;
1.82      albertel  442:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
1.94      raeburn   443:     my ($uname,$udom) = &get_name_dom($group);
1.102     raeburn   444:     $file_name = &prepend_group($file_name,$group);
1.94      raeburn   445:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.55      banghart  446:         $r->print ("The file is locked and cannot be renamed.<br />");
1.94      raeburn   447:         $r->print(&done(undef,$url,$group));
1.55      banghart  448:     } else {
1.94      raeburn   449:         &open_form($r,$url);
1.55      banghart  450:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
                    451:                    <input name="filenewname" type="input" size="50" />?</p>');
1.94      raeburn   452:         &close_form($r,$url,$group);
1.55      banghart  453:     }
1.24      albertel  454: }
                    455: 
                    456: sub rename_confirmed {
1.94      raeburn   457:     my ($r,$url,$group)=@_;
1.82      albertel  458:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
1.94      raeburn   459:     my ($uname,$udom) = &get_name_dom($group);
                    460:     my $port_path = &get_port_path($group);
1.27      albertel  461:     if ($filenewname eq '') {
                    462: 	$r->print('<font color="red">'.
                    463: 		  &mt("Error: no valid filename was provided to rename to.").
                    464: 		  '</font><br />');
1.94      raeburn   465: 	$r->print(&done(undef,$url,$group));
1.27      albertel  466: 	return;
                    467:     } 
                    468:     my $result=
1.94      raeburn   469: 	&Apache::lonnet::renameuserfile($uname,$udom,
                    470:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
                    471:             $port_path.$env{'form.currentpath'}.$filenewname);
1.27      albertel  472:     if ($result ne 'ok') {
                    473: 	$r->print('<font color="red"> An errror occured ('.$result.
                    474: 		  ') while trying to rename '.&display_file().' to '.
                    475: 		  &display_file(undef,$filenewname).'</font><br />');
                    476:     }
1.82      albertel  477:     if ($filenewname ne $env{'form.filenewname'}) {
                    478:         $r->print("The new file name was changed from:<br /><strong>".$env{'form.filenewname'}."</strong> to <strong>$filenewname </strong>");
1.66      banghart  479:     }
1.94      raeburn   480:     $r->print(&done(undef,$url,$group));
1.27      albertel  481: }
1.102     raeburn   482: 
1.104     raeburn   483: sub display_access {
                    484:     my ($r,$url,$group) = @_;
                    485:     my ($uname,$udom) = &get_name_dom($group);
                    486:     my $file_name = $env{'form.currentpath'}.$env{'form.access'};
                    487:     $file_name = &prepend_group($file_name,$group);
                    488:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    489:                                                                         $uname);
                    490:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group,$file_name);
                    491:     &open_form($r,$url);
                    492:     $r->print('<h3>'.&mt('Allowing others to retrieve portfolio file: [_1]',$env{'form.currentpath'}.$env{'form.access'}).'</h3>'."\n");
1.108     raeburn   493:     $r->print(&mt('Access to this file by others can be set to be one or more of the following types: public, password-protected or conditional.').'<br /><ul><li>'.&mt('Public files are available to anyone without the need for login.').'</li><li>'.&mt('Password-protected files do not require log-in, but will require the viewer to enter the password you set.').'</li><li>'.&mt('Conditional files are accessible to logged-in users with accounts in the LON-CAPA network, who satify the conditions you set.').'<br />'.&mt('The conditions can include affiliation with a particular course or group, or a user account in a specific domain.').'<br />'.&mt('Alternatively you can grant access to people with specific LON-CAPA usernames and domains.').'</li></ul>');
1.104     raeburn   494:     &access_setting_table($r,$access_controls{$file_name});
1.108     raeburn   495:     my $button_text = {
                    496:                         'continue' => &mt('Proceed'),
                    497:                         'cancel' => &mt('Back to directory listing'),
                    498:                       };
                    499:     &close_form($r,$url,$group,$button_text);
1.104     raeburn   500: }
                    501: 
                    502: sub update_access {
                    503:     my ($r,$url,$group) = @_;
1.108     raeburn   504:     my $function = &Apache::loncommon::get_users_function();
                    505:     my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
1.104     raeburn   506:     my $totalprocessed = 0;
                    507:     my %processing;
                    508:     my %title  = (
1.108     raeburn   509:                          'activate' => 'New control(s) added',
                    510:                          'delete'   => 'Existing control(s) deleted',
                    511:                          'update'   => 'Existing control(s) modified',
1.104     raeburn   512:                      );
1.108     raeburn   513:     my $changes;
1.104     raeburn   514:     foreach my $chg (sort(keys(%title))) {     
                    515:         @{$processing{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
                    516:         $totalprocessed += @{$processing{$chg}};
                    517:         foreach my $num (@{$processing{$chg}}) {
                    518:             my $scope = $env{'form.scope_'.$num};
                    519:             my ($start,$end) = &get_dates_from_form($num);
                    520:             my $newkey = $num.':'.$scope.'_'.$end.'_'.$start;
                    521:             if ($chg eq 'delete') {
                    522:                 $$changes{$chg}{$newkey} = 1;
                    523:             } else {
                    524:                 $$changes{$chg}{$newkey} = 
1.108     raeburn   525:                             &build_access_record($num,$scope,$start,$end,$chg);
1.104     raeburn   526:             }
                    527:         }
                    528:     }
                    529:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
1.108     raeburn   530:     $r->print('<h3>'.&mt('Allowing others to retrieve portfolio file: [_1]',
                    531:               $file_name).'</h3>'."\n");
1.104     raeburn   532:     $file_name = &prepend_group($file_name,$group);
                    533:     my ($uname,$udom) = &get_name_dom($group);
                    534:     my ($errors,$outcome,$deloutcome,$new_values,$translation);
                    535:     if ($totalprocessed) {
                    536:         ($outcome,$deloutcome,$new_values,$translation) =
1.108     raeburn   537:         &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
                    538:                                                 $uname);
1.104     raeburn   539:     }
1.108     raeburn   540:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    541:                                                                        $uname);
1.109     albertel  542:     my %access_controls = 
                    543: 	&Apache::lonnet::get_access_controls($current_permissions,
                    544: 					     $group,$file_name);
1.104     raeburn   545:     if ($totalprocessed) {
                    546:         if ($outcome eq 'ok') {
                    547:             my $updated_controls = $access_controls{$file_name};
                    548:             my ($showstart,$showend);
                    549:             $r->print(&Apache::loncommon::start_data_table());
1.110     albertel  550:             $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn   551:             $r->print('<th>'.&mt('Type of change').'</th><th>'.
                    552:                       &mt('Access control').'</th><th>'.&mt('Dates available').
                    553:                       '</th><th>'.&mt('Additional information').'</th>');
1.110     albertel  554:             $r->print(&Apache::loncommon::end_data_table_header_row());
1.104     raeburn   555:             foreach my $chg (sort(keys(%processing))) {
                    556:                 if (@{$processing{$chg}} > 0) {
                    557:                     if ($chg eq 'delete') {
                    558:                         if (!($deloutcome eq 'ok')) {
                    559:                             $errors .= &mt('A problem occurred deleting access controls: [_1]',$deloutcome);
                    560:                             next;
                    561:                         }
                    562:                     }
                    563:                     my $numchgs = @{$processing{$chg}};
                    564:                     $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn   565:                     $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
                    566:                               '.</td>');
1.104     raeburn   567:                     my $count = 0;
                    568:                     foreach my $key (sort(keys(%{$$changes{$chg}}))) {
                    569:                         if ($count) {
                    570:                             $r->print(&Apache::loncommon::start_data_table_row());
                    571:                         }
1.108     raeburn   572:                         my ($num,$scope,$end,$start) = &unpack_acc_key($key); 
1.104     raeburn   573:                         my $newkey = $key;
                    574:                         if ($chg eq 'activate') {
                    575:                             $newkey =~ s/^(\d+)/$$translation{$1}/;
                    576:                         }
1.109     albertel  577:                         my $content = $$updated_controls{$newkey};
1.104     raeburn   578:                         if ($chg eq 'delete') {
                    579:                             $showstart = &mt('Deleted');
                    580:                             $showend = $showstart;
                    581:                         } else {
                    582:                             $showstart = localtime($start);
                    583:                             if ($end == 0) {
                    584:                                 $showend = &mt('No end date');
                    585:                             } else {
                    586:                                 $showend = localtime($end);
                    587:                             }
                    588:                         }
1.108     raeburn   589:                         $r->print('<td>'.&mt($scope));
                    590:                         if (($scope eq 'course') || ($scope eq 'group')) {
                    591:                             if ($chg ne 'delete') {
1.109     albertel  592:                                 my $cid = $content->{'domain'}.'_'.$content->{'number'};
1.108     raeburn   593:                                 my %course_description = &Apache::lonnet::coursedescription($cid);
                    594:                                 $r->print('<br />('.$course_description{'description'}.')');
                    595:                             }
                    596:                         }  
                    597:                         $r->print('</td><td>'.&mt('Start: ').$showstart.
                    598:                                   '<br />'.&mt('End: ').$showend.'</td><td>');
                    599:                         if ($chg ne 'delete') {
                    600:                             if ($scope eq 'guest') {
1.109     albertel  601:                                 $r->print(&mt('Password').': '.$content->{'password'});
1.108     raeburn   602:                             } elsif ($scope eq 'course' || $scope eq 'group') {
                    603:                                 $r->print('<table border="0"><tr bgcol = "'.
                    604:                                           $tablecolor.'">');
                    605:                                 $r->print('<th>'.&mt('Roles').'</th><th>'.
                    606:                                           &mt('Access').'</th><th>'.
                    607:                                           &mt('Sections').'</th>');
                    608:                                 if ($scope eq 'course') {
                    609:                                     $r->print('<th>'.&mt('Groups').'</th>');
                    610:                                 } else {
                    611:                                     $r->print('<th>'.&mt('Teams').'</th>');
                    612:                                 }
                    613:                                 $r->print('</tr>');
1.109     albertel  614:                                 foreach my $id (sort(keys(%{$content->{'roles'}}))) {
1.108     raeburn   615:                                     $r->print('<tr>');
                    616:                                     foreach my $item ('role','access','section','group') {
                    617:                                         $r->print('<td>');
                    618:                                         if ($item eq 'role') {
                    619:                                             my $ucscope = $scope;
                    620:                                             $ucscope =~ s/^(\w)/uc($1)/;
                    621:                                             my $role_output;  
1.109     albertel  622:                                             foreach my $role (@{$content->{'roles'}{$id}{$item}}) {
1.108     raeburn   623:                                                 if ($role eq 'all') {
                    624:                                                     $role_output .= $role.',';
                    625:                                                 } elsif ($role =~ /^cr/) {
                    626:                                                     $role_output .= (split('/',$role))[3].',';
                    627:                                                 } else {
                    628:                                                     $role_output .= &Apache::lonnet::plaintext($role,$ucscope).',';
                    629:                                                 }
                    630:                                             }
                    631:                                             $role_output =~ s/,$//;
                    632:                                             $r->print($role_output);  
                    633:                                         } else {
1.109     albertel  634:                                             $r->print(join(',',@{$content->{'roles'}{$id}{$item}}));
1.108     raeburn   635:                                         }
                    636:                                         $r->print('</td>');
                    637:                                     }
                    638:                                 }
                    639:                                 $r->print(&Apache::loncommon::end_data_table_row());
                    640:                                 $r->print(&Apache::loncommon::end_data_table());
                    641:                             } elsif ($scope eq 'domains') {
1.109     albertel  642:                                 $r->print(&mt('Domains: ').join(',',@{$content->{'dom'}}));
1.108     raeburn   643:                             } elsif ($scope eq 'users') {
1.109     albertel  644:                                 my $curr_user_list = &sort_users($content->{'users'});
1.108     raeburn   645:                                 $r->print(&mt('Users: ').$curr_user_list);
                    646:                             } else {
                    647:                                 $r->print('&nbsp;');
                    648:                             }
                    649:                         } else {
                    650:                             $r->print('&nbsp;');
                    651:                         }
                    652:                         $r->print('</td>');
1.104     raeburn   653:                         $r->print(&Apache::loncommon::end_data_table_row());
                    654:                         $count ++;
                    655:                     }
                    656:                 }
                    657:             }
                    658:             $r->print(&Apache::loncommon::end_data_table());
                    659:         } else {
                    660:             if ((@{$processing{'activate'}} > 0) || (@{$processing{'update'}} > 0)) {
                    661:                 $errors .= &mt('A problem occurred storing access control settings: [_1]',$outcome);
                    662:             }
                    663:         }
                    664:         if ($errors) { 
                    665:             $r->print($errors);
                    666:         }
                    667:     }
1.108     raeburn   668:     my $allnew = 0;
                    669:     my $totalnew = 0;
                    670:     my $status = 'new';
                    671:     my ($firstitem,$lastitem);
                    672:     foreach my $newitem ('course','group','domains','users') {
                    673:         $allnew += $env{'form.new'.$newitem};
                    674:     }
                    675:     if ($allnew > 0) {
                    676:         my $now = time;
                    677:         my $then = $now + (60*60*24*180); # six months approx.
                    678:         &open_form($r,$url,$group);
                    679:         foreach my $newitem ('course','group','domains','users') {
                    680:             if ($env{'form.new'.$newitem} > 0) {
                    681:                 $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 />');
                    682:                 $firstitem = $totalnew;
                    683:                 $lastitem = $totalnew + $env{'form.new'.$newitem};
                    684:                 $totalnew = $lastitem;
                    685:                 my @numbers;   
                    686:                 for (my $i=$firstitem; $i<$lastitem; $i++) {
                    687:                     push (@numbers,$i);
                    688:                 }
                    689:                 &display_access_row($r,$status,$newitem,\@numbers,
                    690:                                     $access_controls{$file_name},$now,$then);
                    691:             }
                    692:         }
                    693:         &close_form($r,$url,$group);
                    694:     } else {
                    695:         $r->print('<br /><a href="'.$url.'?access='.$env{'form.selectfile'}.
                    696:                   '&currentpath='.$env{'form.currentpath'}.'">'.
                    697:                    &mt('Display all access settings for this file').'</a>');
                    698:     }
1.104     raeburn   699:     return;
                    700: }
                    701: 
                    702: sub build_access_record {
1.108     raeburn   703:     my ($num,$scope,$start,$end,$chg) = @_;
1.109     albertel  704:     my $record = {
                    705: 	type => $scope,
                    706: 	time => {
                    707: 	    start => $start,
                    708: 	    end   => $end
                    709: 	    },
                    710: 	    };
                    711: 		
                    712:     if ($scope eq 'guest') {	
                    713:         $record->{'password'} = $env{'form.password'};
1.108     raeburn   714:     } elsif (($scope eq 'course') || ($scope eq 'group')) {
1.109     albertel  715:         $record->{'domain'} = $env{'form.crsdom_'.$num};
                    716: 	$record->{'number'} = $env{'form.crsnum_'.$num};
1.108     raeburn   717:         my @role_ids;
1.109     albertel  718:         my @delete_role_ids =
                    719:             &Apache::loncommon::get_env_multiple('form.delete_role_'.$num);
                    720: 	my @preserves =
                    721: 	    &Apache::loncommon::get_env_multiple('form.preserve_role_'.$num);
                    722: 	if (@delete_role_ids) {
                    723: 	    foreach my $id (@preserves) {
                    724: 		if (grep {$_ = $id} (@delete_role_ids)) {
                    725: 		    next;
                    726: 		}
                    727: 		push(@role_ids,$id); 
                    728: 	    }
                    729: 	} else {
                    730: 	    push(@role_ids,@preserves);
                    731: 	}
                    732: 
                    733: 	my $next_id = $env{'form.add_role_'.$num};
                    734: 	if ($next_id) {
                    735: 	    push(@role_ids,$next_id);
                    736: 	}
                    737: 
1.108     raeburn   738:         foreach my $id (@role_ids) {
                    739:             my (@roles,@accesses,@sections,@groups);
                    740:             if (($id == $next_id) && ($chg eq 'update')) {
1.109     albertel  741:                 @roles    = split(/,/,$env{'form.role_'.$num.'_'.$next_id});
1.108     raeburn   742:                 @accesses = split(/,/,$env{'form.access_'.$num.'_'.$next_id});
                    743:                 @sections = split(/,/,$env{'form.section_'.$num.'_'.$next_id});
1.109     albertel  744:                 @groups   = split(/,/,$env{'form.group_'.$num.'_'.$next_id});
1.108     raeburn   745:             } else {
                    746:                 @roles = &Apache::loncommon::get_env_multiple('form.role_'.$num.'_'.$id);
                    747:                 @accesses = &Apache::loncommon::get_env_multiple('form.access_'.$num.'_'.$id);
                    748:                 @sections = &Apache::loncommon::get_env_multiple('form.section_'.$num.'_'.$id);
                    749:                 @groups = &Apache::loncommon::get_env_multiple('form.group_'.$num.'_'.$id);
                    750:             }
1.109     albertel  751: 	    $record->{'roles'}{$id}{'role'}    = \@roles;
                    752: 	    $record->{'roles'}{$id}{'access'}  = \@accesses;
                    753: 	    $record->{'roles'}{$id}{'section'} = \@sections;
                    754: 	    $record->{'roles'}{$id}{'group'}   = \@groups;
1.108     raeburn   755:         }
                    756:     } elsif ($scope eq 'domains') {
                    757:         my @doms = &Apache::loncommon::get_env_multiple('form.dom_'.$num);
1.109     albertel  758: 	$record->{'dom'} = \@doms;
1.108     raeburn   759:     } elsif ($scope eq 'users') {
                    760:         my $userlist = $env{'form.users_'.$num};
1.109     albertel  761:         $userlist =~ s/\s+//sg;
                    762: 	my %userhash = map { ($_,1) } (split(/,/,$userlist));
                    763:         foreach my $user (keys(%userhash)) {
1.108     raeburn   764:             my ($uname,$udom) = split(/:/,$user);
1.109     albertel  765: 	    push(@{$record->{'users'}}, {
                    766: 		'uname' => $uname,
                    767: 		'udom'  => $udom
                    768: 		});
                    769: 	}
1.108     raeburn   770:     }
1.104     raeburn   771:     return $record;
                    772: }
                    773: 
                    774: sub get_dates_from_form {
                    775:     my ($id) = @_;
                    776:     my $startdate;
                    777:     my $enddate;
                    778:     $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$id);
                    779:     $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$id);
                    780:     if ( exists ($env{'form.noend_'.$id}) ) {
                    781:         $enddate = 0;
                    782:     }
                    783:     return ($startdate,$enddate);
                    784: }
                    785: 
1.108     raeburn   786: sub sort_users {
1.109     albertel  787:     my ($users) = @_; 
                    788:     my @curr_users = map {
                    789: 	$_->{'uname'}.':'.$_->{'udom'}
                    790:     } (@{$users});
                    791:     my $curr_user_list = join(",\n",sort(@curr_users));
1.108     raeburn   792:     return $curr_user_list;
                    793: }
                    794: 
1.104     raeburn   795: sub access_setting_table {
                    796:     my ($r,$access_controls) = @_;
                    797:     my ($public,$publictext);
1.108     raeburn   798:     $publictext = &mt('Off');
1.104     raeburn   799:     my ($guest,$guesttext);
1.108     raeburn   800:     $guesttext = &mt('Off');
1.104     raeburn   801:     my @courses = ();
                    802:     my @groups = ();
                    803:     my @domains = ();
                    804:     my @users = ();
                    805:     my $now = time;
                    806:     my $then = $now + (60*60*24*180); # six months approx.
1.108     raeburn   807:     my ($num,$scope,$publicnum,$guestnum);
                    808:     my (%acl_count,%end,%start);
1.104     raeburn   809:     foreach my $key (sort(keys(%{$access_controls}))) {
1.108     raeburn   810:         ($num,$scope,$end{$key},$start{$key}) = &unpack_acc_key($key);
1.104     raeburn   811:         if ($scope eq 'public') {
                    812:             $public = $key;
1.108     raeburn   813:             $publicnum = $num;
                    814:             $publictext = &acl_status($start{$key},$end{$key},$now);
                    815:         } elsif ($scope eq 'guest') {
                    816:             $guest=$key;
                    817:             $guestnum = $num;  
                    818:             $guesttext = &acl_status($start{$key},$end{$key},$now);
                    819:         } elsif ($scope eq 'course') {
                    820:             push(@courses,$key);
                    821:         } elsif ($scope eq 'group') {
                    822:             push(@groups,$key);
                    823:         } elsif ($scope eq 'domains') {
                    824:             push(@domains,$key);
                    825:         } elsif ($scope eq 'users') {
                    826:             push(@users,$key);
1.104     raeburn   827:         }
1.108     raeburn   828:         $acl_count{$scope} ++;
1.104     raeburn   829:     }
1.108     raeburn   830:     $r->print('<table border="0"><tr><td valign="top">');
                    831:     $r->print('<h3>'.&mt('Public access:').' '.$publictext.'</h3>');
1.104     raeburn   832:     $r->print(&Apache::loncommon::start_data_table());
1.110     albertel  833:     $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn   834:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').'</th>');
1.110     albertel  835:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.104     raeburn   836:     $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn   837:     if ($public) {
                    838:         $r->print('<td>'.&actionbox('old',$publicnum,'public').'</td><td>'.
                    839:                   &dateboxes($publicnum,$start{$public},$end{$public}).'</td>');
                    840:     } else {
                    841:         $r->print('<td>'.&actionbox('new','0','public').'</td><td>'.
                    842:                   &dateboxes('0',$now,$then).'</td>');
                    843:     }
                    844:     $r->print(&Apache::loncommon::end_data_table_row());
                    845:     $r->print(&Apache::loncommon::end_data_table());
                    846:     $r->print('</td><td width="40">&nbsp;</td><td valign="top">');
                    847:     $r->print('<h3>'.&mt('Password-protected access:').' '.$guesttext.'</h3>');
1.104     raeburn   848:     $r->print(&Apache::loncommon::start_data_table());
1.110     albertel  849:     $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn   850:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').
                    851:               '</th><th>'. &mt('Password').'</th>');
1.110     albertel  852:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.108     raeburn   853:     $r->print(&Apache::loncommon::start_data_table_row());
                    854:     my $passwd;
                    855:     if ($guest) {
1.109     albertel  856:         $passwd = $$access_controls{$guest}{'password'};
1.108     raeburn   857:         $r->print('<td>'.&actionbox('old',$guestnum,'guest').'</td><td>'.
                    858:                   &dateboxes($guestnum,$start{$guest},$end{$guest}).'</td>');
                    859:     } else {
                    860:         $r->print('<td>'.&actionbox('new','1','guest').'</td><td>'.
                    861:                   &dateboxes('1',$now,$then).'</td>');
1.104     raeburn   862:     }
1.108     raeburn   863:     $r->print('<td><input type="text" size="15" name="password" value="'.
                    864:               $passwd.'" /></td>');
1.104     raeburn   865:     $r->print(&Apache::loncommon::end_data_table_row());
                    866:     $r->print(&Apache::loncommon::end_data_table());
1.108     raeburn   867:     $r->print('</td></tr><tr><td colspan="3">&nbsp;</td></tr><tr><td valign="top">');
                    868:     &access_element($r,'domains',\%acl_count,\@domains,$access_controls,$now,$then);
                    869:     $r->print('</td><td>&nbsp;</td><td valign="top">');
                    870:     &access_element($r,'users',\%acl_count,\@users,$access_controls,$now,$then);
                    871:     $r->print('</td></tr><tr><td colspan="3"></td></tr><tr>');
                    872:     if (@courses > 0 || @groups > 0) {
                    873:         $r->print('<td colspan="3" valign="top">');
                    874:     } else {
                    875:         $r->print('<td valign="top">');
                    876:     }
                    877:     &access_element($r,'course',\%acl_count,\@courses,$access_controls,$now,$then);
1.104     raeburn   878:     $r->print('</td>');
1.108     raeburn   879:     if (@courses > 0 || @groups > 0) {
                    880:         $r->print('</tr><tr><td colspan="3">&nbsp;</td></tr><tr><td colspan="3" valign="top">');
                    881:     } else {
                    882:         $r->print('<td>&nbsp;</td><td valign="top">');
                    883:     }
                    884:     &access_element($r,'group',\%acl_count,\@groups,$access_controls,$now,$then);
                    885:     $r->print('</td></tr></table>');
                    886: }
                    887: 
                    888: sub acl_status {
                    889:     my ($start,$end,$now) = @_;
                    890:     if ($start > $now) {
                    891:         return &mt('Inactive');
                    892:     }
                    893:     if ($end && $end<$now) {
                    894:         return &mt('Inactive');
                    895:     }
                    896:     return &mt('Active');
                    897: }
                    898: 
                    899: sub access_element {
                    900:     my ($r,$type,$acl_count,$items,$access_controls,$now,$then) = @_;
                    901:     my $title = $type;
                    902:     $title =~ s/s$//;
                    903:     $title =~ s/^(\w)/uc($1)/e;
                    904:     $r->print('<h3>'.&mt('[_1]-based conditional access: ',$title));
                    905:     if ($$acl_count{$type}) {
                    906:         $r->print($$acl_count{$type}.' ');
                    907:         if ($$acl_count{$type} > 1) {
                    908:             $r->print(&mt('conditions'));
                    909:         } else {
                    910:             $r->print(&mt('condition'));
                    911:         }
                    912:     } else {
                    913:         $r->print(&mt('Off'));
                    914:     }
                    915:     $r->print('</h3>');
                    916:     &display_access_row($r,'old',$type,$items,$access_controls,$now,$then);
                    917:     return;
                    918: }
                    919: 
                    920: sub display_access_row {
                    921:     my ($r,$status,$type,$items,$access_controls,$now,$then) = @_;
                    922:     if (@{$items} > 0) {
                    923:         my @all_doms;
                    924:         my $tablecolor;
                    925:         my $colspan = 3;
                    926:         my $uctype = $type;
                    927:         $uctype =~ s/^(\w)/uc($1)/e;
                    928:         $r->print(&Apache::loncommon::start_data_table());
1.110     albertel  929:         $r->print(&Apache::loncommon::start_data_table_header_row());
                    930:         $r->print('<th>'.&mt('Action?').'</th><th>'.&mt($uctype).'</th><th>'.
1.108     raeburn   931:               &mt('Dates available').'</th>');
                    932:         if (($type eq 'course') || ($type eq 'group')) {
                    933:             $r->print('<th>'.&mt('Allowed [_1] member affiliations',$type).
                    934:                       '</th>');
                    935:             $colspan ++;
                    936:             my $function = &Apache::loncommon::get_users_function();
                    937:             $tablecolor=&Apache::loncommon::designparm($function.'.tabbg');
                    938:         } elsif ($type eq 'domains') {
                    939:             @all_doms = &Apache::loncommon::get_domains();
                    940:         }
1.110     albertel  941:         $r->print(&Apache::loncommon::end_data_table_header_row());
                    942: 	$r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn   943:         foreach my $key (@{$items}) {
                    944:             if (($type eq 'course') || ($type eq 'group')) {
                    945:                 &course_row($r,$status,$type,$key,$access_controls,
                    946:                             $tablecolor,$now,$then);
                    947:             } elsif ($type eq 'domains') {
                    948:                 &domains_row($r,$status,$key,\@all_doms,$access_controls,$now,
                    949:                             $then);
                    950:             } elsif ($type eq 'users') {
                    951:                 &users_row($r,$status,$key,$access_controls,$now,$then);
                    952:             }
                    953:         }
                    954:         $r->print(&Apache::loncommon::end_data_table_row());
                    955:         if ($status eq 'old') {
1.111     albertel  956: 	    $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn   957:             $r->print('<td colspan="',$colspan.'">'.&additional_item($type).
                    958:                       '</td>');
1.111     albertel  959: 	    $r->print(&Apache::loncommon::end_data_table_row());
1.108     raeburn   960:         }
                    961:         $r->print(&Apache::loncommon::end_data_table());
                    962:     } else {
                    963:         $r->print(&mt('No [_1]-based conditions defined.<br />',$type).
                    964:                   &additional_item($type));
                    965:     }
                    966:     return;
                    967: }
                    968: 
1.110     albertel  969: sub course_js {
                    970:     return qq|
1.108     raeburn   971: <script type="text/javascript">
                    972: function setRoleOptions(caller,num,cdom,cnum,type) {
                    973:     addIndexnum = getCallerIndex(caller);
                    974:     updateIndexnum = getIndex('update',num);
                    975:     if (caller.checked) {
                    976:         document.portform.elements[updateIndexnum].checked = true;
                    977:         var url = '/adm/portfolio?action=rolepicker&setroles='+addIndexnum+'&cnum='+cnum+'&cdom='+cdom+'&type='+type;
                    978:         var title = 'Roles_Chooser';
                    979:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    980:         options += ',width=700,height=600';
                    981:         rolebrowser = open(url,title,options,'1');
                    982:         rolebrowser.focus();
                    983:     } else {
                    984:         for (var j=0;j<5;j++) {
                    985:             document.portform.elements[addIndexnum+j].value = '';
                    986:         }
                    987:     }
                    988: }
                    989: 
                    990: function getCallerIndex(caller) {
                    991:     for (var i=0;i<document.portform.elements.length;i++) {
                    992:         if (document.portform.elements[i] == caller) {
                    993:             return i;
                    994:         }
                    995:     }
                    996:     return -1;
                    997: }
                    998: 
                    999: function getIndex(name,value) {
                   1000:     for (var i=0;i<document.portform.elements.length;i++) {
                   1001:         if (document.portform.elements[i].name == name && document.portform.elements[i].value == value) {
                   1002:             return i;
                   1003:         }
                   1004:     }
                   1005:     return -1;
                   1006: }
                   1007: 
                   1008: </script>
1.110     albertel 1009: |;
                   1010: }
                   1011: 
                   1012: sub course_row {
                   1013:     my ($r,$status,$type,$item,$access_controls,$tablecolor,$now,$then) = @_;
                   1014:     my %content;
                   1015:     my $defdom = $env{'user.domain'};
                   1016:     if ($status eq 'old') {
                   1017:         $defdom =  $$access_controls{$item}{'domain'}; 
                   1018:     }
                   1019:     my $js = &Apache::loncommon::coursebrowser_javascript($defdom)
                   1020: 	.&course_js();
1.108     raeburn  1021:     my $crsgrptext = 'Groups';
                   1022:     if ($type eq 'group') {
                   1023:         $crsgrptext = 'Teams';
                   1024:     }
                   1025:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1026:                                                     $type);
1.110     albertel 1027:     $r->print('<td>'.$js.&actionbox($status,$num,$scope).'</td>');
1.108     raeburn  1028:     if ($status eq 'old') {
                   1029:         my $cid = $content{'domain'}.'_'.$content{'number'};
                   1030:         my %course_description = &Apache::lonnet::coursedescription($cid);
                   1031:         $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>');
                   1032:     } elsif ($status eq 'new') {
                   1033:         my $uctype = $type;
                   1034:         $uctype =~ s/^(\w)/uc($1)/e;
                   1035:         $r->print('<td>'.&Apache::loncommon::selectcourse_link('portform','crsnum_'.$num,'crsdom_'.$num,'description_'.$num,undef,undef,$uctype).'&nbsp;&nbsp;<input type="text" name="description_'.$num.'" size="30" /><input type="hidden" name="crsdom_'.$num.'" /><input type="hidden" name="crsnum_'.$num.'" /></td>');
                   1036:     }
                   1037:     $r->print('<td>'.&dateboxes($num,$start,$end).'</td>');
                   1038:     $r->print('<td><table border="0"><tr bgcolor="'.$tablecolor.'">');
                   1039:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Roles').'</th><th>'.
                   1040:               &mt('Access').'</th><th>'.&mt('Sections').'</th><th>'.
                   1041:               &mt($crsgrptext).'</th></tr>');
                   1042:     if ($status eq 'old') {
                   1043:         my $max_id = 0;
                   1044:         foreach my $role_id (sort(keys(%{$content{'roles'}}))) {
                   1045:             if ($role_id > $max_id) {
                   1046:                 $max_id = $role_id;
                   1047:             }
                   1048:             $max_id ++;
                   1049:             my $role_selects = &role_selectors($num,$role_id,$status,$type,\%content,'display');
1.112   ! albertel 1050:             $r->print('<tr><td><span style="white-space: nowrap"><label><input type="checkbox" name="delete_role_'.$num.'" value="'.$role_id.'" />'.&mt('Delete').'</label></span><br /><input type="hidden" name="preserve_role_'.$num.'" value="'.$role_id.'" /></td>'.$role_selects.'</tr>');
1.108     raeburn  1051:         }
                   1052:         $r->print('</table><br />'.&mt('Add a roles-based condition').'&nbsp;<input type="checkbox" name ="add_role_'.$num.'" onClick="javascript:setRoleOptions(this,'."'$num','$content{domain}','$content{number}','Course'".')" value="'.$max_id.'" /><input type="hidden" name="role_'.$num.'_'.$max_id.'" /><input type="hidden" name="access_'.$num.'_'.$max_id.'" /><input type="hidden" name="section_'.$num.'_'.$max_id.'" /><input type="hidden" name="group_'.$num.'_'.$max_id.'" /></td>');
                   1053:     } elsif ($status eq 'new') {
                   1054:         my $role_id = 1;
                   1055:         my $role_selects = &role_selectors($num,$role_id,$status,$type,undef,'display');
                   1056:         $r->print('<tr><td><input type="checkbox" name="add_role_'.$num.'" value="'.$role_id.'" checked="checked" />'.&mt('Add').'<input type="hidden" name="grplist_'.$num.'_'.$role_id.'" /></td>'.$role_selects);
                   1057:         $r->print('</tr></table></td>');
                   1058:     }
                   1059:     return;
                   1060: }
                   1061: 
                   1062: sub domains_row {
                   1063:     my ($r,$status,$item,$all_doms,$access_controls,$now,$then) = @_;
                   1064:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1065:                                                     'domains');
1.112   ! albertel 1066:     my $dom_select = '<select name="dom_'.$num.'" size="4" multiple="true">'.
1.108     raeburn  1067:                      ' <option value="">'.&mt('Please select').'</option>';
                   1068:     if ($status eq 'old') {
1.109     albertel 1069:         my $content =  $$access_controls{$item};
                   1070: 	foreach my $dom (@{$all_doms}) {
                   1071:             if ((@{$content->{'dom'}} > 0) 
                   1072: 		&& (grep(/^\Q$dom\E$/,@{$content->{'dom'}}))) {
1.108     raeburn  1073:                 $dom_select .= '<option value="'.$dom.'" selected>'.
                   1074:                                $dom.'</option>';
                   1075:             } else {
                   1076:                 $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
                   1077:             }
                   1078:         }
                   1079:     } else {
                   1080:         foreach my $dom (@{$all_doms}) {
                   1081:             $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
                   1082:         }
                   1083:     }
1.112   ! albertel 1084:     $dom_select .= '</select>';
1.108     raeburn  1085:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.$dom_select.
                   1086:               '</td><td>'.&dateboxes($num,$start,$end).'</td>');
                   1087: }
                   1088: 
                   1089: sub users_row {
                   1090:     my ($r,$status,$item,$access_controls,$now,$then) = @_;
                   1091:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1092:                                                     'users');
                   1093:     my $curr_user_list;
                   1094:     if ($status eq 'old') {
1.109     albertel 1095:         my $content = $$access_controls{$item};
                   1096:         $curr_user_list = &sort_users($content->{'users'});
1.108     raeburn  1097:     }
                   1098:     $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>');
                   1099: }
                   1100: 
                   1101: sub additional_item {
                   1102:     my ($type) = @_;
                   1103:     my $output = &mt('Add new [_1] condition(s)?',$type).'&nbsp;'.&mt('Number to add: ').'<input type="text" name="new'.$type.'" size="3" value="0" />';
                   1104:     return $output;
                   1105: }
                   1106: 
                   1107: sub actionbox {
                   1108:     my ($status,$num,$scope) = @_;
1.112   ! albertel 1109:     my $output = '<span style="white-space: nowrap"><label>';
1.108     raeburn  1110:     if ($status eq 'new') {
                   1111:         $output .= '<input type="checkbox" name="activate" value="'.$num.'" />'.
                   1112:         &mt('Activate');
                   1113:     } else {
                   1114:         $output .= '<input type="checkbox" name="delete" value="'.$num.
1.112   ! albertel 1115:                    '" />'.&mt('Delete').'</label></span><br /><span style="white-space: nowrap">'.
1.108     raeburn  1116:                    '<label><input type="checkbox" name="update" value="'.
                   1117:                    $num.'" />'.&mt('Update');
                   1118:     }
1.112   ! albertel 1119:     $output .= '</label></span><input type="hidden" name="scope_'.$num.                '" value="'.$scope.'" />';
1.108     raeburn  1120:     return $output;
                   1121: }
                   1122:                                                                                    
                   1123: sub dateboxes {
                   1124:     my ($num,$start,$end) = @_;
                   1125:     my $noend;
                   1126:     if ($end == 0) {
                   1127:         $noend = 'checked="checked"';
                   1128:     }
                   1129:     my $startdate = &Apache::lonhtmlcommon::date_setter('portform',
                   1130:                            'startdate_'.$num,$start,undef,undef,undef,1,undef,
                   1131:                             undef,undef,1);
                   1132:     my $enddate = &Apache::lonhtmlcommon::date_setter('portform',
                   1133:                                'enddate_'.$num,$end,undef,undef,undef,1,undef,
1.112   ! albertel 1134:                                 undef,undef,1). '&nbsp;&nbsp;<span style="white-space: nowrap"><label>'.
1.108     raeburn  1135:                                 '<input type="checkbox" name="noend_'.
                   1136:                                 $num.'" '.$noend.' />'.&mt('No end date').
1.112   ! albertel 1137:                                 '</label></span>';
1.108     raeburn  1138:                                                                                    
                   1139:     my $output = &mt('Start: ').$startdate.'<br />'.&mt('End: ').$enddate;
                   1140:     return $output;
                   1141: }
                   1142: 
                   1143: sub unpack_acc_key {
                   1144:     my ($acc_key) = @_;
                   1145:     my ($num,$scope,$end,$start) = ($acc_key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   1146:     return ($num,$scope,$end,$start);
                   1147: }
                   1148: 
                   1149: sub set_identifiers {
                   1150:     my ($status,$item,$now,$then,$scope) = @_;
                   1151:     if ($status eq 'old') {
                   1152:         return(&unpack_acc_key($item));
                   1153:     } else {
                   1154:         return($item,$scope,$then,$now);
                   1155:     }
                   1156: } 
                   1157: 
                   1158: sub role_selectors {
                   1159:     my ($num,$role_id,$status,$type,$content,$caller) = @_;
                   1160:     my ($output,$cdom,$cnum,$longid);
                   1161:     if ($caller eq 'display') {
                   1162:         $longid = '_'.$num.'_'.$role_id;
                   1163:         if ($status eq 'new') {
                   1164:             foreach my $item ('role','access','section','group') {
                   1165:                 $output .= '<td><select name="'.$item.$longid.'">'.
                   1166:                            '<option value="">'.&mt('Pick [_1] first',$type).
                   1167:                            '</option></select></td>';
                   1168:             }
                   1169:             return $output;
                   1170:         } else {
                   1171:             $cdom = $$content{'domain'};
                   1172:             $cnum = $$content{'number'};
                   1173:         }
                   1174:     } elsif ($caller eq 'rolepicker') {
                   1175:          $cdom = $env{'form.cdom'};
                   1176:          $cnum = $env{'form.cnum'};
                   1177:     }
                   1178:     my ($sections,$groups,$allroles,$rolehash,$accesshash) =
                   1179:             &Apache::loncommon::get_secgrprole_info($cdom,$cnum,1,$type);
                   1180:     if (!@{$sections}) {
                   1181:         @{$sections} = ('none');
                   1182:     } else {
                   1183:         unshift(@{$sections},('all','none'));
                   1184:     }
                   1185:     if (!@{$groups}) {
                   1186:         @{$groups} = ('none');
                   1187:     } else {
                   1188:         unshift(@{$groups},('all','none'));
                   1189:     }
                   1190:     my @allacesses = sort(keys(%{$accesshash}));
                   1191:     my (%sectionhash,%grouphash);
                   1192:     foreach my $sec (@{$sections}) {
                   1193:         $sectionhash{$sec} = $sec;
                   1194:     }
                   1195:     foreach my $grp (@{$groups}) {
                   1196:         $grouphash{$grp} = $grp;
                   1197:     }
                   1198:     my %lookup = (
                   1199:                    'role' => $rolehash,
                   1200:                    'access' => $accesshash,
                   1201:                    'section' => \%sectionhash,
                   1202:                    'group' => \%grouphash,
                   1203:                  );
                   1204:     my @allaccesses = sort(keys(%{$accesshash}));
                   1205:     my %allitems = (
                   1206:                     'role' => $allroles,
                   1207:                     'access' => \@allaccesses,
                   1208:                     'section' => $sections,
                   1209:                     'group' => $groups, 
                   1210:                    );
                   1211:     foreach my $item ('role','access','section','group') {
                   1212:         $output .= '<td><select name="'.$item.$longid.'" multiple="true" size="4">'."\n";
                   1213:         foreach my $entry (@{$allitems{$item}}) {
                   1214:             if ($caller eq 'display') {
                   1215:                 if ((@{$$content{'roles'}{$role_id}{$item}} > 0) && 
                   1216:                     (grep(/^\Q$entry\E$/,@{$$content{'roles'}{$role_id}{$item}}))) {
                   1217:                     $output .= '  <option value="'.$entry.'" selected>'.
                   1218:                                   $lookup{$item}{$entry}.'</option>';
                   1219:                     next;
                   1220:                 }
                   1221:             }
                   1222:             $output .= '  <option value="'.$entry.'">'.
                   1223:                        $lookup{$item}{$entry}.'</option>';
                   1224:         }
                   1225:         $output .= '</select>';
                   1226:     }
                   1227:     $output .= '</td>';
                   1228:     return $output;
                   1229: }
                   1230: 
                   1231: sub role_options_window {
                   1232:     my ($r) = @_;
                   1233:     my $cdom = $env{'form.cdom'};
                   1234:     my $cnum = $env{'form.cnum'};
                   1235:     my $type = $env{'form.type'};
                   1236:     my $addindex = $env{'form.setroles'};
                   1237:     my $role_selects = &role_selectors(1,1,'new',$type,undef,'rolepicker');
                   1238:     $r->print(<<"END_SCRIPT");
                   1239: <script type="text/javascript">
                   1240: function setRoles() {
                   1241:     var addidx = $addindex+1;
                   1242:     for (var i=0; i<4; i++) {
                   1243:         var copylist = '';
                   1244:         for (var j=0; j<document.rolepicker.elements[i].length; j++) {
                   1245:             if (document.rolepicker.elements[i].options[j].selected) {
                   1246:                 copylist = copylist + document.rolepicker.elements[i].options[j].value + ',';
                   1247:             }
                   1248:         }
                   1249:         copylist = copylist.substr(0,copylist.length-1);
                   1250:         opener.document.portform.elements[addidx+i].value = copylist;
                   1251:     }
                   1252:     self.close();
                   1253: }
                   1254: </script>
                   1255: END_SCRIPT
                   1256:     $r->print(&mt('Select roles, course status, section(s) and group(s) for users who will be able to access the portfolio file.'));
                   1257:     $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()" />');
                   1258:     return;
1.104     raeburn  1259: }
                   1260: 
1.47      banghart 1261: sub select_files {
1.104     raeburn  1262:     my ($r,$group) = @_;
1.82      albertel 1263:     if ($env{'form.continue'} eq 'true') {
1.60      banghart 1264:         # here we update the selections for the currentpath
                   1265:         # eventually, have to handle removing those not checked, but . . . 
1.83      banghart 1266:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
                   1267:         if (scalar(@items)){
1.85      banghart 1268:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
1.83      banghart 1269:         }
1.62      banghart 1270:     } else {
                   1271:             #empty the file for a fresh start
1.83      banghart 1272:             &Apache::lonnet::clear_selected_files($env{'user.name'});
1.62      banghart 1273:     }
1.82      albertel 1274:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
1.62      banghart 1275:     my $java_files = join ",", @files;
                   1276:     if ($java_files) {
                   1277:         $java_files.=',';
1.60      banghart 1278:     }
1.63      banghart 1279:     my $javascript =(<<ENDSMP);
1.48      banghart 1280:         <script language='javascript'>
                   1281:         function finishSelect() {
1.62      banghart 1282: ENDSMP
1.63      banghart 1283:     $javascript .= 'fileList = "'.$java_files.'";';
                   1284:     $javascript .= (<<ENDSMP);
1.49      banghart 1285:             for (i=0;i<document.forms.checkselect.length;i++) { 
                   1286:                 if (document.forms.checkselect[i].checked){
1.54      banghart 1287:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
1.49      banghart 1288:                 }
                   1289:             }
                   1290:             opener.document.forms.lonhomework.
                   1291: ENDSMP
1.82      albertel 1292:     $javascript .= $env{'form.fieldname'};
1.63      banghart 1293:     $javascript .= (<<ENDSMP);
1.49      banghart 1294:         .value=fileList;
1.48      banghart 1295:             self.close();
                   1296:         }
                   1297:         </script>
                   1298: ENDSMP
1.63      banghart 1299:     $r->print($javascript);
1.47      banghart 1300:     $r->print("<h1>Select portfolio files</h1>
1.88      albertel 1301:                 Check as many as you wish in response to the problem.<br />");
                   1302:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
                   1303:     if (@otherfiles) {
                   1304: 	$r->print("<strong>Files selected from other directories:</strong><br />");
                   1305: 	foreach my $file (@otherfiles) {
                   1306: 	    $r->print($file."<br />");
                   1307: 	}
1.60      banghart 1308:     }
1.47      banghart 1309: }
1.24      albertel 1310: sub upload {
1.94      raeburn  1311:     my ($r,$url,$group)=@_;
1.82      albertel 1312:     my $fname=$env{'form.uploaddoc.filename'};
                   1313:     my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
1.38      banghart 1314:     my $disk_quota = 20000; # expressed in k
1.34      banghart 1315:     $fname=&Apache::lonnet::clean_filename($fname);
1.94      raeburn  1316: 
                   1317:     my $portfolio_root=&get_portfolio_root($group);
                   1318:     my ($uname,$udom) = &get_name_dom($group);
                   1319:     my $port_path = &get_port_path($group);
1.38      banghart 1320:     # Fixme --- Move the checking for existing file to LOND error return
1.94      raeburn  1321:     my @dir_list=&get_dir_list($portfolio_root,$group);
1.34      banghart 1322:     my $found_file = 0;
1.76      banghart 1323:     my $locked_file = 0;
1.33      banghart 1324:     foreach my $line (@dir_list) {
1.76      banghart 1325:         my ($file_name)=split(/\&/,$line,2);
                   1326:         if ($file_name eq $fname){
1.102     raeburn  1327:             $file_name = $env{'form.currentpath'}.$file_name;
                   1328:             $file_name = &prepend_group($file_name,$group);
1.33      banghart 1329:             $found_file = 1;
1.102     raeburn  1330:             if (defined($group)) {
                   1331:                 $file_name = $group.'/'.$file_name;
                   1332:             }
                   1333:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.76      banghart 1334:                 $locked_file = 1;
                   1335:             } 
1.33      banghart 1336:         }
                   1337:     }
1.94      raeburn  1338:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root);
1.87      albertel 1339:     if (($current_disk_usage + $filesize) > $disk_quota){
1.86      albertel 1340:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes)</strong>. Disk quota will be exceeded.'.
1.38      banghart 1341:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
1.94      raeburn  1342:         $r->print(&done('Back',$url,$group));
1.38      banghart 1343:     } 
1.76      banghart 1344:     elsif ($found_file){
                   1345:         if ($locked_file){
1.94      raeburn  1346:             $r->print('<font color="red">Unable to upload <strong>'.$fname.'</strong>, a <strong>locked</strong> file by that name was found in <strong>'.$port_path.$env{'form.currentpath'}.'</strong></font>'.
1.76      banghart 1347:                   '<br />You will be able to rename or delete existing '.$fname.' after a grade has been assigned.');
1.94      raeburn  1348:             $r->print(&done('Back',$url,$group));      
1.76      banghart 1349:         } else {   
1.94      raeburn  1350:             $r->print('<font color="red">Unable to upload <strong>'.$fname.'</strong>, a file by that name was found in <strong>'.$port_path.$env{'form.currentpath'}.'</strong></font>'.
                   1351:                   '<br />To upload, rename or delete existing '.$fname.' in '.$port_path.$env{'form.currentpath'});
                   1352:             $r->print(&done('Back',$url,$group));
1.76      banghart 1353:         }
1.33      banghart 1354:     } else {
                   1355:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
1.94      raeburn  1356: 	        	 $port_path.$env{'form.currentpath'});
1.33      banghart 1357:         if ($result !~ m|^/uploaded/|) {
1.34      banghart 1358:             $r->print('<font color="red"> An errror occured ('.$result.
                   1359: 	              ') while trying to upload '.&display_file().'</font><br />');
1.94      raeburn  1360: 	    $r->print(&done('Back',$url,$group));
1.76      banghart 1361:         } else {
1.94      raeburn  1362:             $r->print(&done(undef,$url,$group));
1.33      banghart 1363:         }
1.25      albertel 1364:     }
                   1365: }
1.80      banghart 1366: sub lock_info {
1.94      raeburn  1367:     my ($r,$url,$group) = @_;
                   1368:     my ($uname,$udom) = &get_name_dom($group);
1.102     raeburn  1369:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                   1370:                                                                        $uname);
1.84      banghart 1371:     my $file_name = $env{'form.lockinfo'};
1.102     raeburn  1372:     $file_name = &prepend_group($file_name,$group);
                   1373:     if (defined($file_name) && defined($$current_permissions{$file_name})) {
                   1374:         foreach my $array_item (@{$$current_permissions{$file_name}}) {
                   1375:             if (ref($array_item) eq 'ARRAY') {
                   1376:                 my $filetext;
                   1377:                 if (defined($group)) {
                   1378:                     $filetext = '<strong>'.$env{'form.lockinfo'}.
                   1379:                                     '</strong> (group: '.$group.')'; 
                   1380:                 } else {
                   1381:                     $filetext = '<strong>'.$file_name.'</strong>';
                   1382:                 } 
                   1383:                 $r->print(&mt('[_1] was submitted in response to problem: ',
                   1384:                               $filetext).
                   1385:                           '<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
                   1386:                           '</strong><br />');
                   1387:                 my %course_description = &Apache::lonnet::coursedescription($$array_item[1]);
                   1388:                 $r->print(&mt('In the course: <strong>[_1]</strong><br />',
                   1389:                               $course_description{'description'}));
                   1390:                 # $r->print('the third is '.$$array_item[2].'<br>');
                   1391:                 # $r->print("item is $$array_item[0]<br> and $$array_item[0]");
1.85      banghart 1392:             }
1.102     raeburn  1393:         }
1.84      banghart 1394:     }
1.94      raeburn  1395:     $r->print(&done('Back',$url,$group));
1.80      banghart 1396:     return 'ok';
                   1397: }
1.25      albertel 1398: sub createdir {
1.94      raeburn  1399:     my ($r,$url,$group)=@_;
1.82      albertel 1400:     my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
1.28      albertel 1401:     if ($newdir eq '') {
1.37      banghart 1402:     	$r->print('<font color="red">'.
                   1403: 	    	  &mt("Error: no directory name was provided.").
                   1404: 		      '</font><br />');
1.94      raeburn  1405: 	    $r->print(&done(undef,$url,$group));
1.37      banghart 1406: 	    return;
1.94      raeburn  1407:     }
                   1408:     my $portfolio_root = &get_portfolio_root($group); 
                   1409:     my @dir_list=&get_dir_list($portfolio_root,$group);
1.37      banghart 1410:     my $found_file = 0;
                   1411:     foreach my $line (@dir_list) {
                   1412:         my ($filename)=split(/\&/,$line,2);
                   1413:         if ($filename eq $newdir){
                   1414:             $found_file = 1;
                   1415:         }
                   1416:     }
                   1417:     if ($found_file){
                   1418:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
                   1419:     	            ' </strong>a file or directory by that name already exists.</font><br />');
                   1420:     } else {
1.94      raeburn  1421:         my ($uname,$udom) = &get_name_dom($group);
                   1422:         my $port_path = &get_port_path($group);
                   1423:         my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
                   1424: 	         $port_path.$env{'form.currentpath'}.$newdir);
1.37      banghart 1425:         if ($result ne 'ok') {
                   1426:     	    $r->print('<font color="red"> An errror occured ('.$result.
                   1427: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
                   1428:         }
1.24      albertel 1429:     }
1.82      albertel 1430:     if ($newdir ne $env{'form.newdir'}) {
                   1431:         $r->print("The new directory name was changed from:<br /><strong>".$env{'form.newdir'}."</strong> to <strong>$newdir </strong>");  
1.67      banghart 1432:     }
1.94      raeburn  1433:     $r->print(&done(undef,$url,$group));
                   1434: }
                   1435: 
                   1436: sub get_portfolio_root {
                   1437:     my ($group) = @_;
                   1438:     my ($portfolio_root,$udom,$uname,$path);
                   1439:     ($uname,$udom) = &get_name_dom($group);
                   1440:     if (defined($group)) {
                   1441:         $path = '/userfiles/groups/'.$group.'/portfolio';
                   1442:     } else {
                   1443:         $path = '/userfiles/portfolio';
                   1444:     }
                   1445:     return (&Apache::loncommon::propath($udom,$uname).$path);
                   1446: }
                   1447: 
                   1448: sub get_dir_list {
                   1449:     my ($portfolio_root,$group) = @_;
                   1450:     my ($uname,$udom) = &get_name_dom($group);
                   1451:     return &Apache::lonnet::dirlist($env{'form.currentpath'},
                   1452:                                           $udom,$uname,$portfolio_root);
                   1453: }
                   1454: 
                   1455: sub get_name_dom {
                   1456:     my ($group) = @_;
                   1457:     my ($uname,$udom);
                   1458:     if (defined($group)) {
                   1459:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1460:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1461:     } else {
                   1462:         $udom = $env{'user.domain'};
                   1463:         $uname = $env{'user.name'};
                   1464:     }
                   1465:     return ($uname,$udom);
                   1466: }
                   1467: 
1.102     raeburn  1468: sub prepend_group {
                   1469:     my ($filename,$group) = @_;
                   1470:     if (defined($group)) {
                   1471:         $filename = $group.'/'.$filename;
                   1472:     }
                   1473:     return $filename;
                   1474: }
                   1475: 
1.94      raeburn  1476: sub get_namespace {
                   1477:     my ($group) = @_;
                   1478:     my $namespace = 'portfolio';
                   1479:     if (defined($group)) {
                   1480:         my ($uname,$udom) = &get_name_dom($group);
                   1481:         $namespace .= '_'.$udom.'_'.$uname.'_'.$group;
                   1482:     }
                   1483:     return $namespace;
                   1484: }
                   1485: 
                   1486: sub get_port_path {
                   1487:     my ($group) = @_;
                   1488:     my $port_path;
                   1489:     if (defined($group)) {
                   1490:        $port_path = "groups/$group/portfolio";
                   1491:     } else {
                   1492:        $port_path = 'portfolio';
                   1493:     }
                   1494:     return $port_path;
1.24      albertel 1495: }
                   1496: 
                   1497: sub handler {
                   1498:     # this handles file management
                   1499:     my $r = shift;
1.73      banghart 1500:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.108     raeburn  1501:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
                   1502: 	  'fieldname','mode','rename','continue','group','access','setnum',
                   1503:           'cnum','cdom','type','setroles']);
1.94      raeburn  1504:     my ($uname,$udom,$portfolio_root,$url,$group,$caller,$title);
                   1505:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
                   1506:         $url = $1.$2;
                   1507:         $caller = $2;
                   1508:     }
                   1509:     if ($caller eq 'coursegrp_portfolio') {
                   1510:     #  Needs to be in a course
                   1511:         if (! ($env{'request.course.fn'})) {
                   1512:         # Not in a course
                   1513:             $env{'user.error.msg'}=
                   1514:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
                   1515:             return HTTP_NOT_ACCEPTABLE;
                   1516:         }
                   1517:         my $earlyout = 0;
                   1518:         my $view_permission = &Apache::lonnet::allowed('vcg',
                   1519:                                                 $env{'request.course.id'});
                   1520:         $group = $env{'form.group'};
                   1521:         $group =~ s/\W//g;
                   1522:         if ($group) {
                   1523:             ($uname,$udom) = &get_name_dom($group);
1.99      raeburn  1524:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
1.98      albertel 1525: 							       $group); 
                   1526:             if (%curr_groups) {
1.94      raeburn  1527:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
                   1528:                                       $env{'request.course.id'}.'/'.$group))) {
                   1529:                     $portfolio_root = &get_portfolio_root($group);
                   1530:                 } else {
                   1531:                     $r->print('You do not have the privileges required to access the shared files space for this group');
                   1532:                     $earlyout = 1;
                   1533:                 }
                   1534:             } else {
                   1535:                 $r->print('Not a valid group for this course');
                   1536:                 $earlyout = 1;
                   1537:             }
                   1538:             $title = &mt('Group files').' for '.$group; 
                   1539:         } else {
                   1540:             $r->print('Invalid group');
                   1541:             $earlyout = 1;
                   1542:         }
                   1543:         if ($earlyout) { return OK; }
                   1544:     } else {
                   1545:         ($uname,$udom) = &get_name_dom();
                   1546:         $portfolio_root = &get_portfolio_root();
                   1547:         $title = &mt('Portfolio Manager');
                   1548:     }
                   1549: 
1.24      albertel 1550:     &Apache::loncommon::no_cache($r);
                   1551:     &Apache::loncommon::content_type($r,'text/html');
                   1552:     $r->send_http_header;
                   1553:     # Give the LON-CAPA page header
1.82      albertel 1554:     if ($env{"form.mode"} eq 'selectfile'){
1.96      albertel 1555:         $r->print(&Apache::loncommon::start_page($title,undef,
1.97      albertel 1556: 						 {'only_body' => 1}));
1.108     raeburn  1557:     } elsif ($env{'form.action'} eq 'rolepicker') {
                   1558:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
                   1559:                                                  {'no_nav_bar'  => 1, }));
1.74      banghart 1560:     } else {
1.97      albertel 1561:         $r->print(&Apache::loncommon::start_page($title));
1.74      banghart 1562:     }
1.24      albertel 1563:     $r->rflush();
1.88      albertel 1564: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
1.40      banghart 1565:    	    $r->print('<font color="red"> No file was selected to upload.'.
                   1566:    	            'To upload a file, click <strong>Browse...</strong>'.
                   1567:    	            ', select a file, then click <strong>Upload</strong>,</font>');
                   1568: 	}
1.82      albertel 1569:     if ($env{'form.meta'}) {
1.94      raeburn  1570:         &open_form($r,$url);
1.82      albertel 1571: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
1.70      banghart 1572:         $r->print('Edit the meta data<br />');
1.94      raeburn  1573:         &close_form($r,$url,$group);
1.70      banghart 1574:     }
1.82      albertel 1575:     if ($env{'form.store'}) {
1.70      banghart 1576:     }
                   1577: 
1.82      albertel 1578:     if ($env{'form.uploaddoc.filename'}) {
1.94      raeburn  1579: 	&upload($r,$url,$group);
1.82      albertel 1580:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
1.94      raeburn  1581: 	&delete_confirmed($r,$url,$group);
1.82      albertel 1582:     } elsif ($env{'form.action'} eq 'delete') {
1.94      raeburn  1583: 	&delete($r,$url,$group);
1.82      albertel 1584:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
1.94      raeburn  1585: 	&delete_dir_confirmed($r,$url,$group);
1.82      albertel 1586:     } elsif ($env{'form.action'} eq 'deletedir'){
1.94      raeburn  1587: 	&delete_dir($r,$url,$group);
1.82      albertel 1588:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
1.94      raeburn  1589: 	&rename_confirmed($r,$url,$group);
1.82      albertel 1590:     } elsif ($env{'form.rename'}) {
                   1591:         $env{'form.selectfile'} = $env{'form.rename'};
                   1592:         $env{'form.action'} = 'rename';
1.94      raeburn  1593: 	&rename($r,$url,$group);
1.104     raeburn  1594:     } elsif ($env{'form.access'}) {
                   1595:         $env{'form.selectfile'} = $env{'form.access'};
                   1596:         $env{'form.action'} = 'chgaccess';
                   1597:         &display_access($r,$url,$group);
                   1598:     } elsif ($env{'form.action'} eq 'chgaccess') {
                   1599:         &update_access($r,$url,$group);
1.108     raeburn  1600:     } elsif ($env{'form.action'} eq 'rolepicker') {
                   1601:         &role_options_window($r);
1.82      albertel 1602:     } elsif ($env{'form.createdir'}) {
1.94      raeburn  1603: 	&createdir($r,$url,$group);
1.82      albertel 1604:     } elsif ($env{'form.lockinfo'}) {
1.94      raeburn  1605:         &lock_info($r,$url,$group);
1.24      albertel 1606:     } else {
                   1607: 	my $current_path='/';
1.82      albertel 1608: 	if ($env{'form.currentpath'}) {
                   1609: 	    $current_path = $env{'form.currentpath'};
1.24      albertel 1610: 	}
1.94      raeburn  1611:         my @dir_list=&get_dir_list($portfolio_root,$group);
1.46      albertel 1612: 	if ($dir_list[0] eq 'no_such_dir'){
                   1613: 	    # two main reasons for this:
                   1614:             #    1) never been here, so directory structure not created
                   1615: 	    #    2) back-button navigation after deleting a directory
                   1616: 	    if ($current_path eq '/'){
1.100     albertel 1617: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
                   1618: 					       &get_port_path($group));
1.46      albertel 1619: 	    } else {
                   1620:                 # some directory that snuck in get rid of the directory
                   1621:                 # from the recent pulldown, just in case
                   1622: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
                   1623: 						      [$current_path]);
                   1624: 		$current_path = '/'; # force it back to the root        
                   1625: 	    }
                   1626: 	    # now grab the directory list again, for the first time
                   1627: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
1.94      raeburn  1628: 					    $udom,$uname,$portfolio_root);
1.43      banghart 1629:         }
1.46      albertel 1630: 	# need to know if directory is empty so it can be removed if desired
                   1631: 	my $is_empty=(@dir_list == 2);
1.94      raeburn  1632: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,$group);
                   1633:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group);
1.95      albertel 1634: 	$r->print(&Apache::loncommon::end_page());
1.30      banghart 1635:     }
1.90      albertel 1636:     return OK;
1.2       banghart 1637: }
1.1       banghart 1638: 1;
                   1639: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
500 Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.