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

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

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