File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.121.2.5: download - view: text, annotated - select for diffs
Mon Jul 3 04:25:08 2006 UTC (17 years, 11 months ago) by albertel
CVS tags: version_2_1_99_0
- backport 1.127 1.128

    1: # The LearningOnline Network
    2: # portfolio browser
    3: #
    4: # $Id: portfolio.pm,v 1.121.2.5 2006/07/03 04:25:08 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 HTML::Entities;
   40: use LONCAPA;
   41: 
   42: # receives a file name and path stub from username/userfiles/portfolio/
   43: # returns an anchor tag consisting encoding filename and currentpath
   44: sub make_anchor {
   45:     my ($url, $filename, $current_path, $current_mode, $field_name,
   46:         $continue_select,$group) = @_;
   47:     if ($continue_select ne 'true') {$continue_select = 'false'};
   48:     my $anchor = '<a href="'.$url.'?selectfile='.$filename.'&amp;currentpath='.$current_path.'&amp;mode='.$current_mode.'&amp;continue='.$continue_select.'&amp;fieldname='.$field_name;
   49:     if (defined($group)) {
   50:         $anchor .= '&amp;group='.$group;
   51:     }
   52:     $anchor .= '">'.$filename.'</a>';
   53:     return $anchor;
   54: }
   55: my $dirptr=16384;
   56: sub display_common {
   57:     my ($r,$url,$current_path,$is_empty,$dir_list,$group,$can_upload)=@_;
   58:     my $namespace = &get_namespace($group);
   59:     my $port_path = &get_port_path($group);
   60:     if ($can_upload) {
   61:         my $groupitem;
   62:         if (defined($group)) {
   63:             $groupitem = '<input type="hidden" name="group" value="'.$group.'" />';
   64:         } 
   65:         my $iconpath= $r->dir_config('lonIconsURL') . "/";
   66:         my %text=&Apache::lonlocal::texthash(
   67: 					 'upload' => 'Upload',
   68: 					 'upload_label' =>  
   69: 					 'Upload file to current directory:',
   70: 					 'createdir' => 'Create Subdirectory',
   71: 					 'createdir_label' => 
   72: 					 'Create subdirectory in current directory:');
   73:         my $escuri = &HTML::Entities::encode($r->uri,'&<>"');
   74:         $r->print(<<"TABLE"); 
   75: <table id="LC_portfolio_actions">
   76:   <tr id="LC_portfolio_upload">
   77:     <td class="LC_label">
   78:       $text{'upload_label'}
   79:     </td>
   80:     <td class="LC_value">
   81:       <form method="post" enctype="multipart/form-data" action="$escuri">
   82:         $groupitem 
   83:         <input name="uploaddoc" type="file" />
   84: 	<input type="hidden" name="currentpath" value="$current_path" />
   85: 	<input type="hidden" name="action" value="$env{"form.action"}" />
   86: 	<input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
   87: 	<input type="hidden" name="mode" value="$env{"form.mode"}" />
   88: 	<input type="submit" name="storeupl" value="$text{'upload'}" />
   89:       </form>
   90:     </td>
   91:   </tr>
   92:   <tr id="LC_portfolio_createdir">
   93:     <td class="LC_label">
   94:       $text{'createdir_label'}
   95:     </td>
   96:     <td class="LC_value">
   97:       <form method="post" action="$escuri">
   98:         <input name="newdir" type="input" />$groupitem
   99:         <input type="hidden" name="currentpath" value="$current_path" />
  100:         <input type="hidden" name="action" value="$env{"form.action"}" />
  101:         <input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
  102:         <input type="hidden" name="mode" value="$env{"form.mode"}" />
  103:         <input type="submit" name="createdir" value="$text{'createdir'}" />
  104:       </form>
  105:     </td>
  106:   </tr>
  107: </table>
  108: TABLE
  109:     }
  110:     my @tree = split (/\//,$current_path);
  111:     $r->print('<span class="LC_current_location">'.&make_anchor($url,$port_path,'/',$env{"form.mode"},$env{"form.fieldname"},$env{"form.continue"},$group).'/');
  112:     if (@tree > 1){
  113:         my $newCurrentPath = '';
  114:         for (my $i = 1; $i< @tree; $i++){
  115:             $newCurrentPath .= $tree[$i].'/';
  116:             $r->print(&make_anchor($url,$tree[$i],'/'.$newCurrentPath, $env{"form.mode"},$env{"form.fieldname"}, $env{"form.continue"},$group).'/');
  117:         }
  118:     }
  119:     $r->print('</span>');
  120:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
  121:     $r->print('<br /><form method="post" action="'.$url.'?mode='.$env{"form.mode"}.'&amp;fieldname='.$env{"form.fieldname"});
  122:     if (defined($group)) {
  123:         $r->print('&amp;group='.$group);
  124:     }
  125:     $r->print('">'.
  126: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
  127: 						    'this.form.submit();'));
  128:     $r->print("</form>");
  129: }
  130: sub display_directory {
  131:     my ($r,$url,$current_path,$is_empty,$dir_list,$group,$can_upload,
  132:         $can_modify,$can_delete,$can_setacl)=@_;
  133:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
  134:     my ($groupitem,$groupecho);
  135:     my $display_out;
  136:     my $select_mode;
  137:     my $checked_files;
  138:     my $port_path = &get_port_path($group);
  139:     my ($uname,$udom) = &get_name_dom($group);
  140:     my $access_admin_text = &mt('View Status');
  141:     if ($can_setacl) {
  142:         $access_admin_text = &mt('View/Change Status');
  143:     }
  144:     if ((defined($group)) && (defined($env{'request.course.id'}))) {
  145:        $groupitem = '<input type="hidden" name="group" value="'.$group.'" />'; 
  146:        $groupecho = '&amp;group='.$group;
  147:     }
  148: 
  149:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  150:                                                                         $uname);
  151:     my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash(
  152:                                                   $current_permissions,$group);
  153:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group);
  154:     my $now = time;
  155:     if ($env{"form.mode"} eq 'selectfile'){
  156: 	&select_files($r);
  157: 	$checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
  158: 	$select_mode = 'true';
  159:     } 
  160:     if ($is_empty && ($current_path ne '/') && $can_delete) {
  161:         $display_out = '<form method="post" action="'.$url.'">'.$groupitem.
  162:         '<input type="hidden" name="action" value="deletedir" />'.
  163:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
  164:         '<input type="hidden" name="selectfile" value="" />'.
  165:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
  166:         '</form>';
  167:         
  168:         $r->print($display_out);
  169: 	return;
  170:     }
  171:     if ($select_mode eq 'true') {
  172:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
  173:         $r->print('<table id="LC_browser">'.
  174:             '<tr><th>Select</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
  175:     } else {
  176:         $r->print('<form method="post" action="'.$url.'">');
  177:         $r->print('<table id="LC_browser">'.
  178:             '<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>');
  179:     }
  180:     if (defined($group)) {
  181:         $r->print("\n".$groupitem."\n");
  182:     }
  183:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
  184:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
  185:     my @dir_lines;
  186:     my %versioned;
  187:     foreach my $line (sort 
  188: 		      { 
  189: 			  my ($afile)=split('&',$a,2);
  190: 			  my ($bfile)=split('&',$b,2);
  191: 			  return (lc($afile) cmp lc($bfile));
  192: 		      } (@$dir_list)) {
  193:     	#$strip holds directory/file name
  194:     	#$dom 
  195:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
  196:     	$filename =~ s/\s+$//;
  197:     	my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
  198:     	if ($version) {
  199:     	    $versioned{$fname} .= $version.",";
  200:     	}
  201:         push(@dir_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
  202:     }
  203:     foreach my $line (@dir_lines) {
  204:         my ($filename,$dom,$testdir,$size,$mtime,$obs) = @$line;
  205:         my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
  206:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
  207:             if ($dirptr&$testdir) {
  208: 		my $colspan='colspan="2"';
  209:                 if ($select_mode eq 'true'){
  210: 		    undef($colspan);
  211:                 }
  212: 		$r->print('<tr class="LC_browser_folder"><td '.$colspan.'><img alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_closed.gif" /></td>');
  213:                 $r->print('<td>Go to ...</td>');
  214:                 $r->print('<td>'.&make_anchor($url,$filename.'/',$current_path.$filename.'/',$env{'form.mode'},$env{"form.fieldname"},$env{'form.continue'},$group).'</td>'); 
  215:                 $r->print('</tr>'); 
  216:             } else {
  217: 		my $css_class = 'LC_browser_file';
  218: 		my $line;
  219:                 my $version_flag;
  220:                 if (exists($versioned{$fname})) {
  221:                    $version_flag = "*";
  222:                 } else {
  223:                     $version_flag = "";
  224:                 }
  225:                my $fullpath = $current_path.$filename;
  226:                 $fullpath = &prepend_group($fullpath,$group);
  227:                 if ($select_mode eq 'true') {
  228:                     $line='<td><input type="checkbox" name="checkfile" value="'.$filename.'"';
  229: 		    if ($$checked_files{$filename} eq 'selected') {
  230:                         $line.=" checked ";
  231:                     }
  232: 		    $line.=' /></td>';
  233:                 } else {
  234:                     if (exists $locked_files{$fullpath}) {
  235:                         $line.='<td colspan="2"><a href="'.$url.'?lockinfo='.$current_path.$filename.$groupecho.'">Locked</a></td>';
  236: 			$css_class= 'LC_browser_file_locked';
  237:                     } else {
  238:                         if (!$can_modify) {
  239:                             $line .= '<td colspan="2">';
  240:                         } else {
  241:                             $line .= '<td>';
  242:                         }
  243:                         if ($can_delete) {
  244:                             $line .= '<input type="checkbox" name="selectfile" value="'.$filename.'" />';
  245:                         }
  246:                         if ($can_modify) {
  247:                             my $cat='<img alt="'.&mt('Catalog Information').
  248:                             '" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
  249:                             $line .= '<a href="'.$url.'?rename='.$filename.'&amp;currentpath='.$current_path.$groupecho.'">Rename</a>';
  250:                             $line .= '</td><td><a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>';
  251:                         }
  252:                         $line .= '</td>';
  253:                     }
  254:                 }
  255: 		$r->print('<tr class="'.$css_class.'">');
  256: 		$r->print($line);
  257: 		my $curr_access;
  258: 		if ($select_mode ne 'true') {
  259: 		    my $pub_access = 0;
  260: 		    my $guest_access = 0;
  261: 		    my $cond_access = 0;
  262: 		    foreach my $key (sort(keys(%{$access_controls{$fullpath}}))) {
  263: 			my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  264: 			if (($now > $start) && (!$end || $end > $now)) {
  265: 			    if ($scope eq 'public')  {
  266: 				$pub_access = 1;
  267: 			    } elsif ($scope eq 'guest') {
  268: 				$guest_access = 1;
  269: 			    } else {
  270: 				$cond_access = 1;
  271: 			    }
  272: 			}
  273: 		    }
  274: 		    if (!$pub_access && !$guest_access && !$cond_access) {
  275: 			$curr_access = &mt('Private');
  276: 		    } else {
  277: 			my @allaccesses; 
  278: 			if ($pub_access) {
  279: 			    push(@allaccesses,&mt('Public'));
  280: 			}
  281: 			if ($guest_access) {
  282: 			    push(@allaccesses,&mt('Passphrase-protected'));
  283: 			}
  284: 			if ($cond_access) {
  285: 			    push(@allaccesses,&mt('Conditional'));
  286: 			}
  287: 			$curr_access = join('+ ',@allaccesses);
  288: 		    }
  289: 		}
  290: 		$r->print('<td><img alt="" src="'.&Apache::loncommon::icon($filename).'" /></td>');
  291: 		$r->print('<td><a href="'.$href_location.$filename.'">'.
  292: 			  $filename.'</a></td>'); 
  293: 		$r->print('<td>'.$size.'</td>');
  294: 		$r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
  295: 		if ($select_mode ne 'true') {
  296: 		    $r->print('<td><span style="white-space: nowrap">'.
  297: 			      &mt($curr_access).'&nbsp;&nbsp;&nbsp;');
  298: 		    $r->print('<a href="'.$url.'?access='.$filename.
  299: 			      '&amp;currentpath='.$current_path.$groupecho.
  300: 			      '">'.$access_admin_text.'</a></span></td>');
  301: 		}
  302: 		$r->print('</tr>'.$/);
  303:             }
  304:         }
  305:     }
  306:     if ($select_mode eq 'true') {
  307:         $r->print('</table>
  308:             <input type="hidden" name="continue" value="true" />
  309:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'" />
  310:             <input type="hidden" name="mode" value="selectfile" />
  311:             <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
  312:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
  313:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
  314:         </form>');        
  315:     } else {
  316:         $r->print('</table>');
  317:         if ($can_delete) {
  318:             $r->print('
  319:         <input type="submit" name="doit" value="Delete Checked Files" />
  320:         <input type="hidden" name="action" value="delete" />
  321:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
  322:         </form>'
  323:             );
  324:         }
  325:     }
  326: }
  327: 
  328: sub open_form {
  329:     my ($r,$url)=@_;
  330:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  331:     $r->print('<form name="portform" method="post" action="'.$url.'">');
  332:     $r->print('<input type="hidden" name="action" value="'.
  333: 	      $env{'form.action'}.'" />');
  334:     $r->print('<input type="hidden" name="confirmed" value="1" />');
  335:     foreach (@files) {
  336:         $r->print('<input type="hidden" name="selectfile" value="'.
  337: 	      $_.'" />');
  338:     }
  339:     $r->print('<input type="hidden" name="currentpath" value="'.
  340: 	      $env{'form.currentpath'}.'" />');
  341: }
  342: 
  343: sub close_form {
  344:     my ($r,$url,$group,$button_text)=@_;
  345:     if (!defined($button_text)) {
  346:         $button_text = {
  347:                          'continue' => &mt('Continue'),
  348:                          'cancel'   => &mt('Cancel'),
  349:                        };
  350:     }
  351:     $r->print('<p><input type="submit" value="'.$button_text->{'continue'}.'" />');
  352:     if (defined($group)) {
  353:        $r->print("\n".'<input type="hidden" name="group" value="'.
  354:               $group.'" />');
  355:     }
  356:     $r->print('</p></form>');
  357:     $r->print('<form action="'.$url.'" method="post">
  358:                <p>
  359:               <input type="hidden" name="currentpath" value="'.
  360: 	      $env{'form.currentpath'}.'" />');
  361:     if (defined($group)) {
  362:        $r->print("\n".'<input type="hidden" name="group" value="'.
  363:               $group.'" />');
  364:     }
  365:     $r->print("\n".'   <input type="submit" value="'.$button_text->{'cancel'}.'" />
  366:                </p></form>'); 
  367: }
  368: 
  369: sub display_file {
  370:     my ($path,$filename)=@_;
  371:     my $display_file_text;
  372:     my $file_start='<span class="LC_filename">';
  373:     my $file_end='</span>';
  374:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
  375:     if (!defined($filename)) { 
  376:         $filename=$env{'form.selectfile'};
  377:         $display_file_text = $file_start.$path.$filename.$file_end;
  378:     } elsif (ref($filename) eq "ARRAY") {
  379:         foreach my $file (@$filename) {
  380:             $display_file_text .= $file_start.$path.$file.$file_end.'<br />';
  381:         }
  382:     } elsif (ref($filename) eq "SCALAR") {
  383:         $display_file_text = $file_start.$path.$$filename.$file_end;
  384:     } else {
  385: 	$display_file_text = $file_start.$path.$filename.$file_end;
  386:     }
  387:     return $display_file_text;
  388: }
  389: 
  390: sub done {
  391:     my ($message,$url,$group)=@_;
  392:     unless (defined $message) {
  393:         $message='Done';
  394:     }
  395:     my $result = '<h3><a href="'.$url.'?currentpath='.
  396: 	         $env{'form.currentpath'}.
  397: 	         '&fieldname='.$env{'form.fieldname'}.
  398: 	         '&mode='.$env{'form.mode'};
  399:     if (defined($group)) {
  400:         $result .= '&group='.$group;
  401:     }
  402:     $result .= '">'.&mt($message).'</a></h3>';
  403:     return $result;
  404: }
  405: 
  406: sub delete {
  407:     my ($r,$url,$group)=@_;
  408:     my @check;
  409:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  410:     $file_name = &prepend_group($file_name,$group);
  411:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  412:     my ($uname,$udom) = &get_name_dom($group);
  413:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  414:         $r->print ("The file is locked and cannot be deleted.<br />");
  415:         $r->print(&done('Back',$url,$group));
  416:     } else {
  417:         if (scalar(@files)) {
  418:             &open_form($r,$url);
  419:             $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
  420:             &close_form($r,$url,$group);
  421:         } else {
  422:             $r->print("No file was checked to delete.<br />");
  423:             $r->print(&done(undef,$url,$group));
  424:         }
  425:     }
  426: } 
  427: 
  428: sub delete_confirmed {
  429:     my ($r,$url,$group)=@_;
  430:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  431:     my $result;
  432:     my ($uname,$udom) = &get_name_dom($group);
  433:     my $port_path = &get_port_path($group);
  434:     foreach my $delete_file (@files) {
  435:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  436: 					       $env{'form.currentpath'}.
  437: 					       $delete_file);
  438:         if ($result ne 'ok') {
  439: 	$r->print('<span class="LC_error"> An error occured ('.$result.
  440: 		  ') while trying to delete '.&display_file(undef, $delete_file).'</span><br />');
  441:         }
  442:     }
  443:     $r->print(&done(undef,$url,$group));
  444: }
  445: 
  446: sub delete_dir {
  447:     my ($r,$url,$group)=@_;
  448:     &open_form($r,$url);
  449:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
  450:     &close_form($r,$url,$group);
  451: } 
  452: 
  453: sub delete_dir_confirmed {
  454:     my ($r,$url,$group)=@_;
  455:     my $directory_name = $env{'form.currentpath'};
  456:     $directory_name =~ s|/$||; # remove any trailing slash
  457:     my ($uname,$udom) = &get_name_dom($group);
  458:     my $namespace = &get_namespace($group);
  459:     my $port_path = &get_port_path($group);
  460:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  461: 					       $directory_name);
  462: 					       
  463:     if ($result ne 'ok') {
  464: 	$r->print('<span class="LC_error"> An error occured (dir) ('.$result.
  465: 		  ') while trying to delete '.$directory_name.'</span><br />');
  466:     } else {
  467:         # now remove from recent
  468: #        $r->print('<br /> removing '.$directory_name.'<br /');
  469:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
  470:         my @dirs = split m!/!, $directory_name;
  471:         
  472: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
  473:         $directory_name='/';
  474:         for (my $i=1; $i < (@dirs - 1); $i ++){
  475:             $directory_name .= $dirs[$i].'/';
  476:         }
  477:         $env{'form.currentpath'} = $directory_name;
  478:     }
  479:     $r->print(&done(undef,$url,$group));
  480: }
  481: 
  482: sub rename {
  483:     my ($r,$url,$group)=@_;
  484:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
  485:     my ($uname,$udom) = &get_name_dom($group);
  486:     $file_name = &prepend_group($file_name,$group);
  487:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  488:         $r->print ("The file is locked and cannot be renamed.<br />");
  489:         $r->print(&done(undef,$url,$group));
  490:     } else {
  491:         &open_form($r,$url);
  492:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  493:                    <input name="filenewname" type="input" size="50" />?</p>');
  494:         &close_form($r,$url,$group);
  495:     }
  496: }
  497: 
  498: sub rename_confirmed {
  499:     my ($r,$url,$group)=@_;
  500:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
  501:     my ($uname,$udom) = &get_name_dom($group);
  502:     my $port_path = &get_port_path($group);
  503:     if ($filenewname eq '') {
  504: 	$r->print('<span class="LC_error">'.
  505: 		  &mt("Error: no valid filename was provided to rename to.").
  506: 		  '</span><br />');
  507: 	$r->print(&done(undef,$url,$group));
  508: 	return;
  509:     } 
  510:     my $result=
  511: 	&Apache::lonnet::renameuserfile($uname,$udom,
  512:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
  513:             $port_path.$env{'form.currentpath'}.$filenewname);
  514:     if ($result ne 'ok') {
  515: 	$r->print('<span class="LC_error">'.
  516: 		  &mt('An errror occured ([_1]) while trying to rename [_2]'
  517: 		      .' to [_3]',$result,&display_file(),
  518: 		      &display_file('',$filenewname)).'</span><br />');
  519:     }
  520:     if ($filenewname ne $env{'form.filenewname'}) {
  521:         $r->print(&mt("The new file name was changed from:<br />[_1] to [_2]",
  522: 		      '<strong>'.&display_file('',$env{'form.filenewname'}).'</strong>',
  523: 		      '<strong>'.&display_file('',$filenewname).'</strong>'));
  524:     }
  525:     $r->print(&done(undef,$url,$group));
  526: }
  527: 
  528: sub display_access {
  529:     my ($r,$url,$group,$can_setacl) = @_;
  530:     my ($uname,$udom) = &get_name_dom($group);
  531:     my $file_name = $env{'form.currentpath'}.$env{'form.access'};
  532:     $file_name = &prepend_group($file_name,$group);
  533:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  534:                                                                         $uname);
  535:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group,$file_name);
  536:     my $aclcount = keys(%access_controls);
  537:     my $header = '<h3>'.&mt('Allowing others to retrieve portfolio file: [_1]',$env{'form.currentpath'}.$env{'form.access'}).'</h3>';
  538:     my $info .= 
  539: 	&mt('Access to this file by others can be set to be one or more of the following types: public, passphrase-protected or conditional.').
  540: 	'<br /><ul><li>'.
  541: 	&mt('Public files are available to anyone without the need for login.').
  542: 	'</li><li>'.
  543: 	&mt('Passphrase-protected files do not require log-in, but will require the viewer to enter the passphrase you set.').
  544: 	'</li>'.
  545: 	#'<li>'.
  546: 	#&mt('Conditional files are accessible to logged-in users with accounts in the LON-CAPA network, who satisfy the conditions you set.').
  547: 	#'<br />'.
  548: 	#&mt('The conditions can include affiliation with a particular course or group, or a user account in a specific domain.').
  549: 	#'<br />'.
  550: 	#&mt('Alternatively access can be granted to people with specific LON-CAPA usernames and domains.').
  551: 	'</li></ul>';
  552:     if ($can_setacl) {
  553:         &open_form($r,$url);
  554:         $r->print($header.$info);
  555:         &access_setting_table($r,$access_controls{$file_name});
  556:         my $button_text = {
  557:                         'continue' => &mt('Proceed'),
  558:                         'cancel' => &mt('Back to directory listing'),
  559:                       };
  560:         &close_form($r,$url,$group,$button_text);
  561:     } else {
  562:         $r->print($header);
  563:         if ($aclcount) {  
  564:             $r->print($info);
  565:         }
  566:         &view_access_settings($r,$url,$group,$access_controls{$file_name},
  567:                               $aclcount);
  568:     }
  569: }
  570: 
  571: sub view_access_settings {
  572:     my ($r,$url,$group,$access_controls,$aclcount) = @_;
  573:     my ($showstart,$showend);
  574:     my %todisplay;
  575:     foreach my $key (sort(keys(%{$access_controls}))) {
  576:         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  577:         $todisplay{$scope}{$key} = $$access_controls{$key};
  578:     }
  579:     if ($aclcount) {
  580:         $r->print(&mt('<h4>Current access controls defined for this file:</h4>'));
  581:         $r->print(&Apache::loncommon::start_data_table());
  582:         $r->print(&Apache::loncommon::start_data_table_header_row());
  583:         $r->print('<th>'.&mt('Access control').'</th><th>'.&mt('Dates available').
  584:                   '</th><th>'.&mt('Additional information').'</th>');
  585:         $r->print(&Apache::loncommon::end_data_table_header_row());
  586:         my $count = 1;
  587:         my $chg = 'none';
  588:         &build_access_summary($r,$count,$chg,%todisplay);
  589:         $r->print(&Apache::loncommon::end_data_table());
  590:     } else {
  591:         $r->print(&mt('No access control settings currently exist for this file.<br />' ));
  592:     }
  593:     my $group_arg;
  594:     if ($group) {
  595:         $group_arg = '&amp;group='.$group;
  596:     }
  597:     $r->print('<br /><a href="'.$url.'?currentpath='.$env{'form.currentpath'}.
  598:               $group_arg.'">'.&mt('Return to directory listing').'</a>');
  599:     return;
  600: }
  601: 
  602: sub build_access_summary {
  603:     my ($r,$count,$chg,%todisplay) = @_; 
  604:     my ($showstart,$showend);
  605:     my %scope_desc = (
  606:                       public => 'Public',
  607:                       guest => 'Passphrase-protected',
  608:                       domains => 'Conditional: domain-based',
  609:                       users => 'Conditional: user-based',
  610:                       course => 'Conditional: course-based',
  611:                       group => 'Conditional: group-based',
  612:                      );
  613:     my @allscopes = ('public','guest','domains','users','course','group');
  614:     foreach my $scope (@allscopes) {
  615:         if ((!(exists($todisplay{$scope}))) || (ref($todisplay{$scope}) ne 'HASH')) {
  616:             next;
  617:         }
  618:         foreach my $key (sort(keys(%{$todisplay{$scope}}))) {
  619:             if ($count) {
  620:                 $r->print(&Apache::loncommon::start_data_table_row());
  621:             }
  622:             my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  623:             my $content = $todisplay{$scope}{$key};
  624:             if ($chg eq 'delete') {
  625:                 $showstart = &mt('Deleted');
  626:                 $showend = $showstart;
  627:             } else {
  628:                 $showstart = localtime($start);
  629:                 if ($end == 0) {
  630:                     $showend = &mt('No end date');
  631:                 } else {
  632:                     $showend = localtime($end);
  633:                 }
  634:             }
  635:             $r->print('<td>'.&mt($scope_desc{$scope}));
  636:             if (($scope eq 'course') || ($scope eq 'group')) {
  637:                 if ($chg ne 'delete') {
  638:                     my $cid = $content->{'domain'}.'_'.$content->{'number'};
  639:                     my %course_description = &Apache::lonnet::coursedescription($cid);
  640:                     $r->print('<br />('.$course_description{'description'}.')');
  641:                 }
  642:             }
  643:             $r->print('</td><td>'.&mt('Start: ').$showstart.
  644:                   '<br />'.&mt('End: ').$showend.'</td><td>');
  645:             if ($chg ne 'delete') {
  646:                 if ($scope eq 'guest') {
  647:                     $r->print(&mt('Passphrase').': '.$content->{'password'});
  648:                 } elsif ($scope eq 'course' || $scope eq 'group') {
  649:                     $r->print('<table><tr>');
  650:                     $r->print('<th>'.&mt('Roles').'</th><th>'.
  651:                           &mt('Access').'</th><th>'.
  652:                                           &mt('Sections').'</th>');
  653:                     if ($scope eq 'course') {
  654:                         $r->print('<th>'.&mt('Groups').'</th>');
  655:                     } else {
  656:                         $r->print('<th>'.&mt('Teams').'</th>');
  657:                     }
  658:                     $r->print('</tr>');
  659:                     foreach my $id (sort(keys(%{$content->{'roles'}}))) {
  660:                         $r->print('<tr>');
  661:                         foreach my $item ('role','access','section','group') {
  662:                             $r->print('<td>');
  663:                             if ($item eq 'role') {
  664:                                 my $ucscope = $scope;
  665:                                 $ucscope =~ s/^(\w)/uc($1)/e;
  666:                                 my $role_output;
  667:                                 foreach my $role (@{$content->{'roles'}{$id}{$item}}) {
  668:                                     if ($role eq 'all') {
  669:                                         $role_output .= $role.',';
  670:                                     } elsif ($role =~ /^cr/) {
  671:                                         $role_output .= (split('/',$role))[3].',';
  672:                                     } else {
  673:                                         $role_output .= &Apache::lonnet::plaintext($role,$ucscope).',';
  674:                                     }
  675:                                 }
  676:                                 $role_output =~ s/,$//;
  677:                                 $r->print($role_output);
  678:                             } else {
  679:                                 $r->print(join(',',@{$content->{'roles'}{$id}{$item}}));
  680:                             }
  681:                             $r->print('</tr>');
  682:                         }
  683: 			$r->print("</table>");
  684:                     }
  685: 		    $r->print("</tr></table>");
  686:                 } elsif ($scope eq 'domains') {
  687:                     $r->print(&mt('Domains: ').join(',',@{$content->{'dom'}}));
  688:                 } elsif ($scope eq 'users') {
  689:                     my $curr_user_list = &sort_users($content->{'users'});
  690:                     $r->print(&mt('Users: ').$curr_user_list);
  691:                 } else {
  692:                     $r->print('&nbsp;');
  693:                 }
  694:             } else {
  695:                 $r->print('&nbsp;');
  696:             }
  697:             $r->print('</td>');
  698:             $r->print(&Apache::loncommon::end_data_table_row());
  699:             $count ++;
  700:         }
  701:     }
  702: }
  703: 
  704: 
  705: sub update_access {
  706:     my ($r,$url,$group) = @_;
  707:     my $totalprocessed = 0;
  708:     my %processing;
  709:     my %title  = (
  710:                          'activate' => 'New control(s) added',
  711:                          'delete'   => 'Existing control(s) deleted',
  712:                          'update'   => 'Existing control(s) modified',
  713:                      );
  714:     my $changes;
  715:     foreach my $chg (sort(keys(%title))) {     
  716:         @{$processing{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
  717:         $totalprocessed += @{$processing{$chg}};
  718:         foreach my $num (@{$processing{$chg}}) {
  719:             my $scope = $env{'form.scope_'.$num};
  720:             my ($start,$end) = &get_dates_from_form($num);
  721:             my $newkey = $num.':'.$scope.'_'.$end.'_'.$start;
  722:             if ($chg eq 'delete') {
  723:                 $$changes{$chg}{$newkey} = 1;
  724:             } else {
  725:                 $$changes{$chg}{$newkey} = 
  726:                             &build_access_record($num,$scope,$start,$end,$chg);
  727:             }
  728:         }
  729:     }
  730:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  731:     $r->print('<h3>'.&mt('Allowing others to retrieve portfolio file: [_1]',
  732:               $file_name).'</h3>'."\n");
  733:     $file_name = &prepend_group($file_name,$group);
  734:     my ($uname,$udom) = &get_name_dom($group);
  735:     my ($errors,$outcome,$deloutcome,$new_values,$translation);
  736:     if ($totalprocessed) {
  737:         ($outcome,$deloutcome,$new_values,$translation) =
  738:         &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
  739:                                                 $uname);
  740:     }
  741:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  742:                                                                        $uname);
  743:     my %access_controls = 
  744: 	&Apache::lonnet::get_access_controls($current_permissions,
  745: 					     $group,$file_name);
  746:     if ($totalprocessed) {
  747:         if ($outcome eq 'ok') {
  748:             my $updated_controls = $access_controls{$file_name};
  749:             my ($showstart,$showend);
  750:             $r->print(&Apache::loncommon::start_data_table());
  751:             $r->print(&Apache::loncommon::start_data_table_header_row());
  752:             $r->print('<th>'.&mt('Type of change').'</th><th>'.
  753:                       &mt('Access control').'</th><th>'.&mt('Dates available').
  754:                       '</th><th>'.&mt('Additional information').'</th>');
  755:             $r->print(&Apache::loncommon::end_data_table_header_row());
  756:             foreach my $chg (sort(keys(%processing))) {
  757:                 if (@{$processing{$chg}} > 0) {
  758:                     if ($chg eq 'delete') {
  759:                         if (!($deloutcome eq 'ok')) {
  760:                             $errors .='<span class="LC_error">'.
  761: 				&mt('A problem occurred deleting access controls: [_1]',$deloutcome).
  762: 				'</span>';
  763:                             next;
  764:                         }
  765:                     }
  766:                     my $numchgs = @{$processing{$chg}};
  767:                     $r->print(&Apache::loncommon::start_data_table_row());
  768:                     $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
  769:                               '.</td>');
  770:                     my $count = 0;
  771:                     my %todisplay;
  772:                     foreach my $key (sort(keys(%{$$changes{$chg}}))) {
  773:                         my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  774:                         my $newkey = $key;
  775:                         if ($chg eq 'activate') {
  776:                             $newkey =~ s/^(\d+)/$$translation{$1}/;
  777:                         }
  778:                         $todisplay{$scope}{$newkey} = $$updated_controls{$newkey};
  779:                     }
  780:                     &build_access_summary($r,$count,$chg,%todisplay);  
  781:                 }
  782:             }
  783:             $r->print(&Apache::loncommon::end_data_table());
  784:         } else {
  785:             if ((@{$processing{'activate'}} > 0) || (@{$processing{'update'}} > 0)) {
  786:                 $errors .= '<span class="LC_error">'.
  787: 		    &mt('A problem occurred storing access control settings: [_1]',$outcome).
  788: 		    '</span>';
  789:             }
  790:         }
  791:         if ($errors) { 
  792:             $r->print($errors);
  793:         }
  794:     }
  795:     my $allnew = 0;
  796:     my $totalnew = 0;
  797:     my $status = 'new';
  798:     my ($firstitem,$lastitem);
  799:     foreach my $newitem ('course','group','domains','users') {
  800:         $allnew += $env{'form.new'.$newitem};
  801:     }
  802:     if ($allnew > 0) {
  803:         my $now = time;
  804:         my $then = $now + (60*60*24*180); # six months approx.
  805:         &open_form($r,$url,$group);
  806:         foreach my $newitem ('course','group','domains','users') {
  807:             if ($env{'form.new'.$newitem} > 0) {
  808:                 $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 />');
  809:                 $firstitem = $totalnew;
  810:                 $lastitem = $totalnew + $env{'form.new'.$newitem};
  811:                 $totalnew = $lastitem;
  812:                 my @numbers;   
  813:                 for (my $i=$firstitem; $i<$lastitem; $i++) {
  814:                     push (@numbers,$i);
  815:                 }
  816:                 &display_access_row($r,$status,$newitem,\@numbers,
  817:                                     $access_controls{$file_name},$now,$then);
  818:             }
  819:         }
  820:         &close_form($r,$url,$group);
  821:     } else {
  822:         my $group_arg;
  823:         if ($group) {
  824:             $group_arg = '&amp;group='.$group;
  825:         }
  826:         $r->print('<br /><a href="'.$url.'?access='.$env{'form.selectfile'}.
  827:                   '&amp;currentpath='.$env{'form.currentpath'}.$group_arg.'">'.
  828:                    &mt('Display all access settings for this file').'</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,$group) = @_;
 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: sub upload {
 1442:     my ($r,$url,$group)=@_;
 1443:     my $fname=$env{'form.uploaddoc.filename'};
 1444:     my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
 1445:     my $disk_quota = 20000; # expressed in k
 1446:     if (defined($group)) {
 1447:         my $grp_quota = &get_group_quota($group); # quota expressed in k 
 1448:         if ($grp_quota ne '') {
 1449:             $disk_quota = $grp_quota;
 1450:         } else {
 1451:             $disk_quota = 0;
 1452:         }
 1453:     }
 1454:     $fname=&Apache::lonnet::clean_filename($fname);
 1455: 
 1456:     my $portfolio_root=&get_portfolio_root($group);
 1457:     my ($uname,$udom) = &get_name_dom($group);
 1458:     my $port_path = &get_port_path($group);
 1459:     # Fixme --- Move the checking for existing file to LOND error return
 1460:     my @dir_list=&get_dir_list($portfolio_root,$group);
 1461:     my $found_file = 0;
 1462:     my $locked_file = 0;
 1463:     foreach my $line (@dir_list) {
 1464:         my ($file_name)=split(/\&/,$line,2);
 1465:         if ($file_name eq $fname){
 1466:             $file_name = $env{'form.currentpath'}.$file_name;
 1467:             $file_name = &prepend_group($file_name,$group);
 1468:             $found_file = 1;
 1469:             if (defined($group)) {
 1470:                 $file_name = $group.'/'.$file_name;
 1471:             }
 1472:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
 1473:                 $locked_file = 1;
 1474:             } 
 1475:         }
 1476:     }
 1477:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root);
 1478:     if (($current_disk_usage + $filesize) > $disk_quota){
 1479:         $r->print('<span class="LC_error">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes)</strong>. Disk quota will be exceeded.</span>'.
 1480:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
 1481:         $r->print(&done('Back',$url,$group));
 1482:     } 
 1483:     elsif ($found_file){
 1484:         if ($locked_file){
 1485:             $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>'.
 1486:                   '<br />You will be able to rename or delete existing '.$fname.' after a grade has been assigned.');
 1487:             $r->print(&done('Back',$url,$group));      
 1488:         } else {   
 1489:             $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>'.
 1490:                   '<br />To upload, rename or delete existing '.$fname.' in '.$port_path.$env{'form.currentpath'});
 1491:             $r->print(&done('Back',$url,$group));
 1492:         }
 1493:     } else {
 1494:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
 1495: 	        	 $port_path.$env{'form.currentpath'});
 1496:         if ($result !~ m|^/uploaded/|) {
 1497:             $r->print('<span class="LC_error">'.'An errror occured ('.$result.
 1498: 	              ') while trying to upload '.&display_file().'</span><br />');
 1499: 	    $r->print(&done('Back',$url,$group));
 1500:         } else {
 1501:             $r->print(&done(undef,$url,$group));
 1502:         }
 1503:     }
 1504: }
 1505: 
 1506: sub lock_info {
 1507:     my ($r,$url,$group) = @_;
 1508:     my ($uname,$udom) = &get_name_dom($group);
 1509:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
 1510:                                                                        $uname);
 1511:     my $file_name = $env{'form.lockinfo'};
 1512:     $file_name = &prepend_group($file_name,$group);
 1513:     if (defined($file_name) && defined($$current_permissions{$file_name})) {
 1514:         foreach my $array_item (@{$$current_permissions{$file_name}}) {
 1515:             if (ref($array_item) eq 'ARRAY') {
 1516:                 my $filetext;
 1517:                 if (defined($group)) {
 1518:                     $filetext = '<strong>'.$env{'form.lockinfo'}.
 1519:                                     '</strong> (group: '.$group.')'; 
 1520:                 } else {
 1521:                     $filetext = '<strong>'.$file_name.'</strong>';
 1522:                 } 
 1523:                 $r->print(&mt('[_1] was submitted in response to problem: ',
 1524:                               $filetext).
 1525:                           '<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
 1526:                           '</strong><br />');
 1527:                 my %course_description = &Apache::lonnet::coursedescription($$array_item[1]);
 1528:                 $r->print(&mt('In the course: <strong>[_1]</strong><br />',
 1529:                               $course_description{'description'}));
 1530:                 # $r->print('the third is '.$$array_item[2].'<br>');
 1531:                 # $r->print("item is $$array_item[0]<br> and $$array_item[0]");
 1532:             }
 1533:         }
 1534:     }
 1535:     $r->print(&done('Back',$url,$group));
 1536:     return 'ok';
 1537: }
 1538: sub createdir {
 1539:     my ($r,$url,$group)=@_;
 1540:     my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
 1541:     if ($newdir eq '') {
 1542:     	$r->print('<span class="LC_error">'.
 1543: 	    	  &mt("Error: no directory name was provided.").
 1544: 		      '</span><br />');
 1545: 	    $r->print(&done(undef,$url,$group));
 1546: 	    return;
 1547:     }
 1548:     my $portfolio_root = &get_portfolio_root($group); 
 1549:     my @dir_list=&get_dir_list($portfolio_root,$group);
 1550:     my $found_file = 0;
 1551:     foreach my $line (@dir_list) {
 1552:         my ($filename)=split(/\&/,$line,2);
 1553:         if ($filename eq $newdir){
 1554:             $found_file = 1;
 1555:         }
 1556:     }
 1557:     if ($found_file){
 1558:     	    $r->print('<span class="LC_error">'.'Unable to create a directory named <strong>'.$newdir.
 1559:     	            ' </strong>a file or directory by that name already exists.</span><br />');
 1560:     } else {
 1561:         my ($uname,$udom) = &get_name_dom($group);
 1562:         my $port_path = &get_port_path($group);
 1563:         my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
 1564: 	         $port_path.$env{'form.currentpath'}.$newdir);
 1565:         if ($result ne 'ok') {
 1566:     	    $r->print('<span class="LC_error">'.'An errror occured ('.$result.
 1567: 	    	      ') while trying to create a new directory '.&display_file().'</span><br />');
 1568:         }
 1569:     }
 1570:     if ($newdir ne $env{'form.newdir'}) {
 1571:         $r->print("The new directory name was changed from:<br /><strong>".$env{'form.newdir'}."</strong> to <strong>$newdir </strong>");  
 1572:     }
 1573:     $r->print(&done(undef,$url,$group));
 1574: }
 1575: 
 1576: sub get_portfolio_root {
 1577:     my ($group) = @_;
 1578:     my ($portfolio_root,$udom,$uname,$path);
 1579:     ($uname,$udom) = &get_name_dom($group);
 1580:     if (defined($group)) {
 1581:         $path = '/userfiles/groups/'.$group.'/portfolio';
 1582:     } else {
 1583:         $path = '/userfiles/portfolio';
 1584:     }
 1585:     return (&Apache::loncommon::propath($udom,$uname).$path);
 1586: }
 1587: 
 1588: sub get_group_quota {
 1589:     my ($group) = @_;
 1590:     my $group_quota; 
 1591:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1592:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1593:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
 1594:     if (%curr_groups) {
 1595:         my %group_info =  &Apache::longroup::get_group_settings(
 1596:                                                     $curr_groups{$group});
 1597:         $group_quota = $group_info{'quota'}; #expressed in Mb
 1598:         if ($group_quota) {
 1599:             $group_quota = 1000 * $group_quota; #expressed in k
 1600:         } 
 1601:     }
 1602:     return $group_quota;
 1603: } 
 1604: 
 1605: sub get_dir_list {
 1606:     my ($portfolio_root,$group) = @_;
 1607:     my ($uname,$udom) = &get_name_dom($group);
 1608:     return &Apache::lonnet::dirlist($env{'form.currentpath'},
 1609:                                           $udom,$uname,$portfolio_root);
 1610: }
 1611: 
 1612: sub get_name_dom {
 1613:     my ($group) = @_;
 1614:     my ($uname,$udom);
 1615:     if (defined($group)) {
 1616:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1617:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
 1618:     } else {
 1619:         $udom = $env{'user.domain'};
 1620:         $uname = $env{'user.name'};
 1621:     }
 1622:     return ($uname,$udom);
 1623: }
 1624: 
 1625: sub prepend_group {
 1626:     my ($filename,$group) = @_;
 1627:     if (defined($group)) {
 1628:         $filename = $group.$filename;
 1629:     }
 1630:     return $filename;
 1631: }
 1632: 
 1633: sub get_namespace {
 1634:     my ($group) = @_;
 1635:     my $namespace = 'portfolio';
 1636:     if (defined($group)) {
 1637:         my ($uname,$udom) = &get_name_dom($group);
 1638:         $namespace .= '_'.$udom.'_'.$uname.'_'.$group;
 1639:     }
 1640:     return $namespace;
 1641: }
 1642: 
 1643: sub get_port_path {
 1644:     my ($group) = @_;
 1645:     my $port_path;
 1646:     if (defined($group)) {
 1647:        $port_path = "groups/$group/portfolio";
 1648:     } else {
 1649:        $port_path = 'portfolio';
 1650:     }
 1651:     return $port_path;
 1652: }
 1653: 
 1654: sub missing_priv {
 1655:     my ($r,$url,$priv,$group) = @_;
 1656:     my $longtext = {
 1657:                       upload => 'upload files',
 1658:                       delete => 'delete files',
 1659:                       rename => 'rename files',
 1660:                       setacl => 'set access controls for files',
 1661:                    };
 1662:     my $escpath = &HTML::Entities::encode($env{'form.currentpath'},'&<>"');
 1663:     my $rtnlink = '<a href="'.$url;
 1664:     if ($url =~ /\?/) {
 1665:         $rtnlink .= '&';
 1666:     } else {
 1667:         $rtnlink .= '?';
 1668:     }
 1669:     $rtnlink .= 'currentpath='.$escpath;
 1670:     $r->print(&mt('<h3>Action disallowed</h3>'));
 1671:     $r->print(&mt('You do not have sufficient privileges to [_1] ',
 1672:                   $longtext->{$priv}));
 1673:     if ($group) {
 1674:         $r->print(&mt("in the group's file repository."));
 1675:         $rtnlink .= '&group='.$group;
 1676:     } else {
 1677:         $r->print(&mt('in this portfolio.'));
 1678:     }
 1679:     $rtnlink .= '">'.&mt('Return to directory listing page').'</a>';
 1680:     $r->print('<br />'.$rtnlink);
 1681:     $r->print(&Apache::loncommon::end_page());
 1682:     return;
 1683: }
 1684: 
 1685: sub handler {
 1686:     # this handles file management
 1687:     my $r = shift;
 1688:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1689:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
 1690: 	  'fieldname','mode','rename','continue','group','access','setnum',
 1691:           'cnum','cdom','type','setroles']);
 1692:     my ($uname,$udom,$portfolio_root,$url,$group,$caller,$title);
 1693:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
 1694:         $url = $1.$2;
 1695:         $caller = $2;
 1696:     }
 1697:     my ($can_modify,$can_delete,$can_upload,$can_setacl);
 1698:     if ($caller eq 'coursegrp_portfolio') {
 1699:     #  Needs to be in a course
 1700:         if (! ($env{'request.course.fn'})) {
 1701:         # Not in a course
 1702:             $env{'user.error.msg'}=
 1703:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
 1704:             return HTTP_NOT_ACCEPTABLE;
 1705:         }
 1706:         my $earlyout = 0;
 1707:         my $view_permission = &Apache::lonnet::allowed('vcg',
 1708:                                                 $env{'request.course.id'});
 1709:         $group = $env{'form.group'};
 1710:         $group =~ s/\W//g;
 1711:         if ($group) {
 1712:             ($uname,$udom) = &get_name_dom($group);
 1713:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
 1714: 							       $group); 
 1715:             if (%curr_groups) {
 1716:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
 1717:                                       $env{'request.course.id'}.'/'.$group))) {
 1718:                     $portfolio_root = &get_portfolio_root($group);
 1719:                 } else {
 1720:                     $r->print('You do not have the privileges required to access the shared files space for this group.');
 1721:                     $earlyout = 1;
 1722:                 }
 1723:             } else {
 1724:                 $r->print('Not a valid group for this course');
 1725:                 $earlyout = 1;
 1726:             }
 1727:             $title = &mt('Group files').' for '.$group; 
 1728:         } else {
 1729:             $r->print('Invalid group');
 1730:             $earlyout = 1;
 1731:         }
 1732:         if ($earlyout) { return OK; }
 1733:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
 1734:             $can_modify = 1;
 1735:             $can_delete = 1;
 1736:             $can_upload = 1;
 1737:             $can_setacl = 1;
 1738:         } else {
 1739:             if (&Apache::lonnet::allowed('agf',$env{'request.course.id'}.'/'.$group)) {
 1740:                 $can_setacl = 1;
 1741:             }
 1742:             if (&Apache::lonnet::allowed('ugf',$env{'request.course.id'}.'/'.$group)) {
 1743:                 $can_upload = 1;
 1744:             }
 1745:             if (&Apache::lonnet::allowed('mgf',$env{'request.course.id'}.'/'.$group)) {
 1746:                 $can_modify = 1;
 1747:             }
 1748:             if (&Apache::lonnet::allowed('dgf',$env{'request.course.id'}.'/'.$group)) {
 1749:                 $can_delete = 1;
 1750:             }
 1751:         }
 1752:     } else {
 1753:         ($uname,$udom) = &get_name_dom();
 1754:         $portfolio_root = &get_portfolio_root();
 1755:         $title = &mt('Portfolio Manager');
 1756:         $can_modify = 1;
 1757:         $can_delete = 1;
 1758:         $can_upload = 1;
 1759:         $can_setacl = 1;
 1760:     }
 1761: 
 1762:     &Apache::loncommon::no_cache($r);
 1763:     &Apache::loncommon::content_type($r,'text/html');
 1764:     $r->send_http_header;
 1765:     # Give the LON-CAPA page header
 1766:     if ($env{"form.mode"} eq 'selectfile'){
 1767:         $r->print(&Apache::loncommon::start_page($title,undef,
 1768: 						 {'only_body' => 1}));
 1769:     } elsif ($env{'form.action'} eq 'rolepicker') {
 1770:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
 1771:                                                  {'no_nav_bar'  => 1, }));
 1772:     } else {
 1773:         $r->print(&Apache::loncommon::start_page($title));
 1774:     }
 1775:     $r->rflush();
 1776: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
 1777:    	    $r->print('<span class="LC_error">'.
 1778: 		      'No file was selected to upload.'.
 1779: 		      'To upload a file, click <strong>Browse...</strong>'.
 1780: 		      ', select a file, then click <strong>Upload</strong>.'.
 1781: 		      '</span>');
 1782: 	}
 1783:     if ($env{'form.meta'}) {
 1784:         &open_form($r,$url);
 1785: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
 1786:         $r->print('Edit the meta data<br />');
 1787:         &close_form($r,$url,$group);
 1788:     }
 1789:     if ($env{'form.store'}) {
 1790:     }
 1791: 
 1792:     if ($env{'form.uploaddoc.filename'}) {
 1793:         if ($can_upload) {
 1794: 	    &upload($r,$url,$group);
 1795:         } else {
 1796:             &missing_priv($r,$url,'upload',$group),
 1797:         }
 1798:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
 1799:         if ($can_delete) {
 1800: 	    &delete_confirmed($r,$url,$group);
 1801:         } else {
 1802:             &missing_priv($r,$url,'delete',$group);
 1803:         }
 1804:     } elsif ($env{'form.action'} eq 'delete') {
 1805:         if ($can_delete) {
 1806: 	    &delete($r,$url,$group);
 1807:         } else {
 1808:             &missing_priv($r,$url,'delete',$group);
 1809:         }
 1810:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
 1811:         if ($can_delete) {
 1812: 	    &delete_dir_confirmed($r,$url,$group);
 1813:         } else {
 1814:             &missing_priv($r,$url,'delete',$group);
 1815:         }
 1816:     } elsif ($env{'form.action'} eq 'deletedir') {
 1817:         if ($can_delete) {
 1818: 	    &delete_dir($r,$url,$group);
 1819:         } else {
 1820:             &missing_priv($r,$url,'delete',$group);
 1821:         }
 1822:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
 1823:         if ($can_modify) {
 1824: 	    &rename_confirmed($r,$url,$group);
 1825:         } else {
 1826:             &missing_priv($r,$url,'rename',$group);
 1827:         }
 1828:     } elsif ($env{'form.rename'}) {
 1829:         $env{'form.selectfile'} = $env{'form.rename'};
 1830:         $env{'form.action'} = 'rename';
 1831:         if ($can_modify) {
 1832: 	    &rename($r,$url,$group);
 1833:         } else {
 1834:             &missing_priv($r,$url,'rename',$group);
 1835:         }
 1836:     } elsif ($env{'form.access'}) {
 1837:         $env{'form.selectfile'} = $env{'form.access'};
 1838:         $env{'form.action'} = 'chgaccess';
 1839:         &display_access($r,$url,$group,$can_setacl);
 1840:     } elsif ($env{'form.action'} eq 'chgaccess') {
 1841:         if ($can_setacl) {
 1842:             &update_access($r,$url,$group);
 1843:         } else {
 1844:             &missing_priv($r,$url,'setacl',$group);
 1845:         }
 1846:     } elsif ($env{'form.action'} eq 'rolepicker') {
 1847:         if ($can_setacl) { 
 1848:             &role_options_window($r);
 1849:         } else {
 1850:             &missing_priv($r,$url,'setacl',$group);
 1851:         }
 1852:     } elsif ($env{'form.createdir'}) {
 1853:         if ($can_upload) {
 1854: 	    &createdir($r,$url,$group);
 1855:         } else {
 1856:             &missing_priv($r,$url,'upload',$group);
 1857:         }
 1858:     } elsif ($env{'form.lockinfo'}) {
 1859:         &lock_info($r,$url,$group);
 1860:     } else {
 1861: 	my $current_path='/';
 1862: 	if ($env{'form.currentpath'}) {
 1863: 	    $current_path = $env{'form.currentpath'};
 1864: 	}
 1865:         my @dir_list=&get_dir_list($portfolio_root,$group);
 1866: 	if ($dir_list[0] eq 'no_such_dir'){
 1867: 	    # two main reasons for this:
 1868:             #    1) never been here, so directory structure not created
 1869: 	    #    2) back-button navigation after deleting a directory
 1870: 	    if ($current_path eq '/'){
 1871: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
 1872: 					       &get_port_path($group));
 1873: 	    } else {
 1874:                 # some directory that snuck in get rid of the directory
 1875:                 # from the recent pulldown, just in case
 1876: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
 1877: 						      [$current_path]);
 1878: 		$current_path = '/'; # force it back to the root        
 1879: 	    }
 1880: 	    # now grab the directory list again, for the first time
 1881: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
 1882: 					    $udom,$uname,$portfolio_root);
 1883:         }
 1884: 	# need to know if directory is empty so it can be removed if desired
 1885: 	my $is_empty=(@dir_list == 2);
 1886: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,$group,
 1887:                         $can_upload);
 1888:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group,
 1889:                            $can_upload,$can_modify,$can_delete,$can_setacl);
 1890: 	$r->print(&Apache::loncommon::end_page());
 1891:     }
 1892:     return OK;
 1893: }
 1894: 
 1895: 1;
 1896: __END__

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