File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.121.2.7: download - view: text, annotated - select for diffs
Wed Jul 19 15:58:33 2006 UTC (17 years, 11 months ago) by albertel
- backprot 1.136 1.137 1.138

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

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