File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.126: download - view: text, annotated - select for diffs
Wed Jun 28 23:38:09 2006 UTC (17 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
File sizes for uploads to course group portfolios may not exceed quota set for group's file repository. When creating a course, a quota can be specified.  The sum of quotas assigned to all groups in a course can not exceed the total quota defined for the course (when the course was created).  - client side and server side enforcement still needed here.  Portfolio permissions modified to allow any user with group creation rights in a course access to all course group portfolio functions.  All uses of words: group, Group, course and Course in "Course Groups Manager" now replaced with context-sensitive terminology provided by loncommon::course_type() and loncommon::group_term() which will supply appropriate text once a decision is made on Group vs Group. Currently "ANGEL-like Discussion Groups" are called Groups, regular courses are called Courses; and within regular courses sectionesque units are called groups, and within Groups they are called teams.

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

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