File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.116: download - view: text, annotated - select for diffs
Thu Jun 22 19:01:30 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- switching file browser to CSS (some of BUG#3925)
- errors now use span class="LC_error">
- filenames now using span class="LC_filename"

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

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