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

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

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